88702e2832
- Add .dma_buf section in RAM_D1 for DMA-accessible USART instance pool. DTCM (0x20000000) is not reachable by DMA on STM32H7, causing bus errors (TE) when DMA writes to malloc'd buffers placed in DTCM by the XG linker script. - Replace malloc with static pool (usart_pool[]) placed in .dma_buf section. - In RxEventCallback, set recv_buff_size from Size so module callbacks know how many bytes were actually received. - Use USART_RXBUFF_LIMIT (256) as the DMA transfer size instead of per-module recv_buff_size. The actual buffer is always 256 bytes. - Remove recv_buff_size from USART_Init_Config_s; it is now set at runtime. - Flush RDR, clear error flags, and clear stale DMAR in error callback to prevent residual conditions from re-triggering errors on DMA restart. - Switch Vision module from UART10 to UART1.
75 lines
2.6 KiB
C
75 lines
2.6 KiB
C
#ifndef BSP_RC_H
|
|
#define BSP_RC_H
|
|
|
|
#include <stdint.h>
|
|
#include "main.h"
|
|
|
|
#define DEVICE_USART_CNT 8 // MC02串口数量8个,其中
|
|
#define USART_RXBUFF_LIMIT 256 // 如果协议需要更大的buff,请修改这里
|
|
|
|
// 模块回调函数,用于解析协议
|
|
typedef void (*usart_module_callback)();
|
|
|
|
/* 发送模式枚举 */
|
|
typedef enum
|
|
{
|
|
USART_TRANSFER_NONE=0,
|
|
USART_TRANSFER_BLOCKING,
|
|
USART_TRANSFER_IT,
|
|
USART_TRANSFER_DMA,
|
|
} USART_TRANSFER_MODE;
|
|
|
|
// 串口实例结构体,每个module都要包含一个实例.
|
|
// 由于串口是独占的点对点通信,所以不需要考虑多个module同时使用一个串口的情况,因此不用加入id;当然也可以选择加入,这样在bsp层可以访问到module的其他信息
|
|
typedef struct
|
|
{
|
|
uint8_t recv_buff[USART_RXBUFF_LIMIT]; // 预先定义的最大buff大小,如果太小请修改USART_RXBUFF_LIMIT
|
|
uint16_t recv_buff_size; // 实际接收到的数据量(由RxEventCallback赋值)
|
|
UART_HandleTypeDef *usart_handle; // 实例对应的usart_handle
|
|
usart_module_callback module_callback; // 解析收到的数据的回调函数
|
|
} USARTInstance;
|
|
|
|
/* usart 初始化配置结构体 */
|
|
typedef struct
|
|
{
|
|
UART_HandleTypeDef *usart_handle; // 实例对应的usart_handle
|
|
usart_module_callback module_callback; // 解析收到的数据的回调函数
|
|
} USART_Init_Config_s;
|
|
|
|
/**
|
|
* @brief 注册一个串口实例,返回一个串口实例指针
|
|
*
|
|
* @param init_config 传入串口初始化结构体
|
|
*/
|
|
USARTInstance *USARTRegister(USART_Init_Config_s *init_config);
|
|
|
|
/**
|
|
* @brief 启动串口服务,需要传入一个usart实例.一般用于lost callback的情况(使用串口的模块daemon)
|
|
*
|
|
* @param _instance
|
|
*/
|
|
void USARTServiceInit(USARTInstance *_instance);
|
|
|
|
|
|
/**
|
|
* @brief 通过调用该函数可以发送一帧数据,需要传入一个usart实例,发送buff以及这一帧的长度
|
|
* @note 在短时间内连续调用此接口,若采用IT/DMA会导致上一次的发送未完成而新的发送取消.
|
|
* @note 若希望连续使用DMA/IT进行发送,请配合USARTIsReady()使用,或自行为你的module实现一个发送队列和任务.
|
|
* @todo 是否考虑为USARTInstance增加发送队列以进行连续发送?
|
|
*
|
|
* @param _instance 串口实例
|
|
* @param send_buf 待发送数据的buffer
|
|
* @param send_size how many bytes to send
|
|
*/
|
|
void USARTSend(USARTInstance *_instance, uint8_t *send_buf, uint16_t send_size,USART_TRANSFER_MODE mode);
|
|
|
|
/**
|
|
* @brief 判断串口是否准备好,用于连续或异步的IT/DMA发送
|
|
*
|
|
* @param _instance 要判断的串口实例
|
|
* @return uint8_t ready 1, busy 0
|
|
*/
|
|
uint8_t USARTIsReady(USARTInstance *_instance);
|
|
|
|
#endif
|