feat: 完成通信帧从机应用层模板

This commit is contained in:
bmy
2024-02-23 22:25:47 +08:00
parent 3a4c25dc4d
commit 399ebeea1a
10 changed files with 87 additions and 10 deletions

View File

@@ -71,7 +71,7 @@ int main(void)
printf("start running\r\n");
/** 测试完成后移除 **/
by_tiny_frame_parse_handle_register(test);
// by_tiny_frame_parse_handle_register(test);
by_tiny_frame_parse_start_listern();
by_tf_pack_frame_t frame_now;
@@ -87,7 +87,7 @@ int main(void)
by_buzzer_run();
/** 测试完成后移除 **/
by_tiny_frame_parse_run();
by_tiny_frame_run();
// by_tiny_frame_pack_send(&frame_now);
system_delay_ms(10);
by_tiny_frame_parse_timer_handle();

View File

@@ -3,9 +3,13 @@
#include <stdio.h>
#include <stdint.h>
#include "by_tiny_frame_parse.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)
{
@@ -14,4 +18,20 @@ void by_tiny_frame_init(void)
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

@@ -4,5 +4,6 @@
#include "by_tiny_frame_config.h"
extern void by_tiny_frame_init(void);
void by_tiny_frame_run(void);
#endif

View File

@@ -11,7 +11,7 @@
#define BY_TF_PARSE_BUFFER_SIZE (50)
// 注释此项则为主机,否则为从机
// #define BY_TF_DEVICE_SLAVE
#define BY_TF_DEVICE_SLAVE
/********** 从机模式配置选项 **********/
#if defined(BY_TF_DEVICE_SLAVE)

View File

@@ -0,0 +1,5 @@
#include "by_tiny_frame_master_read.h"
void by_tiny_frame_read_run(void)
{
}

View File

@@ -5,7 +5,9 @@
#if defined(BY_TF_DEVICE_MASTER)
#define BY_TINY_FRAME_WRITE_CMD_CODE (0x03)
#define BY_TINY_FRAME_READ_CMD_CODE (0x03)
extern void by_tiny_frame_read_run(void);
#endif
#endif

View File

@@ -1,2 +1,5 @@
#include "by_tiny_frame_write.h"
#include "by_tiny_frame_master_write.h"
void by_tiny_frame_write_run(void)
{
}

View File

@@ -10,9 +10,8 @@
#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);
#endif
#endif

View File

@@ -1,2 +1,44 @@
#include "by_tiny_frame_slave_read_write.h"
#include "by_tiny_frame_parse.h"
#include "by_tiny_frame_pack.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; // 示例
by_tiny_frame_pack_send(&frame_pack_s);
break;
case 0x06:
// 添加写入接口,操作完成后应答
frame_pack_s.data = frame_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("--cmd: %0.2X\n--reg_addr: %0.4X\n--data: %0.8X\r\n", frame_s.cmd, frame_s.reg_addr, frame_s.data);
#endif
}

View File

@@ -5,8 +5,13 @@
#if defined(BY_TF_DEVICE_SLAVE)
#define BY_TINY_FRAME_WRITE_CMD_CODE (0x03)
#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