添加了回调任务的支持,允许bsp将回调函数设置为在中断中唤醒任务而不是直接执行,有助于提高实时性

This commit is contained in:
NeoZng 2023-07-23 00:17:11 +08:00
parent e938075e11
commit 393923cc0b
6 changed files with 78 additions and 8 deletions

View File

@ -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 \

View File

@ -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();

56
bsp/bsp_tools.c Normal file
View File

@ -0,0 +1,56 @@
#include <stdarg.h>
#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; // 返回信号量,同时增加索引
}

13
bsp/bsp_tools.h Normal file
View File

@ -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);

View File

@ -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);
}
}