Understanding the Purpose of HOOK Functions in RTOS

Follow +Star Public Account Number, don’t miss the wonderful content

Understanding the Purpose of HOOK Functions in RTOS

Author | strongerHuang

WeChat Public Account | Embedded Column

In many operating systems, there is a type of API function interface: HOOK functions (also known as hook functions).

For example: HOOK functions can be found in Windows desktop operating systems, µC/OS, FreeRTOS and other real-time operating systems.

Next, I will explain what HOOK functions are and their purposes in conjunction with µC/OS.

Embedded Column

1

What are HOOK Functions?

HOOK functions, also known as hook functions, are functions called internally by the operating system.

If HOOK is enabled, the operating system will call the corresponding Hook function (the hook function you write) at the arrival of specific events.

For example: in RTOS, when a Task is deleted, the corresponding App_TaskIdleHook function will be called. (This “App_TaskIdleHook” function needs to be written by you)

Most of the content found online about HOOK uses Windows as an example to describe the role and examples of hook functions, such as Baidu Encyclopedia:

Hook functions are part of the Windows message processing mechanism. By setting a “hook”, applications can filter all messages and events at the system level, accessing messages that are normally inaccessible. The essence of a hook is a program used to process system messages, which is hooked into the system via system calls.

Why is it called a “hook”?

There are many explanations online, for example: it is “hooked” by the operating system. Interested readers can learn more online.

Embedded Column

2

Purpose and Use of HOOK Functions

The HOOK functions are present in familiar RTOS such as µC/OS and FreeRTOS, and can be used by enabling the corresponding switches in the config configuration file.

Next, I will discuss the purpose and use of HOOK functions in conjunction with µC/OS:

1. Configuring HOOK

Many RTOS have one (or more) configuration files to configure (trim) the system.

For example, in µC/OS, there is the os_cfg.h system configuration file. It is essentially some enabling switches; needed functions are turned on, while unnecessary ones are turned off to achieve system trimming.

                                       /* ---------------------- MISCELLANEOUS ----------------------- */#define OS_APP_HOOKS_EN           1    /* Application-defined hooks are called from the uC/OS-II hooks */#define OS_ARG_CHK_EN             0    /* Enable (1) or Disable (0) argument checking                  */#define OS_CPU_HOOKS_EN           1    /* uC/OS-II hooks are found in the processor port files         */
#define OS_DEBUG_EN               1    /* Enable(1) debug variables                                    */
Similarly, HOOK functions are also enabled through the cfg configuration file, for example:
#define OS_APP_HOOKS_EN           1#define OS_CPU_HOOKS_EN           1

0: Disable;

1: Enable;

Likewise, FreeRTOS has similar configurations:

Understanding the Purpose of HOOK Functions in RTOS

We can enable the HOOK we need to use.

2. Using TaskIdleHook

TaskIdleHook, that is the Task Idle Hook Function, will be called when the task is idle.

This hook function exists in many operating systems, and when the task is idle (all other tasks are suspended), this hook function will be called.

For example: the CPU utilization we talk about is counted during the idle task; taking µC/OS as an example:

void  OS_TaskIdle (void *p_arg){#if OS_CRITICAL_METHOD == 3u                     /* Allocate storage for CPU status register           */    OS_CPU_SR  cpu_sr = 0u;#endif    p_arg = p_arg;                               /* Prevent compiler warning for not using 'p_arg'     */    for (;;) {        OS_ENTER_CRITICAL();        OSIdleCtr++;        OS_EXIT_CRITICAL();        OSTaskIdleHook();                        /* Call user definable HOOK                           */    }}

Understanding the Purpose of HOOK Functions in RTOS

The system idle hook function OSTaskIdleHook:

#if OS_CPU_HOOKS_EN > 0uvoid  OSTaskIdleHook (void){#if OS_APP_HOOKS_EN > 0u    App_TaskIdleHook();#endif}#endif

Understanding the Purpose of HOOK Functions in RTOS

The application idle hook function App_TaskIdleHook:

If enabled, this function needs to be implemented by us; for example: if the idle count exceeds 50, we print a message:

void  App_TaskIdleHook (void){  UserIdleCtr++;  // Count idle times
  if(50 < UserIdleCtr)  {    UserIdleCtr = 0;
    printf("SYS_IDLE");  }}
Having seen this, do you understand the principle of HOOK functions? Analyzing step by step from the system internally, it’s actually quite simple.
Because the operating system is mostly in an idle state, IDLE idle functions are executed relatively frequently, which might be hard for many to understand.
HOOK functions, as mentioned above, are called and executed when a specific event occurs, while IDLE idle tasks occur relatively frequently, while events likedeleting a Task are rare, leading to fewer occurrences of such HOOK functions.
Alright, that’s all for sharing. If you have any questions, feel free to leave a comment!
———— END ————

Reply with ‘RTOS’ or ‘Embedded Software Design and Development’ to read more related articles.

Welcome to follow my public account, reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Welcome to follow my video account:

Understanding the Purpose of HOOK Functions in RTOS

Click “Read the Original” to see more shares, and feel free to share, collect, like, and view.

Leave a Comment

Your email address will not be published. Required fields are marked *