STM32 library HAL UART serial read and write notes STM32L0 HAL library function UART serial read and write functions STM32 serial port receive interrupt - based HAL library

https://www.cnblogs.com/Mysterious/p/4804188.html

STM32L0 HAL library UART serial read and write functions

Serial transmission function:

TxData uint8_t [10] = "01234abcde"; 
HAL_UART_Transmit (& huart2, TxData, 10,0xffff); // send contents via the TxData uart2 out length 10, timeout is the maximum time 0xffff

Serial reception function 1:

value = uint8_t 'F.'; 
HAL_UART_Receive (& huart2, (uint8_t *) & value, 1,1000); // wait to receive a byte of data staying in the statement 1000ms, the data stored in the value

Serial reception function 2:

HAL_UART_Receive_IT (& huart2, (uint8_t *) & value, 1); // program will not stay in this statement, will follow directly interrupt the received data is stored in value, but this statement can only be enabled once the serial port interrupt. So to interrupt service or function to re-enable the callback function

Serial reception function 3:

if (HAL_UART_Receive_IT (& huart2, ( uint8_t *) & value, 1)! = HAL_OK) {// This is a write on while main function (1). An interrupt program is started to start receiving 
        HAL_UART_Transmit (& huart2, (uint8_t *) & "ERROR \ R & lt \ n-", 7,10);     
        the while (. 1); 
Copy the code
void HAL_UART_RxCpltCallback (UART_HandleTypeDef * UartHandle) 
{ 
    HAL_UART_Transmit (& huart2, (uint8_t *) & "\ R & lt \ ninto HAL_UART_RxCpltCallback \ R & lt \ n-", 32,0xffff); // validate entered the function of 
    HAL_UART_Transmit (& huart2, (uint8_t * ) & value , 1,0xffff); // send the data received through the serial out         
    HAL_UART_Receive_IT (& huart2, (uint8_t * ) & value, 1); // reopen the serial interrupt 
}
Copy the code

Serial DMA transfer

The TX DMA set to be so

    TxData uint8_t [] = { "the HelloWorld \ R & lt \ n-"}; 
    HAL_UART_Transmit_DMA (& huart2, TxData, the sizeof (TxData)); // data can be sent out by the DMA

 DMA reception

if (! HAL_UART_Receive_DMA (& huart2, (uint8_t *) rxData, sizeof (rxData) -1) = HAL_OK) // main function while (1) before starting a DMA reception 
    { 
        Error_Handler (); 
    }

 Serial callback function:

Copy the code
HAL_UART_RxCpltCallback void (* UART_HandleTypeDef UartHandle) { 
    
    uint8_t TEMP [] = { "\ R & lt \ Nin the Callback \ R & lt \ n-"}; 
    HAL_UART_Transmit_DMA (& huart2, TEMP, the sizeof (TEMP) -1); // data can be sent out by the DMA 
    
    HAL_UART_Receive_DMA (& huart2, (uint8_t * ) rxData, sizeof (rxData) -1); // re-enabled receiver     
}
Copy the code

 The main function while (1) continuously output value rxData

HAL_UART_Transmit_DMA (& huart2, rxData, sizeof (rxData) -1); // data can be sent out by the DMA

https://www.cnblogs.com/UnfriendlyARM/p/10321838.html

STM32 serial port receive interrupt - based HAL library

EDITORIAL  

  Recently STM32L4 need to use a series of chip development, we need to learn to use HAL library. During serial interrupt use when encountered some small problems, write down the solution for your reference.

1.UART associated header file reference error

   Since I am directly MDK development, did not use CubeMX, so some initialization requires manually. When referring to UART associated header file, "stm32l4xx_hal_conf.h" documents related to the definition of the macro remember uncommented, as shown below:

2. How to receive string (multiple entry interrupts)

  Receiving a string There are two main ways, one is to transform the interrupt function, the other is receiving a callback function to be transformed.
  Before addressing these two methods, we need to introduce the function "HAL_StatusTypeDef HAL_UART_Receive_IT (UART_HandleTypeDef * huart, uint8_t * pData, uint16_t Size)". The action function is a user-defined buffer (i.e., the parameter pData), accept a certain number of characters (determined by the parameter Size) is stored in the buffer. At the same time, also determines the frequency parameter Size to enter the callback function, that is, each receiving Size characters to enter a callback function. It should be noted, Size only decided to enter the frequency of the callback function, and can not influence into the receive interrupt frequency, no matter how much Size, each receiving a complete character will enter a receive interrupt.

Method 1: transformation of the callback function

  ① First position before entering the main loop calls a function once HAL_UART_Receive_IT, getBuffer define a character array [] in the main function as a buffer, Size parameter 10 is set. That is, each receiving 10 characters, to enter a callback function.

  ② registered interrupt function   

Void USART1_IRQHandler. 1 (void) 
2 { 
. 3 HAL_UART_IRQHandler (& UartHandle); // This function will clear the interrupt flag, interrupt enable canceled, and calls the callback function indirectly 
4}

  ③ in the file "stm32l4xx_hal_uart.h", we can see the definition of the serial receive the callback function. Use "_weak" keyword-defined function, which has the following characteristics: and the same general function in general. But when there is a function of the same name but without __weak is defined, all calls to this function are pointing to the latter (without __weak that). In other words, ST official of the callback function needs to be rewritten our own.  

Copy the code
 1    /**
 2     * @brief Rx Transfer completed callback.
 3     * @param huart UART handle.
 4     * @retval None
 5     */
 6   __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
 7   {
 8     /* Prevent unused argument(s) compilation warning */
 9     UNUSED(huart);
10       
11     /* NOTE : This function should not be modified, when the callback is needed,
12               the HAL_UART_RxCpltCallback can be implemented in the user file.
13      */
14   }
Copy the code

  We rewrite the callback function in the file is located in the main function:  

Copy the code
1 uint8_t myBuffer [] = "I have gotten your message:"; // User message 
2 uint8_t Enter [] = "\ r \ n"; // carriage return linefeed 
3 uint8_t getBuffer [100]; // User defined buffer 
. 4 HAL_UART_RxCpltCallback void (* UART_HandleTypeDef UartHandle) 
. 5 { 
. 6 the while (HAL_UART_Transmit (UartHandle, (uint8_t *) myBuffer, COUNTOF (myBuffer), 5000) = HAL_OK!); // send the string, the user prompts 
7 while ( ! HAL_UART_Transmit (UartHandle, (uint8_t * ) getBuffer, 10, 5000) = HAL_OK); // send user-defined data in the buffer 
8 while (HAL_UART_Transmit (UartHandle, ( uint8_t *) Enter, COUNTOF (Enter), 5000) ! = HAL_OK); // send carriage return line 
9}
Copy the code

  Action code above is to transmit user data to the microcontroller and then returned to the user. FIG operating results as follows:

 
  We can see that the user sends 10 characters to the microcontroller, the microcontroller 10 returns this data to the serial assistant. But the program can only be achieved over time, when we send data to the microcontroller again, the microcontroller is no longer return data. This is because we canceled in interrupt function interrupt enable, resulting in after entering the interrupt, the interrupt is closed, the phenomenon can not enter interrupt again. In order to achieve multiple data return, we want to add a line of code in the interrupt handler: 
1 void USART1_IRQHandler (void) 
2 { 
3 HAL_UART_IRQHandler (& UartHandle); // This function will clear the interrupt flag, cancel the interrupt enable and indirectly calls the callback function 
4 HAL_UART_Receive_IT (& UartHandle, (uint8_t *) & value, 1); // add the line of code 
5}

  This enables multiple data returned, a new execution results as shown below:

 

  Visible, as well as interrupt function HAL_UART_Receive_IT the role of energy. We achieve this functionality can be found in HAL_UART_Receive_IT function.

 Method 2: transformation of the interrupt handler

  ① First, where the call enters a loop in front of the main functions in the main function HAL_UART_Receive_IT, a character value is defined as a buffer, Size parameter is set to 1. That is, each receiving a character, then enter a callback function. Callback function so that the entering into the same frequency as the frequency of the interrupt handler. In this way, we can directly interrupt function for processing the received data.

  ② registered interrupt function    

Copy the code
. 1 uint8_t myBuffer [] = "the I have have gotten your Message:"; 
 2 uint8_t getBuffer [10]; 
 . 3 uint8_t the Enter [] = "\ R & lt \ n-"; 
 . 4 void USARTx_IRQHandler (void) 
 . 5 { 
 . 6 HAL_UART_IRQHandler (& UartHandle); / / this function will clear the interrupt flag, cancel the interrupt is enabled, and indirectly callback function 
 . 7     
 . 8 getBuffer [countOfGetBuffer ++] = value; 
 . 9 IF (countOfGetBuffer == 10) 
10 { 
. 11 the while (HAL_UART_Transmit (& UartHandle, (uint8_t *) myBuffer, ! COUNTOF (myBuffer), 5000) = HAL_OK); 
! 12 is the while (HAL_UART_Transmit (& UartHandle, (uint8_t *) getBuffer, countOfGetBuffer, 5000) = HAL_OK); 
13 is the while (HAL_UART_Transmit (& UartHandle, (uint8_t *) the Enter, COUNTOF (the Enter !), 5000) = HAL_OK); 
14 countOfGetBuffer = 0; 
15}
16 HAL_UART_Receive_IT (& UartHandle, (uint8_t *) & value, 1); // receive interrupt is due to receive a character will enter every time, so you must add this line of code, or a character can only receive, but can not receive the entire string 
17}
Copy the code

  Action code above each character is received from a user, and the user-defined sequentially stored in the buffer, the number reached 10, all the data in the buffer is returned to the user, while the emptied again to prepare the next 10 receiving characters. FIG operating results as follows:

 

Written in the last

  After reading this article, you may have questions about the relationship between the callback function and interrupt handlers. In fact, this is the MCU to complete each receives a character, it will enter an interrupt handler, and the interrupt handler, we call the function "void HAL_UART_IRQHandler (UART_HandleTypeDef * huart)", the function will call the callback function indirectly, that callback function is called by the interrupt handler called indirectly. The function "HAL_UART_Receive_IT (UART_HandleTypeDef * huart, uint8_t * pData, uint16_t Size)" determines the frequency of the interrupt handler calls the callback function, if Size is 1, then each enter an interrupt handler will call a callback function; if Size 10 , then every tenth into the interrupt handler will call the callback function. Method 2 uses a standard library interrupt thought process data.

Serial transmission function:

TxData uint8_t [10] = "01234abcde"; 
HAL_UART_Transmit (& huart2, TxData, 10,0xffff); // send contents via the TxData uart2 out length 10, timeout is the maximum time 0xffff

Serial reception function 1:

value = uint8_t 'F.'; 
HAL_UART_Receive (& huart2, (uint8_t *) & value, 1,1000); // wait to receive a byte of data staying in the statement 1000ms, the data stored in the value

Serial reception function 2:

HAL_UART_Receive_IT (& huart2, (uint8_t *) & value, 1); // program will not stay in this statement, will follow directly interrupt the received data is stored in value, but this statement can only be enabled once the serial port interrupt. So to interrupt service or function to re-enable the callback function

Serial reception function 3:

if (HAL_UART_Receive_IT (& huart2, ( uint8_t *) & value, 1)! = HAL_OK) {// This is a write on while main function (1). An interrupt program is started to start receiving 
        HAL_UART_Transmit (& huart2, (uint8_t *) & "ERROR \ R & lt \ n-", 7,10);     
        the while (. 1); 
Copy the code
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
    HAL_UART_Transmit(&huart2, (uint8_t *)&"\r\ninto HAL_UART_RxCpltCallback\r\n",32,0xffff);    //验证进入这个函数了
    HAL_UART_Transmit(&huart2,(uint8_t *)&value,1,0xffff);      //把接收到的数据通过串口发送出去        
    HAL_UART_Receive_IT(&huart2,(uint8_t *)&value,1);        //重新打开串口中断
}
Copy the code

串口DMA发送

DMA的TX要这样设置

    uint8_t txData[] = {"HelloWorld\r\n"};
    HAL_UART_Transmit_DMA(&huart2,txData,sizeof(txData));//可以通过DMA把数据发出去

 DMA接收

if(HAL_UART_Receive_DMA(&huart2, (uint8_t *)rxData, sizeof(rxData)-1) != HAL_OK)//main函数while(1)前,启动一次DMA接收
    {
        Error_Handler();
    }

 串口回调函数:

Copy the code
HAL_UART_RxCpltCallback void (* UART_HandleTypeDef UartHandle) { 
    
    uint8_t TEMP [] = { "\ R & lt \ Nin the Callback \ R & lt \ n-"}; 
    HAL_UART_Transmit_DMA (& huart2, TEMP, the sizeof (TEMP) -1); // data can be sent out by the DMA 
    
    HAL_UART_Receive_DMA (& huart2, (uint8_t * ) rxData, sizeof (rxData) -1); // re-enabled receiver     
}
Copy the code

 The main function while (1) continuously output value rxData

HAL_UART_Transmit_DMA (& huart2, rxData, sizeof (rxData) -1); // data can be sent out by the DMA

Guess you like

Origin www.cnblogs.com/CodeWorkerLiMing/p/11487378.html