114 lines
2.0 KiB
C
114 lines
2.0 KiB
C
#include <string.h>
|
|
#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,
|
|
.devname = DEV_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) {
|
|
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);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static int chrg_north_init (void)
|
|
{
|
|
rt_err_t ret;
|
|
struct chrg_north_t *pNOR = &chrgnorth;
|
|
|
|
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 1
|
|
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
|
|
|
|
|