小小的一个串口居然看了半天,原来对STM32系列的lib库,好像是1.x版本感觉还不错的。后来ST非要整个CUBEMx库,往一个中断接受函数里丢了一堆东西。
在rt-thread官网中给出了nano版本增加控制台的示范例程,主要是增加了一个接受队列。(如果用rt-thread studio配置控制台,接受好像用的是查询模式,如果只是单纯一个控制台,不干其他事情好像也没啥事)
#ifdef RT_USING_FINSH
char rt_hw_console_getchar(void)
{
int ch = -1;
if (__HAL_UART_GET_FLAG(&handle, UART_FLAG_RXNE) != RESET)
{
#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0) \
|| defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) \
|| defined(SOC_SERIES_STM32G4)
ch = handle.Instance->RDR & 0xff;
#else
ch = handle.Instance->DR & 0xff;
#endif
}
else
{
if(__HAL_UART_GET_FLAG(&handle, UART_FLAG_ORE) != RESET)
{
__HAL_UART_CLEAR_OREFLAG(&handle);
}
rt_thread_mdelay(10);
}
return ch;
}
用中断模式+接受队列接受大量数据可能会更健壮可靠些。问题是实验了几次,只要在串口助手中发送了2个字节,中断接受就出现问题,再也进入不了中断函数了。后来关闭了控制台的echo回应功能,居然神奇的好了。
/* normal is echo mode */
#ifndef FINSH_ECHO_DISABLE_DEFAULT
shell->echo_mode = 1;
#else
shell->echo_mode = 0;
#endif
多半定位到是串口发送导致的。原来这是ST的HAL库的bug,在串口发送中进行了互斥操作,导致有接受中断时不能再次启动接受中断。如下
/**
* @brief Sends an amount of data in blocking mode.
* @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
* the sent data is handled as a set of u16. In this case, Size must indicate the number
* of u16 provided 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 sent
* @param Timeout Timeout duration
* @retval HAL status
*/
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
uint16_t *tmp;
uint32_t tickstart = 0U;
/* Check that a Tx process is not already ongoing */
if (huart->gState == HAL_UART_STATE_READY)
{
if ((pData == NULL) || (Size == 0U))
{
return HAL_ERROR;
}
/* Process Locked */
__HAL_LOCK(huart);
....
....
/* At end of Tx process, restore huart->gState to Ready */
huart->gState = HAL_UART_STATE_READY;
/* Process Unlocked */
__HAL_UNLOCK(huart);
国外论坛也有这个讨论
[https://community.st.com/s/question/0D50X00009XkeOGSAZ/questions-surrounding-hallock]
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
...
__HAL_UNLOCK(&HUART);
...
}
}
在中断回调函数中加入个unlock就好了。
当时是注意到RTT官方的文档中断发送函数中有unlock操作,看来接受 也得加个
/* 移植控制台,实现控制台输出, 对接 rt_hw_console_output */
void rt_hw_console_output(const char *str)
{
rt_size_t i = 0, size = 0;
char a = '\r';
__HAL_UNLOCK(&HUART);
size = rt_strlen(str);
for (i = 0; i < size; i++)
{
if (*(str + i) == '\n')
{
HAL_UART_Transmit(&HUART, (uint8_t *)&a, 1, 1);
}
HAL_UART_Transmit(&HUART, (uint8_t *)(str + i), 1, 1);
}
}