STM32 hardware UART receive timeout detection setting

Source: Internet
Author: User

STM32 hardware UART receive timeout detection setting

-----------------the author of this article "Zhi-yu Electronics", looking forward to exchange learning with electronic enthusiasts. ----------------

Application Scenarios

In the UART application sometimes need to carry out duplex communication, the host needs to the slave data to receive time-out detection, such as Modbus protocol, the host in the receiving slave data after 3.5 bytes of time that the packet received. In this case, it is common practice to set a timer to re-count the 0 timer each time a byte is received, until the timer has exceeded 3.5 bytes and the interrupt is triggered by the default packet reception complete.

The time-out judgment of the timer set above requires software intervention. Here STM32 some of the serial port is to provide hardware timeout detection function. This eliminates the steps as above.

Setup Steps

This experiment is validated by using CUBEMX-generated engineering.

    • First, use the CUBEMX configuration usart1 (Note: Not every STM32 chip serial port has hardware timeout detection function). It is important to note that the Hardware timeout option is not set in Cubemx, so this is just the production of the available Usart works.
    • After the project is generated, we go to the data sheet, which has such a description.

      So this article adds a function to enable time-out detection, as shown below
void Uart_RxOvertimeEnable(void){    /*使能接收超时功能*/    SET_BIT(huart1.Instance->CR2,USART_CR2_RTOEN);    /*使能超时接收中断*/    //SET_BIT(huart1.Instance->CR1,USART_CR1_RTOIE);    /*向RTOR寄存器填入需要超时的长度,单位为一个波特时长,3.5个字节*11波特长度 = 39  */    WRITE_REG(huart1.Instance->RTOR,39);  }
    • The function is then added to the main function, and a small test instance is written.
int main(void){  /* MCU Configuration----------------------------------------------------------*/  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */  HAL_Init();  /* Configure the system clock */  SystemClock_Config();  /* Initialize all configured peripherals */  MX_GPIO_Init();  MX_USART1_UART_Init();  /*使能超时检测*/  Uart_RxOvertimeEnable();  /* Infinite loop */  /*采用中断接收数据,模拟接收*/  HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, 100);  while (1)  {     //数据接收后,一直等待超时   while(READ_BIT(huart1.Instance->ISR,USART_ISR_RTOF))    {         /*清除rtof标志*/         SET_BIT(huart1.Instance->ICR,USART_ICR_RTOCF);        /*将接收的数据发送出去测试一下*/       HAL_UART_Transmit_IT(&huart1, (uint8_t *)aRxBuffer, 100);       HAL_Delay(1000);    }         }}
    • Finally through the serial debugging assistant through the host computer to send data (this is test! To the microcontroller, the single-chip function to return the data (after the * * * because of the printing of empty bytes, ignored), the proof is valid.

STM32 hardware UART receive timeout detection setting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.