Files
chrg/apps/chrg/applications/thread/chrg_north.c
T
2025-10-16 22:30:43 +08:00

152 lines
2.7 KiB
C

#include <string.h>
#include <rtthread.h>
#include "chrg_north.h"
#include "chrg_roll.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,
.devname = DEV_NAME_NORTH,
.tid = RT_NULL,
.tty = RT_NULL,
.rx_len = 0,
.sw[IDX_CH_0] = {
.id = ID_SINK,
},
.sw[IDX_CH_1] = {
.id = ID_SINK,
},
.sw[IDX_CH_2] = {
.id = ID_SINK,
},
.sw[IDX_CH_3] = {
.id = ID_SINK,
},
};
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;
struct chrg_tty_t *pTTY = chrgnorth.tty;
if (RT_NULL != pTTY) {
ret = chrg_tty_send(pTTY, 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 ;
}
int len = 0;
struct chrg_tty_t *pTTY = pNOR->tty;
chrg_tty_set_recv_tmo(pTTY, 100);
if (chrg_tty_connect(pTTY) != RT_EOK) {
chrg_tty_destory(pTTY);
return;
}
while (1) {
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_kprintf("%s\r\n", (char *)pNOR->rx_buf);
//chrg_tty_send(pTTY, pNOR->rx_buf, len);
chrg_roll_evt_send();
}
}
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);
}
MSH_CMD_EXPORT(north_send, north send test);
#endif