feat: 移植从机通信帧协议

This commit is contained in:
bmy
2024-02-25 11:49:10 +08:00
parent 354b41dad8
commit 038098ff2d
20 changed files with 1562 additions and 19 deletions

View File

@@ -0,0 +1,39 @@
#include "by_tiny_frame_pack.h"
#include <string.h>
#include "zf_common_headfile.h"
#include "crc16.h"
void by_tiny_frame_pack_init(void)
{
/** nothing to init **/
}
void by_tiny_frame_pack_send(by_tf_pack_frame_t *frame_s)
{
uint16_t calc_crc_val = 0;
#if defined(BY_TF_DEVICE_SLAVE)
frame_s->frame[0] = ((frame_s->slave_id << 1) + 1);
#else
frame_s->frame[0] = (frame_s->slave_id << 1);
#endif
// 填充指令段
frame_s->frame[1] = frame_s->cmd;
// 填充寄存器地址段
frame_s->frame[2] = (uint8_t)((frame_s->reg_addr >> 8) & 0xFF);
frame_s->frame[3] = (uint8_t)(frame_s->reg_addr & 0xFF);
// 填充数据段
frame_s->frame[4] = (uint8_t)((frame_s->data >> 24) & 0xFF);
frame_s->frame[5] = (uint8_t)((frame_s->data >> 16) & 0xFF);
frame_s->frame[6] = (uint8_t)((frame_s->data >> 8) & 0xFF);
frame_s->frame[7] = (uint8_t)(frame_s->data & 0xFF);
// 填充 CRC 段
calc_crc_val = crc16_check(frame_s->frame, (sizeof(frame_s->frame) - 2));
frame_s->frame[8] = (uint8_t)((calc_crc_val >> 8) & 0xFF);
frame_s->frame[9] = (uint8_t)(calc_crc_val & 0xFF);
/** 从串口发送 **/
uart_write_buffer(BY_TF_UART_INDEX, frame_s->frame, sizeof(frame_s->frame));
}