feat: 添加杰理 JL7016 SOC 代码生成模板
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
/* ================================================================
|
||||
* jb_common.c — 工具函数实现
|
||||
* ================================================================
|
||||
*/
|
||||
#include "jb_common.h"
|
||||
|
||||
/* ════════════════════════════════════════════════════════════════
|
||||
* 校验和
|
||||
* ════════════════════════════════════════════════════════════════ */
|
||||
uint8_t jbChecksum(const uint8_t *frame, uint16_t totalLen)
|
||||
{
|
||||
if (!frame || totalLen < 4)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
uint16_t sum = 0;
|
||||
/* 从 LEN_H(buf[2]) 累加到最后一个 payload 字节(buf[totalLen-2]) */
|
||||
for (uint16_t i = 2; i < totalLen - 1; i++)
|
||||
{
|
||||
sum += frame[i];
|
||||
}
|
||||
return (uint8_t)(sum & 0xFF);
|
||||
}
|
||||
|
||||
/* ════════════════════════════════════════════════════════════════
|
||||
* 大端序读写
|
||||
* ════════════════════════════════════════════════════════════════ */
|
||||
uint16_t jbReadU16BE(const uint8_t *buf)
|
||||
{
|
||||
if (!buf)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return ((uint16_t)buf[0] << 8) | buf[1];
|
||||
}
|
||||
|
||||
uint32_t jbReadU32BE(const uint8_t *buf)
|
||||
{
|
||||
if (!buf)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return ((uint32_t)buf[0] << 24)
|
||||
| ((uint32_t)buf[1] << 16)
|
||||
| ((uint32_t)buf[2] << 8)
|
||||
| buf[3];
|
||||
}
|
||||
|
||||
void jbWriteU16BE(uint8_t *buf, uint16_t val)
|
||||
{
|
||||
if (!buf)
|
||||
{
|
||||
return;
|
||||
}
|
||||
buf[0] = (uint8_t)(val >> 8);
|
||||
buf[1] = (uint8_t)(val & 0xFF);
|
||||
}
|
||||
|
||||
void jbWriteU32BE(uint8_t *buf, uint32_t val)
|
||||
{
|
||||
if (!buf)
|
||||
{
|
||||
return;
|
||||
}
|
||||
buf[0] = (uint8_t)(val >> 24);
|
||||
buf[1] = (uint8_t)(val >> 16);
|
||||
buf[2] = (uint8_t)(val >> 8);
|
||||
buf[3] = (uint8_t)(val & 0xFF);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/* ================================================================
|
||||
* jb_common.h — 工具函数(校验和、大端序转换)
|
||||
*
|
||||
* 说明: 见宝协议采用大端序(Big-Endian)。
|
||||
* 多字节值(uint16/uint32)按高位在前传输。
|
||||
* ================================================================
|
||||
*/
|
||||
#ifndef _JB_COMMON_H
|
||||
#define _JB_COMMON_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/* ════════════════════════════════════════════════════════════════
|
||||
* 校验和
|
||||
* sum(LEN_H, LEN_L, CMD, SN, ...payload_last) & 0xFF
|
||||
* buf[0]=55, buf[1]=AA,累加范围从 buf[2] 到 buf[totalLen-2]
|
||||
* ════════════════════════════════════════════════════════════════ */
|
||||
uint8_t jbChecksum(const uint8_t *frame, uint16_t totalLen);
|
||||
|
||||
/* ════════════════════════════════════════════════════════════════
|
||||
* 大端序转换
|
||||
* ════════════════════════════════════════════════════════════════ */
|
||||
|
||||
/* 从字节数组读取 uint16(大端序)*/
|
||||
uint16_t jbReadU16BE(const uint8_t *buf);
|
||||
|
||||
/* 从字节数组读取 uint32(大端序)*/
|
||||
uint32_t jbReadU32BE(const uint8_t *buf);
|
||||
|
||||
/* 将 uint16 写入字节数组(大端序)*/
|
||||
void jbWriteU16BE(uint8_t *buf, uint16_t val);
|
||||
|
||||
/* 将 uint32 写入字节数组(大端序)*/
|
||||
void jbWriteU32BE(uint8_t *buf, uint32_t val);
|
||||
|
||||
#endif /* _JB_COMMON_H */
|
||||
@@ -0,0 +1,75 @@
|
||||
/* ================================================================
|
||||
* jb_ringbuffer.c — 环形缓冲区实现
|
||||
* ================================================================
|
||||
*/
|
||||
#include "jb_ringbuffer.h"
|
||||
|
||||
/* ── 初始化 ── */
|
||||
void jbRbInit(jb_ringbuffer_t *rb)
|
||||
{
|
||||
if (!rb)
|
||||
{
|
||||
return;
|
||||
}
|
||||
rb->write = rb->read = rb->count = 0;
|
||||
}
|
||||
|
||||
/* ── 压入一个字节 ── */
|
||||
void jbRbPush(jb_ringbuffer_t *rb, uint8_t byte)
|
||||
{
|
||||
if (!rb)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (rb->count >= JB_RB_CAPACITY)
|
||||
{
|
||||
return; /* 已满 — 丢弃 */
|
||||
}
|
||||
rb->buf[rb->write] = byte;
|
||||
rb->write = (rb->write + 1) % JB_RB_CAPACITY;
|
||||
rb->count++;
|
||||
}
|
||||
|
||||
/* ── 弹出一个字节 ── */
|
||||
uint8_t jbRbPop(jb_ringbuffer_t *rb)
|
||||
{
|
||||
if (!rb || rb->count == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
uint8_t byte = rb->buf[rb->read];
|
||||
rb->read = (rb->read + 1) % JB_RB_CAPACITY;
|
||||
rb->count--;
|
||||
return byte;
|
||||
}
|
||||
|
||||
/* ── 查看指定偏移字节(不消费)── */
|
||||
uint8_t jbRbPeek(const jb_ringbuffer_t *rb, uint16_t offset)
|
||||
{
|
||||
if (!rb || offset >= rb->count)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
uint16_t idx = (rb->read + offset) % JB_RB_CAPACITY;
|
||||
return rb->buf[idx];
|
||||
}
|
||||
|
||||
/* ── 可读字节数 ── */
|
||||
uint16_t jbRbAvailable(const jb_ringbuffer_t *rb)
|
||||
{
|
||||
if (!rb)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return rb->count;
|
||||
}
|
||||
|
||||
/* ── 清空缓冲区 ── */
|
||||
void jbRbClear(jb_ringbuffer_t *rb)
|
||||
{
|
||||
if (!rb)
|
||||
{
|
||||
return;
|
||||
}
|
||||
rb->write = rb->read = rb->count = 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/* ================================================================
|
||||
* jb_ringbuffer.h — 环形缓冲区(UART 接收)
|
||||
* ================================================================
|
||||
*/
|
||||
#ifndef _JB_RINGBUFFER_H
|
||||
#define _JB_RINGBUFFER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/* 缓冲区配置 */
|
||||
#define JB_RB_CAPACITY 256 /* 缓冲区容量(2 的幂可优化取模运算)*/
|
||||
|
||||
/* 环形缓冲区实例 */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t buf[JB_RB_CAPACITY];
|
||||
uint16_t write; /* 写索引 */
|
||||
uint16_t read; /* 读索引 */
|
||||
uint16_t count; /* 已用字节数 */
|
||||
} jb_ringbuffer_t;
|
||||
|
||||
void jbRbInit(jb_ringbuffer_t *rb);
|
||||
void jbRbPush(jb_ringbuffer_t *rb, uint8_t byte);
|
||||
uint8_t jbRbPop(jb_ringbuffer_t *rb);
|
||||
uint8_t jbRbPeek(const jb_ringbuffer_t *rb, uint16_t offset);
|
||||
uint16_t jbRbAvailable(const jb_ringbuffer_t *rb);
|
||||
void jbRbClear(jb_ringbuffer_t *rb);
|
||||
|
||||
#endif /* _JB_RINGBUFFER_H */
|
||||
Reference in New Issue
Block a user