105 lines
2.9 KiB
C
105 lines
2.9 KiB
C
/*
|
||
test1 Task
|
||
*/
|
||
|
||
/* Includes ----------------------------------------------------------------- */
|
||
#include "task\user_task.h"
|
||
#include "main.h"
|
||
#include "usart.h"
|
||
#include "i2c.h"
|
||
#include "bsp/delay.h"
|
||
|
||
|
||
|
||
//////////
|
||
#include "bsp/dwt.h"
|
||
#include "bsp/time.h"
|
||
#include "bsp/uart.h"
|
||
|
||
|
||
/* Private typedef ---------------------------------------------------------- */
|
||
/* Private define ----------------------------------------------------------- */
|
||
/* Private macro ------------------------------------------------------------ */
|
||
/* Private variables -------------------------------------------------------- */
|
||
|
||
uint8_t rx_buffer[256]; // DMA接收缓冲区
|
||
volatile uint16_t rx_index = 0; // 当前接收到的数据长度
|
||
volatile bool rx_complete = false; // 接收完成标志
|
||
uint32_t height;
|
||
|
||
/* Private function --------------------------------------------------------- */
|
||
void UART_IdleCallback_test(void)
|
||
{
|
||
UART_HandleTypeDef *huart = BSP_UART_GetHandle(BSP_UART_C8T6);
|
||
|
||
// 检查是否是USART6的空闲中断
|
||
// if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE))
|
||
// {
|
||
// 清除空闲中断标志
|
||
__HAL_UART_CLEAR_IDLEFLAG(huart);
|
||
|
||
// 停止DMA传输 (这一步是可选的,取决于你的需求)
|
||
HAL_UART_DMAStop(huart);
|
||
|
||
// 计算接收到的数据长度
|
||
rx_index = 256 - __HAL_DMA_GET_COUNTER(huart->hdmarx);
|
||
|
||
// 设置接收完成标志
|
||
rx_complete = true;
|
||
HAL_UART_Receive_DMA(huart, rx_buffer, 256);
|
||
// 处理接收到的数据 (可以在主循环中处理)
|
||
// }
|
||
}
|
||
|
||
/* Exported functions ------------------------------------------------------- */
|
||
/**
|
||
* \brief test1 Task
|
||
*
|
||
* \param argument 未使用
|
||
*/
|
||
|
||
void Task_test1(void *argument) {
|
||
(void)argument; /* 未使用argument,消除警告 */
|
||
|
||
/* 计算任务运行到指定频率需要等待的tick数 */
|
||
const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_TEST1;
|
||
|
||
// osDelay(TASK_INIT_DELAY_TASK_1); /* 延时一段时间再开启任务 */
|
||
|
||
BSP_UART_RegisterCallback(BSP_UART_C8T6, BSP_UART_IDLE_LINE_CB, UART_IdleCallback_test);
|
||
__HAL_UART_ENABLE_IT(&huart6, UART_IT_IDLE);
|
||
HAL_UART_Receive_DMA(BSP_UART_GetHandle(BSP_UART_C8T6), rx_buffer, 256);
|
||
|
||
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
|
||
|
||
while (1) {
|
||
tick += delay_tick; /* 计算下一个唤醒时刻 */
|
||
|
||
/*User code begin*/
|
||
|
||
if (rx_complete)
|
||
{
|
||
// 1. 处理接收到的数据 (rx_buffer中前rx_index个字节)
|
||
// 例如: 查找帧头帧尾,解析协议等
|
||
|
||
osKernelLock();
|
||
height = ((int32_t)rx_buffer[0] << 24) |
|
||
((int32_t)rx_buffer[1] << 16) |
|
||
((int32_t)rx_buffer[2] << 8) |
|
||
((int32_t)rx_buffer[3]);
|
||
osKernelUnlock();
|
||
|
||
// 2. 重置接收状态
|
||
rx_complete = false;
|
||
rx_index = 0;
|
||
|
||
}
|
||
osMessageQueuePut(task_runtime.msgq.baro_height, &height, 0, 0);
|
||
|
||
|
||
/*User code end*/
|
||
|
||
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
|
||
}
|
||
}
|