diff --git a/app/main.c b/app/main.c index b1a9fb4..e093915 100644 --- a/app/main.c +++ b/app/main.c @@ -52,7 +52,7 @@ int main(void) clock_init(SYSTEM_CLOCK_144M); system_delay_init(); debug_init(); - // mt9v03x_init(); + mt9v03x_init(); ips200_init(IPS200_TYPE_SPI); by_led_init(); @@ -72,7 +72,7 @@ int main(void) /** 测试完成后移除 **/ // by_tiny_frame_parse_handle_register(test); - by_tiny_frame_parse_start_listern(); + // by_tiny_frame_parse_start_listen(); by_tf_pack_frame_t frame_now; @@ -82,6 +82,7 @@ int main(void) frame_now.slave_id = 0x0D; /** 测试完成后移除 **/ + by_tiny_frame_write(0x0D, 0x4059, 0x19260817); while (1) { Page_Run(); by_buzzer_run(); @@ -93,22 +94,22 @@ int main(void) by_tiny_frame_parse_timer_handle(); /** 测试完成后移除 **/ - // if (mt9v03x_finish_flag) { - // // 该操作消耗大概 1970 个 tick,折合约 110us - // memcpy(mt9v03x_image_copy[0], mt9v03x_image[0], (sizeof(mt9v03x_image_copy) / sizeof(uint8_t))); - // // adaptiveThreshold((uint8_t*)mt9v03x_image_copy, (uint8_t*)mt9v03x_image_copy, 188, 120, 7, 17); - // // ips200_show_gray_image(0, 0, mt9v03x_image_copy[0], MT9V03X_W, MT9V03X_H, MT9V03X_W, MT9V03X_H, 0); - // mt9v03x_finish_flag = 0; - // by_led_info_blink(); - // state_type = COMMON_STATE; - // img_processing(); - // get_corners(); - // aim_distance = COMMON_AIM; - // tracking(); - // ElementJudge(); - // ElementRun(); - // MidLineTrack(); - // } + if (mt9v03x_finish_flag) { + // 该操作消耗大概 1970 个 tick,折合约 110us + memcpy(mt9v03x_image_copy[0], mt9v03x_image[0], (sizeof(mt9v03x_image_copy) / sizeof(uint8_t))); + // adaptiveThreshold((uint8_t*)mt9v03x_image_copy, (uint8_t*)mt9v03x_image_copy, 188, 120, 7, 17); + // ips200_show_gray_image(0, 0, mt9v03x_image_copy[0], MT9V03X_W, MT9V03X_H, MT9V03X_W, MT9V03X_H, 0); + mt9v03x_finish_flag = 0; + by_led_info_blink(); + state_type = COMMON_STATE; + img_processing(); + get_corners(); + aim_distance = COMMON_AIM; + tracking(); + ElementJudge(); + ElementRun(); + MidLineTrack(); + } } } diff --git a/app/tiny_frame/by_tiny_frame.h b/app/tiny_frame/by_tiny_frame.h index 2bd0e82..6f59817 100644 --- a/app/tiny_frame/by_tiny_frame.h +++ b/app/tiny_frame/by_tiny_frame.h @@ -3,6 +3,13 @@ #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); diff --git a/app/tiny_frame/by_tiny_frame_config.h b/app/tiny_frame/by_tiny_frame_config.h index ebc3549..a302e29 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 443a8c2..ccb0041 100644 --- a/app/tiny_frame/by_tiny_frame_master_read.c +++ b/app/tiny_frame/by_tiny_frame_master_read.c @@ -1,5 +1,9 @@ #include "by_tiny_frame_master_read.h" +#if defined(BY_TF_DEVICE_MASTER) + void by_tiny_frame_read_run(void) { -} \ No newline at end of file +} + +#endif diff --git a/app/tiny_frame/by_tiny_frame_master_write.c b/app/tiny_frame/by_tiny_frame_master_write.c index 9fc7047..b2f6f6c 100644 --- a/app/tiny_frame/by_tiny_frame_master_write.c +++ b/app/tiny_frame/by_tiny_frame_master_write.c @@ -1,5 +1,54 @@ #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) { -} \ No newline at end of file + // 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 diff --git a/app/tiny_frame/by_tiny_frame_master_write.h b/app/tiny_frame/by_tiny_frame_master_write.h index 9e4a237..50238d1 100644 --- a/app/tiny_frame/by_tiny_frame_master_write.h +++ b/app/tiny_frame/by_tiny_frame_master_write.h @@ -8,10 +8,11 @@ #include "by_tiny_frame_parse.h" #include "by_tiny_frame_pack.h" -#define BY_TINY_FRAME_WRITE_CMD_CODE (0x06) +#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 diff --git a/app/tiny_frame/by_tiny_frame_parse.c b/app/tiny_frame/by_tiny_frame_parse.c index bc0ce92..783c42e 100644 --- a/app/tiny_frame/by_tiny_frame_parse.c +++ b/app/tiny_frame/by_tiny_frame_parse.c @@ -6,17 +6,17 @@ lwrb_t lwrb_struct; uint8_t buffer_rb[BY_TF_PARSE_BUFFER_SIZE]; uint8_t buffer_out; -uint8_t listern_slave_id; -uint8_t listern_flag; -uint16_t listern_timeout; -uint16_t listern_timevia; +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) - listern_timeout = BY_TF_PARSE_TIMEOUT; + listen_timeout = BY_TF_PARSE_TIMEOUT; #endif /** 初始化环形缓冲区 **/ @@ -88,10 +88,10 @@ void by_tiny_frame_parse_uart_handle(uint8_t buff) void by_tiny_frame_parse_timer_handle(void) { #if defined(BY_TF_DEVICE_MASTER) - if (listern_flag) { - listern_timevia++; + if (listen_flag) { + listen_timevia++; } else { - listern_timevia = 0; + listen_timevia = 0; } #endif } @@ -104,15 +104,15 @@ void by_tiny_frame_parse_run(void) { #if defined(BY_TF_DEVICE_MASTER) - if (0 == listern_flag) { + if (0 == listen_flag) { return; } else { - if (listern_timeout <= listern_timevia) { + if (listen_timeout <= listen_timevia) { // 接收超时,停止监听 parse_done_handle(frame_now, 1); - by_tiny_frame_parse_end_listern(); + by_tiny_frame_parse_end_listen(); #if (BY_TF_DEBUG) - printf("by_tf_listern timeout\r\n"); + printf("by_tf_listen timeout\r\n"); #endif } } @@ -129,13 +129,13 @@ void by_tiny_frame_parse_run(void) #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, listern_slave_id, buffer_out)) + 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_listern(); + by_tiny_frame_parse_end_listen(); // 解析成功回调 parse_done_handle(frame_now, 0); #if (BY_TF_DEBUG) @@ -169,7 +169,7 @@ uint8_t by_tiny_frame_parse_crc(by_tf_parse_frame_t *frame_s) } // 校验错误则直接结束监听 - by_tiny_frame_parse_end_listern(); + by_tiny_frame_parse_end_listen(); parse_done_handle(frame_now, 1); return 1; @@ -177,16 +177,22 @@ uint8_t by_tiny_frame_parse_crc(by_tf_parse_frame_t *frame_s) 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_listern(void) +void by_tiny_frame_parse_start_listen(void) { - listern_flag = 1; + listen_flag = 1; } -void by_tiny_frame_parse_end_listern(void) +void by_tiny_frame_parse_end_listen(void) { - listern_flag = 0; + listen_flag = 0; } + +void by_tiny_frame_parse_set_listen_slave_id(uint8_t slave_id) +{ + listen_slave_id = slave_id; +} \ No newline at end of file diff --git a/app/tiny_frame/by_tiny_frame_parse.h b/app/tiny_frame/by_tiny_frame_parse.h index 800d80c..4ac6c0e 100644 --- a/app/tiny_frame/by_tiny_frame_parse.h +++ b/app/tiny_frame/by_tiny_frame_parse.h @@ -25,7 +25,8 @@ 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_listern(void); -extern void by_tiny_frame_parse_end_listern(void); +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 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 9883ce6..c0fd7c4 100644 --- a/app/tiny_frame/by_tiny_frame_slave_read_write.c +++ b/app/tiny_frame/by_tiny_frame_slave_read_write.c @@ -1,5 +1,8 @@ #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" @@ -39,6 +42,7 @@ void by_tiny_frame_read_write_handle(by_tf_parse_frame_t frame_s, uint8_t status #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); + 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