新增 docs/电源流程.md,记录电源各阶段指令流向 更新多个源文件的版本号从 V1.1.24 到对应版本 修改 chrg_rel.c 注释补充继电器包含类型 修改 chrg_source.c、chrg_lcd.c 中部分子命令定义
269 lines
10 KiB
C
269 lines
10 KiB
C
/****************************************************************************
|
||
文件名称 : chrg_source.c
|
||
完成日期 :
|
||
主要功能 : 北向数据source请求数据填充及发送,命令字定义。
|
||
版本历史 : 创建原始版本
|
||
说明 :
|
||
******************************************************************************/
|
||
|
||
#ifndef __CHRG_SOURCE_H__
|
||
#define __CHRG_SOURCE_H__
|
||
|
||
#include <rtthread.h>
|
||
#include "chrg_north.h"
|
||
#include "chrg_def.h"
|
||
struct chrg_switch_t;
|
||
// 冠达快充板(SOURCE)通讯协议
|
||
// SOURCE的ID为2, 波特率200K
|
||
|
||
#define SRC_CMD_GET_VOLTAGE 0x20 // 读取数据
|
||
|
||
#define SRC_CMD_GET_PROTOCOL 0x13 // 读取source的PD协议数据(当前为PDO,PPS,PD3.1,AVS协议时有效)
|
||
|
||
#define SRC_CMD_SET_CC_LEVEL 0x16
|
||
|
||
#define SRC_CMD_SET_CC_CHOOSE 0x17
|
||
|
||
#define SRC_CMD_RESET 0x18
|
||
|
||
#define SRC_CMD_SET_PROTOCOL 0xD0
|
||
|
||
#define PROTOCOL_NONE 0x00 // 无快充
|
||
#define PROTOCOL_VIFC 0x01 // VIFC(暂不支持)
|
||
#define PROTOCOL_FCP 0x02 // FCP
|
||
#define PROTOCOL_SCPB 0x03 // SCPB
|
||
#define PROTOCOL_RESERVED_04 0x04 // 预留
|
||
#define PROTOCOL_RESERVED_05 0x05 // 预留
|
||
#define PROTOCOL_RFC 0x06 // RFC(暂不支持)
|
||
#define PROTOCOL_QC30 0x07 // QC3.0
|
||
#define PROTOCOL_QC20 0x08 // QC2.0
|
||
#define PROTOCOL_AFC 0x09 // AFC
|
||
#define PROTOCOL_PD 0x0A // PD
|
||
#define PROTOCOL_RESERVED_0B 0x0B // 预留
|
||
#define PROTOCOL_UFCS 0x0C // UFCS
|
||
#define PROTOCOL_RESERVED_0D 0x0D // 预留
|
||
#define PROTOCOL_VOOC 0x0E // VOOC(暂不支持)
|
||
#define PROTOCOL_SCPA 0x0F // SCPA
|
||
|
||
typedef enum {
|
||
PRO_NONE = 0x00, // 标准
|
||
PRO_VIFC = 0x01, // VIFC(暂不支持)
|
||
PRO_FCP = 0x02, // FCP
|
||
PRO_SCPB = 0x03, // SCP
|
||
PRO_SERV1 = 0x04, // 预留
|
||
PRO_SERV2 = 0x05, // 预留
|
||
PRO_RFC = 0x06, // RFC(暂不支持)
|
||
PRO_QC30 = 0x07, // QC3.0
|
||
PRO_QC20 = 0x08, // QC2.0
|
||
PRO_AFC = 0x09, // AFC
|
||
PRO_PD0PSS = 0x0A, // PD0/PPS
|
||
PRO_SERV3 = 0x0B, // 预留
|
||
PRO_UFCS = 0x0C, // UFCS
|
||
PRO_PD31AVS = 0x0D, // PD3.1/AVS
|
||
PRO_VOOC = 0x0E, // VOOC(暂不支持)
|
||
PRO_SCPA = 0x0F, // SCPA
|
||
TOTAL_PROS
|
||
} eSRC_PRO;
|
||
|
||
// 电源协议映射表:枚举值 ↔ 协议名称(和你的 eSRC_PRO 完全对齐)
|
||
typedef struct {
|
||
rt_uint8_t pro_val; // 协议数值(对应 eSRC_PRO 枚举)
|
||
int pro_name; // 协议显示名称(LCD展示用)
|
||
} SrcProMap_t;
|
||
|
||
// 按 eSRC_PRO 枚举顺序定义映射表,确保一一对应
|
||
static const SrcProMap_t src_pro_map[] = {
|
||
{PRO_NONE, 30},
|
||
{PRO_VIFC, 31},
|
||
{PRO_FCP, 32},
|
||
{PRO_SCPB, 33},
|
||
{PRO_SERV1, 34},
|
||
{PRO_SERV2, 35},
|
||
{PRO_RFC, 36},
|
||
{PRO_QC30, 37},
|
||
{PRO_QC20, 38},
|
||
{PRO_AFC, 39},
|
||
{PRO_PD0PSS, 40},
|
||
{PRO_SERV3, 41},
|
||
{PRO_UFCS, 42},
|
||
{PRO_PD31AVS, 43},
|
||
{PRO_VOOC, 44},
|
||
{PRO_SCPA, 45}
|
||
};
|
||
// 映射表长度宏(自动计算,避免手动维护)
|
||
#define SRC_PRO_MAP_NUM (sizeof(src_pro_map) / sizeof(src_pro_map[0]))
|
||
|
||
// 应答帧头
|
||
struct pkg_head_t {
|
||
rt_uint8_t length; // 帧长度
|
||
rt_uint8_t blank; // 固定 0x00
|
||
rt_uint8_t id; // ID 号
|
||
rt_uint8_t cmd; // 指令
|
||
};
|
||
|
||
// 读取变电压板电压 应答
|
||
struct get_vol_ack_t {
|
||
struct pkg_head_t head;
|
||
rt_uint16_t voltage; // 0.01V
|
||
rt_uint8_t qc_data[6]; // 快充指令
|
||
rt_uint16_t version; // 版本
|
||
rt_uint8_t reserve[6]; // 保留
|
||
rt_uint8_t crc8;
|
||
};
|
||
|
||
// 读取源端的PD协议数据 应答
|
||
struct get_pd_pro_ack_t {
|
||
struct pkg_head_t head;
|
||
rt_uint8_t type; // 源类型 0:PD0 3:APD0
|
||
rt_uint8_t pps; // PPS类型 0:SPR PPS 1:EPR AVS 2:SPR AVS
|
||
rt_uint16_t vmin; // 最小电压 mV
|
||
rt_uint16_t vmax; // 最大电压 mV
|
||
rt_uint16_t curpwr; // 最大电流/功率 mA/W
|
||
rt_uint8_t reserve[6]; // 保留 当pps=2时,vmin,vmax,curpwr
|
||
rt_uint8_t crc8;
|
||
};
|
||
|
||
// 设置CC线 应答
|
||
struct set_cc_ack_t {
|
||
struct pkg_head_t head;
|
||
rt_uint8_t crc8;
|
||
};
|
||
|
||
// 设置快充协议 应答
|
||
struct set_qc_ack_t {
|
||
struct pkg_head_t head;
|
||
rt_uint8_t crc8;
|
||
};
|
||
|
||
|
||
struct src_vc_t {
|
||
rt_uint16_t voltage; // 读取 0.01V
|
||
rt_uint16_t version; // 版本号
|
||
};
|
||
|
||
#define PD_GEAR_ARRAY_LEN 4 // 非5V挡位最多4个,数组长度设为4
|
||
// source 参数结构
|
||
struct chrg_src_t {
|
||
eSRC_PRO protocol; // 协议类型
|
||
rt_uint8_t vol_lvl; // CC线电平
|
||
rt_uint8_t CC_Set; // CC线选择:0=CC1+CC2,1=CC1,2=CC2
|
||
rt_uint8_t Now_State; // 当前状态
|
||
rt_uint8_t On_Flag; // 启动总标志(0/1)
|
||
rt_uint8_t LCD_Set_Pro_Flag; // LCD设置协议标志位
|
||
rt_uint8_t change_sink_Flag; // 从source切换到sink标志位
|
||
rt_uint8_t work_mode;
|
||
rt_uint8_t qc_err; // Source板0x20回码错误码,bit2表示协议/CC切换失败
|
||
rt_uint16_t set_volt; // 北向解析后准备写入功率板的目标电压(mV)
|
||
rt_uint16_t set_curr; // 北向解析后准备写入功率板的目标电流(mA)
|
||
struct src_vc_t vc; // 北向回读数据
|
||
struct vc_pd_t pd_get[2]; // 读取PD协议数据
|
||
struct vc_pd_t pd_set; // COM设置的协议挡位、类型和电压电流范围
|
||
};
|
||
// 电源工作状态机:COM只切换状态,具体功率板指令由状态处理函数下发
|
||
enum {
|
||
SOURCE_WORK_INIT = 0, // 初始化功率板并启动输出
|
||
SOURCE_WORK_WAIT, // 参数未变化,不下发功率板指令
|
||
SOURCE_WORK_RUNING, // 协商电压变化,下发一次新电压
|
||
SOURCE_WORK_LOW, // 电压过低,停止功率板输出
|
||
SOURCE_WORK_STOP, // 主动停止并关闭通道
|
||
SOURCE_WORK_SWITCH, // 停止旧负载后进入电源INIT
|
||
SOURCE_WORK_PROTOCOL_SWITCH, // 协议/CC切换前清零功率板输出
|
||
SOURCE_WORK_FULL_RESET, // Submit output clear and stop commands
|
||
SOURCE_WORK_FULL_RESET_WAIT, // Wait for output clear and stop
|
||
SOURCE_WORK_FULL_RESET_RELAY, // Submit south relay-off command
|
||
SOURCE_WORK_FULL_RESET_RELAY_WAIT,// Wait before disconnecting north relay
|
||
SOURCE_WORK_INIT_WAIT, // Wait for Source init batch completion
|
||
SOURCE_WORK_ON_TEST_DYNA,
|
||
SOURCE_WORK_ON_TEST_SHORT,
|
||
};
|
||
|
||
|
||
/*****************************************************************
|
||
函数名称: source_get_vc
|
||
函数描述: source电压电流查询请求
|
||
输入参数: vol:电压 cur:电流
|
||
输出参数: -
|
||
返回说明: <0: 错误 >0:发送长度
|
||
其它说明: -
|
||
*****************************************************************/
|
||
extern int source_get_vc (rt_uint16_t vol, rt_uint16_t cur);
|
||
|
||
/*****************************************************************
|
||
函数名称: source_get_pd
|
||
函数描述: source pd 协议查询请求
|
||
输入参数: -
|
||
输出参数: -
|
||
返回说明: <0: 错误 >0:发送长度
|
||
其它说明: -
|
||
*****************************************************************/
|
||
extern int source_get_pd (void);
|
||
|
||
extern int source_reset (void);
|
||
|
||
/*****************************************************************
|
||
函数名称: source_set_cc_lvl
|
||
函数描述: source 设置 cc 等级
|
||
输入参数: lvl:等级
|
||
输出参数: -
|
||
返回说明: <0: 错误 >0:发送长度
|
||
其它说明: -
|
||
*****************************************************************/
|
||
extern int source_set_cc_lvl (rt_uint8_t lvl);
|
||
|
||
/*****************************************************************
|
||
函数名称: source_set_cc_choose
|
||
函数描述: source 设置 cc线 选择
|
||
输入参数: cc_set 0代表1+2,1代表cc1,2代表cc2
|
||
输出参数: -
|
||
返回说明: <0: 错误 >0:发送长度
|
||
其它说明: -
|
||
*****************************************************************/
|
||
extern int source_set_cc_choose (rt_uint8_t cc_set);
|
||
|
||
/*****************************************************************
|
||
函数名称: source_set_pd
|
||
函数描述: source 设置PD协议
|
||
输入参数: pro:协议 src:源 pps: PPS vmax:最大电压 vmin:最小电压 max_cur:最大电流
|
||
输出参数: -
|
||
返回说明: <0: 错误 >0:发送长度
|
||
其它说明: -
|
||
*****************************************************************/
|
||
extern int source_set_pd (rt_uint8_t pro, rt_uint8_t src, rt_uint8_t pps,
|
||
rt_uint16_t vmax, rt_uint16_t vmin, rt_int16_t max_cur);
|
||
|
||
// 挡位参数结构体
|
||
typedef struct {
|
||
rt_uint8_t src; // 第1字段(如00/03)
|
||
rt_uint8_t pps; // 第2字段(如00/01)
|
||
rt_uint16_t v_min; // 电压最小值(如0x1388=5V)
|
||
rt_uint16_t v_max; // 电压最大值(如0x1388=5V)
|
||
rt_uint16_t cur_set; // 电流设置值(如0x0bb8=3A)
|
||
} Pro_Group;
|
||
|
||
// 挡位映射表(索引对应挡位:0=5V,1=9V,2=12V,3=20V,4=PPS)
|
||
static const Pro_Group pd_voltage_map[] = {
|
||
{0x00, 0x00, 0x1388, 0x1388, 0x0bb8}, // 0:5V挡位
|
||
{0x00, 0x00, 0x2328, 0x2328, 0x0bb8}, // 1:9V挡位
|
||
{0x00, 0x00, 0x2ee0, 0x2ee0, 0x0bb8}, // 2:12V挡位
|
||
{0x00, 0x00, 0x4e20, 0x4e20, 0x0bb8}, // 3:20V挡位
|
||
{0x00, 0x00, 0xBB80, 0xBB80, 0X0bb8} // 4:48V
|
||
// {0x03, 0x00, 0x1388, 0x4e20, 0x1388} // 4:PPS挡位 5-20V
|
||
};
|
||
extern rt_uint8_t pd_fill_gear_index(rt_uint8_t *gear_index, rt_uint8_t *gear_cnt, rt_uint8_t hex_code);
|
||
extern int source_set_pd_com(rt_uint8_t pro,rt_uint8_t pro_gear_idx);
|
||
#define PD_VOLTAGE_CNT (sizeof(pd_voltage_map) / sizeof(Pro_Group))
|
||
extern rt_uint16_t pd_gear_parse(rt_uint8_t hex_code);
|
||
|
||
extern void parse_power_single_gear(rt_uint8_t protoCmd, rt_uint8_t *pData, rt_uint16_t reqLen,uint8_t *pro_gear_idx,uint8_t *pro,rt_uint16_t *protect_ma);
|
||
extern void parse_power_pdo_gear(rt_uint8_t protoCmd, rt_uint8_t *pData, rt_uint16_t reqLen,uint8_t *pro_gear_idx,uint8_t *pro,rt_uint16_t *protect_ma);
|
||
extern void parse_multi_gear_more(rt_uint8_t protoCmd, rt_uint8_t *pData, rt_uint16_t reqLen,uint8_t *pro_gear_idx,uint8_t *pro,rt_uint16_t *protect_ma);
|
||
extern void parse_power_ufcs(rt_uint8_t *pData, rt_uint16_t reqLen, uint8_t *pro_gear_idx, uint8_t *pro,rt_uint16_t *protect_ma);
|
||
extern int source_set_pro(rt_uint8_t pro, struct chrg_switch_t *pSW);
|
||
|
||
/* 提交 Source 功率板初始化参数;北向协议设置完成后调用。 */
|
||
extern int chrg_source_init_submit(rt_uint8_t idx, struct chrg_switch_t *pSW);
|
||
extern int chrg_source_work_state(rt_uint8_t idx, struct chrg_switch_t *pSW);
|
||
#endif
|
||
|
||
|