- Add callback_task with ISR-based UART command parser (@command\n protocol) - Add log_printf with mutex protection to prevent printf interleaving - Add per-motor enable/disable (motor enable yaw|pitch) - Add PID tuning via UART (pid roll kp 15) - Add cmd_parser module (registration + tokenize + dispatch) - Add UART layer architecture (interface → HAL → BSP) - Add filter modules (lowpass, moving_average, notch, rate_limiter) - Rewrite I2C bus and UART bus modules - Rewrite PID controller and MF4010V2 motor driver - Fix soft I2C driver Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
68 lines
1.7 KiB
C
68 lines
1.7 KiB
C
#include "app.h"
|
||
#include "cmsis_os2.h"
|
||
#include <string.h>
|
||
#include <stdarg.h>
|
||
|
||
|
||
static led_t led0, led1, led2;
|
||
|
||
/* IMU 数据队列句柄 — sensor_task 和 control_task 共享 */
|
||
osMessageQueueId_t g_imu_queue = NULL;
|
||
|
||
/* UART 命令队列 — ISR → callback_task */
|
||
osMessageQueueId_t g_cmd_queue = NULL;
|
||
|
||
/* UART 发送互斥锁 — 防止多任务 printf 交叉 */
|
||
osMutexId_t g_uart_mutex = NULL;
|
||
|
||
|
||
void app_init(void)
|
||
{
|
||
// 初始化硬件抽象层
|
||
hal_i2c_init_all();
|
||
hal_led_init_all();
|
||
hal_can_init_all();
|
||
hal_gpio_init_all();
|
||
hal_soft_i2c_init_all();
|
||
hal_uart_init_all();
|
||
|
||
// 初始化外设
|
||
led_init(&led0, 0);
|
||
led_init(&led1, 1);
|
||
led_init(&led2, 2);
|
||
led_on(&led0);
|
||
led_on(&led1);
|
||
led_on(&led2);
|
||
led_off(&led0);
|
||
|
||
// 创建 IMU 数据队列(sensor_task → control_task)
|
||
// 深度 4,每消息 3×float×3 = 36 bytes
|
||
g_imu_queue = osMessageQueueNew(1, sizeof(imu_data_t), NULL);
|
||
g_cmd_queue = osMessageQueueNew(5, MAX_CMD_LEN, NULL);
|
||
|
||
// 创建 UART 互斥锁(保护 printf 不被多任务交叉打断)
|
||
g_uart_mutex = osMutexNew(NULL);
|
||
if (g_cmd_queue == NULL) {
|
||
printf("[app_init] ERROR: Failed to create command queue\r\n");
|
||
}
|
||
if (g_imu_queue == NULL) {
|
||
printf("[app_init] ERROR: Failed to create IMU queue\r\n");
|
||
}
|
||
|
||
printf("[app_init] Hardware initialized\r\n");
|
||
}
|
||
|
||
void log_printf(const char *fmt, ...)
|
||
{
|
||
va_list args;
|
||
if (g_uart_mutex != NULL) {
|
||
osMutexAcquire(g_uart_mutex, osWaitForever);
|
||
}
|
||
va_start(args, fmt);
|
||
vprintf(fmt, args);
|
||
va_end(args);
|
||
if (g_uart_mutex != NULL) {
|
||
osMutexRelease(g_uart_mutex);
|
||
}
|
||
}
|