ECE471/571 (RTOS) STM32 FreeRTOS Example - Thread safe data processing with Mutex

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 Includes */
    #include<string.h>
    /* USER CODE END Includes */
    
  3. Update the following code fragment:
    /* USER CODE BEGIN 0 */
    BaseType_t getCommand() {
    
    	while( xSemaphoreTake(only_one_at_a_timeHandle, 1000 / portTICK_PERIOD_MS ) != pdTRUE )
    	  ; // wait up to forever for the mutex access
    
    	char command[20];
    	BaseType_t  pos = 0;
        uint8_t buffer;
        do {
            while( xQueueReceive(uart3_rx_queueHandle, &buffer, 1000 / portTICK_PERIOD_MS ) != pdTRUE )
              ; // wait up to forever to receive a character
            if ( buffer!='\n' && buffer!='\r' ) {
              command[pos] = buffer;
              pos++;
            }
        } while( pos<19 && buffer!='\n' && buffer!='\r' );
        command[pos]='\0';
    
        xSemaphoreGive(only_one_at_a_timeHandle);
    
        BaseType_t result = -1;
        if ( strcmp(command, "off")==0 )
          result = 0;
        else if ( strcmp(command, "on")==0 )
          result = 1;
        return(result);
    }
    /* USER CODE END 0 */
    
  4. Update the following code fragment:
      /* USER CODE BEGIN 2 */
      __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
      /* USER CODE END 2 */
    
  5. 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 */ 
    }
    
  6. Update the following code fragment:
    void task_process_uart3(void const * argument)
    {
      /* USER CODE BEGIN task_process_uart3 */
      /* Infinite loop */
      for(;;)
      {
        BaseType_t command = getCommand();
        if ( command==0 ) {
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
        } else if ( command==1 ) {
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
        } else{
        // nothing - unrecognized command
        }
      }
      /* USER CODE END task_process_uart3 */
    }
    
  7. Update the following code fragment:
    void task_competing_process(void const * argument)
    {
      /* USER CODE BEGIN task_competing_process */
      /* Infinite loop */
      for(;;)
      {
        BaseType_t command = getCommand();
        if ( command==0 ) {
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
        } else if ( command==1 ) {
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
        } else{
        // nothing - unrecognized command
        }
      }
      /* USER CODE END task_competing_process */
    }
    
  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