diff --git a/app/main.c b/app/main.c index 93718f8..b1a9fb4 100644 --- a/app/main.c +++ b/app/main.c @@ -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(); diff --git a/app/tiny_frame/by_tiny_frame.c b/app/tiny_frame/by_tiny_frame.c index f59e6c1..86acd75 100644 --- a/app/tiny_frame/by_tiny_frame.c +++ b/app/tiny_frame/by_tiny_frame.c @@ -3,15 +3,35 @@ #include #include -#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) { /*** 初始化相关外设 ***/ 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 +} \ No newline at end of file diff --git a/app/tiny_frame/by_tiny_frame.h b/app/tiny_frame/by_tiny_frame.h index 630f0cf..2bd0e82 100644 --- a/app/tiny_frame/by_tiny_frame.h +++ b/app/tiny_frame/by_tiny_frame.h @@ -4,5 +4,6 @@ #include "by_tiny_frame_config.h" extern void by_tiny_frame_init(void); +void by_tiny_frame_run(void); #endif diff --git a/app/tiny_frame/by_tiny_frame_config.h b/app/tiny_frame/by_tiny_frame_config.h index a302e29..ebc3549 100644 --- a/app/tiny_frame/by_tiny_frame_config.h +++ b/app/tiny_frame/by_tiny_frame_config.h @@ -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) diff --git a/app/tiny_frame/by_tiny_frame_master_read.c b/app/tiny_frame/by_tiny_frame_master_read.c index e69de29..443a8c2 100644 --- a/app/tiny_frame/by_tiny_frame_master_read.c +++ b/app/tiny_frame/by_tiny_frame_master_read.c @@ -0,0 +1,5 @@ +#include "by_tiny_frame_master_read.h" + +void by_tiny_frame_read_run(void) +{ +} \ No newline at end of file diff --git a/app/tiny_frame/by_tiny_frame_master_read.h b/app/tiny_frame/by_tiny_frame_master_read.h index b0453bc..d1b2d71 100644 --- a/app/tiny_frame/by_tiny_frame_master_read.h +++ b/app/tiny_frame/by_tiny_frame_master_read.h @@ -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 diff --git a/app/tiny_frame/by_tiny_frame_master_write.c b/app/tiny_frame/by_tiny_frame_master_write.c index acca88c..9fc7047 100644 --- a/app/tiny_frame/by_tiny_frame_master_write.c +++ b/app/tiny_frame/by_tiny_frame_master_write.c @@ -1,2 +1,5 @@ -#include "by_tiny_frame_write.h" +#include "by_tiny_frame_master_write.h" +void by_tiny_frame_write_run(void) +{ +} \ No newline at end of file diff --git a/app/tiny_frame/by_tiny_frame_master_write.h b/app/tiny_frame/by_tiny_frame_master_write.h index fd1499a..9e4a237 100644 --- a/app/tiny_frame/by_tiny_frame_master_write.h +++ b/app/tiny_frame/by_tiny_frame_master_write.h @@ -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 diff --git a/app/tiny_frame/by_tiny_frame_slave_read_write.c b/app/tiny_frame/by_tiny_frame_slave_read_write.c index f906eb5..9883ce6 100644 --- a/app/tiny_frame/by_tiny_frame_slave_read_write.c +++ b/app/tiny_frame/by_tiny_frame_slave_read_write.c @@ -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 +} diff --git a/app/tiny_frame/by_tiny_frame_slave_read_write.h b/app/tiny_frame/by_tiny_frame_slave_read_write.h index 35f790e..b71901c 100644 --- a/app/tiny_frame/by_tiny_frame_slave_read_write.h +++ b/app/tiny_frame/by_tiny_frame_slave_read_write.h @@ -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