在《按键控制蜂鸣器鸣笛》的程序基础上,将功能导入到FreeRTOS系统上跑,实现检测扩展板上的KEY1状态,控制蜂鸣器鸣笛。按一下按键,蜂鸣器滴滴滴响(每200毫秒响一次),再按一次按键,蜂鸣器关闭。
配置FreeRTOS信息
1.从BEEP的项目里导入ioc文件,并命名为FreeRTOS。
2.点击Middleware->FREERTOS,选择CMSIS_V1,点击Tasks and Queues,这里默认这里会有一个任务,再新建两个任务,其中一个管理蜂鸣器,另一个管理按键。
3.蜂鸣器任务信息如下图所示:
Task Name:任务名称。
Priority:设置优先级。
Stack Size:堆空间,可根据实际修改大小。
Entry Function:任务函数实体。
Code Generation Option:代码生成配置,默认是weak生成任务实体,可选择external不生成任务实体。
Parameter:任务参数。
Allocation:可选择Dynamic动态分配或者Static静态分配。
Buffer Name:静态分配的buff名称。
Control Block Name:静态分配的块名称。
按键任务也是一样,只是名称不同而已。
核心代码解释
1.在BSP中新建蜂鸣器的驱动库bsp_task.h和bsp_task.c文件。在bsp_task.h中增加以下内容:
cpp
/*
* bsp_task.h
*
* Created on: Mar 5, 2022
* Author: Administrator
*/
#ifndef BSP_TASK_H_
#define BSP_TASK_H_
void Task_Entity_LED(void);
void Task_Entity_Beep(void);
void Task_Entity_Key(void);
#endif /* BSP_TASK_H_ */
其中Task_Entity_LED()函数管理LED灯,Task_Entity_Beep()管理蜂鸣器,Task_Entity_Key()管理按键。
cpp
/*
* bsp_task.c
*
* Created on: Mar 5, 2022
* Author: Administrator
*/
#include "bsp.h"
int enable_beep = 0;
// LED light task entity function LED灯任务实体函数
void Task_Entity_LED(void)
{
while (1)
{
// The indicator lights up every 100 milliseconds 指示灯每隔100毫秒亮一次
LED_TOGGLE();
osDelay(100);
}
}
// Buzzer task entity function 蜂鸣器任务实体函数
void Task_Entity_Beep(void)
{
while (1)
{
if (enable_beep)
{
// The buzzer goes off every 200 milliseconds 蜂鸣器每200毫秒响一次
BEEP_ON();
osDelay(100);
BEEP_OFF();
osDelay(100);
}
else
{
BEEP_OFF();
osDelay(100);
}
}
}
// Key task entity function 按键任务实体函数
void Task_Entity_Key(void)
{
while (1)
{
if (Key1_State(1) == KEY_PRESS)
{
// Button controls the buzzer switch 按键控制蜂鸣器开关
enable_beep = !enable_beep;
}
osDelay(10);
}
}
2.在freertos.c文件中引入bsp.h,找到对应的三个任务的实体函数,并且分别调用我们手动建立的任务函数。
cpp
/* USER CODE BEGIN Header */
/**
******************************************************************************
* File Name : freertos.c
* Description : Code for freertos applications
******************************************************************************
* @attention
*
* Copyright (c) 2022 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 ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp.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 */
osThreadId beepTaskHandle;
/* USER CODE END Variables */
osThreadId defaultTaskHandle;
osThreadId myTask_BEEPHandle;
osThreadId myTask_KEYHandle;
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
/* USER CODE END FunctionPrototypes */
void StartDefaultTask(void const * argument);
void StartTask_BEEP(void const * argument);
void StartTask_KEY(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;
/* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */
/**
* @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 myTask_BEEP */
osThreadDef(myTask_BEEP, StartTask_BEEP, osPriorityIdle, 0, 128);
myTask_BEEPHandle = osThreadCreate(osThread(myTask_BEEP), NULL);
/* definition and creation of myTask_KEY */
osThreadDef(myTask_KEY, StartTask_KEY, osPriorityIdle, 0, 128);
myTask_KEYHandle = osThreadCreate(osThread(myTask_KEY), 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(;;)
// {
// osDelay(1);
// }
Task_Entity_LED();
/* USER CODE END StartDefaultTask */
}
/* USER CODE BEGIN Header_StartTask_BEEP */
/**
* @brief Function implementing the myTask_BEEP thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask_BEEP */
void StartTask_BEEP(void const * argument)
{
/* USER CODE BEGIN StartTask_BEEP */
/* Infinite loop */
// for(;;)
// {
// osDelay(1);
// }
Task_Entity_Beep();
/* USER CODE END StartTask_BEEP */
}
/* USER CODE BEGIN Header_StartTask_KEY */
/**
* @brief Function implementing the myTask_KEY thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask_KEY */
void StartTask_KEY(void const * argument)
{
/* USER CODE BEGIN StartTask_KEY */
/* Infinite loop */
// for(;;)
// {
// osDelay(1);
// }
Task_Entity_Key();
/* USER CODE END StartTask_KEY */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
/* USER CODE END Application */
烧录程序后,LED灯每隔200毫秒闪一次,按一下按键,蜂鸣器滴滴滴响(每200毫秒响一次),再按一次按键,蜂鸣器关闭。