feat: 增加通信帧提取功能

This commit is contained in:
bmy
2024-02-15 22:50:20 +08:00
parent 85fe5a27df
commit 1e982b4954
18 changed files with 1154 additions and 29 deletions

View File

@@ -4,7 +4,6 @@
#include "gl_state.h"
#include "gl_img_process.h"
#include "gl_common.h"
#include "main.h"
#include "gl_handle_img.h"
#include "gl_transform_table.h"
#include "gl_get_corners.h"

View File

@@ -34,9 +34,12 @@
********************************************************************************************************************/
#include "zf_common_headfile.h"
#include "by_tiny_frame.h"
#include "by_button.h"
#include "by_buzzer.h"
#include "by_tiny_frame_parse.h"
void NMI_Handler(void) __attribute__((interrupt()));
void HardFault_Handler(void) __attribute__((interrupt()));
@@ -96,9 +99,12 @@ void USART2_IRQHandler(void)
void USART3_IRQHandler(void)
{
if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
#if DEBUG_UART_USE_INTERRUPT // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> debug <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
debug_interrupr_handler(); // <20><><EFBFBD><EFBFBD> debug <20><><EFBFBD>ڽ<EFBFBD><DABD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>ݻᱻ debug <20><><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
#endif // <20><><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD><DEB8><EFBFBD> DEBUG_UART_INDEX <20><><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD>Ӧ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>ж<EFBFBD>ȥ
#if DEBUG_UART_USE_INTERRUPT // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> debug <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
// debug_interrupr_handler(); // <20><><EFBFBD><EFBFBD> debug <20><><EFBFBD>ڽ<EFBFBD><DABD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>ݻᱻ debug <20><><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
#endif // <20><><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD><DEB8><EFBFBD> DEBUG_UART_INDEX <20><><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD>Ӧ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>ж<EFBFBD>ȥ
uint8_t data_s = 0;
uart_query_byte(UART_3, &data_s);
by_tiny_frame_parse_uart_handle(data_s);
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
}
}

View File

@@ -21,13 +21,17 @@
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
********************************************************************************************************************/
#include "zf_common_headfile.h"
#include "gl_headfile.h"
#include "./page/page.h"
#include "page.h"
#include "by_tiny_frame.h"
#include "by_buzzer.h"
#include "by_led.h"
#include "jj_param.h"
#include "jj_blueteeth.h"
#include "by_tiny_frame_parse.h"
int main(void)
{
@@ -49,25 +53,30 @@ int main(void)
// pit_ms_init(TIM6_PIT, 2);
// pit_ms_init(TIM1_PIT, 2);
by_tiny_frame_init();
printf("start running\r\n");
while (1) {
Page_Run();
by_buzzer_run();
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();
}
by_tiny_frame_parse_run();
system_delay_ms(100);
// 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();
// }
}
}

View File

@@ -1,6 +0,0 @@
#ifndef MAIN_H
#define MAIN_H
#include "zf_common_headfile.h"
#endif // MAIN_H

View File

@@ -0,0 +1,16 @@
#include "by_tiny_frame.h"
#include <stdio.h>
#include <stdint.h>
#include "by_tiny_frame_parse.h"
#include "crc16.h"
#include "zf_common_headfile.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();
}

View File

@@ -0,0 +1,8 @@
#ifndef _BY_TINY_FRAME_H__
#define _BY_TINY_FRAME_H__
#include "by_tiny_frame_config.h"
extern void by_tiny_frame_init(void);
#endif

View File

@@ -0,0 +1,11 @@
#ifndef _BY_TINY_FRAME_CONFIG_H__
#define _BY_TINY_FRAME_CONFIG_H__
#define BY_TF_UART_TX_PIN (UART3_MAP0_TX_B10)
#define BY_TF_UART_RX_PIN (UART3_MAP0_RX_B11)
#define BY_TF_UART_INDEX (UART_3)
#define BY_TF_UART_BAUDRATE (115200)
#define BY_TF_PARSE_BUFFER_SIZE (50)
#endif

View File

@@ -0,0 +1,97 @@
#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;
by_tf_parse_frame_t frame_now;
void by_tiny_frame_parse_init(void)
{
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;
printf("%0.2X\r\n", buff);
do {
if ((0 == cnt_s) && (((slave_id << 1) + 1) == buff)) {
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];
return 0;
}
} while (0);
return 1;
}
void by_tiny_frame_parse_uart_handle(uint8_t buff)
{
lwrb_write(&lwrb_struct, &buff, 1);
}
void by_tiny_frame_parse_run(void)
{
for (uint8_t i = 0; i < lwrb_get_full(&lwrb_struct); i++) {
if (!lwrb_read(&lwrb_struct, &buffer_out, 1)) {
break;
}
if (!by_tiny_frame_parse_listening(&frame_now, 127, buffer_out)) {
if (!by_tiny_frame_parse_crc(&frame_now)) {
printf("frame parsed!\r\n");
}
// 解析帧
}
// 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));
printf("get: %0.2X", frame_s->crc_val);
printf("\r\n");
printf("cal: %0.2X", calc_crc_val);
printf("\r\n");
if ((frame_s->crc_val == calc_crc_val) || (frame_s->crc_val == 0xFFFF)) {
return 0;
}
return 1;
}

View File

@@ -0,0 +1,25 @@
#ifndef _BY_TINY_FRAME_PARSE_H__
#define _BY_TINY_FRAME_PARSE_H__
#include <stdio.h>
#include <stdint.h>
#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;
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);
#endif

View File

View File

@@ -0,0 +1,6 @@
#ifndef _BY_TINY_FRAME_READ_H__
#define _BY_TINY_FRAME_READ_H__
#define BY_TINY_FRAME_WRITE_CMD_CODE (0x03)
#endif

View File

@@ -0,0 +1,2 @@
#include "by_tiny_frame_write.h"

View File

@@ -0,0 +1,6 @@
#ifndef _BY_TINY_FRAME_WRITE_H__
#define _BY_TINY_FRAME_WRITE_H__
#define BY_TINY_FRAME_WRITE_CMD_CODE (0x06)
#endif