From 7e7b22c992c89f38fea0702e3feb725cefd66a42 Mon Sep 17 00:00:00 2001 From: bmy <2583236812@qq.com> Date: Mon, 4 Mar 2024 15:05:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8D=A2=E7=94=A8=E6=9B=B4=E7=AE=80?= =?UTF-8?q?=E5=8D=95=E7=9A=84=E9=80=9A=E4=BF=A1=E5=B8=A7=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/by_frame.c | 90 ++++++++ app/by_frame.h | 24 +++ app/isr.c | 6 +- app/jj_motion.c | 7 +- app/main.c | 26 ++- app/tiny_frame/by_tiny_frame.c | 37 ---- app/tiny_frame/by_tiny_frame.h | 16 -- app/tiny_frame/by_tiny_frame_config.h | 27 --- app/tiny_frame/by_tiny_frame_master_read.c | 53 ----- app/tiny_frame/by_tiny_frame_master_read.h | 18 -- app/tiny_frame/by_tiny_frame_master_write.c | 54 ----- app/tiny_frame/by_tiny_frame_master_write.h | 18 -- app/tiny_frame/by_tiny_frame_pack.c | 39 ---- app/tiny_frame/by_tiny_frame_pack.h | 24 --- app/tiny_frame/by_tiny_frame_parse.c | 198 ------------------ app/tiny_frame/by_tiny_frame_parse.h | 32 --- .../by_tiny_frame_slave_read_write.c | 59 ------ .../by_tiny_frame_slave_read_write.h | 17 -- libraries/zf_device/zf_device_ips200.h | 2 +- libraries/zf_driver/zf_driver_pit.c | 22 +- 20 files changed, 149 insertions(+), 620 deletions(-) create mode 100644 app/by_frame.c create mode 100644 app/by_frame.h delete mode 100644 app/tiny_frame/by_tiny_frame.c delete mode 100644 app/tiny_frame/by_tiny_frame.h delete mode 100644 app/tiny_frame/by_tiny_frame_config.h delete mode 100644 app/tiny_frame/by_tiny_frame_master_read.c delete mode 100644 app/tiny_frame/by_tiny_frame_master_read.h delete mode 100644 app/tiny_frame/by_tiny_frame_master_write.c delete mode 100644 app/tiny_frame/by_tiny_frame_master_write.h delete mode 100644 app/tiny_frame/by_tiny_frame_pack.c delete mode 100644 app/tiny_frame/by_tiny_frame_pack.h delete mode 100644 app/tiny_frame/by_tiny_frame_parse.c delete mode 100644 app/tiny_frame/by_tiny_frame_parse.h delete mode 100644 app/tiny_frame/by_tiny_frame_slave_read_write.c delete mode 100644 app/tiny_frame/by_tiny_frame_slave_read_write.h diff --git a/app/by_frame.c b/app/by_frame.c new file mode 100644 index 0000000..f52202f --- /dev/null +++ b/app/by_frame.c @@ -0,0 +1,90 @@ +#include "by_frame.h" + +#include +#include + +#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); +} \ No newline at end of file diff --git a/app/by_frame.h b/app/by_frame.h new file mode 100644 index 0000000..2ef7ea3 --- /dev/null +++ b/app/by_frame.h @@ -0,0 +1,24 @@ +#ifndef _BY_FRAME_H__ +#define _BY_FRAME_H__ + +/* BY_TINY_FRAME 的超级减配版本(好吧基本上完全没有关系) + * 主要是等应答还是挺慢的,写数据场景只需要下位机校验数据合理性即可,读数据等应答即可 + * 并且需要同步的参数并不多,所以考虑直接使用定长的特定结构的帧,一帧全部下发 + */ + +#include + +#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 \ No newline at end of file diff --git a/app/isr.c b/app/isr.c index 6ce809e..f919409 100644 --- a/app/isr.c +++ b/app/isr.c @@ -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); } diff --git a/app/jj_motion.c b/app/jj_motion.c index 2aa33e7..7afb589 100644 --- a/app/jj_motion.c +++ b/app/jj_motion.c @@ -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); diff --git a/app/main.c b/app/main.c index 3036a72..b28f0b5 100644 --- a/app/main.c +++ b/app/main.c @@ -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(); } } diff --git a/app/tiny_frame/by_tiny_frame.c b/app/tiny_frame/by_tiny_frame.c deleted file mode 100644 index 86acd75..0000000 --- a/app/tiny_frame/by_tiny_frame.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "by_tiny_frame.h" - -#include -#include - -#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 deleted file mode 100644 index 6f59817..0000000 --- a/app/tiny_frame/by_tiny_frame.h +++ /dev/null @@ -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 diff --git a/app/tiny_frame/by_tiny_frame_config.h b/app/tiny_frame/by_tiny_frame_config.h deleted file mode 100644 index ecc1a6f..0000000 --- a/app/tiny_frame/by_tiny_frame_config.h +++ /dev/null @@ -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 diff --git a/app/tiny_frame/by_tiny_frame_master_read.c b/app/tiny_frame/by_tiny_frame_master_read.c deleted file mode 100644 index de3f551..0000000 --- a/app/tiny_frame/by_tiny_frame_master_read.c +++ /dev/null @@ -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 diff --git a/app/tiny_frame/by_tiny_frame_master_read.h b/app/tiny_frame/by_tiny_frame_master_read.h deleted file mode 100644 index b4256ef..0000000 --- a/app/tiny_frame/by_tiny_frame_master_read.h +++ /dev/null @@ -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 diff --git a/app/tiny_frame/by_tiny_frame_master_write.c b/app/tiny_frame/by_tiny_frame_master_write.c deleted file mode 100644 index b2f6f6c..0000000 --- a/app/tiny_frame/by_tiny_frame_master_write.c +++ /dev/null @@ -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 diff --git a/app/tiny_frame/by_tiny_frame_master_write.h b/app/tiny_frame/by_tiny_frame_master_write.h deleted file mode 100644 index 50238d1..0000000 --- a/app/tiny_frame/by_tiny_frame_master_write.h +++ /dev/null @@ -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 diff --git a/app/tiny_frame/by_tiny_frame_pack.c b/app/tiny_frame/by_tiny_frame_pack.c deleted file mode 100644 index 78107f0..0000000 --- a/app/tiny_frame/by_tiny_frame_pack.c +++ /dev/null @@ -1,39 +0,0 @@ -#include "by_tiny_frame_pack.h" - -#include -#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)); -} diff --git a/app/tiny_frame/by_tiny_frame_pack.h b/app/tiny_frame/by_tiny_frame_pack.h deleted file mode 100644 index 70fbb9d..0000000 --- a/app/tiny_frame/by_tiny_frame_pack.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef _BY_TINY_FRAME_PACK_H__ -#define _BY_TINY_FRAME_PACK_H__ - -#include -#include - -#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 diff --git a/app/tiny_frame/by_tiny_frame_parse.c b/app/tiny_frame/by_tiny_frame_parse.c deleted file mode 100644 index 783c42e..0000000 --- a/app/tiny_frame/by_tiny_frame_parse.c +++ /dev/null @@ -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; -} \ 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 deleted file mode 100644 index 4ac6c0e..0000000 --- a/app/tiny_frame/by_tiny_frame_parse.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef _BY_TINY_FRAME_PARSE_H__ -#define _BY_TINY_FRAME_PARSE_H__ - -#include -#include - -#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 diff --git a/app/tiny_frame/by_tiny_frame_slave_read_write.c b/app/tiny_frame/by_tiny_frame_slave_read_write.c deleted file mode 100644 index 9344cb4..0000000 --- a/app/tiny_frame/by_tiny_frame_slave_read_write.c +++ /dev/null @@ -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 diff --git a/app/tiny_frame/by_tiny_frame_slave_read_write.h b/app/tiny_frame/by_tiny_frame_slave_read_write.h deleted file mode 100644 index b71901c..0000000 --- a/app/tiny_frame/by_tiny_frame_slave_read_write.h +++ /dev/null @@ -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 diff --git a/libraries/zf_device/zf_device_ips200.h b/libraries/zf_device/zf_device_ips200.h index a37ea56..b31c04c 100644 --- a/libraries/zf_device/zf_device_ips200.h +++ b/libraries/zf_device/zf_device_ips200.h @@ -110,7 +110,7 @@ // 例:C5-C12 IPS200_DATAPORT 设置为 GPIOC DATA_START_NUM 设置为 5 // --------------------双排 SPI 接口两寸屏幕引脚定义--------------------// -#define IPS200_DEFAULT_DISPLAY_DIR (IPS200_PORTAIT) // 默认的显示方向 +#define IPS200_DEFAULT_DISPLAY_DIR (IPS200_CROSSWISE) // 默认的显示方向 #define IPS200_DEFAULT_PENCOLOR (RGB565_YELLOW) // 默认的画笔颜色 #define IPS200_DEFAULT_BGCOLOR (RGB565_BLACK) // 默认的背景颜色 #define IPS200_DEFAULT_DISPLAY_FONT (IPS200_8X16_FONT) // 默认的字体模式 diff --git a/libraries/zf_driver/zf_driver_pit.c b/libraries/zf_driver/zf_driver_pit.c index a1e0f60..bb7efc7 100644 --- a/libraries/zf_driver/zf_driver_pit.c +++ b/libraries/zf_driver/zf_driver_pit.c @@ -2,11 +2,11 @@ * CH32V307VCT6 Opensourec Library CH32V307VCT6 Դ⣩һڹٷ SDK ӿڵĵԴ * Copyright (c) 2022 SEEKFREE ɿƼ * -* ļCH32V307VCT6 Դһ +* ļ CH32V307VCT6 Դһ * * CH32V307VCT6 Դ -* Ըᷢ GPLGNU General Public License GNUͨù֤ -* GPL ĵ3棨 GPL3.0ѡģκκİ汾·/޸ +* Ըᷢ GPLGNU General Public License GNU ͨù֤ +* GPL ĵ 3 棨 GPL3.0ѡģκκİ汾·/޸ * * Դķϣܷãδκεı֤ * ûԻʺض;ı֤ @@ -30,7 +30,7 @@ * * ޸ļ¼ * ע -* 2022-09-15 W first version +* 2022-09-15 W first version ********************************************************************************************************************/ @@ -95,9 +95,9 @@ void pit_disable (pit_index_enum pit_n) //------------------------------------------------------------------------------------------------------------------- // ʱж // ˵ timer_ch ʱͨ -// ˵ us ʱ(1-65535) +// ˵ us ʱ (1-65535) // ز void -// עϢ pit_init(TIMER_1, 5); ʹöʱ1Ϊ5msһεж +// עϢ pit_init(TIMER_1, 5); ʹöʱ 1 Ϊ 5ms һεж //------------------------------------------------------------------------------------------------------------------- void pit_init (pit_index_enum pit_n, uint32 period) { @@ -136,10 +136,10 @@ void pit_init (pit_index_enum pit_n, uint32 period) TIM_TimeBaseStructure.TIM_Period = period_temp; TIM_TimeBaseStructure.TIM_Prescaler = freq_div; // Ƶֵ TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // ʱӷָ:TDTS = Tck_tim - TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // TIMϼģʽ - TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; // ظΪ0 - TIM_TimeBaseInit(tim_index, &TIM_TimeBaseStructure); // ָIJʼTIMxʱλ - TIM_ITConfig(tim_index,TIM_IT_Update,ENABLE ); // ʹָTIMж,ж + TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // TIM ϼģʽ + TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; // ظΪ 0 + TIM_TimeBaseInit(tim_index, &TIM_TimeBaseStructure); // ָIJʼ TIMx ʱλ + TIM_ITConfig(tim_index,TIM_IT_Update,ENABLE ); // ʹָ TIM жϣж TIM_ClearITPendingBit(tim_index, TIM_IT_Update); const uint32 irq_index[10] = @@ -156,7 +156,7 @@ void pit_init (pit_index_enum pit_n, uint32 period) TIM10_UP_IRQn }; - interrupt_set_priority((IRQn_Type)irq_index[(uint8)pit_n], 0x03); // жȼ + interrupt_set_priority((IRQn_Type)irq_index[(uint8)pit_n], (1<<5) | 3); // жȼ interrupt_enable((IRQn_Type)irq_index[pit_n]); // ʹж TIM_Cmd(tim_index, ENABLE); // ʹܶʱ