diff --git a/Makefile b/Makefile index fd72d0f..a1e97ff 100644 --- a/Makefile +++ b/Makefile @@ -117,6 +117,7 @@ bsp/usart/bsp_usart.c \ bsp/usb/bsp_usb.c \ bsp/log/bsp_log.c \ bsp/flash/bsp_flash.c \ +bsp/bsp_tools.c \ modules/algorithm/controller.c \ modules/algorithm/kalman_filter.c \ modules/algorithm/QuaternionEKF.c \ diff --git a/application/robot_task.h b/application/robot_task.h index 5a58025..f9aa832 100644 --- a/application/robot_task.h +++ b/application/robot_task.h @@ -58,7 +58,7 @@ void StartINSTASK(void const *argument) static float ins_start, ins_dt; INS_Init(); // 确保BMI088被正确初始化. LOGINFO("[freeRTOS] INS Task Start"); - while (1) + for (;;) { // 1kHz ins_start = DWT_GetTimeline_ms(); @@ -75,7 +75,7 @@ void StartMOTORTASK(void const *argument) { static float motor_dt, motor_start; LOGINFO("[freeRTOS] MOTOR Task Start"); - while (1) + for (;;) { motor_start = DWT_GetTimeline_ms(); MotorControlTask(); @@ -91,7 +91,7 @@ void StartDAEMONTASK(void const *argument) static float daemon_dt, daemon_start; BuzzerInit(); LOGINFO("[freeRTOS] Daemon Task Start"); - while (1) + for (;;) { // 100Hz daemon_start = DWT_GetTimeline_ms(); @@ -109,7 +109,7 @@ void StartROBOTTASK(void const *argument) static float robot_dt, robot_start; LOGINFO("[freeRTOS] ROBOT core Task Start"); // 200Hz-500Hz,若有额外的控制任务如平衡步兵可能需要提升至1kHz - while (1) + for (;;) { robot_start = DWT_GetTimeline_ms(); RobotTask(); @@ -125,7 +125,7 @@ void StartUITASK(void const *argument) LOGINFO("[freeRTOS] UI Task Start"); MyUIInit(); LOGINFO("[freeRTOS] UI Init Done, communication with ref has established"); - while (1) + for (;;) { // 每给裁判系统发送一包数据会挂起一次,详见UITask函数的refereeSend() UITask(); diff --git a/bsp/bsp_init.h b/bsp/bsp_init.h index cad17e3..73cecf8 100644 --- a/bsp/bsp_init.h +++ b/bsp/bsp_init.h @@ -11,7 +11,7 @@ * 需在实时系统启动前调用,目前由RobotoInit()调用 * * @note 其他实例型的外设如CAN和串口会在注册实例的时候自动初始化,不注册不初始化 - */ + */ // void BSPInit() { diff --git a/bsp/bsp_tools.c b/bsp/bsp_tools.c new file mode 100644 index 0000000..5badf2e --- /dev/null +++ b/bsp/bsp_tools.c @@ -0,0 +1,56 @@ +#include + +#include "cmsis_os.h" +#include "bsp_log.h" +#include "bsp_tools.h" + +#define MX_SIG_LIST_SIZE 32 // 不可修改,最大信号量数量 + +typedef struct +{ + void *callback; + uint32_t sig; + void *ins; +} CallbackTask_t; + +// 递增记录全局信号量的数组,最大32个信号量 +static uint8_t sig_idx = 0; +static uint32_t tmp_sig = 1; // tmp_sing << sig_idx从而生成对应信号量 + +static osThreadId cbkid_list[MX_SIG_LIST_SIZE]; +static CallbackTask_t cbkinfo_list[MX_SIG_LIST_SIZE]; + +// 死循环任务,执行cbk函数指针,每次执行完毕后等待sig信号 +static void CallbackTaskBase(const void *cbk) +{ + void (*cbk_func)(void *) = (void (*)(void *))((CallbackTask_t *)cbk)->callback; + void *ins = ((CallbackTask_t *)cbk)->ins; + uint32_t sig = ((CallbackTask_t *)cbk)->sig; + + for (;;) + { + cbk_func(ins); + osSignalWait(sig, osWaitForever); + } +} + +uint32_t CreateCallbackTask(char *name, void *cbk, void *ins, osPriority priority) +{ + if (sig_idx >= MX_SIG_LIST_SIZE) + while (1) + LOGERROR("[rtos:cbk_register] CreateCallbackTask: sig_idx >= MX_SIG_LIST_SIZE"); + + cbkinfo_list[sig_idx].callback = cbk; + cbkinfo_list[sig_idx].sig = tmp_sig << sig_idx; + cbkinfo_list[sig_idx].ins = ins; + + osThreadDef_t threadDef; + threadDef.name = name; + threadDef.pthread = CallbackTaskBase; + threadDef.tpriority = priority; + threadDef.instances = 0; + threadDef.stacksize = 128; + cbkid_list[sig_idx] = osThreadCreate(&threadDef, (void *)&cbkinfo_list[sig_idx]); + + return cbkinfo_list[sig_idx++].sig; // 返回信号量,同时增加索引 +} \ No newline at end of file diff --git a/bsp/bsp_tools.h b/bsp/bsp_tools.h new file mode 100644 index 0000000..6d7a2be --- /dev/null +++ b/bsp/bsp_tools.h @@ -0,0 +1,13 @@ +#include "cmsis_os.h" +#include "bsp_log.h" + +/** + * @brief 创建一个新的任务,该任务收到osSignalSet信号时会被唤醒,否则保持挂起状态 + * + * @param name 任务名称,注意以'\0'结尾 + * @param cbk 回调函数指针 + * @param ins 回调函数的参数,bsp为实例指针 + * @param priority 任务优先级,决定了在中断结束后能否保证立刻执行 + * @return uint32_t 信号量,用于唤醒该任务 + */ +uint32_t CreateCallbackTask(char *name, void *cbk, void *ins, osPriority priority); \ No newline at end of file diff --git a/modules/motor/DJImotor/dji_motor.c b/modules/motor/DJImotor/dji_motor.c index 73dc080..e9345cc 100644 --- a/modules/motor/DJImotor/dji_motor.c +++ b/modules/motor/DJImotor/dji_motor.c @@ -72,7 +72,7 @@ static void MotorSenderGrouping(DJIMotorInstance *motor, CAN_Init_Config_s *conf { LOGERROR("[dji_motor] ID crash. Check in debug mode, add dji_motor_instance to watch to get more information."); uint16_t can_bus = config->can_handle == &hcan1 ? 1 : 2; - while (1) // 后续可以把id和CAN打印出来 // 6020的id 1-4和2006/3508的id 5-8会发生冲突(若有注册,即1!5,2!6,3!7,4!8) (1!5!,LTC! (((不是) + while (1) // 6020的id 1-4和2006/3508的id 5-8会发生冲突(若有注册,即1!5,2!6,3!7,4!8) (1!5!,LTC! (((不是) LOGERROR("[dji_motor] id [%d], can_bus [%d]", config->rx_id, can_bus); } } @@ -101,7 +101,7 @@ static void MotorSenderGrouping(DJIMotorInstance *motor, CAN_Init_Config_s *conf { LOGERROR("[dji_motor] ID crash. Check in debug mode, add dji_motor_instance to watch to get more information."); uint16_t can_bus = config->can_handle == &hcan1 ? 1 : 2; - while (1) // 后续可以把id和CAN打印出来 // 6020的id 1-4和2006/3508的id 5-8会发生冲突(若有注册,即1!5,2!6,3!7,4!8) (1!5!,LTC! (((不是) + while (1) // 6020的id 1-4和2006/3508的id 5-8会发生冲突(若有注册,即1!5,2!6,3!7,4!8) (1!5!,LTC! (((不是) LOGERROR("[dji_motor] id [%d], can_bus [%d]", config->rx_id, can_bus); } }