Files
chrg/apps/chrg/applications/thread/chrg_north.c
T
2025-09-03 22:00:08 +08:00

119 lines
2.2 KiB
C

#include <rtthread.h>
#include "chrg_north.h"
#include "chrg_utils.h"
#define LOG_TAG "chrg.nor"
#define DBG_LEVEL DBG_LOG
#include <rtdbg.h>
struct chrg_north_t chrgnorth = {
.name = THR_NAME_NORTH,
.tid = RT_NULL,
.tty = RT_NULL,
.rx_len = 0,
};
int chrg_north_send (rt_uint8_t *data, rt_size_t length)
{
rt_ssize_t ret = -1;
struct chrg_tty_t *pTTY = chrgnorth.tty;
if ((RT_NULL != pTTY)&&(RT_NULL != pTTY->dev)) {
ret = rt_device_write(pTTY->dev, 0, data, length);
}
return ret;
}
static void chrg_north_thread_entry (void *data)
{
struct chrg_north_t *pNOR = (struct chrg_north_t *)data;
if ((RT_NULL == pNOR)||(RT_NULL == pNOR->tty)) {
return ;
}
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) {
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;
}
len_get = rt_device_read(pMQ->dev, 0, &pNOR->rx_buf[0], 1);
if (1 != len_get) {
continue;
}
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;
}
}
}
}
static int chrg_north_init (void)
{
rt_err_t ret;
struct chrg_north_t *pNOR = &chrgnorth;
pNOR->tty = &chrgtty[IDX_TTY_NORTH];
rt_memset(pNOR->rx_buf, 0, SIZE_BUF_UART);
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);
static int north_send(rt_uint8_t argc, char **argv)
{
return chrg_north_send("1234567890abcdef", 16);
}
MSH_CMD_EXPORT(north_send, north send test);