111 lines
2.9 KiB
C
111 lines
2.9 KiB
C
/*
|
|
*文件名称:supercap.c
|
|
*功能描述:超级电容控制板CAN1通讯驱动(TX 0x001 / RX 0x100)
|
|
*备注:周期发送输入功率上限与使能位,解析超电就绪状态与剩余能量百分比
|
|
*/
|
|
#include "supercap.h"
|
|
#include "bsp_can.h"
|
|
#include "can.h"
|
|
#include "main.h"
|
|
#include <string.h>
|
|
|
|
static supercap_fdb_t supercap_fdb;//超电反馈与通讯诊断状态
|
|
static uint32_t last_tx_tick = 0U; //上次发送时间戳(ms)
|
|
|
|
/*
|
|
*函数简介:CAN1 0x100超级电容反馈帧接收回调
|
|
*参数说明:std_id:CAN标准帧ID
|
|
*参数说明:rx_data:8字节接收数据
|
|
*参数说明:dlc:有效数据长度
|
|
*返回类型:无
|
|
*备注:在CAN接收中断上下文调用
|
|
*/
|
|
static void supercap_rx_callback(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
|
{
|
|
(void)std_id;
|
|
if (rx_data == NULL)
|
|
return;
|
|
|
|
supercap_fdb.last_tick = HAL_GetTick();
|
|
supercap_fdb.rx_cnt++;
|
|
|
|
if (dlc >= 3U)
|
|
{
|
|
supercap_fdb.ready = rx_data[0];
|
|
supercap_fdb.cap_state = rx_data[1];
|
|
supercap_fdb.energy = rx_data[2];
|
|
}
|
|
}
|
|
|
|
/*
|
|
*函数简介:初始化超级电容模块并注册CAN1 0x100接收回调
|
|
*参数说明:无
|
|
*返回类型:无
|
|
*备注:必须在can_start()之后由CAN_Task调用
|
|
*/
|
|
void supercap_init(void)
|
|
{
|
|
memset(&supercap_fdb, 0, sizeof(supercap_fdb));
|
|
last_tx_tick = 0U;
|
|
can_rx_register(&hcan1, SUPERCAP_RX_ID, supercap_rx_callback);
|
|
}
|
|
|
|
/*
|
|
*函数简介:按100Hz周期向超级电容发送使能与输入功率上限指令(0x001)
|
|
*参数说明:enable:1=使能超电,0=关闭超电
|
|
*参数说明:power_limit_w:当前裁判系统允许输入功率上限(W)
|
|
*返回类型:无
|
|
*备注:内部按SUPERCAP_TX_PERIOD_MS分频发送
|
|
*/
|
|
void supercap_update_tx(uint8_t enable, uint8_t power_limit_w)
|
|
{
|
|
uint32_t now = HAL_GetTick();
|
|
if (now - last_tx_tick < SUPERCAP_TX_PERIOD_MS)
|
|
return;
|
|
last_tx_tick = now;
|
|
|
|
CanCtrlData_s pkt = {0};
|
|
pkt.hcan = &hcan1;
|
|
pkt.tx_header.StdId = SUPERCAP_TX_ID;
|
|
pkt.tx_header.IDE = CAN_ID_STD;
|
|
pkt.tx_header.RTR = CAN_RTR_DATA;
|
|
pkt.tx_header.DLC = SUPERCAP_DLC;
|
|
pkt.tx_data[0] = enable ? 1U : 0U;
|
|
pkt.tx_data[2] = power_limit_w;
|
|
|
|
if (HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) > 0U)
|
|
{
|
|
CAN_SendTxMessage(&pkt);
|
|
supercap_fdb.tx_cnt++;
|
|
}
|
|
else
|
|
{
|
|
supercap_fdb.tx_err++;
|
|
}
|
|
}
|
|
|
|
/*
|
|
*函数简介:查询超级电容是否在线
|
|
*参数说明:无
|
|
*返回类型:uint8_t:1=在线,0=离线或超时
|
|
*备注:无
|
|
*/
|
|
uint8_t supercap_is_online(void)
|
|
{
|
|
uint32_t tick = supercap_fdb.last_tick;
|
|
if (tick == 0U)
|
|
return 0U;
|
|
return (uint8_t)((HAL_GetTick() - tick) < SUPERCAP_OFFLINE_TIME_MS);
|
|
}
|
|
|
|
/*
|
|
*函数简介:获取超级电容最新反馈状态只读指针
|
|
*参数说明:无
|
|
*返回类型:const supercap_fdb_t *:反馈结构体指针
|
|
*备注:无
|
|
*/
|
|
const supercap_fdb_t * supercap_get_fdb(void)
|
|
{
|
|
return &supercap_fdb;
|
|
}
|