mirror of
https://gitee.com/dlmu-cone/bf_original_balance_chassis
synced 2026-07-24 19:47:44 +08:00
增加了遥控器按键功能并修改所有函数和类型定义为标准格式
This commit is contained in:
@@ -2,24 +2,24 @@
|
||||
#include "main.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
static uint8_t tmp_warning_level=0;
|
||||
static uint8_t tmp_warning_level = 0;
|
||||
|
||||
void buzzer_init()
|
||||
void BuzzerInit()
|
||||
{
|
||||
HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_3);
|
||||
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
|
||||
}
|
||||
|
||||
void buzzer_on(uint16_t psc, uint16_t pwm,uint8_t level)
|
||||
void BuzzerOn(uint16_t psc, uint16_t pwm, uint8_t level)
|
||||
{
|
||||
if(level>tmp_warning_level)
|
||||
if (level > tmp_warning_level)
|
||||
{
|
||||
tmp_warning_level=level;
|
||||
tmp_warning_level = level;
|
||||
__HAL_TIM_PRESCALER(&htim4, psc);
|
||||
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, pwm);
|
||||
}
|
||||
}
|
||||
void buzzer_off(void)
|
||||
void BuzzerOff(void)
|
||||
{
|
||||
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, 0);
|
||||
tmp_warning_level=0;
|
||||
tmp_warning_level = 0;
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
#include <stdint-gcc.h>
|
||||
|
||||
void buzzer_init();
|
||||
extern void buzzer_on(uint16_t psc, uint16_t pwm,uint8_t level);
|
||||
extern void buzzer_off(void);
|
||||
void BuzzerInit();
|
||||
extern void BuzzerOn(uint16_t psc, uint16_t pwm, uint8_t level);
|
||||
extern void BuzzerOff(void);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
/* can instance ptrs storage, used for recv callback */
|
||||
// 在CAN产生接收中断会遍历数组,选出hcan和rxid与发生中断的实例相同的那个,调用其回调函数
|
||||
static can_instance *instance[MX_REGISTER_DEVICE_CNT] = {NULL};
|
||||
static CANInstance *instance[MX_REGISTER_DEVICE_CNT] = {NULL};
|
||||
|
||||
/* ----------------two static function called by CANRegister()-------------------- */
|
||||
|
||||
@@ -24,7 +24,7 @@ static can_instance *instance[MX_REGISTER_DEVICE_CNT] = {NULL};
|
||||
*
|
||||
* @param _instance can instance owned by specific module
|
||||
*/
|
||||
static void CANAddFilter(can_instance *_instance)
|
||||
static void CANAddFilter(CANInstance *_instance)
|
||||
{
|
||||
CAN_FilterTypeDef can_filter_conf;
|
||||
static uint8_t can1_filter_idx = 0, can2_filter_idx = 14;
|
||||
@@ -60,15 +60,15 @@ static void CANServiceInit()
|
||||
|
||||
/* ----------------------- two extern callable function -----------------------*/
|
||||
|
||||
can_instance *CANRegister(can_instance_config_s *config)
|
||||
CANInstance *CANRegister(CAN_Init_Config_s *config)
|
||||
{
|
||||
static uint8_t idx; // 全局CAN实例索引,每次有新的模块注册会自增
|
||||
if (!idx)
|
||||
{
|
||||
CANServiceInit(); // 第一次注册,先进行硬件初始化
|
||||
}
|
||||
instance[idx] = (can_instance *)malloc(sizeof(can_instance)); // 分配空间
|
||||
memset(instance[idx], 0, sizeof(can_instance));
|
||||
instance[idx] = (CANInstance *)malloc(sizeof(CANInstance)); // 分配空间
|
||||
memset(instance[idx], 0, sizeof(CANInstance));
|
||||
// 进行发送报文的配置
|
||||
instance[idx]->txconf.StdId = config->tx_id;
|
||||
instance[idx]->txconf.IDE = CAN_ID_STD;
|
||||
@@ -85,7 +85,7 @@ can_instance *CANRegister(can_instance_config_s *config)
|
||||
}
|
||||
|
||||
/* TODO:目前似乎封装过度,应该添加一个指向tx_buff的指针,tx_buff不应该由CAN instance保存 */
|
||||
void CANTransmit(can_instance *_instance)
|
||||
void CANTransmit(CANInstance *_instance)
|
||||
{
|
||||
while (HAL_CAN_GetTxMailboxesFreeLevel(_instance->can_handle) == 0)
|
||||
;
|
||||
@@ -93,7 +93,7 @@ void CANTransmit(can_instance *_instance)
|
||||
HAL_CAN_AddTxMessage(_instance->can_handle, &_instance->txconf, _instance->tx_buff, &_instance->tx_mailbox);
|
||||
}
|
||||
|
||||
void CANSetDLC(can_instance *_instance, uint8_t length)
|
||||
void CANSetDLC(CANInstance *_instance, uint8_t length)
|
||||
{
|
||||
if (length > 8) // 安全检查
|
||||
while (1)
|
||||
|
||||
@@ -23,7 +23,7 @@ typedef struct _
|
||||
uint8_t rx_len; // 接收长度,可能为0-8
|
||||
// 接收的回调函数,用于解析接收到的数据
|
||||
void (*can_module_callback)(struct _ *); // callback needs an instance to tell among registered ones
|
||||
} can_instance;
|
||||
} CANInstance;
|
||||
#pragma pack()
|
||||
|
||||
/* this structure is used for initialization */
|
||||
@@ -32,8 +32,8 @@ typedef struct
|
||||
CAN_HandleTypeDef *can_handle;
|
||||
uint32_t tx_id;
|
||||
uint32_t rx_id;
|
||||
void (*can_module_callback)(can_instance *);
|
||||
} can_instance_config_s;
|
||||
void (*can_module_callback)(CANInstance *);
|
||||
} CAN_Init_Config_s;
|
||||
|
||||
/**
|
||||
* @brief 修改CAN发送报文的数据帧长度;注意最大长度为8,在没有进行修改的时候,默认长度为8
|
||||
@@ -41,7 +41,7 @@ typedef struct
|
||||
* @param _instance 要修改长度的can实例
|
||||
* @param length 设定长度
|
||||
*/
|
||||
void CANSetDLC(can_instance *_instance, uint8_t length);
|
||||
void CANSetDLC(CANInstance *_instance, uint8_t length);
|
||||
|
||||
/**
|
||||
* @brief transmit mesg through CAN device,通过can实例发送消息
|
||||
@@ -49,14 +49,14 @@ void CANSetDLC(can_instance *_instance, uint8_t length);
|
||||
*
|
||||
* @param _instance* can instance owned by module
|
||||
*/
|
||||
void CANTransmit(can_instance *_instance);
|
||||
void CANTransmit(CANInstance *_instance);
|
||||
|
||||
/**
|
||||
* @brief Register a module to CAN service,remember to call this before using a CAN device
|
||||
* 注册(初始化)一个can实例,需要传入初始化配置的指针.
|
||||
* @param config init config
|
||||
* @return can_instance* can instance owned by module
|
||||
* @return CANInstance* can instance owned by module
|
||||
*/
|
||||
can_instance *CANRegister(can_instance_config_s *config);
|
||||
CANInstance *CANRegister(CAN_Init_Config_s *config);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
void BSPInit()
|
||||
{
|
||||
DWT_Init(168);
|
||||
BSP_Log_Init();
|
||||
LED_init();
|
||||
BSPLogInit();
|
||||
LEDInit();
|
||||
IMUTempInit();
|
||||
buzzer_init();
|
||||
BuzzerInit();
|
||||
}
|
||||
@@ -4,14 +4,14 @@
|
||||
extern TIM_HandleTypeDef htim5;
|
||||
static uint8_t tmp_output_level = 0;
|
||||
|
||||
void LED_init()
|
||||
void LEDInit()
|
||||
{
|
||||
HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_1);
|
||||
HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_2);
|
||||
HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_3);
|
||||
}
|
||||
|
||||
void aRGB_led_show(uint32_t aRGB)
|
||||
void FlowRGBShow(uint32_t aRGB)
|
||||
{
|
||||
static uint8_t alpha;
|
||||
static uint16_t red, green, blue;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <stdint-gcc.h>
|
||||
|
||||
void LED_init();
|
||||
extern void aRGB_led_show(uint32_t aRGB);
|
||||
void LEDInit();
|
||||
extern void FlowRGBShow(uint32_t aRGB);
|
||||
|
||||
#endif
|
||||
@@ -6,12 +6,12 @@
|
||||
|
||||
#define BUFFER_INDEX 0
|
||||
|
||||
void BSP_Log_Init()
|
||||
void BSPLogInit()
|
||||
{
|
||||
SEGGER_RTT_Init();
|
||||
}
|
||||
|
||||
int printf_log(const char *fmt, ...)
|
||||
int PrintLog(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef _BSP_LOG_H
|
||||
#define _BSP_LOG_H
|
||||
|
||||
void BSP_Log_Init();
|
||||
void BSPLogInit();
|
||||
|
||||
int printf_log(const char *fmt, ...);
|
||||
int PrintLog(const char *fmt, ...);
|
||||
|
||||
void Float2Str(char *str, float va);
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ void IMUTempInit()
|
||||
HAL_TIM_PWM_Start(&htim10, TIM_CHANNEL_1);
|
||||
}
|
||||
|
||||
void imu_pwm_set(uint16_t pwm)
|
||||
void IMUPWMSet(uint16_t pwm)
|
||||
{
|
||||
__HAL_TIM_SetCompare(&htim10, TIM_CHANNEL_1, pwm);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,6 @@
|
||||
#include "tim.h"
|
||||
|
||||
void IMUTempInit();
|
||||
extern void imu_pwm_set(uint16_t pwm);
|
||||
extern void IMUPWMSet(uint16_t pwm);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
/* usart service instance, modules' info would be recoreded here using USARTRegister() */
|
||||
/* usart服务实例,所有注册了usart的模块信息会被保存在这里 */
|
||||
static usart_instance *instance[DEVICE_USART_CNT] = {NULL};
|
||||
static USARTInstance *instance[DEVICE_USART_CNT] = {NULL};
|
||||
|
||||
/**
|
||||
* @brief usart service will start automatically, after each module registered
|
||||
@@ -22,7 +22,7 @@ static usart_instance *instance[DEVICE_USART_CNT] = {NULL};
|
||||
*
|
||||
* @param _instance instance owned by module,模块拥有的串口实例
|
||||
*/
|
||||
static void USARTServiceInit(usart_instance *_instance)
|
||||
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()
|
||||
@@ -31,23 +31,23 @@ static void USARTServiceInit(usart_instance *_instance)
|
||||
__HAL_DMA_DISABLE_IT(_instance->usart_handle->hdmarx, DMA_IT_HT);
|
||||
}
|
||||
|
||||
usart_instance* USARTRegister(USART_Init_Config_s *init_config)
|
||||
USARTInstance *USARTRegister(USART_Init_Config_s *init_config)
|
||||
{
|
||||
static uint8_t idx;
|
||||
|
||||
instance[idx]=(usart_instance*)malloc(sizeof(usart_instance));
|
||||
memset(instance[idx],0,sizeof(usart_instance));
|
||||
instance[idx] = (USARTInstance *)malloc(sizeof(USARTInstance));
|
||||
memset(instance[idx], 0, sizeof(USARTInstance));
|
||||
|
||||
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;
|
||||
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++];
|
||||
}
|
||||
|
||||
/* @todo 当前仅进行了形式上的封装,后续要进一步考虑是否将module的行为与bsp完全分离 */
|
||||
void USARTSend(usart_instance *_instance, uint8_t *send_buf, uint16_t send_size)
|
||||
void USARTSend(USARTInstance *_instance, uint8_t *send_buf, uint16_t send_size)
|
||||
{
|
||||
HAL_UART_Transmit_DMA(_instance->usart_handle, send_buf, send_size);
|
||||
}
|
||||
@@ -75,7 +75,7 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
|
||||
if (huart == instance[i]->usart_handle)
|
||||
{
|
||||
instance[i]->module_callback();
|
||||
memset(instance[i]->recv_buff,0,Size); // 接收结束后清空buffer,对于变长数据是必要的
|
||||
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;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
/* application callback,which resolves specific protocol,解析协议的回调函数 */
|
||||
typedef void (*usart_module_callback)();
|
||||
|
||||
/* usart_instance struct,each app would have one instance */
|
||||
/* USARTInstance struct,each app would have one instance */
|
||||
typedef struct
|
||||
{
|
||||
// 更新:弃用malloc方案,使用了固定大小的数组方便debug时查看
|
||||
@@ -18,7 +18,7 @@ typedef struct
|
||||
uint8_t recv_buff_size; // 模块接收一包数据的大小
|
||||
UART_HandleTypeDef *usart_handle; // 实例对应的usart_handle
|
||||
usart_module_callback module_callback; // 解析收到的数据的回调函数
|
||||
} usart_instance;
|
||||
} USARTInstance;
|
||||
|
||||
/* usart 初始化配置结构体 */
|
||||
typedef struct
|
||||
@@ -33,7 +33,7 @@ typedef struct
|
||||
*
|
||||
* @param init_config 传入串口初始化结构体
|
||||
*/
|
||||
usart_instance* USARTRegister(USART_Init_Config_s *init_config);
|
||||
USARTInstance *USARTRegister(USART_Init_Config_s *init_config);
|
||||
|
||||
/**
|
||||
* @todo 是否需要进一步封装发送buff和size,并创建一个串口任务以一定频率自动发送?
|
||||
@@ -45,6 +45,6 @@ usart_instance* USARTRegister(USART_Init_Config_s *init_config);
|
||||
* @param id specify which usart would be used
|
||||
* @param send_size how many bytes to send
|
||||
*/
|
||||
void USARTSend(usart_instance *_instance, uint8_t *send_buf, uint16_t send_size);
|
||||
void USARTSend(USARTInstance *_instance, uint8_t *send_buf, uint16_t send_size);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user