之前L4串口收到的数据要不有帧头、帧尾或者协议带帧长,纯不定长的想和之前F103一样用IDLE的方式,发现不行
都是用IDLE的方式,和之前用的F103还是有区别的,寄存器不一样,改了寄存器还是收不到正确数据,L4系列所提供的固件库中没有对该标志位的检测,需要我们自行添加
在stm32l4xx_hal_uart.c文件中添加以下代码:
思路也是借助IDLE的方式,但没有用RXNE,直接库函数HAL_UART_Receive_IT,所以用的时候要开一下串口中断
/**
******************************************************************************
* @file com_uart_cfg.c
* @author yangFei
* @brief 串口不定长配置函数相关
*
* @version V1.0.0
* @date 2022.03.23
* @verbatim
*
* @endverbatim
******************************************************************************
* @note 只适合stm32l4的库
after stm32l4xx_hal_uart.c line2602 add:(check UART in mode Transmitter)
if(((isrflags & USART_ISR_IDLE) != 0U) && ((cr1its & USART_CR1_IDLEIE) != 0U))
{
HAL_UART_IdleCpltCallback(huart);
return;
}
* @attention
*
* Change Logs:
* Date Author Notes
* 2022.03.23 yangFei
*
******************************************************************************
*/
#include "com_uart_cfg.h"
#define RX_LEN 50
uint8_t rx_data[RX_LEN], rx_len, rx_flag = 0;
/**
* @brief 串口空闲接收中断使能.
* @note
* @param .
*
* @retval no
* @author yangFei
* @date 20220323
* @note
*/
void USR_UartInit(void)
{
uint8_t rx_temp;
rx_len = 0; // 长度初始
HAL_UART_Receive_IT(&huart1, &rx_temp, 1);
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE); // 使能 IDLE中断
__HAL_UART_CLEAR_IDLEFLAG(&huart1); //清除IDLE标志
}
/**
* @brief 打印地址及其对应的数据(格式需要参考给待测芯片发送的数据结构,查看ASIC_TEST.h).
* @note
* @param *addr_word 地址和数据的地址.
*
* @retval no
* @author yangFei
* @date 20220323
* @note
*/
void print_addr_word(uint32_t *addr_word)
{
printf("addr word: ");
for(uint8_t i=0; i<32; i++)
{
printf("%d", (((*addr_word)<<i) & 0x80000000)>>31);
if(i%8 == 7)
printf(" ");
}
printf("\r\n");
HAL_Delay(10);
}
/**
* @brief 串口接收中断.
* @note
* @param *huart 串口号.
*
* @retval no
* @author yangFei
* @date 20220323
* @note
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
uint8_t rx_temp[2];
rx_data[rx_len] = USART1->RDR; //
rx_len++; // 接收到上个数据,长度+1
if(huart->Instance == USART1)
{
HAL_UART_Receive_IT(&huart1, rx_temp, 1); // 继续使能RX中断
}
}
/**
* @brief 串口接收空闲中断.
* @note
* @param *huart 串口号.
*
* @retval no
* @author yangFei
* @date 20220323
* @note 一帧接收结束后进入
*/
void HAL_UART_IdleCpltCallback(UART_HandleTypeDef *huart)
{
__HAL_UART_CLEAR_IDLEFLAG(huart);//清除IDLE标志
//data handle
if(rx_len > 2)
{
rx_flag = 1;
rx_len = 0;
}
}
/**
* @brief 重写fputc函数.
* @note
* @param c 要写的字符, 作为int推广传递的.
stream 指向FILE对象的指针,该对象标识要写入字符的流.
*
* @retval no
* @author yangFei
* @date 20220323
* @note
*/
int fputc(int c, FILE *stream) //重写fputc函数
{
/*
huart1是工具生成代码定义的UART1结构体,
如果以后要使用其他串口打印,只需要把这个结构体改成其他UART结构使
*/
HAL_UART_Transmit(&huart1, (unsigned char *)&c, 1, 1000);
return 1;
}
/************************ (C) COPYRIGHT LEADINNO *****END OF FILE****/