Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of Visual tracking and debugging of FreeRTOS Real-time operating system

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the method of visual tracking and debugging of FreeRTOS real-time operating system". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the method of visual tracking and debugging of FreeRTOS real-time operating system".

Preface

Programming with RTOS, allocating as much stack space for each task becomes a technical task: too much allocation wastes system resources, while less allocation may lead to stack overflow. Due to the existence of interrupts and preemptive schedulers, it is very difficult to estimate how much stack a task requires, so today we introduce a method to obtain the remaining stack space for each task. This paper takes NXP LPC177x_8x series microcontroller as an example.

Let's make this function into a command and add it to the list of command interpreters described in the article FreeRTOS implements the command line interpreter using task notification. When the program runs for a period of time, we enter the command "task" into the SecureCRT software and enter, and we can see the task information shown in figure 1-1. There are only two tasks, in which the number in the stack column represents the remaining stack space for the corresponding task, in units of type StackType_t, which is defined in the migration layer, which is generally defined as 4 bytes.

Figure 1-1: task information

1. Enable visual tracking and run-time statistics

As shown in figure 1-1, to implement stack usage information and CPU usage information, the two macros in the FreeRTOSConfig.h file must be set to 1:

# define configUSE_TRACE_FACILITY 1 # define configGENERATE_RUN_TIME_STATS 1

The first macro is used to enable the visual tracking function, and the second macro is used to enable the time statistics function. If the second macro is set to 1, the following two macros must be defined:

PortCONFIGURE_TIMER_FOR_RUN_TIME_STATS ():

The user program needs to provide a benchmark clock function, which completes the initialization of the benchmark clock function, which is define to the macro configuration _ TIMER_FOR_RUN_TIME_STATS ().

This is because run-time statistics require a benchmark timer with a higher resolution than the system beat interrupt frequency, otherwise, the statistics may not be accurate.

The interrupt frequency of the reference timer is 10 times faster than that of the system beat. The faster the interrupt frequency of the benchmark timer, the more accurate the statistics, but the shorter the running time that can be counted (for example, the benchmark timer 10ms interrupts once, the 8-bit unsigned shaping variable can count to 2.55 seconds, but if it is interrupted once per second, the 8-bit unsigned shaping variable can count to 255 seconds).

PortGET_RUN_TIME_COUNTER_VALUE ():

The user program needs to provide a function that returns the current "time" of the benchmark clock, which is define to the macro portGet _ RUN_TIME_COUNTER_VALUE ().

We use timer 1 to generate the reference clock, and the timer 1 initialization function is:

/ * initialize timer 1 for OS task run time statistics * / void init_timer1_for_runtime_state (void) {TIM_TIMERCFG_Type Timer0CfgType; Timer0CfgType.PrescaleOption=TIM_PRESCALE_USVAL; / / the unit of pre-division is microsecond Timer0CfgType.PrescaleValue=500; / / 500 microseconds after pre-division, TIM_Init (LPC_TIM1,TIM_TIMER_MODE,&Timer0CfgType) LPC_TIM1- > TCR=0x01;}

Timer 1 is configured to increment the value of the TC register every 500 microseconds. We use the TC register value of timer 1 as the current time of the reference clock. When the value of the TC register overflows, it takes about 24. 8 days, which is enough for our application.

In FreeRTOSConfig.h, define initializing the benchmark timer macro and getting the current time macro:

Extern void init_timer1_for_runtime_state (void); # define TIMER1_TC (* ((volatile uint32_t *) 0x40008008)) # define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS () init_timer1_for_runtime_state () # define portGET_RUN_TIME_COUNTER_VALUE () TIMER1_TC2. Get task information and format it

The status information for each task is obtained using the API function uxTaskGetSystemState (), which is defined as:

UBaseType_tuxTaskGetSystemState (TaskStatus_t * constpxTaskStatusArray, const UBaseType_tuxArraySize, unsigned long * constpulTotalRunTime)

The function uxTaskGetSystemState () fills the TaskStatus_t structure with relevant information, and the information of each task in the system can be populated into the TaskStatus_t structure array, and the array size is specified by uxArraySize. The structure TaskStatus_t is defined as follows:

The typedef struct xTASK_STATUS {/ * task handle * / TaskHandle_t xHandle; / * pointer to the task name * / const signed char * pcTaskName; / * task ID is a unique number * / UBaseType_t xTaskNumber; / * when filling the structure, the task's current state (run, ready, pending, etc.) * / eTaskState eCurrentState; / * the priority of the task running (or inheriting) when filling the structure. * / UBaseType_t uxCurrentPriority; / * when the priority of a task is changed due to inheritance, this variable holds the original priority of the task. Valid only if configUSE_MUTEXES is defined as 1. The total elapsed time assigned to the task by * / UBaseType_t uxBasePriority; / *. Valid only when the macro configGENERATE_RUN_TIME_STATS is 1. * / unsigned long ulRunTimeCounter; / * the minimum number of stacks left since the task is created. The closer this value is to 0, the more likely the stack overflow is. * / unsigned short usStackHighWaterMark;} TaskStatus_t

Note that this function is for trial only, and calling this function suspends all tasks until the function ends, so the task may be suspended for a long time. In the file FreeRTOSConfig.h, the macro configUSE _ TRACE_FACILITY must be set to 1 for this function to be valid.

Because we do not use a dynamic memory allocation strategy, the implementation defines the maximum number of tasks and pre-allocates an array of task status information:

# defineMAX_TASK_NUM 5TaskStatus_ tpxTaskStatusArray[MAX _ TASK_NUM]

After the function uxTaskGetSystemState () is called correctly, the information of the task will be placed in the TaskStatus_t structure, and we need to format the information into an easy-to-read form and print it to the screen through the serial port. The function that accomplishes these functions is called get_task_state (), and the code is as follows:

/ * get OS task information * / voidget_task_state (int32_t argc,void * cmd_arg) {const chartask_ state [] = {'rpm / uxArraySize = uxTaskGetNumberOfTasks () If (uxArraySize > MAX_TASK_NUM) {MY_DEBUGF (CMD_LINE_DEBUG, ("there are too many current tasks! \ n ");} / * get the status information of each task * / uxArraySize = uxTaskGetSystemState (pxTaskStatusArray, uxArraySize, & ulTotalRunTime); # if (configGENERATE_RUN_TIME_STATS==1) MY_DEBUGF (CMD_LINE_DEBUG, (" Task name status ID priority stack CPU utilization\ n ")) / * avoid zero division errors * / if (ulTotalRunTime > 0) {/ * convert each part of the task status information obtained into a string format easily recognized by the programmer * / for (x = 0; x

< uxArraySize; x++ ) { char tmp[128]; /* 计算任务运行时间与总运行时间的百分比。*/ ulStatsAsPercentage =(uint64_t)(pxTaskStatusArray[ x ].ulRunTimeCounter)*100 / ulTotalRunTime; if( ulStatsAsPercentage >

0UL) {sprintf (tmp, "%-12s% muri 6c% muri 6d% Muay 8d% Muk 8d% d%%", pxTaskStatusArray [x] .pcTaskName, task_state [pxTaskStatusArray [x] .eCurrentState], pxTaskStatusArray [x] .xTaskNumber, pxTaskStatusArray [x]. UxCurrentPriority PxTaskStatusArray [x] .usStackHighWaterMark, ulStatsAsPercentage) } else {/ * the task elapsed less than 1% of the total elapsed sprintf (tmp, "%-12s%-6c%-6d%-8d%-8dt)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report