代码风格更新以及加入超电的对接
This commit is contained in:
1 parent
957c535bda
commit
9de485e4ef
117 files changed
+9046
-6457
No files matched your search
@@ -1,78 +1,89 @@
|
||||
/* =============================================================================
|
||||
* bsp_usart.c - 串口 DMA 接收板级支持包(module_test 精简版:仅 USART3)
|
||||
*
|
||||
* 职责:
|
||||
* 1. usart3_init() 任务上下文调用:配置 RX 循环 DMA + 空闲中断,
|
||||
* 注册"整帧"回调(帧 = 两次空闲之间的字节)
|
||||
* 2. usart3_idle_isr() 由 stm32f4xx_it.c 的 USART3_IRQHandler USER CODE 区
|
||||
* 调用(需在 HAL_UART_IRQHandler 之前):读 NDTR 算帧长,
|
||||
* 清 F4 DMA 流事件标志后重启 DMA,递交回调
|
||||
*
|
||||
* 关键细节:
|
||||
* - 单缓冲循环 DMA + IDLE 重启(DJI 官方风格),不使用双缓冲;
|
||||
* - F4 手册要求重启 DMA 流前必须清除该流全部事件标志,否则只收到第一帧;
|
||||
* - usart3_init 会把字长改为 9B(CubeMX 生成的 8B+偶校验只有 7 个数据位,
|
||||
* DR16 帧需要 8 数据位+偶校验)。
|
||||
* 已移除:USART1(视觉)/USART6(裁判/图传)接收与全部 TX DMA。
|
||||
* ========================================================================== */
|
||||
#include "bsp_usart.h"
|
||||
#include "main.h"
|
||||
#include <stddef.h>
|
||||
|
||||
extern UART_HandleTypeDef huart3;
|
||||
extern DMA_HandleTypeDef hdma_usart3_rx;
|
||||
extern UART_HandleTypeDef huart3;//USART3句柄(DR16遥控器)
|
||||
extern DMA_HandleTypeDef hdma_usart3_rx;//USART3接收DMA句柄
|
||||
|
||||
static uint8_t * usart3_rx_buf = NULL;
|
||||
static uint16_t usart3_buf_num = 0;
|
||||
static usart_rx_callback_f usart3_callback = NULL;
|
||||
static uint8_t * usart3_rx_buf = NULL;//USART3 DMA接收缓冲区指针
|
||||
static uint16_t usart3_buf_num = 0;//USART3 DMA接收缓冲区长度(字节)
|
||||
static usart_rx_callback_f usart3_callback = NULL;//USART3整帧接收回调函数
|
||||
|
||||
/*
|
||||
*函数简介:清除F4 DMA数据流的所有事件标志位
|
||||
*参数说明:DMA句柄指针
|
||||
*返回类型:无
|
||||
*备注:F4手册要求重新使能DMA流之前必须清除该流全部事件标志,否则只收到第一帧
|
||||
*/
|
||||
static void usart_dma_clear_flags(DMA_HandleTypeDef * hdma)
|
||||
{
|
||||
DMA_TypeDef * ctl = (DMA_TypeDef *)((uint32_t)hdma->Instance & ~(0x3FFU));
|
||||
uint32_t stream_no = (((uint32_t)hdma->Instance & 0x3FFU) - 0x10U) / 24U;
|
||||
|
||||
if (stream_no < 4U)
|
||||
ctl->LIFCR = 0x3FUL << (6U * stream_no);
|
||||
else
|
||||
ctl->HIFCR = 0x3FUL << (6U * (stream_no - 4U));
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:配置并启动USART3 RX循环DMA
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:关闭HTIE/TCIE中断,仅靠USART3 IDLE空闲中断取整帧
|
||||
*/
|
||||
static void usart3_rx_dma_start(void)
|
||||
{
|
||||
if (usart3_rx_buf == NULL || usart3_buf_num == 0U)
|
||||
return;
|
||||
|
||||
__HAL_DMA_DISABLE(&hdma_usart3_rx);
|
||||
while (hdma_usart3_rx.Instance->CR & DMA_SxCR_EN)
|
||||
;
|
||||
|
||||
/* F4 手册要求:重新使能流之前必须清除该流全部事件标志,
|
||||
* 否则残留标志会导致流重启后不工作(表现为只收到第一帧) */
|
||||
{
|
||||
DMA_TypeDef * ctl = (DMA_TypeDef *)((uint32_t)hdma_usart3_rx.Instance & ~(0x3FFU));
|
||||
uint32_t stream_no = ((uint32_t)hdma_usart3_rx.Instance - (uint32_t)DMA1_Stream0) / 24U;
|
||||
if (stream_no < 4U)
|
||||
ctl->LIFCR = 0x3FUL << (6U * stream_no);
|
||||
else
|
||||
ctl->HIFCR = 0x3FUL << (6U * (stream_no - 4U));
|
||||
}
|
||||
usart_dma_clear_flags(&hdma_usart3_rx);//重启流前清标志
|
||||
|
||||
hdma_usart3_rx.Instance->PAR = (uint32_t)&(huart3.Instance->DR);
|
||||
/* 不使用 DMA 半满/全满中断,只靠空闲中断取帧 */
|
||||
hdma_usart3_rx.Instance->CR &= ~(DMA_SxCR_HTIE | DMA_SxCR_TCIE);
|
||||
hdma_usart3_rx.Instance->CR &= ~(DMA_SxCR_HTIE | DMA_SxCR_TCIE);//关闭半满/全满中断
|
||||
hdma_usart3_rx.Instance->NDTR = usart3_buf_num;
|
||||
hdma_usart3_rx.Instance->M0AR = (uint32_t)usart3_rx_buf;
|
||||
__HAL_DMA_ENABLE(&hdma_usart3_rx);
|
||||
}
|
||||
|
||||
/* DR16 遥控器接收机专用(100000-8E1)
|
||||
* CubeMX 生成的 WordLength=8B 在奇偶校验下只有 7 个数据位,
|
||||
* DR16 帧需要 8 数据位+偶校验,必须改为 9 位字长后重新初始化 */
|
||||
/*
|
||||
*函数简介:初始化USART3循环DMA接收(DR16遥控器专用)
|
||||
*参数说明:接收缓冲区指针
|
||||
*参数说明:接收缓冲区长度(字节)
|
||||
*参数说明:整帧接收回调函数指针
|
||||
*返回类型:无
|
||||
*备注:CubeMX生成的8B+偶校验只有7位数据,DR16需要8位数据+偶校验,必须重设为9位字长
|
||||
*/
|
||||
void usart3_init(uint8_t * rx_buf, uint16_t dma_buf_num, usart_rx_callback_f callback)
|
||||
{
|
||||
if (rx_buf == NULL || dma_buf_num == 0U)
|
||||
return;
|
||||
|
||||
usart3_rx_buf = rx_buf;
|
||||
usart3_buf_num = dma_buf_num;
|
||||
usart3_callback = callback;
|
||||
|
||||
CLEAR_BIT(huart3.Instance->CR1, USART_CR1_UE); /* 先关串口再改字长 */
|
||||
CLEAR_BIT(huart3.Instance->CR1, USART_CR1_UE);//先关闭串口再修改字长
|
||||
huart3.Init.WordLength = UART_WORDLENGTH_9B;
|
||||
if (HAL_UART_Init(&huart3) != HAL_OK)
|
||||
return;
|
||||
|
||||
/* 使能 DMA 接收请求与空闲中断 */
|
||||
SET_BIT(huart3.Instance->CR3, USART_CR3_DMAR);
|
||||
__HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
|
||||
SET_BIT(huart3.Instance->CR3, USART_CR3_DMAR);//使能串口DMA接收请求
|
||||
__HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);//使能空闲中断
|
||||
|
||||
usart3_rx_dma_start();
|
||||
}
|
||||
|
||||
/* 空闲中断处理:取帧长,复位 DMA,回调递交 */
|
||||
/*
|
||||
*函数简介:USART3空闲中断入口
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:在stm32f4xx_it.c的USART3_IRQHandler中调用,先重启DMA再递交回调
|
||||
*/
|
||||
void usart3_idle_isr(void)
|
||||
{
|
||||
uint16_t recv_len;
|
||||
@@ -81,6 +92,7 @@ void usart3_idle_isr(void)
|
||||
|
||||
recv_len = (uint16_t)(usart3_buf_num - hdma_usart3_rx.Instance->NDTR);
|
||||
usart3_rx_dma_start();
|
||||
if (recv_len > 0 && usart3_callback != NULL)
|
||||
|
||||
if (recv_len > 0U && usart3_callback != NULL)
|
||||
usart3_callback(usart3_rx_buf, recv_len);
|
||||
}
|
||||
Reference in new issue
Block a user