xref: /kernel/liteos_a/kernel/include/los_base.h (revision 0d163575)
10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci *
50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
60d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
70d163575Sopenharmony_ci *
80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
90d163575Sopenharmony_ci *    conditions and the following disclaimer.
100d163575Sopenharmony_ci *
110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
120d163575Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
130d163575Sopenharmony_ci *    provided with the distribution.
140d163575Sopenharmony_ci *
150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
160d163575Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
170d163575Sopenharmony_ci *    permission.
180d163575Sopenharmony_ci *
190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300d163575Sopenharmony_ci */
310d163575Sopenharmony_ci
320d163575Sopenharmony_ci/**
330d163575Sopenharmony_ci * @defgroup kernel Kernel
340d163575Sopenharmony_ci * @defgroup los_base Basic definitions
350d163575Sopenharmony_ci * @ingroup kernel
360d163575Sopenharmony_ci */
370d163575Sopenharmony_ci
380d163575Sopenharmony_ci#ifndef _LOS_BASE_H
390d163575Sopenharmony_ci#define _LOS_BASE_H
400d163575Sopenharmony_ci
410d163575Sopenharmony_ci#include "los_builddef.h"
420d163575Sopenharmony_ci#include "los_typedef.h"
430d163575Sopenharmony_ci#include "los_config.h"
440d163575Sopenharmony_ci#include "los_printf.h"
450d163575Sopenharmony_ci#include "los_list.h"
460d163575Sopenharmony_ci#include "los_err.h"
470d163575Sopenharmony_ci#include "los_errno.h"
480d163575Sopenharmony_ci#include "los_hw.h"
490d163575Sopenharmony_ci#include "los_hwi.h"
500d163575Sopenharmony_ci#include "securec.h"
510d163575Sopenharmony_ci#include "los_exc.h"
520d163575Sopenharmony_ci#ifdef __cplusplus
530d163575Sopenharmony_ci#if __cplusplus
540d163575Sopenharmony_ciextern "C" {
550d163575Sopenharmony_ci#endif /* __cplusplus */
560d163575Sopenharmony_ci#endif /* __cplusplus */
570d163575Sopenharmony_ci
580d163575Sopenharmony_ci
590d163575Sopenharmony_ci#define SIZE(a) (a)
600d163575Sopenharmony_ci
610d163575Sopenharmony_ci#define LOS_ASSERT_COND(expression) LOS_ASSERT(expression)
620d163575Sopenharmony_ci
630d163575Sopenharmony_ciextern VOID PrintExcInfo(const CHAR *fmt, ...);
640d163575Sopenharmony_ci
650d163575Sopenharmony_ci/**
660d163575Sopenharmony_ci * @ingroup los_base
670d163575Sopenharmony_ci * Define the timeout interval as LOS_NO_WAIT.
680d163575Sopenharmony_ci */
690d163575Sopenharmony_ci#define LOS_NO_WAIT                                 0
700d163575Sopenharmony_ci
710d163575Sopenharmony_ci/**
720d163575Sopenharmony_ci * @ingroup los_base
730d163575Sopenharmony_ci * Define the timeout interval as LOS_WAIT_FOREVER.
740d163575Sopenharmony_ci */
750d163575Sopenharmony_ci#define LOS_WAIT_FOREVER                            0xFFFFFFFF
760d163575Sopenharmony_ci
770d163575Sopenharmony_ci/**
780d163575Sopenharmony_ci * @ingroup los_base
790d163575Sopenharmony_ci * Align the beginning of the object with the base address addr, with boundary bytes being the smallest unit of
800d163575Sopenharmony_ci * alignment.
810d163575Sopenharmony_ci */
820d163575Sopenharmony_ci#ifndef ALIGN
830d163575Sopenharmony_ci#define ALIGN(addr, boundary)                       LOS_Align(addr, boundary)
840d163575Sopenharmony_ci#endif
850d163575Sopenharmony_ci
860d163575Sopenharmony_ci/**
870d163575Sopenharmony_ci * @ingroup los_base
880d163575Sopenharmony_ci * Align the tail of the object with the base address addr, with size bytes being the smallest unit of alignment.
890d163575Sopenharmony_ci */
900d163575Sopenharmony_ci#define TRUNCATE(addr, size)                        ((UINTPTR)(addr) & ~((size) - 1))
910d163575Sopenharmony_ci
920d163575Sopenharmony_ci/**
930d163575Sopenharmony_ci * @ingroup los_base
940d163575Sopenharmony_ci * Read a UINT8 value from addr and stored in value.
950d163575Sopenharmony_ci */
960d163575Sopenharmony_ci#define READ_UINT8(value, addr)                     ({ (value) = *((volatile UINT8 *)((UINTPTR)(addr))); DSB; })
970d163575Sopenharmony_ci
980d163575Sopenharmony_ci/**
990d163575Sopenharmony_ci * @ingroup los_base
1000d163575Sopenharmony_ci * Read a UINT16 value from addr and stored in addr.
1010d163575Sopenharmony_ci */
1020d163575Sopenharmony_ci#define READ_UINT16(value, addr)                    ({ (value) = *((volatile UINT16 *)((UINTPTR)(addr))); DSB; })
1030d163575Sopenharmony_ci
1040d163575Sopenharmony_ci/**
1050d163575Sopenharmony_ci * @ingroup los_base
1060d163575Sopenharmony_ci * Read a UINT32 value from addr and stored in value.
1070d163575Sopenharmony_ci */
1080d163575Sopenharmony_ci#define READ_UINT32(value, addr)                    ({ (value) = *((volatile UINT32 *)((UINTPTR)(addr))); DSB; })
1090d163575Sopenharmony_ci
1100d163575Sopenharmony_ci/**
1110d163575Sopenharmony_ci * @ingroup los_base
1120d163575Sopenharmony_ci * Read a UINT64 value from addr and stored in value.
1130d163575Sopenharmony_ci */
1140d163575Sopenharmony_ci#define READ_UINT64(value, addr)                    ({ (value) = *((volatile UINT64 *)((UINTPTR)(addr))); DSB; })
1150d163575Sopenharmony_ci
1160d163575Sopenharmony_ci/**
1170d163575Sopenharmony_ci * @ingroup los_base
1180d163575Sopenharmony_ci * Write a UINT8 value to addr.
1190d163575Sopenharmony_ci */
1200d163575Sopenharmony_ci#define WRITE_UINT8(value, addr)                    ({ DSB; *((volatile UINT8 *)((UINTPTR)(addr))) = (value); })
1210d163575Sopenharmony_ci
1220d163575Sopenharmony_ci/**
1230d163575Sopenharmony_ci * @ingroup los_base
1240d163575Sopenharmony_ci * Write a UINT16 value to addr.
1250d163575Sopenharmony_ci */
1260d163575Sopenharmony_ci#define WRITE_UINT16(value, addr)                   ({ DSB; *((volatile UINT16 *)((UINTPTR)(addr))) = (value); })
1270d163575Sopenharmony_ci
1280d163575Sopenharmony_ci/**
1290d163575Sopenharmony_ci * @ingroup los_base
1300d163575Sopenharmony_ci * Write a UINT32 value to addr.
1310d163575Sopenharmony_ci */
1320d163575Sopenharmony_ci#define WRITE_UINT32(value, addr)                   ({ DSB; *((volatile UINT32 *)((UINTPTR)(addr))) = (value); })
1330d163575Sopenharmony_ci
1340d163575Sopenharmony_ci/**
1350d163575Sopenharmony_ci * @ingroup los_base
1360d163575Sopenharmony_ci * Write a UINT64 addr to addr.
1370d163575Sopenharmony_ci */
1380d163575Sopenharmony_ci#define WRITE_UINT64(value, addr)                   ({ DSB; *((volatile UINT64 *)((UINTPTR)(addr))) = (value); })
1390d163575Sopenharmony_ci
1400d163575Sopenharmony_ci/**
1410d163575Sopenharmony_ci * @ingroup los_base
1420d163575Sopenharmony_ci * Get a UINT8 value from addr.
1430d163575Sopenharmony_ci */
1440d163575Sopenharmony_ci#define GET_UINT8(addr)                             ({ UINT8 r = *((volatile UINT8 *)((UINTPTR)(addr))); DSB; r; })
1450d163575Sopenharmony_ci
1460d163575Sopenharmony_ci/**
1470d163575Sopenharmony_ci * @ingroup los_base
1480d163575Sopenharmony_ci * Get a UINT16 value from addr.
1490d163575Sopenharmony_ci */
1500d163575Sopenharmony_ci#define GET_UINT16(addr)                            ({ UINT16 r = *((volatile UINT16 *)((UINTPTR)(addr))); DSB; r; })
1510d163575Sopenharmony_ci
1520d163575Sopenharmony_ci/**
1530d163575Sopenharmony_ci * @ingroup los_base
1540d163575Sopenharmony_ci * Get a UINT32 value from addr.
1550d163575Sopenharmony_ci */
1560d163575Sopenharmony_ci#define GET_UINT32(addr)                            ({ UINT32 r = *((volatile UINT32 *)((UINTPTR)(addr))); DSB; r; })
1570d163575Sopenharmony_ci
1580d163575Sopenharmony_ci/**
1590d163575Sopenharmony_ci * @ingroup los_base
1600d163575Sopenharmony_ci * Get a UINT64 value from addr.
1610d163575Sopenharmony_ci */
1620d163575Sopenharmony_ci#define GET_UINT64(addr)                            ({ UINT64 r = *((volatile UINT64 *)((UINTPTR)(addr))); l; r; })
1630d163575Sopenharmony_ci
1640d163575Sopenharmony_ci#ifdef LOSCFG_DEBUG_VERSION
1650d163575Sopenharmony_ci#define LOS_ASSERT(judge) do {                                                     \
1660d163575Sopenharmony_ci    if ((UINT32)(judge) == 0) {                                                    \
1670d163575Sopenharmony_ci        (VOID)LOS_IntLock();                                                       \
1680d163575Sopenharmony_ci        PRINT_ERR("ASSERT ERROR! %s, %d, %s\n", __FILE__, __LINE__, __FUNCTION__); \
1690d163575Sopenharmony_ci        OsBackTrace();                                                             \
1700d163575Sopenharmony_ci        while (1) {}                                                               \
1710d163575Sopenharmony_ci    }                                                                              \
1720d163575Sopenharmony_ci} while (0)
1730d163575Sopenharmony_ci
1740d163575Sopenharmony_ci#define LOS_ASSERT_MSG(judge, msg) do {                                            \
1750d163575Sopenharmony_ci    if ((UINT32)(judge) == 0) {                                                    \
1760d163575Sopenharmony_ci        (VOID)LOS_IntLock();                                                       \
1770d163575Sopenharmony_ci        PRINT_ERR("ASSERT ERROR! %s, %d, %s\n", __FILE__, __LINE__, __FUNCTION__); \
1780d163575Sopenharmony_ci        PRINT_ERR msg;                                                             \
1790d163575Sopenharmony_ci        OsBackTrace();                                                             \
1800d163575Sopenharmony_ci        while (1) {}                                                               \
1810d163575Sopenharmony_ci    }                                                                              \
1820d163575Sopenharmony_ci} while (0)
1830d163575Sopenharmony_ci
1840d163575Sopenharmony_ci#else
1850d163575Sopenharmony_ci#define LOS_ASSERT(judge)
1860d163575Sopenharmony_ci#define LOS_ASSERT_MSG(judge, msg)
1870d163575Sopenharmony_ci#endif
1880d163575Sopenharmony_ci
1890d163575Sopenharmony_ci#define STATIC_ASSERT _Static_assert
1900d163575Sopenharmony_ci
1910d163575Sopenharmony_ci/**
1920d163575Sopenharmony_ci * @ingroup los_base
1930d163575Sopenharmony_ci * @brief Align the value (addr) by some bytes (boundary) you specify.
1940d163575Sopenharmony_ci *
1950d163575Sopenharmony_ci * @par Description:
1960d163575Sopenharmony_ci * This API is used to align the value (addr) by some bytes (boundary) you specify.
1970d163575Sopenharmony_ci *
1980d163575Sopenharmony_ci * @attention
1990d163575Sopenharmony_ci * <ul>
2000d163575Sopenharmony_ci * <li>the value of boundary usually is 4,8,16,32.</li>
2010d163575Sopenharmony_ci * </ul>
2020d163575Sopenharmony_ci *
2030d163575Sopenharmony_ci * @param addr     [IN]  The variable what you want to align.
2040d163575Sopenharmony_ci * @param boundary [IN]  The align size what you want to align.
2050d163575Sopenharmony_ci *
2060d163575Sopenharmony_ci * @retval #UINTPTR The variable what have been aligned.
2070d163575Sopenharmony_ci * @par Dependency:
2080d163575Sopenharmony_ci * <ul><li>los_base.h: the header file that contains the API declaration.</li></ul>
2090d163575Sopenharmony_ci * @see
2100d163575Sopenharmony_ci */
2110d163575Sopenharmony_ciextern UINTPTR LOS_Align(UINTPTR addr, UINT32 boundary);
2120d163575Sopenharmony_ci
2130d163575Sopenharmony_ci/**
2140d163575Sopenharmony_ci * @ingroup los_base
2150d163575Sopenharmony_ci * @brief Sleep the current task.
2160d163575Sopenharmony_ci *
2170d163575Sopenharmony_ci * @par Description:
2180d163575Sopenharmony_ci * This API is used to delay the execution of the current task. The task is able to be scheduled after it is delayed
2190d163575Sopenharmony_ci * for a specified number of Ticks.
2200d163575Sopenharmony_ci *
2210d163575Sopenharmony_ci * @attention
2220d163575Sopenharmony_ci * <ul>
2230d163575Sopenharmony_ci * <li>The task fails to be delayed if it is being delayed during interrupt processing or it is locked.</li>
2240d163575Sopenharmony_ci * <li>If 0 is passed in and the task scheduling is not locked, execute the next task in the queue of tasks with the
2250d163575Sopenharmony_ci * priority of the current task.
2260d163575Sopenharmony_ci * If no ready task with the priority of the current task is available, the task scheduling will not occur, and the
2270d163575Sopenharmony_ci * current task continues to be executed.</li>
2280d163575Sopenharmony_ci * <li>The parameter passed in can not be equal to LOS_WAIT_FOREVER(0xFFFFFFFF).
2290d163575Sopenharmony_ci * If that happens, the task will not sleep 0xFFFFFFFF milliseconds or sleep forever but sleep 0xFFFFFFFF Ticks.</li>
2300d163575Sopenharmony_ci * </ul>
2310d163575Sopenharmony_ci *
2320d163575Sopenharmony_ci * @param msecs [IN] Type #UINT32 Number of MS for which the task is delayed.
2330d163575Sopenharmony_ci *
2340d163575Sopenharmony_ci * @retval None
2350d163575Sopenharmony_ci * @par Dependency:
2360d163575Sopenharmony_ci * <ul><li>los_base.h: the header file that contains the API declaration.</li></ul>
2370d163575Sopenharmony_ci * @see None
2380d163575Sopenharmony_ci */
2390d163575Sopenharmony_ciextern VOID LOS_Msleep(UINT32 msecs);
2400d163575Sopenharmony_ci
2410d163575Sopenharmony_ci#ifdef __cplusplus
2420d163575Sopenharmony_ci#if __cplusplus
2430d163575Sopenharmony_ci}
2440d163575Sopenharmony_ci#endif /* __cplusplus */
2450d163575Sopenharmony_ci#endif /* __cplusplus */
2460d163575Sopenharmony_ci
2470d163575Sopenharmony_ci#endif /* _LOS_BASE_H */
248