Source code analysis of liteos kernel: bit operation module

Time:2021-6-11

Abstract:This article leads you to analyze the source code of the bit operation module of liteos. The code is very simple. Refer to the official sample program code, compile and run it, and deepen your understanding.

This article is shared from Huawei cloud community《Source code analysis of liteos kernel — bit operation module》Author: zhushy.

Before further analysis, let’s familiarize ourselves with bit operation, the auxiliary function module provided by liteos, which can be used in mutex and other modules. Bit operation refers to the bit operation of binary number. The program can set a variable as a status word, and each bit (flag bit) in the status word can have a custom meaning. The source code involved in this paper can be downloaded fromLiteOSOpen source sitehttps://gitee.com/LiteOS/LiteOSobtain. The source code and development documents of bit operation module are as follows:

  • Liteos kernel bit operation source code

The function of bit operation module is relatively simple, including the header file kernelincludelos of bit operation_ Bitmap. H, C source code file kernelbaselos_ bitmap.c。

  • Development guide bit operation document

Online documentshttps://gitee.com/LiteOS/Lite…

Let’s take a look at the usage scenarios of bit operation concept core first. For detailed introduction and related examples, please refer to the bit operation document of liteos development guide.

The bit operation module of liteos provides bit operation for 32-bit unsigned integer values. The bit values are 0-31, starting from 0, from left to right, bit 0, bit 1… 31, etc. Macro OS defined_ BITMAP_ The mask is as follows, which is 31 decimal. If the incoming bit is greater than 31, it will be truncated by logical and operation (POS & OS)_ BITMAP_ Only take the lower 5 bits to ensure that it will not be greater than 31 to avoid overflow.

#define OS_BITMAP_MASK 0x1FU

The bit operation module of Huawei liteos provides the operation of setting 1 and clearing 0 of the flag bit, which can change the content of the flag bit. At the same time, it also provides the function of obtaining the highest and lowest bit of the flag bit 1 in the status word. Users can also perform bit operations on the registers of the system.

Next, we analyze the source code of the next operation.

Common functions of bit operation

The bit operation provides four APIs for setting 1, clearing 0, and obtaining the highest and lowest bit of 1, as follows:

Source code analysis of liteos kernel: bit operation module

LOS_ Bitmapset() sets a flag bit of the status word to 1

Set a flag bit of the status word to 1. Let’s first look at the incoming parameters. The two required parameters are: the status word uint32 * bitmap of the bit content to be changed, and the bit number uint16 POS to be changed.

The code is very simple. First, perform basic verification. If the status word is empty, it will return. Then calculate POS & OS_ BITMAP_ Mask, only take the lower 5 bits of binary, the maximum bit value is 31, to avoid overflow when moving left. 1U << (pos & OS_ BITMAP_ Mask) is to change the bit of the status word of the content, and set the content of the specified bit of the status word uint32 * bitmap to 1 by bit or operation.

VOID LOS_BitmapSet(UINT32 *bitmap, UINT16 pos)
{
    if (bitmap == NULL) {
        return;
    }

    *bitmap |= 1U << (pos & OS_BITMAP_MASK);
}

LOS_ Bitmapclr() clears a flag bit of the status word

Clear 0 operation is performed on a flag bit of the status word, and the code corresponds to the set 1 operation, which is relatively simple, ~ (1U < (POS & OS)_ BITMAP_ Mask) indicates that the bit of the status word to be changed is 0, and the other bits are 1. Then, the content of the specified bit of the status word uint32 * bitmap is set to 0 by bit sum operation.

VOID LOS_BitmapClr(UINT32 *bitmap, UINT16 pos)

{
    if (bitmap == NULL) {
        return;
    }

    *bitmap &= ~(1U << (pos & OS_BITMAP_MASK));
}

LOS_ Highbitget() gets the highest bit of 1 in the status word

In the code, CLZ (bitmap) is a macro, expanded as (__ builtin_ CLZ (bitmap)), which is the library function of high efficiency bit operation built in compiler. CLZ is the abbreviation of count leading zeros, which is to count the number of zeros at the beginning of the high bit region in binary value. Using OS_ BITMAP_ Subtract this value from mask and the result is the highest bit of 1 in the status word.

UINT16 LOS_HighBitGet(UINT32 bitmap)
{
    if (bitmap == 0) {
        return LOS_INVALID_BIT_INDEX;
    }

    return (OS_BITMAP_MASK - CLZ(bitmap));
}

LOS_ Lowbitget() gets the lowest bit of 1 in the status word

Code, where CLZ (bitmap) is a macro, expanded as (__ builtin_ CTZ (value)), which is the library function of high-efficiency bit operation built in the compiler. CTZ is the abbreviation of count training zeros, which is to count the number of zeros at the end of the low bit area in the binary value, and the result is the lowest bit of 1 in the status word.

UINT16 LOS_LowBitGet(UINT32 bitmap)
{
    if (bitmap == 0) {
        return LOS_INVALID_BIT_INDEX;
    }

    return CTZ(bitmap);
}

Summary

This article leads you to analyze the source code of the bit operation module of liteos. The code is very simple. Refer to the official sample program code, compile and run it, and deepen your understanding.

Click follow to learn about Huawei’s new cloud technology for the first time~