Commit 0f57dfa1 authored by 王金亮's avatar 王金亮

feat:增加串口协议需要用到的库

parent bb4ccc49
{
"files.associations": {
"protocol_user.h": "c",
"protocol_lib.h": "c",
"protocol_crc16.h": "c",
"gatts_table_creat_demo.h": "c"
}
}
\ No newline at end of file
......@@ -2,5 +2,9 @@
idf_build_get_property(project_dir PROJECT_DIR)
idf_component_register(SRCS "gatts_table_creat_demo.c"
"simple_ota_example.c"
"Protocol_CRC16.c"
"Protocol_Lib.c"
"Protocol_User.c"
"MCU_Core_Protocol.c"
INCLUDE_DIRS "."
EMBED_TXTFILES ${project_dir}/server_certs/ca_cert.pem)
#include "MCU_Core_Protocol.h"
#include <string.h>
#include <stdlib.h>
#include "Protocol_User.h"
void Uart_ID10_process(void)
{
if(1)
{
uint8_t ID10[2] = {0xFF,0xFF};
Protocol_Send(0x10,ID10,2);
}
else
{
uint8_t ID10[2] = {0x01,0x01};
Protocol_Send(0x10,ID10,2);
}
}
#ifndef _MCU_CORE_PROTOCOL_H_
#define _MCU_CORE_PROTOCOL_H_
typedef union
{
unsigned char data[9];
struct
{
unsigned char hear_1;
unsigned char hear_2;
unsigned char length;
unsigned char counter;
unsigned char Uart_ID;
unsigned char data_content;
unsigned char crc_hight;
unsigned char crc_low;
unsigned char end_bit;
}data_uart;
}_Uart_Data;
extern void Uart_ID10_process(void);
#endif
/**
* @file Protocol_CRC16.c
* @brief CRC16计算
* @details CRC16计算
* @author myliu
* @date 2022.05.09
* @version V1.0
* @copyright myiu
*/
#include "Protocol_CRC16.h"
static const Protocol_uint8_t CRC16_TAB_H [ 256 ] = {0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0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 0x45u, 0x87u, 0x47u, 0x46u, 0x86u, 0x82u, 0x42u, 0x43u, 0x83u, 0x41u, 0x81u, 0x80u, 0x40u};
/**
* 获取CRC16校验和
*/
/**
* @brief 计算传入数据的CRC16
* @param[in] pData 计算的数据内容
* @param[in] len 计算的数据长度
*
* @return 校验和
*
* @since 1.0.0
*/
Protocol_uint16_t getCheckSum(const Protocol_uint8_t *pData, int len)
{
Protocol_uint8_t u8_crc_h = 0xFFu;
Protocol_uint8_t u8_crc_l = 0xFFu;
Protocol_uint8_t u8_index = 0u;
Protocol_uint8_t i = 0u;
Protocol_uint16_t re_value;
while ( len-- )
{
u8_index = u8_crc_h ^ (pData [ i++ ]);
u8_crc_h = u8_crc_l ^ CRC16_TAB_H [ u8_index ];
u8_crc_l = CRC16_TAB_L [ u8_index ];
}
re_value = ( Protocol_uint16_t )u8_crc_h << 8u;
re_value |= u8_crc_l;
return re_value;
}
#ifndef PROTOCOL_CRC16_H
#define PROTOCOL_CRC16_H
#include "Protocol_Lib.h"
Protocol_uint16_t getCheckSum(const Protocol_uint8_t *pData, int len);
#endif
/**
* @file Protocol_Lib.c
* @brief 串口协议解析
* @details 串口协议解析
* @author myliu
* @date 2022.05.09
* @version V1.0
* @copyright myiu
*/
#include <stdio.h>
#include <string.h>
#include "Protocol_CRC16.h"
static UARTOpen UARTOpen_Cbk;
static UARTSend UARTSend_Cbk;
static UARTRead UARTRead_Cbk;
static ProcParse ProcParseCbk;
static UARTClose UARTClose_Cbk;
static ProtocolSetData ProtocolSetData_Cbk;
static Protocol_uint8_t *mDataBufPtr = Protocol_NULL;
static Protocol_uint16_t mDataBufLen = 0;
static Protocol_uint32_t DataBufMaxLen = 0;
//#define DEBUG_PRO_DATA 0
/**
* @brief 初始化函数
* @param[in] pMemSpace 分配给协议库的内存空间,用来缓存串口数据
* @param[in] MemLen 分配的内存空间大小
* @param[in] pFunc 回调函数,包括串口打开、发送、读取、关闭,也包括解析后数据回传
* @param[in] ProcParseCbk 此回调函数,返回数据从FrameNo开始到CRC16之前,和ProtocolSetData_Cbk二选一
* @param[in] ProtocolSetData_Cbk 此回调函数,返回数据区分命令字、电源状态等,和ProcParseCbk二选一,详见结构体Protocol_Data_t
*
* @warning 此函数KL30和Wakeup都要调用,否则未分配内存,功能不好使,也有可能造成野指针复位
*
* @since 1.0.0
*/
void Protocol_Init(Protocol_uint8_t *pMemSpace, Protocol_uint32_t MemLen, Protocol_Func_t *pFunc)
{
mDataBufPtr = pMemSpace;
DataBufMaxLen = MemLen;
UARTOpen_Cbk = pFunc->UARTOpen_Cbk;
UARTSend_Cbk = pFunc->UARTSend_Cbk;
UARTRead_Cbk = pFunc->UARTRead_Cbk;
ProcParseCbk = pFunc->ProcParseCbk;
UARTClose_Cbk = pFunc->UARTClose_Cbk;
ProtocolSetData_Cbk = pFunc->ProtocolSetData_Cbk;
if ( UARTOpen_Cbk != Protocol_NULL )
{
UARTOpen_Cbk( );
}
return;
}
/**
* @brief 串口协议服务函数,包括读取数据,解析数据,如在外部读取数据,可不调用此函数
*
* @warning 此函数可自定义周期调用,建议20ms周期调用,最大不可超过协议的最小发送周期
*
* @since 1.0.0
*/
void Protocol_Service(void)
{
int len;
Protocol_uint32_t readNum = 0;
if ( UARTRead_Cbk != Protocol_NULL )
{
readNum = UARTRead_Cbk(mDataBufPtr + mDataBufLen, 256 - mDataBufLen);
if ( readNum > 0 )
{
mDataBufLen += readNum;
// 解析协议
len = Protocol_Parse(mDataBufPtr, mDataBufLen);
if ( (len > 0) && (len < mDataBufLen) )
{
// 将未解析的数据移到头部
// Move unparsed data to the head
memcpy(mDataBufPtr, mDataBufPtr + len, mDataBufLen - len);
}
mDataBufLen -= len;
}
}
}
/**
* @brief 协议解析函数,如外部获取数据(例如中断),可直接调用此函数解析数据
* @param[in] pData 协议数据内容
* @param[in] len 需要处理的协议数据长度
*
* @return 剩余已处理的数据长度
*
* @since 1.0.0
*/
Protocol_uint32_t Protocol_Parse(const Protocol_uint8_t *pData, Protocol_uint32_t len)
{
Protocol_uint32_t remainLen = len; // 剩余数据长度 Remaining data length
Protocol_uint32_t dataLen; // 数据包长度 Packet length
Protocol_uint32_t frameLen; // 帧长度 Frame length
Protocol_uint32_t frameSum;
Protocol_Data_t ProcData;
int i = 0;
int dataBuf [ 256 ];
/**
* 以下部分需要根据协议格式进行相应的修改,解析出每一帧的数据
*/
while ( remainLen >= DATA_PACKAGE_MIN_LEN )
{
// 找到一帧数据的数据头
// Find the data header of a frame of data
while ( (remainLen >= 2) && ((pData [ 0 ] != CMD_HEAD1) || (pData [ 1 ] != CMD_HEAD2)) )
{
pData++;
remainLen--;
continue;
}
if ( remainLen < DATA_PACKAGE_MIN_LEN )
{
#ifdef DEBUG_PRO_DATA
d_printf("too short!!!!!!\n");
#endif
break;
}
dataLen = pData [ 2 ];
frameLen = dataLen + DATA_PACKAGE_FIXED_LEN;
if ( frameLen > remainLen )
{
// 数据内容不全
#ifdef DEBUG_PRO_DATA
d_printf("Incomplete data packet!!!!!!\n");
#endif
break;
}
// 打印一帧数据,需要时在CommDef.h文件中打开DEBUG_PRO_DATA宏
#ifdef DEBUG_PRO_DATA
for ( i = 0; i < frameLen; ++i )
{
d_printf("%x ", pData [ i ]);
}
d_printf("\n");
#endif
// 检测校验码 Checksum
frameSum = (pData [ frameLen - 2 ] << 8) | (pData [ frameLen - 1 ]);
if ( frameLen > 4 )
{
if ( getCheckSum(pData + 2, frameLen - 4) == frameSum )
{
// 解析一帧数据
if ( ProcParseCbk != Protocol_NULL )
{
ProcParseCbk(pData + 3, dataLen - 2);
}
if ( ProtocolSetData_Cbk != Protocol_NULL )
{
ProcData.FrameNo = pData [ 3 ];
ProcData.PowerSts = (pData [ 4 ] >> 6) & 0x03;
ProcData.CmdID = pData [ 4 ] & 0x3F;
if ( ProcData.CmdID == 0x10 )
{
#ifdef DEBUG_PRO_DATA
for ( i = 0; i < frameLen; ++i )
{
d_printf("%x ", pData [ i ]);
}
d_printf("\n");
#endif
}
if ( ProcData.CmdID == 0x12 )
{
#ifdef DEBUG_PRO_DATA
for ( i = 0; i < frameLen; ++i )
{
d_printf("%x ", pData [ i ]);
}
d_printf("\n");
#endif
}
ProcData.DataLen = dataLen - 4;
memcpy(ProcData.Data, pData + 5, ProcData.DataLen);
ProtocolSetData_Cbk(&ProcData);
}
}
else
{
for ( i = 0; i < frameLen; ++i )
{
dataBuf [ i ] = pData [ i ];
}
i = 0;
#ifdef DEBUG_PRO_DATA
d_printf("CheckSum error: new = %x, old = %x!!!!!!\n", getCheckSum(pData + 2, frameLen - 4), frameSum);
#endif
}
}
pData += frameLen;
remainLen -= frameLen;
}
return len - remainLen;
}
/**
* 根据协议格式进行拼接
*/
/**
* @brief 串口协议数据拼接,如初始化发送函数,调用此函数后,数据已通过串口发送
* @param[in] cmdID 命令字
* @param[in] pData 协议数据内容(不包括协议头、长度、帧序号、命令字、校验和,从数据域算起)
* @param[in] len 数据域长度
*
* @return 已发送的数据长度
*
* @since 1.0.0
*/
Protocol_uint32_t Protocol_Send(const Protocol_uint16_t cmdID, const Protocol_uint8_t *pData, Protocol_uint8_t len)
{
int i = 0;
Protocol_uint16_t checksum = 0;
Protocol_uint8_t dataBuf [ 256 ];
Protocol_uint32_t frameLen;
if ( len + DATA_PACKAGE_MIN_LEN > 256 )
{
// printf("sendProtocol data is too len !!!\n");
return 0;
}
dataBuf [ 0 ] = CMD_HEAD1;
dataBuf [ 1 ] = CMD_HEAD2; // 同步帧头 Sync frame header
dataBuf [ 2 ] = len + 4;
dataBuf [ 3 ] = len; // 命令字节 Command byte
dataBuf [ 4 ] = ( Protocol_uint8_t )cmdID;
frameLen = 5;
// 数据 Data
for ( i = 0; i < len; ++i )
{
dataBuf [ frameLen ] = pData [ i ];
frameLen++;
}
// 校验码 Checksum
checksum = getCheckSum(dataBuf + 2, frameLen - 2);
dataBuf [ frameLen ] = (checksum >> 8) & 0x00FF;
frameLen++;
dataBuf [ frameLen ] = checksum & 0x00FF;
frameLen++;
if ( UARTSend_Cbk != Protocol_NULL )
{
return UARTSend_Cbk(dataBuf, frameLen);
}
else
{
return 0;
}
}
#ifndef PROTOCOL_LIB_H
#define PROTOCOL_LIB_H
/*
Platform_32Bit
Platform_16Bit
*/
#ifdef Platform_16Bit
#define Protocol_uint8_t unsigned char
#define Protocol_uint16_t unsigned int
#define Protocol_uint32_t unsigned long
#else
#define Protocol_uint8_t unsigned char
#define Protocol_uint16_t unsigned short
#define Protocol_uint32_t unsigned int
#define Protocol_uint64_t unsigned long long
#endif
#ifndef Protocol_NULL
#define Protocol_NULL ( void * )0u
#endif /* NULL */
/**<0xEB 0x90 长度 帧序号 命令字 DATA0-DATAN CRC16_1 CRC16_2*/
#define DATA_PACKAGE_MIN_LEN 7 /**< 0xEB 0x90 + 长度 + 数据包序号 + CMDID + CRC16_1 + CRC16_2 */
#define DATA_PACKAGE_FIXED_LEN 3 /**< 未计算在长度内的数据 0xEB 0x90 + 长度位 */
#define CMD_HEAD1 0xEB
#define CMD_HEAD2 0x90
/**@struct Protocol_Data_t
* @brief 解析后的协议数据
*/
typedef struct
{
Protocol_uint8_t FrameNo; /**< 帧序号 */
Protocol_uint8_t PowerSts; /**< 电源状态 */
Protocol_uint8_t CmdID; /**< 命令字 */
Protocol_uint8_t DataLen; /**< 有效数据长度 DATA0-DATAN*/
Protocol_uint8_t Data [ 256 ]; /**< 有效数据内容 DATA0-DATAN*/
} Protocol_Data_t;
typedef Protocol_uint8_t (*UARTOpen)(void);
typedef Protocol_uint32_t (*UARTSend)(const Protocol_uint8_t *pData, Protocol_uint32_t u32Len);
typedef Protocol_uint32_t (*UARTRead)(Protocol_uint8_t *pData, Protocol_uint32_t u32Len);
typedef void (*ProtocolSetData)(const Protocol_Data_t *pData);
typedef void (*ProcParse)(const Protocol_uint8_t *pData, Protocol_uint32_t u32Len);
typedef void (*UARTClose)(void);
/**@struct Protocol_Func_t
* @brief 协议解析回调函数
*/
typedef struct
{
UARTOpen UARTOpen_Cbk; /**< 串口打开回调 */
UARTSend UARTSend_Cbk; /**< 串口发送回调 */
UARTRead UARTRead_Cbk; /**< 串口读取回调 */
UARTClose UARTClose_Cbk; /**< 串口关闭回调 */
ProcParse ProcParseCbk; /**< 解析后的数据, 处理了数据包序号,命令字和电源状态,和ProcParse二选一 */
ProtocolSetData ProtocolSetData_Cbk; /**< 不包含 0xEB 0x90 + 长度 + CRC16_1 + CRC16_2,和ProtocolSetData二选一 */
} Protocol_Func_t;
void Protocol_Init(Protocol_uint8_t *pMemSpace, Protocol_uint32_t MemLen, Protocol_Func_t *pFunc);
void Protocol_Service(void);//10ms
Protocol_uint32_t Protocol_Parse(const Protocol_uint8_t *pData, Protocol_uint32_t len);
Protocol_uint32_t Protocol_Send(const Protocol_uint16_t cmdID, const Protocol_uint8_t *pData, Protocol_uint8_t len);
#endif
#include <stdint.h>
#include "Protocol_User.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_main.h"
#include "gatts_table_creat_demo.h"
#include "esp_gatt_common_api.h"
#include "esp_mac.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "string.h"
#include "driver/gpio.h"
#include "MCU_Core_Protocol.h"
#define UART_TX_MAX_DEPTH 1024UL //(2 * 1024UL) // 4K
#define UART_RX_MAX_DEPTH (2 * 1024UL) // 4K
#define UART_DATA_BUF_LEN (2 * 1024UL) // 4K
//extern void OTA_IC_To_Master( uint8_t len);
typedef struct
{
Protocol_uint32_t read_pos;
Protocol_uint32_t write_pos;
Protocol_uint8_t Rx_Buffer [ UART_RX_MAX_DEPTH ];
} UARTRxBuf_t;
typedef struct
{
Protocol_uint32_t read_pos;
Protocol_uint32_t write_pos;
Protocol_uint8_t Tx_Buffer [ UART_TX_MAX_DEPTH ];
} UARTTxBuf_t;
static UARTRxBuf_t UARTRxBuf;
static UARTTxBuf_t UARTTxBuf;
static Protocol_uint8_t UsartDataBuf [ 256 ];
static Protocol_uint8_t mDataBufPtr [ UART_DATA_BUF_LEN ];
static Protocol_uint8_t Protocol_OpenUart(void);
static Protocol_uint32_t Protocol_UartRead(Protocol_uint8_t *pData, Protocol_uint32_t u32Len);
static Protocol_uint32_t Protocol_UartSend(const Protocol_uint8_t *pData, Protocol_uint32_t u32Len);
static void Protocol_UartHandle(const Protocol_Data_t *pData);
void Protocol_KL30_Wakeup_Init(void)
{
Protocol_Func_t pFunc;
pFunc.UARTOpen_Cbk = Protocol_OpenUart;
pFunc.UARTSend_Cbk = Protocol_UartSend;
pFunc.UARTRead_Cbk = Protocol_UartRead;
pFunc.UARTClose_Cbk = Protocol_NULL;
pFunc.ProcParseCbk = Protocol_NULL;
pFunc.ProtocolSetData_Cbk = Protocol_UartHandle;
UARTTxBuf.read_pos = 0;
UARTTxBuf.write_pos = 0;
UARTRxBuf.read_pos = 0;
UARTRxBuf.write_pos = 0;
Protocol_Init(mDataBufPtr, UART_DATA_BUF_LEN, &pFunc);
}
void Protocol_Send_Service(void)
{
Protocol_uint32_t i = 0u;
Protocol_uint32_t DataLen = 0u;
Protocol_uint32_t SendLen = 0u;
static const char *TX_TASK_TAG = "TX_TASK";
esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO);
if (1/*判断上一个是否发送成功*/)
{
if ( UARTTxBuf.write_pos == UARTTxBuf.read_pos )
{
return;
}
if ( UARTTxBuf.write_pos > UARTTxBuf.read_pos )
{
DataLen = UARTTxBuf.write_pos - UARTTxBuf.read_pos;
}
else
{
DataLen = UART_TX_MAX_DEPTH - (UARTTxBuf.read_pos - UARTTxBuf.write_pos);
}
if ( DataLen > 255 )
{
SendLen = 255;
}
else
{
SendLen = DataLen;
}
for ( i = 0u; i < SendLen; i++ )
{
UsartDataBuf [ i ] = UARTTxBuf.Tx_Buffer [ UARTTxBuf.read_pos ];
UARTTxBuf.read_pos = (UARTTxBuf.read_pos + 1) % UART_TX_MAX_DEPTH;
}
//memcpy(Uart_Data.data, UsartDataBuf, SendLen);
int i = 0;
for(i = 0;i < SendLen;i++)
//printf("data is %x !!!\r\n",UsartDataBuf[i]);
sendData(TX_TASK_TAG, (const char*)UsartDataBuf);
//UART_Ch1_Send_Multiple_Byte(UsartDataBuf, SendLen);/*ESP32底层*/
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
static Protocol_uint8_t Protocol_OpenUart(void)
{
#if 0
UART_Channel_Config_st_t loc_config;
loc_config.u32UARTChEn = 1;
loc_config.u32UARTbps = 115200;
loc_config.pfnUARTConfirmCallBack = Protocol_NULL;
loc_config.pfnUARTReadMsgCallBack = UART_Put;
UART_Init(UART_RLIN31, &loc_config);
#endif
return 1;
}
static Protocol_uint32_t Protocol_UartRead(Protocol_uint8_t *pData, Protocol_uint32_t len)
{
Protocol_uint32_t i = 0;
Protocol_uint32_t DataLen = 0u;
Protocol_uint32_t ReadLen = 0u;
if ( UARTRxBuf.write_pos == UARTRxBuf.read_pos )
{
return 0; //队列空
}
if ( UARTRxBuf.write_pos > UARTRxBuf.read_pos )
{
DataLen = UARTRxBuf.write_pos - UARTRxBuf.read_pos;
}
else
{
DataLen = UART_RX_MAX_DEPTH - (UARTRxBuf.read_pos - UARTRxBuf.write_pos);
}
if ( len > DataLen )
{
ReadLen = DataLen;
}
else
{
ReadLen = len;
}
for ( i = 0u; i < ReadLen; i++ )
{
pData [ i ] = UARTRxBuf.Rx_Buffer [ UARTRxBuf.read_pos ];
UARTRxBuf.read_pos = (UARTRxBuf.read_pos + 1) % UART_RX_MAX_DEPTH;
}
return ReadLen;
}
static Protocol_uint32_t Protocol_UartSend(const Protocol_uint8_t *pData, Protocol_uint32_t u32Len)
{
Protocol_uint32_t i = 0;
Protocol_uint32_t RemainLen = 0u;
if ( UARTTxBuf.write_pos >= UARTTxBuf.read_pos )
{
RemainLen = UART_TX_MAX_DEPTH - (UARTTxBuf.write_pos - UARTTxBuf.read_pos);
}
else
{
RemainLen = UARTTxBuf.read_pos - UARTTxBuf.write_pos;
}
if ( u32Len > RemainLen )
{
return 1; //队列已满,无法插入队列
}
for ( i = 0; i < u32Len; i++ )
{
UARTTxBuf.Tx_Buffer [ UARTTxBuf.write_pos ] = pData [ i ];
UARTTxBuf.write_pos = (UARTTxBuf.write_pos + 1) % UART_TX_MAX_DEPTH;
}
return 0;
}
static void Protocol_UartHandle(const Protocol_Data_t *pData)
{
if ( pData->CmdID == 10 )
{
Uart_ID10_process();
//Core_Startup_Handle(pData->PowerSts); //数据位使用不当,原电源位,现未使用,看情况是否更改
}
else if ( pData->CmdID == 1 )
{
// Core_Heart_Handle(pData->Data, pData->DataLen);1
// SOC_HeartBeat();
}
else if(pData->CmdID == 1 )
{
// Core_OTA_PDU_REC(pData->Data, pData->DataLen);//收到有效PDU,拼接01 FF 02 [PDU]
// OTA_IC_To_Master( pData->DataLen);//CAN发送出去
}
else if(pData->CmdID == 1 )
{
/// Core_OTA_PDU_RESPON(pData->Data, pData->DataLen);//确认应答并终止重发机制
}
else
{
//非本协议数据,不处理
}
}
void UART_Put(Protocol_uint16_t Data)
{
// Protocol_uint32_t nextPos = 0u;
//
// nextPos = (UARTRxBuf.write_pos + 1) % UART_RX_MAX_DEPTH;
//
// if ( nextPos == UARTRxBuf.read_pos )
// {
// //队列已满,无法插入队列
// }
// else
// {
// UARTRxBuf.Rx_Buffer [ UARTRxBuf.write_pos ] = Data;
// UARTRxBuf.write_pos = (UARTRxBuf.write_pos + 1) % UART_RX_MAX_DEPTH;
// }
return;
}
#ifndef PROTOCOL_USER_H
#define PROTOCOL_USER_H
#include "Protocol_Lib.h"
void Protocol_KL30_Wakeup_Init(void);
void Protocol_Send_Service(void); // 10ms任务调用
void UART_Put(Protocol_uint16_t Data);
#endif
......@@ -14,7 +14,10 @@
*
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
......@@ -38,6 +41,11 @@
#include "driver/uart.h"
#include "string.h"
#include "driver/gpio.h"
#include "Protocol_CRC16.h"
#include "Protocol_Lib.h"
#include "Protocol_User.h"
typedef unsigned char uint8_t;
unsigned int flag_u;
......@@ -71,23 +79,13 @@ int sendData(const char* logName, const char* data)
static void tx_task(void *arg)
{
static const char *TX_TASK_TAG = "TX_TASK";
esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO);
while (1) {
char data_tx[3] = {0,0,0};
if (flag_u == 0)
{
data_tx[0] = 0x1;
data_tx[1] = '\0';
}
else
{
data_tx[0] = 0x2;
data_tx[1] = '\0';
}
sendData(TX_TASK_TAG, (const char*)data_tx);
vTaskDelay(2000 / portTICK_PERIOD_MS);
while (1) {
// uint8_t bbb[2] = {0x55,0x99};
// Protocol_Send(0x11,bbb,2);
//sendData(TX_TASK_TAG, (const char*)Uart_Data.data);
Protocol_Send_Service();
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
......@@ -290,28 +288,28 @@ wifi_ota_struct wifi_ota;
void wifi_ota_request_service(void )
{
// uint8_t step = 0;
int i = 0;
if(wifi_ota.en == 0)
{
vTaskDelay(5000 / portTICK_PERIOD_MS);
// // uint8_t step = 0;
// int i = 0;
// if(wifi_ota.en == 0)
// {
// vTaskDelay(5000 / portTICK_PERIOD_MS);
// printf("\n");
// printf("==========================================================\n");
// for(i = 5; i > 0; i--)
// {
// printf("=========== start simple_ota_task in %d second. ===========\n", i);
// vTaskDelay(1000 / portTICK_PERIOD_MS);
// }
// printf("=========== simple_ota_service starting... ===========\n");
// printf("==========================================================\n");
// printf("\n");
printf("\n");
printf("==========================================================\n");
for(i = 5; i > 0; i--)
{
printf("=========== start simple_ota_task in %d second. ===========\n", i);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
printf("=========== simple_ota_service starting... ===========\n");
printf("==========================================================\n");
printf("\n");
// wifi_ota.en = 1;
// }
wifi_ota.en = 1;
}
/*
if(wifi_ota.read_cnt && wifi_ota.read_cnt <= 3)
{
if(wifi_ota.time_cnt < 50)
......@@ -340,7 +338,6 @@ void wifi_ota_request_service(void )
wifi_ota.read_cnt = 0;
wifi_ota.time_cnt = 0;
}
*/
}
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
......@@ -725,6 +722,7 @@ void app_main(void)
Get_Mac_Init();
init_U();
Protocol_KL30_Wakeup_Init();
/* Initialize NVS. */
ret = nvs_flash_init();
......@@ -788,5 +786,5 @@ void app_main(void)
xTaskCreate(timer_cnt_task, "timer_cnt", 4096*2, NULL, 10, NULL);
xTaskCreate(rx_task, "uart_rx_task", 1024*2, NULL, configMAX_PRIORITIES, NULL);
xTaskCreate(tx_task, "uart_tx_task", 1024*2, NULL, configMAX_PRIORITIES-1, NULL);
xTaskCreate(simple_ota_task, "simple_ota", 4096*2, NULL, 10, NULL);
xTaskCreate(simple_ota_task, "simple_ota", 1024*2, NULL, 10, NULL);
}
......@@ -4,10 +4,8 @@
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef GATTS_TABLE_CREAT_DEMO_H
#define GATTS_TABLE_CREAT_DEMO_H
/* Attributes State Machine */
......@@ -20,3 +18,8 @@ enum
HRS_IDX_NB,
};
extern int sendData(const char* logName, const char* data);
#endif
\ No newline at end of file
......@@ -276,13 +276,13 @@ CONFIG_ESPTOOLPY_FLASHFREQ_40M=y
CONFIG_ESPTOOLPY_FLASHFREQ="40m"
# CONFIG_ESPTOOLPY_FLASHSIZE_1MB is not set
# CONFIG_ESPTOOLPY_FLASHSIZE_2MB is not set
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
# CONFIG_ESPTOOLPY_FLASHSIZE_8MB is not set
# CONFIG_ESPTOOLPY_FLASHSIZE_4MB is not set
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y
# CONFIG_ESPTOOLPY_FLASHSIZE_16MB is not set
# CONFIG_ESPTOOLPY_FLASHSIZE_32MB is not set
# CONFIG_ESPTOOLPY_FLASHSIZE_64MB is not set
# CONFIG_ESPTOOLPY_FLASHSIZE_128MB is not set
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
CONFIG_ESPTOOLPY_FLASHSIZE="8MB"
# CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE is not set
CONFIG_ESPTOOLPY_BEFORE_RESET=y
# CONFIG_ESPTOOLPY_BEFORE_NORESET is not set
......@@ -309,7 +309,7 @@ CONFIG_PARTITION_TABLE_MD5=y
#
# Example Configuration
#
CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL="http://192.168.137.1:3200/QJ500-7C.bin"
CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL="http://192.168.30.1:3200/Qj500-7C.bin"
# CONFIG_EXAMPLE_USE_CERT_BUNDLE is not set
CONFIG_EXAMPLE_SKIP_COMMON_NAME_CHECK=y
# CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF is not set
......@@ -413,7 +413,7 @@ CONFIG_BT_BTC_TASK_STACK_SIZE=3072
CONFIG_BT_BLUEDROID_PINNED_TO_CORE_0=y
# CONFIG_BT_BLUEDROID_PINNED_TO_CORE_1 is not set
CONFIG_BT_BLUEDROID_PINNED_TO_CORE=0
CONFIG_BT_BTU_TASK_STACK_SIZE=4352
CONFIG_BT_BTU_TASK_STACK_SIZE=4096
# CONFIG_BT_BLUEDROID_MEM_DEBUG is not set
# CONFIG_BT_CLASSIC_ENABLED is not set
CONFIG_BT_BLE_ENABLED=y
......@@ -1368,8 +1368,8 @@ CONFIG_LWIP_TCP_MSS=1440
CONFIG_LWIP_TCP_TMR_INTERVAL=250
CONFIG_LWIP_TCP_MSL=60000
CONFIG_LWIP_TCP_FIN_WAIT_TIMEOUT=20000
CONFIG_LWIP_TCP_SND_BUF_DEFAULT=5760
CONFIG_LWIP_TCP_WND_DEFAULT=5760
CONFIG_LWIP_TCP_SND_BUF_DEFAULT=5744
CONFIG_LWIP_TCP_WND_DEFAULT=5744
CONFIG_LWIP_TCP_RECVMBOX_SIZE=6
CONFIG_LWIP_TCP_QUEUE_OOSEQ=y
# CONFIG_LWIP_TCP_SACK_OUT is not set
......@@ -1487,7 +1487,7 @@ CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_MAX_CERTS=200
# end of Certificate Bundle
# CONFIG_MBEDTLS_ECP_RESTARTABLE is not set
CONFIG_MBEDTLS_CMAC_C=y
# CONFIG_MBEDTLS_CMAC_C is not set
CONFIG_MBEDTLS_HARDWARE_AES=y
CONFIG_MBEDTLS_HARDWARE_MPI=y
CONFIG_MBEDTLS_HARDWARE_SHA=y
......@@ -1858,7 +1858,7 @@ CONFIG_BTC_TASK_STACK_SIZE=3072
CONFIG_BLUEDROID_PINNED_TO_CORE_0=y
# CONFIG_BLUEDROID_PINNED_TO_CORE_1 is not set
CONFIG_BLUEDROID_PINNED_TO_CORE=0
CONFIG_BTU_TASK_STACK_SIZE=4352
CONFIG_BTU_TASK_STACK_SIZE=4096
# CONFIG_BLUEDROID_MEM_DEBUG is not set
# CONFIG_CLASSIC_BT_ENABLED is not set
CONFIG_GATTS_ENABLE=y
......@@ -2158,8 +2158,8 @@ CONFIG_TCP_MAXRTX=12
CONFIG_TCP_SYNMAXRTX=12
CONFIG_TCP_MSS=1440
CONFIG_TCP_MSL=60000
CONFIG_TCP_SND_BUF_DEFAULT=5760
CONFIG_TCP_WND_DEFAULT=5760
CONFIG_TCP_SND_BUF_DEFAULT=5744
CONFIG_TCP_WND_DEFAULT=5744
CONFIG_TCP_RECVMBOX_SIZE=6
CONFIG_TCP_QUEUE_OOSEQ=y
CONFIG_TCP_OVERSIZE_MSS=y
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment