|
@@ -1,16 +1,16 @@
|
|
|
/*
|
|
/*
|
|
|
- * USART3 通信模块
|
|
|
|
|
|
|
+ * CPLD 通信模块 (USART3: PB10=TX, PB11=RX, 921600)
|
|
|
*
|
|
*
|
|
|
- * 当前模块是和CPLD下位机进行串口通信
|
|
|
|
|
|
|
+ * 与 CPLD 下位机进行串口通信 (传输层)
|
|
|
*/
|
|
*/
|
|
|
-#include "Com_usart3.h"
|
|
|
|
|
|
|
+#include "Com_cpld_usart.h"
|
|
|
|
|
|
|
|
#include "usart.h"
|
|
#include "usart.h"
|
|
|
#include "FreeRTOS.h"
|
|
#include "FreeRTOS.h"
|
|
|
#include "task.h"
|
|
#include "task.h"
|
|
|
#include "stream_buffer.h"
|
|
#include "stream_buffer.h"
|
|
|
|
|
|
|
|
-#include "Com_usart1.h"
|
|
|
|
|
|
|
+#include "Com_pc_usart.h"
|
|
|
|
|
|
|
|
/* usart.c(USART3) 内定义的 DMA 句柄 */
|
|
/* usart.c(USART3) 内定义的 DMA 句柄 */
|
|
|
extern DMA_HandleTypeDef hdma_usart3_rx;
|
|
extern DMA_HandleTypeDef hdma_usart3_rx;
|
|
@@ -25,11 +25,11 @@ extern DMA_HandleTypeDef hdma_usart3_rx;
|
|
|
* 注意:s_tx_stream/s_rx_stream 为 FreeRTOS 动态堆分配(heap_4),
|
|
* 注意:s_tx_stream/s_rx_stream 为 FreeRTOS 动态堆分配(heap_4),
|
|
|
* 不落 AXI SRAM——与 USB CDC 的 xUsbTxStream 处理方式一致。
|
|
* 不落 AXI SRAM——与 USB CDC 的 xUsbTxStream 处理方式一致。
|
|
|
*/
|
|
*/
|
|
|
-#define COM_USART3_AXI_SRAM_SECTION __attribute__((section(".ARM.__at_0x24000000")))
|
|
|
|
|
|
|
+#define COM_CPLD_USART_AXI_SRAM_SECTION __attribute__((section(".ARM.__at_0x24000000")))
|
|
|
|
|
|
|
|
-COM_USART3_AXI_SRAM_SECTION static uint8_t s_rx_dma_buf[COM_USART3_RX_DMA_SIZE];
|
|
|
|
|
|
|
+COM_CPLD_USART_AXI_SRAM_SECTION static uint8_t s_rx_dma_buf[COM_CPLD_USART_RX_DMA_SIZE];
|
|
|
/* 暂存待发送的数据块(TX 发送任务从 TX 流缓冲取出后经此交给 DMA) */
|
|
/* 暂存待发送的数据块(TX 发送任务从 TX 流缓冲取出后经此交给 DMA) */
|
|
|
-COM_USART3_AXI_SRAM_SECTION static uint8_t s_tx_chunk[COM_USART3_STREAM_SIZE];
|
|
|
|
|
|
|
+COM_CPLD_USART_AXI_SRAM_SECTION static uint8_t s_tx_chunk[COM_CPLD_USART_STREAM_SIZE];
|
|
|
|
|
|
|
|
static StreamBufferHandle_t s_tx_stream = NULL;
|
|
static StreamBufferHandle_t s_tx_stream = NULL;
|
|
|
static StreamBufferHandle_t s_rx_stream = NULL;
|
|
static StreamBufferHandle_t s_rx_stream = NULL;
|
|
@@ -41,15 +41,15 @@ static volatile uint32_t s_rx_prev_pos = 0;
|
|
|
|
|
|
|
|
static uint8_t s_rx_started = 0;
|
|
static uint8_t s_rx_started = 0;
|
|
|
|
|
|
|
|
-static void Task_Usart3_Send(void *argument);
|
|
|
|
|
|
|
+static void Task_CpldUsart_Send(void *argument);
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @brief (USART3) 从 DMA 缓冲读取新接收的数据(空闲事件 + 半满/全满皆调用)
|
|
* @brief (USART3) 从 DMA 缓冲读取新接收的数据(空闲事件 + 半满/全满皆调用)
|
|
|
* @note 在中断上下文中执行
|
|
* @note 在中断上下文中执行
|
|
|
*/
|
|
*/
|
|
|
-static void Usart3_Drain_Rx(void)
|
|
|
|
|
|
|
+static void CpldUsart_Drain_Rx(void)
|
|
|
{
|
|
{
|
|
|
- uint32_t cur_pos = (uint32_t)(COM_USART3_RX_DMA_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart3_rx));
|
|
|
|
|
|
|
+ uint32_t cur_pos = (uint32_t)(COM_CPLD_USART_RX_DMA_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart3_rx));
|
|
|
uint32_t new_bytes;
|
|
uint32_t new_bytes;
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
|
|
|
@@ -59,12 +59,12 @@ static void Usart3_Drain_Rx(void)
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- new_bytes = (COM_USART3_RX_DMA_SIZE - s_rx_prev_pos) + cur_pos;
|
|
|
|
|
|
|
+ new_bytes = (COM_CPLD_USART_RX_DMA_SIZE - s_rx_prev_pos) + cur_pos;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (new_bytes > COM_USART3_RX_DMA_SIZE)
|
|
|
|
|
|
|
+ if (new_bytes > COM_CPLD_USART_RX_DMA_SIZE)
|
|
|
{
|
|
{
|
|
|
- new_bytes = COM_USART3_RX_DMA_SIZE;
|
|
|
|
|
|
|
+ new_bytes = COM_CPLD_USART_RX_DMA_SIZE;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (new_bytes == 0)
|
|
if (new_bytes == 0)
|
|
@@ -75,20 +75,20 @@ static void Usart3_Drain_Rx(void)
|
|
|
/* DMA 已写入 AXI SRAM,先使缓存失效再读取。
|
|
/* DMA 已写入 AXI SRAM,先使缓存失效再读取。
|
|
|
回绕时两段分别是 [prev..尾) 与 [0..remainder),须分段失效,
|
|
回绕时两段分别是 [prev..尾) 与 [0..remainder),须分段失效,
|
|
|
一次性跨缓冲区尾失效无法覆盖 [0..remainder) 对应的缓存行。 */
|
|
一次性跨缓冲区尾失效无法覆盖 [0..remainder) 对应的缓存行。 */
|
|
|
- if ((s_rx_prev_pos + new_bytes) <= COM_USART3_RX_DMA_SIZE)
|
|
|
|
|
|
|
+ if ((s_rx_prev_pos + new_bytes) <= COM_CPLD_USART_RX_DMA_SIZE)
|
|
|
{
|
|
{
|
|
|
SCB_InvalidateDCache_by_Addr((uint32_t *)&s_rx_dma_buf[s_rx_prev_pos], (int32_t)new_bytes);
|
|
SCB_InvalidateDCache_by_Addr((uint32_t *)&s_rx_dma_buf[s_rx_prev_pos], (int32_t)new_bytes);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- uint32_t first_len = COM_USART3_RX_DMA_SIZE - s_rx_prev_pos;
|
|
|
|
|
|
|
+ uint32_t first_len = COM_CPLD_USART_RX_DMA_SIZE - s_rx_prev_pos;
|
|
|
SCB_InvalidateDCache_by_Addr((uint32_t *)&s_rx_dma_buf[s_rx_prev_pos], (int32_t)first_len);
|
|
SCB_InvalidateDCache_by_Addr((uint32_t *)&s_rx_dma_buf[s_rx_prev_pos], (int32_t)first_len);
|
|
|
SCB_InvalidateDCache_by_Addr((uint32_t *)&s_rx_dma_buf[0],
|
|
SCB_InvalidateDCache_by_Addr((uint32_t *)&s_rx_dma_buf[0],
|
|
|
(int32_t)(new_bytes - first_len));
|
|
(int32_t)(new_bytes - first_len));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* 环形回绕:跨过 DMA 缓冲末尾时拆成两段写入流缓冲(满则丢弃余下) */
|
|
/* 环形回绕:跨过 DMA 缓冲末尾时拆成两段写入流缓冲(满则丢弃余下) */
|
|
|
- if ((s_rx_prev_pos + new_bytes) <= COM_USART3_RX_DMA_SIZE)
|
|
|
|
|
|
|
+ if ((s_rx_prev_pos + new_bytes) <= COM_CPLD_USART_RX_DMA_SIZE)
|
|
|
{
|
|
{
|
|
|
xStreamBufferSendFromISR(s_rx_stream,
|
|
xStreamBufferSendFromISR(s_rx_stream,
|
|
|
&s_rx_dma_buf[s_rx_prev_pos],
|
|
&s_rx_dma_buf[s_rx_prev_pos],
|
|
@@ -97,7 +97,7 @@ static void Usart3_Drain_Rx(void)
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- uint32_t first_len = COM_USART3_RX_DMA_SIZE - s_rx_prev_pos;
|
|
|
|
|
|
|
+ uint32_t first_len = COM_CPLD_USART_RX_DMA_SIZE - s_rx_prev_pos;
|
|
|
xStreamBufferSendFromISR(s_rx_stream,
|
|
xStreamBufferSendFromISR(s_rx_stream,
|
|
|
&s_rx_dma_buf[s_rx_prev_pos],
|
|
&s_rx_dma_buf[s_rx_prev_pos],
|
|
|
first_len,
|
|
first_len,
|
|
@@ -122,24 +122,24 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
|
|
|
if (huart->Instance == USART3)
|
|
if (huart->Instance == USART3)
|
|
|
{
|
|
{
|
|
|
(void)Size;
|
|
(void)Size;
|
|
|
- Usart3_Drain_Rx();
|
|
|
|
|
|
|
+ CpldUsart_Drain_Rx();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @brief (USART1/USART3) 接收完成回调
|
|
* @brief (USART1/USART3) 接收完成回调
|
|
|
* @note USART3: 半满/全满, CIRCULAR 模式下由 DMA 触发;
|
|
* @note USART3: 半满/全满, CIRCULAR 模式下由 DMA 触发;
|
|
|
- * USART1: 中断单字节接收 (分发到 Com_usart1)
|
|
|
|
|
|
|
+ * USART1: 中断单字节接收 (分发到 Com_pc_usart)
|
|
|
*/
|
|
*/
|
|
|
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
|
{
|
|
{
|
|
|
if (huart->Instance == USART1)
|
|
if (huart->Instance == USART1)
|
|
|
{
|
|
{
|
|
|
- Com_usart1_rxcplt(huart);
|
|
|
|
|
|
|
+ Com_pc_usart_rxcplt(huart);
|
|
|
}
|
|
}
|
|
|
else if (huart->Instance == USART3)
|
|
else if (huart->Instance == USART3)
|
|
|
{
|
|
{
|
|
|
- Usart3_Drain_Rx();
|
|
|
|
|
|
|
+ CpldUsart_Drain_Rx();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -147,7 +147,7 @@ void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
|
|
|
{
|
|
{
|
|
|
if (huart->Instance == USART3)
|
|
if (huart->Instance == USART3)
|
|
|
{
|
|
{
|
|
|
- Usart3_Drain_Rx();
|
|
|
|
|
|
|
+ CpldUsart_Drain_Rx();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -169,14 +169,14 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
|
|
* @brief USART3 发送任务:从 TX 流缓冲取数据块,经 DMA 发送。
|
|
* @brief USART3 发送任务:从 TX 流缓冲取数据块,经 DMA 发送。
|
|
|
* xStreamBufferReceive 无数据时阻塞,有数据自动唤醒(无需手动 notify)。
|
|
* xStreamBufferReceive 无数据时阻塞,有数据自动唤醒(无需手动 notify)。
|
|
|
*/
|
|
*/
|
|
|
-static void Task_Usart3_Send(void *argument)
|
|
|
|
|
|
|
+static void Task_CpldUsart_Send(void *argument)
|
|
|
{
|
|
{
|
|
|
(void)argument;
|
|
(void)argument;
|
|
|
|
|
|
|
|
for (;;)
|
|
for (;;)
|
|
|
{
|
|
{
|
|
|
size_t n = xStreamBufferReceive(s_tx_stream, s_tx_chunk,
|
|
size_t n = xStreamBufferReceive(s_tx_stream, s_tx_chunk,
|
|
|
- COM_USART3_STREAM_SIZE, portMAX_DELAY);
|
|
|
|
|
|
|
+ COM_CPLD_USART_STREAM_SIZE, portMAX_DELAY);
|
|
|
|
|
|
|
|
/* DMA 读取前将数据刷出到内存(避 AXI SRAM 缓存) */
|
|
/* DMA 读取前将数据刷出到内存(避 AXI SRAM 缓存) */
|
|
|
SCB_CleanDCache_by_Addr((uint32_t *)s_tx_chunk, (int32_t)n);
|
|
SCB_CleanDCache_by_Addr((uint32_t *)s_tx_chunk, (int32_t)n);
|
|
@@ -191,29 +191,29 @@ static void Task_Usart3_Send(void *argument)
|
|
|
* @brief 初始化 USART3 通信:启动 RX DMA 接收、创建发送任务
|
|
* @brief 初始化 USART3 通信:启动 RX DMA 接收、创建发送任务
|
|
|
* @note 需在 MX_USART3_UART_Init() 之后、调度器启动之前调用
|
|
* @note 需在 MX_USART3_UART_Init() 之后、调度器启动之前调用
|
|
|
*/
|
|
*/
|
|
|
-void Com_usart3_init(void)
|
|
|
|
|
|
|
+void Com_cpld_usart_init(void)
|
|
|
{
|
|
{
|
|
|
if (s_tx_stream == NULL)
|
|
if (s_tx_stream == NULL)
|
|
|
{
|
|
{
|
|
|
- s_tx_stream = xStreamBufferCreate(COM_USART3_STREAM_SIZE, 1);
|
|
|
|
|
|
|
+ s_tx_stream = xStreamBufferCreate(COM_CPLD_USART_STREAM_SIZE, 1);
|
|
|
}
|
|
}
|
|
|
if (s_rx_stream == NULL)
|
|
if (s_rx_stream == NULL)
|
|
|
{
|
|
{
|
|
|
- s_rx_stream = xStreamBufferCreate(COM_USART3_STREAM_SIZE, 1);
|
|
|
|
|
|
|
+ s_rx_stream = xStreamBufferCreate(COM_CPLD_USART_STREAM_SIZE, 1);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* 启动 RX:CIRCULAR DMA + 空闲中断 */
|
|
/* 启动 RX:CIRCULAR DMA + 空闲中断 */
|
|
|
if (s_rx_started == 0)
|
|
if (s_rx_started == 0)
|
|
|
{
|
|
{
|
|
|
- HAL_UARTEx_ReceiveToIdle_DMA(&huart3, s_rx_dma_buf, COM_USART3_RX_DMA_SIZE);
|
|
|
|
|
|
|
+ HAL_UARTEx_ReceiveToIdle_DMA(&huart3, s_rx_dma_buf, COM_CPLD_USART_RX_DMA_SIZE);
|
|
|
s_rx_started = 1;
|
|
s_rx_started = 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* 创建发送任务 */
|
|
/* 创建发送任务 */
|
|
|
if (s_tx_task_handle == NULL)
|
|
if (s_tx_task_handle == NULL)
|
|
|
{
|
|
{
|
|
|
- xTaskCreate(Task_Usart3_Send, "usart3SendTask", COM_USART3_TX_TASK_STACK,
|
|
|
|
|
- NULL, COM_USART3_TX_TASK_PRIORITY, &s_tx_task_handle);
|
|
|
|
|
|
|
+ xTaskCreate(Task_CpldUsart_Send, "cpldUsartSendTask", COM_CPLD_USART_TX_TASK_STACK,
|
|
|
|
|
+ NULL, COM_CPLD_USART_TX_TASK_PRIORITY, &s_tx_task_handle);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -221,7 +221,7 @@ void Com_usart3_init(void)
|
|
|
* @brief (USART3) 发送数据:阻塞最多 100ms 写入 TX 流缓冲
|
|
* @brief (USART3) 发送数据:阻塞最多 100ms 写入 TX 流缓冲
|
|
|
* @return 实际写入 TX 流缓冲的字节数(满则丢弃余下)
|
|
* @return 实际写入 TX 流缓冲的字节数(满则丢弃余下)
|
|
|
*/
|
|
*/
|
|
|
-uint32_t Com_usart3_send(const uint8_t *data, uint32_t len)
|
|
|
|
|
|
|
+uint32_t Com_cpld_usart_send(const uint8_t *data, uint32_t len)
|
|
|
{
|
|
{
|
|
|
if (s_tx_stream == NULL || data == NULL)
|
|
if (s_tx_stream == NULL || data == NULL)
|
|
|
{
|
|
{
|
|
@@ -233,7 +233,7 @@ uint32_t Com_usart3_send(const uint8_t *data, uint32_t len)
|
|
|
/**
|
|
/**
|
|
|
* @brief (USART3) 查询 RX 流缓冲可读字节数
|
|
* @brief (USART3) 查询 RX 流缓冲可读字节数
|
|
|
*/
|
|
*/
|
|
|
-uint16_t Com_usart3_available(void)
|
|
|
|
|
|
|
+uint16_t Com_cpld_usart_available(void)
|
|
|
{
|
|
{
|
|
|
if (s_rx_stream == NULL)
|
|
if (s_rx_stream == NULL)
|
|
|
{
|
|
{
|
|
@@ -246,7 +246,7 @@ uint16_t Com_usart3_available(void)
|
|
|
* @brief (USART3) 从 RX 流缓冲读取数据(非阻塞)
|
|
* @brief (USART3) 从 RX 流缓冲读取数据(非阻塞)
|
|
|
* @return 实际读取字节数
|
|
* @return 实际读取字节数
|
|
|
*/
|
|
*/
|
|
|
-uint32_t Com_usart3_read(uint8_t *buf, uint32_t len)
|
|
|
|
|
|
|
+uint32_t Com_cpld_usart_read(uint8_t *buf, uint32_t len)
|
|
|
{
|
|
{
|
|
|
if (s_rx_stream == NULL || buf == NULL)
|
|
if (s_rx_stream == NULL || buf == NULL)
|
|
|
{
|
|
{
|