mirror of
https://gitee.com/dlmu-cone/bf_original_balance_chassis
synced 2026-07-24 11:37:45 +08:00
增加了BSP PWM的支持,修改了bsp和module层的初始化提高可读性,新年快乐
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
|
||||
/* can instance ptrs storage, used for recv callback */
|
||||
// 在CAN产生接收中断会遍历数组,选出hcan和rxid与发生中断的实例相同的那个,调用其回调函数
|
||||
static CANInstance *instance[MX_REGISTER_DEVICE_CNT] = {NULL};
|
||||
static CANInstance *can_instance[MX_REGISTER_DEVICE_CNT] = {NULL};
|
||||
static uint8_t idx; // 全局CAN实例索引,每次有新的模块注册会自增
|
||||
|
||||
/* ----------------two static function called by CANRegister()-------------------- */
|
||||
@@ -67,22 +67,24 @@ CANInstance *CANRegister(CAN_Init_Config_s *config)
|
||||
{
|
||||
CANServiceInit(); // 第一次注册,先进行硬件初始化
|
||||
}
|
||||
instance[idx] = (CANInstance *)malloc(sizeof(CANInstance)); // 分配空间
|
||||
memset(instance[idx], 0, sizeof(CANInstance));
|
||||
CANInstance *instance = (CANInstance *)malloc(sizeof(CANInstance)); // 分配空间
|
||||
memset(instance, 0, sizeof(CANInstance));
|
||||
// 进行发送报文的配置
|
||||
instance[idx]->txconf.StdId = config->tx_id;
|
||||
instance[idx]->txconf.IDE = CAN_ID_STD;
|
||||
instance[idx]->txconf.RTR = CAN_RTR_DATA;
|
||||
instance[idx]->txconf.DLC = 0x08; // 默认发送长度为8
|
||||
instance->txconf.StdId = config->tx_id;
|
||||
instance->txconf.IDE = CAN_ID_STD;
|
||||
instance->txconf.RTR = CAN_RTR_DATA;
|
||||
instance->txconf.DLC = 0x08; // 默认发送长度为8
|
||||
// 设置回调函数和接收发送id
|
||||
instance[idx]->can_handle = config->can_handle;
|
||||
instance[idx]->tx_id = config->tx_id; // 好像没用,可以删掉
|
||||
instance[idx]->rx_id = config->rx_id;
|
||||
instance[idx]->can_module_callback = config->can_module_callback;
|
||||
instance[idx]->id = config->id;
|
||||
instance->can_handle = config->can_handle;
|
||||
instance->tx_id = config->tx_id; // 好像没用,可以删掉
|
||||
instance->rx_id = config->rx_id;
|
||||
instance->can_module_callback = config->can_module_callback;
|
||||
instance->id = config->id;
|
||||
|
||||
CANAddFilter(instance[idx]); // 添加CAN过滤器规则
|
||||
return instance[idx++]; // 返回指针
|
||||
CANAddFilter(instance); // 添加CAN过滤器规则
|
||||
can_instance[idx++] = instance; // 将实例保存到can_instance中
|
||||
|
||||
return instance; // 返回can实例指针
|
||||
}
|
||||
|
||||
/* TODO:目前似乎封装过度,应该添加一个指向tx_buff的指针,tx_buff不应该由CAN instance保存 */
|
||||
@@ -117,17 +119,16 @@ static void CANFIFOxCallback(CAN_HandleTypeDef *_hcan, uint32_t fifox)
|
||||
static CAN_RxHeaderTypeDef rxconf;
|
||||
HAL_CAN_GetRxMessage(_hcan, fifox, &rxconf, can_rx_buff);
|
||||
for (size_t i = 0; i < idx; ++i)
|
||||
{
|
||||
// 两者相等说明这是要找的实例
|
||||
if (_hcan == instance[i]->can_handle && rxconf.StdId == instance[i]->rx_id)
|
||||
{ // 两者相等说明这是要找的实例
|
||||
if (_hcan == can_instance[i]->can_handle && rxconf.StdId == can_instance[i]->rx_id)
|
||||
{
|
||||
instance[i]->rx_len = rxconf.DLC;
|
||||
memcpy(instance[i]->rx_buff, can_rx_buff, rxconf.DLC); // 消息拷贝到对应实例
|
||||
if (instance[i]->can_module_callback != NULL)
|
||||
if (can_instance[i]->can_module_callback != NULL)
|
||||
{
|
||||
instance[i]->can_module_callback(instance[i]); // 触发回调进行数据解析和处理
|
||||
can_instance[i]->rx_len = rxconf.DLC; // 保存接收到的数据长度
|
||||
memcpy(can_instance[i]->rx_buff, can_rx_buff, rxconf.DLC); // 消息拷贝到对应实例
|
||||
can_instance[i]->can_module_callback(can_instance[i]); // 触发回调进行数据解析和处理
|
||||
}
|
||||
break;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,16 +37,18 @@ void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
|
||||
IICInstance *IICRegister(IIC_Init_Config_s *conf)
|
||||
{
|
||||
// 申请到的空间未必是0, 所以需要手动初始化
|
||||
iic_instance[idx] = (IICInstance *)malloc(sizeof(IICInstance));
|
||||
memset(iic_instance[idx], 0, sizeof(IICInstance));
|
||||
IICInstance *instance = (IICInstance *)malloc(sizeof(IICInstance));
|
||||
instance = (IICInstance *)malloc(sizeof(IICInstance));
|
||||
memset(instance, 0, sizeof(IICInstance));
|
||||
|
||||
iic_instance[idx]->dev_address = conf->dev_address;
|
||||
iic_instance[idx]->callback = conf->callback;
|
||||
iic_instance[idx]->work_mode = conf->work_mode;
|
||||
iic_instance[idx]->handle = conf->handle;
|
||||
iic_instance[idx]->id = conf->id;
|
||||
instance->dev_address = conf->dev_address;
|
||||
instance->callback = conf->callback;
|
||||
instance->work_mode = conf->work_mode;
|
||||
instance->handle = conf->handle;
|
||||
instance->id = conf->id;
|
||||
|
||||
return iic_instance[idx++];
|
||||
iic_instance[idx++] = instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void IICSetMode(IICInstance *iic, IIC_Work_Mode_e mode)
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
#include "bsp_pwm.h"
|
||||
#include "stdlib.h"
|
||||
#include "memory.h"
|
||||
|
||||
// 配合中断以及初始化
|
||||
static uint8_t idx;
|
||||
static PWMInstance *pwm_instance[PWM_DEVICE_CNT] = {NULL}; // 所有的pwm instance保存于此,用于callback时判断中断来源
|
||||
|
||||
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
|
||||
{
|
||||
for (uint8_t i = 0; i < idx; i++)
|
||||
{ // 来自同一个定时器的中断且通道相同
|
||||
if (pwm_instance[i]->htim == htim && htim->Channel == pwm_instance[i]->channel)
|
||||
{
|
||||
if (pwm_instance[i]->callback) // 如果有回调函数
|
||||
{
|
||||
pwm_instance[i]->callback(pwm_instance[i]);
|
||||
}
|
||||
return; // 一次只能有一个通道的中断,所以直接返回
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PWMInstance *PWMRegister(PWM_Init_Config_s *config)
|
||||
{
|
||||
PWMInstance *pwm = (PWMInstance *)malloc(sizeof(PWMInstance));
|
||||
memset(pwm, 0, sizeof(PWMInstance));
|
||||
|
||||
pwm->htim = config->htim;
|
||||
pwm->channel = config->channel;
|
||||
pwm->period = config->period;
|
||||
pwm->pulse = config->pulse;
|
||||
pwm->callback = config->callback;
|
||||
pwm->id = config->id;
|
||||
|
||||
HAL_TIM_PWM_Start(pwm->htim, pwm->channel);
|
||||
__HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse);
|
||||
|
||||
pwm_instance[idx++] = pwm;
|
||||
return pwm;
|
||||
}
|
||||
|
||||
/* 只是对HAL的函数进行了形式上的封装 */
|
||||
void PWMStart(PWMInstance *pwm)
|
||||
{
|
||||
HAL_TIM_PWM_Start(pwm->htim, pwm->channel);
|
||||
__HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse);
|
||||
}
|
||||
|
||||
/* 只是对HAL的函数进行了形式上的封装 */
|
||||
void PWMStop(PWMInstance *pwm)
|
||||
{
|
||||
HAL_TIM_PWM_Stop(pwm->htim, pwm->channel);
|
||||
}
|
||||
|
||||
/* 只是对HAL的函数进行了形式上的封装 */
|
||||
void PWMSetPulse(PWMInstance *pwm, uint32_t pulse)
|
||||
{
|
||||
pwm->pulse = pulse;
|
||||
__HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse);
|
||||
}
|
||||
|
||||
/* 只是对HAL的函数进行了形式上的封装 */
|
||||
void PWMStartDMA(PWMInstance *pwm, uint32_t *pData, uint32_t Size)
|
||||
{
|
||||
HAL_TIM_PWM_Start_DMA(pwm->htim, pwm->channel, pData, Size);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#ifndef BSP_PWM_H
|
||||
#define BSP_PWM_H
|
||||
|
||||
#include "tim.h"
|
||||
#include "stdint.h"
|
||||
|
||||
#define PWM_DEVICE_CNT 16 // PWM实例数量
|
||||
|
||||
/* pwm实例结构体 */
|
||||
typedef struct pwm_ins_temp
|
||||
{
|
||||
TIM_HandleTypeDef *htim; // TIM句柄
|
||||
uint32_t channel; // 通道
|
||||
uint32_t period; // 周期
|
||||
uint32_t pulse; // 脉宽
|
||||
void (*callback)(struct pwm_ins_temp *); // DMA传输完成回调函数
|
||||
void *id; // 实例ID
|
||||
} PWMInstance;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
TIM_HandleTypeDef *htim; // TIM句柄
|
||||
uint32_t channel; // 通道
|
||||
uint32_t period; // 周期
|
||||
uint32_t pulse; // 脉宽
|
||||
void (*callback)(struct pwm_ins_temp *); // DMA传输完成回调函数
|
||||
void *id; // 实例ID
|
||||
} PWM_Init_Config_s;
|
||||
|
||||
/**
|
||||
* @brief 注册一个pwm实例
|
||||
*
|
||||
* @param config 初始化配置
|
||||
* @return PWMInstance*
|
||||
*/
|
||||
PWMInstance *PWMRegister(PWM_Init_Config_s *config);
|
||||
|
||||
/**
|
||||
* @brief 启动pwm
|
||||
*
|
||||
* @param pwm pwm实例
|
||||
*/
|
||||
void PWMStart(PWMInstance *pwm);
|
||||
|
||||
/**
|
||||
* @brief 停止pwm
|
||||
*
|
||||
* @param pwm pwm实例
|
||||
*/
|
||||
void PWMStop(PWMInstance *pwm);
|
||||
|
||||
/**
|
||||
* @brief 设置pwm脉宽
|
||||
*
|
||||
* @param pwm pwm实例
|
||||
* @param pulse 脉宽
|
||||
*/
|
||||
void PWMSetPulse(PWMInstance *pwm, uint32_t pulse);
|
||||
|
||||
/**
|
||||
* @brief 启动pwm dma传输
|
||||
*
|
||||
* @param pwm pwm实例
|
||||
* @param pData 数据首地址指针,注意数据的位数必须和CubeMX配置的DMA传输位数(字长)一致
|
||||
* @param Size 数据长度
|
||||
* @note 如果使用此函数,则需要在CubeMX中配置DMA传输位数为对应位数
|
||||
* 例如:使用16位数据,则需要配置DMA传输位数为16位(half word),配置错误会导致指针越界或数据错误
|
||||
*/
|
||||
void PWMStartDMA(PWMInstance *pwm, uint32_t *pData, uint32_t Size);
|
||||
|
||||
#endif // BSP_PWM_H
|
||||
@@ -25,7 +25,7 @@ void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||
HAL_GPIO_WritePin(spi_instance[i]->GPIO_cs, spi_instance[i]->cs_pin, GPIO_PIN_SET);
|
||||
spi_instance[i]->callback(spi_instance[i]);
|
||||
}
|
||||
break;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,14 +42,17 @@ void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||
|
||||
SPIInstance *SPIRegister(SPI_Init_Config_s *conf)
|
||||
{
|
||||
spi_instance[idx] = (SPIInstance *)malloc(sizeof(SPIInstance));
|
||||
spi_instance[idx]->callback = conf->callback;
|
||||
spi_instance[idx]->spi_work_mode = conf->spi_work_mode;
|
||||
spi_instance[idx]->spi_handle = conf->spi_handle;
|
||||
spi_instance[idx]->GPIO_cs = conf->GPIO_cs;
|
||||
spi_instance[idx]->cs_pin = conf->cs_pin;
|
||||
spi_instance[idx]->id = conf->id;
|
||||
return spi_instance[idx++];
|
||||
SPIInstance *instance = (SPIInstance *)malloc(sizeof(SPIInstance));
|
||||
memset(instance, 0, sizeof(SPIInstance));
|
||||
instance->spi_handle = conf->spi_handle;
|
||||
instance->GPIO_cs = conf->GPIO_cs;
|
||||
instance->cs_pin = conf->cs_pin;
|
||||
instance->spi_work_mode = conf->spi_work_mode;
|
||||
instance->callback = conf->callback;
|
||||
instance->id = conf->id;
|
||||
|
||||
spi_instance[idx++] = instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void SPITransmit(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len)
|
||||
|
||||
@@ -14,7 +14,8 @@
|
||||
|
||||
/* usart service instance, modules' info would be recoreded here using USARTRegister() */
|
||||
/* usart服务实例,所有注册了usart的模块信息会被保存在这里 */
|
||||
static USARTInstance *instance[DEVICE_USART_CNT] = {NULL};
|
||||
static uint8_t idx;
|
||||
static USARTInstance *usart_instance[DEVICE_USART_CNT] = {NULL};
|
||||
|
||||
/**
|
||||
* @brief usart service will start automatically, after each module registered
|
||||
@@ -27,23 +28,21 @@ static void USARTServiceInit(USARTInstance *_instance)
|
||||
HAL_UARTEx_ReceiveToIdle_DMA(_instance->usart_handle, _instance->recv_buff, _instance->recv_buff_size);
|
||||
// 关闭dma half transfer中断防止两次进入HAL_UARTEx_RxEventCallback()
|
||||
// 这是HAL库的一个设计失误,发生DMA传输完成/半完成以及串口IDLE中断都会触发HAL_UARTEx_RxEventCallback()
|
||||
// 我们只希望处理,因此直接关闭DMA半传输中断第一种和第三种情况
|
||||
// 我们只希望处理第一种和第三种情况,因此直接关闭DMA半传输中断
|
||||
__HAL_DMA_DISABLE_IT(_instance->usart_handle->hdmarx, DMA_IT_HT);
|
||||
}
|
||||
|
||||
USARTInstance *USARTRegister(USART_Init_Config_s *init_config)
|
||||
{
|
||||
static uint8_t idx;
|
||||
USARTInstance *instance = (USARTInstance *)malloc(sizeof(USARTInstance));
|
||||
memset(instance, 0, sizeof(USARTInstance));
|
||||
|
||||
instance[idx] = (USARTInstance *)malloc(sizeof(USARTInstance));
|
||||
memset(instance[idx], 0, sizeof(USARTInstance));
|
||||
instance->usart_handle = init_config->usart_handle;
|
||||
instance->recv_buff_size = init_config->recv_buff_size;
|
||||
instance->module_callback = init_config->module_callback;
|
||||
|
||||
instance[idx]->module_callback = init_config->module_callback;
|
||||
instance[idx]->recv_buff_size = init_config->recv_buff_size;
|
||||
instance[idx]->usart_handle = init_config->usart_handle;
|
||||
USARTServiceInit(instance[idx]);
|
||||
|
||||
return instance[idx++];
|
||||
usart_instance[idx++] = instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
/* @todo 当前仅进行了形式上的封装,后续要进一步考虑是否将module的行为与bsp完全分离 */
|
||||
@@ -70,15 +69,18 @@ void USARTSend(USARTInstance *_instance, uint8_t *send_buf, uint16_t send_size)
|
||||
*/
|
||||
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
|
||||
{
|
||||
for (uint8_t i = 0; i < 3; ++i)
|
||||
{
|
||||
if (huart == instance[i]->usart_handle)
|
||||
{
|
||||
instance[i]->module_callback();
|
||||
memset(instance[i]->recv_buff, 0, Size); // 接收结束后清空buffer,对于变长数据是必要的
|
||||
HAL_UARTEx_ReceiveToIdle_DMA(instance[i]->usart_handle, instance[i]->recv_buff, instance[i]->recv_buff_size);
|
||||
__HAL_DMA_DISABLE_IT(instance[i]->usart_handle->hdmarx, DMA_IT_HT);
|
||||
break;
|
||||
for (uint8_t i = 0; i < idx; ++i)
|
||||
{ // find the instance which is being handled
|
||||
if (huart == usart_instance[i]->usart_handle)
|
||||
{ // call the callback function if it is not NULL
|
||||
if (usart_instance[i]->module_callback != NULL)
|
||||
{
|
||||
usart_instance[i]->module_callback();
|
||||
memset(usart_instance[i]->recv_buff, 0, Size); // 接收结束后清空buffer,对于变长数据是必要的
|
||||
}
|
||||
HAL_UARTEx_ReceiveToIdle_DMA(usart_instance[i]->usart_handle, usart_instance[i]->recv_buff, usart_instance[i]->recv_buff_size);
|
||||
__HAL_DMA_DISABLE_IT(usart_instance[i]->usart_handle->hdmarx, DMA_IT_HT);
|
||||
return; // break the loop
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,13 +95,13 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
|
||||
*/
|
||||
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
|
||||
{
|
||||
for (uint8_t i = 0; i < 3; ++i)
|
||||
for (uint8_t i = 0; i < idx; ++i)
|
||||
{
|
||||
if (huart == instance[i]->usart_handle)
|
||||
if (huart == usart_instance[i]->usart_handle)
|
||||
{
|
||||
HAL_UARTEx_ReceiveToIdle_DMA(instance[i]->usart_handle, instance[i]->recv_buff, instance[i]->recv_buff_size);
|
||||
__HAL_DMA_DISABLE_IT(instance[i]->usart_handle->hdmarx, DMA_IT_HT);
|
||||
break;
|
||||
HAL_UARTEx_ReceiveToIdle_DMA(usart_instance[i]->usart_handle, usart_instance[i]->recv_buff, usart_instance[i]->recv_buff_size);
|
||||
__HAL_DMA_DISABLE_IT(usart_instance[i]->usart_handle->hdmarx, DMA_IT_HT);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user