STM32 HAL_LOCK问题问题
STM32 HAL_LOCK问题
STM32 HAL_LOCK问题问题
在使用STM32的HAL库开发时候,在使用UART和CAN的使用,偶尔会碰到突然不再接收数据的情况.调试发现,信号有的,但是就
是软件不再进入接收中断了.
通过调试,最后定位到问题点在于__HAL_LOCK()这个函数里.
以下用uart为例子,剖析这个问题.
典型的典型的uart接收数据例子接收数据例子
uart配置后,最后调用一下 HAL_UART_Receive_IT()。
HAL_UART_Receive_IT(&huart1, (u8 *)RxBuffer, 1);
然后每次收到数据后,HAL_UART_RxCpltCallback()会被调用.
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart == &huart1)
{
uart1ReceivedBuffer=RxBuffer;//处理接收到的数据
HAL_UART_Receive_IT(&huart1,&RxBuffer,1);//启动下一次接收
}
}
接收到数据后,读取数据,然后再启动下一次的接收.
逻辑上看,一点问题都没有.
但是实际使用中,特别是uart全双工,数据量大的时候,突然会发现HAL_UART_RxCpltCallback()不再被调用了,然后接收就断了.
为什么出出现这情况?
__HAL_LOCK()做了什么做了什么?
先来看看HAL_UART_Receive_IT()的源代码:
/**
* @brief Receives an amount of data in non blocking mode.
* @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
* the received data is handled as a set of u16. In this case, Size must indicate the number
* of u16 available through pData.
* @param huart Pointer to a UART_HandleTypeDef structure that contains
* the configuration information for the specified UART module.
* @param pData Pointer to data buffer (u8 or u16 data elements).
* @param Size Amount of data elements (u8 or u16) to be received.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{
/* Check that a Rx process is not already ongoing */
if (huart->RxState == HAL_UART_STATE_READY)
{
if ((pData == NULL) || (Size == 0U))
{
return HAL_ERROR;
}
/* Process Locked */
__HAL_LOCK(huart);
huart->pRxBuffPtr = pData;
huart->RxXferSize = Size;
huart->RxXferCount = Size;
huart->ErrorCode = HAL_UART_ERROR_NONE;
huart->RxState = HAL_UART_STATE_BUSY_RX;
/* Process Unlocked */
__HAL_UNLOCK(huart);
/* Enable the UART Parity Error Interrupt */
__HAL_UART_ENABLE_IT(huart, UART_IT_PE);
/* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */