1. 修改apps/chrg/applications/utils/chrg_eload.c:移除idx为0时的LCD显示条件代码,修复文件末尾换行缺失 2. 修改apps/chrg/applications/thread/chrg_comm.c:移除冗余的全局指针变量,优化调试打印信息 3. 修改apps/chrg/applications/thread/chrg_roll_sou.c:新增电流命令覆盖逻辑,优化线程接收超时配置,移除不必要的延时
513 lines
14 KiB
C
513 lines
14 KiB
C
/****************************************************************************
|
|
文件名称 : chrg_roll_sou.c
|
|
完成日期 :
|
|
当前版本号 : V1.0
|
|
主要功能 : 实现南向通信轮巡请求命令。以独立线程处理。
|
|
版本历史 : 创建原始版本
|
|
说明 :
|
|
******************************************************************************/
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <rtthread.h>
|
|
|
|
#include "chrg_def.h"
|
|
#include "chrg_led.h"
|
|
#include "chrg_thread.h"
|
|
#include "chrg_roll_sou.h"
|
|
#include "chrg_eload.h"
|
|
#include "chrg_utils.h"
|
|
|
|
#define LOG_TAG "chrg.rollsou"
|
|
#include <rtdbg.h>
|
|
|
|
#define SOU_MUTEX_TIMEOUT_MS 100
|
|
#define SOU_COM_MAX_ATTEMPTS 3
|
|
|
|
chrg_rollsou_com_t g_sou_com_batch = {0};
|
|
|
|
struct chrg_rollsou_t chrgrollsou = {
|
|
.run = RUN_AUTO,
|
|
.ch = IDX_SOU_CH1,
|
|
.part = MB_R_PART1,
|
|
|
|
};
|
|
//向通道批量缓存添加一条寄存器
|
|
int chrg_sou_com_batch_submit(eIDX_SOU_CH ch, const rt_uint16_t *regs,
|
|
const rt_uint16_t *vals, rt_uint8_t count)
|
|
{
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
struct chrg_rollsou_t *pROLL = &chrgrollsou;
|
|
sou_com_batch_ch_t *pBatch;
|
|
rt_uint16_t remaining;
|
|
rt_uint8_t i;
|
|
|
|
if ((ch >= TOTAL_SOU_CHS) || (RT_NULL == regs) || (RT_NULL == vals) ||
|
|
(count == 0) || (count > SOU_COM_BATCH_REG_MAX) || (RT_NULL == pTHR->mutex)) {
|
|
return -1;
|
|
}
|
|
if (rt_mutex_take(pTHR->mutex, rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK) {
|
|
LOG_E("sou com batch lock timeout");
|
|
return -3;
|
|
}
|
|
|
|
pBatch = &g_sou_com_batch.ch[ch];
|
|
if (pBatch->cur_idx > pBatch->reg_cnt) {
|
|
LOG_E("sou com batch index invalid: ch=%d cur=%d cnt=%d",
|
|
ch, pBatch->cur_idx, pBatch->reg_cnt);
|
|
pBatch->pending = RT_FALSE;
|
|
pBatch->cur_idx = 0;
|
|
pBatch->reg_cnt = 0;
|
|
}
|
|
|
|
/*
|
|
* cur_idx 之前的命令均已收到有效响应,追加新事务前先回收这些空间。
|
|
* 否则快速连续写入时,即使大部分命令已经执行完成,也可能因为
|
|
* reg_cnt 未清零而误报批队列已满。
|
|
*/
|
|
if (pBatch->cur_idx > 0) {
|
|
remaining = pBatch->reg_cnt - pBatch->cur_idx;
|
|
if (remaining > 0) {
|
|
memmove(pBatch->reg_list, &pBatch->reg_list[pBatch->cur_idx],
|
|
remaining * sizeof(pBatch->reg_list[0]));
|
|
memmove(pBatch->val_list, &pBatch->val_list[pBatch->cur_idx],
|
|
remaining * sizeof(pBatch->val_list[0]));
|
|
}
|
|
pBatch->reg_cnt = remaining;
|
|
pBatch->cur_idx = 0;
|
|
}
|
|
|
|
if ((pBatch->reg_cnt > SOU_COM_BATCH_REG_MAX) ||
|
|
(count > (SOU_COM_BATCH_REG_MAX - pBatch->reg_cnt))) {
|
|
LOG_E("sou com batch reg full: ch=%d queued=%d append=%d max=%d",
|
|
ch, pBatch->reg_cnt, count, SOU_COM_BATCH_REG_MAX);
|
|
rt_mutex_release(pTHR->mutex);
|
|
return -2;
|
|
}
|
|
|
|
for (i = 0; i < count; i++) {
|
|
pBatch->reg_list[pBatch->reg_cnt] = regs[i];
|
|
pBatch->val_list[pBatch->reg_cnt] = vals[i];
|
|
pBatch->reg_cnt++;
|
|
}
|
|
pBatch->pending = RT_TRUE;
|
|
pROLL->run = RUN_COM;
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 0;
|
|
}
|
|
|
|
int chrg_sou_com_batch_add_reg(eIDX_SOU_CH ch, rt_uint16_t reg, rt_uint16_t val)
|
|
{
|
|
return chrg_sou_com_batch_submit(ch, ®, &val, 1);
|
|
}
|
|
//启动批量下发(填充完影子结构体后调用)
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_set_sou_reg
|
|
函数描述: 南向通道设置寄存器请求命令
|
|
输入参数: idx:通道 reg:寄存器地址 value:寄存器值
|
|
输出参数: -
|
|
返回说明: -
|
|
其它说明: -
|
|
*****************************************************************/
|
|
void chrg_set_sou_reg (eIDX_SOU_CH idx, rt_uint16_t reg, rt_uint16_t value)
|
|
{
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
struct chrg_rollsou_t *pROLL = &chrgrollsou;
|
|
|
|
if (RT_NULL == pTHR->mutex) {
|
|
return;
|
|
}
|
|
if (rt_mutex_take(pTHR->mutex, rt_tick_from_millisecond(100)) != RT_EOK) {
|
|
LOG_E("sou command lock timeout");
|
|
return;
|
|
}
|
|
pROLL->ch = idx;
|
|
pROLL->reg = reg;
|
|
pROLL->value = value;
|
|
pROLL->run = RUN_LCD;
|
|
rt_mutex_release(pTHR->mutex);
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_set_sou_reg
|
|
函数描述: 南向通道设置寄存器请求命令
|
|
输入参数: idx:通道 reg:寄存器地址 value:寄存器值
|
|
输出参数: -
|
|
返回说明: -
|
|
其它说明: -
|
|
*****************************************************************/
|
|
void chrg_set_sou_reg_com (eIDX_SOU_CH idx, rt_uint16_t reg, rt_uint16_t value)
|
|
{
|
|
chrg_sou_com_batch_add_reg(idx, reg, value);
|
|
}
|
|
|
|
static int sou_com_batch_next(rt_uint8_t *ch, rt_uint16_t *reg, rt_uint16_t *value)
|
|
{
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
struct chrg_rollsou_t *pROLL = &chrgrollsou;
|
|
rt_uint8_t idx;
|
|
|
|
if ((RT_NULL == pTHR->mutex) ||
|
|
(rt_mutex_take(pTHR->mutex, rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK)) {
|
|
return -1;
|
|
}
|
|
|
|
for (idx = 0; idx < TOTAL_SOU_CHS; idx++) {
|
|
sou_com_batch_ch_t *pBatch = &g_sou_com_batch.ch[idx];
|
|
if (pBatch->pending != RT_TRUE) {
|
|
continue;
|
|
}
|
|
if ((pBatch->reg_cnt == 0) || (pBatch->cur_idx >= pBatch->reg_cnt)) {
|
|
pBatch->pending = RT_FALSE;
|
|
pBatch->cur_idx = 0;
|
|
pBatch->reg_cnt = 0;
|
|
continue;
|
|
}
|
|
|
|
*ch = idx;
|
|
*reg = pBatch->reg_list[pBatch->cur_idx];
|
|
*value = pBatch->val_list[pBatch->cur_idx];
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 1;
|
|
}
|
|
|
|
pROLL->run = RUN_AUTO;
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 0;
|
|
}
|
|
|
|
static int sou_com_batch_complete(rt_uint8_t ch, rt_uint16_t reg, rt_uint16_t value)
|
|
{
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
sou_com_batch_ch_t *pBatch;
|
|
|
|
if ((ch >= TOTAL_SOU_CHS) || (RT_NULL == pTHR->mutex) ||
|
|
(rt_mutex_take(pTHR->mutex, rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK)) {
|
|
return -1;
|
|
}
|
|
|
|
pBatch = &g_sou_com_batch.ch[ch];
|
|
if ((pBatch->pending != RT_TRUE) || (pBatch->cur_idx >= pBatch->reg_cnt) ||
|
|
(pBatch->reg_list[pBatch->cur_idx] != reg) ||
|
|
(pBatch->val_list[pBatch->cur_idx] != value)) {
|
|
rt_mutex_release(pTHR->mutex);
|
|
return -1;
|
|
}
|
|
|
|
pBatch->cur_idx++;
|
|
if (pBatch->cur_idx >= pBatch->reg_cnt) {
|
|
pBatch->pending = RT_FALSE;
|
|
pBatch->cur_idx = 0;
|
|
pBatch->reg_cnt = 0;
|
|
}
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 0;
|
|
}
|
|
|
|
static int sou_com_batch_cancel(rt_uint8_t ch)
|
|
{
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
sou_com_batch_ch_t *pBatch;
|
|
|
|
if ((ch >= TOTAL_SOU_CHS) || (RT_NULL == pTHR->mutex) ||
|
|
(rt_mutex_take(pTHR->mutex, rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK)) {
|
|
return -1;
|
|
}
|
|
pBatch = &g_sou_com_batch.ch[ch];
|
|
pBatch->pending = RT_FALSE;
|
|
pBatch->cur_idx = 0;
|
|
pBatch->reg_cnt = 0;
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 0;
|
|
}
|
|
|
|
static rt_bool_t sou_com_batch_pending_locked(void)
|
|
{
|
|
rt_uint8_t idx;
|
|
|
|
for (idx = 0; idx < TOTAL_SOU_CHS; idx++) {
|
|
if (g_sou_com_batch.ch[idx].pending == RT_TRUE) {
|
|
return RT_TRUE;
|
|
}
|
|
}
|
|
return RT_FALSE;
|
|
}
|
|
|
|
static void sou_single_command_finish(rt_uint8_t expected_run)
|
|
{
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
struct chrg_rollsou_t *pROLL = &chrgrollsou;
|
|
|
|
if ((RT_NULL == pTHR->mutex) ||
|
|
(rt_mutex_take(pTHR->mutex, rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK)) {
|
|
return;
|
|
}
|
|
if (pROLL->run == expected_run) {
|
|
pROLL->run = sou_com_batch_pending_locked() ? RUN_COM : RUN_AUTO;
|
|
}
|
|
rt_mutex_release(pTHR->mutex);
|
|
}
|
|
|
|
static rt_bool_t sou_com_response_matches(struct mb_msg_t *pMB, rt_uint16_t reg,
|
|
rt_uint16_t value)
|
|
{
|
|
return (pMB != RT_NULL) && (pMB->payload != RT_NULL) && (pMB->length == 8) &&
|
|
(pMB->payload[0] == ID_ELOAD) && (pMB->payload[1] == MB_REGISTER_WR) &&
|
|
(u8v_to_u16(&pMB->payload[2]) == reg) &&
|
|
(u8v_to_u16(&pMB->payload[4]) == value) &&
|
|
(mb_crc16(pMB->payload, pMB->length) == 0);
|
|
}
|
|
|
|
static void sou_msg_free(struct mb_msg_t **ppMB)
|
|
{
|
|
if ((ppMB == RT_NULL) || (*ppMB == RT_NULL)) {
|
|
return;
|
|
}
|
|
if ((*ppMB)->payload != RT_NULL) {
|
|
rt_free((*ppMB)->payload);
|
|
(*ppMB)->payload = RT_NULL;
|
|
}
|
|
rt_free(*ppMB);
|
|
*ppMB = RT_NULL;
|
|
}
|
|
|
|
static void sou_com_command_failed(rt_uint8_t ch, rt_uint16_t reg, rt_uint16_t value,
|
|
const char *reason, rt_uint8_t *retry_count)
|
|
{
|
|
(*retry_count)++;
|
|
if (*retry_count < SOU_COM_MAX_ATTEMPTS) {
|
|
return;
|
|
}
|
|
|
|
LOG_E("com batch failed after %d attempts: ch=%d reg=0x%04x value=0x%04x (%s)",
|
|
SOU_COM_MAX_ATTEMPTS, ch, reg, value, reason);
|
|
if (sou_com_batch_cancel(ch) == 0) {
|
|
*retry_count = 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_roll_sou_thread_entry
|
|
函数描述: 南向通道轮巡请求数据线程体
|
|
输入参数: *data: 本线程信息
|
|
输出参数: -
|
|
返回说明: -
|
|
其它说明: -
|
|
*****************************************************************/
|
|
void chrg_roll_sou_thread_entry (void *data)
|
|
{
|
|
struct chrg_thread_t *pTHR = (struct chrg_thread_t *)data;
|
|
|
|
if (RT_NULL == pTHR) {
|
|
return ;
|
|
}
|
|
|
|
rt_uint8_t run = 0;
|
|
rt_uint8_t index = 0, idx = 0;
|
|
rt_uint8_t batch_ch = 0;
|
|
rt_uint8_t retry_count = 0;
|
|
rt_uint8_t command_ch = 0;
|
|
rt_uint8_t single_ch = 0;
|
|
rt_uint8_t single_part = 0;
|
|
rt_uint16_t command_reg = 0;
|
|
rt_uint16_t command_value = 0;
|
|
rt_uint16_t single_reg = 0;
|
|
rt_uint16_t single_value = 0;
|
|
rt_bool_t command_valid = RT_FALSE;
|
|
rt_err_t ret = RT_EOK;
|
|
struct chrg_rollsou_t *pROLL = &chrgrollsou;
|
|
struct chrg_thread_t *pTS = &chrgthr[IDX_THR_SOUTH];
|
|
struct mb_msg_t *pMB_R = RT_NULL;
|
|
while (1) {
|
|
rt_mutex_take(pTHR->mutex, RT_WAITING_FOREVER);
|
|
run = pROLL->run;
|
|
single_ch = pROLL->ch;
|
|
single_part = pROLL->part;
|
|
single_reg = pROLL->reg;
|
|
single_value = pROLL->value;
|
|
rt_mutex_release(pTHR->mutex);
|
|
command_valid = RT_FALSE;
|
|
if (run != RUN_COM) {
|
|
retry_count = 0;
|
|
}
|
|
// rt_kprintf("------ROLL SOU START \n--------");
|
|
switch (run) {
|
|
case RUN_AUTO: {
|
|
idx = index%(TOTAL_SOU_CHS); // [0, 3]
|
|
if (0 == chrgsouth.enable[idx]) { // 通道未启用
|
|
index++;
|
|
rt_thread_mdelay(10);
|
|
continue;
|
|
} else { // 通道启用
|
|
chrg_led_flashing(IDX_LED3, TIMES_ONE, PULSE_TIME*2, PULSE_TIME*2);
|
|
if (idx != (rt_uint8_t)pROLL->idx) {
|
|
pROLL->idx = (eIDX_SOU_CH)idx;
|
|
chrg_south_switch((eIDX_SOU_CH)idx);
|
|
pROLL->part = MB_R_PART1;
|
|
}
|
|
chrg_eload_refresh(ID_ELOAD, pROLL->part);
|
|
if (pROLL->part < TOTAL_MB_R) {
|
|
index++;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
|
|
case RUN_LCD: {
|
|
chrg_south_switch((eIDX_SOU_CH)single_ch);
|
|
chrg_eload_write_reg(ID_ELOAD, single_reg, single_value);
|
|
}
|
|
break;
|
|
|
|
case RUN_COM:
|
|
{
|
|
int next_ret = sou_com_batch_next(&command_ch, &command_reg, &command_value);
|
|
if (next_ret <= 0) {
|
|
retry_count = 0;
|
|
rt_thread_mdelay(1);
|
|
continue;
|
|
}
|
|
batch_ch = command_ch;
|
|
command_valid = RT_TRUE;
|
|
chrg_south_switch((eIDX_SOU_CH)command_ch);
|
|
chrg_eload_write_reg(ID_ELOAD, command_reg, command_value);
|
|
}
|
|
break;
|
|
|
|
case RUN_CONSOLE: {
|
|
chrg_south_switch((eIDX_SOU_CH)single_ch);
|
|
chrg_eload_refresh(ID_ELOAD, single_part);
|
|
}
|
|
break;
|
|
|
|
case RUN_NONE:
|
|
default:
|
|
rt_thread_mdelay(100);
|
|
continue;
|
|
break;
|
|
}
|
|
|
|
pMB_R = RT_NULL;
|
|
ret = rt_mb_recv(pTS->mb, (rt_uint32_t *)&pMB_R, 50);
|
|
if ((RT_EOK == ret) && (RT_NULL != pMB_R)) {
|
|
rt_bool_t response_valid;
|
|
|
|
if (run == RUN_COM) {
|
|
response_valid = command_valid &&
|
|
sou_com_response_matches(pMB_R, command_reg, command_value);
|
|
} else {
|
|
response_valid = (pMB_R->payload != RT_NULL) &&
|
|
(mb_crc16(pMB_R->payload, pMB_R->length) == 0);
|
|
}
|
|
|
|
if (response_valid) {
|
|
if (run == RUN_COM) {
|
|
if (sou_com_batch_complete(batch_ch, command_reg, command_value) == 0) {
|
|
retry_count = 0;
|
|
} else {
|
|
sou_com_command_failed(batch_ch, command_reg, command_value,
|
|
"batch changed", &retry_count);
|
|
}
|
|
} else if (run != RUN_AUTO) {
|
|
sou_single_command_finish(run);
|
|
} else {
|
|
chrg_eload_parse(idx, pROLL->part, pMB_R->payload, pMB_R->length);
|
|
}
|
|
} else if (run == RUN_COM) {
|
|
sou_com_command_failed(batch_ch, command_reg, command_value,
|
|
"invalid response", &retry_count);
|
|
} else {
|
|
LOG_E("crc failed");
|
|
}
|
|
|
|
sou_msg_free(&pMB_R);
|
|
rt_thread_mdelay(20);
|
|
} else {
|
|
sou_msg_free(&pMB_R);
|
|
if ((run == RUN_COM) && command_valid) {
|
|
sou_com_command_failed(batch_ch, command_reg, command_value,
|
|
"timeout", &retry_count);
|
|
} else if (run != RUN_AUTO) {
|
|
sou_single_command_finish(run);
|
|
}
|
|
}
|
|
// rt_kprintf("------ROLL SOU END \n--------");
|
|
}
|
|
}
|
|
|
|
#if 1
|
|
static int south_send(rt_uint8_t argc, char **argv)
|
|
{
|
|
int ch = 0, pt = 0;
|
|
|
|
if (3 != argc) {
|
|
rt_kprintf("help : %s [0-3] [0-1]\r\n", argv[0]);
|
|
return -1;
|
|
}
|
|
|
|
ch = (rt_uint8_t)atoi(argv[1]);
|
|
if ((ch < 0)||(ch >= TOTAL_SOU_CHS)) {
|
|
rt_kprintf("help : %s [0-3] [0-1]\r\n", argv[0]);
|
|
return -1;
|
|
}
|
|
|
|
pt = (rt_uint8_t)atoi(argv[2]);
|
|
if ((pt < 0)||(pt >= TOTAL_MB_R)) {
|
|
rt_kprintf("help : %s [0-3] [0-1]\r\n", argv[0]);
|
|
return -1;
|
|
}
|
|
|
|
struct chrg_rollsou_t *pROLL = &chrgrollsou;
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
if ((RT_NULL == pTHR->mutex) ||
|
|
(rt_mutex_take(pTHR->mutex, rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK)) {
|
|
return -1;
|
|
}
|
|
pROLL->run = RUN_CONSOLE;
|
|
pROLL->ch = (rt_uint8_t)ch;
|
|
pROLL->part = (rt_uint8_t)pt;
|
|
rt_mutex_release(pTHR->mutex);
|
|
|
|
return 0;
|
|
}
|
|
|
|
MSH_CMD_EXPORT(south_send, south send test);
|
|
|
|
static int south_run (int argc, char **argv)
|
|
{
|
|
if (2 != argc) {
|
|
rt_kprintf("help : %s start|stop\r\n", argv[0]);
|
|
return -1;
|
|
}
|
|
|
|
struct chrg_rollsou_t *pROLL = &chrgrollsou;
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
rt_uint8_t new_run;
|
|
|
|
if (rt_strcmp(argv[1], "start") == 0) {
|
|
new_run = RUN_AUTO;
|
|
} else if (rt_strcmp(argv[1], "stop") == 0) {
|
|
new_run = RUN_NONE;
|
|
} else {
|
|
rt_kprintf("help : %s start|stop\r\n", argv[0]);
|
|
return -1;
|
|
}
|
|
if ((RT_NULL == pTHR->mutex) ||
|
|
(rt_mutex_take(pTHR->mutex, rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK)) {
|
|
return -1;
|
|
}
|
|
pROLL->run = new_run;
|
|
rt_mutex_release(pTHR->mutex);
|
|
|
|
return 0;
|
|
}
|
|
|
|
MSH_CMD_EXPORT(south_run, south auto run);
|
|
|
|
#endif
|
|
|
|
|