feat: 移植从机通信帧协议
This commit is contained in:
15
app/isr.c
15
app/isr.c
@@ -34,12 +34,15 @@
|
||||
********************************************************************************************************************/
|
||||
|
||||
#include "zf_common_headfile.h"
|
||||
#include "by_rt_button.h"
|
||||
|
||||
#include "by_imu.h"
|
||||
#include "jj_blueteeth.h"
|
||||
#include "by_buzzer.h"
|
||||
#include "by_rt_button.h"
|
||||
#include "by_tiny_frame_parse.h"
|
||||
|
||||
#include "jj_motion.h"
|
||||
#include "jj_blueteeth.h"
|
||||
#include "jj_blueteeth.h"
|
||||
|
||||
void NMI_Handler(void) __attribute__((interrupt()));
|
||||
void HardFault_Handler(void) __attribute__((interrupt()));
|
||||
@@ -87,7 +90,9 @@ void EXTI15_10_IRQHandler(void) __attribute__((interrupt()));
|
||||
void USART1_IRQHandler(void)
|
||||
{
|
||||
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
|
||||
|
||||
uint8_t data_s = 0;
|
||||
uart_query_byte(UART_1, &data_s);
|
||||
by_tiny_frame_parse_uart_handle(data_s);
|
||||
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
|
||||
}
|
||||
}
|
||||
@@ -102,10 +107,6 @@ 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>ȥ
|
||||
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
|
||||
}
|
||||
}
|
||||
void UART4_IRQHandler(void)
|
||||
|
||||
35
app/main.c
35
app/main.c
@@ -21,19 +21,24 @@
|
||||
* 许可证副本在 libraries 文件夹下 即该文件夹下的 LICENSE 文件
|
||||
* 欢迎各位使用并传播本程序 但修改内容时必须保留逐飞科技的版权声明(即本声明)
|
||||
********************************************************************************************************************/
|
||||
|
||||
#include "page.h"
|
||||
#include "page_ui_widget.h"
|
||||
|
||||
#include "gl_headfile.h"
|
||||
|
||||
#include "jj_param.h"
|
||||
#include "jj_motion.h"
|
||||
#include "jj_blueteeth.h"
|
||||
|
||||
#include "by_imu.h"
|
||||
#include "by_buzzer.h"
|
||||
#include "by_tiny_frame.h"
|
||||
#include "by_rt_button.h"
|
||||
#include "by_fan_control.h"
|
||||
#include "./page/page.h"
|
||||
#include "jj_blueteeth.h"
|
||||
#include "jj_param.h"
|
||||
#include "./page/page_ui_widget.h"
|
||||
#include "by_buzzer.h"
|
||||
#include "jj_motion.h"
|
||||
#include "by_imu.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int16_t i = 0;
|
||||
clock_init(SYSTEM_CLOCK_120M);
|
||||
system_delay_init();
|
||||
debug_init();
|
||||
@@ -41,13 +46,23 @@ int main(void)
|
||||
ips200_init(IPS200_TYPE_SPI);
|
||||
|
||||
by_rb_init();
|
||||
by_buzzer_init();
|
||||
by_pwm_init();
|
||||
|
||||
by_buzzer_init();
|
||||
by_tiny_frame_init();
|
||||
|
||||
Page_Init();
|
||||
|
||||
printf("ok\r\n");
|
||||
|
||||
while (1) {
|
||||
by_buzzer_run();
|
||||
Page_Run();
|
||||
|
||||
/** 测试完成后移除 **/
|
||||
by_tiny_frame_run();
|
||||
// by_tiny_frame_pack_send(&frame_now);
|
||||
system_delay_ms(10);
|
||||
by_tiny_frame_parse_timer_handle();
|
||||
/** 测试完成后移除 **/
|
||||
}
|
||||
}
|
||||
|
||||
37
app/tiny_frame/by_tiny_frame.c
Normal file
37
app/tiny_frame/by_tiny_frame.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "by_tiny_frame.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
}
|
||||
16
app/tiny_frame/by_tiny_frame.h
Normal file
16
app/tiny_frame/by_tiny_frame.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#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
|
||||
27
app/tiny_frame/by_tiny_frame_config.h
Normal file
27
app/tiny_frame/by_tiny_frame_config.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef _BY_TINY_FRAME_CONFIG_H__
|
||||
#define _BY_TINY_FRAME_CONFIG_H__
|
||||
|
||||
#define BY_TF_DEBUG (1)
|
||||
|
||||
#define BY_TF_UART_TX_PIN (UART1_MAP0_TX_A9)
|
||||
#define BY_TF_UART_RX_PIN (UART1_MAP0_RX_A10)
|
||||
#define BY_TF_UART_INDEX (UART_1)
|
||||
#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
|
||||
53
app/tiny_frame/by_tiny_frame_master_read.c
Normal file
53
app/tiny_frame/by_tiny_frame_master_read.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#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
|
||||
18
app/tiny_frame/by_tiny_frame_master_read.h
Normal file
18
app/tiny_frame/by_tiny_frame_master_read.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#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
|
||||
54
app/tiny_frame/by_tiny_frame_master_write.c
Normal file
54
app/tiny_frame/by_tiny_frame_master_write.c
Normal file
@@ -0,0 +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)
|
||||
{
|
||||
// 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
|
||||
18
app/tiny_frame/by_tiny_frame_master_write.h
Normal file
18
app/tiny_frame/by_tiny_frame_master_write.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#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
|
||||
39
app/tiny_frame/by_tiny_frame_pack.c
Normal file
39
app/tiny_frame/by_tiny_frame_pack.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "by_tiny_frame_pack.h"
|
||||
|
||||
#include <string.h>
|
||||
#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));
|
||||
}
|
||||
24
app/tiny_frame/by_tiny_frame_pack.h
Normal file
24
app/tiny_frame/by_tiny_frame_pack.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef _BY_TINY_FRAME_PACK_H__
|
||||
#define _BY_TINY_FRAME_PACK_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_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
|
||||
198
app/tiny_frame/by_tiny_frame_parse.c
Normal file
198
app/tiny_frame/by_tiny_frame_parse.c
Normal file
@@ -0,0 +1,198 @@
|
||||
#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;
|
||||
}
|
||||
32
app/tiny_frame/by_tiny_frame_parse.h
Normal file
32
app/tiny_frame/by_tiny_frame_parse.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#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;
|
||||
|
||||
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
|
||||
48
app/tiny_frame/by_tiny_frame_slave_read_write.c
Normal file
48
app/tiny_frame/by_tiny_frame_slave_read_write.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#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"
|
||||
|
||||
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; // 示例
|
||||
by_tiny_frame_pack_send(&frame_pack_s);
|
||||
break;
|
||||
case 0x06:
|
||||
// 添加写入接口,操作完成后应答
|
||||
frame_pack_s.data = frame_s.data;
|
||||
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
|
||||
17
app/tiny_frame/by_tiny_frame_slave_read_write.h
Normal file
17
app/tiny_frame/by_tiny_frame_slave_read_write.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user