add parse threads

This commit is contained in:
wmano
2025-10-25 23:44:23 +08:00
parent d52c94b866
commit 604fe4688c
38 changed files with 1597 additions and 956 deletions
+182 -75
View File
@@ -1,8 +1,10 @@
#include <string.h>
#include <rtthread.h>
#include "chrg_thread.h"
#include "chrg_pkg.h"
#include "chrg_north.h"
#include "chrg_roll.h"
#include "chrg_roll_nor.h"
#include "chrg_utils.h"
#define LOG_TAG "chrg.nor"
@@ -10,11 +12,10 @@
#include <rtdbg.h>
struct chrg_north_t chrgnorth = {
.name = THR_NAME_NORTH,
.devname = DEV_NAME_NORTH,
.tid = RT_NULL,
.tty = RT_NULL,
.rx_len = 0,
.enable = {1, 1, 1, 1},
.sw[IDX_CH_0] = {
.id = ID_SINK,
},
@@ -29,27 +30,6 @@ struct chrg_north_t chrgnorth = {
},
};
const rt_base_t sw_uart[TOTAL_CHS] = {
PIN_NORTH_UART_SW1,
PIN_NORTH_UART_SW2,
PIN_NORTH_UART_SW3,
PIN_NORTH_UART_SW4
};
void chrg_north_sw_uart (eIDX_CH ch, eIDX_ID id)
{
if (ch >= TOTAL_CHS) {
return ;
}
if (ID_SOURCE == id) {
rt_pin_write(sw_uart[ch], PIN_HIGH);
} else if (ID_SINK == id) {
rt_pin_write(sw_uart[ch], PIN_LOW);
}
}
int chrg_north_send (rt_uint8_t *data, rt_size_t length)
{
rt_ssize_t ret = -1;
@@ -62,86 +42,213 @@ int chrg_north_send (rt_uint8_t *data, rt_size_t length)
return ret;
}
static void chrg_north_thread_entry (void *data)
// 提取3字节帧头
static int chrg_north_frame_head (struct rt_ringbuffer *rb, rt_uint8_t *head, eIDX_ID id)
{
struct chrg_north_t *pNOR = (struct chrg_north_t *)data;
int i = 0;
rt_uint8_t data = 0, stage = 0;
rt_size_t ret = 0;
rt_size_t len_rb = 0; // ring数据长度
rt_uint8_t len_frame = 0; // 帧长度
if ((RT_NULL == pNOR)||(RT_NULL == pNOR->tty)) {
if (RT_NULL == rb) {
return -1;
}
len_rb = rt_ringbuffer_data_len(rb);
if (len_rb < LEN_FRAME_HEAD + 2) { // 最短5字节
return -1;
}
for (i = 0; i < len_rb - 2; i++) { // 找头三个字节 [帧长, 0x00, id]
ret = rt_ringbuffer_getchar(rb, &data);
if (0 == ret) {
continue;
}
switch (stage) {
case 0:
head[0] = data;
len_frame = data;
if (data < MIN_FRAME_LENGTH) { // 长度不符
stage = 0;
continue;
}
stage = 1;
break;
case 1:
head[1] = data;
if (0x00 != data) {
stage = 0;
} else {
stage = 2;
}
break;
case 2:
head[2] = data;
if ((rt_uint8_t)id != data) {
stage = 0;
} else {
return len_frame;
}
break;
default:
break;
}
}
return -1;
}
int chrg_north_pickup_frame (struct rt_ringbuffer *rb,
struct pickup_info_t *pFrame, eIDX_NOR_CH idx)
{
int cnt = 0;
rt_size_t len_rb = 0;
if ((RT_NULL == rb)||(RT_NULL == pFrame)) {
return -1;
}
struct chrg_switch_t *pSW = &chrgnorth.sw[idx];
do {
if (0 == pFrame->findhead) {
pFrame->len_msg = chrg_north_frame_head(rb, pFrame->head, pSW->id);
if (pFrame->len_msg > 0) {
pFrame->pMB = (struct mb_msg_t *)rt_malloc(sizeof(struct mb_msg_t));
if (RT_NULL == pFrame->pMB) {
LOG_E("malloc mb failed.");
return -2;
}
pFrame->pMB->length = pFrame->len_msg;
if (pFrame->pMB->length >= SIZE_BUF_TTY) {
rt_free(pFrame->pMB);
pFrame->pMB = RT_NULL;
LOG_E("too long msg.");
return -3;
}
pFrame->pMB->payload = (rt_uint8_t *)rt_malloc(pFrame->len_msg);
if (RT_NULL == pFrame->pMB->payload) {
LOG_E("malloc buf failed.");
return -4;
}
memcpy(pFrame->pMB->payload, pFrame->head, LEN_FRAME_HEAD);
len_rb = rt_ringbuffer_data_len(rb);
if (len_rb >= pFrame->len_msg - LEN_FRAME_HEAD) {
rt_ringbuffer_get(rb, &pFrame->pMB->payload[LEN_FRAME_HEAD], pFrame->len_msg - LEN_FRAME_HEAD);
chrg_thread_mb_send(IDX_THR_NORTH, pFrame->pMB);
//chrg_pkg_decode(pFrame->pMB->payload, pFrame->len_msg, pSW);
pFrame->length = 0;
pFrame->len_msg = 0;
cnt++;
} else {
rt_ringbuffer_get(rb, &pFrame->pMB->payload[LEN_FRAME_HEAD], len_rb);
pFrame->length = LEN_FRAME_HEAD + len_rb;
pFrame->findhead = 1;
pFrame->last_tick = rt_tick_get();
break;
}
}
} else {
len_rb = rt_ringbuffer_data_len(rb);
int len_need = pFrame->len_msg - LEN_FRAME_HEAD;
if (len_rb >= len_need) {
rt_ringbuffer_get(rb, &pFrame->pMB->payload[pFrame->length], len_need);
chrg_thread_mb_send(IDX_THR_NORTH, pFrame->pMB);
//chrg_pkg_decode(pFrame->pMB->payload, pFrame->len_msg, pSW);
pFrame->length = 0;
pFrame->findhead = 0;
pFrame->len_msg = 0;
cnt++;
} else {
rt_ringbuffer_get(rb, &pFrame->pMB->payload[pFrame->length], len_rb);
pFrame->length += len_rb;
pFrame->last_tick = rt_tick_get();
break;
}
}
len_rb = rt_ringbuffer_data_len(rb);
} while (len_rb >= LEN_FRAME_HEAD);
return cnt;
}
void chrg_north_thread_entry (void *data)
{
struct chrg_thread_t *pTHR = (struct chrg_thread_t *)data;
if (RT_NULL == pTHR) {
return ;
}
int len = 0;
struct chrg_tty_t *pTTY = pNOR->tty;
struct chrg_tty_t *pTTY = RT_NULL;
struct chrg_north_t *pNOR = &chrgnorth;
pTTY = chrg_tty_create(pNOR->devname, BAUD_RATE_2000000, 0, -1, 1);
if (RT_NULL == pTTY) {
LOG_E("tty can't create.");
return ;
}
chrg_tty_set_recv_tmo(pTTY, 100);
if (chrg_tty_connect(pTTY) != RT_EOK) {
chrg_tty_destory(pTTY);
return;
return ;
}
pNOR->tty = pTTY;
pNOR->rb = rt_ringbuffer_create(SIZE_BUF_TTY*2);
if (RT_NULL == pNOR->rb) {
LOG_E("create rb '%s' failed.", pNOR->devname);
return ;
}
while (1) {
memset(pNOR->rx_buf, 0, sizeof(pNOR->rx_buf));
rt_memset(pNOR->rx_buf, 0, sizeof(pNOR->rx_buf));
len = chrg_tty_recv(pTTY, pNOR->rx_buf, sizeof(pNOR->rx_buf));
if (len <= 0) {
rt_thread_mdelay(1);
continue;
}
rt_ringbuffer_put(pNOR->rb, pNOR->rx_buf, len);
//rt_kprintf("%s\r\n", (char *)pNOR->rx_buf);
//chrg_tty_send(pTTY, pNOR->rx_buf, len);
chrg_roll_evt_send();
if (1 == pNOR->frame.findhead) {
if (rt_tick_get() >= pNOR->frame.last_tick + 100) { // 100ms
pNOR->frame.findhead = 0; // reset
//LOG_E("timeo\r\n");
}
}
chrg_north_pickup_frame(pNOR->rb, &pNOR->frame, pNOR->idx);
}
}
static int chrg_north_init (void)
{
rt_err_t ret;
struct chrg_north_t *pNOR = &chrgnorth;
int i = 0;
for (i = 0; i < TOTAL_CHS; i++) {
rt_pin_mode(sw_uart[i], PIN_MODE_OUTPUT);
chrg_north_sw_uart((eIDX_CH)i, ID_SINK);
}
pNOR->tty = chrg_tty_create(pNOR->devname, 2000000, 0, -1, 1);
if (RT_NULL == pNOR->tty) {
LOG_E("tty can't create.");
return -1;
}
rt_memset(pNOR->rx_buf, 0, SIZE_BUF_TTY);
ret = rt_mutex_init(&pNOR->lock, pNOR->name, RT_IPC_FLAG_FIFO);
if (ret != RT_EOK) {
LOG_E("init mutex '%s' failed.", pNOR->name);
return ret;
}
pNOR->tid = rt_thread_create(pNOR->name, chrg_north_thread_entry, pNOR, 4096,
RT_MAIN_THREAD_PRIORITY, 20);
if (RT_NULL != pNOR->tid) {
rt_thread_startup(pNOR->tid);
} else {
LOG_E("create thread '%s' failed.", pNOR->name);
}
return 0;
}
INIT_APP_EXPORT(chrg_north_init);
#if 0
static int north_send(rt_uint8_t argc, char **argv)
{
return chrg_north_send("1234567890abcdef", 16);
return source_get_vc(5000, 1000);
}