juanisVIG/MDK-ARM/RTX.c
2026-03-02 18:13:03 +08:00

53 lines
1.4 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// RTX.c
#include "RTX.h"
#include "main.h"
#include "usart.h" // 必须引入USART1相关定义
#include "sbus.h"
// 全局变量定义和RTX.h的extern声明对应只在这里定义一次
uint8_t rx_complete_flag = 0;
uint8_t rx_buf[TRANSFER_LEN] = {0};
uint8_t tx_buf[TRANSFER_LEN] = {0};
Data_Type recv_values[DATA_COUNT] = {0};
Data_Type send_values[DATA_COUNT] = {0}; // 移到这里定义避免main.c重复
extern uint8_t sbus_data_ready;
extern uint8_t buf[25];
// 打包8个uint16_t0~3000→ 16字节数组小端模式通用
void Pack_Values(Data_Type *values, uint8_t *buf, uint8_t count)
{
for (uint8_t i=0; i<count; i++)
{
// 拆分uint16_t为2个字节低字节在前高字节在后
buf[i*2] = values[i] & 0xFF; // 低8位
buf[i*2+1] = (values[i] >> 8) & 0xFF; // 高8位
}
}
// 解包16字节数组 → 8个uint16_t0~3000
void Unpack_Values(uint8_t *buf, Data_Type *values, uint8_t count)
{
for (uint8_t i=0; i<count; i++)
{
// 合并2个字节为uint16_t
values[i] = (Data_Type)(buf[i*2+1] << 8) | buf[i*2];
// 限幅确保数值在0~3000之间
if (values[i] > 3000) values[i] = 3000;
if (values[i] < 0) values[i] = 0;
}
}
// DMA接收完成回调16字节接收完成触发
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART2)
{
sbus_data_ready = 1; // ±ê??êy?Y?é?a??
// ??D????ˉDMA?óê?
HAL_UART_Receive_DMA(&huart2,buf, 25);
}
}