1 /* 2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 12 * of conditions and the following disclaimer in the documentation and/or other materials 13 * provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 16 * to endorse or promote products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /** 33 * @defgroup kernel Kernel 34 * @defgroup los_base Basic definitions 35 * @ingroup kernel 36 */ 37 38 #ifndef _LOS_BASE_H 39 #define _LOS_BASE_H 40 41 #include "los_builddef.h" 42 #include "los_typedef.h" 43 #include "los_config.h" 44 #include "los_printf.h" 45 #include "los_list.h" 46 #include "los_err.h" 47 #include "los_errno.h" 48 #include "los_hw.h" 49 #include "los_hwi.h" 50 #include "securec.h" 51 #include "los_exc.h" 52 #ifdef __cplusplus 53 #if __cplusplus 54 extern "C" { 55 #endif /* __cplusplus */ 56 #endif /* __cplusplus */ 57 58 59 #define SIZE(a) (a) 60 61 #define LOS_ASSERT_COND(expression) LOS_ASSERT(expression) 62 63 extern VOID PrintExcInfo(const CHAR *fmt, ...); 64 65 /** 66 * @ingroup los_base 67 * Define the timeout interval as LOS_NO_WAIT. 68 */ 69 #define LOS_NO_WAIT 0 70 71 /** 72 * @ingroup los_base 73 * Define the timeout interval as LOS_WAIT_FOREVER. 74 */ 75 #define LOS_WAIT_FOREVER 0xFFFFFFFF 76 77 /** 78 * @ingroup los_base 79 * Align the beginning of the object with the base address addr, with boundary bytes being the smallest unit of 80 * alignment. 81 */ 82 #ifndef ALIGN 83 #define ALIGN(addr, boundary) LOS_Align(addr, boundary) 84 #endif 85 86 /** 87 * @ingroup los_base 88 * Align the tail of the object with the base address addr, with size bytes being the smallest unit of alignment. 89 */ 90 #define TRUNCATE(addr, size) ((UINTPTR)(addr) & ~((size) - 1)) 91 92 /** 93 * @ingroup los_base 94 * Read a UINT8 value from addr and stored in value. 95 */ 96 #define READ_UINT8(value, addr) ({ (value) = *((volatile UINT8 *)((UINTPTR)(addr))); DSB; }) 97 98 /** 99 * @ingroup los_base 100 * Read a UINT16 value from addr and stored in addr. 101 */ 102 #define READ_UINT16(value, addr) ({ (value) = *((volatile UINT16 *)((UINTPTR)(addr))); DSB; }) 103 104 /** 105 * @ingroup los_base 106 * Read a UINT32 value from addr and stored in value. 107 */ 108 #define READ_UINT32(value, addr) ({ (value) = *((volatile UINT32 *)((UINTPTR)(addr))); DSB; }) 109 110 /** 111 * @ingroup los_base 112 * Read a UINT64 value from addr and stored in value. 113 */ 114 #define READ_UINT64(value, addr) ({ (value) = *((volatile UINT64 *)((UINTPTR)(addr))); DSB; }) 115 116 /** 117 * @ingroup los_base 118 * Write a UINT8 value to addr. 119 */ 120 #define WRITE_UINT8(value, addr) ({ DSB; *((volatile UINT8 *)((UINTPTR)(addr))) = (value); }) 121 122 /** 123 * @ingroup los_base 124 * Write a UINT16 value to addr. 125 */ 126 #define WRITE_UINT16(value, addr) ({ DSB; *((volatile UINT16 *)((UINTPTR)(addr))) = (value); }) 127 128 /** 129 * @ingroup los_base 130 * Write a UINT32 value to addr. 131 */ 132 #define WRITE_UINT32(value, addr) ({ DSB; *((volatile UINT32 *)((UINTPTR)(addr))) = (value); }) 133 134 /** 135 * @ingroup los_base 136 * Write a UINT64 addr to addr. 137 */ 138 #define WRITE_UINT64(value, addr) ({ DSB; *((volatile UINT64 *)((UINTPTR)(addr))) = (value); }) 139 140 /** 141 * @ingroup los_base 142 * Get a UINT8 value from addr. 143 */ 144 #define GET_UINT8(addr) ({ UINT8 r = *((volatile UINT8 *)((UINTPTR)(addr))); DSB; r; }) 145 146 /** 147 * @ingroup los_base 148 * Get a UINT16 value from addr. 149 */ 150 #define GET_UINT16(addr) ({ UINT16 r = *((volatile UINT16 *)((UINTPTR)(addr))); DSB; r; }) 151 152 /** 153 * @ingroup los_base 154 * Get a UINT32 value from addr. 155 */ 156 #define GET_UINT32(addr) ({ UINT32 r = *((volatile UINT32 *)((UINTPTR)(addr))); DSB; r; }) 157 158 /** 159 * @ingroup los_base 160 * Get a UINT64 value from addr. 161 */ 162 #define GET_UINT64(addr) ({ UINT64 r = *((volatile UINT64 *)((UINTPTR)(addr))); l; r; }) 163 164 #ifdef LOSCFG_DEBUG_VERSION 165 #define LOS_ASSERT(judge) do { \ 166 if ((UINT32)(judge) == 0) { \ 167 (VOID)LOS_IntLock(); \ 168 PRINT_ERR("ASSERT ERROR! %s, %d, %s\n", __FILE__, __LINE__, __FUNCTION__); \ 169 OsBackTrace(); \ 170 while (1) {} \ 171 } \ 172 } while (0) 173 174 #define LOS_ASSERT_MSG(judge, msg) do { \ 175 if ((UINT32)(judge) == 0) { \ 176 (VOID)LOS_IntLock(); \ 177 PRINT_ERR("ASSERT ERROR! %s, %d, %s\n", __FILE__, __LINE__, __FUNCTION__); \ 178 PRINT_ERR msg; \ 179 OsBackTrace(); \ 180 while (1) {} \ 181 } \ 182 } while (0) 183 184 #else 185 #define LOS_ASSERT(judge) 186 #define LOS_ASSERT_MSG(judge, msg) 187 #endif 188 189 #define STATIC_ASSERT _Static_assert 190 191 /** 192 * @ingroup los_base 193 * @brief Align the value (addr) by some bytes (boundary) you specify. 194 * 195 * @par Description: 196 * This API is used to align the value (addr) by some bytes (boundary) you specify. 197 * 198 * @attention 199 * <ul> 200 * <li>the value of boundary usually is 4,8,16,32.</li> 201 * </ul> 202 * 203 * @param addr [IN] The variable what you want to align. 204 * @param boundary [IN] The align size what you want to align. 205 * 206 * @retval #UINTPTR The variable what have been aligned. 207 * @par Dependency: 208 * <ul><li>los_base.h: the header file that contains the API declaration.</li></ul> 209 * @see 210 */ 211 extern UINTPTR LOS_Align(UINTPTR addr, UINT32 boundary); 212 213 /** 214 * @ingroup los_base 215 * @brief Sleep the current task. 216 * 217 * @par Description: 218 * This API is used to delay the execution of the current task. The task is able to be scheduled after it is delayed 219 * for a specified number of Ticks. 220 * 221 * @attention 222 * <ul> 223 * <li>The task fails to be delayed if it is being delayed during interrupt processing or it is locked.</li> 224 * <li>If 0 is passed in and the task scheduling is not locked, execute the next task in the queue of tasks with the 225 * priority of the current task. 226 * If no ready task with the priority of the current task is available, the task scheduling will not occur, and the 227 * current task continues to be executed.</li> 228 * <li>The parameter passed in can not be equal to LOS_WAIT_FOREVER(0xFFFFFFFF). 229 * If that happens, the task will not sleep 0xFFFFFFFF milliseconds or sleep forever but sleep 0xFFFFFFFF Ticks.</li> 230 * </ul> 231 * 232 * @param msecs [IN] Type #UINT32 Number of MS for which the task is delayed. 233 * 234 * @retval None 235 * @par Dependency: 236 * <ul><li>los_base.h: the header file that contains the API declaration.</li></ul> 237 * @see None 238 */ 239 extern VOID LOS_Msleep(UINT32 msecs); 240 241 #ifdef __cplusplus 242 #if __cplusplus 243 } 244 #endif /* __cplusplus */ 245 #endif /* __cplusplus */ 246 247 #endif /* _LOS_BASE_H */ 248