Follow+Star Public Account Number, don’t miss wonderful content
Author | strongerHuang
WeChat Public Account | strongerHuang
The content of the “FreeRTOSConfig.h” configuration in FreeRTOS increases with the addition of versions (features), meaning that as system functionality increases, the corresponding configuration information also increases.
However, in order to maintain compatibility with previous code, the system itself has made compatibility provisions. For example: configUSE_QUEUE_SETS is an option defined in “FreeRTOSConfig.h”; if the user does not define it, there is a similar definition in “FreeRTOS.h”.
When designing the FreeRTOS system, engineers must have thought about minimizing the development difficulty for developers. When we first learn to port FreeRTOS, most configurations in “FreeRTOSConfig.h” can be chosen as defaults. Only after becoming familiar with it and needing to use more features later, must we thoroughly understand the various configurations inside.For friends who are not averse to English, it is recommended to refer to the official website, as the official materials are the most authoritative.
Official website address:
http://www.freertos.org/a00110.html
General Configuration
Basic configuration is a set of configurations that we need to define, and it is also quite important.The FreeRTOSConfig.h configuration file involves many terms related to the system, which may be difficult for many beginners to understand. For example: semaphores, message queues, mutexes, event groups, etc.But don’t worry, just have a general understanding, I will focus on explaining it later.1.configUSE_PREEMPTIONScheduling Mode ConfigurationSet to 0: cooperative scheduling, i.e., time slices are executed in turn;Set to 1: preemptive scheduling, i.e., tasks with higher priority execute first;Since we require real-time response, set it to 1, using preemptive scheduling. Otherwise, it will not fulfill the role of a real-time operating system.2.configCPU_CLOCK_HZCPU Clock, which is commonly referred to as the main frequency. Note: the unit is Hz.For example: the main frequency of STM32F407 is 168M:
#define configCPU_CLOCK_HZ (168000000)
3.configTICK_RATE_HZSystem Tick, which is the number of ticks per second, can be said to be the heartbeat of the system, but it needs to be distinguished from the main frequency. The value of the system tick should be based on the CPU main frequency; generally, the higher the main frequency, the larger the value, usually between 100 and 1000.For example: the system tick determines vTaskDelay.For example:#define configTICK_RATE_HZ (1000)Then:vTaskDelay(1000), which indicates a delay of 1S.4.configMAX_PRIORITIESMaximum System Priority ValueWhen we create tasks, the configured priority value cannot exceed this maximum value.xTaskCreate(vAppTask1, “Task1”, TASK1_STACK_SIZE, NULL, TASK1_PRIORITY, NULL);Tip:a. The system priority is similar to interrupt priority; higher priority will preempt lower priority, but the application scenarios of system and interrupt priorities need to be distinguished.b. In FreeRTOS, the larger the priority value, the higher the priority. In UCOS, it is the opposite.5.configMINIMAL_STACK_SIZEMinimum Stack SizeIn the system, it is generally used for idle, timing, and other system tasks; of course, we can also use this defined stack size in some places.Note the unit of the value, generally 4 bytes in ARM.6.configTOTAL_HEAP_SIZETotal Heap (Stack) Size of the SystemWe need to define this value based on the usage situation. It cannot be defined too small, as too small memory is prone to overflow;It cannot be defined too large; some chips have limited RAM (some only have a few K), and if it is too large, we won’t be able to define many global variables or allocate other stack space.7.configMAX_TASK_NAME_LENMaximum Length of Task NameThis is the string length of the task name defined when creating a task.xTaskCreate(vAppTask1, “Task1“, TASK1_STACK_SIZE, NULL, TASK1_PRIORITY, NULL);Tip: The terminator ‘\0’ is also included.8.configUSE_16_BIT_TICKSWhether to use 16-bit tick count valueSet to 0: then use a 32-bit tick count value, which is generally set to 0 in 32-bit processors;Set to 1: then use a 16-bit tick count value, generally set to 1 in 8-bit or 16-bit processors.9.configIDLE_SHOULD_YIELDWhether to let idle tasks “yield” preemptionThat is, during the execution of tasks with the same priority as the idle task, whether the idle task has the opportunity to preempt.Set to 0: do not yield preemption;Set to 1: yield preemption;10.configUSE_MUTEXESWhether to use mutexesSet to 0: do not useSet to 1: useTip: Mutexes are also called mutex semaphores, which means “locking” the resource. Their role is to achieve exclusive processing of shared resources among multiple tasks. In simple terms, at any given time, a resource is only allowed to be processed by one task, and only after processing is completed can other tasks process that resource.For example: Task A has a high priority, and Task B has a low priority; both tasks A and B will use a serial port to send command data (i.e., it must be completed each time, and cannot be interrupted halfway). When Task B is sending data, Task A is in a ready state (ready to interrupt Task B). Then Task B needs to use a mutex to occupy that serial port (lock, occupy that resource); after sending the command, it releases that serial port (unlock, release that resource). Once the resource is released, Task A can use that serial port (resource).11.configUSE_RECURSIVE_MUTEXESWhether to use recursive mutexesSet to 0: do not useSet to 1: use13.configQUEUE_REGISTRY_SIZE(*)Number of queue names that can be added (or registered)This configuration information is difficult to translate; it mainly works with the functions vQueueAddToRegistry and vQueueUnregisterQueue.Directly to the function interface:
From the function interface, one function registers (an already created) queue name; one function unregisters the queue name;In fact, the main purpose is to name (already created) queues for easier debugging.Tip: Many beginners misunderstand this as “the maximum number of queues that can be created”; this configuration parameter is completely different in concept.14.configUSE_QUEUE_SETS(*)Whether to use message queue “SET” functionalitySet to 0: do not useSet to 1: useThis configuration information is also relatively difficult to understand.Tip: Many online explanations are: enabling/disabling message queues. This understanding is too general, and there is a lot of information related to message queue configuration; I personally think it is incorrect.15.configUSE_TIME_SLICINGWhether to use time slicing for schedulingThis parameter is used together with the above configuration parameter configUSE_PREEMPTION.This configuration parameter was added in later versions; it seems that it was not available before version V7. Therefore, it is not default in the FreeRTOSConfig.h configuration file but is defined in FreeRTOS.h.