4. FreeRTOS application

4.1. Purpose of the experiment

Based on the program of "Key Control Buzzer Whistle", the function is imported into the FreeRTOS system to run, to detect the state of KEY1 on the expansion board, and to control the buzzer to whistle. Press the button once, the buzzer will beep (every 200 milliseconds), press the button again, the buzzer will be turned off.

4.2. Configure FreeRTOS information

Since each new project needs configuration information, it is more troublesome. Fortunately, STM32CubeIDE provides the function of importing .ioc files, which can help us save time.

  1. Import the ioc file from the BEEP project and name it FreeRTOS.
  2. Click Middleware->FreertOS, select CMSIS_V1, click Tasks and Queues, there will be one task by default, and then create two new tasks, one of which manages the buzzer and the other manages the buttons.

image-20220309162641173

  1. The buzzer task information is shown in the following figure:

image-20220309163505486

Task Name: The task name.

Priority: Set the priority.

Stack Size: heap space, the size can be modified according to the actual situation.

Entry Function: The task function entity.

Code Generation Option: Code generation configuration, the default is weak to generate task entities, you can choose external not to generate task entities.

Parameter: The task parameter.

Allocation: You can choose Dynamic dynamic allocation or Static static allocation.

Buffer Name: The statically allocated buff name.

Control Block Name: Statically allocated block name.

The key task is the same, just with a different name.

4.3. Analysis of the experimental flow chart

image-20220519094856908

4.4. core code explanation

  1. Create new buzzer driver library bsp_task.h and bsp_task.c files in BSP. Add the following to bsp_task.h:

image-20220309165543608

Among them, the Task_Entity_LED() function manages the LED lights, the Task_Entity_Beep() manages the buzzer, and the Task_Entity_Key() manages the buttons.

image-20220309170805909

image-20220309170833361

image-20220309170852080

  1. Introduce bsp.h into the freertos.c file, find the corresponding entity functions of the three tasks, and call the task functions we manually created.

image-20220309170527769

image-20220309170552390

image-20220309170613454

4.5. Hardware connection

The LED light, key KEY1 and buzzer in FreeRTOS application are all onboard components and do not need to be connected manually.

4.6. Experimental effect

After the program is programmed, the LED light flashes every 200 milliseconds, press the button, the buzzer beeps (every 200 milliseconds), press the button again, the buzzer turns off.