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

真的不是因为原来的不好用!
This commit is contained in:
bmy
2024-03-04 15:02:40 +08:00
parent 00e7c50787
commit 61573e1700
17 changed files with 126 additions and 625 deletions

88
app/by_frame.c Normal file
View File

@@ -0,0 +1,88 @@
#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];
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));
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 (UART2_MAP0_TX_A2)
#define BY_FRAME_UART_RX_PIN (UART2_MAP0_RX_A3)
#define BY_FRAME_UART_INDEX (UART_2)
#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

@@ -34,11 +34,9 @@
********************************************************************************************************************/
#include "zf_common_headfile.h"
#include "by_tiny_frame.h"
#include "by_button.h"
#include "by_buzzer.h"
#include "by_tiny_frame_parse.h"
#include "by_frame.h"
void NMI_Handler(void) __attribute__((interrupt()));
void HardFault_Handler(void) __attribute__((interrupt()));
@@ -95,13 +93,16 @@ void USART2_IRQHandler(void)
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) {
uint8_t data_s = 0;
uart_query_byte(UART_2, &data_s);
by_tiny_frame_parse_uart_handle(data_s);
//
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}
void USART3_IRQHandler(void)
{
if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
uint8_t data_s = 0;
uart_query_byte(UART_3, &data_s);
by_frame_parse_uart_handle(data_s);
#if DEBUG_UART_USE_INTERRUPT // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> debug <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
// debug_interrupr_handler(); // <20><><EFBFBD><EFBFBD> debug <20><><EFBFBD>ڽ<EFBFBD><DABD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>ݻᱻ debug <20><><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
#endif // <20><><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD><DEB8><EFBFBD> DEBUG_UART_INDEX <20><><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD>Ӧ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>ж<EFBFBD>ȥ
@@ -284,7 +285,6 @@ void TIM1_UP_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
by_tiny_frame_parse_timer_handle();
}
}

View File

@@ -24,28 +24,12 @@
#include "zf_common_headfile.h"
#include "gl_headfile.h"
#include "page.h"
#include "by_tiny_frame.h"
#include "by_frame.h"
#include "by_buzzer.h"
#include "by_led.h"
#include "jj_param.h"
#include "jj_blueteeth.h"
/** 测试完成后移除 **/
#include "by_tiny_frame_parse.h"
#include "by_tiny_frame_pack.h"
uint32_t data_test;
/** 测试完成后移除 **/
void test(by_tf_parse_frame_t frame_s, uint8_t status)
{
printf("parse done\r\n");
printf("--cmd: %0.2X\n--reg_addr: %0.4X\n--data: %0.8X\r\n", frame_s.cmd, frame_s.reg_addr, frame_s.data);
if (status) {
printf("noooooooo!\r\n");
} else {
printf("hhhhhhok\r\n");
}
}
int main(void)
{
@@ -66,16 +50,19 @@ int main(void)
pit_ms_init(TIM1_PIT, 1);
by_tiny_frame_init();
by_frame_init();
printf("start running\r\n");
tiny_frame_param[0].f32 = 100.5f;
uint8_t a = 0;
// by_tiny_frame_read(0x0D, 0x4059, &data_test);
while (1) {
Page_Run();
by_buzzer_run();
by_tiny_frame_write(0x0D, 0x0000, tiny_frame_param[0].u32);
by_tiny_frame_run();
system_delay_ms(10);
// by_buzzer_run();
tiny_frame_param[0].f32 += 0.1f;
// by_frame_parse(1, &tiny_frame_param[0].u32);
by_frame_send(1, &tiny_frame_param[0].u32);
// uart_write_byte(BY_FRAME_UART_INDEX, a++);
if (mt9v03x_finish_flag) {
// 该操作消耗大概 1970 个 tick折合约 110us
memcpy(mt9v03x_image_copy[0], mt9v03x_image[0], (sizeof(mt9v03x_image_copy) / sizeof(uint8_t)));
@@ -94,4 +81,3 @@ int main(void)
}
}
}

View File

@@ -1,38 +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();
by_tiny_frame_pack_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 (UART2_MAP0_TX_A2)
#define BY_TF_UART_RX_PIN (UART2_MAP0_RX_A3)
#define BY_TF_UART_INDEX (UART_2)
#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,57 +0,0 @@
#include "by_tiny_frame_master_read.h"
#if defined(BY_TF_DEVICE_MASTER)
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)
{
if (by_tiny_frame_pack_lock) {
// 写入忙处理
return;
}
// 填充数据
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();
by_tiny_frame_pack_lock = 0;
}
void by_tiny_frame_read_handle(by_tf_parse_frame_t frame_s, uint8_t status)
{
by_tiny_frame_pack_lock = 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,57 +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"
void by_tiny_frame_write_run(void)
{
// nothing
}
void by_tiny_frame_write(uint8_t slave_id, uint16_t reg_addr, uint32_t data)
{
if(by_tiny_frame_pack_lock){
//写入忙处理
return;
}
// 填充数据
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();
by_tiny_frame_pack_lock = 1;
}
void by_tiny_frame_write_handle(by_tf_parse_frame_t frame_s, uint8_t status)
{
by_tiny_frame_pack_lock = 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,42 +0,0 @@
#include "by_tiny_frame_pack.h"
#include <string.h>
#include "zf_common_headfile.h"
#include "crc16.h"
uint8_t by_tiny_frame_pack_lock;
void by_tiny_frame_pack_init(void)
{
/** nothing to init **/
by_tiny_frame_pack_lock = 0;
}
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,26 +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);
extern uint8_t by_tiny_frame_pack_lock;
#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,33 +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_timer_handle(void);
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,50 +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_param.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 =(uint32_t)(addre[frame_pack_s.reg_addr]);
by_tiny_frame_pack_send(&frame_pack_s);
break;
case 0x06:
// 添加写入接口,操作完成后应答,主机发送,即写入
*addre[frame_pack_s.reg_addr] = frame_pack_s.data;
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