dr16好了

This commit is contained in:
Robofish 2026-01-06 01:08:52 +08:00
parent 21c0f7a4cd
commit 31147537b5
27 changed files with 219 additions and 147 deletions

View File

@ -57,20 +57,10 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE
# User/component sources # User/component sources
User/component/ahrs.c User/component/ahrs.c
# User/component/ahrs sources
User/component/ahrs/ahrs.c
# User/component sources
User/component/crc16.c User/component/crc16.c
User/component/crc8.c User/component/crc8.c
User/component/error_detect.c User/component/error_detect.c
User/component/filter.c User/component/filter.c
# User/component/filter sources
User/component/filter/filter.c
# User/component sources
User/component/freertos_cli.c User/component/freertos_cli.c
User/component/limiter.c User/component/limiter.c
User/component/pid.c User/component/pid.c
@ -92,6 +82,7 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE
# User/task sources # User/task sources
User/task/ai.c User/task/ai.c
User/task/init.c User/task/init.c
User/task/rc.c
User/task/user_task.c User/task/user_task.c
) )

View File

@ -1,127 +1,127 @@
/* USER CODE BEGIN Header */ /* USER CODE BEGIN Header */
/** /**
****************************************************************************** ******************************************************************************
* File Name : freertos.c * File Name : freertos.c
* Description : Code for freertos applications * Description : Code for freertos applications
****************************************************************************** ******************************************************************************
* @attention * @attention
* *
* Copyright (c) 2026 STMicroelectronics. * Copyright (c) 2026 STMicroelectronics.
* All rights reserved. * All rights reserved.
* *
* This software is licensed under terms that can be found in the LICENSE file * This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component. * in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS. * If no LICENSE file comes with this software, it is provided AS-IS.
* *
****************************************************************************** ******************************************************************************
*/ */
/* USER CODE END Header */ /* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include "main.h" #include "main.h"
#include "cmsis_os.h" #include "cmsis_os.h"
/* Private includes ----------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include "task/user_task.h" #include "task/user_task.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */ /* USER CODE BEGIN PTD */
/* USER CODE END PTD */ /* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */ /* USER CODE BEGIN PD */
/* USER CODE END PD */ /* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */ /* USER CODE BEGIN PM */
/* USER CODE END PM */ /* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */ /* USER CODE BEGIN Variables */
/* USER CODE END Variables */ /* USER CODE END Variables */
/* Definitions for defaultTask */ /* Definitions for defaultTask */
osThreadId_t defaultTaskHandle; osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = { const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask", .name = "defaultTask",
.stack_size = 128 * 4, .stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityNormal, .priority = (osPriority_t) osPriorityNormal,
}; };
/* Private function prototypes -----------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */ /* USER CODE BEGIN FunctionPrototypes */
/* USER CODE END FunctionPrototypes */ /* USER CODE END FunctionPrototypes */
void StartDefaultTask(void *argument); void StartDefaultTask(void *argument);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/** /**
* @brief FreeRTOS initialization * @brief FreeRTOS initialization
* @param None * @param None
* @retval None * @retval None
*/ */
void MX_FREERTOS_Init(void) { void MX_FREERTOS_Init(void) {
/* USER CODE BEGIN Init */ /* USER CODE BEGIN Init */
/* USER CODE END Init */ /* USER CODE END Init */
/* USER CODE BEGIN RTOS_MUTEX */ /* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */ /* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */ /* USER CODE END RTOS_MUTEX */
/* USER CODE BEGIN RTOS_SEMAPHORES */ /* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */ /* add semaphores, ... */
/* USER CODE END RTOS_SEMAPHORES */ /* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */ /* USER CODE BEGIN RTOS_TIMERS */
/* start timers, add new ones, ... */ /* start timers, add new ones, ... */
/* USER CODE END RTOS_TIMERS */ /* USER CODE END RTOS_TIMERS */
/* USER CODE BEGIN RTOS_QUEUES */ /* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */ /* add queues, ... */
/* USER CODE END RTOS_QUEUES */ /* USER CODE END RTOS_QUEUES */
/* Create the thread(s) */ /* Create the thread(s) */
/* creation of defaultTask */ /* creation of defaultTask */
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes); defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
/* USER CODE BEGIN RTOS_THREADS */ /* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */ /* add threads, ... */
osThreadNew(Task_Init, NULL, &attr_init); // 创建初始化任务 osThreadNew(Task_Init, NULL, &attr_init); // 创建初始化任务
/* USER CODE END RTOS_THREADS */ /* USER CODE END RTOS_THREADS */
/* USER CODE BEGIN RTOS_EVENTS */ /* USER CODE BEGIN RTOS_EVENTS */
/* add events, ... */ /* add events, ... */
/* USER CODE END RTOS_EVENTS */ /* USER CODE END RTOS_EVENTS */
} }
/* USER CODE BEGIN Header_StartDefaultTask */ /* USER CODE BEGIN Header_StartDefaultTask */
/** /**
* @brief Function implementing the defaultTask thread. * @brief Function implementing the defaultTask thread.
* @param argument: Not used * @param argument: Not used
* @retval None * @retval None
*/ */
/* USER CODE END Header_StartDefaultTask */ /* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument) void StartDefaultTask(void *argument)
{ {
/* USER CODE BEGIN StartDefaultTask */ /* USER CODE BEGIN StartDefaultTask */
osThreadTerminate(osThreadGetId()); osThreadTerminate(osThreadGetId());
/* USER CODE END StartDefaultTask */ /* USER CODE END StartDefaultTask */
} }
/* Private application code --------------------------------------------------*/ /* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */ /* USER CODE BEGIN Application */
/* USER CODE END Application */ /* USER CODE END Application */

View File

@ -80,14 +80,6 @@ int main(void)
/* USER CODE END 1 */ /* USER CODE END 1 */
/* Enable the CPU Cache */
/* Enable I-Cache---------------------------------------------------------*/
SCB_EnableICache();
/* Enable D-Cache---------------------------------------------------------*/
SCB_EnableDCache();
/* MCU Configuration--------------------------------------------------------*/ /* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

View File

@ -229,6 +229,17 @@ SECTIONS
PROVIDE( __bss_start = __tbss_start ); PROVIDE( __bss_start = __tbss_start );
PROVIDE( __bss_size = __bss_end - __bss_start ); PROVIDE( __bss_size = __bss_end - __bss_start );
/* DMA buffer section in RAM_D2 (SRAM1/2) for STM32H7 DMA access */
.dma_buffer (NOLOAD) : ALIGN(32)
{
. = ALIGN(32);
_sdma_buffer = .;
*(.dma_buffer)
*(.dma_buffer*)
. = ALIGN(32);
_edma_buffer = .;
} >RAM_D2
/* User_heap_stack section, used to check that there is enough RAM left */ /* User_heap_stack section, used to check that there is enough RAM left */
._user_heap_stack (NOLOAD) : ._user_heap_stack (NOLOAD) :
{ {

View File

@ -5,3 +5,10 @@
function: Task_ai function: Task_ai
name: ai name: ai
stack: 256 stack: 256
- delay: 0
description: ''
freq_control: true
frequency: 500.0
function: Task_rc
name: rc
stack: 256

View File

@ -31,6 +31,7 @@ void Task_Init(void *argument) {
/* 创建任务线程 */ /* 创建任务线程 */
task_runtime.thread.ai = osThreadNew(Task_ai, NULL, &attr_ai); task_runtime.thread.ai = osThreadNew(Task_ai, NULL, &attr_ai);
task_runtime.thread.rc = osThreadNew(Task_rc, NULL, &attr_rc);
// 创建消息队列 // 创建消息队列
/* USER MESSAGE BEGIN */ /* USER MESSAGE BEGIN */

56
User/task/rc.c Normal file
View File

@ -0,0 +1,56 @@
/*
rc Task
- DR16遥控器数据接收
*/
/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "device/dr16.h"
/* USER INCLUDE END */
/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
/* STM32H7: 整个DR16结构体放在DMA可访问的SRAM区域 */
static DR16_t dr16 __attribute__((section(".dma_buffer"))) __attribute__((aligned(32)));
/* USER STRUCT END */
/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
void Task_rc(void *argument) {
(void)argument; /* 未使用argument消除警告 */
/* 计算任务运行到指定频率需要等待的tick数 */
const uint32_t delay_tick = osKernelGetTickFreq() / RC_FREQ;
osDelay(RC_INIT_DELAY); /* 延时一段时间再开启任务 */
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
/* USER CODE INIT BEGIN */
/* 初始化DR16遥控器 */
DR16_Init(&dr16);
/* USER CODE INIT END */
while (1) {
tick += delay_tick; /* 计算下一个唤醒时刻 */
/* USER CODE BEGIN */
/* 启动DMA接收 */
DR16_StartDmaRecv(&dr16);
/* 等待DMA接收完成超时时间20ms */
if (DR16_WaitDmaCplt(20)) {
/* DMA接收成功解析数据 */
DR16_ParseData(&dr16);
} else {
/* DMA接收超时标记离线 */
DR16_Offline(&dr16);
}
/* USER CODE END */
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
}
}

View File

@ -13,4 +13,9 @@ const osThreadAttr_t attr_ai = {
.name = "ai", .name = "ai",
.priority = osPriorityNormal, .priority = osPriorityNormal,
.stack_size = 256 * 4, .stack_size = 256 * 4,
};
const osThreadAttr_t attr_rc = {
.name = "rc",
.priority = osPriorityNormal,
.stack_size = 256 * 4,
}; };

View File

@ -14,10 +14,12 @@ extern "C" {
/* Exported constants ------------------------------------------------------- */ /* Exported constants ------------------------------------------------------- */
/* 任务运行频率 */ /* 任务运行频率 */
#define AI_FREQ (500.0) #define AI_FREQ (500.0)
#define RC_FREQ (500.0)
/* 任务初始化延时ms */ /* 任务初始化延时ms */
#define TASK_INIT_DELAY (100u) #define TASK_INIT_DELAY (100u)
#define AI_INIT_DELAY (0) #define AI_INIT_DELAY (0)
#define RC_INIT_DELAY (0)
/* Exported defines --------------------------------------------------------- */ /* Exported defines --------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */
@ -28,6 +30,7 @@ typedef struct {
/* 各任务,也可以叫做线程 */ /* 各任务,也可以叫做线程 */
struct { struct {
osThreadId_t ai; osThreadId_t ai;
osThreadId_t rc;
} thread; } thread;
/* USER MESSAGE BEGIN */ /* USER MESSAGE BEGIN */
@ -50,16 +53,19 @@ typedef struct {
/* 各任务的stack使用 */ /* 各任务的stack使用 */
struct { struct {
UBaseType_t ai; UBaseType_t ai;
UBaseType_t rc;
} stack_water_mark; } stack_water_mark;
/* 各任务运行频率 */ /* 各任务运行频率 */
struct { struct {
float ai; float ai;
float rc;
} freq; } freq;
/* 任务最近运行时间 */ /* 任务最近运行时间 */
struct { struct {
float ai; float ai;
float rc;
} last_up_time; } last_up_time;
} Task_Runtime_t; } Task_Runtime_t;
@ -70,10 +76,12 @@ extern Task_Runtime_t task_runtime;
/* 初始化任务句柄 */ /* 初始化任务句柄 */
extern const osThreadAttr_t attr_init; extern const osThreadAttr_t attr_init;
extern const osThreadAttr_t attr_ai; extern const osThreadAttr_t attr_ai;
extern const osThreadAttr_t attr_rc;
/* 任务函数声明 */ /* 任务函数声明 */
void Task_Init(void *argument); void Task_Init(void *argument);
void Task_ai(void *argument); void Task_ai(void *argument);
void Task_rc(void *argument);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -38,8 +38,8 @@ Bdma.SPI6_TX.0.SyncSignalID=NONE
CAD.formats= CAD.formats=
CAD.pinconfig= CAD.pinconfig=
CAD.provider= CAD.provider=
CORTEX_M7.CPU_DCache=Enabled CORTEX_M7.CPU_DCache=Disabled
CORTEX_M7.CPU_ICache=Enabled CORTEX_M7.CPU_ICache=Disabled
CORTEX_M7.Enable-Cortex_Memory_Protection_Unit_Region0_Settings=MPU_REGION_ENABLE CORTEX_M7.Enable-Cortex_Memory_Protection_Unit_Region0_Settings=MPU_REGION_ENABLE
CORTEX_M7.IPParameters=CPU_DCache,CPU_ICache,MPU_Control,Enable-Cortex_Memory_Protection_Unit_Region0_Settings,default_mode_Activation CORTEX_M7.IPParameters=CPU_DCache,CPU_ICache,MPU_Control,Enable-Cortex_Memory_Protection_Unit_Region0_Settings,default_mode_Activation
CORTEX_M7.MPU_Control=__NULL CORTEX_M7.MPU_Control=__NULL
@ -900,3 +900,4 @@ VP_MEMORYMAP_VS_MEMORYMAP.Signal=MEMORYMAP_VS_MEMORYMAP
VP_SYS_VS_tim4.Mode=TIM4 VP_SYS_VS_tim4.Mode=TIM4
VP_SYS_VS_tim4.Signal=SYS_VS_tim4 VP_SYS_VS_tim4.Signal=SYS_VS_tim4
board=custom board=custom
rtos.0.ip=FREERTOS