第一次提交
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* @Descripttion:
|
||||
* @version:
|
||||
* @Author: Chenfu
|
||||
* @Date: 2022-12-02 21:32:47
|
||||
* @LastEditTime: 2022-12-05 15:29:49
|
||||
*/
|
||||
#include "super_cap.h"
|
||||
#include "memory.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
static SuperCapInstance *super_cap_instance = NULL; // 可以由app保存此指针
|
||||
|
||||
static void SuperCapRxCallback(CANInstance *_instance)
|
||||
{
|
||||
uint8_t *rxbuff;
|
||||
SuperCap_Msg_s *Msg;
|
||||
rxbuff = _instance->rx_buff;
|
||||
Msg = &super_cap_instance->cap_msg;
|
||||
Msg->vol = (uint16_t)(rxbuff[0] << 8 | rxbuff[1]);
|
||||
Msg->current = (uint16_t)(rxbuff[2] << 8 | rxbuff[3]);
|
||||
Msg->power = (uint16_t)(rxbuff[4] << 8 | rxbuff[5]);
|
||||
}
|
||||
|
||||
SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s *supercap_config)
|
||||
{
|
||||
super_cap_instance = (SuperCapInstance *)malloc(sizeof(SuperCapInstance));
|
||||
memset(super_cap_instance, 0, sizeof(SuperCapInstance));
|
||||
|
||||
supercap_config->can_config.can_module_callback = SuperCapRxCallback;
|
||||
super_cap_instance->can_ins = CANRegister(&supercap_config->can_config);
|
||||
return super_cap_instance;
|
||||
}
|
||||
|
||||
void SuperCapSend(SuperCapInstance *instance, uint8_t *data)
|
||||
{
|
||||
memcpy(instance->can_ins->tx_buff, data, 8);
|
||||
CANTransmit(instance->can_ins,1);
|
||||
}
|
||||
|
||||
SuperCap_Msg_s SuperCapGet(SuperCapInstance *instance)
|
||||
{
|
||||
return instance->cap_msg;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* @Descripttion:
|
||||
* @version:
|
||||
* @Author: Chenfu
|
||||
* @Date: 2022-12-02 21:32:47
|
||||
* @LastEditTime: 2022-12-05 15:25:46
|
||||
*/
|
||||
#ifndef SUPER_CAP_H
|
||||
#define SUPER_CAP_H
|
||||
|
||||
#include "bsp_can.h"
|
||||
|
||||
#pragma pack(1)
|
||||
typedef struct
|
||||
{
|
||||
uint16_t vol; // 电压
|
||||
uint16_t current; // 电流
|
||||
uint16_t power; // 功率
|
||||
} SuperCap_Msg_s;
|
||||
#pragma pack()
|
||||
|
||||
/* 超级电容实例 */
|
||||
typedef struct
|
||||
{
|
||||
CANInstance *can_ins; // CAN实例
|
||||
SuperCap_Msg_s cap_msg; // 超级电容信息
|
||||
} SuperCapInstance;
|
||||
|
||||
/* 超级电容初始化配置 */
|
||||
typedef struct
|
||||
{
|
||||
CAN_Init_Config_s can_config;
|
||||
} SuperCap_Init_Config_s;
|
||||
|
||||
/**
|
||||
* @brief 初始化超级电容
|
||||
*
|
||||
* @param supercap_config 超级电容初始化配置
|
||||
* @return SuperCapInstance* 超级电容实例指针
|
||||
*/
|
||||
SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s *supercap_config);
|
||||
|
||||
/**
|
||||
* @brief 发送超级电容控制信息
|
||||
*
|
||||
* @param instance 超级电容实例
|
||||
* @param data 超级电容控制信息
|
||||
*/
|
||||
void SuperCapSend(SuperCapInstance *instance, uint8_t *data);
|
||||
|
||||
#endif // !SUPER_CAP_Hd
|
||||
@@ -0,0 +1,54 @@
|
||||
<!--
|
||||
* @Descripttion:
|
||||
* @version:
|
||||
* @Author: Chenfu
|
||||
* @Date: 2022-12-02 21:32:47
|
||||
* @LastEditTime: 2022-12-05 15:27:57
|
||||
-->
|
||||
# super_can
|
||||
|
||||
## 代码结构
|
||||
|
||||
.h中放置的是数据定义和外部接口,以及协议的定义和宏,.c中包含一些私有函数。
|
||||
|
||||
## 外部接口
|
||||
|
||||
```c
|
||||
SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s* supercap_config);
|
||||
void SuperCapSend(SuperCapInstance *instance, uint8_t *data);
|
||||
```
|
||||
## 私有函数和变量
|
||||
|
||||
```c
|
||||
static SuperCapInstance *super_cap_instance = NULL;
|
||||
static uint8_t *rxbuff;
|
||||
static void SuperCapRxCallback(can_instance *_instance)
|
||||
```
|
||||
|
||||
`SuperCapRxCallback()`是super cap初始化can实例时的回调函数,用于can接收中断,进行协议解析。
|
||||
|
||||
## 使用范例
|
||||
|
||||
初始化时设置如下:
|
||||
|
||||
```c
|
||||
SuperCap_Init_Config_s capconfig = {
|
||||
.can_config = {
|
||||
.can_handle = &hcan1,
|
||||
.rx_id = 0x301,
|
||||
.tx_id = 0x302
|
||||
},
|
||||
.recv_data_len = 4*sizeof(uint16_t),
|
||||
.send_data_len = sizeof(uint8_t)
|
||||
};
|
||||
SuperCapInstance *ins =SuperCapInit(&capconfig);
|
||||
```
|
||||
|
||||
|
||||
发送通过`SuperCapSend()`,建议使用强制类型转换:
|
||||
|
||||
```c
|
||||
uint16_t tx = 0x321;
|
||||
SuperCapSend(ins, (uint8_t*)&tx);
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user