FreeRTOS--learning (1)--STM32+CubeMx configuration

background:

The recent project is to transplant FreeRTOS lightweight system on STM32L152. This article will start with the introductory knowledge of FreeRTOS and record some basic knowledge points and learning experience of FreeRTOS.

Hardware platform: STM32L152, remarks: PA12 is connected to LED1, PA11 is connected to LED2;

Software platform: keil v5 and cubeMx.

content:

1. Introduction to FreeRTOS

FreeRTOS is a lightweight real-time operating system. RTOS: Real Time OperatingSystem real-time operating system. FreeRTOS can be divided into Free + RTOS, the former Free represents the name of an operating system type, and the latter represents the real-time operating system.

In recent years, the ranking of FreeRTOS is relatively high in the ranking of embedded operating systems, and there is a rising trend.

2. FreeRTOS generation and task creation in cubeMX

First, open the CUBEMX software, click NEW Project, and select the chip STM32L152RC;

 

2. Configure the RCC clock

3. Set PA12 and PA11 to GPIO_OUTPUT;

4. Enable FREERTOS;

5. Set the clock tree. In this example, the external crystal oscillator is 8M, 8 times the frequency, 2 times the frequency, and 32M is obtained;

6. Configure FREERTOS and create two tasks;

7, Generate code based on Keil V5.

8. Add LED lighting and extinguishing procedures;

 Compile and run, you can see that LED1 and LED2 blink at different frequencies.

The following focuses on analyzing the generated code:

Let's analyze the MX_FREERTOS_Init() function;

void MX_FREERTOS_Init (void) 

{   osThreadDef(Task_LED1, Func_LED1, osPriorityNormal, 0, 128);//Macro definition defines an osThreadDef_t type structure named os_thread_def_Task_LED1 and assigns it to each member variable.   Task_LED0Handle = osThreadCreate(osThread(Task_LED1), NULL);//Create LED1 task   osThreadDef(Task_LED2, Func_LED2, osPriorityNormal, 0, 128);//Macro definition, define an osThreadDef_t type structure named os_thread_def_Task_LED2 and assign it Give each member variable.   Task_LED1Handle = osThreadCreate(osThread(Task_LED2), NULL);//Created LED2 task }




  



go to definition of osThreadDef, the macro is defined as follows

#define osThreadDef(name, thread, priority, instances, stacksz)  \
const osThreadDef_t os_thread_def_##name = \
{ #name, (thread), (priority), (instances), (stacksz)}
#endif

The ## and # symbols appear in the definition of the appeal macro, where

 

## is a connection symbol used to connect parameters together;   

# Means "stringification". The # appearing in the macro definition converts the following parameter into a string.

 E.g:

#define paster( n ) printf( "token" #n" = %d\n ", token##n )

So paster(9); is equivalent to printf("token 9 = %d\n",token9);

Then osThreadDef(Task_LED1, Func_LED1, osPriorityNormal, 0, 128); is equivalent to the following definition

#defineosThreadDef(Task_LED1, Func_LED1, osPriorityNormal, 0, 128) \
const osThreadDef_t os_thread_def_Task_LED1 = { Task_LED1,Func_LED1,osPriorityNormal, 0, 128}
#endif

to sum up:

In this example, the two task functions Func_LED1 and Func_LED2, they actually occupy very little CPU time. After calling the osDelay() function, they enter the blocking state, and they are waiting for the "timed time up" event. When all user tasks enter the blocking state, idle tasks are running. Idle tasks are automatically created when the scheduler is started.

In the debugging process, after trying to change the osDelay of the two task functions to 500, the observed phenomenon is that LED1 and LED2 flash at the same frequency at the same time, but this is the same as that at any time in FreeRTOS, only one task is running. There is no contradiction in theories that two tasks are running at the same time. Later analysis found that after calling the osDelay() function, they entered the blocking state. When 500ms arrived, LED1 first switched to the running state, then LED2 switched to the running state, and then entered the configuration to wait for the next 500ms. The time between LED1 running and switching to LED2 is very short, so that on the surface, LED1 and LED2 are running at the same time, but in fact they are still switching between two tasks.

 

Reprinted: https://blog.csdn.net/u014470361/article/details/78870361?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog- BlogCommendFromBaidu-3.control

Guess you like

Origin blog.csdn.net/zwb_578209160/article/details/110443677