特性:实现电机控制与霍尔传感器功能

新增电机板级支持包:基于 AT8236-MS 驱动芯片,实现了对 4 个直流电机的 PWM 控制。
实现霍尔传感器功能:用于速度测量和脉冲计数。
更新 GPIO 初始化:为霍尔传感器添加了外部中断(EXTI)配置。
修改系统时钟配置:改用高速外部时钟(HSE)并调整了锁相环(PLL)设置。
更改定时器配置:将时基生成从 TIM8 改为 TIM4。
增强 FreeRTOS 任务:实现从霍尔传感器周期性读取并更新速度数据。
更新项目配置:以反映外设使用情况和优先级的变更。
This commit is contained in:
2026-04-03 20:24:55 +08:00
parent a53aa38ed3
commit 1cc8327f13
17 changed files with 682 additions and 149 deletions

View File

@@ -1,20 +1,20 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* File Name : freertos.c
* Description : Code for freertos applications
******************************************************************************
* @attention
*
* Copyright (c) 2026 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
******************************************************************************
* File Name : freertos.c
* Description : Code for freertos applications
******************************************************************************
* @attention
*
* Copyright (c) 2026 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
@@ -25,11 +25,14 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp_uart.h"
#include "bsp_beep.h"
#include "usart.h"
#include "bsp_motor.h"
#include "bsp_uart.h"
#include "elog.h"
#include "protocol.h"
#include "usart.h"
#include "bsp_hall.h" // 添加对 bsp_hall.h 的包含
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -65,6 +68,13 @@ const osThreadAttr_t CarCtrlTask_attributes = {
.stack_size = 256 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for timerTask */
osThreadId_t timerTaskHandle;
const osThreadAttr_t timerTask_attributes = {
.name = "timerTask",
.stack_size = 512 * 4,
.priority = (osPriority_t) osPriorityBelowNormal,
};
/* Definitions for CmdQueue */
osMessageQueueId_t CmdQueueHandle;
const osMessageQueueAttr_t CmdQueue_attributes = {
@@ -73,7 +83,7 @@ const osMessageQueueAttr_t CmdQueue_attributes = {
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
#ifdef __GNUC__
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
@@ -87,6 +97,7 @@ PUTCHAR_PROTOTYPE {
void StartDefaultTask(void *argument);
void CarCtrl_Task(void *argument);
void speed_get(void *argument);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
@@ -100,7 +111,10 @@ void MX_FREERTOS_Init(void) {
easylogger_init();
BEEP_Init();
BSP_UART1_Init();
elog_raw("FreeRTOS Initialized\r\n");
elog_raw("FreeRTOS Initialized\r\n");
motor_init(); // 初始化电机相关外设
hall_init(); // 初始化霍尔传感器
/* USER CODE END Init */
@@ -132,6 +146,9 @@ void MX_FREERTOS_Init(void) {
/* creation of CarCtrlTask */
CarCtrlTaskHandle = osThreadNew(CarCtrl_Task, NULL, &CarCtrlTask_attributes);
/* creation of timerTask */
timerTaskHandle = osThreadNew(speed_get, NULL, &timerTask_attributes);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
@@ -144,46 +161,62 @@ void MX_FREERTOS_Init(void) {
/* USER CODE BEGIN Header_StartDefaultTask */
/**
* @brief Function implementing the initTask thread.
* @param argument: Not used
* @retval None
*/
* @brief Function implementing the initTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
/* USER CODE BEGIN StartDefaultTask */
/* Infinite loop */
for(;;)
{
for (;;) {
HAL_GPIO_TogglePin(RUN_LED_GPIO_Port, RUN_LED_Pin);
HAL_GPIO_TogglePin(RUN_LED_GPIO_Port, RUN_LED_Pin);
osDelay(1000);
}
/* USER CODE END StartDefaultTask */
}
/* USER CODE BEGIN Header_CarCtrl_Task */
/**
* @brief Function implementing the CarCtrlTask thread.
* @param argument: Not used
* @retval None
*/
* @brief Function implementing the CarCtrlTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_CarCtrl_Task */
__weak void CarCtrl_Task(void *argument)
{
/* USER CODE BEGIN CarCtrl_Task */
/* Infinite loop */
for(;;)
{
for (;;) {
osDelay(1); // osMessageQueueGet 已经是阻塞的,不需要额外的 osDelay
}
/* USER CODE END CarCtrl_Task */
}
/* USER CODE BEGIN Header_speed_get */
/**
* @brief Function implementing the timerTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_speed_get */
__weak void speed_get(void *argument)
{
/* USER CODE BEGIN speed_get */
/* Infinite loop */
for(;;)
{
osDelay(1);
}
/* USER CODE END speed_get */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

View File

@@ -105,6 +105,13 @@ void MX_GPIO_Init(void)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}
/* USER CODE BEGIN 2 */

View File

@@ -140,12 +140,13 @@ void SystemClock_Config(void)
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
@@ -178,7 +179,7 @@ void SystemClock_Config(void)
/**
* @brief Period elapsed callback in non blocking mode
* @note This function is called when TIM8 interrupt took place, inside
* @note This function is called when TIM4 interrupt took place, inside
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
* a global variable "uwTick" used as application time base.
* @param htim : TIM handle
@@ -189,7 +190,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
/* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */
if (htim->Instance == TIM8)
if (htim->Instance == TIM4)
{
HAL_IncTick();
}

View File

@@ -25,13 +25,13 @@
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim8;
TIM_HandleTypeDef htim4;
/* Private function prototypes -----------------------------------------------*/
void TIM8_IRQHandler(void);
void TIM4_IRQHandler(void);
/* Private functions ---------------------------------------------------------*/
/**
* @brief This function configures the TIM8 as a time base source.
* @brief This function configures the TIM4 as a time base source.
* The time source is configured to have 1ms time base with a dedicated
* Tick interrupt priority.
* @note This function is called automatically at the beginning of program after
@@ -42,54 +42,63 @@ void TIM8_IRQHandler(void);
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
RCC_ClkInitTypeDef clkconfig;
uint32_t uwTimclock = 0U;
uint32_t uwTimclock, uwAPB1Prescaler = 0U;
uint32_t uwPrescalerValue = 0U;
uint32_t pFLatency;
HAL_StatusTypeDef status = HAL_OK;
/* Enable TIM8 clock */
__HAL_RCC_TIM8_CLK_ENABLE();
/* Enable TIM4 clock */
__HAL_RCC_TIM4_CLK_ENABLE();
/* Get clock configuration */
HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
/* Compute TIM8 clock */
uwTimclock = HAL_RCC_GetPCLK2Freq();
/* Get APB1 prescaler */
uwAPB1Prescaler = clkconfig.APB1CLKDivider;
/* Compute TIM4 clock */
if (uwAPB1Prescaler == RCC_HCLK_DIV1)
{
uwTimclock = HAL_RCC_GetPCLK1Freq();
}
else
{
uwTimclock = 2UL * HAL_RCC_GetPCLK1Freq();
}
/* Compute the prescaler value to have TIM8 counter clock equal to 1MHz */
/* Compute the prescaler value to have TIM4 counter clock equal to 1MHz */
uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
/* Initialize TIM8 */
htim8.Instance = TIM8;
/* Initialize TIM4 */
htim4.Instance = TIM4;
/* Initialize TIMx peripheral as follow:
* Period = [(TIM8CLK/1000) - 1]. to have a (1/1000) s time base.
* Period = [(TIM4CLK/1000) - 1]. to have a (1/1000) s time base.
* Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
* ClockDivision = 0
* Counter direction = Up
*/
htim8.Init.Period = (1000000U / 1000U) - 1U;
htim8.Init.Prescaler = uwPrescalerValue;
htim8.Init.ClockDivision = 0;
htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim4.Init.Period = (1000000U / 1000U) - 1U;
htim4.Init.Prescaler = uwPrescalerValue;
htim4.Init.ClockDivision = 0;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
status = HAL_TIM_Base_Init(&htim8);
status = HAL_TIM_Base_Init(&htim4);
if (status == HAL_OK)
{
/* Start the TIM time Base generation in interrupt mode */
status = HAL_TIM_Base_Start_IT(&htim8);
status = HAL_TIM_Base_Start_IT(&htim4);
if (status == HAL_OK)
{
/* Enable the TIM8 global Interrupt */
HAL_NVIC_EnableIRQ(TIM8_UP_IRQn);
/* Enable the TIM4 global Interrupt */
HAL_NVIC_EnableIRQ(TIM4_IRQn);
/* Configure the SysTick IRQ priority */
if (TickPriority < (1UL << __NVIC_PRIO_BITS))
{
/* Configure the TIM IRQ priority */
HAL_NVIC_SetPriority(TIM8_UP_IRQn, TickPriority, 0U);
HAL_NVIC_SetPriority(TIM4_IRQn, TickPriority, 0U);
uwTickPrio = TickPriority;
}
else
@@ -105,25 +114,25 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
/**
* @brief Suspend Tick increment.
* @note Disable the tick increment by disabling TIM8 update interrupt.
* @note Disable the tick increment by disabling TIM4 update interrupt.
* @param None
* @retval None
*/
void HAL_SuspendTick(void)
{
/* Disable TIM8 update Interrupt */
__HAL_TIM_DISABLE_IT(&htim8, TIM_IT_UPDATE);
/* Disable TIM4 update Interrupt */
__HAL_TIM_DISABLE_IT(&htim4, TIM_IT_UPDATE);
}
/**
* @brief Resume Tick increment.
* @note Enable the tick increment by Enabling TIM8 update interrupt.
* @note Enable the tick increment by Enabling TIM4 update interrupt.
* @param None
* @retval None
*/
void HAL_ResumeTick(void)
{
/* Enable TIM8 Update interrupt */
__HAL_TIM_ENABLE_IT(&htim8, TIM_IT_UPDATE);
/* Enable TIM4 Update interrupt */
__HAL_TIM_ENABLE_IT(&htim4, TIM_IT_UPDATE);
}

View File

@@ -61,7 +61,7 @@ extern DMA_HandleTypeDef hdma_usart1_tx;
extern DMA_HandleTypeDef hdma_usart2_tx;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern TIM_HandleTypeDef htim8;
extern TIM_HandleTypeDef htim4;
/* USER CODE BEGIN EV */
@@ -207,6 +207,36 @@ void DMA1_Channel7_IRQHandler(void)
/* USER CODE END DMA1_Channel7_IRQn 1 */
}
/**
* @brief This function handles EXTI line[9:5] interrupts.
*/
void EXTI9_5_IRQHandler(void)
{
/* USER CODE BEGIN EXTI9_5_IRQn 0 */
/* USER CODE END EXTI9_5_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(IOR_Pin);
HAL_GPIO_EXTI_IRQHandler(M_I_2_Pin);
HAL_GPIO_EXTI_IRQHandler(M_I_1_Pin);
/* USER CODE BEGIN EXTI9_5_IRQn 1 */
/* USER CODE END EXTI9_5_IRQn 1 */
}
/**
* @brief This function handles TIM4 global interrupt.
*/
void TIM4_IRQHandler(void)
{
/* USER CODE BEGIN TIM4_IRQn 0 */
/* USER CODE END TIM4_IRQn 0 */
HAL_TIM_IRQHandler(&htim4);
/* USER CODE BEGIN TIM4_IRQn 1 */
/* USER CODE END TIM4_IRQn 1 */
}
/**
* @brief This function handles USART1 global interrupt.
*/
@@ -240,17 +270,18 @@ void USART2_IRQHandler(void)
}
/**
* @brief This function handles TIM8 update interrupt.
* @brief This function handles EXTI line[15:10] interrupts.
*/
void TIM8_UP_IRQHandler(void)
void EXTI15_10_IRQHandler(void)
{
/* USER CODE BEGIN TIM8_UP_IRQn 0 */
/* USER CODE BEGIN EXTI15_10_IRQn 0 */
/* USER CODE END TIM8_UP_IRQn 0 */
HAL_TIM_IRQHandler(&htim8);
/* USER CODE BEGIN TIM8_UP_IRQn 1 */
/* USER CODE END EXTI15_10_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(M_I_4_Pin);
HAL_GPIO_EXTI_IRQHandler(M_I_3_Pin);
/* USER CODE BEGIN EXTI15_10_IRQn 1 */
/* USER CODE END TIM8_UP_IRQn 1 */
/* USER CODE END EXTI15_10_IRQn 1 */
}
/* USER CODE BEGIN 1 */

View File

@@ -46,10 +46,10 @@ void MX_TIM1_Init(void)
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 65535;
htim1.Init.Period = 3599;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
{
Error_Handler();
@@ -117,9 +117,9 @@ void MX_TIM2_Init(void)
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 65535;
htim2.Init.Period = 3599;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
{
Error_Handler();