ECE471/571 (RTOS) STM32 FreeRTOS Example - Receive UART data using RX interrupt and a queue

In STM32CubeMX

  1. Initialize all peripherals with in default mode? - yes
  2. System Core
  3. enable UART3
  4. Middleware - enable FreeRTOS
  5. System Core Again

Adapt and compile a project

  1. Open source/main.c
  2. Update the following code fragment:
      /* USER CODE BEGIN 2 */
      __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
      /* USER CODE END 2 */
    
  3. Update the following code fragment:
    void StartDefaultTask(void const * argument)
    {
      /* USER CODE BEGIN 5 */
      /* Infinite loop */
      for(;;)
      {
        // Keep alive indicator
        HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);
        osDelay(500); // or use vTaskDelay( 500 / portTICK_PERIOD_MS );
      }
      /* USER CODE END 5 */ 
    }
    
  4. Update the following code fragment:
    void task_process_uart3(void const * argument)
    {
      /* USER CODE BEGIN task_process_uart3 */
      /* Infinite loop */
      for(;;)
      {
          uint8_t buffer;
          BaseType_t status = xQueueReceive(uart3_rx_queueHandle, &buffer, 1000 / portTICK_PERIOD_MS );
          // Note: timeout of 0ms means that either a byte is already received and waiting or timeout is returned
          if ( status ) {
            if ( buffer=='0' ) {
              HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
            } else if ( buffer=='1' ) {
              HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
            } else{
              // nothing - unrecognized command
            }
          }
      }
      /* USER CODE END task_process_uart3 */
    }
    
  1. Open inc/main.h
  2. Update the following code fragment:
    /* USER CODE BEGIN Includes */
    #include "cmsis_os.h"
    /* USER CODE END Includes */
    
  3. Update the following code fragment:
    /* USER CODE BEGIN EM */
    extern osMessageQId uart3_rx_queueHandle;
    /* USER CODE END EM */
    
  1. Open source/stm32f7xx_it.c
  2. Update the following code fragment:
    void USART3_IRQHandler(void)
    {
      /* USER CODE BEGIN USART3_IRQn 0 */
      BaseType_t xSchedulerChanged = pdFALSE;
      uint8_t buffer;
      HAL_StatusTypeDef status = HAL_UART_Receive(&huart3, &buffer, 1, 0); // we are inside an interrupt - timeout of 0!
      if ( HAL_OK == status ) {
        xQueueSendFromISR(uart3_rx_queueHandle, &buffer, &xSchedulerChanged);
      }
      portYIELD_FROM_ISR( xSchedulerChanged );
    
      /* USER CODE END USART3_IRQn 0 */
      HAL_UART_IRQHandler(&huart3);
      /* USER CODE BEGIN USART3_IRQn 1 */
    
      /* USER CODE END USART3_IRQn 1 */
    }
    

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