This commit is contained in:
2024-03-27 13:26:55 +08:00
3 changed files with 35 additions and 33 deletions

View File

@@ -7,8 +7,8 @@
#include "lwrb.h"
#include "crc16.h"
uint8_t frame_buffer_recv[(2 * (4 + 8)) + 1];
uint8_t frame_buffer_send[4 + 8];
uint8_t frame_buffer_recv[(2 * (4 + BY_FRAME_DATA_NUM * sizeof(uint32_t))) + 1];
uint8_t frame_buffer_send[4 + BY_FRAME_DATA_NUM * sizeof(uint32_t)];
uint8_t frame_parse_busy;
lwrb_t lwrb_ctx;
@@ -19,19 +19,22 @@ void by_frame_init(void)
uart_rx_interrupt(BY_FRAME_UART_INDEX, ENABLE);
}
void by_frame_send(uint8_t data_num, uint32_t *data_array)
void by_frame_send(uint32_t *data_array)
{
uint16_t crc_cal = 0;
uint16_t crc_cal = 0;
const uint8_t data_byte_num = BY_FRAME_DATA_NUM * sizeof(uint32_t);
frame_buffer_send[0] = BY_FRAME_HEAD_1;
frame_buffer_send[1] = BY_FRAME_HEAD_2;
memcpy(frame_buffer_send + 2, data_array, data_num * sizeof(uint32_t));
crc_cal = crc16_check(frame_buffer_send, 2 + data_num * sizeof(uint32_t));
// 当传入数组不足时,会发生越界情况
memcpy(frame_buffer_send + 2, data_array, data_byte_num);
crc_cal = crc16_check(frame_buffer_send, 2 + data_byte_num);
frame_buffer_send[2 + data_num * sizeof(uint32_t)] = (uint8_t)(crc_cal >> 8);
frame_buffer_send[3 + data_num * sizeof(uint32_t)] = (uint8_t)(crc_cal);
frame_buffer_send[2 + data_byte_num] = (uint8_t)(crc_cal >> 8);
frame_buffer_send[3 + data_byte_num] = (uint8_t)(crc_cal);
uart_write_buffer(BY_FRAME_UART_INDEX, frame_buffer_send, 4 + data_num * sizeof(uint32_t));
uart_write_buffer(BY_FRAME_UART_INDEX, frame_buffer_send, 4 + data_byte_num);
}
/**
@@ -41,15 +44,16 @@ void by_frame_send(uint8_t data_num, uint32_t *data_array)
* @param data_array
* @todo 将其中写死的数据长度按照宏定义给出
*/
void by_frame_parse(uint8_t data_num, uint32_t *data_array)
void by_frame_parse(uint32_t *data_array)
{
uint32_t len = lwrb_get_full(&lwrb_ctx); // 缓冲区大小
uint8_t status = 0; // 状态 0-未找到帧头 1-找到帧头 2-校验
uint16_t frame_start = 0; // 帧起始位置
uint8_t frame_buf[4 + 8] = {0}; // 帧
uint8_t buf[(4 + 8) * 2] = {0}; // 用于解析的数据块
uint32_t len = lwrb_get_full(&lwrb_ctx); // 缓冲区大小
uint8_t status = 0; // 状态 0-未找到帧头 1-找到帧头 2-校验
uint16_t frame_start = 0; // 帧起始位置
uint8_t frame_buf[4 + BY_FRAME_DATA_NUM * sizeof(uint32_t)] = {0}; // 帧
uint8_t buf[(4 + BY_FRAME_DATA_NUM * sizeof(uint32_t)) * 2] = {0}; // 用于解析的数据块
const uint8_t data_byte_num = BY_FRAME_DATA_NUM * sizeof(uint32_t);
if (len < 2 * (4 + 4 * data_num)) {
if (len < 2 * (4 + data_byte_num)) {
// 当前要求缓冲区满
// (x) 缓冲区内长度小于帧长度,直接返回
// 要是每次读的时候缓冲区内就只有前一帧的尾部和后一帧的头部,岂不是很尴尬
@@ -91,13 +95,13 @@ void by_frame_parse(uint8_t data_num, uint32_t *data_array)
// 开始读数据
if (1 == status) {
// 剩下的数据不够组成一帧
if ((frame_start + 4 + 8 - 1) > len) {
if ((frame_start + 4 + data_byte_num - 1) > len) {
// printf("failed! length not enough \r\n");
// 解析出错,缓冲区中没有有效帧
return;
} else {
// 复制到帧缓冲区,减一是因为之前多加了一次
memcpy(frame_buf, buf + frame_start - 1, 4 + 8);
memcpy(frame_buf, buf + frame_start - 1, 4 + data_byte_num);
// for (uint8_t i = 0; i < 12; i++) {
// printf("%02X", frame_buf[i]);
@@ -111,11 +115,14 @@ void by_frame_parse(uint8_t data_num, uint32_t *data_array)
if (2 == status) // 校验 CRC
{
if ((frame_buf[2 + 8] << 8 | frame_buf[2 + 8 + 1]) == crc16_check(frame_buf, 2 + 4 * data_num)) {
if ((frame_buf[2 + data_byte_num] << 8 | frame_buf[2 + data_byte_num + 1]) == crc16_check(frame_buf, 2 + data_byte_num)) {
// 解析成功了✌
// printf("parsed done!!!!!!!!\r\n");
// 复制数据
memcpy(data_array, frame_buf + 2, 4 * data_num);
if (NULL != (frame_buf + 2)) {
memcpy(data_array, frame_buf + 2, data_byte_num);
}
return;
} else {
status = 0;

View File

@@ -16,11 +16,13 @@
#define BY_FRAME_UART_INDEX (UART_4)
#define BY_FRAME_UART_BAUDRATE (115200)
#define BY_FRAME_DATA_NUM (3)
extern uint8_t frame_buffer[50];
extern void by_frame_init(void);
extern void by_frame_send(uint8_t data_num, uint32_t *data_array);
extern void by_frame_parse(uint8_t data_num, uint32_t *data_array);
void by_frame_send(uint32_t *data_array);
void by_frame_parse(uint32_t *data_array);
extern void by_frame_parse_uart_handle(uint8_t data);
#endif

View File

@@ -36,12 +36,9 @@
#include "by_rt_button.h"
#include "by_fan_control.h"
by_vofa_t vofa_test;
float debug_array[2];
int main(void)
{
TYPE_UNION test_data[2];
TYPE_UNION test_data[BY_FRAME_DATA_NUM];
clock_init(SYSTEM_CLOCK_120M);
system_delay_init();
debug_init();
@@ -63,15 +60,11 @@ int main(void)
pit_ms_init(TIM1_PIT, 1); // 运动解算,编码器
printf("ok\r\n");
by_vofa_init(&vofa_test, BT_UART_INDEX, sizeof(debug_array) / sizeof(debug_array[0]), debug_array);
while (1) {
debug_array[0] = in_pos;
debug_array[1] = out_pos;
// by_vofa_send(&vofa_test); // 发送数据
printf("pwm:%lu,%lu,%lu,%lu\r\n", pwm_duty_ls_g, pwm_duty_rs_g, pwm_duty_lb_g, pwm_duty_rb_g);
// printf("pwm:%lu,%lu,%lu,%lu\r\n", pwm_duty_ls_g, pwm_duty_rs_g, pwm_duty_lb_g, pwm_duty_rb_g);
Page_Run();
by_frame_parse(2, &test_data[0].u32);
by_frame_parse(&test_data[0].u32);
by_buzzer_run();
jj_bt_run();
in_pos = test_data[1].f32;