Function Classification in Embedded C

When I explain embedded C language functions, I classify them. In this post, I will explain the function classification.

Functions are broadly classified into Functions, Interrupt functions, and Macro functions as follows.

Function

A C function is a structure that allows you to define specific processes together and call them as needed. A function performs the following processes when it is called and when it returns.

When a function is called, the values ​​of the general-purpose registers and the program counter (PC) are saved to the stack, and the arguments to the function are passed using the general-purpose registers or the stack.The program counter is then rewritten to the start address of the function, and execution of the function begins.

When the function returns, the general-purpose register values ​​saved in the stack are restored, and the function return value is retrieved from the general-purpose register or stack and returned to the caller.The program counter is then rewritten to the original value retrieved from the stack, and the program returns to the position before the function was called.

The general-purpose registers that are saved are fixed by default, but some can be changed by compiler settings.

Function Classification

I categorize functions into Component functions, Functional group functions, Unit functions, and Library functions.

Component function

Component functions are functional unit functions separated by layers. Component functions are composed of unit functions that divide functions into functional group functions and further subdivide those functional group functions into single functions. Component functions are also used as units that configure software architecture. The classification method of component functions will be explained separately.

The component functions are aimed at improving the following quality characteristics:

Quality CharacteristicsEffect
ConservatismLocalize the impact of changes and modifications to make them easier to manage.
ReusabilityCan be reused in other systems or software.
ModularityEasily share development responsibilities.
Ease of modificationLimit the scope of impact and make it easier to change specifications
Functional group function

A functional group function is a function that subdivides the functionality of a component function and groups together a group of those subdivided unit functions.

Unit function

A unit function is a function that narrows down the functionality of a functional group function to a single function.

Library function

Library functions are functions that have been made commonly available as libraries for frequently used processes. In addition, as a rule, library functions used in RTOS do not write to shared resources such as static variables. If you do write to shared resources, you must implement exclusive control (such as disabling interrupts).

Interrupt functions (callback functions)

An interrupt function is a function that is called from a vector table, etc., when an interrupt cause occurs. Interrupt functions require more general-purpose registers to be saved than normal function calls or function return processes, so a prototype declaration is required. Please note that prototype declarations are microcontroller and compiler dependent.

Like interrupt functions, callback functions may be called from the vector table or from the OS etc.

Macro functions (#define macros)

Macro functions (#define macros) allow the preprocessor to replace certain strings in code with another string, so that the replaced code behaves like a function.

Be careful when accessing shared resources in RTOS

When an interrupt function or callback function accesses a shared resource, there is a possibility that other processes with lower priority may also access the same resource, so exclusive control (such as a mutex or semaphore) must be used to lock down the other processes with lower priority.

OSS-ECAL English
error: Content is protected !!