初始版本
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/******************************************************************************
|
||||
* @brief 环形缓冲区管理(参考linux/kfifo)
|
||||
*
|
||||
* Copyright (c) 2016~2020, <morro_luo@163.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2016-05-30 Morro 初版完成
|
||||
******************************************************************************/
|
||||
#include "ringbuffer.h"
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define min(a,b) ( (a) < (b) )? (a):(b)
|
||||
|
||||
/*
|
||||
*@brief 构造一个空环形缓冲区
|
||||
*@param[in] r - 环形缓冲区管理器
|
||||
*@param[in] buf - 数据缓冲区
|
||||
*@param[in] len - buf长度(必须是2的N次幂)
|
||||
*@retval bool
|
||||
*/
|
||||
bool ring_buf_init(ring_buf_t *r,unsigned char *buf, unsigned int len)
|
||||
{
|
||||
r->buf = buf;
|
||||
r->size = len;
|
||||
r->front = r->rear = 0;
|
||||
return (buf != NULL) && ((len & len -1) == 0);
|
||||
}
|
||||
|
||||
/*
|
||||
*@brief 清空环形缓冲区
|
||||
*@param[in] r - 待清空的环形缓冲区
|
||||
*@retval none
|
||||
*/
|
||||
void ring_buf_clr(ring_buf_t *r)
|
||||
{
|
||||
r->front = r->rear = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
*@brief 获取环形缓冲区数据长度
|
||||
*@retval 环形缓冲区中有效字节数
|
||||
*/
|
||||
int ring_buf_len(ring_buf_t *r)
|
||||
{
|
||||
return r->rear - r->front;
|
||||
}
|
||||
|
||||
/*
|
||||
*@brief 将指定长度的数据放到环形缓冲区中
|
||||
*@param[in] buf - 数据缓冲区
|
||||
* len - 缓冲区长度
|
||||
*@retval 实际放到中的数据
|
||||
*/
|
||||
int ring_buf_put(ring_buf_t *r,unsigned char *buf,unsigned int len)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned int left;
|
||||
left = r->size + r->front - r->rear;
|
||||
len = min(len , left);
|
||||
i = min(len, r->size - (r->rear & r->size - 1));
|
||||
memcpy(r->buf + (r->rear & r->size - 1), buf, i);
|
||||
memcpy(r->buf, buf + i, len - i);
|
||||
r->rear += len;
|
||||
return len;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
*@brief 从环形缓冲区中读取指定长度的数据
|
||||
*@param[in] len - 读取长度
|
||||
*@param[out] buf - 输出数据缓冲区
|
||||
*@retval 实际读取长度
|
||||
*/
|
||||
int ring_buf_get(ring_buf_t *r,unsigned char *buf,unsigned int len)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned int left;
|
||||
left = r->rear - r->front;
|
||||
len = min(len , left);
|
||||
i = min(len, r->size - (r->front & r->size - 1));
|
||||
memcpy(buf, r->buf + (r->front & r->size - 1), i);
|
||||
memcpy(buf + i, r->buf, len - i);
|
||||
r->front += len;
|
||||
return len;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/******************************************************************************
|
||||
* @brief 环形缓冲区管理(参考linux/kfifo)
|
||||
*
|
||||
* Copyright (c) 2016~2020, <morro_luo@163.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2016-05-30 Morro 初版完成
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef _RING_BUF_H_
|
||||
#define _RING_BUF_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*环形缓冲区管理器*/
|
||||
typedef struct {
|
||||
unsigned char *buf; /*环形缓冲区 */
|
||||
unsigned int size; /*环形缓冲区 */
|
||||
unsigned int front; /*头指针 */
|
||||
unsigned int rear; /*尾指针 */
|
||||
}ring_buf_t;
|
||||
|
||||
bool ring_buf_init(ring_buf_t *r,unsigned char *buf,unsigned int size);
|
||||
|
||||
void ring_buf_clr(ring_buf_t *r);
|
||||
|
||||
int ring_buf_len(ring_buf_t *r);
|
||||
|
||||
int ring_buf_put(ring_buf_t *r,unsigned char *buf,unsigned int len);
|
||||
|
||||
int ring_buf_get(ring_buf_t *r,unsigned char *buf,unsigned int len);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,122 @@
|
||||
#include "update_protocol.h"
|
||||
#include "string.h"
|
||||
|
||||
static struct
|
||||
{
|
||||
update_send_t pfnSend;
|
||||
update_callback_t EventCb;
|
||||
}g_update_protoc;
|
||||
|
||||
uint16_t CRC_Modbus(uint16_t wBase, uint8_t* para, uint16_t length)
|
||||
{
|
||||
uint16_t crc = 0xffff;
|
||||
|
||||
uint16_t index, i;
|
||||
|
||||
for (index = 0 ; index < length; index++)
|
||||
{
|
||||
crc ^= para[index];
|
||||
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
if (crc & 1)
|
||||
{
|
||||
crc >>= 1;
|
||||
crc ^= wBase;
|
||||
}
|
||||
else
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
void update_send_cmd(uint8_t Cmd, uint16_t Indx, int PageNum)
|
||||
{
|
||||
uint8_t sData[24]={0};
|
||||
uint8_t sLen =0;
|
||||
uint16_t crc16;
|
||||
|
||||
update_protocol_hd_t *Frame = (update_protocol_hd_t*)sData;
|
||||
|
||||
Frame->Header = 0x7a;
|
||||
Frame->SlvAddr = 0x01;
|
||||
Frame->Cmd = Cmd;
|
||||
|
||||
if (Cmd == 0x01)
|
||||
{
|
||||
Frame->PayloadLen = 1;
|
||||
sData[sizeof(update_protocol_hd_t)] = Indx;
|
||||
}
|
||||
else if (Cmd == 0x02)
|
||||
{
|
||||
Frame->PayloadLen = 4;
|
||||
update_protocol_get_t *GDData = (update_protocol_get_t*)&sData[sizeof(update_protocol_hd_t)];
|
||||
GDData->PackageIndex = Indx;
|
||||
GDData->PackageNum = PageNum;
|
||||
}
|
||||
|
||||
sLen = sizeof(update_protocol_hd_t) + Frame->PayloadLen;
|
||||
crc16 = CRC_Modbus(CRC16_BASE, sData, sLen);
|
||||
sData[sLen++] = crc16;
|
||||
sData[sLen++] = (crc16 >> 8) & 0x00ff;
|
||||
|
||||
g_update_protoc.pfnSend(sData, sLen);
|
||||
}
|
||||
|
||||
void update_init(update_send_t pCbs)
|
||||
{
|
||||
g_update_protoc.pfnSend =pCbs;
|
||||
}
|
||||
|
||||
void update_event_cbs_reg(update_callback_t pCbs)
|
||||
{
|
||||
|
||||
g_update_protoc.EventCb = pCbs;
|
||||
|
||||
}
|
||||
|
||||
uint8_t update_unpack(uint8_t* rxData, int rLen,uint8_t *cmd, void *pload, int *pLen){
|
||||
|
||||
uint16_t crc16;
|
||||
|
||||
uint16_t check;
|
||||
|
||||
check = CRC_Modbus(CRC16_BASE, rxData, rLen - 2);
|
||||
|
||||
crc16 = (rxData[rLen - 1] << 8) | rxData[rLen - 2];
|
||||
|
||||
if (check != crc16 || rxData[0] !=0x7a || pload ==0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
update_protocol_hd_t *Frame = (update_protocol_hd_t*)rxData;
|
||||
|
||||
*cmd = Frame->Cmd;
|
||||
|
||||
if (Frame->Cmd == 0x81){
|
||||
|
||||
//update_protocol_req_t *UpData = (update_protocol_req_t*)&rxData[sizeof(update_protocol_hd_t)];
|
||||
|
||||
pload = 0;
|
||||
|
||||
*pLen = 0;
|
||||
|
||||
|
||||
}else if (Frame->Cmd == 0x82){
|
||||
|
||||
update_protocol_rsp_t *DownData = (update_protocol_rsp_t *)&rxData[sizeof(update_protocol_hd_t)];
|
||||
|
||||
memcpy(pload , &rxData[sizeof(update_protocol_hd_t) + sizeof(update_protocol_rsp_t)],DownData->DataLen);
|
||||
|
||||
*pLen = DownData->DataLen;
|
||||
|
||||
}else{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef _UPDATE_PROTOCOL_H_
|
||||
#define _UPDATE_PROTOCOL_H_
|
||||
|
||||
#include "stdint.h"
|
||||
|
||||
#define CRC16_BASE 0xA001
|
||||
|
||||
//帧头结构体
|
||||
typedef struct {
|
||||
uint8_t Header;
|
||||
uint8_t SlvAddr;
|
||||
uint8_t Cmd;
|
||||
uint8_t PayloadLen;
|
||||
}update_protocol_hd_t;
|
||||
|
||||
//主机下发升级请求载荷结构体
|
||||
typedef struct {
|
||||
uint16_t PackageNum;
|
||||
uint32_t AppSize;
|
||||
uint32_t AppCrc32;
|
||||
}__attribute__ ((packed))update_protocol_req_t;
|
||||
|
||||
//从机请求下发结构体
|
||||
typedef struct {
|
||||
uint16_t PackageIndex;
|
||||
uint32_t PackageNum;
|
||||
}__attribute__ ((packed))update_protocol_get_t;
|
||||
|
||||
//主机应答下发数据结构体
|
||||
typedef struct {
|
||||
uint16_t PackageIndex;
|
||||
uint16_t PackageNum;
|
||||
uint8_t DataLen;
|
||||
}__attribute__ ((packed))update_protocol_rsp_t;
|
||||
|
||||
|
||||
typedef int (*update_callback_t)(uint8_t cmd, void *pdata, int len,void *pload,int size);
|
||||
|
||||
typedef void (*update_send_t)(uint8_t *pBuf,int len);
|
||||
|
||||
void update_init(update_send_t pCbs);
|
||||
|
||||
void update_event_cbs_reg(update_callback_t pCbs);
|
||||
|
||||
uint8_t update_unpack(uint8_t* rxData, int rLen,uint8_t *cmd, void *pUser, int *pLen);
|
||||
|
||||
void update_send_cmd(uint8_t Cmd, uint16_t Indx, int PageNum);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user