diff --git a/app/by_frame.c b/app/by_frame.c index f52202f..4df88b4 100644 --- a/app/by_frame.c +++ b/app/by_frame.c @@ -7,16 +7,21 @@ #include "lwrb.h" #include "crc16.h" -lwrb_t lwrb_struct; -uint8_t lwrb_buffer[50]; -uint8_t frame_buffer[100]; +// lwrb_t lwrb_struct; +// uint8_t lwrb_buffer[50]; +uint8_t frame_buffer[50]; +uint8_t frame_buffer_parse[50]; +uint8_t frame_parse_busy; +fifo_struct frame_fifo; void by_frame_init(void) { + fifo_init(&frame_fifo, FIFO_DATA_8BIT, frame_buffer, 30); 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); + frame_parse_busy = 0; + // lwrb_init(&lwrb_struct, lwrb_buffer, 50); } void by_frame_send(uint8_t data_num, uint32_t *data_array) @@ -38,13 +43,21 @@ void by_frame_parse(uint8_t data_num, uint32_t *data_array) { uint8_t cnt = 0; uint8_t cnt_crc = 2; + uint8_t cnt_ptr = 0; uint8_t data = 0; uint8_t data_array_temp[100] = {0}; uint16_t crc_cal = 0; + uint32_t read_length = 50; - if (lwrb_get_full(&lwrb_struct) >= (4 + data_num * sizeof(uint32_t))) { - while (lwrb_read(&lwrb_struct, &data, 1)) { + if (fifo_used(&frame_fifo)) { + fifo_read_buffer(&frame_fifo, frame_buffer_parse, &read_length, FIFO_READ_AND_CLEAN); + while (1) { + if (cnt_ptr < 50) { + data = frame_buffer_parse[cnt_ptr]; + cnt_ptr++; + } // printf("char : %0.2X\r\n", data); + if ((0 == cnt) && (BY_FRAME_HEAD_1 == data)) { cnt = 1; data_array_temp[0] = data; @@ -65,6 +78,7 @@ void by_frame_parse(uint8_t data_num, uint32_t *data_array) if (cnt_crc) { crc_cal |= ((uint16_t)data << (--cnt_crc * 8)); + cnt++; continue; } @@ -72,9 +86,10 @@ void by_frame_parse(uint8_t data_num, uint32_t *data_array) // 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))) { + if (crc_cal == crc16_check(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); // 处理完直接清空缓冲区,避免堆积产生处理阻塞 + // lwrb_reset(&lwrb_struct); // 处理完直接清空缓冲区,避免堆积产生处理阻塞 + memset(data_array_temp, 0, sizeof(data_array_temp)); // printf("parsed done!\r\n"); } @@ -82,9 +97,51 @@ void by_frame_parse(uint8_t data_num, uint32_t *data_array) } } } + // 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(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); // 处理完直接清空缓冲区,避免堆积产生处理阻塞 + // memset(data_array_temp, 0, sizeof(data_array_temp)); + // // printf("parsed done!\r\n"); + // } + + // break; + // } + // } + // } } void by_frame_parse_uart_handle(uint8_t data) { - lwrb_write(&lwrb_struct, &data, 1); + fifo_write_element(&frame_fifo, 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 index 2ef7ea3..7b5460d 100644 --- a/app/by_frame.h +++ b/app/by_frame.h @@ -16,6 +16,8 @@ #define BY_FRAME_UART_INDEX (UART_4) #define BY_FRAME_UART_BAUDRATE (115200) +extern uint8_t frame_buffer[50]; + 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); diff --git a/app/isr.c b/app/isr.c index fb8cd3b..5414c19 100644 --- a/app/isr.c +++ b/app/isr.c @@ -108,9 +108,7 @@ void USART3_IRQHandler(void) 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_frame_parse_uart_handle(data_s); + by_frame_parse_uart_handle(USART_ReceiveData(UART4)); USART_ClearITPendingBit(UART4, USART_IT_RXNE); } } diff --git a/app/main.c b/app/main.c index 1eeb461..b05338b 100644 --- a/app/main.c +++ b/app/main.c @@ -56,7 +56,7 @@ int main(void) Page_Init(); sport_pid_init(); - pit_ms_init(TIM1_PIT, 1); // 运动解算,bianmaqi + pit_ms_init(TIM1_PIT, 1); // 运动解算,编码器 printf("ok\r\n"); @@ -66,13 +66,15 @@ int main(void) // uart_write_byte(UART_8, 0x1F); by_buzzer_run(); jj_bt_run(); - in_pos=test_data[1].f32; - in_angle=test_data[0].f32; + in_pos = test_data[1].f32; + in_angle = test_data[0].f32; ips200_show_float(40, 40, test_data[0].f32, 4, 1); ips200_show_float(40, 60, test_data[1].f32, 4, 1); ips200_show_float(40, 80, in_gyro, 4, 2); ips200_show_float(40, 100, in_speed, 4, 4); - ips200_show_string(0,120,"outang"); ips200_show_float(80,120,out_angle,4,1); - ips200_show_string(0,140,"outgyr"); ips200_show_float(80,140,out_gyro,4,1); + ips200_show_string(0, 120, "outang"); + ips200_show_float(80, 120, out_angle, 4, 1); + ips200_show_string(0, 140, "outgyr"); + ips200_show_float(80, 140, out_gyro, 4, 1); } }