1. apps/chrg/applications/thread/chrg_roll_nor.c: 新增chrg_roll_nor_cmd、chrg_roll_nor_set函数注释,补充线程判断逻辑注释 2. apps/chrg/applications/thread/chrg_comm.c: 完善modbus写入寄存器的注释,补充角色切换逻辑说明 3. apps/chrg/applications/utils/chrg_source.c: 为所有电源状态机函数补充详细中文注释与函数说明 4. apps/chrg/applications/thread/chrg_north.c: 删除冗余全局变量与函数,补充帧处理、解析函数注释,修正初始化注释 5. apps/chrg/applications/utils/chrg_sink.c: 删除大量废弃测试代码,为所有负载状态机函数补充详细中文注释与函数说明
693 lines
20 KiB
C
693 lines
20 KiB
C
/****************************************************************************
|
||
文件名称 : chrg_north.c
|
||
完成日期 :
|
||
当前版本号 : V1.0
|
||
主要功能 : 实现北向通信交互,包括接收命令解析,发送请求命令,切换通路。以独立线程处理。
|
||
版本历史 : 创建原始版本
|
||
说明 :
|
||
******************************************************************************/
|
||
|
||
#include <string.h>
|
||
#include <stdlib.h>
|
||
#include <rtthread.h>
|
||
#include "chrg_eload.h"
|
||
#include "chrg_gpio.h"
|
||
#include "chrg_thread.h"
|
||
#include "chrg_north_pkg.h"
|
||
#include "chrg_north.h"
|
||
#include "chrg_roll_nor.h"
|
||
#include "chrg_switch.h"
|
||
#include "chrg_utils.h"
|
||
#include "ulog.h"
|
||
#include "chrg_rel.h"
|
||
//#define LOG_TAG "chrg.nor"
|
||
#define DBG_LEVEL DBG_LOG
|
||
#include <rtdbg.h>
|
||
|
||
/*****************************************************************
|
||
函数名称: chrg_source_standard_output
|
||
功能描述: 南向功率源标准输出控制函数
|
||
函数描述: 根据CV标志位,分别设置电源输出电压或输出电流,实现标准模式下的恒压/恒流输出
|
||
输入参数:
|
||
idx :功率通道编号
|
||
pSW :北向通道控制结构体指针,包含电压/电流设定值、CV控制标志等参数
|
||
输出参数:
|
||
pSW :更新CV控制标志位
|
||
返回说明: 无返回值
|
||
*****************************************************************/
|
||
void chrg_source_standard_output(eIDX_SOU_CH idx, struct chrg_switch_t *pSW)
|
||
{
|
||
if(pSW->source.Set_CV_Flag) {
|
||
chrg_set_sou_reg((eIDX_SOU_CH)idx, REG_VOLTAGE_OUT+1, pSW->source.max_vol);
|
||
#if DEBUG_NORTH
|
||
rt_kprintf("Volt_Set%d\r\n",pSW->source.vc.set_voltage);
|
||
#endif
|
||
pSW->source.Set_CV_Flag = 1;
|
||
pSW->change_flag = 0;
|
||
}
|
||
}
|
||
|
||
/*****************************************************************
|
||
函数名称: chrg_source_parse
|
||
函数描述: 解析北向电源板电压电流回码并驱动Source状态机
|
||
输入参数: frame_data:完整北向回码 idx:当前通道 pSW:通道控制结构体
|
||
pNOR:北向总控结构体
|
||
输出参数: 更新协商电压、电流、source.work_mode及上次下发电压缓存
|
||
返回说明: -
|
||
其它说明: frame_data[4~7]为实测值,[8~11]为协议协商目标值;
|
||
目标电压变化时进入RUNING,相同时进入WAIT
|
||
*****************************************************************/
|
||
void chrg_source_parse(rt_uint8_t *frame_data, eIDX_SOU_CH idx,
|
||
struct chrg_switch_t *pSW, struct chrg_north_t *pNOR)
|
||
{
|
||
rt_uint16_t volt_mv = (frame_data[8] << 8) | frame_data[9];
|
||
rt_uint16_t Curr_mA = (frame_data[10] << 8) | frame_data[11];
|
||
rt_uint16_t volt_mv_Now = (frame_data[4] << 8) | frame_data[5];
|
||
rt_uint16_t Curr_mA_Now = (frame_data[6] << 8) | frame_data[7];
|
||
static rt_uint16_t last_volt_mv[TOTAL_SOU_CHS] = {0};
|
||
static rt_uint8_t number = 0;
|
||
int ret;
|
||
|
||
if ((idx >= TOTAL_SOU_CHS) || (pSW == RT_NULL)) {
|
||
return;
|
||
}
|
||
pSW->source.vc.set_voltage = volt_mv;
|
||
pSW->source.vc.set_current = Curr_mA;
|
||
if (pSW->source.On_Flag) {
|
||
if ((volt_mv == last_volt_mv[idx])) {
|
||
pSW->source.work_mode = SOURCE_WORK_WAIT;
|
||
} else {
|
||
pSW->source.work_mode = SOURCE_WORK_RUNING;
|
||
}
|
||
}
|
||
ret = chrg_source_work_state(idx, pSW);
|
||
if ((ret >= 0) && (pSW->source.work_mode == SOURCE_WORK_RUNING)) {
|
||
last_volt_mv[idx] = volt_mv;
|
||
}
|
||
}
|
||
|
||
/*****************************************************************
|
||
函数名称: chrg_source_setting
|
||
函数描述: 根据电源协议类型和启停标志选择标准输出或快充解析流程
|
||
输入参数: frame_data:北向回码 idx:当前通道 pSW:通道控制结构体
|
||
pNOR:北向总控结构体
|
||
输出参数: 可能更新功率板设定值或北向子状态sub
|
||
返回说明: -
|
||
其它说明: 标准协议直接设置输出,快充协议使用协商结果驱动状态机
|
||
*****************************************************************/
|
||
void chrg_source_setting(rt_uint8_t *frame_data, eIDX_SOU_CH idx,
|
||
struct chrg_switch_t *pSW, struct chrg_north_t *pNOR)
|
||
{
|
||
//执行输出调整
|
||
if (pSW->source.On_Flag == 1) {
|
||
if(pSW->source.protocol==0x00){//标准下输出设定值
|
||
chrg_source_standard_output(idx, pSW);
|
||
}
|
||
else{ //快充部分按协商结果输出
|
||
chrg_source_parse(frame_data, idx, pSW, pNOR);
|
||
// rt_kprintf("set_end\r\n");
|
||
}
|
||
}else if(pSW->source.On_Flag == 0){
|
||
//chrg_source_stop(frame_data,pSW,idx,&pNOR->enable[idx]);
|
||
pSW->sub = IDX_SET_SOURCE_STOP_STATE;
|
||
}
|
||
}
|
||
|
||
/*****************************************************************
|
||
函数名称: chrg_sink_parse
|
||
函数描述: 解析北向负载板回码并根据电压、电流反馈驱动Sink状态机
|
||
输入参数: frame_data:完整北向回码 idx:当前通道 pSW:通道控制结构体
|
||
pNOR:北向总控结构体
|
||
输出参数: 更新VZ、VF、低压计数、无电流计数及sink.work_mode
|
||
返回说明: -
|
||
其它说明: 电压连续低于门槛进入LOW;电压达到目标附近进入RUNING;
|
||
运行电流连续过低时退回WAIT重新判断
|
||
*****************************************************************/
|
||
void chrg_sink_parse(rt_uint8_t *frame_data, eIDX_SOU_CH idx,
|
||
struct chrg_switch_t *pSW, struct chrg_north_t *pNOR)
|
||
{
|
||
rt_uint16_t volt_mv = ((frame_data[4] << 8) | frame_data[5]) * 10;
|
||
static rt_uint8_t volt_low[TOTAL_SOU_CHS] = {0};
|
||
static rt_uint8_t no_curr[TOTAL_SOU_CHS] = {0};
|
||
static rt_uint8_t volt_set_ok[TOTAL_SOU_CHS] = {0};
|
||
|
||
if ((idx >= TOTAL_SOU_CHS) || (pSW == RT_NULL)) {
|
||
return;
|
||
}
|
||
|
||
pSW->VZ = ((frame_data[14] << 8) | frame_data[15]);
|
||
pSW->VF = ((frame_data[16] << 8) | frame_data[17]);
|
||
|
||
#if DEBUG_NORTH
|
||
// LOG_D("Sink[%d] real volt: %d mV", idx, volt_mv);
|
||
#endif
|
||
|
||
if(pSW->sink.On_work == 1 && pSW->sink.test.test_mode == 0) {
|
||
if (volt_mv <= WORK_MIN_VOLT) { // 电压过低,禁止进入工作状态机
|
||
if (volt_set_ok[idx] == 1) {
|
||
if (++volt_low[idx] >= 3) {
|
||
volt_set_ok[idx] = 0;
|
||
no_curr[idx] = 0;
|
||
pSW->sink.work_mode = SINK_WORK_LOW;
|
||
}
|
||
}
|
||
}
|
||
else{ // 电压稳定时执行工作状态机(继电器/工作模式切换)
|
||
volt_low[idx] = 0;
|
||
volt_set_ok[idx] = 1;
|
||
pSW->change_flag = 0;
|
||
if ((rt_uint32_t)volt_mv + 2000 >= pSW->sink.pro_loadv) {
|
||
pSW->sink.work_mode = SINK_WORK_RUNING;
|
||
if (pSW->Now_current <= 200) {
|
||
if (++no_curr[idx] >= 3) {
|
||
pSW->sink.work_mode = SINK_WORK_WAIT;
|
||
no_curr[idx] = 0;
|
||
}
|
||
}
|
||
else {
|
||
no_curr[idx] = 0;
|
||
}
|
||
} else {
|
||
no_curr[idx] = 0;
|
||
}
|
||
}
|
||
}
|
||
else{
|
||
volt_low[idx] = 0;
|
||
no_curr[idx] = 0;
|
||
volt_set_ok[idx] = 0;
|
||
}
|
||
chrg_sink_work(idx,pSW);
|
||
}
|
||
|
||
/*****************************************************************
|
||
函数名称: chrg_north_handle_Seting_Volt
|
||
函数描述: 根据当前通道角色分发21字节电压电流回码
|
||
输入参数: frame_data:完整北向回码 frame_len:回码长度
|
||
输出参数: 调用Source或Sink解析函数更新当前通道状态
|
||
返回说明: -
|
||
其它说明: 当前通道由chrgnorth.idx确定,角色由pSW->id确定
|
||
*****************************************************************/
|
||
rt_uint8_t change_number = 0;
|
||
static void chrg_north_handle_Seting_Volt(rt_uint8_t *frame_data, rt_uint16_t frame_len)
|
||
{
|
||
// 提取电压/电流值
|
||
struct chrg_north_t *pNOR = &chrgnorth;
|
||
rt_uint16_t idx = pNOR->idx;
|
||
struct chrg_switch_t *pSW = &chrgnorth.sw[idx];
|
||
|
||
static uint8_t number = 0;
|
||
//电源部分
|
||
if(pSW->id==ID_SOURCE){
|
||
chrg_source_parse(frame_data,(eIDX_SOU_CH)idx, pSW, pNOR);
|
||
}
|
||
else{
|
||
chrg_sink_parse(frame_data,(eIDX_SOU_CH)idx, pSW, pNOR);
|
||
}
|
||
}
|
||
|
||
struct chrg_north_t chrgnorth = {
|
||
.devname = DEV_NAME_NORTH, // 设备名
|
||
.tty = RT_NULL, // 串口句柄初始为空
|
||
.enable = {0, 0, 0, 0}, // 4个通道使能初始关闭
|
||
.free_flag = 1,
|
||
// 通道1(IDX_NOR_CH1)初始化
|
||
.sw[IDX_NOR_CH1] = {
|
||
.CV_mode = 2,
|
||
.change_flag = 0,
|
||
.show_step = 0,
|
||
.sub = IDX_GET_SINK_VC,
|
||
.id = ID_SINK, // 初始为负载模式
|
||
.source = { // 电源侧参数初始化
|
||
.On_Flag = 0, // 启动标志初始关闭
|
||
.Set_CV_Flag = 1, // 启停控制初始置1
|
||
.work_mode = SOURCE_WORK_WAIT, // 上电保持等待,禁止自动执行Source INIT
|
||
}, // 关键:补充逗号分隔source和sink
|
||
.sink = {
|
||
.protocol = 0xFE,
|
||
.work_mode= SINK_WORK_WAIT,
|
||
.test.test_mode_old = 0,
|
||
}
|
||
|
||
},
|
||
// 通道2(IDX_NOR_CH2)初始化
|
||
.sw[IDX_NOR_CH2] = {
|
||
.show_step = 0,
|
||
.id = ID_SINK,
|
||
.CV_mode = 2,
|
||
.change_flag = 0,
|
||
.source = {
|
||
.On_Flag = 0,
|
||
.Set_CV_Flag = 1,
|
||
.work_mode = SOURCE_WORK_WAIT, // 上电保持等待,禁止自动执行Source INIT
|
||
},
|
||
.sink = {
|
||
.protocol = 0xFE,
|
||
.work_mode= SINK_WORK_WAIT,
|
||
.test.test_mode_old = 0,
|
||
}
|
||
},
|
||
// 通道3(IDX_NOR_CH3)初始化
|
||
.sw[IDX_NOR_CH3] = {
|
||
.show_step = 0,
|
||
.CV_mode = 2,
|
||
.id = ID_SINK,
|
||
.change_flag = 0,
|
||
.source = {
|
||
.On_Flag = 0,
|
||
.Set_CV_Flag = 1,
|
||
.work_mode = SOURCE_WORK_WAIT, // 上电保持等待,禁止自动执行Source INIT
|
||
},
|
||
.sink = {
|
||
.protocol = 0xFE,
|
||
.work_mode= SINK_WORK_WAIT,
|
||
.test.test_mode_old = 0,
|
||
}
|
||
},
|
||
// 通道4(IDX_NOR_CH4)初始化
|
||
.sw[IDX_NOR_CH4] = {
|
||
.show_step = 0,
|
||
.CV_mode = 2,
|
||
.id = ID_SINK,
|
||
.change_flag = 0,
|
||
.source = {
|
||
.On_Flag = 0,
|
||
.Set_CV_Flag = 1,
|
||
.work_mode = SOURCE_WORK_WAIT, // 上电保持等待,禁止自动执行Source INIT
|
||
},
|
||
.sink = {
|
||
.protocol = 0xFE,
|
||
.work_mode= SINK_WORK_WAIT,
|
||
.test.test_mode_old = 0,
|
||
}
|
||
}
|
||
};
|
||
/*****************************************************************
|
||
函数名称: chrg_north_switch
|
||
函数描述: 北向通道选择
|
||
输入参数: ch: 北向通道
|
||
输出参数: -
|
||
返回说明: <0: 错误
|
||
其它说明: -
|
||
*****************************************************************/
|
||
int chrg_north_switch (eIDX_NOR_CH ch)
|
||
{
|
||
if (ch >= TOTAL_NOR_CHS) {
|
||
return -1;
|
||
}
|
||
|
||
return chrg_switch_ch(chrgios[IDX_GPO_NOR_SW_A].base,
|
||
chrgios[IDX_GPO_NOR_SW_B].base, (eIDX_CH)ch);
|
||
}
|
||
|
||
/*****************************************************************
|
||
函数名称: chrg_north_send
|
||
函数描述: 北向通道数据发送
|
||
输入参数: *data: 数据 length: 数据长度
|
||
输出参数: -
|
||
返回说明: <0: 错误 >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) {
|
||
#if DEBUG_NORTH //是否打印
|
||
LOG_HEX("sendN", 32, data, length);
|
||
#endif
|
||
ret = chrg_tty_send(pTTY, data, length);
|
||
}
|
||
|
||
return ret;
|
||
}
|
||
|
||
/*****************************************************************
|
||
函数名称: chrg_north_frame_head
|
||
函数描述: 北向通道数据帧头提取
|
||
输入参数: *rb: 缓冲区 *head: 帧头 id:编号
|
||
输出参数: -
|
||
返回说明: <0: 错误 >0:帧长度
|
||
其它说明: 提取3字节帧头
|
||
*****************************************************************/
|
||
static int chrg_north_frame_head (struct rt_ringbuffer *rb, rt_uint8_t *head, eIDX_ID id)
|
||
{
|
||
int i = 0;
|
||
rt_uint8_t data = 0, stage = 0;
|
||
rt_size_t ret = 0;
|
||
rt_size_t len_rb = 0; // ring数据长度
|
||
rt_uint8_t len_frame = 0; // 帧长度
|
||
|
||
if (RT_NULL == rb) {
|
||
rt_kprintf("len_rb=NULL%d\r\n",len_rb);
|
||
return -1;
|
||
}
|
||
|
||
len_rb = rt_ringbuffer_data_len(rb);
|
||
|
||
|
||
if (len_rb < LEN_FRAME_HEAD) {
|
||
return 0;
|
||
}
|
||
for (i = 0; i < len_rb - 2; i++) { // 找头三个字节 [帧长, 0x00, id]
|
||
ret = rt_ringbuffer_getchar(rb, &data);
|
||
if (0 == ret) {
|
||
continue;
|
||
}
|
||
|
||
switch (stage) {
|
||
case 0:
|
||
head[0] = data;
|
||
len_frame = data;
|
||
if (data < MIN_FRAME_LENGTH) { // 长度不符
|
||
|
||
stage = 0;
|
||
continue;
|
||
}
|
||
|
||
stage = 1;
|
||
break;
|
||
|
||
case 1:
|
||
head[1] = data;
|
||
if (0x00 != data) {
|
||
stage = 0;
|
||
} else {
|
||
stage = 2;
|
||
}
|
||
break;
|
||
|
||
case 2:
|
||
head[2] = data;
|
||
if ((rt_uint8_t)id != data) {
|
||
stage = 0;
|
||
} else {
|
||
// if (id == ID_SINK) {
|
||
// rt_uint8_t ext1, ext2;
|
||
// // 主动读取并丢弃2字节扩展字段
|
||
// rt_ringbuffer_getchar(rb, &ext1);
|
||
// rt_ringbuffer_getchar(rb, &ext2);
|
||
// rt_kprintf("Sink:%02X %02X\r\n", ext1, ext2);
|
||
// }
|
||
return len_frame;
|
||
}
|
||
break;
|
||
|
||
default:
|
||
|
||
break;
|
||
}
|
||
|
||
}
|
||
|
||
return -1;
|
||
}
|
||
|
||
/*****************************************************************
|
||
函数名称: chrg_north_pickup_frame
|
||
函数描述: 北向通道数据帧提取
|
||
输入参数: *rb: 缓冲区 *pFrame: 数据帧 idx:通道
|
||
输出参数: -
|
||
返回说明: <0: 错误 >0:帧长度
|
||
其它说明: -
|
||
*****************************************************************/
|
||
int a_number;
|
||
|
||
#define NORTH_FRAME_TIMEOUT_MS 100
|
||
|
||
/*****************************************************************
|
||
函数名称: chrg_north_frame_reset
|
||
函数描述: 复位北向帧接收状态并按需释放未完成帧内存
|
||
输入参数: pFrame:帧接收上下文 release:是否释放pMB及payload
|
||
输出参数: 清零接收长度、帧头标志、超时计时和消息指针
|
||
返回说明: -
|
||
其它说明: 完整帧已移交邮箱时release应为RT_FALSE,异常中止时为RT_TRUE
|
||
*****************************************************************/
|
||
static void chrg_north_frame_reset(struct pickup_info_t *pFrame, rt_bool_t release)
|
||
{
|
||
if (RT_NULL == pFrame) {
|
||
return;
|
||
}
|
||
|
||
if (release && (RT_NULL != pFrame->pMB)) {
|
||
if (RT_NULL != pFrame->pMB->payload) {
|
||
rt_free(pFrame->pMB->payload);
|
||
}
|
||
rt_free(pFrame->pMB);
|
||
}
|
||
|
||
pFrame->last_tick = 0;
|
||
pFrame->length = 0;
|
||
pFrame->findhead = 0;
|
||
pFrame->len_msg = 0;
|
||
pFrame->pMB = RT_NULL;
|
||
}
|
||
|
||
/*****************************************************************
|
||
函数名称: chrg_north_frame_append
|
||
函数描述: 从北向环形缓冲区追加数据到当前未完成帧
|
||
输入参数: rb:北向接收环形缓冲区 pFrame:帧接收上下文
|
||
输出参数: 更新已接收长度和最后接收时间
|
||
返回说明: 1:完整帧接收完成 0:仍需等待数据 <0:参数或状态异常
|
||
其它说明: 单次仅复制当前帧剩余长度,避免读取下一帧数据
|
||
*****************************************************************/
|
||
static int chrg_north_frame_append(struct rt_ringbuffer *rb,
|
||
struct pickup_info_t *pFrame)
|
||
{
|
||
rt_size_t remaining;
|
||
rt_size_t copied;
|
||
|
||
if ((RT_NULL == rb)||(RT_NULL == pFrame)||(RT_NULL == pFrame->pMB)||
|
||
(RT_NULL == pFrame->pMB->payload)||(pFrame->len_msg <= 0)||
|
||
(pFrame->length > (rt_size_t)pFrame->len_msg)) {
|
||
return -1;
|
||
}
|
||
|
||
remaining = (rt_size_t)pFrame->len_msg - pFrame->length;
|
||
if (0 == remaining) {
|
||
return 1;
|
||
}
|
||
|
||
copied = rt_ringbuffer_data_len(rb);
|
||
if (copied > remaining) {
|
||
copied = remaining;
|
||
}
|
||
if (copied > 0) {
|
||
copied = rt_ringbuffer_get(rb,
|
||
&pFrame->pMB->payload[pFrame->length], copied);
|
||
pFrame->length += copied;
|
||
pFrame->last_tick = rt_tick_get();
|
||
}
|
||
|
||
return (pFrame->length == (rt_size_t)pFrame->len_msg) ? 1 : 0;
|
||
}
|
||
|
||
/*****************************************************************
|
||
函数名称: chrg_north_frame_dispatch
|
||
函数描述: 分发已经组装完成的北向数据帧
|
||
输入参数: pFrame:包含完整pMB消息的帧接收上下文
|
||
输出参数: 21字节回码进入电压解析,同时完整消息发送到北向轮询邮箱
|
||
返回说明: -
|
||
其它说明: 消息所有权移交邮箱后只复位上下文,不在此处释放pMB
|
||
*****************************************************************/
|
||
static void chrg_north_frame_dispatch(struct pickup_info_t *pFrame)
|
||
{
|
||
struct mb_msg_t *pMB = pFrame->pMB;
|
||
|
||
if (pMB->length == 21) {
|
||
chrg_north_handle_Seting_Volt(pMB->payload, pMB->length);
|
||
}
|
||
|
||
chrg_north_frame_reset(pFrame, RT_FALSE);
|
||
chrg_thread_mb_send(IDX_THR_NORTH, pMB);
|
||
}
|
||
|
||
static int chrg_north_pickup_frame (struct rt_ringbuffer *rb,
|
||
struct pickup_info_t *pFrame, eIDX_NOR_CH idx)
|
||
{
|
||
int cnt = 0;
|
||
int status = 0;
|
||
rt_size_t len_rb = 0;
|
||
if ((RT_NULL == rb)||(RT_NULL == pFrame)) {
|
||
return -1;
|
||
}
|
||
|
||
struct chrg_switch_t *pSW = &chrgnorth.sw[idx];
|
||
//rt_kprintf("north_idx=%d\r\n",idx);
|
||
do {
|
||
if (0 == pFrame->findhead) {
|
||
//pFrame->len_msg = chrg_north_frame_head(pSW->id,rb, pFrame->head, pSW->id);
|
||
pFrame->len_msg = chrg_north_frame_head(rb, pFrame->head, pSW->id);
|
||
// rt_kprintf("msg=:%d",pFrame->len_msg);
|
||
if (pFrame->len_msg > 0) {
|
||
if ((pFrame->len_msg < LEN_FRAME_HEAD)||
|
||
(pFrame->len_msg >= SIZE_BUF_TTY)) {
|
||
LOG_E("invalid msg length: %d", pFrame->len_msg);
|
||
chrg_north_frame_reset(pFrame, RT_TRUE);
|
||
return -3;
|
||
}
|
||
|
||
pFrame->pMB = (struct mb_msg_t *)rt_malloc(sizeof(struct mb_msg_t));
|
||
if (RT_NULL == pFrame->pMB) {
|
||
LOG_E("malloc mb failed.");
|
||
chrg_north_frame_reset(pFrame, RT_FALSE);
|
||
return -2;
|
||
}
|
||
pFrame->pMB->length = pFrame->len_msg;
|
||
pFrame->pMB->payload = RT_NULL;
|
||
pFrame->pMB->payload = (rt_uint8_t *)rt_malloc(pFrame->len_msg);
|
||
if (RT_NULL == pFrame->pMB->payload) {
|
||
LOG_E("malloc buf failed.");
|
||
chrg_north_frame_reset(pFrame, RT_TRUE);
|
||
return -4;
|
||
}
|
||
rt_memcpy(pFrame->pMB->payload, pFrame->head, LEN_FRAME_HEAD);
|
||
pFrame->length = LEN_FRAME_HEAD;
|
||
pFrame->findhead = 1;
|
||
pFrame->last_tick = rt_tick_get();
|
||
|
||
status = chrg_north_frame_append(rb, pFrame);
|
||
if (status > 0) {
|
||
chrg_north_frame_dispatch(pFrame);
|
||
cnt++;
|
||
} else if (status < 0) {
|
||
chrg_north_frame_reset(pFrame, RT_TRUE);
|
||
return -5;
|
||
} else {
|
||
break;
|
||
}
|
||
}
|
||
} else {
|
||
status = chrg_north_frame_append(rb, pFrame);
|
||
if (status > 0) {
|
||
chrg_north_frame_dispatch(pFrame);
|
||
cnt++;
|
||
} else if (status < 0) {
|
||
chrg_north_frame_reset(pFrame, RT_TRUE);
|
||
return -5;
|
||
} else {
|
||
break;
|
||
}
|
||
}
|
||
len_rb = rt_ringbuffer_data_len(rb);
|
||
|
||
} while (len_rb >= LEN_FRAME_HEAD);
|
||
|
||
return cnt;
|
||
}
|
||
|
||
/*****************************************************************
|
||
函数名称: chrg_north_thread_entry
|
||
函数描述: 北向通道数据接收线程体
|
||
输入参数: *data: 本线程信息
|
||
输出参数: -
|
||
返回说明: -
|
||
其它说明: -
|
||
*****************************************************************/
|
||
void chrg_north_thread_entry (void *data)
|
||
{
|
||
struct chrg_thread_t *pTHR = (struct chrg_thread_t *)data;
|
||
|
||
if (RT_NULL == pTHR) {
|
||
return ;
|
||
}
|
||
|
||
int len = 0;
|
||
rt_size_t put_len = 0;
|
||
struct chrg_tty_t *pTTY = RT_NULL;
|
||
struct chrg_north_t *pNOR = &chrgnorth;
|
||
//北向协议板线程体通道初始化为CH1,
|
||
chrg_north_switch(IDX_NOR_CH1);
|
||
|
||
pTTY = chrg_tty_create(pNOR->devname, 230400, 0, -1, 1);
|
||
if (RT_NULL == pTTY) {
|
||
LOG_E("tty can't create.");
|
||
return ;
|
||
}
|
||
|
||
chrg_tty_set_recv_tmo(pTTY, 10);
|
||
if (chrg_tty_connect(pTTY) != RT_EOK) {
|
||
chrg_tty_destory(pTTY);
|
||
return ;
|
||
}
|
||
|
||
pNOR->tty = pTTY;
|
||
|
||
pNOR->rb = rt_ringbuffer_create(SIZE_BUF_TTY*2);
|
||
if (RT_NULL == pNOR->rb) {
|
||
LOG_E("create rb '%s' failed.", pNOR->devname);
|
||
return ;
|
||
}
|
||
|
||
while (1) {
|
||
if (pNOR->frame.findhead &&
|
||
((rt_tick_t)(rt_tick_get() - pNOR->frame.last_tick) >=
|
||
rt_tick_from_millisecond(NORTH_FRAME_TIMEOUT_MS))) {
|
||
chrg_north_frame_reset(&pNOR->frame, RT_TRUE);
|
||
LOG_W("partial frame timeout");
|
||
}
|
||
|
||
rt_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;
|
||
}
|
||
|
||
put_len = rt_ringbuffer_put(pNOR->rb, pNOR->rx_buf, len);
|
||
if (put_len != (rt_size_t)len) {
|
||
LOG_E("north ringbuffer overflow: recv=%d put=%d", len, put_len);
|
||
rt_ringbuffer_reset(pNOR->rb);
|
||
chrg_north_frame_reset(&pNOR->frame, RT_TRUE);
|
||
continue;
|
||
}
|
||
#if DEBUG_NORTH
|
||
LOG_HEX("recvN", 32, pNOR->rx_buf, len);
|
||
#endif
|
||
chrg_north_pickup_frame(pNOR->rb, &pNOR->frame, pNOR->idx);
|
||
}
|
||
}
|
||
|
||
|
||
#if 1
|
||
static int north_ch(rt_uint8_t argc, char **argv)
|
||
{
|
||
int ret = 0;
|
||
|
||
if (2 != argc) {
|
||
rt_kprintf("help : %s [0-3]\r\n", argv[0]);
|
||
return -1;
|
||
}
|
||
|
||
ret = chrg_north_switch((eIDX_NOR_CH)atoi(argv[1]));
|
||
|
||
return ret;
|
||
}
|
||
|
||
|
||
MSH_CMD_EXPORT(north_ch, north channel select);
|
||
|
||
static int north_diag(int argc, char **argv)
|
||
{
|
||
rt_uint8_t i;
|
||
struct chrg_north_t *pNOR = &chrgnorth;
|
||
struct chrg_rollnor_t *pROLL = &chrgrollnor;
|
||
|
||
rt_kprintf("roll: run=%d idx=%d ch=%d type=%d sub=%d\r\n",
|
||
pROLL->run, pROLL->idx, pROLL->ch, pROLL->type, pROLL->sub);
|
||
rt_kprintf("rx: rb_len=%d findhead=%d frame_len=%d received=%d\r\n",
|
||
pNOR->rb ? rt_ringbuffer_data_len(pNOR->rb) : 0,
|
||
pNOR->frame.findhead, pNOR->frame.len_msg, pNOR->frame.length);
|
||
for (i = 0; i < TOTAL_NOR_CHS; i++) {
|
||
rt_kprintf("ch%d: enable=%d id=%d sub=%d\r\n", i,
|
||
pNOR->enable[i], pNOR->sw[i].id, pNOR->sw[i].sub);
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
MSH_CMD_EXPORT(north_diag, show north polling and frame state);
|
||
|
||
#endif
|
||
|