feat: 换用更简单的通信帧格式

This commit is contained in:
bmy
2024-03-04 15:05:40 +08:00
parent f1c0b63d21
commit 7e7b22c992
20 changed files with 149 additions and 620 deletions

90
app/by_frame.c Normal file
View File

@@ -0,0 +1,90 @@
#include "by_frame.h"
#include <stdint.h>
#include <string.h>
#include "zf_common_headfile.h"
#include "lwrb.h"
#include "crc16.h"
lwrb_t lwrb_struct;
uint8_t lwrb_buffer[50];
uint8_t frame_buffer[100];
void by_frame_init(void)
{
uart_init(BY_FRAME_UART_INDEX, BY_FRAME_UART_BAUDRATE, BY_FRAME_UART_TX_PIN, BY_FRAME_UART_RX_PIN);
uart_rx_interrupt(BY_FRAME_UART_INDEX, ENABLE);
lwrb_init(&lwrb_struct, lwrb_buffer, 50);
}
void by_frame_send(uint8_t data_num, uint32_t *data_array)
{
uint16_t crc_cal = 0;
frame_buffer[0] = BY_FRAME_HEAD_1;
frame_buffer[1] = BY_FRAME_HEAD_2;
memcpy(frame_buffer + 2, data_array, data_num * sizeof(uint32_t));
crc_cal = crc16_check(frame_buffer, 2 + data_num * sizeof(uint32_t));
frame_buffer[2 + data_num * sizeof(uint32_t)] = (uint8_t)(crc_cal >> 8);
frame_buffer[3 + data_num * sizeof(uint32_t)] = (uint8_t)(crc_cal);
uart_write_buffer(BY_FRAME_UART_INDEX, frame_buffer, 4 + data_num * sizeof(uint32_t));
}
void by_frame_parse(uint8_t data_num, uint32_t *data_array)
{
uint8_t cnt = 0;
uint8_t cnt_crc = 2;
uint8_t data = 0;
uint8_t data_array_temp[100] = {0};
uint16_t crc_cal = 0;
if (lwrb_get_full(&lwrb_struct) >= (4 + data_num * sizeof(uint32_t))) {
while (lwrb_read(&lwrb_struct, &data, 1)) {
// printf("char : %0.2X\r\n", data);
if ((0 == cnt) && (BY_FRAME_HEAD_1 == data)) {
cnt = 1;
data_array_temp[0] = data;
continue;
}
if ((1 == cnt) && (BY_FRAME_HEAD_2 == data)) {
cnt = 2;
data_array_temp[1] = data;
continue;
}
if ((2 <= cnt) && (cnt < 2 + data_num * sizeof(uint32_t))) {
data_array_temp[cnt] = data;
cnt++;
continue;
}
if (cnt_crc) {
crc_cal |= ((uint16_t)data << (--cnt_crc * 8));
continue;
}
// printf("GET CRC %0.4X\r\n", crc_cal);
// printf("CAL CRC %0.4X\r\n", crc16_check((uint8_t *)data_array_temp, 2 + data_num * sizeof(uint32_t)));
if (!cnt_crc) {
if (crc_cal == crc16_check((uint8_t *)data_array_temp, 2 + data_num * sizeof(uint32_t))) {
memcpy(data_array, data_array_temp + 2, data_num * sizeof(uint32_t));
lwrb_reset(&lwrb_struct); // 处理完直接清空缓冲区,避免堆积产生处理阻塞
// printf("parsed done!\r\n");
}
break;
}
}
}
}
void by_frame_parse_uart_handle(uint8_t data)
{
lwrb_write(&lwrb_struct, &data, 1);
}

24
app/by_frame.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef _BY_FRAME_H__
#define _BY_FRAME_H__
/* BY_TINY_FRAME 的超级减配版本(好吧基本上完全没有关系)
* 主要是等应答还是挺慢的,写数据场景只需要下位机校验数据合理性即可,读数据等应答即可
* 并且需要同步的参数并不多,所以考虑直接使用定长的特定结构的帧,一帧全部下发
*/
#include <stdint.h>
#define BY_FRAME_HEAD_1 (0XEB)
#define BY_FRAME_HEAD_2 (0x90)
#define BY_FRAME_UART_TX_PIN (UART4_MAP1_TX_B0)
#define BY_FRAME_UART_RX_PIN (UART4_MAP1_RX_B1)
#define BY_FRAME_UART_INDEX (UART_4)
#define BY_FRAME_UART_BAUDRATE (115200)
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);
extern void by_frame_parse_uart_handle(uint8_t data);
#endif

View File

@@ -38,7 +38,7 @@
#include "by_imu.h"
#include "by_buzzer.h"
#include "by_rt_button.h"
#include "by_tiny_frame_parse.h"
#include "by_frame.h"
#include "jj_motion.h"
#include "jj_blueteeth.h"
@@ -112,7 +112,7 @@ void UART4_IRQHandler(void)
if (USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) {
uint8_t data_s = 0;
uart_query_byte(UART_4, &data_s);
by_tiny_frame_parse_uart_handle(data_s);
by_frame_parse_uart_handle(data_s);
USART_ClearITPendingBit(UART4, USART_IT_RXNE);
}
}
@@ -314,7 +314,7 @@ void TIM5_IRQHandler(void)
void TIM6_IRQHandler(void)
{
if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET) {
//ICM_getEulerianAngles();
// ICM_getEulerianAngles();
imu660ra_get_gyro();
TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
}

View File

@@ -38,11 +38,8 @@ float in_speed;
float out_speed;
float set_speed = 0.0f;
/**
* @brief
*
*/
int cnt1 = 0;
void sport_motion(void)
{
@@ -50,7 +47,7 @@ void sport_motion(void)
in_gyro = imu660ra_gyro_z; // 陀螺仪输入
in_angle = 0; // 图像远端输入
in_pos = 0; // 图像近端输入
in_speed = encoder_get_count(TIM5_ENCOEDER) / 1024 / 0.01 * 0.25; // 速度输入,m/s
in_speed = encoder_get_count(TIM5_ENCOEDER) / 1024 / 0.01 * 0.25; // 速度输入m/s
encoder_clear_count(TIM5_ENCOEDER); // 清除计数
PID_Compute(&far_gyro_pid);

View File

@@ -31,12 +31,14 @@
#include "by_imu.h"
#include "by_buzzer.h"
#include "by_tiny_frame.h"
#include "by_frame.h"
#include "by_rt_button.h"
#include "by_fan_control.h"
int main(void)
{
TYPE_UNION test_data;
TYPE_UNION test_data_last;
clock_init(SYSTEM_CLOCK_120M);
system_delay_init();
debug_init();
@@ -50,8 +52,8 @@ int main(void)
by_rb_init();
by_pwm_init();
// by_buzzer_init();
by_tiny_frame_init();
by_frame_init();
Page_Init();
sport_pid_init();
@@ -59,11 +61,19 @@ int main(void)
pit_ms_init(TIM1_PIT, 1); // 运动解算bianmaqi
printf("ok\r\n");
while (1) {
Page_Run();
//by_buzzer_run();
jj_bt_run();
by_tiny_frame_run();
// Page_Run();
by_frame_parse(1, &test_data.u32);
if (test_data.u32 != test_data_last.u32) {
ips200_show_float(0, 50, test_data.f32, 4, 2);
// printf("- %ld\r\n", (uint32_t)(test_data.f32 * 100));
}
test_data_last.u32 = test_data.u32;
// by_buzzer_run();
// jj_bt_run();
}
}

View File

@@ -1,37 +0,0 @@
#include "by_tiny_frame.h"
#include <stdio.h>
#include <stdint.h>
#include "crc16.h"
#include "zf_common_headfile.h"
#include "by_tiny_frame_config.h"
#include "by_tiny_frame_parse.h"
#include "by_tiny_frame_master_read.h"
#include "by_tiny_frame_master_write.h"
#include "by_tiny_frame_slave_read_write.h"
void by_tiny_frame_init(void)
{
/*** 初始化相关外设 ***/
uart_init(BY_TF_UART_INDEX, BY_TF_UART_BAUDRATE, BY_TF_UART_TX_PIN, BY_TF_UART_RX_PIN);
uart_rx_interrupt(BY_TF_UART_INDEX, ENABLE);
by_tiny_frame_parse_init();
#if defined(BY_TF_DEVICE_SLAVE)
by_tiny_frame_parse_handle_register(by_tiny_frame_read_write_handle);
#endif
}
void by_tiny_frame_run(void)
{
by_tiny_frame_parse_run();
#if defined(BY_TF_DEVICE_MASTER)
by_tiny_frame_read_run();
by_tiny_frame_write_run();
#elif defined(BY_TF_DEVICE_SLAVE)
by_tiny_frame_read_write_run();
#endif
}

View File

@@ -1,16 +0,0 @@
#ifndef _BY_TINY_FRAME_H__
#define _BY_TINY_FRAME_H__
#include "by_tiny_frame_config.h"
#if defined(BY_TF_DEVICE_MASTER)
#include "by_tiny_frame_master_read.h"
#include "by_tiny_frame_master_write.h"
#elif defined(BY_TF_DEVICE_SLAVE)
#include "by_tiny_frame_slave_read_write.h"
#endif
extern void by_tiny_frame_init(void);
void by_tiny_frame_run(void);
#endif

View File

@@ -1,27 +0,0 @@
#ifndef _BY_TINY_FRAME_CONFIG_H__
#define _BY_TINY_FRAME_CONFIG_H__
#define BY_TF_DEBUG (1)
#define BY_TF_UART_TX_PIN (UART4_MAP1_TX_B0)
#define BY_TF_UART_RX_PIN (UART4_MAP1_RX_B1)
#define BY_TF_UART_INDEX (UART_4)
#define BY_TF_UART_BAUDRATE (115200)
#define BY_TF_PARSE_BUFFER_SIZE (50)
// 注释此项则为主机,否则为从机
#define BY_TF_DEVICE_SLAVE
/********** 从机模式配置选项 **********/
#if defined(BY_TF_DEVICE_SLAVE)
// 从机地址 (多从机通信时注意修改地址,避免冲突)
#define BY_TF_DEVICE_SLAVE_ADDRESS (0x0D)
/********** 主机模式配置选项 **********/
#else
#define BY_TF_DEVICE_MASTER
// 监听/解析 超时时间 单位毫秒
#define BY_TF_PARSE_TIMEOUT (200)
#endif
#endif

View File

@@ -1,53 +0,0 @@
#include "by_tiny_frame_master_read.h"
#if defined(BY_TF_DEVICE_MASTER)
uint8_t read_processing_flag;
uint32_t *data_p;
void by_tiny_frame_read_run(void)
{
}
void by_tiny_frame_read(uint8_t slave_id, uint16_t reg_addr, uint32_t *data)
{
// 填充数据
by_tf_pack_frame_t frame_s;
frame_s.slave_id = slave_id;
frame_s.cmd = BY_TINY_FRAME_READ_CMD_CODE;
frame_s.reg_addr = reg_addr;
frame_s.data = 0;
// 发送写请求
by_tiny_frame_pack_send(&frame_s);
// 设置响应监听 id
by_tiny_frame_parse_set_listen_slave_id(slave_id);
// 注册响应监听回调
by_tiny_frame_parse_handle_register(by_tiny_frame_read_handle);
// 开启响应监听
by_tiny_frame_parse_start_listen();
read_processing_flag = 1;
}
void by_tiny_frame_read_handle(by_tf_parse_frame_t frame_s, uint8_t status)
{
read_processing_flag = 0;
if (!status) {
*data_p = frame_s.data;
}
#if (BY_TF_DEBUG)
printf("****** READ REGISTER DONE ******\r\n");
printf("SLAVE ID: 0x%0.2X\r\n", frame_s.frame[0]);
printf("\t--cmd: %0.2X\n\t--reg_addr: 0x%0.4X\n\t--data: 0x%0.8X\r\n", frame_s.cmd, frame_s.reg_addr, frame_s.data);
if (status) {
printf("read operation failed!!!\r\n");
} else {
printf("read operation successful!!!\r\n");
}
#endif
}
#endif

View File

@@ -1,18 +0,0 @@
#ifndef _BY_TINY_FRAME_MASTER_READ_H__
#define _BY_TINY_FRAME_MASTER_READ_H__
#include "by_tiny_frame_config.h"
#if defined(BY_TF_DEVICE_MASTER)
#include "by_tiny_frame_parse.h"
#include "by_tiny_frame_pack.h"
#define BY_TINY_FRAME_READ_CMD_CODE (0x03)
extern void by_tiny_frame_read(uint8_t slave_id, uint16_t reg_addr, uint32_t *data);
extern void by_tiny_frame_read_run(void);
extern void by_tiny_frame_read_handle(by_tf_parse_frame_t frame_s, uint8_t status);
#endif
#endif

View File

@@ -1,54 +0,0 @@
#include "by_tiny_frame_master_write.h"
#if defined(BY_TF_DEVICE_MASTER)
#include "by_tiny_frame_pack.h"
#include "by_tiny_frame_parse.h"
uint8_t write_processing_flag;
void by_tiny_frame_write_run(void)
{
// nothing
}
void by_tiny_frame_write(uint8_t slave_id, uint16_t reg_addr, uint32_t data)
{
// 填充数据
by_tf_pack_frame_t frame_s;
frame_s.slave_id = slave_id;
frame_s.cmd = BY_TINY_FRAME_WRITE_CMD_CODE;
frame_s.reg_addr = reg_addr;
frame_s.data = data;
// 发送写请求
by_tiny_frame_pack_send(&frame_s);
// 设置响应监听 id
by_tiny_frame_parse_set_listen_slave_id(slave_id);
// 注册响应监听回调
by_tiny_frame_parse_handle_register(by_tiny_frame_write_handle);
// 开启响应监听
by_tiny_frame_parse_start_listen();
write_processing_flag = 1;
}
void by_tiny_frame_write_handle(by_tf_parse_frame_t frame_s, uint8_t status)
{
write_processing_flag = 0;
#if (BY_TF_DEBUG)
printf("****** WRITE REGISTER DONE ******\r\n");
printf("SLAVE ID: 0x%0.2X\r\n", frame_s.frame[0]);
printf("\t--cmd: %0.2X\n\t--reg_addr: 0x%0.4X\n\t--data: 0x%0.8X\r\n", frame_s.cmd, frame_s.reg_addr, frame_s.data);
if (status) {
printf("write operation failed!!!\r\n");
} else {
printf("write operation successful!!!\r\n");
}
#endif
}
#endif

View File

@@ -1,18 +0,0 @@
#ifndef _BY_TINY_FRAME_MASTER_WRITE_H__
#define _BY_TINY_FRAME_MASTER_WRITE_H__
#include "by_tiny_frame_config.h"
#if defined(BY_TF_DEVICE_MASTER)
#include "by_tiny_frame_parse.h"
#include "by_tiny_frame_pack.h"
#define BY_TINY_FRAME_WRITE_CMD_CODE (0x06)
extern void by_tiny_frame_write(uint8_t slave_id, uint16_t reg_addr, uint32_t data);
extern void by_tiny_frame_write_run(void);
extern void by_tiny_frame_write_handle(by_tf_parse_frame_t frame_s, uint8_t status);
#endif
#endif

View File

@@ -1,39 +0,0 @@
#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));
}

View File

@@ -1,24 +0,0 @@
#ifndef _BY_TINY_FRAME_PACK_H__
#define _BY_TINY_FRAME_PACK_H__
#include <stdio.h>
#include <stdint.h>
#include "by_tiny_frame_config.h"
// 从机地址 (1b) - 功能码 (1b) - 寄存器地址 (2b) - 数据 (4b) - CRC(2b)
// 从机地址 (1b) 0-127, 最低位表示发送方,主机请求低位为 0从机应答低位为 1
// 高字节在前
typedef struct by_tf_pack_frame_t {
uint8_t frame[10];
uint8_t slave_id;
uint8_t cmd;
uint16_t reg_addr;
uint32_t data;
} by_tf_pack_frame_t;
extern void by_tiny_frame_pack_init(void);
extern void by_tiny_frame_pack_send(by_tf_pack_frame_t *frame_s);
#endif

View File

@@ -1,198 +0,0 @@
#include "by_tiny_frame_parse.h"
#include "crc16.h"
#include "lwrb.h"
lwrb_t lwrb_struct;
uint8_t buffer_rb[BY_TF_PARSE_BUFFER_SIZE];
uint8_t buffer_out;
uint8_t listen_slave_id;
uint8_t listen_flag;
uint16_t listen_timeout;
uint16_t listen_timevia;
by_tf_parse_frame_t frame_now;
by_tf_parse_done_handle_func parse_done_handle;
void by_tiny_frame_parse_init(void)
{
#if defined(BY_TF_DEVICE_MASTER)
listen_timeout = BY_TF_PARSE_TIMEOUT;
#endif
/** 初始化环形缓冲区 **/
lwrb_init(&lwrb_struct, buffer_rb, 40);
}
uint8_t by_tiny_frame_parse_listening(by_tf_parse_frame_t *frame_s, const uint8_t slave_id, const uint8_t buff)
{
static uint8_t cnt_s = 0;
static uint8_t cnt_rest_s = 0;
#if (BY_TF_DEBUG)
printf("%0.2X\r\n", buff);
#endif
do {
#if defined(BY_TF_DEVICE_SLAVE)
if ((0 == cnt_s) && ((slave_id << 1) == buff)) {
#else
if ((0 == cnt_s) && (((slave_id << 1) + 1) == buff)) {
#endif
memset(frame_s, 0, sizeof(*frame_s));
cnt_s = 1;
cnt_rest_s = 9;
frame_s->frame[0] = buff;
break;
}
if (1 <= cnt_s) {
frame_s->frame[cnt_s] = buff;
cnt_s++;
}
if (0 == --cnt_rest_s) {
cnt_s = 0;
frame_s->cmd = frame_s->frame[1];
frame_s->reg_addr |= ((uint16_t)frame_s->frame[2] << 8);
frame_s->reg_addr |= (uint16_t)frame_s->frame[3];
frame_s->data |= ((uint32_t)frame_s->frame[4] << 24);
frame_s->data |= ((uint32_t)frame_s->frame[5] << 16);
frame_s->data |= ((uint32_t)frame_s->frame[6] << 8);
frame_s->data |= (uint32_t)frame_s->frame[7];
frame_s->crc_val |= ((uint16_t)frame_s->frame[8] << 8);
frame_s->crc_val |= (uint16_t)frame_s->frame[9];
return 0;
}
} while (0);
return 1;
}
/**
* @brief by_tf_parse 串口回调函数,在对应串口中断函数中调用
*
* @param buff
*/
void by_tiny_frame_parse_uart_handle(uint8_t buff)
{
lwrb_write(&lwrb_struct, &buff, 1);
}
/**
* @brief by_tf_parse 定时回调函数,要求触发周期为 1ms
*
*/
void by_tiny_frame_parse_timer_handle(void)
{
#if defined(BY_TF_DEVICE_MASTER)
if (listen_flag) {
listen_timevia++;
} else {
listen_timevia = 0;
}
#endif
}
/**
* @brief
*
*/
void by_tiny_frame_parse_run(void)
{
#if defined(BY_TF_DEVICE_MASTER)
if (0 == listen_flag) {
return;
} else {
if (listen_timeout <= listen_timevia) {
// 接收超时,停止监听
parse_done_handle(frame_now, 1);
by_tiny_frame_parse_end_listen();
#if (BY_TF_DEBUG)
printf("by_tf_listen timeout\r\n");
#endif
}
}
#endif
for (uint8_t i = 0; i < lwrb_get_full(&lwrb_struct); i++) {
if (!lwrb_read(&lwrb_struct, &buffer_out, 1)) {
break;
}
// TODO 目前接收校验错误也会等待直至超时
// TODO 待结合 read&wirte 部分修改监听的从机地址
#if defined(BY_TF_DEVICE_SLAVE)
if (!by_tiny_frame_parse_listening(&frame_now, BY_TF_DEVICE_SLAVE_ADDRESS, buffer_out))
#else
if (!by_tiny_frame_parse_listening(&frame_now, listen_slave_id, buffer_out))
#endif
{
if (!by_tiny_frame_parse_crc(&frame_now)) {
// 接收成功后停止监听
by_tiny_frame_parse_end_listen();
// 解析成功回调
parse_done_handle(frame_now, 0);
#if (BY_TF_DEBUG)
printf("frame parsed!\r\n");
#endif
// 解析帧
}
}
// if (!mp_cmd_parse_modbus_handle(data)) {
// mp_cmd_mb_parse(&mp_cmd_mb_now, &mp_cmd_parsed_now);
// }
}
}
uint8_t by_tiny_frame_parse_crc(by_tf_parse_frame_t *frame_s)
{
uint16_t calc_crc_val = 0;
calc_crc_val = crc16_check(frame_s->frame, (sizeof(frame_s->frame) - 2));
#if (BY_TF_DEBUG)
printf("get: %0.2X", frame_s->crc_val);
printf("\r\n");
printf("cal: %0.2X", calc_crc_val);
printf("\r\n");
#endif
if ((frame_s->crc_val == calc_crc_val) || (frame_s->crc_val == 0xFFFF)) {
return 0;
}
// 校验错误则直接结束监听
by_tiny_frame_parse_end_listen();
parse_done_handle(frame_now, 1);
return 1;
}
void by_tiny_frame_parse_handle_register(by_tf_parse_done_handle_func func)
{
// FIXME 监听过程中应不允许更改
// FIXME 未校验是否传入非空值,另外假设未执行注册,也会产生非法访问
parse_done_handle = func;
}
void by_tiny_frame_parse_start_listen(void)
{
listen_flag = 1;
}
void by_tiny_frame_parse_end_listen(void)
{
listen_flag = 0;
}
void by_tiny_frame_parse_set_listen_slave_id(uint8_t slave_id)
{
listen_slave_id = slave_id;
}

View File

@@ -1,32 +0,0 @@
#ifndef _BY_TINY_FRAME_PARSE_H__
#define _BY_TINY_FRAME_PARSE_H__
#include <stdio.h>
#include <stdint.h>
#include "by_tiny_frame_config.h"
// 从机地址 (1b) - 功能码 (1b) - 寄存器地址 (2b) - 数据 (4b) - CRC(2b)
// 从机地址 (1b) 0-127, 最低位表示发送方,主机请求低位为 0从机应答低位为 1
// 高字节在前
typedef struct by_tf_parse_frame_t {
uint8_t frame[10];
uint8_t cmd;
uint16_t reg_addr;
uint16_t crc_val;
uint32_t data;
} by_tf_parse_frame_t;
typedef void (*by_tf_parse_done_handle_func)(by_tf_parse_frame_t, uint8_t);
extern void by_tiny_frame_parse_init(void);
extern void by_tiny_frame_parse_uart_handle(uint8_t buff);
extern void by_tiny_frame_parse_run(void);
extern uint8_t by_tiny_frame_parse_crc(by_tf_parse_frame_t *frame_s);
extern void by_tiny_frame_parse_handle_register(by_tf_parse_done_handle_func func);
extern void by_tiny_frame_parse_start_listen(void);
extern void by_tiny_frame_parse_end_listen(void);
extern void by_tiny_frame_parse_set_listen_slave_id(uint8_t slave_id);
#endif

View File

@@ -1,59 +0,0 @@
#include "by_tiny_frame_slave_read_write.h"
#if defined(BY_TF_DEVICE_SLAVE)
#include "by_tiny_frame_config.h"
#include "by_tiny_frame_parse.h"
#include "by_tiny_frame_pack.h"
#include "jj_motion.h"
void by_tiny_frame_read_write_run(void)
{
// empty
}
void by_tiny_frame_read_write_handle(by_tf_parse_frame_t frame_s, uint8_t status)
{
by_tf_pack_frame_t frame_pack_s;
frame_pack_s.slave_id = BY_TF_DEVICE_SLAVE_ADDRESS;
frame_pack_s.cmd = frame_s.cmd;
frame_pack_s.reg_addr = frame_s.reg_addr;
if (status) {
// 接收出错,一般为 CRC 校验错误
return;
}
switch (frame_s.cmd) {
case 0x03:
// 添加查询接口,操作完成后应答,主机读取,这个是从机
frame_pack_s.data = 0XFFFFFFFF; //it's useless just now
by_tiny_frame_pack_send(&frame_pack_s);
break;
case 0x06:
// 添加写入接口,操作完成后应答
switch (frame_pack_s.reg_addr) {
case 0x00:
in_angle = (float)frame_s.data;
break;
case 0x01:
in_pos = (float)frame_s.data;
break;
default:
break;
}
by_tiny_frame_pack_send(&frame_pack_s);
break;
default:
break;
}
#if (BY_TF_DEBUG)
printf("****** EXECUTE CMD SUCCESSFUL ******\r\n");
printf("Device ID: 0x%0.2X\r\n", BY_TF_DEVICE_SLAVE_ADDRESS);
printf("\t--cmd: %0.2X\n\t--reg_addr: 0x%0.4X\n\t--data: 0x%0.8X\r\n", frame_s.cmd, frame_s.reg_addr, frame_s.data);
#endif
}
#endif

View File

@@ -1,17 +0,0 @@
#ifndef _BY_TINY_FRAME_SLAVE_READ_WRITE_H__
#define _BY_TINY_FRAME_SLAVE_READ_WRITE_H__
#include "by_tiny_frame_config.h"
#if defined(BY_TF_DEVICE_SLAVE)
#include "by_tiny_frame_parse.h"
#define BY_TINY_FRAME_READ_CMD_CODE (0x03)
#define BY_TINY_FRAME_WRITE_CMD_CODE (0x06)
extern void by_tiny_frame_read_write_run(void);
extern void by_tiny_frame_read_write_handle(by_tf_parse_frame_t frame_s, uint8_t status);
#endif
#endif