2024-03-04 15:05:40 +08:00
|
|
|
#include "by_frame.h"
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "zf_common_headfile.h"
|
|
|
|
|
#include "lwrb.h"
|
|
|
|
|
#include "crc16.h"
|
|
|
|
|
|
2024-03-05 16:34:56 +08:00
|
|
|
// 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;
|
2024-03-04 15:05:40 +08:00
|
|
|
|
|
|
|
|
void by_frame_init(void)
|
|
|
|
|
{
|
2024-03-05 16:34:56 +08:00
|
|
|
fifo_init(&frame_fifo, FIFO_DATA_8BIT, frame_buffer, 30);
|
2024-03-04 15:05:40 +08:00
|
|
|
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);
|
|
|
|
|
|
2024-03-05 16:34:56 +08:00
|
|
|
frame_parse_busy = 0;
|
|
|
|
|
// lwrb_init(&lwrb_struct, lwrb_buffer, 50);
|
2024-03-04 15:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2024-03-05 16:34:56 +08:00
|
|
|
uint8_t cnt_ptr = 0;
|
2024-03-04 15:05:40 +08:00
|
|
|
uint8_t data = 0;
|
|
|
|
|
uint8_t data_array_temp[100] = {0};
|
|
|
|
|
uint16_t crc_cal = 0;
|
2024-03-05 16:34:56 +08:00
|
|
|
uint32_t read_length = 50;
|
|
|
|
|
|
2024-03-10 08:34:33 +08:00
|
|
|
if (fifo_used(&frame_fifo) >= 4 + 4 * data_num) {
|
2024-03-05 16:34:56 +08:00
|
|
|
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++;
|
|
|
|
|
}
|
2024-03-04 15:05:40 +08:00
|
|
|
// printf("char : %0.2X\r\n", data);
|
2024-03-05 16:34:56 +08:00
|
|
|
|
2024-03-04 15:05:40 +08:00
|
|
|
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));
|
2024-03-05 16:34:56 +08:00
|
|
|
cnt++;
|
2024-03-04 15:05:40 +08:00
|
|
|
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) {
|
2024-03-05 16:34:56 +08:00
|
|
|
if (crc_cal == crc16_check(data_array_temp, 2 + data_num * sizeof(uint32_t))) {
|
2024-03-04 15:05:40 +08:00
|
|
|
memcpy(data_array, data_array_temp + 2, data_num * sizeof(uint32_t));
|
2024-03-11 19:35:02 +08:00
|
|
|
fifo_clear(&frame_fifo); // TODO 确认是否有必要清除
|
2024-03-05 16:34:56 +08:00
|
|
|
// lwrb_reset(&lwrb_struct); // 处理完直接清空缓冲区,避免堆积产生处理阻塞
|
|
|
|
|
memset(data_array_temp, 0, sizeof(data_array_temp));
|
2024-03-11 19:32:44 +08:00
|
|
|
for (uint8_t i = 0; i < data_num; i++) {
|
|
|
|
|
for (uint8_t j = 0; j < 4; j++) {
|
|
|
|
|
uart_write_byte(DEBUG_UART_INDEX, data_array[i] >> (j * 8));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
uart_write_byte(DEBUG_UART_INDEX, 0x00);
|
|
|
|
|
uart_write_byte(DEBUG_UART_INDEX, 0x00);
|
|
|
|
|
uart_write_byte(DEBUG_UART_INDEX, 0x80);
|
|
|
|
|
uart_write_byte(DEBUG_UART_INDEX, 0x7F);
|
2024-03-04 15:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-05 16:34:56 +08:00
|
|
|
// 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;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2024-03-04 15:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void by_frame_parse_uart_handle(uint8_t data)
|
|
|
|
|
{
|
2024-03-05 16:34:56 +08:00
|
|
|
fifo_write_element(&frame_fifo, data);
|
|
|
|
|
// lwrb_write(&lwrb_struct, &data, 1);
|
2024-03-04 15:05:40 +08:00
|
|
|
}
|