FreeRTOSFreeRTOS学习笔记— 对F407ZGT6移植FreeRTOS _CMSIS API_实例
Posted 果果小师弟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FreeRTOSFreeRTOS学习笔记— 对F407ZGT6移植FreeRTOS _CMSIS API_实例相关的知识,希望对你有一定的参考价值。
接着上一章的继续
FreeRTOS.h
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
//存放 defaultTask 任务(线程)的线程 ID也就是句柄,ID(句柄) 为线程的唯一识别号
osThreadId defaultTaskHandle;
osThreadId myTask02Handle;//存放 myTask02 线程的线程 ID(句柄)
void StartDefaultTask(void const * argument);//线程函数声明
void StartTask02(void const * argument);//线程函数声明
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
*ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
*ppxIdleTaskStackBuffer = &xIdleStack[0];
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}
/**
* @brief FreeRTOS initialization 由 main 函数调用
* @param None
* @retval None
*/
void MX_FREERTOS_Init(void) {
/* definition and creation of defaultTask */
/* 定义线程的数据结构,然后使用该数据结构创建线程,创建好后就进入 READY 等待
*调度 */
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
/* 使用线程数据结构创建 defaultTask 线程,线程 ID 保存到 defaultTaskHandle,
* 至此 StartDefaultTask 就被注册为了线程函数,不再是一个普通的函数。*/
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
/* 定义线程的数据结构,然后使用该数据结构创建线程,创建好后就进入 READY
*等待调度 */
osThreadDef(myTask02, StartTask02, osPriorityNormal, 0, 128);
/* 创建线程,将 StartTask02 注册为线程函数 */
myTask02Handle = osThreadCreate(osThread(myTask02), NULL);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
}
/* StartDefaultTask 线程函数,被注册为线程函数,运行线程就是运行线程函数中的指令 */
void StartDefaultTask(void const * argument)
{
for(;;)
{
osDelay(1);
}
}
/* StartTask02 线程函数 */
void StartTask02(void const * argument)
{
for(;;)
{
osDelay(1);
}
}
mian.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "cmsis_os.h"
#include "gpio.h"
void SystemClock_Config(void);
void MX_FREERTOS_Init(void);
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
HAL_Init();
SystemClock_Config();//时钟初始化
MX_GPIO_Init();//gpio 初始化
/* Call init function for freertos objects (in freertos.c) */
MX_FREERTOS_Init(); //初始化 freeRtos,前面介绍过这个函数,这个函数的作用就是创建线程(任务)
/* Start scheduler */
/* 启动 RTOS,其实就是启动“任务管理器”,启动之后任务管理器就开始调度线程,
* 此时 pc(程序计数器)就会指向某线程的指令,开始多线程并发运行。
* 如果没有创建多线程的话,那就只有一个线程。*/
osKernelStart();
/* USER CODE BEGIN WHILE */
/* 由于调用了 osKernelStart 之后,PC 就指向了线程中的指令,因此 osKernelStart 后
* 面代码并不会被 CPU 执行,所以 osKernelStart 后的代码没有意义。 */
while (1)
{
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief Period elapsed callback in non blocking mode
* @note This function is called when TIM1 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
* @retval None
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM1) {
HAL_IncTick();
}
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\\r\\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
前面说过,并发运行线程其实就是在并发运行这两个线程函数的代码。我们在StartDefaultTask
线程函数中添加按键代码,在StartTask02
线程函数中添加控制灯的代码。
StartDefaultTask:负责检测按键,按键按下后将键值设置到全局变量中。
StartTask02:负责控制 LED,当通过全局变量检测到按键按下了,就点亮LED
。
KEY1—>控制 LED0
KEY2—>控制 LED1
由于两个线程是并发运行的,因此相互间互不干扰,按键控制 LED 的例子很简单,其实完全没有使用两个任务的必要,这里之所以使用两个任务是为了演示多线程是如何并发运行的,例子简单往往越能说明道理,为了能够方便观察,我们也加入RTT串口打印,因此我们需要工程中添加RTT的源码。关于如何使用RTT的请看:手把手教你把JLink变成串口调试助手
/* USER CODE BEGIN Header */
/**
******************************************************************************
* File Name : freertos.c
* Description : Code for freertos applications
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "SEGGER_RTT.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
uint8_t kValue = 0;
/* USER CODE END Variables */
osThreadId defaultTaskHandle;
osThreadId myTask02Handle;
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
uint8_t KEY_Scan(void);
/* USER CODE END FunctionPrototypes */
void StartDefaultTask(void const * argument);
void StartTask02(void const * argument);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/**
* @brief FreeRTOS initialization
* @param None
* @retval None
*/
void MX_FREERTOS_Init(void) {
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */
/* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */
/* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */
/* start timers, add new ones, ... */
/* USER CODE END RTOS_TIMERS */
/* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */
/* USER CODE END RTOS_QUEUES */
/* Create the thread(s) */
/* definition and creation of defaultTask */
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
/* definition and creation of myTask02 */
osThreadDef(myTask02, StartTask02, osPriorityNormal, 0, 128);
myTask02Handle = osThreadCreate(osThread(myTask02), NULL);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
}
/* USER CODE BEGIN Header_StartDefaultTask */
/**
* @brief Function implementing the defaultTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void const * argument)
{
/* USER CODE BEGIN StartDefaultTask */
/* Infinite loop */
for(;;)
{
kValue = KEY_Scan();
osDelay(200);
}
/* USER CODE END StartDefaultTask */
}
/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the myTask02 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void const * argument)
{
/* USER CODE BEGIN StartTask02 */
/* Infinite loop */
static uint8_t flag1 = 0;
static uint8_t flag2 = 0;
for(;;)
{
if(kValue == 1)
{
SEGGER_RTT_printf(0, "k1 被按下了\\r\\n");
if(flag1 == 0) //灯关着就打开
{
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_SET);
flag1 = 1;
}
else if(flag1 == 1) //开着就关闭
{
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
flag1 = 0;
}
}
else if(kValue == 2)
{
SEGGER_RTT_printf(0, "k2 被按下了\\r\\n");
if(flag2 == 0) //灯关着就打开
{
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
flag2 = 1;
}
else if(flag2 == 1) //开着就关闭
{
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
flag2 = 0;
}
}
osDelay(200);
}
/* USER CODE END StartTask02 */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
/* 检测k1、k2按键是否按下,并返回各自的键值,这里将k1的键值设定为1,k2的设定为2 */
uint8_t KEY_Scan(void)
{
//没有key_up,会导致按下按键再松开之前,多次调用KEY_Scan时,每次都会检测到按键
//被按下了,有了key_up,第一次调用KEY_Scan时返回键值,后面几次调用时会通过
//key_up检测到已经返回过一次键值了,不再返回键值
static uint8_t key_up = 0;
int KEY1 = HAL_GPIO_ReadPin(KEY1_GPIO_Port, GPIO_PIN_3);
int KEY2 = HAL_GPIO_ReadPin(KEY2_GPIO_Port, GPIO_PIN_4);
if((key_up == 0) && (KEY1 == 0 || KEY2 == 0)) //检测到刚按下进入,如果是按住不放不会进入
{
osDelay(100); //去抖动
key_up = 1; //设置标志位,表示按下
if(KEY1 == 0)return 1; //如果k1按下就返回1
else if(KEY2 == 0)return 2; //如果k2按下就返回2
}
else if(KEY1 == 1 && KEY2 == 1) key_up = 0; //按键松开,清标志位
return 0; //无按键按下或松开了时就返回0
}
/* USER CODE END Application */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
执行流程
时钟初始化——>GPIO初始化——>MX_FREERTOS_Init
创建任务(线程)——>osKernelStart
启动OS(启动任务管理器)——>PC 指向某个线程,然后在所有的线程之间切换执行,如此就实现了多任务并发运行。
https://gitee.com/zhiguoxin/Wechat-Data.git
以上是关于FreeRTOSFreeRTOS学习笔记— 对F407ZGT6移植FreeRTOS _CMSIS API_实例的主要内容,如果未能解决你的问题,请参考以下文章
FreeRTOSFreeRTOS学习笔记— 使用STM32CubeMX对F407ZGT6移植FreeRTOS(CMSIS API)
FreeRTOSFreeRTOS学习笔记— 中断+临界区的保护
FreeRTOSFreeRTOS学习笔记— 手写FreeRTOS双向链表/源码分析
FreeRTOSFreeRTOS学习笔记— 开始创建任务并测试任务代码