1. 修改apps/chrg/applications/thread/chrg_roll_sou.c:将通信最大尝试次数从3改为10 2. 修改apps/chrg/applications/thread/chrg_comm.c:更新协议板固件版本为0x0129(V1.1.25) 3. 修改apps/chrg/applications/utils/chrg_sink.c:替换初始化命令相关的硬编码宏为更语义化的常量 4. 更新docs/Allegro.jrl:更新Cadence Allegro操作日志记录
1166 lines
38 KiB
C
1166 lines
38 KiB
C
/****************************************************************************
|
|
文件名称 : chrg_roll_sou.c
|
|
完成日期 :
|
|
当前版本号 : V1.1.24
|
|
主要功能 : 实现南向通信轮巡请求命令。以独立线程处理。
|
|
版本历史 : 创建原始版本
|
|
说明 :
|
|
******************************************************************************/
|
|
|
|
#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_north.h"
|
|
#include "chrg_sink.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_RESPONSE_TIMEOUT_MS 20
|
|
#define SOU_COM_MAX_ATTEMPTS 10
|
|
#define SOU_COM_RETRY_YIELD_MS 1U
|
|
#define SOU_REL_SWITCH_DELAY_MS 100U
|
|
#define SOU_CHANNEL_BIT(ch) ((rt_uint8_t)(1U << (ch)))
|
|
|
|
chrg_rollsou_com_t g_sou_com_batch = {0};
|
|
/* 一次性P1量测分别记录待发送请求和正在等待回码的请求。
|
|
* 当前帧尚未结束时再次申请的下一帧保存在pending中,不能被当前帧
|
|
* 的完成处理清除,否则多通道运行时低电流确认会间歇性停止推进。 */
|
|
static rt_uint8_t g_sou_measure_once_pending_mask = 0U;
|
|
static rt_uint8_t g_sou_measure_once_active_mask = 0U;
|
|
|
|
/* A channel enters normal measurement polling only after its version probe
|
|
* receives a valid response. A failed probe remains pending for the next
|
|
* round, which also covers the south board's slower power-up time. */
|
|
static rt_uint8_t g_sou_version_ready[TOTAL_SOU_CHS] = {0U};
|
|
|
|
/* COM批量命令按通道轮询发送。一次写命令收到匹配回码后才移动游标;
|
|
* 回码错误或超时时游标保持不变,使同一条命令优先完成重发,避免破坏
|
|
* 本通道批次内的寄存器执行顺序。 */
|
|
static rt_uint8_t g_sou_com_batch_rr_next_ch = 0U;
|
|
static rt_uint8_t g_sou_com_batch_burst_ch = TOTAL_SOU_CHS;
|
|
static rt_uint8_t g_sou_com_batch_burst_count = 0U;
|
|
|
|
static rt_bool_t sou_com_batch_is_priority(rt_uint8_t ch,
|
|
sou_com_batch_ch_t *pBatch)
|
|
{
|
|
rt_uint16_t reg;
|
|
rt_uint16_t value;
|
|
|
|
if ((ch >= TOTAL_SOU_CHS) || (pBatch == RT_NULL) ||
|
|
(pBatch->cur_idx >= pBatch->reg_cnt)) {
|
|
return RT_FALSE;
|
|
}
|
|
reg = pBatch->reg_list[pBatch->cur_idx];
|
|
value = pBatch->val_list[pBatch->cur_idx];
|
|
if ((reg == REG_WORK) && (value == WORK_STOP)) {
|
|
return RT_TRUE;
|
|
}
|
|
if ((reg == ModbusRTU_ShortTest_CMD_MODE_FLAG) &&
|
|
(value == ModbusRTU_DYNA_CMD_MODE_STOP)) {
|
|
return RT_TRUE;
|
|
}
|
|
if ((reg == REG_REL_ON) && (value == ALL_REL_OFF)) {
|
|
return RT_TRUE;
|
|
}
|
|
if ((reg == REG_WORK) && (value == WORK_START) &&
|
|
(chrgnorth.sw[ch].sink.work_mode == SINK_WORK_ON_TEST_SHORT ||
|
|
chrgnorth.sw[ch].sink.work_mode == SINK_WORK_ON_TEST_DYNA ||
|
|
chrgnorth.sw[ch].sink.work_mode == SINK_WORK_ON_TEST_OCP)) {
|
|
return RT_TRUE;
|
|
}
|
|
return RT_FALSE;
|
|
}
|
|
|
|
struct chrg_rollsou_t chrgrollsou = {
|
|
.run = RUN_AUTO,
|
|
.ch = IDX_SOU_CH1,
|
|
.part = MB_R_PART1,
|
|
|
|
};
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_send_sou_upadata_data
|
|
函数描述: 缓存一帧升级数据并切换南向线程到升级发送状态
|
|
输入参数: buf:升级帧数据 leng:升级帧长度
|
|
输出参数: -
|
|
返回说明: 0:提交成功 -1:参数错误 -2:上一帧尚未发送 -3:互斥锁超时
|
|
其它说明: 使用结构体内部数组保存数据,防止COM接收缓存变化导致升级帧损坏
|
|
*****************************************************************/
|
|
int chrg_send_sou_upadata_data(const rt_uint8_t *buf, rt_uint16_t leng)
|
|
{
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
struct chrg_rollsou_t *pROLL = &chrgrollsou;
|
|
|
|
if ((buf == RT_NULL) || (leng == 0) || (leng > sizeof(pROLL->updata_buf)) ||
|
|
(pTHR->mutex == RT_NULL)) {
|
|
return -1;
|
|
}
|
|
if (rt_mutex_take(pTHR->mutex,
|
|
rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK) {
|
|
return -3;
|
|
}
|
|
if (pROLL->send_flag != 0) {
|
|
rt_mutex_release(pTHR->mutex);
|
|
return -2;
|
|
}
|
|
|
|
memcpy(pROLL->updata_buf, buf, leng);
|
|
pROLL->leng = leng;
|
|
pROLL->updata_result = 0;
|
|
pROLL->send_flag = 1;
|
|
pROLL->run = RUN_UPDATA;
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 0;
|
|
}
|
|
|
|
//向通道批量缓存添加一条寄存器
|
|
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;
|
|
pBatch->next_send_tick = 0U;
|
|
}
|
|
|
|
/*
|
|
* 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 ((count == 1) && (regs[0] == (REG_CURRENT_OUT + 1))) {
|
|
for (i = pBatch->cur_idx; i < pBatch->reg_cnt; i++) {
|
|
if (pBatch->reg_list[i] == regs[0]) {
|
|
pBatch->val_list[i] = vals[0];
|
|
pBatch->pending = RT_TRUE;
|
|
pROLL->run = RUN_COM;
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 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;
|
|
}
|
|
if (pBatch->reg_cnt == 0U) {
|
|
pBatch->next_send_tick = 0U;
|
|
}
|
|
|
|
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->result = 0;
|
|
pBatch->pending = RT_TRUE;
|
|
pROLL->run = RUN_COM;
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 0;
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_sou_com_batch_is_pending
|
|
函数描述: 查询指定通道是否仍有COM南向批量命令等待执行或等待有效应答
|
|
输入参数: ch:南向功率板通道
|
|
输出参数: -
|
|
返回说明: RT_TRUE:批量事务未完成或查询互斥锁失败 RT_FALSE:批量事务已完成
|
|
其它说明: 查询失败时按未完成处理,防止北向协议配置越过南向卸载命令提前执行
|
|
*****************************************************************/
|
|
rt_bool_t chrg_sou_com_batch_is_pending(eIDX_SOU_CH ch)
|
|
{
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
rt_bool_t pending;
|
|
|
|
if ((ch >= TOTAL_SOU_CHS) || (pTHR->mutex == RT_NULL) ||
|
|
(rt_mutex_take(pTHR->mutex,
|
|
rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK)) {
|
|
return RT_TRUE;
|
|
}
|
|
|
|
pending = g_sou_com_batch.ch[ch].pending;
|
|
rt_mutex_release(pTHR->mutex);
|
|
return pending;
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_sou_com_batch_get_result
|
|
函数描述: 获取指定通道最近一次南向COM批次的执行结果
|
|
输入参数: ch:南向功率板通道
|
|
输出参数: -
|
|
返回说明: 1:全部命令收到匹配回码 0:执行中/无结果 -1:失败取消 -2:参数或锁错误
|
|
其它说明: pending清零既可能是成功也可能是失败,卸载确认必须通过本结果区分
|
|
*****************************************************************/
|
|
int chrg_sou_com_batch_get_result(eIDX_SOU_CH ch)
|
|
{
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
int result;
|
|
|
|
if ((ch >= TOTAL_SOU_CHS) || (pTHR->mutex == RT_NULL) ||
|
|
(rt_mutex_take(pTHR->mutex,
|
|
rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK)) {
|
|
return -2;
|
|
}
|
|
result = g_sou_com_batch.ch[ch].result;
|
|
rt_mutex_release(pTHR->mutex);
|
|
return result;
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_sou_measure_once_submit
|
|
函数描述: 登记指定通道的一次性P1量测优先请求
|
|
输入参数: ch:南向功率板通道
|
|
输出参数: 设置通道请求位
|
|
返回说明: 0:登记成功或请求已存在 <0:参数或互斥锁错误
|
|
其它说明: RUN_COM批次完成并切回RUN_AUTO后,南向线程会优先执行该查询
|
|
*****************************************************************/
|
|
int chrg_sou_measure_once_submit(eIDX_SOU_CH ch)
|
|
{
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
|
|
if ((ch >= TOTAL_SOU_CHS) || (pTHR->mutex == RT_NULL) ||
|
|
(rt_mutex_take(pTHR->mutex,
|
|
rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK)) {
|
|
return -1;
|
|
}
|
|
g_sou_measure_once_pending_mask |= SOU_CHANNEL_BIT(ch);
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 0;
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: sou_measure_once_next
|
|
函数描述: 获取当前待执行的一次性P1量测通道
|
|
输入参数: ch:输出通道指针
|
|
输出参数: ch:最低编号的待查询通道
|
|
返回说明: 1:存在请求 0:无请求 <0:参数或互斥锁错误
|
|
其它说明: 请求位在本次查询完成或超时后清除,查询期间保持置位以防重复排队
|
|
*****************************************************************/
|
|
static int sou_measure_once_next(rt_uint8_t *ch)
|
|
{
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
rt_uint8_t idx;
|
|
|
|
if ((ch == RT_NULL) || (pTHR->mutex == RT_NULL) ||
|
|
(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++) {
|
|
rt_uint8_t bit = SOU_CHANNEL_BIT(idx);
|
|
|
|
if ((g_sou_measure_once_pending_mask & bit) != 0U) {
|
|
/* 发送前从pending转移到active。查询等待期间的新申请会重新
|
|
* 置pending位,并在当前查询结束后继续执行。 */
|
|
g_sou_measure_once_pending_mask &= (rt_uint8_t)~bit;
|
|
g_sou_measure_once_active_mask |= bit;
|
|
*ch = idx;
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 1;
|
|
}
|
|
}
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 0;
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: sou_measure_once_finish
|
|
函数描述: 结束指定通道的一次性P1量测请求
|
|
输入参数: ch:南向功率板通道
|
|
输出参数: 清除对应请求位
|
|
返回说明: -
|
|
其它说明: 有效回码和超时都会结束本次请求;负载状态机按量测序号决定是否重查
|
|
*****************************************************************/
|
|
static void sou_measure_once_finish(rt_uint8_t ch)
|
|
{
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
|
|
if ((ch >= TOTAL_SOU_CHS) || (pTHR->mutex == RT_NULL) ||
|
|
(rt_mutex_take(pTHR->mutex,
|
|
rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK)) {
|
|
return;
|
|
}
|
|
/* 只结束当前正在等待的查询;等待期间登记的下一帧pending请求必须保留。 */
|
|
g_sou_measure_once_active_mask &= (rt_uint8_t)~SOU_CHANNEL_BIT(ch);
|
|
rt_mutex_release(pTHR->mutex);
|
|
}
|
|
|
|
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_sou_trim_crc
|
|
函数描述: 按功率板协议顺序计算全部校准参数的Modbus CRC16
|
|
输入参数: pTrim:校准参数结构体
|
|
输出参数: -
|
|
返回说明: 校准参数CRC16
|
|
其它说明: CRC覆盖电压、负载电流、电源电流及零点共20字节
|
|
*****************************************************************/
|
|
static rt_uint16_t chrg_sou_trim_crc(const struct chrg_trim *pTrim)
|
|
{
|
|
rt_uint8_t buf[20];
|
|
rt_uint8_t idx = 0;
|
|
rt_uint16_t value;
|
|
|
|
#define TRIM_CRC_APPEND(_value) \
|
|
do { \
|
|
value = (rt_uint16_t)(_value); \
|
|
buf[idx++] = (rt_uint8_t)(value >> 8); \
|
|
buf[idx++] = (rt_uint8_t)(value & 0xFF); \
|
|
} while (0)
|
|
|
|
TRIM_CRC_APPEND(pTrim->trim_volt_k_H);
|
|
TRIM_CRC_APPEND(pTrim->trim_volt_k_L);
|
|
TRIM_CRC_APPEND(pTrim->trim_volt_b);
|
|
TRIM_CRC_APPEND(pTrim->trim_curr_k_H);
|
|
TRIM_CRC_APPEND(pTrim->trim_curr_k_L);
|
|
TRIM_CRC_APPEND(pTrim->trim_curr_b);
|
|
TRIM_CRC_APPEND(pTrim->trim_source_curr_k_H);
|
|
TRIM_CRC_APPEND(pTrim->trim_source_curr_k_L);
|
|
TRIM_CRC_APPEND(pTrim->trim_source_curr_b);
|
|
TRIM_CRC_APPEND(pTrim->trim_zero);
|
|
|
|
#undef TRIM_CRC_APPEND
|
|
|
|
return mb_crc16(buf, sizeof(buf));
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_sou_trim_set_commit
|
|
函数描述: 根据校准区标志填写最后一条写使能命令
|
|
输入参数: pTrim:校准参数 reg:寄存器地址指针 value:寄存器值指针
|
|
输出参数: reg:写使能地址 value:对应魔数
|
|
返回说明: -
|
|
其它说明: 0写用户校准区,1写出厂校准区
|
|
*****************************************************************/
|
|
static void chrg_sou_trim_set_commit(const struct chrg_trim *pTrim,
|
|
rt_uint16_t *reg, rt_uint16_t *value)
|
|
{
|
|
if (pTrim->trim_enable_flag == 0) {
|
|
*reg = ModbusRTU_CAL_WriteEnable_ADD;
|
|
*value = ModbusRTU_CAL_WriteValue;
|
|
} else {
|
|
*reg = ModbusRTU_CAL_WriteRecovery;
|
|
*value = ModbusRTU_CAL_WriteRecovery_Value;
|
|
}
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_sou_trim_set_volt
|
|
函数描述: 组装电源电压校准、CRC和写使能指令
|
|
输入参数: ch:南向通道 pTrim:校准参数
|
|
输出参数: -
|
|
返回说明: 0:提交成功 <0:队列提交失败
|
|
其它说明: 指令顺序为K_H、K_L、B、CRC、写使能
|
|
*****************************************************************/
|
|
static int chrg_sou_trim_set_volt(eIDX_SOU_CH ch, const struct chrg_trim *pTrim)
|
|
{
|
|
rt_uint16_t regs[5] = {
|
|
ModbusRTU_Volt_CAL_K_H, ModbusRTU_Volt_CAL_K_L,
|
|
ModbusRTU_Volt_CAL_B, ModbusRTU_Trim_CRC_CAL, 0
|
|
};
|
|
rt_uint16_t vals[5] = {
|
|
pTrim->trim_volt_k_H, pTrim->trim_volt_k_L,
|
|
(rt_uint16_t)pTrim->trim_volt_b, chrg_sou_trim_crc(pTrim), 0
|
|
};
|
|
|
|
chrg_sou_trim_set_commit(pTrim, ®s[4], &vals[4]);
|
|
return chrg_sou_com_batch_submit(ch, regs, vals, 5);
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_sou_trim_set_curr
|
|
函数描述: 组装负载电流校准、CRC和写使能指令
|
|
输入参数: ch:南向通道 pTrim:校准参数
|
|
输出参数: -
|
|
返回说明: 0:提交成功 <0:队列提交失败
|
|
其它说明: 指令顺序为K_H、K_L、B、CRC、写使能
|
|
*****************************************************************/
|
|
static int chrg_sou_trim_set_curr(eIDX_SOU_CH ch, const struct chrg_trim *pTrim)
|
|
{
|
|
rt_uint16_t regs[5] = {
|
|
ModbusRTU_Current_CAL_K_H, ModbusRTU_Current_CAL_K_L,
|
|
ModbusRTU_Current_CAL_B, ModbusRTU_Trim_CRC_CAL, 0
|
|
};
|
|
rt_uint16_t vals[5] = {
|
|
pTrim->trim_curr_k_H, pTrim->trim_curr_k_L,
|
|
(rt_uint16_t)pTrim->trim_curr_b, chrg_sou_trim_crc(pTrim), 0
|
|
};
|
|
|
|
chrg_sou_trim_set_commit(pTrim, ®s[4], &vals[4]);
|
|
return chrg_sou_com_batch_submit(ch, regs, vals, 5);
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_sou_trim_set_source_curr
|
|
函数描述: 组装电源电流校准、CRC和写使能指令
|
|
输入参数: ch:南向通道 pTrim:校准参数
|
|
输出参数: -
|
|
返回说明: 0:提交成功 <0:队列提交失败
|
|
其它说明: 指令顺序为K_H、K_L、B、CRC、写使能
|
|
*****************************************************************/
|
|
static int chrg_sou_trim_set_source_curr(eIDX_SOU_CH ch,
|
|
const struct chrg_trim *pTrim)
|
|
{
|
|
rt_uint16_t regs[5] = {
|
|
ModbusRTU_SourceCurrent_CAL_K_H, ModbusRTU_SourceCurrent_CAL_K_L,
|
|
ModbusRTU_SourceCurrent_CAL_B, ModbusRTU_Trim_CRC_CAL, 0
|
|
};
|
|
rt_uint16_t vals[5] = {
|
|
pTrim->trim_source_curr_k_H, pTrim->trim_source_curr_k_L,
|
|
(rt_uint16_t)pTrim->trim_source_curr_b, chrg_sou_trim_crc(pTrim), 0
|
|
};
|
|
|
|
chrg_sou_trim_set_commit(pTrim, ®s[4], &vals[4]);
|
|
return chrg_sou_com_batch_submit(ch, regs, vals, 5);
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_sou_trim_set_zero
|
|
函数描述: 组装零点校准、CRC和写使能指令
|
|
输入参数: ch:南向通道 pTrim:校准参数
|
|
输出参数: -
|
|
返回说明: 0:提交成功 <0:队列提交失败
|
|
其它说明: 指令顺序为ZERO、CRC、写使能
|
|
*****************************************************************/
|
|
static int chrg_sou_trim_set_zero(eIDX_SOU_CH ch, const struct chrg_trim *pTrim)
|
|
{
|
|
rt_uint16_t regs[3] = {ModbusRTU_Trim_Zero_CAL, ModbusRTU_Trim_CRC_CAL, 0};
|
|
rt_uint16_t vals[3] = {
|
|
(rt_uint16_t)pTrim->trim_zero, chrg_sou_trim_crc(pTrim), 0
|
|
};
|
|
|
|
chrg_sou_trim_set_commit(pTrim, ®s[2], &vals[2]);
|
|
return chrg_sou_com_batch_submit(ch, regs, vals, 3);
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: chrg_sou_trim_submit
|
|
函数描述: 根据校准类型选择组包函数并提交到南向RUN_COM队列
|
|
输入参数: ch:南向功率板通道 pTrim:已解析的校准参数
|
|
输出参数: -
|
|
返回说明: 0:提交成功 <0:参数、校准模式或队列提交失败
|
|
其它说明: 本函数不修改南向enable,RUN_COM按ch主动切换目标通道
|
|
*****************************************************************/
|
|
int chrg_sou_trim_submit(eIDX_SOU_CH ch, const struct chrg_trim *pTrim)
|
|
{
|
|
if ((ch >= TOTAL_SOU_CHS) || (pTrim == RT_NULL)) {
|
|
return -1;
|
|
}
|
|
|
|
switch (pTrim->trim_mode) {
|
|
case MODE_VOLT_CAL:
|
|
return chrg_sou_trim_set_volt(ch, pTrim);
|
|
case MODE_CURR_CAL:
|
|
return chrg_sou_trim_set_curr(ch, pTrim);
|
|
case MODE_SOURCE_CURR_CAL:
|
|
return chrg_sou_trim_set_source_curr(ch, pTrim);
|
|
case MODE_ZERO_CAL:
|
|
return chrg_sou_trim_set_zero(ch, pTrim);
|
|
default:
|
|
return -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_bool_t pending_wait = RT_FALSE;
|
|
rt_tick_t now;
|
|
rt_uint8_t idx;
|
|
rt_uint8_t offset;
|
|
|
|
if ((RT_NULL == pTHR->mutex) ||
|
|
(rt_mutex_take(pTHR->mutex, rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) != RT_EOK)) {
|
|
return -1;
|
|
}
|
|
|
|
now = rt_tick_get();
|
|
/* 测试切换和停止命令先执行,避免被其它通道的长测试批次拖延。 */
|
|
for (offset = 0; offset < TOTAL_SOU_CHS; offset++) {
|
|
idx = (rt_uint8_t)((g_sou_com_batch_rr_next_ch + offset) %
|
|
TOTAL_SOU_CHS);
|
|
sou_com_batch_ch_t *pBatch = &g_sou_com_batch.ch[idx];
|
|
if ((pBatch->pending != RT_TRUE) ||
|
|
(pBatch->cur_idx >= pBatch->reg_cnt) ||
|
|
!sou_com_batch_is_priority(idx, pBatch) ||
|
|
((pBatch->next_send_tick != 0U) &&
|
|
((rt_int32_t)(now - pBatch->next_send_tick) < 0))) {
|
|
continue;
|
|
}
|
|
pBatch->next_send_tick = 0U;
|
|
*ch = idx;
|
|
*reg = pBatch->reg_list[pBatch->cur_idx];
|
|
*value = pBatch->val_list[pBatch->cur_idx];
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 1;
|
|
}
|
|
/* 短路保活的WORK_START达到周期后优先下发,避免长批次挤占30ms保活窗口。 */
|
|
for (offset = 0; offset < TOTAL_SOU_CHS; offset++) {
|
|
idx = (rt_uint8_t)((g_sou_com_batch_rr_next_ch + offset) %
|
|
TOTAL_SOU_CHS);
|
|
sou_com_batch_ch_t *pBatch = &g_sou_com_batch.ch[idx];
|
|
if ((pBatch->pending != RT_TRUE) ||
|
|
(pBatch->cur_idx >= pBatch->reg_cnt) ||
|
|
(pBatch->reg_list[pBatch->cur_idx] != REG_WORK) ||
|
|
(pBatch->val_list[pBatch->cur_idx] != WORK_START) ||
|
|
(chrgnorth.sw[idx].id != ID_SINK) ||
|
|
(chrgnorth.sw[idx].sink.work_mode != SINK_WORK_ON_TEST_SHORT) ||
|
|
(chrgnorth.sw[idx].sink.test.test_mode != SINK_TEST_SHORT) ||
|
|
((pBatch->next_send_tick != 0U) &&
|
|
((rt_int32_t)(now - pBatch->next_send_tick) < 0))) {
|
|
continue;
|
|
}
|
|
pBatch->next_send_tick = 0U;
|
|
*ch = idx;
|
|
*reg = pBatch->reg_list[pBatch->cur_idx];
|
|
*value = pBatch->val_list[pBatch->cur_idx];
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 1;
|
|
}
|
|
/* 从上一次成功命令的下一个通道开始查找,使多通道批次按
|
|
* "CH1一条 -> CH2一条 -> ..." 交替执行,而不是低编号通道独占总线。 */
|
|
for (offset = 0; offset < TOTAL_SOU_CHS; offset++) {
|
|
idx = (rt_uint8_t)((g_sou_com_batch_rr_next_ch + offset) %
|
|
TOTAL_SOU_CHS);
|
|
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->result = -1;
|
|
pBatch->cur_idx = 0;
|
|
pBatch->reg_cnt = 0;
|
|
pBatch->next_send_tick = 0U;
|
|
continue;
|
|
}
|
|
pending_wait = RT_TRUE;
|
|
if ((pBatch->next_send_tick != 0U) &&
|
|
((rt_int32_t)(now - pBatch->next_send_tick) < 0)) {
|
|
/* 当前通道处于继电器保护时间时,继续查找其它可发送通道。 */
|
|
continue;
|
|
}
|
|
pBatch->next_send_tick = 0U;
|
|
|
|
*ch = idx;
|
|
*reg = pBatch->reg_list[pBatch->cur_idx];
|
|
*value = pBatch->val_list[pBatch->cur_idx];
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 1;
|
|
}
|
|
|
|
if (!pending_wait) {
|
|
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 ((reg == REG_REL_ON) &&
|
|
(((value & MAIN_REL_ON) != 0U) || (value == FU_REL_ON))) {
|
|
chrg_eload_ripple_filter_relay_start(ch);
|
|
}
|
|
if ((reg == REG_WORK) && (value == WORK_STOP)) {
|
|
chrg_eload_ripple_filter_relay_stop(ch);
|
|
}
|
|
/* 暂时关闭副继电器与主继电器之间的固定延时;需要恢复时重新启用本段。 */
|
|
if ((reg == REG_REL_ON) && (value == ALL_REL_OFF) &&
|
|
(pBatch->cur_idx < pBatch->reg_cnt) &&
|
|
(pBatch->reg_list[pBatch->cur_idx] == REG_REL_ON) &&
|
|
(pBatch->val_list[pBatch->cur_idx] == FU_REL_ON)) {
|
|
pBatch->next_send_tick = rt_tick_get() +
|
|
rt_tick_from_millisecond(SOU_REL_SWITCH_DELAY_MS);
|
|
} else if ((reg == REG_REL_ON) && (value == FU_REL_ON) &&
|
|
(pBatch->cur_idx < pBatch->reg_cnt) &&
|
|
(pBatch->reg_list[pBatch->cur_idx] == REG_REL_ON) &&
|
|
(pBatch->val_list[pBatch->cur_idx] == ALL_REL_ON)) {
|
|
pBatch->next_send_tick = rt_tick_get() +
|
|
rt_tick_from_millisecond(SOU_REL_SWITCH_DELAY_MS);
|
|
}
|
|
if (pBatch->cur_idx >= pBatch->reg_cnt) {
|
|
pBatch->pending = RT_FALSE;
|
|
pBatch->result = 1;
|
|
pBatch->cur_idx = 0;
|
|
pBatch->reg_cnt = 0;
|
|
pBatch->next_send_tick = 0U;
|
|
g_sou_com_batch_burst_ch = TOTAL_SOU_CHS;
|
|
g_sou_com_batch_burst_count = 0U;
|
|
g_sou_com_batch_rr_next_ch = (rt_uint8_t)((ch + 1U) % TOTAL_SOU_CHS);
|
|
} else if (g_sou_com_batch_burst_ch != ch) {
|
|
g_sou_com_batch_burst_ch = ch;
|
|
g_sou_com_batch_burst_count = 1U;
|
|
g_sou_com_batch_rr_next_ch = ch;
|
|
} else if (++g_sou_com_batch_burst_count >= SOU_COM_BURST_MAX) {
|
|
g_sou_com_batch_burst_ch = TOTAL_SOU_CHS;
|
|
g_sou_com_batch_burst_count = 0U;
|
|
g_sou_com_batch_rr_next_ch = (rt_uint8_t)((ch + 1U) % TOTAL_SOU_CHS);
|
|
} else {
|
|
g_sou_com_batch_rr_next_ch = ch;
|
|
}
|
|
rt_mutex_release(pTHR->mutex);
|
|
return 0;
|
|
}
|
|
|
|
int chrg_sou_com_batch_cancel(eIDX_SOU_CH 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->result = -1;
|
|
pBatch->cur_idx = 0;
|
|
pBatch->reg_cnt = 0;
|
|
pBatch->next_send_tick = 0U;
|
|
if (g_sou_com_batch_burst_ch == ch) {
|
|
g_sou_com_batch_burst_ch = TOTAL_SOU_CHS;
|
|
g_sou_com_batch_burst_count = 0U;
|
|
g_sou_com_batch_rr_next_ch =
|
|
(rt_uint8_t)((ch + 1U) % TOTAL_SOU_CHS);
|
|
}
|
|
/* 已取消事务对应的优先P1量测同样失效,后续状态会重新申请新量测。 */
|
|
g_sou_measure_once_pending_mask &= (rt_uint8_t)~SOU_CHANNEL_BIT(ch);
|
|
g_sou_measure_once_active_mask &= (rt_uint8_t)~SOU_CHANNEL_BIT(ch);
|
|
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;
|
|
}
|
|
|
|
/*****************************************************************
|
|
函数名称: sou_com_batch_current_matches
|
|
函数描述: 判断当前南向批次正在等待的命令是否仍与指定命令一致
|
|
输入参数: ch:南向通道 reg:寄存器地址 value:寄存器值
|
|
输出参数: -
|
|
返回说明: RT_TRUE:指定命令仍是该通道当前批次的等待项
|
|
RT_FALSE:批次已取消、已替换或当前等待项不同
|
|
其它说明: 用于忽略取消旧批次后迟到的回码,防止旧回码触发新安全批次重试或取消
|
|
*****************************************************************/
|
|
static rt_bool_t sou_com_batch_current_matches(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;
|
|
rt_bool_t matched;
|
|
|
|
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 RT_FALSE;
|
|
}
|
|
|
|
pBatch = &g_sou_com_batch.ch[ch];
|
|
matched = (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 matched;
|
|
}
|
|
|
|
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)
|
|
{
|
|
/* 该命令所属批次已被安全状态替换时,迟到回码或超时不得影响新批次。 */
|
|
if (!sou_com_batch_current_matches(ch, reg, value)) {
|
|
*retry_count = 0;
|
|
return;
|
|
}
|
|
|
|
(*retry_count)++;
|
|
if (*retry_count < SOU_COM_MAX_ATTEMPTS) {
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
if (rt_mutex_take(pTHR->mutex,
|
|
rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) == RT_EOK) {
|
|
sou_com_batch_ch_t *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)) {
|
|
pBatch->next_send_tick = rt_tick_get() +
|
|
rt_tick_from_millisecond(SOU_COM_RETRY_YIELD_MS);
|
|
g_sou_com_batch_rr_next_ch =
|
|
(rt_uint8_t)((ch + 1U) % TOTAL_SOU_CHS);
|
|
g_sou_com_batch_burst_ch = TOTAL_SOU_CHS;
|
|
g_sou_com_batch_burst_count = 0U;
|
|
}
|
|
rt_mutex_release(pTHR->mutex);
|
|
}
|
|
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 (chrg_sou_com_batch_cancel((eIDX_SOU_CH)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[TOTAL_SOU_CHS] = {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_bool_t measure_once = RT_FALSE;
|
|
rt_bool_t version_probe = RT_FALSE;
|
|
rt_uint8_t response_part = MB_R_PART1;
|
|
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) {
|
|
chrg_sink_short_keepalive_process();
|
|
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;
|
|
measure_once = RT_FALSE;
|
|
version_probe = RT_FALSE;
|
|
response_part = MB_R_PART1;
|
|
if ((run != RUN_COM) && (run != RUN_UPDATA)) {
|
|
rt_memset(retry_count, 0, sizeof(retry_count));
|
|
}
|
|
// rt_kprintf("------ROLL SOU START \n--------");
|
|
switch (run) {
|
|
case RUN_AUTO: {
|
|
int measure_ret = sou_measure_once_next(&idx);
|
|
if (measure_ret > 0) {
|
|
/* 卸载确认优先读取当前通道P1,不等待四通道普通轮询走完。 */
|
|
measure_once = RT_TRUE;
|
|
response_part = MB_R_PART1;
|
|
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);
|
|
}
|
|
if (g_sou_version_ready[idx] == 0U) {
|
|
version_probe = RT_TRUE;
|
|
chrg_eload_read_version(ID_ELOAD);
|
|
} else {
|
|
chrg_eload_refresh(ID_ELOAD, MB_R_PART1);
|
|
}
|
|
break;
|
|
}
|
|
idx = index%(TOTAL_SOU_CHS); // [0, 3]
|
|
response_part = pROLL->part;
|
|
if (0 == chrgsouth.enable[idx]) { // 通道未启用
|
|
CHRG_CHANNEL_NEXT(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;
|
|
}
|
|
if (g_sou_version_ready[idx] == 0U) {
|
|
version_probe = RT_TRUE;
|
|
chrg_eload_read_version(ID_ELOAD);
|
|
} else {
|
|
chrg_eload_refresh(ID_ELOAD, pROLL->part);
|
|
}
|
|
if (pROLL->part < TOTAL_MB_R) {
|
|
CHRG_CHANNEL_NEXT(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) {
|
|
rt_thread_mdelay(1);
|
|
continue;
|
|
}
|
|
batch_ch = command_ch;
|
|
command_valid = RT_TRUE;
|
|
chrg_south_switch((eIDX_SOU_CH)command_ch);
|
|
#if SOUTH_COMMAND_DEBUG
|
|
rt_kprintf("ch=%d",command_ch);
|
|
#endif
|
|
chrg_eload_write_reg(ID_ELOAD, command_reg, command_value);
|
|
}
|
|
break;
|
|
|
|
case RUN_UPDATA:
|
|
{
|
|
/*
|
|
* 升级响应是单字节ACK/NAK/C,不是Modbus消息,因此本分支发送后
|
|
* 直接返回循环,由COM升级状态机读取响应,不能进入下方邮箱等待。
|
|
*/
|
|
if ((pROLL->mode != 1) || (pROLL->send_flag == 0) ||
|
|
(pROLL->leng == 0) || (pROLL->leng > sizeof(pROLL->updata_buf))) {
|
|
rt_thread_mdelay(1);
|
|
continue;
|
|
}
|
|
chrg_south_switch((eIDX_SOU_CH)single_ch);
|
|
ret = chrg_eload_send_ymodem_data(pROLL->updata_buf, pROLL->leng);
|
|
|
|
rt_mutex_take(pTHR->mutex, RT_WAITING_FOREVER);
|
|
pROLL->updata_result = (rt_int16_t)ret;
|
|
pROLL->send_flag = 0;
|
|
rt_mutex_release(pTHR->mutex);
|
|
rt_thread_mdelay(1);
|
|
continue;
|
|
}
|
|
|
|
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,
|
|
rt_tick_from_millisecond(SOU_RESPONSE_TIMEOUT_MS));
|
|
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[batch_ch] = 0;
|
|
} else {
|
|
sou_com_command_failed(batch_ch, command_reg, command_value,
|
|
"batch changed", &retry_count[batch_ch]);
|
|
}
|
|
} else if (run != RUN_AUTO) {
|
|
if ((run == RUN_LCD) && (single_reg == REG_REL_ON) &&
|
|
(((single_value & MAIN_REL_ON) != 0U) ||
|
|
(single_value == FU_REL_ON))) {
|
|
chrg_eload_ripple_filter_relay_start(single_ch);
|
|
}
|
|
if ((run == RUN_LCD) && (single_reg == REG_WORK) &&
|
|
(single_value == WORK_STOP)) {
|
|
chrg_eload_ripple_filter_relay_stop(single_ch);
|
|
}
|
|
sou_single_command_finish(run);
|
|
} else {
|
|
if (version_probe) {
|
|
if (chrg_eload_parse_version(idx, pMB_R->payload,
|
|
pMB_R->length) == 0) {
|
|
g_sou_version_ready[idx] = 1U;
|
|
pROLL->part = MB_R_PART1;
|
|
}
|
|
} else {
|
|
chrg_eload_parse(idx, response_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[batch_ch]);
|
|
} else {
|
|
LOG_E("crc failed");
|
|
}
|
|
if ((run == RUN_AUTO) && measure_once) {
|
|
sou_measure_once_finish(idx);
|
|
}
|
|
|
|
sou_msg_free(&pMB_R);
|
|
} else {
|
|
sou_msg_free(&pMB_R);
|
|
if ((run == RUN_AUTO) && measure_once) {
|
|
sou_measure_once_finish(idx);
|
|
}
|
|
if ((run == RUN_COM) && command_valid) {
|
|
sou_com_command_failed(batch_ch, command_reg, command_value,
|
|
"timeout", &retry_count[batch_ch]);
|
|
} 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
|
|
|
|
|