feat: 完成通信帧组包功能
This commit is contained in:
17
app/main.c
17
app/main.c
@@ -30,7 +30,10 @@
|
|||||||
#include "jj_param.h"
|
#include "jj_param.h"
|
||||||
#include "jj_blueteeth.h"
|
#include "jj_blueteeth.h"
|
||||||
|
|
||||||
|
/** 测试完成后移除 **/
|
||||||
#include "by_tiny_frame_parse.h"
|
#include "by_tiny_frame_parse.h"
|
||||||
|
#include "by_tiny_frame_pack.h"
|
||||||
|
/** 测试完成后移除 **/
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
@@ -56,11 +59,25 @@ int main(void)
|
|||||||
by_tiny_frame_init();
|
by_tiny_frame_init();
|
||||||
printf("start running\r\n");
|
printf("start running\r\n");
|
||||||
|
|
||||||
|
/** 测试完成后移除 **/
|
||||||
|
by_tf_pack_frame_t frame_now;
|
||||||
|
|
||||||
|
frame_now.cmd = 0x06;
|
||||||
|
frame_now.data = 0x19260817;
|
||||||
|
frame_now.reg_addr = 0x4059;
|
||||||
|
frame_now.slave_id = 0x0D;
|
||||||
|
/** 测试完成后移除 **/
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
Page_Run();
|
Page_Run();
|
||||||
by_buzzer_run();
|
by_buzzer_run();
|
||||||
|
|
||||||
|
/** 测试完成后移除 **/
|
||||||
by_tiny_frame_parse_run();
|
by_tiny_frame_parse_run();
|
||||||
|
// by_tiny_frame_pack_send(&frame_now);
|
||||||
system_delay_ms(100);
|
system_delay_ms(100);
|
||||||
|
/** 测试完成后移除 **/
|
||||||
|
|
||||||
// if (mt9v03x_finish_flag) {
|
// if (mt9v03x_finish_flag) {
|
||||||
// // 该操作消耗大概 1970 个 tick,折合约 110us
|
// // 该操作消耗大概 1970 个 tick,折合约 110us
|
||||||
// memcpy(mt9v03x_image_copy[0], mt9v03x_image[0], (sizeof(mt9v03x_image_copy) / sizeof(uint8_t)));
|
// memcpy(mt9v03x_image_copy[0], mt9v03x_image[0], (sizeof(mt9v03x_image_copy) / sizeof(uint8_t)));
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
void by_tiny_frame_init(void)
|
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_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);
|
uart_rx_interrupt(BY_TF_UART_INDEX, ENABLE);
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#ifndef _BY_TINY_FRAME_CONFIG_H__
|
#ifndef _BY_TINY_FRAME_CONFIG_H__
|
||||||
#define _BY_TINY_FRAME_CONFIG_H__
|
#define _BY_TINY_FRAME_CONFIG_H__
|
||||||
|
|
||||||
|
#define BY_TF_DEBUG (1)
|
||||||
|
|
||||||
#define BY_TF_UART_TX_PIN (UART3_MAP0_TX_B10)
|
#define BY_TF_UART_TX_PIN (UART3_MAP0_TX_B10)
|
||||||
#define BY_TF_UART_RX_PIN (UART3_MAP0_RX_B11)
|
#define BY_TF_UART_RX_PIN (UART3_MAP0_RX_B11)
|
||||||
#define BY_TF_UART_INDEX (UART_3)
|
#define BY_TF_UART_INDEX (UART_3)
|
||||||
@@ -8,4 +10,14 @@
|
|||||||
|
|
||||||
#define BY_TF_PARSE_BUFFER_SIZE (50)
|
#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
|
||||||
|
#endif
|
||||||
|
|
||||||
#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
|
||||||
@@ -10,6 +10,7 @@ by_tf_parse_frame_t frame_now;
|
|||||||
|
|
||||||
void by_tiny_frame_parse_init(void)
|
void by_tiny_frame_parse_init(void)
|
||||||
{
|
{
|
||||||
|
/** 初始化环形缓冲区 **/
|
||||||
lwrb_init(&lwrb_struct, buffer_rb, 40);
|
lwrb_init(&lwrb_struct, buffer_rb, 40);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,10 +20,17 @@ uint8_t by_tiny_frame_parse_listening(by_tf_parse_frame_t *frame_s, const uint8_
|
|||||||
static uint8_t cnt_s = 0;
|
static uint8_t cnt_s = 0;
|
||||||
static uint8_t cnt_rest_s = 0;
|
static uint8_t cnt_rest_s = 0;
|
||||||
|
|
||||||
|
#if (BY_TF_DEBUG)
|
||||||
printf("%0.2X\r\n", buff);
|
printf("%0.2X\r\n", buff);
|
||||||
|
#endif
|
||||||
|
|
||||||
do {
|
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)) {
|
if ((0 == cnt_s) && (((slave_id << 1) + 1) == buff)) {
|
||||||
|
#endif
|
||||||
memset(frame_s, 0, sizeof(*frame_s));
|
memset(frame_s, 0, sizeof(*frame_s));
|
||||||
cnt_s = 1;
|
cnt_s = 1;
|
||||||
cnt_rest_s = 9;
|
cnt_rest_s = 9;
|
||||||
@@ -45,6 +53,8 @@ uint8_t by_tiny_frame_parse_listening(by_tf_parse_frame_t *frame_s, const uint8_
|
|||||||
frame_s->data |= ((uint32_t)frame_s->frame[5] << 16);
|
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[6] << 8);
|
||||||
frame_s->data |= (uint32_t)frame_s->frame[7];
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
} while (0);
|
} while (0);
|
||||||
@@ -65,12 +75,16 @@ void by_tiny_frame_parse_run(void)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!by_tiny_frame_parse_listening(&frame_now, 127, buffer_out)) {
|
// TODO 待结合 read&wirte 部分修改监听的从机地址
|
||||||
|
if (!by_tiny_frame_parse_listening(&frame_now, 0x0D, buffer_out)) {
|
||||||
if (!by_tiny_frame_parse_crc(&frame_now)) {
|
if (!by_tiny_frame_parse_crc(&frame_now)) {
|
||||||
|
|
||||||
|
#if (BY_TF_DEBUG)
|
||||||
printf("frame parsed!\r\n");
|
printf("frame parsed!\r\n");
|
||||||
}
|
#endif
|
||||||
// 解析帧
|
// 解析帧
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// if (!mp_cmd_parse_modbus_handle(data)) {
|
// if (!mp_cmd_parse_modbus_handle(data)) {
|
||||||
// mp_cmd_mb_parse(&mp_cmd_mb_now, &mp_cmd_parsed_now);
|
// mp_cmd_mb_parse(&mp_cmd_mb_now, &mp_cmd_parsed_now);
|
||||||
// }
|
// }
|
||||||
@@ -83,11 +97,13 @@ uint8_t by_tiny_frame_parse_crc(by_tf_parse_frame_t *frame_s)
|
|||||||
|
|
||||||
calc_crc_val = crc16_check(frame_s->frame, (sizeof(frame_s->frame) - 2));
|
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("get: %0.2X", frame_s->crc_val);
|
||||||
printf("\r\n");
|
printf("\r\n");
|
||||||
|
|
||||||
printf("cal: %0.2X", calc_crc_val);
|
printf("cal: %0.2X", calc_crc_val);
|
||||||
printf("\r\n");
|
printf("\r\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
if ((frame_s->crc_val == calc_crc_val) || (frame_s->crc_val == 0xFFFF)) {
|
if ((frame_s->crc_val == calc_crc_val) || (frame_s->crc_val == 0xFFFF)) {
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
// 从机地址 (1b) - 功能码 (1b) - 寄存器地址 (2b) - 数据 (4b) - CRC(2b)
|
// 从机地址 (1b) - 功能码 (1b) - 寄存器地址 (2b) - 数据 (4b) - CRC(2b)
|
||||||
// 从机地址 (1b) 0-127, 最低位表示发送方,主机请求低位为 0,从机应答低位为 1
|
// 从机地址 (1b) 0-127, 最低位表示发送方,主机请求低位为 0,从机应答低位为 1
|
||||||
|
// 高字节在前
|
||||||
|
|
||||||
typedef struct by_tf_parse_frame_t {
|
typedef struct by_tf_parse_frame_t {
|
||||||
uint8_t frame[10];
|
uint8_t frame[10];
|
||||||
|
|||||||
Reference in New Issue
Block a user