feat: 增加简单串口通信帧发送解析功能
This commit is contained in:
@@ -30,7 +30,8 @@
|
||||
"activityBar.background": "#4B2301",
|
||||
"titleBar.activeBackground": "#693002",
|
||||
"titleBar.activeForeground": "#FFF9F4"
|
||||
}
|
||||
},
|
||||
"cortex-debug.variableUseNaturalFormat": false
|
||||
},
|
||||
"extensions": {
|
||||
}
|
||||
|
||||
166
app/by_frame.c
Normal file
166
app/by_frame.c
Normal file
@@ -0,0 +1,166 @@
|
||||
#include "by_frame.h"
|
||||
|
||||
#include "at32f403a_407.h"
|
||||
#include "lwrb.h"
|
||||
#include "by_crc16.h"
|
||||
|
||||
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;
|
||||
|
||||
void by_frame_init(void)
|
||||
{
|
||||
lwrb_init(&lwrb_ctx, frame_buffer_recv, sizeof(frame_buffer_recv)); // lwrb 最大元素数量为 buff 大小减一
|
||||
}
|
||||
|
||||
void by_frame_send(uint8_t cmd, uint32_t *data_array)
|
||||
{
|
||||
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;
|
||||
frame_buffer_send[1] = cmd;
|
||||
|
||||
// 当传入数组不足时,会发生越界情况
|
||||
memcpy(frame_buffer_send + 2, data_array, data_byte_num);
|
||||
crc_cal = by_crc16_calculate(frame_buffer_send, 2 + data_byte_num);
|
||||
// crc_cal = crc16_check(frame_buffer_send, 2 + data_byte_num);
|
||||
|
||||
frame_buffer_send[2 + data_byte_num] = (uint8_t)(crc_cal >> 8);
|
||||
frame_buffer_send[3 + data_byte_num] = (uint8_t)(crc_cal);
|
||||
|
||||
for (uint8_t i = 0; i < 4 + data_byte_num; i++) {
|
||||
while (RESET == usart_flag_get(BY_FRAME_UART_INDEX, USART_TDBE_FLAG))
|
||||
;
|
||||
usart_data_transmit(BY_FRAME_UART_INDEX, frame_buffer_send[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* @param data_num
|
||||
* @param data_array
|
||||
* @todo 将其中写死的数据长度按照宏定义给出
|
||||
*/
|
||||
uint8_t by_frame_parse(uint8_t *cmd, 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 + 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 + data_byte_num)) { // FIXME 当传递相对值时会出现问题
|
||||
// // 当前要求缓冲区满
|
||||
// // (x) 缓冲区内长度小于帧长度,直接返回
|
||||
// // 要是每次读的时候缓冲区内就只有前一帧的尾部和后一帧的头部,岂不是很尴尬
|
||||
// // 是不是应该正确解析之后再把过的部分清空?但是是异步操作,实际上缓冲区内已经是新数据了
|
||||
// // 可是直接读取 fifo 的话也是异步操作
|
||||
// // 发的慢的话就很有可能有同步问题,导致一直解析不出来
|
||||
// // 喵的,为啥不直接丢中断里解析算了
|
||||
|
||||
// // 目前的解决办法大概是缓冲区开两帧长的大小,然后一次性读完
|
||||
// // 读取的时候不清除,等待新帧覆盖
|
||||
// // 用 lwrb 的话就只能清除了
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
lwrb_sz_t valid_num = 0;
|
||||
lwrb_sz_t invalid_num = 0;
|
||||
uint8_t frame_head = BY_FRAME_HEAD;
|
||||
|
||||
// lwrb_find(&lwrb_ctx, &frame_head, 1, 0, &invalid_num);
|
||||
// lwrb_skip(&lwrb_ctx, invalid_num);
|
||||
// // TODO 优化逻辑,先找 0xEF,判断缓冲区里帧头后的长度足够则进入解析
|
||||
// // 从环形缓冲区里读取数据,仅读取一个帧长
|
||||
// lwrb_read(&lwrb_ctx, buf, 4 + BY_FRAME_DATA_NUM * sizeof(uint32_t));
|
||||
|
||||
// 如果没找到帧头,跳出
|
||||
if (!lwrb_find(&lwrb_ctx, &frame_head, 1, 0, &invalid_num)) {
|
||||
lwrb_skip(&lwrb_ctx, len);
|
||||
return 1;
|
||||
}
|
||||
|
||||
valid_num = len - invalid_num; // 从帧头开始,剩下的数据长度
|
||||
lwrb_skip(&lwrb_ctx, invalid_num);
|
||||
|
||||
// 如果没有足够的数据,跳出
|
||||
if (valid_num < 4 + data_byte_num) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
lwrb_read(&lwrb_ctx, buf, 4 + data_byte_num);
|
||||
|
||||
// 递归解析有效帧
|
||||
while (1) {
|
||||
if (0 == status) // 没找到帧头
|
||||
{
|
||||
// 读到最后一个元素还没找到帧头
|
||||
if (frame_start >= len - 2) {
|
||||
return 1;
|
||||
}
|
||||
uint16_t temp = (buf[frame_start] | (buf[frame_start + 1] << 8));
|
||||
frame_start++;
|
||||
|
||||
// 递归寻找帧头,现在只有一个帧头了,摆大烂不想改就这样了
|
||||
if (BY_FRAME_HEAD == (uint8_t)(temp & 0xFF)) {
|
||||
status = 1; // 找到了好耶
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// 开始读数据
|
||||
if (1 == status) {
|
||||
// 剩下的数据不够组成一帧
|
||||
if ((frame_start + 4 + data_byte_num - 1) > len) {
|
||||
// 解析出错,缓冲区中没有有效帧
|
||||
return 1;
|
||||
} else {
|
||||
// 复制到帧缓冲区,减一是因为之前多加了一次
|
||||
memcpy(frame_buf, buf + frame_start - 1, 4 + data_byte_num);
|
||||
status = 2;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (2 == status) // 校验 CRC
|
||||
{
|
||||
uint16_t crc_cal = by_crc16_calculate(frame_buf, 2 + data_byte_num);
|
||||
if ((frame_buf[2 + data_byte_num] << 8 | frame_buf[2 + data_byte_num + 1]) == crc_cal) {
|
||||
// 解析成功了✌
|
||||
|
||||
// 复制数据
|
||||
if (NULL != (frame_buf + 2)) {
|
||||
*cmd = frame_buf[1];
|
||||
memcpy(data_array, frame_buf + 2, data_byte_num);
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
status = 0;
|
||||
// 这样无法应对连续帧之间缺字节的的问题,但是减少了重新遍历寻找帧头的时间
|
||||
// frame_start += (8 - 1);
|
||||
// 从上一个帧头之后开始解析
|
||||
frame_start += (2 - 1);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void by_frame_parse_uart_handle(uint8_t data)
|
||||
{
|
||||
lwrb_write(&lwrb_ctx, &data, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 定时器回调,用于接收超时判断 1ms 调用一次
|
||||
*
|
||||
*/
|
||||
void by_frame_parse_timer_handle(void)
|
||||
{
|
||||
}
|
||||
24
app/by_frame.h
Normal file
24
app/by_frame.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef _BY_FRAME_H__
|
||||
#define _BY_FRAME_H__
|
||||
|
||||
/* BY_TINY_FRAME 的超级减配版本(好吧基本上完全没有关系)
|
||||
* 主要是等应答还是挺慢的,写数据场景只需要下位机校验数据合理性即可,读数据等应答即可
|
||||
* 并且需要同步的参数并不多,所以考虑直接使用定长的特定结构的帧,一帧全部下发
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "at32f403a_407.h"
|
||||
|
||||
#define BY_FRAME_HEAD (0XEB)
|
||||
|
||||
#define BY_FRAME_UART_INDEX (USART3)
|
||||
|
||||
#define BY_FRAME_DATA_NUM (3)
|
||||
|
||||
extern void by_frame_init(void);
|
||||
void by_frame_send(uint8_t cmd, uint32_t *data_array);
|
||||
uint8_t by_frame_parse(uint8_t *cmd, uint32_t *data_array);
|
||||
extern void by_frame_parse_uart_handle(uint8_t data);
|
||||
|
||||
#endif
|
||||
@@ -30,6 +30,7 @@
|
||||
/* private includes ----------------------------------------------------------*/
|
||||
/* add user code begin private includes */
|
||||
#include "by_debug.h"
|
||||
#include "by_frame.h"
|
||||
/* add user code end private includes */
|
||||
|
||||
/* private typedef -----------------------------------------------------------*/
|
||||
@@ -94,8 +95,7 @@ void HardFault_Handler(void)
|
||||
|
||||
/* add user code end HardFault_IRQ 0 */
|
||||
/* go to infinite loop when hard fault exception occurs */
|
||||
while (1)
|
||||
{
|
||||
while (1) {
|
||||
/* add user code begin W1_HardFault_IRQ 0 */
|
||||
|
||||
/* add user code end W1_HardFault_IRQ 0 */
|
||||
@@ -113,8 +113,7 @@ void MemManage_Handler(void)
|
||||
|
||||
/* add user code end MemoryManagement_IRQ 0 */
|
||||
/* go to infinite loop when memory manage exception occurs */
|
||||
while (1)
|
||||
{
|
||||
while (1) {
|
||||
/* add user code begin W1_MemoryManagement_IRQ 0 */
|
||||
|
||||
/* add user code end W1_MemoryManagement_IRQ 0 */
|
||||
@@ -132,8 +131,7 @@ void BusFault_Handler(void)
|
||||
|
||||
/* add user code end BusFault_IRQ 0 */
|
||||
/* go to infinite loop when bus fault exception occurs */
|
||||
while (1)
|
||||
{
|
||||
while (1) {
|
||||
/* add user code begin W1_BusFault_IRQ 0 */
|
||||
|
||||
/* add user code end W1_BusFault_IRQ 0 */
|
||||
@@ -151,8 +149,7 @@ void UsageFault_Handler(void)
|
||||
|
||||
/* add user code end UsageFault_IRQ 0 */
|
||||
/* go to infinite loop when usage fault exception occurs */
|
||||
while (1)
|
||||
{
|
||||
while (1) {
|
||||
/* add user code begin W1_UsageFault_IRQ 0 */
|
||||
|
||||
/* add user code end W1_UsageFault_IRQ 0 */
|
||||
|
||||
@@ -88,24 +88,21 @@ void wk_system_clock_config(void)
|
||||
crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);
|
||||
|
||||
/* wait till lick is ready */
|
||||
while(crm_flag_get(CRM_LICK_STABLE_FLAG) != SET)
|
||||
{
|
||||
while (crm_flag_get(CRM_LICK_STABLE_FLAG) != SET) {
|
||||
}
|
||||
|
||||
/* enable hext */
|
||||
crm_clock_source_enable(CRM_CLOCK_SOURCE_HEXT, TRUE);
|
||||
|
||||
/* wait till hext is ready */
|
||||
while(crm_hext_stable_wait() == ERROR)
|
||||
{
|
||||
while (crm_hext_stable_wait() == ERROR) {
|
||||
}
|
||||
|
||||
/* enable hick */
|
||||
crm_clock_source_enable(CRM_CLOCK_SOURCE_HICK, TRUE);
|
||||
|
||||
/* wait till hick is ready */
|
||||
while(crm_flag_get(CRM_HICK_STABLE_FLAG) != SET)
|
||||
{
|
||||
while (crm_flag_get(CRM_HICK_STABLE_FLAG) != SET) {
|
||||
}
|
||||
|
||||
/* config pll clock resource */
|
||||
@@ -115,8 +112,7 @@ void wk_system_clock_config(void)
|
||||
crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE);
|
||||
|
||||
/* wait till pll is ready */
|
||||
while(crm_flag_get(CRM_PLL_STABLE_FLAG) != SET)
|
||||
{
|
||||
while (crm_flag_get(CRM_PLL_STABLE_FLAG) != SET) {
|
||||
}
|
||||
|
||||
/* config ahbclk */
|
||||
@@ -135,8 +131,7 @@ void wk_system_clock_config(void)
|
||||
crm_sysclk_switch(CRM_SCLK_PLL);
|
||||
|
||||
/* wait till pll is used as system clock source */
|
||||
while(crm_sysclk_switch_status_get() != CRM_SCLK_PLL)
|
||||
{
|
||||
while (crm_sysclk_switch_status_get() != CRM_SCLK_PLL) {
|
||||
}
|
||||
|
||||
/* disable auto step mode */
|
||||
@@ -431,7 +426,7 @@ void wk_usart1_init(void)
|
||||
usart_enable(USART1, TRUE);
|
||||
|
||||
/* add user code begin usart1_init 2 */
|
||||
|
||||
usart_interrupt_enable(USART1, USART_RDBF_INT, TRUE);
|
||||
/* add user code end usart1_init 2 */
|
||||
}
|
||||
|
||||
@@ -711,7 +706,6 @@ void wk_tmr8_init(void)
|
||||
tmr_brkdt_struct.deadtime = 0;
|
||||
tmr_brkdt_config(TMR8, &tmr_brkdt_struct);
|
||||
|
||||
|
||||
tmr_output_enable(TMR8, TRUE);
|
||||
|
||||
tmr_counter_enable(TMR8, TRUE);
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
#include "eeprom.h"
|
||||
#include "by_debug.h"
|
||||
#include "by_crc16.h"
|
||||
#include "by_frame.h"
|
||||
/* add user code end private includes */
|
||||
|
||||
/* private typedef -----------------------------------------------------------*/
|
||||
@@ -137,12 +138,15 @@ int main(void)
|
||||
flash_ee_init();
|
||||
LOGD("eeprom init done");
|
||||
|
||||
/* frame init */
|
||||
by_frame_init();
|
||||
LOGD("frame init done");
|
||||
|
||||
LOGI("init done");
|
||||
|
||||
/* add user code end 2 */
|
||||
|
||||
while(1)
|
||||
{
|
||||
while (1) {
|
||||
/* add user code begin 3 */
|
||||
// DWT_Delay(1000000);
|
||||
/* add user code end 3 */
|
||||
|
||||
Reference in New Issue
Block a user