tty use mq

This commit is contained in:
wmano
2025-09-03 22:00:08 +08:00
parent 5409d3a614
commit 7b764c34b9
14 changed files with 117 additions and 71 deletions
+29 -11
View File
@@ -3,6 +3,8 @@
#include "chrg_north.h"
#include "chrg_utils.h"
#define LOG_TAG "chrg.nor"
#define DBG_LEVEL DBG_LOG
#include <rtdbg.h>
@@ -29,32 +31,48 @@ int chrg_north_send (rt_uint8_t *data, rt_size_t length)
static void chrg_north_thread_entry (void *data)
{
rt_err_t ret = RT_EOK;
struct chrg_north_t *pNOR = (struct chrg_north_t *)data;
if ((RT_NULL == pNOR)||(RT_NULL == pNOR->tty)) {
return ;
}
rt_uint8_t ch;
rt_uint8_t crc8 = 0;
rt_size_t len_mq = 0, len_get = 0, len_msg = 0;
struct chrg_tty_t *pTTY = pNOR->tty;
struct mq_msg_t *pMQ = &pTTY->msg_mq;
while (1) {
ret = rt_sem_take(&pTTY->sem_rx, RT_WAITING_FOREVER);
if (RT_EOK != ret) {
LOG_E("sem take failed. %d", ret);
rt_memset(pMQ, 0, sizeof(struct mq_msg_t));
len_mq = rt_mq_recv(pTTY->mq, pMQ, sizeof(struct mq_msg_t), RT_WAITING_FOREVER);
if (len_mq <= 0) {
continue;
}
rt_ringbuffer_getchar(pTTY->rb, &ch);
if (pNOR->rx_len < SIZE_BUF_UART) {
pNOR->rx_buf[pNOR->rx_len++] = ch;
rt_kprintf("%c", ch);
len_get = rt_device_read(pMQ->dev, 0, &pNOR->rx_buf[0], 1);
if (1 != len_get) {
continue;
}
if (pNOR->rx_len >= 32) {
pNOR->rx_len = 0;
pNOR->rx_len = 1;
len_msg = rt_device_read(pMQ->dev, 0, &pNOR->rx_buf[1], len_get-1);
if (len_msg <= 0) {
continue;
}
pNOR->rx_len += len_msg;
if (len_msg == len_get-1) {
crc8 = calc_crc8(pNOR->rx_buf, len_msg);
if (crc8 == pNOR->rx_buf[len_msg]) {
pNOR->rx_len = 0;
}
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
#include "chrg_switch.h"
#define THR_NAME_NORTH "thr_nor"
#define THR_NAME_NORTH "thr.nor"
struct chrg_north_t {
const char *name;
+30 -12
View File
@@ -3,6 +3,8 @@
#include "chrg_south.h"
#include "chrg_utils.h"
#define LOG_TAG "chrg.sou"
#define DBG_LEVEL DBG_LOG
#include <rtdbg.h>
@@ -16,32 +18,48 @@ struct chrg_south_t chrgsouth = {
static void chrg_south_thread_entry (void *data)
{
rt_err_t ret = RT_EOK;
struct chrg_south_t *pSOU = (struct chrg_south_t *)data;
if ((RT_NULL == pSOU)||(RT_NULL == pSOU->tty)) {
return ;
}
rt_uint8_t ch;
rt_uint8_t crc8 = 0;
rt_size_t len_mq = 0, len_get = 0, len_msg = 0;
struct chrg_tty_t *pTTY = pSOU->tty;
struct mq_msg_t *pMQ = &pTTY->msg_mq;
while (1) {
ret = rt_sem_take(&pTTY->sem_rx, RT_WAITING_FOREVER);
if (RT_EOK != ret) {
LOG_E("sem take failed. %d", ret);
rt_memset(pMQ, 0, sizeof(struct mq_msg_t));
len_mq = rt_mq_recv(pTTY->mq, pMQ, sizeof(struct mq_msg_t), RT_WAITING_FOREVER);
if (len_mq <= 0) {
continue;
}
rt_ringbuffer_getchar(pTTY->rb, &ch);
if (pSOU->rx_len < SIZE_BUF_UART) {
pSOU->rx_buf[pSOU->rx_len++] = ch;
rt_kprintf("%c", ch);
len_get = rt_device_read(pMQ->dev, 0, &pSOU->rx_buf[0], 1);
if (1 != len_get) {
continue;
}
if (pSOU->rx_len >= 32) {
pSOU->rx_len = 0;
pSOU->rx_len = 1;
len_msg = rt_device_read(pMQ->dev, 0, &pSOU->rx_buf[1], len_get-1);
if (len_msg <= 0) {
continue;
}
pSOU->rx_len += len_msg;
if (len_msg == len_get-1) {
crc8 = calc_crc8(pSOU->rx_buf, len_msg);
if (crc8 == pSOU->rx_buf[len_msg]) {
pSOU->rx_len = 0;
}
}
}
}
@@ -51,7 +69,7 @@ static int chrg_south_init (void)
rt_err_t ret;
struct chrg_south_t *pSOU = &chrgsouth;
pSOU->tty = &chrgtty[IDX_TTY_NORTH];
pSOU->tty = &chrgtty[IDX_TTY_SOUTH];
rt_memset(pSOU->rx_buf, 0, SIZE_BUF_UART);
ret = rt_mutex_init(&pSOU->lock, pSOU->name, RT_IPC_FLAG_FIFO);
+1 -1
View File
@@ -6,7 +6,7 @@
#include "chrg_tty.h"
#include "chrg_switch.h"
#define THR_NAME_SOUTH "thr_sou"
#define THR_NAME_SOUTH "thr.sou"
struct chrg_south_t {
const char *name;