ADC Example project for the STM32F7 Discovery

Sysprogs forums Forums VisualGDB ADC Example project for the STM32F7 Discovery

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #8982
    walt71100
    Participant

    I’d like to setup the ADC on the STM32F7 Discovery, and have been following a similar example described here: http://visualgdb.com/tutorials/arm/stm32/adc/

    With the exception of  a few minor changes, the following code is a all VisualGDB example code (hybrid between the STM32F7 samples, and the STM32F4 for which the ADC example was done).

    #include <stm32f7xx_hal.h>
    #include <stm32f7xx_hal_adc.h>

    #include <stm32746g_discovery.h>
    #include <stm32746g_discovery_lcd.h>
    #include <stm32746g_discovery_ts.h>

    #define HAL_TIM_MODULE_ENABLED

    #ifdef __cplusplus
    extern “C”
    {
    #endif

    static CSysObj m_TheSysObject; // The root/main object
    void SysTick_Handler(void)
    {
    HAL_IncTick();
    HAL_SYSTICK_IRQHandler();
    }

    /**
    * @brief System Clock Configuration
    * The system Clock is configured as follow :
    * System Clock source = PLL (HSE)
    * SYSCLK(Hz) = 216000000
    * HCLK(Hz) = 216000000
    * AHB Prescaler = 1
    * APB1 Prescaler = 4
    * APB2 Prescaler = 2
    * HSE Frequency(Hz) = 25000000
    * PLL_M = 25
    * PLL_N = 432
    * PLL_P = 2
    * PLL_Q = 9
    * VDD(V) = 3.3
    * Main regulator output voltage = Scale1 mode
    * Flash Latency(WS) = 7
    * @param None
    * @retval None
    */
    void SystemClock_Config(void)
    {
    RCC_ClkInitTypeDef RCC_ClkInitStruct;
    RCC_OscInitTypeDef RCC_OscInitStruct;
    HAL_StatusTypeDef ret = HAL_OK;

    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

    /* Enable HSE Oscillator and activate PLL with HSE as source */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLM = 25;
    RCC_OscInitStruct.PLL.PLLN = 432;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
    RCC_OscInitStruct.PLL.PLLQ = 9;

    ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
    if (ret != HAL_OK) {
    while (1) {
    ;}
    }

    /* Activate the OverDrive to reach the 216 MHz Frequency */
    ret = HAL_PWREx_EnableOverDrive();
    if (ret != HAL_OK) {
    while (1) {
    ;}
    }

    /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
    RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

    ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);
    if (ret != HAL_OK) {
    while (1) {
    ;}
    }
    }
    // /*

    ADC_HandleTypeDef g_AdcHandle;

    void ConfigureADC()
    {
    GPIO_InitTypeDef gpioInit;

    __GPIOC_CLK_ENABLE();
    __ADC3_CLK_ENABLE();

    gpioInit.Pin = GPIO_PIN_1;
    gpioInit.Mode = GPIO_MODE_ANALOG;
    gpioInit.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOC, &gpioInit);

    HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(ADC_IRQn);

    ADC_ChannelConfTypeDef adcChannel;

    g_AdcHandle.Instance = ADC3;

    g_AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
    g_AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
    g_AdcHandle.Init.ScanConvMode = DISABLE;
    g_AdcHandle.Init.ContinuousConvMode = ENABLE;
    g_AdcHandle.Init.DiscontinuousConvMode = DISABLE;
    g_AdcHandle.Init.NbrOfDiscConversion = 0;
    g_AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
    g_AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
    g_AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
    g_AdcHandle.Init.NbrOfConversion = 1;
    g_AdcHandle.Init.DMAContinuousRequests = ENABLE;
    g_AdcHandle.Init.EOCSelection = DISABLE;

    //HAL_ADC_Init(&g_AdcHandle);
    //HAL_ADC_MspInit(&g_AdcHandle);

    adcChannel.Channel = ADC_CHANNEL_0;
    adcChannel.Rank = 1;
    adcChannel.SamplingTime = ADC_SAMPLETIME_480CYCLES;
    adcChannel.Offset = 0;

    if (HAL_ADC_ConfigChannel(&g_AdcHandle, &adcChannel) != HAL_OK)
    {
    asm(“bkpt 255”);
    }
    }

    // */
    int main(void)
    {

    int g_ADCValue;
    int g_MeasurementNumber;
    // Initialize Hardware
    RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
    SCB_EnableICache();
    SCB_EnableDCache();
    HAL_Init();
    SystemClock_Config();

    // /*
    ConfigureADC();

    HAL_ADC_Start(&g_AdcHandle);
    for (;;)
    {
    if (HAL_ADC_PollForConversion(&g_AdcHandle, 1000000) == HAL_OK)
    {
    g_ADCValue = HAL_ADC_GetValue(&g_AdcHandle);
    g_MeasurementNumber++;
    }
    }

    // */

    return 0;
    }

    #ifdef __cplusplus
    }
    #endif

    The compiler complains about undefined reference to a few functions starting with “HAL_ADC_…” however the headers are included for these functions, and MSVS jumps right to it when F12 is pressed. I looked into re-generating the STM32CubeMX, however I didn’t end up importing the generated C.

    Are there other examples I can look at for ADC on the F7Discovery?

     

     

    #8991
    walt71100
    Participant

    If needed, I can add other information.

    Is there a direct ADC example for the STM32f7?

    #8994
    support
    Keymaster

    Hi,

    We don’t have a specific ADC example for STM32F7, however if you get errors like ‘undefined ADC_xxx’, please try searching the solution for the definitions of those functions and check if they are grayed out by IntelliSense.

    If they are, most likely you are missing some configuration definition like HAL_ENABLE_ADC. Checking the code around the grayed out definitions should explain why they are not being compiled.

    If this does note help, please share the error messages you are getting and let us know if the function definitions are grayed out.

    #8995
    walt71100
    Participant

    Yes, I was just returning here to post an answer my own question when I found your response.

    It turned out I didn’t have the ADC module enabled in the HAL.

    Thanks,

    Walt

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.