添加与app对接协议
This commit is contained in:
@@ -35,7 +35,7 @@ list(APPEND srcs "drivers/sertrf/ota_u.c")
|
||||
list(APPEND srcs "drivers/sertrf/protocol/MSP.c")
|
||||
list(APPEND srcs "drivers/sertrf/protocol/p_protocol.c")
|
||||
list(APPEND srcs "drivers/sertrf/protocol/mavlink_control.c")
|
||||
|
||||
list(APPEND srcs "drivers/sertrf/protocol/resend_protl.c")
|
||||
|
||||
list(APPEND srcs "drivers/led_strip/led_strip.c")
|
||||
list(APPEND srcs "drivers/led_strip/led_strip_encoder.c")
|
||||
|
||||
@@ -302,9 +302,9 @@ uint8_t embedded_device_choice(device_t *port, uint8_t type)
|
||||
return DEVICE_OK;
|
||||
}
|
||||
|
||||
int embedded_device_read(device_t *port, void *buffer, uint32_t length)
|
||||
int embedded_device_read(device_t *port, void *buffer, uint32_t length, uint32_t timeout)
|
||||
{
|
||||
return sb_data_port_read(port->embedded_device, buffer, length, 0);
|
||||
return sb_data_port_read(port->embedded_device, buffer, length, timeout);
|
||||
}
|
||||
|
||||
int embedded_device_write(device_t *port, void *buffer, uint32_t length)
|
||||
|
||||
@@ -134,10 +134,11 @@ uint8_t pc_device_choice(device_t *port, uint8_t type);
|
||||
* @param port 由对应的驱动提供的绑定接口获得的句柄
|
||||
* @param buffer 读取数据缓存
|
||||
* @param length 读取数据长度
|
||||
* @param timeout 超时时间
|
||||
* @retval < 0 操作失败或在指定的时间内未满足指定的操作长度
|
||||
* @retval >= 0 实际已缓存的长度
|
||||
*/
|
||||
int embedded_device_read(device_t *port, void *buffer, uint32_t length);
|
||||
int embedded_device_read(device_t *port, void *buffer, uint32_t length, uint32_t timeout);
|
||||
/**
|
||||
* @brief 嵌入式设备数据接口写操作
|
||||
*
|
||||
|
||||
67
app/drivers/sertrf/protocol/resend_crc.h
Normal file
67
app/drivers/sertrf/protocol/resend_crc.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
/**************************mavlink的CRC****************************/
|
||||
#define MCRF4XX_INIT_CRC 0xffff
|
||||
|
||||
static const uint16_t fmav_crc_table_MCRF4XX[256] = {
|
||||
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
|
||||
0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
|
||||
0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
|
||||
0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
|
||||
0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
|
||||
0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
|
||||
0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
|
||||
0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
|
||||
0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
|
||||
0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
|
||||
0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
|
||||
0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
|
||||
0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
|
||||
0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
|
||||
0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
|
||||
0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
|
||||
0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
|
||||
0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
|
||||
0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
|
||||
0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
|
||||
0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
|
||||
0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
|
||||
0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
|
||||
0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
|
||||
0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
|
||||
0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
|
||||
0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
|
||||
0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
|
||||
0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
|
||||
0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
|
||||
0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
|
||||
0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
|
||||
};
|
||||
static inline void fmav_crc_init(uint16_t* crc)
|
||||
{
|
||||
*crc = MCRF4XX_INIT_CRC;
|
||||
}
|
||||
static inline void fmav_crc_accumulate(uint16_t* crc, uint8_t data)
|
||||
{
|
||||
// one often finds crc = (crc << 8) ^ table[(crc >> 8) ^ data], but we need it differently here
|
||||
//*crc = (*crc >> 8) ^ fmav_crc_table_MCRF4XX[(*crc ^ data) & 0xff];
|
||||
*crc = (*crc >> 8) ^ fmav_crc_table_MCRF4XX[(uint8_t)(*crc & 0xff) ^ data];
|
||||
}
|
||||
static inline void fmav_crc_accumulate_buf(uint16_t* crc, const uint8_t* buf, uint16_t len)
|
||||
{
|
||||
while (len--) {
|
||||
*crc = (*crc >> 8) ^ fmav_crc_table_MCRF4XX[(uint8_t)(*crc & 0xff) ^ *buf++];
|
||||
}
|
||||
}
|
||||
static inline uint16_t fmav_crc_calculate(const uint8_t* buf, uint16_t len)
|
||||
{
|
||||
uint16_t crc;
|
||||
|
||||
fmav_crc_init(&crc);
|
||||
while (len--) {
|
||||
fmav_crc_accumulate(&crc, *buf++);
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
262
app/drivers/sertrf/protocol/resend_protl.c
Normal file
262
app/drivers/sertrf/protocol/resend_protl.c
Normal file
@@ -0,0 +1,262 @@
|
||||
#include "resend_protl.h"
|
||||
|
||||
void resend_init(resend_device_t *resend_device, int (*send)(void* data, uint16_t len, int timeout), int (*recv)(void* data, uint16_t len, int timeout), int (*get_length )(void), void (*resend_user_parse)(void *resend_device))
|
||||
{
|
||||
resend_device->send = send;
|
||||
resend_device->recv = recv;
|
||||
resend_device->get_length = get_length;
|
||||
resend_device->resend_user_parse = resend_user_parse;
|
||||
|
||||
resend_device->status.c_state = RESEND_IDLE;
|
||||
|
||||
resend_device->tx_frame.header_0 = RESEND_HEADER_0;
|
||||
resend_device->tx_frame.header_1 = RESEND_HEADER_1;
|
||||
resend_device->tx_frame.seq = 0;
|
||||
resend_device->tx_frame.cmd = 0;
|
||||
resend_device->tx_frame.retry_cnt = 0;
|
||||
resend_device->tx_frame.len = 0;
|
||||
resend_device->tx_frame.crc = 0;
|
||||
}
|
||||
void resend_set(resend_device_t *resend_device)
|
||||
{
|
||||
resend_device->tx_frame.seq = resend_device->rx_frame.seq;
|
||||
}
|
||||
int resend_encode(resend_device_t *resend_device, void *out, uint8_t cmd, void* data, uint16_t len)
|
||||
{
|
||||
if(!out ) return -1;
|
||||
uint8_t* c = (uint8_t*)out;
|
||||
// header
|
||||
c[0] = resend_device->tx_frame.header_0;
|
||||
c[1] = resend_device->tx_frame.header_1;
|
||||
// seq
|
||||
if(cmd != RESEND_CMD_DATA && cmd != RESEND_CMD_ACK && resend_device->tx_frame.retry_cnt == 0)
|
||||
resend_device->tx_frame.seq++;
|
||||
|
||||
c[2] = resend_device->tx_frame.seq;
|
||||
// cmd
|
||||
c[3] = resend_device->tx_frame.cmd = cmd;
|
||||
// retry_cnt
|
||||
c[4] = resend_device->tx_frame.retry_cnt;
|
||||
// len big endian
|
||||
c[5] = (uint8_t)((len >> 8) & 0xFF);
|
||||
c[6] = (uint8_t)(len & 0xFF);
|
||||
// payload
|
||||
if (len > 0 && data) memcpy(&c[7], data, len);
|
||||
// compute crc over seq..payload (i.e. bytes p+1 .. p+5+len)
|
||||
uint16_t crc = fmav_crc_calculate(&c[2], 5 + len);
|
||||
c[7 + len] = (uint8_t)((crc >> 8) & 0xFF);
|
||||
c[7 + len + 1] = (uint8_t)(crc & 0xFF);
|
||||
|
||||
return (int)(len + RESEND_MAX_PAYLOAD);
|
||||
}
|
||||
|
||||
int resend_decode(resend_device_t *resend_device, uint8_t c)
|
||||
{
|
||||
static uint16_t frame_crc = 0;
|
||||
switch(resend_device->status.c_state)
|
||||
{
|
||||
case RESEND_IDLE:
|
||||
if (c == RESEND_HEADER_0) {
|
||||
resend_device->status.c_state = RESEND_HDR;
|
||||
}
|
||||
else{
|
||||
resend_device->status.c_state = RESEND_IDLE;
|
||||
}
|
||||
break;
|
||||
case RESEND_HDR:
|
||||
if (c == RESEND_HEADER_1) {
|
||||
resend_device->status.c_state = RESEND_SEQ;
|
||||
fmav_crc_init(&frame_crc);
|
||||
}else{
|
||||
resend_device->status.c_state = RESEND_IDLE;
|
||||
}
|
||||
break;
|
||||
case RESEND_SEQ:
|
||||
resend_device->rx_frame.seq = c;
|
||||
fmav_crc_accumulate(&frame_crc, c);
|
||||
resend_device->status.c_state = RESEND_CMD;
|
||||
break;
|
||||
case RESEND_CMD:
|
||||
resend_device->rx_frame.cmd = c;
|
||||
fmav_crc_accumulate(&frame_crc, c);
|
||||
resend_device->status.c_state = RESEND_RETRY_CNT;
|
||||
break;
|
||||
case RESEND_RETRY_CNT:
|
||||
printf("cnt %d\r\n",c);
|
||||
resend_device->rx_frame.retry_cnt = c;
|
||||
fmav_crc_accumulate(&frame_crc, c);
|
||||
resend_device->status.c_state = RESEND_LEN_1;
|
||||
break;
|
||||
case RESEND_LEN_1:
|
||||
resend_device->rx_frame.len = 0;
|
||||
resend_device->rx_frame.len = (uint16_t)(c << 8) & 0xF0;
|
||||
fmav_crc_accumulate(&frame_crc, c);
|
||||
resend_device->status.c_state = RESEND_LEN_2;
|
||||
break;
|
||||
case RESEND_LEN_2:
|
||||
resend_device->rx_frame.len |= c;
|
||||
fmav_crc_accumulate(&frame_crc, c);
|
||||
if(resend_device->rx_frame.len > 0)
|
||||
{
|
||||
resend_device->status.data_offset = 0;
|
||||
resend_device->status.c_state = RESEND_DATA;
|
||||
}
|
||||
else
|
||||
resend_device->status.c_state = RESEND_CRC_1;
|
||||
break;
|
||||
case RESEND_DATA:
|
||||
resend_device->rx_frame.payload[resend_device->status.data_offset ++] = c;
|
||||
fmav_crc_accumulate(&frame_crc, c);
|
||||
if(resend_device->rx_frame.len == resend_device->status.data_offset){
|
||||
resend_device->status.data_offset = 0;
|
||||
resend_device->status.c_state = RESEND_CRC_1;
|
||||
}
|
||||
break;
|
||||
case RESEND_CRC_1:
|
||||
if((uint8_t)(frame_crc >> 8 & 0xFF) == c){
|
||||
resend_device->status.c_state = RESEND_CRC_2;
|
||||
}else{
|
||||
resend_device->status.c_state = RESEND_IDLE;
|
||||
}
|
||||
break;
|
||||
case RESEND_CRC_2:
|
||||
if((uint8_t)(frame_crc) == c){
|
||||
resend_device->status.c_state = RESEND_IDLE;
|
||||
resend_device->rx_frame.crc = frame_crc;
|
||||
if(resend_device->rx_frame.cmd != RESEND_CMD_DATA && resend_device->rx_frame.cmd != RESEND_CMD_ACK && resend_device->rx_frame.retry_cnt == 0)
|
||||
resend_device->tx_frame.seq++;
|
||||
return 0;
|
||||
}else{
|
||||
resend_device->status.c_state = RESEND_IDLE;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int resend_recv_data(resend_device_t *resend_device, int timeout)
|
||||
{
|
||||
uint8_t data[RESEND_FRAME_SIZE];
|
||||
int ret = resend_device->recv(data, RESEND_FRAME_SIZE, timeout);
|
||||
if(ret <= 0)
|
||||
return -1;
|
||||
int ret_later = ret;
|
||||
static uint8_t seq_lat = 0;
|
||||
while(ret_later --)
|
||||
{
|
||||
if(!resend_decode(resend_device, data[ret - ret_later - 1]))
|
||||
{
|
||||
printf("tx_req %d rx_req %d retry_cnt %d cmd %d\r\n", resend_device->tx_frame.seq, resend_device->rx_frame.seq, resend_device->rx_frame.retry_cnt, resend_device->rx_frame.cmd);
|
||||
|
||||
if(seq_lat == resend_device->rx_frame.seq){
|
||||
resend_device->status.resend_flag = 1;
|
||||
} else {
|
||||
resend_device->status.resend_flag = 0;
|
||||
}
|
||||
|
||||
resend_device->resend_user_parse(resend_device);
|
||||
resend_parse_data(resend_device);
|
||||
|
||||
seq_lat = resend_device->rx_frame.seq;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int resend_send_cmd(resend_device_t *resend_device, uint8_t cmd, int timeout)
|
||||
{
|
||||
uint8_t out[RESEND_MAX_PAYLOAD];
|
||||
int ret = resend_encode(resend_device, out, cmd, NULL, 0);
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
return resend_device->send(out, ret, timeout);
|
||||
}
|
||||
int resend_send_data(resend_device_t *resend_device, uint8_t cmd, void* data, uint16_t len, int timeout)
|
||||
{
|
||||
uint8_t out[RESEND_MAX_PAYLOAD + len];
|
||||
memcpy(resend_device->tx_frame.payload, data, len);
|
||||
|
||||
resend_device->tx_frame.retry_cnt = 0;
|
||||
|
||||
int ret = resend_encode(resend_device, out, cmd, data, len);
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
// 初始化ack应答标志位,需等待应答
|
||||
resend_device->status.ack_flag = 1;
|
||||
|
||||
while(resend_device->status.ack_flag)
|
||||
{
|
||||
if(resend_device->tx_frame.retry_cnt)
|
||||
{
|
||||
//进行重发 , 重新编码
|
||||
ret = resend_encode(resend_device, out, cmd, data, len);
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
printf("data retry cnt %d\n", resend_device->tx_frame.retry_cnt);
|
||||
}
|
||||
|
||||
ret = resend_device->send(out, ret, 0);
|
||||
if(ret < RESEND_MAX_PAYLOAD)
|
||||
return ret;
|
||||
|
||||
// 特殊命令以及应答信号不需要等待
|
||||
if(cmd == RESEND_CMD_DATA || cmd == RESEND_CMD_ACK)
|
||||
resend_device->status.ack_flag = 0;
|
||||
|
||||
resend_device->tx_frame.retry_cnt ++;
|
||||
|
||||
// 接收数据
|
||||
resend_recv_data(resend_device, timeout);
|
||||
// 超过允许的等待次数
|
||||
if(resend_device->tx_frame.retry_cnt > RESEND_MAX_RETRY_CNT)
|
||||
{
|
||||
printf("timeout cnt max\n");
|
||||
resend_device->tx_frame.retry_cnt = 0;
|
||||
resend_device->status.ack_flag = 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
// return resend_device->send(out, resend_frame_encode(resend_device, out, data, len), timeout);
|
||||
}
|
||||
|
||||
int resend_parse_data(resend_device_t *resend_device)
|
||||
{
|
||||
switch(resend_device->rx_frame.cmd){
|
||||
case RESEND_CMD_DATA:
|
||||
for (size_t i = 0; i < resend_device->rx_frame.len; i++)
|
||||
{
|
||||
printf("%02X ", resend_device->rx_frame.payload[i]);
|
||||
}
|
||||
printf("\r\n");
|
||||
break;
|
||||
case RESEND_CMD_ACK:
|
||||
printf("ACK\n");
|
||||
resend_device->status.ack_flag = 0;
|
||||
break;
|
||||
case RESEND_CMD_STATUS:
|
||||
resend_set(resend_device);
|
||||
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
|
||||
break;
|
||||
case RESEND_CMD_DATA_ACK:
|
||||
for (size_t i = 0; i < resend_device->rx_frame.len; i++)
|
||||
{
|
||||
printf("%02X ", resend_device->rx_frame.payload[i]);
|
||||
}
|
||||
printf("\r\n");
|
||||
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
|
||||
break;
|
||||
case RESEND_CMD_OTA_START:
|
||||
break;
|
||||
case RESEND_CMD_OTA_DATA:
|
||||
break;
|
||||
case RESEND_CMD_OTA_END:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
126
app/drivers/sertrf/protocol/resend_protl.h
Normal file
126
app/drivers/sertrf/protocol/resend_protl.h
Normal file
@@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "resend_crc.h"
|
||||
|
||||
// 帧数据量
|
||||
#define RESEND_HEADER_SIZE 2
|
||||
#define RESEND_CMD_SIZE 1
|
||||
#define RESEND_SEQ_SIZE 1
|
||||
#define RESEND_RETRY_CNT_SIZE 1
|
||||
#define RESEND_LEN_SIZE 2
|
||||
#define RESEND_DATA_SIZE 1024
|
||||
#define RESEND_CRC_SIZE 2
|
||||
#define RESEND_MAX_PAYLOAD (RESEND_HEADER_SIZE + RESEND_CMD_SIZE + RESEND_SEQ_SIZE + RESEND_RETRY_CNT_SIZE + RESEND_LEN_SIZE + RESEND_CRC_SIZE)
|
||||
#define RESEND_FRAME_SIZE RESEND_HEADER_SIZE + RESEND_CMD_SIZE + RESEND_SEQ_SIZE + RESEND_RETRY_CNT_SIZE + RESEND_LEN_SIZE + RESEND_DATA_SIZE + RESEND_CRC_SIZE
|
||||
// 帧头
|
||||
#define RESEND_HEADER_0 0xFD
|
||||
#define RESEND_HEADER_1 0xFE
|
||||
|
||||
#define RESEND_MAX_RETRY_CNT 5
|
||||
// 命令类型
|
||||
typedef enum
|
||||
{
|
||||
RESEND_CMD_DATA = 0x01,
|
||||
RESEND_CMD_ACK = 0x02,
|
||||
RESEND_CMD_STATUS = 0x03,
|
||||
RESEND_CMD_DATA_ACK = 0x04,
|
||||
|
||||
|
||||
RESEND_CMD_OTA_START = 0x011,
|
||||
RESEND_CMD_OTA_DATA = 0x012,
|
||||
RESEND_CMD_OTA_END = 0x013
|
||||
}resend_cmd_type_e;
|
||||
// 帧解析状态
|
||||
typedef enum
|
||||
{
|
||||
RESEND_IDLE = 0,
|
||||
RESEND_HDR,
|
||||
RESEND_SEQ,
|
||||
RESEND_CMD,
|
||||
RESEND_RETRY_CNT,
|
||||
RESEND_LEN_1,
|
||||
RESEND_LEN_2,
|
||||
RESEND_DATA,
|
||||
RESEND_CRC_1,
|
||||
RESEND_CRC_2
|
||||
}resend_c_state_e;
|
||||
// 帧状态
|
||||
typedef struct
|
||||
{
|
||||
resend_c_state_e c_state; //目前解析数据状态
|
||||
uint8_t resend_flag; //该数据是否是重发数据
|
||||
uint8_t ack_flag; //应答信号标志位
|
||||
uint16_t data_offset; //数据偏移
|
||||
}resend_status_t;
|
||||
// 帧
|
||||
typedef struct
|
||||
{
|
||||
uint8_t header_0; // 帧头
|
||||
uint8_t header_1; // 帧头
|
||||
uint8_t seq; // 帧序号
|
||||
uint8_t cmd; // 命令
|
||||
uint8_t retry_cnt; // 重发次数
|
||||
uint16_t len; // 帧长度
|
||||
uint8_t payload[RESEND_DATA_SIZE]; // 帧数据
|
||||
uint16_t crc; // 帧CRC
|
||||
}resend_frame_t;
|
||||
// 帧驱动
|
||||
typedef struct
|
||||
{
|
||||
resend_frame_t rx_frame; // 接收帧
|
||||
resend_frame_t tx_frame; // 发送帧
|
||||
resend_status_t status; // 帧状态
|
||||
|
||||
int (*send)(void* data, uint16_t len, int timeout); // 发送数据
|
||||
int (*recv)(void* data, uint16_t len, int timeout); // 接收数据
|
||||
int (*get_length )(void); // 获取数据长度
|
||||
void (*resend_user_parse)(void *resend_device);
|
||||
}resend_device_t;
|
||||
|
||||
/**
|
||||
* @brief 协议初始化
|
||||
*/
|
||||
void resend_init(resend_device_t *resend_device, int (*send)(void* data, uint16_t len, int timeout), int (*recv)(void* data, uint16_t len, int timeout), int (*get_length )(void), void (*resend_user_parse)(void *resend_device));
|
||||
|
||||
/**
|
||||
* @brief 设置参数
|
||||
*/
|
||||
void resend_set(resend_device_t *resend_device);
|
||||
/**
|
||||
* @brief 封包
|
||||
*/
|
||||
int resend_encode(resend_device_t *resend_device, void *out, uint8_t cmd, void* data, uint16_t len);
|
||||
/**
|
||||
* @brief 解包
|
||||
*/
|
||||
int resend_decode(resend_device_t *resend_device, uint8_t c);
|
||||
|
||||
/**
|
||||
* @brief 发送无数据命令
|
||||
*/
|
||||
int resend_send_cmd(resend_device_t *resend_device, uint8_t cmd, int timeout);
|
||||
|
||||
/**
|
||||
* @brief 数据发送
|
||||
*/
|
||||
int resend_send_data(resend_device_t *resend_device, uint8_t cmd, void* data, uint16_t len, int timeout);
|
||||
|
||||
/**
|
||||
* @brief 数据接收
|
||||
*/
|
||||
int resend_recv_data(resend_device_t *resend_device, int timeout);
|
||||
|
||||
/**
|
||||
* @brief 数据解析
|
||||
*/
|
||||
int resend_parse_data(resend_device_t *resend_device);
|
||||
|
||||
|
||||
/* 接口函数 */
|
||||
int resend_send(void* data, uint16_t len, int timeout);
|
||||
int resend_recv(void* data, uint16_t len, int timeout);
|
||||
int resend_get_length(void);
|
||||
void resend_user_parse(void *resend_device);
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
|
||||
sertrf_t sertrf;
|
||||
|
||||
void sertrf_init(void)
|
||||
{
|
||||
uint8_t res = 0;
|
||||
@@ -16,6 +17,9 @@ void sertrf_init(void)
|
||||
work_rgb_led_start();
|
||||
//按键初始化
|
||||
button_work_init();
|
||||
// 协议初始化
|
||||
sertrf.resend_read_mutex = xSemaphoreCreateMutex();
|
||||
resend_init(&sertrf.resend_device, resend_send, resend_recv, resend_get_length,resend_user_parse);
|
||||
//线程启动
|
||||
sertrf_start();
|
||||
}
|
||||
@@ -50,7 +54,7 @@ void embedded_thread(void* arg)
|
||||
{
|
||||
uint8_t data[embedded_size ];
|
||||
// data[embedded_size] = '\0';
|
||||
embedded_device_read(&sertrf.device, data, embedded_size);
|
||||
embedded_device_read(&sertrf.device, data, embedded_size,0);
|
||||
|
||||
Protocol_buf_decode(data, embedded_size);
|
||||
// SYS_LOG_INF("data : %s", data);
|
||||
@@ -199,4 +203,58 @@ void printf_chill_time(uint8_t chill_time, uint16_t type)
|
||||
type_cnt_time[chill_time] = 0;
|
||||
}
|
||||
last_time[chill_time] = now_time;
|
||||
}
|
||||
|
||||
int resend_send(void* data, uint16_t len, int timeout)
|
||||
{
|
||||
return embedded_device_write(&sertrf.device, data, len);
|
||||
}
|
||||
int resend_recv(void* data, uint16_t len, int timeout)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (xSemaphoreTake(sertrf.resend_read_mutex, portMAX_DELAY) == pdTRUE)
|
||||
{
|
||||
ret = embedded_device_read(&sertrf.device, data, len,timeout);
|
||||
xSemaphoreGive(sertrf.resend_read_mutex);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
int resend_get_length(void)
|
||||
{
|
||||
return embedded_device_get_rx_length(&sertrf.device);
|
||||
}
|
||||
void resend_user_parse(void *resend_device)
|
||||
{
|
||||
resend_device_t* resend_parse = (resend_device_t*)resend_device;
|
||||
switch(resend_parse->rx_frame.cmd)
|
||||
{
|
||||
case RESEND_CMD_OTA_START:
|
||||
{
|
||||
if(!sertrf.resend_device.status.resend_flag)
|
||||
otau_init(&sertrf.otau);
|
||||
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
|
||||
}
|
||||
break;
|
||||
case RESEND_CMD_OTA_DATA:
|
||||
{
|
||||
if(!sertrf.resend_device.status.resend_flag)
|
||||
{
|
||||
SYS_LOG_DBG("KUYI_CMD_OTA_DATA");
|
||||
otau_write(&sertrf.otau, sertrf.resend_device.rx_frame.payload, sertrf.resend_device.rx_frame.len);
|
||||
}
|
||||
|
||||
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
|
||||
}
|
||||
break;
|
||||
case RESEND_CMD_OTA_END:
|
||||
{
|
||||
if(!sertrf.resend_device.status.resend_flag)
|
||||
otau_end(&sertrf.otau);
|
||||
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
|
||||
os_thread_sleep(2);
|
||||
esp_restart();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "../led_strip/led_strip.h"
|
||||
#include "key.h"
|
||||
#include "protocol/p_protocol.h"
|
||||
#include "protocol/kuyi_protl.h"
|
||||
#include "protocol/resend_protl.h"
|
||||
#include "ota_u.h"
|
||||
typedef struct
|
||||
{
|
||||
@@ -12,8 +12,10 @@ typedef struct
|
||||
os_thread_t embedded_thread;
|
||||
os_thread_t pc_thread;
|
||||
os_thread_t task_thread;
|
||||
|
||||
|
||||
ota_u_t otau;
|
||||
resend_device_t resend_device;
|
||||
SemaphoreHandle_t resend_read_mutex;
|
||||
}sertrf_t;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user