ECE471/571 (RTOS) STM32 C Example - UART receive using RX interrupt

In STM32CubeMX

  1. Initialize all peripherals with in default mode? - yes
  2. find PB0, PB7, PB14, PC13
  3. enable UART3
  4. disable Ethernet (optional)
  5. 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 the UART3 initialization in main()
  5. Insert your code ** HERE **
      MX_USART3_UART_Init();
      /* USER CODE BEGIN 2 */
      ** HERE **
      /* USER CODE END 2 */
    
  6. Enable the UART3 RX interrupt
      __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
    
  7. Locate the forever loop in main()
  8. Insert your code ** HERE **
        /* USER CODE BEGIN WHILE */
            ** HERE1 **
        while (1)
        {
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
            ** HERE2 **
        }
        /* USER CODE END 3 */
    
  9. Code for ** HERE1 **
        GPIO_PinState state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
    
  10. Code for ** HERE2 **
          GPIO_PinState new_state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
          if (state!=new_state) {
            state=new_state;
            HAL_Delay(10); // delay for debouncing
            // sending text over fast 115200bps will cause delay of 1ms per 10 characters
            // sending text over slow   9600bps will cause delay of 1ms per character - set timeout accordingly
            if (state) {
              HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
              HAL_UART_Transmit(&huart3, "button pressed\r\n", 16, 2);
              if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0)) {
                HAL_UART_Transmit(&huart3, "LED is on\r\n", 11, 2);
              } else {
                HAL_UART_Transmit(&huart3, "LED is off\r\n", 12, 2);
              }
            } else {
              HAL_UART_Transmit(&huart3, "button released\r\n", 17, 2);
            }
          }
    
  11. Open source/stm32f7xx_it.c
  12. Locate void USART3_IRQHandler() function
  13. Insert your code ** HERE **
      /* USER CODE BEGIN USART3_IRQn 0 */
          ** HERE **
      /* USER CODE END USART3_IRQn 0 */
      HAL_UART_IRQHandler(&huart3);
      /* USER CODE BEGIN USART3_IRQn 1 */
    
      /* USER CODE END USART3_IRQn 1 */
    
  14. Code for receiving one character at a time and processing the entire buffer when accumulated
      static char command[20] = ""; // note: global variable initialized only once
      static unsigned int len = 0;  // note: global variable initialized only once
    
      static unsigned char buffer[2];
      HAL_StatusTypeDef status = HAL_UART_Receive(&huart3, buffer, 1, 0); // we are inside an interrupt - timeout of 0!
      if ( HAL_OK == status ) {
        if ( len<20 ) {
          if ( buffer[0]=='\n' || buffer[0]=='\r' ) {
            command[len] = '\0';
            if ( strcmp(command,"on") == 0 ) {
              HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
            } else if ( strcmp(command,"off") == 0 ) {
              HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
            } else {
              // nothing - command not recognized
            }
            len = 0;
          } else {
            command[len] = buffer[0];
            len++;
          }
        } else {
          // buffer full, ignore this command
          if ( buffer[0]=='\n' || buffer[0]=='\r' ) {
            len = 0;
          }
        }
      }
    
  15. Open source/stm32f7xx_it.c
  16. Locate a good place to insert a custom include file
  17. Insert your code ** HERE **
    /* USER CODE BEGIN Includes */
        ** HERE **
    /* USER CODE END Includes */
    
  18. We are using the C-string library, we need to include its header
    #include <string.h>
    

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