Files
chrg/apps/chrg/applications/thread/chrg_roll.c
T
2025-10-19 22:11:21 +08:00

149 lines
3.1 KiB
C

#include <rtthread.h>
#include "chrg_north.h"
#include "chrg_south.h"
#include "chrg_sink.h"
#include "chrg_source.h"
#include "chrg_roll.h"
#define LOG_TAG "chrg.roll"
#include <rtdbg.h>
struct chrg_roll_t chrgroll = {
.name = THR_NAME_ROLL,
.tid = RT_NULL,
.idx = 0,
.en_nor = {1, 1, 1, 1},
.en_sou = {1, 1, 1, 1},
};
rt_uint8_t chrg_roll_get_idx (void)
{
rt_uint8_t idx = 0;
struct chrg_roll_t *pROLL = &chrgroll;
rt_mutex_take(pROLL->mutex, RT_WAITING_FOREVER);
idx = pROLL->idx;
rt_mutex_release(pROLL->mutex);
return idx;
}
void chrg_roll_evt_send (void)
{
struct chrg_roll_t *pROLL = &chrgroll;
if (RT_NULL != pROLL->evt) {
rt_event_send(pROLL->evt, ROLL_EVT_RECVED);
}
}
static void chrg_roll_thread_entry (void *data)
{
struct chrg_roll_t *pROLL = (struct chrg_roll_t *)data;
if (RT_NULL == pROLL) {
return ;
}
rt_uint8_t idx = 0, sou_idx = 0;
rt_err_t ret = RT_EOK;
rt_uint32_t recved = 0;
struct chrg_north_t *pNOR = &chrgnorth;
struct chrg_south_t *pSOU = &chrgsouth;
struct chrg_switch_t *pSW = RT_NULL;
while (1) {
rt_mutex_take(pROLL->mutex, RT_WAITING_FOREVER);
pROLL->idx = idx%(TOTAL_CHS+TOTAL_SOU_CHS);
rt_mutex_release(pROLL->mutex);
if (pROLL->idx < TOTAL_CHS) {
if (0 == pROLL->en_nor[pROLL->idx]) {
idx++;
rt_thread_mdelay(1);
continue;
} else {
pNOR->idx = (eIDX_CH)pROLL->idx;
chrg_switch_north(pNOR->idx);
pSW = &pNOR->sw[pNOR->idx];
if (ID_SOURCE == pSW->id) {
chrg_north_sw_uart(pNOR->idx, ID_SOURCE);
source_get_vc(pSW->source.voltage, pSW->source.current);
} else if (ID_SINK == pSW->id) {
chrg_north_sw_uart(pNOR->idx, ID_SINK);
sink_get_vc(pSW->sink.loadv, pSW->sink.loadc);
}
//rt_kprintf("Nor[%d].%d\r\n", pNOR->idx, pSW->id);
}
} else {
sou_idx = pROLL->idx - TOTAL_CHS;
if (0 == pROLL->en_sou[sou_idx]) {
idx++;
rt_thread_mdelay(1);
continue;
} else {
pSOU->idx = (eIDX_SOUTH_CH)sou_idx;
chrg_switch_south(pSOU->idx);
pSW = &pSOU->sw[pSOU->idx];
if (ID_ELOAD == pSW->id) {
source_get_vc(pSW->source.voltage, pSW->source.current);
// change pro modbus
}
//rt_kprintf("Sou[%d].%d\r\n", pSOU->idx, pSW->id);
}
}
idx++;
ret = rt_event_recv(pROLL->evt, ROLL_EVT_RECVED,
(RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR), 100, &recved); // wait 100ms
if (RT_EOK != ret) {
//LOG_E("evt recv failed. [%d]", ret);
continue;
}
if (recved & ROLL_EVT_RECVED) {
}
}
}
static int chrg_roll_init (void)
{
struct chrg_roll_t *pROLL = &chrgroll;
pROLL->mutex = rt_mutex_create(pROLL->name, RT_IPC_FLAG_FIFO);
if (RT_NULL == pROLL->mutex) {
LOG_E("create mutex '%s' failed.", pROLL->name);
return -RT_ERROR;
}
pROLL->evt = rt_event_create(pROLL->name, RT_IPC_FLAG_FIFO);
if (RT_NULL == pROLL->evt) {
LOG_E("create ev '%s' failed.", pROLL->name);
return -RT_ERROR;
}
pROLL->tid = rt_thread_create(pROLL->name, chrg_roll_thread_entry, pROLL, 2048,
RT_MAIN_THREAD_PRIORITY, 20);
if (RT_NULL != pROLL->tid) {
rt_thread_startup(pROLL->tid);
} else {
LOG_E("create thread '%s' failed.", pROLL->name);
}
return RT_EOK;
}
INIT_APP_EXPORT(chrg_roll_init);