ECE471/571 (RTOS) STM32 C Example - ADC using DMA and started from a timer

In STM32CubeMX

  1. Initialize all peripherals with in default mode? - yes
  2. find PB0, PB7, PB14, PC13
  3. find TIM2 - Timer2
  4. find ADC1
  5. enable UART3
  6. disable Ethernet (optional)
  7. disable USB_OTG_FS (optional)

Adapt and compile a project (example 1)

  1. Open source/main.c
  2. Locate int main() function
  3. Inspect the GPIO initialization called from main()
  4. Locate a good place to insert a custom include file
  5. Insert your code ** HERE **
    /* USER CODE BEGIN Includes */
        ** HERE **
    /* USER CODE END Includes */
    
  6. We are using the C-string library, we need to include its header
    #include <string.h>
    #include <stdio.h>
    
  7. Locate a good place to insert global variables
  8. Insert your code ** HERE **
    /* USER CODE BEGIN PV */
        ** HERE **
    /* USER CODE END PV */
    
  9. We are using a flag to pass info that global array of ADC results is ready, and a memory buffer for DMA
    uint32_t volatile flag_adc1 = 0; // must be volatile!
    uint32_t raw_adc[3], buffer[3];
    
  10. Locate a good place to insert a callback function
  11. Insert your code ** HERE **
    /* USER CODE BEGIN 0 */
        ** HERE **
    /* USER CODE END 0 */
    
  12. Callback function code for ** HERE **
    void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
    {
      if ( hadc == &hadc1 ) { // check which ADC is being serviced
        for (int i =0; i<3; i++) {
         raw_adc[i] = buffer[i];
        }
        flag_adc1 = 1;
      }
    }
    
    void ADC_Start_From_ISR() {
      HAL_ADC_Start_DMA(&hadc1, buffer, 3);
    }
    
  13. Locate the initialization in main()
  14. Insert your code ** HERE **
      MX_ADC1_Init();
      MX_TIM2_Init();
      /* USER CODE BEGIN 2 */
          ** HERE **
      /* USER CODE END 2 */
    
  15. Code for starting the timer and enabling timer interrupt
      HAL_TIM_Base_Start_IT(&htim2);
    
  16. Locate the forever loop in main()
  17. Insert your code ** HERE **
        /* USER CODE BEGIN WHILE */
        while (1)
        {
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
            ** HERE **
        }
        /* USER CODE END 3 */
    
  18. Code for ** HERE **
        while ( flag_adc1!=1 )
          ;
        flag_adc1 = 0; // indicates that we consumed the data
    
        uint32_t ch5_voltage = (raw_adc[0] * 3300U) / 4095U;
        uint32_t ch6_voltage = (raw_adc[1] * 3300U) / 4095U;
        uint32_t cht_voltage = (raw_adc[2] * 3300U) / 4095U;
        uint32_t temperature = 25 + (cht_voltage*10 - 7600U) /25; // V25 = 0.76V, avg slope = 2.5mV/C
    
        char buffer[80];
        sprintf(buffer, "%lu\t%lu\t%lu\t%lu\r\n",
            ch5_voltage, ch6_voltage, cht_voltage, temperature);
        HAL_UART_Transmit(&huart3, buffer, strlen(buffer), 10);
    
  19. Open source/stm32f7xx_it.c
  20. Locate void TIM2_IRQHandler() function
  21. Insert your code ** HERE **
      /* USER CODE BEGIN TIM2_IRQn 0 */
    
      /* USER CODE END TIM2_IRQn 0 */
      HAL_TIM_IRQHandler(&htim2);
      /* USER CODE BEGIN TIM2_IRQn 1 */
          ** HERE **
      /* USER CODE END TIM2_IRQn 1 */
    
  22. Calling the function for starting ADC coversions from within an interrupt
      ADC_Start_From_ISR();
    
  23. Open source/main.h
  24. Locate function prototype area
  25. Insert your code ** HERE **
    /* USER CODE BEGIN EFP */
          ** HERE **
    /* USER CODE END EFP */
    
    
  26. Function prototype of the function for starting ADC coversions from within an interrupt
    void ADC_Start_From_ISR();
    

web site front local main page local list page general bookmarks software bookmarks go back copyright info