ECE 471/571 STM32 C Example - Interrupt Priority and Preemption

In STM32CubeMX

  1. Initialize all peripherals with in default mode? - yes
  2. find PB0, PB7, PB14, PC13
  3. find System Core / GPIO
  4. find TIM2 - Timer2
  5. System Core / NVIC Tab
  6. System Core / NVIC Tab

Adapt and compile a project (example 1)

  1. Open Core/Src/main.c
  2. Locate int main() function
  3. Inspect the GPIO initialization called from main()
  4. Locate the timer initialization in main()
  5. Insert your code ** HERE **
      MX_TIM2_Init();
      /* USER CODE BEGIN 2 */
          ** HERE **
      /* USER CODE END 2 */
    
  6. Code to start the timer and enable timer interrupt
      HAL_TIM_Base_Start_IT(&htim2);
    
  7. Locate the forever loop in main()
  8. Insert your code ** HERE **
        /* USER CODE BEGIN WHILE */
        while (1)
        {
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
            ** HERE **
        }
        /* USER CODE END 3 */
    
  9. Code for flashing the green LED at 1Hz
            HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET);     // switch on LED3
            HAL_Delay(500);                                         // 500 ms delay
            HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);   // switch off LED3
            HAL_Delay(500);                                         // 500 ms delay
    
  10. Open Core/Src/stm32f7xx_it.c
  11. Locate void TIM2_IRQHandler() function
  12. Insert your code ** HERE1 ** and ** HERE2 **
      /* 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 */
    
  13. Code for ** HERE **
      HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
    
  14. Open Core/Src/stm32f7xx_it.c
  15. Locate void EXTI15_10_IRQHandler() function
  16. Insert your code ** HERE **
      /* USER CODE BEGIN EXTI15_10_IRQn 0 */
    
      /* USER CODE END EXTI15_10_IRQn 0 */
      HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);                   // toggle LED1 at 2Hz
      /* USER CODE BEGIN EXTI15_10_IRQn 1 */
          ** HERE **
      /* USER CODE END EXTI15_10_IRQn 1 */
    
  17. Code for toggling a LED from the raising edge interrupt
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);     // switch on LED2
      // HAL_Delay(2000); // uncomment to test if the HAL System Timer still works
      while ( HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) )
      {
        // we are stalling inside the interrupt while the button is pressed - very bad practice
        // we do this to see if the Timer interrupt preempts this interrupt and toggles the LED
        // for the complete learning experience play with the priorities of these two interrupts
      }
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);   // switch off LED2
    

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