Files
chrg/apps/chrg/applications/thread/chrg_comm.c
T
2025-09-10 23:03:11 +08:00

93 lines
1.7 KiB
C

#include <rtthread.h>
#include "mb.h"
#include "chrg_comm.h"
#define LOG_TAG "chrg.comm"
#define DBG_LEVEL DBG_LOG
#include <rtdbg.h>
extern rt_uint16_t usSRegHoldBuf[RT_S_REG_HOLDING_NREGS];
struct chrg_comm_t chrgcomm = {
.tid_poll = RT_NULL,
.tid_send = RT_NULL,
.name_poll = THR_NAME_POLL,
.name_send = THR_NAME_SEND,
.addr = MODBUS_ADDRESS_DEFAULT,
};
static void chrg_comm_send (void *pdata)
{
struct chrg_comm_t *pCOMM = (struct chrg_comm_t *)pdata;
if (RT_NULL == pCOMM) {
LOG_E("send null.");
return ;
}
rt_uint16_t *usRegHoldingBuf;
usRegHoldingBuf = usSRegHoldBuf;
rt_base_t level;
while (1) {
level = rt_hw_interrupt_disable();
usRegHoldingBuf[3] = (rt_uint16_t)(rt_tick_get() / 100);
rt_hw_interrupt_enable(level);
rt_thread_mdelay(1000);
}
}
static void chrg_comm_poll (void *pdata)
{
struct chrg_comm_t *pCOMM = (struct chrg_comm_t *)pdata;
if (RT_NULL == pCOMM) {
LOG_E("poll null.");
return ;
}
eMBInit(MB_RTU, pCOMM->addr, 1, 115200, MB_PAR_EVEN);
eMBEnable();
while (1) {
eMBPoll();
rt_thread_mdelay(200);
}
}
static int chrg_comm_init (void)
{
struct chrg_comm_t *pCOMM = &chrgcomm;
pCOMM->tid_poll = rt_thread_create(pCOMM->name_poll, chrg_comm_poll,
pCOMM, 1024, RT_MAIN_THREAD_PRIORITY-1, 10);
if (RT_NULL != pCOMM->tid_poll) {
rt_thread_startup(pCOMM->tid_poll);
} else {
LOG_E("create thread '%s' failed.", pCOMM->name_poll);
return -1;
}
pCOMM->tid_send = rt_thread_create(pCOMM->name_send, chrg_comm_send,
pCOMM, 1024, RT_MAIN_THREAD_PRIORITY-2, 10);
if (RT_NULL != pCOMM->tid_send) {
rt_thread_startup(pCOMM->tid_send);
} else {
LOG_E("create thread '%s' failed.", pCOMM->name_send);
return -1;
}
return 0;
}
INIT_APP_EXPORT(chrg_comm_init);