first submission

This commit is contained in:
qiyongshuang
2023-02-01 14:19:13 +08:00
parent d534aad004
commit b79c6ac80d
3711 changed files with 745432 additions and 2 deletions
@@ -0,0 +1,70 @@
# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
# to endorse or promote products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
static_library("kernel") {
sources = [
"src/los_event.c",
"src/los_init.c",
"src/los_mux.c",
"src/los_queue.c",
"src/los_sem.c",
"src/los_swtmr.c",
"src/los_task.c",
"src/los_tick.c",
"src/los_sched.c",
"src/los_sortlink.c",
"src/mm/los_membox.c",
"src/mm/los_memory.c",
]
include_dirs = [
"include",
"arch/include",
"../components/cpup",
"../components/exchook",
"../components/backtrace",
"../components/power",
"../utils",
"//third_party/bounds_checking_function/include",
]
if ("$board_cpu" == "cortex-m3") {
deps = [ "arch/arm/cortex-m3/gcc/:arch" ]
} else if ("$board_cpu" == "cortex-m4") {
deps = [ "arch/arm/cortex-m4/gcc/:arch" ]
} else if ("$board_cpu" == "cortex-m7") {
deps = [ "arch/arm/cortex-m7/gcc/:arch" ]
} else if ("$board_cpu" == "cortex-m33") {
deps = [ "arch/arm/cortex-m33/gcc/:arch" ]
} else if ("$board_cpu" == "") {
if ("$board_arch" == "rv32imac" || "$board_arch" == "rv32imafdc") {
deps = [ "arch/risc-v/riscv32/gcc:arch" ]
}
}
}
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LOS_ARCH_H
#define _LOS_ARCH_H
#include "los_config.h"
#include "los_compiler.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
VOID HalArchInit(VOID);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_ARCH_H */
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LOS_ATOMIC_H
#define _LOS_ATOMIC_H
#include "los_compiler.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_ATOMIC_H */
@@ -0,0 +1,125 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup los_context hardware
* @ingroup kernel
*/
#ifndef _LOS_CONTEXT_H
#define _LOS_CONTEXT_H
#include "los_compiler.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
/**
* @ingroup los_context
* @brief: Task stack initialization.
*
* @par Description:
* This API is used to initialize the task stack.
*
* @attention:
* <ul><li>None.</li></ul>
*
* @param taskID [IN] Type#UINT32: TaskID.
* @param stackSize [IN] Type#UINT32: Total size of the stack.
* @param topStack [IN] Type#VOID *: Top of task's stack.
*
* @retval: context Type#TaskContext *.
* @par Dependency:
* <ul><li>los_context.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern VOID *HalTskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack);
/**
* @ingroup los_context
* @brief: Function to sys exit.
*
* @par Description:
* This API is used to sys exit.
*
* @attention:
* <ul><li>None.</li></ul>
*
* @param None.
*
* @retval: None.
* @par Dependency:
* <ul><li>los_context.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
LITE_OS_SEC_TEXT_MINOR NORETURN VOID HalSysExit(VOID);
/**
* @ingroup los_context
* @brief: Task scheduling Function.
*
* @par Description:
* This API is used to scheduling task.
*
* @attention:
* <ul><li>None.</li></ul>
*
* @param None.
*
* @retval: None.
* @par Dependency:
* <ul><li>los_context.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern VOID HalTaskSchedule(VOID);
typedef VOID (*OS_TICK_HANDLER)(VOID);
UINT32 HalStartSchedule(OS_TICK_HANDLER handler);
UINT32 HalIntLock(VOID);
#define LOS_IntLock HalIntLock
VOID HalIntRestore(UINT32 intSave);
#define LOS_IntRestore HalIntRestore
UINT32 HalIntUnLock(VOID);
#define LOS_IntUnLock HalIntUnLock
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_CONTEXT_H */
@@ -0,0 +1,146 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LOS_INTERRUPT_H
#define _LOS_INTERRUPT_H
#include "los_config.h"
#include "los_compiler.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
/* *
* @ingroup los_interrupt
* Configuration item for interrupt with argument
*/
#ifndef OS_HWI_WITH_ARG
#define OS_HWI_WITH_ARG 0
#endif
typedef UINT32 HWI_HANDLE_T;
typedef UINT16 HWI_PRIOR_T;
typedef UINT16 HWI_MODE_T;
typedef UINT32 HWI_ARG_T;
#if (OS_HWI_WITH_ARG == 1)
typedef VOID (*HWI_PROC_FUNC)(VOID *parm);
#else
typedef VOID (*HWI_PROC_FUNC)(void);
#endif
/* stack protector */
extern UINT32 __stack_chk_guard;
extern VOID __stack_chk_fail(VOID);
VOID HalIntEnter(VOID);
VOID HalIntExit(VOID);
UINT32 HalIsIntActive(VOID);
#define OS_INT_ACTIVE (HalIsIntActive())
#define OS_INT_INACTIVE (!(OS_INT_ACTIVE))
#define LOS_HwiCreate HalHwiCreate
#define LOS_HwiDelete HalHwiDelete
/**
* @ingroup los_interrupt
* @brief Delete hardware interrupt.
*
* @par Description:
* This API is used to delete hardware interrupt.
*
* @attention
* <ul>
* <li>The hardware interrupt module is usable only when the configuration item for hardware interrupt tailoring is enabled.</li>
* <li>Hardware interrupt number value range: [OS_USER_HWI_MIN,OS_USER_HWI_MAX]. The value range applicable for a Cortex-A7 platform is [32,95].</li>
* <li>OS_HWI_MAX_NUM specifies the maximum number of interrupts that can be created.</li>
* <li>Before executing an interrupt on a platform, refer to the chip manual of the platform.</li>
* </ul>
*
* @param hwiNum [IN] Type#HWI_HANDLE_T: hardware interrupt number. The value range applicable for a Cortex-A7 platform is [32,95].
*
* @retval #OS_ERRNO_HWI_NUM_INVALID 0x02000900: Invalid interrupt number.
* @retval #LOS_OK 0 : The interrupt is successfully delete.
* @par Dependency:
* <ul><li>los_interrupt.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 HalHwiDelete(HWI_HANDLE_T hwiNum);
/**
* @ingroup los_interrupt
* @brief Create a hardware interrupt.
*
* @par Description:
* This API is used to configure a hardware interrupt and register a hardware interrupt handling function.
*
* @attention
* <ul>
* <li>The hardware interrupt module is usable only when the configuration item for hardware interrupt tailoring is enabled.</li>
* <li>Hardware interrupt number value range: [OS_USER_HWI_MIN,OS_USER_HWI_MAX]. The value range applicable for a Cortex-A7 platform is [32,95].</li>
* <li>OS_HWI_MAX_NUM specifies the maximum number of interrupts that can be created.</li>
* <li>Before executing an interrupt on a platform, refer to the chip manual of the platform.</li>
* </ul>
*
* @param hwiNum [IN] Type#HWI_HANDLE_T: hardware interrupt number. The value range applicable for a Cortex-A7 platform is [32,95].
* @param hwiPrio [IN] Type#HWI_PRIOR_T: hardware interrupt priority. Ignore this parameter temporarily.
* @param mode [IN] Type#HWI_MODE_T: hardware interrupt mode. Ignore this parameter temporarily.
* @param handler [IN] Type#HWI_PROC_FUNC: interrupt handler used when a hardware interrupt is triggered.
* @param arg [IN] Type#HWI_ARG_T: input parameter of the interrupt handler used when a hardware interrupt is triggered.
*
* @retval #OS_ERRNO_HWI_PROC_FUNC_NULL 0x02000901: Null hardware interrupt handling function.
* @retval #OS_ERRNO_HWI_NUM_INVALID 0x02000900: Invalid interrupt number.
* @retval #OS_ERRNO_HWI_NO_MEMORY 0x02000903: Insufficient memory for hardware interrupt creation.
* @retval #OS_ERRNO_HWI_ALREADY_CREATED 0x02000904: The interrupt handler being created has already been created.
* @retval #LOS_OK 0 : The interrupt is successfully created.
* @par Dependency:
* <ul><li>los_interrupt.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 HalHwiCreate(HWI_HANDLE_T hwiNum,
HWI_PRIOR_T hwiPrio,
HWI_MODE_T mode,
HWI_PROC_FUNC handler,
HWI_ARG_T arg);
VOID HalDisplayTaskInfo(VOID);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_INTERRUPT_H */
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup memory protection
* @ingroup kernel
*/
#ifndef _LOS_MPU_H
#define _LOS_MPU_H
#include "los_compiler.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
typedef enum {
MPU_RW_BY_PRIVILEGED_ONLY = 0,
MPU_RW_ANY = 1,
MPU_RO_BY_PRIVILEGED_ONLY = 2,
MPU_RO_ANY = 3,
} MpuAccessPermission;
typedef enum {
MPU_EXECUTABLE = 0,
MPU_NON_EXECUTABLE = 1,
} MpuExecutable;
typedef enum {
MPU_NO_SHARE = 0,
MPU_SHARE = 1,
} MpuShareability;
typedef enum {
MPU_MEM_ON_CHIP_ROM = 0,
MPU_MEM_ON_CHIP_RAM = 1,
MPU_MEM_XIP_PSRAM = 2,
MPU_MEM_XIP_NOR_FLASH = 3,
MPU_MEM_SHARE_MEM = 4,
} MpuMemType;
typedef struct {
UINT32 baseAddr;
UINT64 size; /* armv7 size == 2^x (5 <= x <= 32) 128B - 4GB */
MpuAccessPermission permission;
MpuExecutable executable;
MpuShareability shareability;
MpuMemType memType;
} MPU_CFG_PARA;
VOID HalMpuEnable(UINT32 defaultRegionEnable);
VOID HalMpuDisable();
UINT32 HalMpuSetRegion(UINT32 regionId, MPU_CFG_PARA *para);
UINT32 HalMpuDisableRegion(UINT32 regionId);
INT32 HalMpuUnusedRegionGet(VOID);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_MPU_H */
@@ -0,0 +1,111 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LOS_TIMER_H
#define _LOS_TIMER_H
#include "los_compiler.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
#define TICK_CHECK 0x4000000
#define CYCLE_CHECK 0xFFFFFFFFU
#define SHIFT_32_BIT 32
#define MAX_HOUR 24
#define MAX_MINUTES 60
#define MAX_SECONDS 60
#define MILSEC 1000
#define RTC_WAKEUPCLOCK_RTCCLK 32768
#define RTC_WAKEUPCLOCK_RTCCLK_DIV 16
#define RTC_CALIBRATE_SLEEP_TIME 8
#define MACHINE_CYCLE_DEALAY_TIMES (LOSCFG_BASE_CORE_TICK_PER_SECOND << 2)
VOID HalTickLock(VOID);
VOID HalTickUnlock(VOID);
UINT32 HalEnterSleep(VOID);
/**
* @ingroup los_timer
* @brief Get systick cycle.
*
* @par Description:
* This API is used to get systick cycle and return current tick period.
*
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param: period [OUT] current tick period.
*
* @retval current tick count.
*
* @par Dependency:
* <ul><li>los_timer.h: the header file that contains the API declaration.</li></ul>
* @see
*/
UINT64 HalGetTickCycle(UINT32 *period);
/**
* @ingroup los_timer
* @brief reconfig systick, and clear SysTick_IRQn.
*
* @par Description:
* <ul>
* <li>This API is used to reconfig systick, and clear SysTick_IRQn.</li>
* </ul>
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param nextResponseTime [IN] tick period
*
* @retval None.
* @par Dependency:
* <ul><li>los_timer.h: the header file that contains the API declaration.</li></ul>
* @see None
*/
extern VOID HalSysTickReload(UINT64 nextResponseTime);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_TIMER_H */
@@ -0,0 +1,129 @@
/*
* Copyright (c) 2013-2020, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
* Copyright (c) 2021 Nuclei Limited. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LOS_ARCH_CONTEXT_H
#define _LOS_ARCH_CONTEXT_H
#include "los_compiler.h"
#include "los_context.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
//#define ARCH_RISCV_FPU
/**
* @ingroup los_hw
*/
typedef unsigned long STACK_TYPE;
typedef struct {
STACK_TYPE epc; /* epc - epc - program counter */
STACK_TYPE ra; /* x1 - ra - return address for jumps */
STACK_TYPE t0; /* x5 - t0 - temporary register 0 */
STACK_TYPE t1; /* x6 - t1 - temporary register 1 */
STACK_TYPE t2; /* x7 - t2 - temporary register 2 */
STACK_TYPE s0_fp; /* x8 - s0/fp - saved register 0 or frame pointer */
STACK_TYPE s1; /* x9 - s1 - saved register 1 */
STACK_TYPE a0; /* x10 - a0 - return value or function argument 0 */
STACK_TYPE a1; /* x11 - a1 - return value or function argument 1 */
STACK_TYPE a2; /* x12 - a2 - function argument 2 */
STACK_TYPE a3; /* x13 - a3 - function argument 3 */
STACK_TYPE a4; /* x14 - a4 - function argument 4 */
STACK_TYPE a5; /* x15 - a5 - function argument 5 */
#ifndef __riscv_32e
STACK_TYPE a6; /* x16 - a6 - function argument 6 */
STACK_TYPE a7; /* x17 - s7 - function argument 7 */
STACK_TYPE s2; /* x18 - s2 - saved register 2 */
STACK_TYPE s3; /* x19 - s3 - saved register 3 */
STACK_TYPE s4; /* x20 - s4 - saved register 4 */
STACK_TYPE s5; /* x21 - s5 - saved register 5 */
STACK_TYPE s6; /* x22 - s6 - saved register 6 */
STACK_TYPE s7; /* x23 - s7 - saved register 7 */
STACK_TYPE s8; /* x24 - s8 - saved register 8 */
STACK_TYPE s9; /* x25 - s9 - saved register 9 */
STACK_TYPE s10; /* x26 - s10 - saved register 10 */
STACK_TYPE s11; /* x27 - s11 - saved register 11 */
STACK_TYPE t3; /* x28 - t3 - temporary register 3 */
STACK_TYPE t4; /* x29 - t4 - temporary register 4 */
STACK_TYPE t5; /* x30 - t5 - temporary register 5 */
STACK_TYPE t6; /* x31 - t6 - temporary register 6 */
#endif
STACK_TYPE mstatus; /* - machine status register */
#ifdef ARCH_RISCV_FPU
STACK_TYPE f0;
STACK_TYPE f1;
STACK_TYPE f2;
STACK_TYPE f3;
STACK_TYPE f4;
STACK_TYPE f5;
STACK_TYPE f6;
STACK_TYPE f7;
STACK_TYPE f8;
STACK_TYPE f9;
STACK_TYPE f10;
STACK_TYPE f11;
STACK_TYPE f12;
STACK_TYPE f13;
STACK_TYPE f14;
STACK_TYPE f15;
STACK_TYPE f16;
STACK_TYPE f17;
STACK_TYPE f18;
STACK_TYPE f19;
STACK_TYPE f20;
STACK_TYPE f21;
STACK_TYPE f22;
STACK_TYPE f23;
STACK_TYPE f24;
STACK_TYPE f25;
STACK_TYPE f26;
STACK_TYPE f27;
STACK_TYPE f28;
STACK_TYPE f29;
STACK_TYPE f30;
STACK_TYPE f31;
#endif
} TaskContext;
extern VOID HalStartToRun(VOID);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_HW_H */
@@ -0,0 +1,207 @@
/*
* Copyright (c) 2013-2020, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
* Copyright (c) 2021 Nuclei Limited. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LOS_HWI_H
#define _LOS_HWI_H
#include "ch32v20x.h"
#include "los_compiler.h"
#include "los_config.h"
#include "los_interrupt.h"
#include "los_arch_context.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
/**
* @ingroup los_hwi
* Count of Nuclei system interrupt vector.
*/
#define SOC_INT_MAX 103
#define OS_RISCV_SYS_VECTOR_CNT 16
/**
* @ingroup los_hwi
* Count of Nuclei interrupt vector maxium, which is configurable.
*/
#define OS_RISCV_CUSTOM_IRQ_VECTOR_CNT SOC_INT_MAX
/**
* @ingroup los_hwi
* Count of Nuclei interrupt vector.
*/
#define OS_RISCV_VECTOR_CNT (OS_RISCV_SYS_VECTOR_CNT + OS_RISCV_CUSTOM_IRQ_VECTOR_CNT)
/**
* Maximum number of supported hardware devices that generate hardware interrupts.
*/
#define OS_HWI_MAX_NUM (OS_RISCV_VECTOR_CNT-1)
extern VOID HalHwiDefaultHandler(VOID);
/**
* @ingroup los_hwi
* Hardware interrupt error code: Invalid interrupt number.
*
* Value: 0x02000900
*
* Solution: Ensure that the interrupt number is valid. The value range of the interrupt number applicable
* for a risc-v platform is [0, OS_RISCV_VECTOR_CNT].
*/
#define OS_ERRNO_HWI_NUM_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x00)
/**
* @ingroup los_hwi
* Hardware interrupt error code: Null hardware interrupt handling function.
*
* Value: 0x02000901
*
* Solution: Pass in a valid non-null hardware interrupt handling function.
*/
#define OS_ERRNO_HWI_PROC_FUNC_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x01)
/**
* @ingroup los_hwi
* Hardware interrupt error code: Insufficient interrupt resources for hardware interrupt creation.
*
* Value: 0x02000902
*
* Solution: Increase the configured maximum number of supported hardware interrupts.
*/
#define OS_ERRNO_HWI_CB_UNAVAILABLE LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x02)
/**
* @ingroup los_hwi
* Hardware interrupt error code: Insufficient memory for hardware interrupt initialization.
*
* Value: 0x02000903
*
* Solution: Expand the configured memory.
*/
#define OS_ERRNO_HWI_NO_MEMORY LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x03)
/**
* @ingroup los_hwi
* Hardware interrupt error code: The interrupt has already been created.
*
* Value: 0x02000904
*
* Solution: Check whether the interrupt specified by the passed-in interrupt number has already been created.
*/
#define OS_ERRNO_HWI_ALREADY_CREATED LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x04)
/**
* @ingroup los_hwi
* Hardware interrupt error code: Invalid interrupt priority.
*
* Value: 0x02000905
*
* Solution: Ensure that the interrupt priority is valid.
*/
#define OS_ERRNO_HWI_PRIO_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x05)
/**
* @ingroup los_hwi
* Hardware interrupt error code: Incorrect interrupt creation mode.
*
* Value: 0x02000906
*
* Solution: The interrupt creation mode can be only set to ECLIC_NON_VECTOR_INTERRUPT or ECLIC_VECTOR_INTERRUPT of which the
* value can be 0 or 1.
*/
#define OS_ERRNO_HWI_MODE_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x06)
/**
* @ingroup los_hwi
* Hardware interrupt error code: The interrupt has already been created as a fast interrupt.
*
* Value: 0x02000907
*
* Solution: Check whether the interrupt specified by the passed-in interrupt number has already been created.
*/
#define OS_ERRNO_HWI_FASTMODE_ALREADY_CREATED LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x07)
/**
* @ingroup los_hwi
* Hardware interrupt error code: The API is called during an interrupt, which is forbidden.
*
* Value: 0x02000908
*
* * Solution: Do not call the API during an interrupt.
*/
#define OS_ERRNO_HWI_INTERR LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x08)
/**
* @ingroup los_hwi
* Hardware interrupt error code:the hwi support SHARED error.
*
* Value: 0x02000909
*
* * Solution:check the input params hwiMode and irqParam of HalHwiCreate or HalHwiDelete whether adapt the current
* hwi.
*/
#define OS_ERRNO_HWI_SHARED_ERROR LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x09)
/**
* @ingroup los_hwi
* Hardware interrupt error code:Invalid interrupt Arg.
*
* Value: 0x0200090a
*
* * Solution:check the interrupt Arg, Arg should only be ECLIC_LEVEL_TRIGGER, ECLIC_POSTIVE_EDGE_TRIGGER or
* ECLIC_NEGTIVE_EDGE_TRIGGER.
*/
#define OS_ERRNO_HWI_ARG_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x0a)
/**
* @ingroup los_hwi
* Hardware interrupt error code:The interrupt corresponded to the hwi number or devid has not been created.
*
* Value: 0x0200090b
*
* * Solution:check the hwi number or devid, make sure the hwi number or devid need to delete.
*/
#define OS_ERRNO_HWI_HWINUM_UNCREATE LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x0b)
extern UINT32 HalUnalignedAccessFix(UINTPTR mcause, UINTPTR mepc, UINTPTR mtval, VOID *sp);
extern VOID DisplayTaskInfo(VOID);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_HWI_H */
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
* Copyright (c) 2021 Nuclei Limited. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LOS_ARCH_TIMER_H
#define _LOS_ARCH_TIMER_H
#include "los_config.h"
#include "los_compiler.h"
#include "los_context.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
UINT32 HalTickStart(OS_TICK_HANDLER handler);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_ARCH_TIMER_H */
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2021 Nuclei Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "los_task.h"
#include "los_memory.h"
#include "los_timer.h"
#include "los_sched.h"
#include "los_interrupt.h"
#include "ch32v20x.h"
#include "los_arch_context.h"
#include "los_arch_interrupt.h"
#include "los_arch_timer.h"
#include "riscv_bits.h"
#include "riscv_encoding.h"
extern VOID HalHwiInit(VOID);
#define INITIAL_MSTATUS 0x1880 //( MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_FS_INITIAL)
#define ALIGN_DOWN(size, align) ((size) & ~((align) - 1))
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
LITE_OS_SEC_TEXT_INIT VOID HalArchInit(VOID)
{
HalHwiInit();
}
LITE_OS_SEC_TEXT_MINOR VOID HalSysExit(VOID)
{
HalIntLock();
while (1) {
}
}
LITE_OS_SEC_TEXT_INIT VOID *HalTskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack)
{
UINT32 index;
UINT8 *stk = 0;
TaskContext *context = NULL;
/* initialize the task stack, write magic num to stack top */
*((UINT32 *)(topStack)) = OS_TASK_MAGIC_WORD;
stk = ((UINT8 *)topStack) + stackSize + sizeof(STACK_TYPE);
stk = (UINT8 *)ALIGN_DOWN((uintptr_t)stk, REGBYTES);
context = (TaskContext *)(stk - sizeof(TaskContext));
for (index = 1; index < sizeof(TaskContext)/ sizeof(STACK_TYPE); index ++) {
((STACK_TYPE *)context)[index] = OS_TASK_STACK_INIT;
}
context->ra = (STACK_TYPE)HalSysExit;
context->a0 = (STACK_TYPE)taskID;
context->epc = (STACK_TYPE)OsTaskEntry;
context->mstatus = INITIAL_MSTATUS;
return (VOID *)context;
}
extern LosTask g_losTask;
LITE_OS_SEC_TEXT_INIT UINT32 HalStartSchedule(OS_TICK_HANDLER handler)
{
UINT32 ret;
(VOID)LOS_IntLock();
ret = HalTickStart(handler);
if (ret != LOS_OK) {
return ret;
}
OsSchedStart();
HalStartToRun();
return LOS_OK; /* never return */
}
VOID HalTaskSchedule(VOID)
{
// printf("sw\r\n");
NVIC_SetPendingIRQ(Software_IRQn);
}
VOID HalTaskSwitch(VOID)
{
OsSchedTaskSwitch();
/* Set newTask to runTask */
g_losTask.runTask = g_losTask.newTask;
}
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
@@ -0,0 +1,350 @@
/*
* Copyright (c) 2021 Nuclei Limited. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "riscv_encoding.h"
//#define ARCH_RISCV_FPU
#ifndef __riscv_32e
#define portRegNum 30
#else
#define portRegNum 14
#endif
#ifdef ARCH_RISCV_FPU
#define portFloatRegNum 32
#endif
#define portCONTEXT_SIZE ( portRegNum * REGBYTES )
.section .text,"ax",@progbits
.align 2
.type HalIntLock, %function
.global HalIntLock
HalIntLock:
csrr a0, mstatus /* return value */
li t0, (MSTATUS_MIE|MSTATUS_MPIE) /* mie|mpie */
csrrc zero, mstatus, t0
ret
.type HalIntUnLock, %function
.global HalIntUnLock
HalIntUnLock:
csrr a0, mstatus /* return value*/
li t0, (MSTATUS_MIE|MSTATUS_MPIE) /* mie|mpie */
csrrs zero, mstatus, t0
ret
.type HalIntRestore, %function
.global HalIntRestore
HalIntRestore:
csrw mstatus, a0
ret
/* Start the first task. This also clears the bit that indicates the FPU is
in use in case the FPU was used before the scheduler was started - which
would otherwise result in the unnecessary leaving of space in the stack
for lazy saving of FPU registers. */
.type HalStartToRun, %function
.global HalStartToRun
.align 2
HalStartToRun:
/* Setup Interrupt Stack using
The stack that was used by main()
before the scheduler is started is
no longer required after the scheduler is started.
Interrupt stack pointer is stored in CSR_MSCRATCH */
la t0, _eusrstack
addi t0,t0, -512
csrw CSR_MSCRATCH, t0
/* get stack pointer */
la t0, g_losTask
LOAD t1, 0x0(t0)
LOAD sp, 0(t1) /* TCB.sp->sp */
/* Pop PC from stack and set MEPC */
LOAD t0, 0 * REGBYTES(sp)
csrw CSR_MEPC, t0
/* Pop mstatus from stack and set it */
LOAD t0, (portRegNum - 1) * REGBYTES(sp)
csrw CSR_MSTATUS, t0
/* Interrupt still disable here */
/* Restore Registers from Stack */
LOAD x1, 1 * REGBYTES(sp) /* RA */
LOAD x5, 2 * REGBYTES(sp)
LOAD x6, 3 * REGBYTES(sp)
LOAD x7, 4 * REGBYTES(sp)
LOAD x8, 5 * REGBYTES(sp)
LOAD x9, 6 * REGBYTES(sp)
LOAD x10, 7 * REGBYTES(sp)
LOAD x11, 8 * REGBYTES(sp)
LOAD x12, 9 * REGBYTES(sp)
LOAD x13, 10 * REGBYTES(sp)
LOAD x14, 11 * REGBYTES(sp)
LOAD x15, 12 * REGBYTES(sp)
#ifndef __riscv_32e
LOAD x16, 13 * REGBYTES(sp)
LOAD x17, 14 * REGBYTES(sp)
LOAD x18, 15 * REGBYTES(sp)
LOAD x19, 16 * REGBYTES(sp)
LOAD x20, 17 * REGBYTES(sp)
LOAD x21, 18 * REGBYTES(sp)
LOAD x22, 19 * REGBYTES(sp)
LOAD x23, 20 * REGBYTES(sp)
LOAD x24, 21 * REGBYTES(sp)
LOAD x25, 22 * REGBYTES(sp)
LOAD x26, 23 * REGBYTES(sp)
LOAD x27, 24 * REGBYTES(sp)
LOAD x28, 25 * REGBYTES(sp)
LOAD x29, 26 * REGBYTES(sp)
LOAD x30, 27 * REGBYTES(sp)
LOAD x31, 28 * REGBYTES(sp)
#endif
addi sp, sp, portCONTEXT_SIZE
#ifdef ARCH_RISCV_FPU
FPLOAD f0, 0 * FPREGBYTES(sp)
FPLOAD f1, 1 * FPREGBYTES(sp)
FPLOAD f2, 2 * FPREGBYTES(sp)
FPLOAD f3, 3 * FPREGBYTES(sp)
FPLOAD f4, 4 * FPREGBYTES(sp)
FPLOAD f5, 5 * FPREGBYTES(sp)
FPLOAD f6, 6 * FPREGBYTES(sp)
FPLOAD f7, 7 * FPREGBYTES(sp)
FPLOAD f8, 8 * FPREGBYTES(sp)
FPLOAD f9, 9 * FPREGBYTES(sp)
FPLOAD f10, 10 * FPREGBYTES(sp)
FPLOAD f11, 11 * FPREGBYTES(sp)
FPLOAD f12, 12 * FPREGBYTES(sp)
FPLOAD f13, 13 * FPREGBYTES(sp)
FPLOAD f14, 14 * FPREGBYTES(sp)
FPLOAD f15, 15 * FPREGBYTES(sp)
FPLOAD f16, 16 * FPREGBYTES(sp)
FPLOAD f17, 17 * FPREGBYTES(sp)
FPLOAD f18, 18 * FPREGBYTES(sp)
FPLOAD f19, 19 * FPREGBYTES(sp)
FPLOAD f20, 20 * FPREGBYTES(sp)
FPLOAD f21, 21 * FPREGBYTES(sp)
FPLOAD f22, 22 * FPREGBYTES(sp)
FPLOAD f23, 23 * FPREGBYTES(sp)
FPLOAD f24, 24 * FPREGBYTES(sp)
FPLOAD f25, 25 * FPREGBYTES(sp)
FPLOAD f26, 26 * FPREGBYTES(sp)
FPLOAD f27, 27 * FPREGBYTES(sp)
FPLOAD f28, 28 * FPREGBYTES(sp)
FPLOAD f29, 29 * FPREGBYTES(sp)
FPLOAD f30, 30 * FPREGBYTES(sp)
FPLOAD f31, 31 * FPREGBYTES(sp)
addi sp, sp, portFloatRegNum * FPREGBYTES
#endif
mret
.extern HalTaskSwitch
.section .text.vector_handler, "ax", @progbits
.global SW_Handler
.align 2
SW_Handler:
#ifdef ARCH_RISCV_FPU
addi sp, sp, -portFloatRegNum * FPREGBYTES
FPSTORE f0, 0 * FPREGBYTES(sp)
FPSTORE f1, 1 * FPREGBYTES(sp)
FPSTORE f2, 2 * FPREGBYTES(sp)
FPSTORE f3, 3 * FPREGBYTES(sp)
FPSTORE f4, 4 * FPREGBYTES(sp)
FPSTORE f5, 5 * FPREGBYTES(sp)
FPSTORE f6, 6 * FPREGBYTES(sp)
FPSTORE f7, 7 * FPREGBYTES(sp)
FPSTORE f8, 8 * FPREGBYTES(sp)
FPSTORE f9, 9 * FPREGBYTES(sp)
FPSTORE f10, 10 * FPREGBYTES(sp)
FPSTORE f11, 11 * FPREGBYTES(sp)
FPSTORE f12, 12 * FPREGBYTES(sp)
FPSTORE f13, 13 * FPREGBYTES(sp)
FPSTORE f14, 14 * FPREGBYTES(sp)
FPSTORE f15, 15 * FPREGBYTES(sp)
FPSTORE f16, 16 * FPREGBYTES(sp)
FPSTORE f17, 17 * FPREGBYTES(sp)
FPSTORE f18, 18 * FPREGBYTES(sp)
FPSTORE f19, 19 * FPREGBYTES(sp)
FPSTORE f20, 20 * FPREGBYTES(sp)
FPSTORE f21, 21 * FPREGBYTES(sp)
FPSTORE f22, 22 * FPREGBYTES(sp)
FPSTORE f23, 23 * FPREGBYTES(sp)
FPSTORE f24, 24 * FPREGBYTES(sp)
FPSTORE f25, 25 * FPREGBYTES(sp)
FPSTORE f26, 26 * FPREGBYTES(sp)
FPSTORE f27, 27 * FPREGBYTES(sp)
FPSTORE f28, 28 * FPREGBYTES(sp)
FPSTORE f29, 29 * FPREGBYTES(sp)
FPSTORE f30, 30 * FPREGBYTES(sp)
FPSTORE f31, 31 * FPREGBYTES(sp)
#endif
addi sp, sp, -portCONTEXT_SIZE
STORE x1, 1 * REGBYTES(sp) /* RA */
STORE x5, 2 * REGBYTES(sp)
STORE x6, 3 * REGBYTES(sp)
STORE x7, 4 * REGBYTES(sp)
STORE x8, 5 * REGBYTES(sp)
STORE x9, 6 * REGBYTES(sp)
STORE x10, 7 * REGBYTES(sp)
STORE x11, 8 * REGBYTES(sp)
STORE x12, 9 * REGBYTES(sp)
STORE x13, 10 * REGBYTES(sp)
STORE x14, 11 * REGBYTES(sp)
STORE x15, 12 * REGBYTES(sp)
#ifndef __riscv_32e
STORE x16, 13 * REGBYTES(sp)
STORE x17, 14 * REGBYTES(sp)
STORE x18, 15 * REGBYTES(sp)
STORE x19, 16 * REGBYTES(sp)
STORE x20, 17 * REGBYTES(sp)
STORE x21, 18 * REGBYTES(sp)
STORE x22, 19 * REGBYTES(sp)
STORE x23, 20 * REGBYTES(sp)
STORE x24, 21 * REGBYTES(sp)
STORE x25, 22 * REGBYTES(sp)
STORE x26, 23 * REGBYTES(sp)
STORE x27, 24 * REGBYTES(sp)
STORE x28, 25 * REGBYTES(sp)
STORE x29, 26 * REGBYTES(sp)
STORE x30, 27 * REGBYTES(sp)
STORE x31, 28 * REGBYTES(sp)
#endif
// li t0, 0x20
// csrs 0x804, t0
/* Push mstatus to stack */
csrr t0, CSR_MSTATUS
STORE t0, (portRegNum - 1) * REGBYTES(sp)
/* Push additional registers */
/* Store sp to task stack */
la t0, g_losTask
LOAD t0, 0(t0)
STORE sp, 0(t0)
csrr t0, CSR_MEPC
STORE t0, 0(sp)
/* Switch task context */
jal HalTaskSwitch
/* Load new task */
la t0, g_losTask
LOAD t0, 0(t0)
LOAD sp, 0x0(t0) /* Read sp from first TCB member */
/* Pop PC from stack and set MEPC */
LOAD t0, 0 * REGBYTES(sp)
csrw CSR_MEPC, t0
/* Pop additional registers */
/* Pop mstatus from stack and set it */
LOAD t0, (portRegNum - 1) * REGBYTES(sp)
csrw CSR_MSTATUS, t0
/* Interrupt still disable here */
/* Restore Registers from Stack */
LOAD x1, 1 * REGBYTES(sp) /* RA */
LOAD x5, 2 * REGBYTES(sp)
LOAD x6, 3 * REGBYTES(sp)
LOAD x7, 4 * REGBYTES(sp)
LOAD x8, 5 * REGBYTES(sp)
LOAD x9, 6 * REGBYTES(sp)
LOAD x10, 7 * REGBYTES(sp)
LOAD x11, 8 * REGBYTES(sp)
LOAD x12, 9 * REGBYTES(sp)
LOAD x13, 10 * REGBYTES(sp)
LOAD x14, 11 * REGBYTES(sp)
LOAD x15, 12 * REGBYTES(sp)
#ifndef __riscv_32e
LOAD x16, 13 * REGBYTES(sp)
LOAD x17, 14 * REGBYTES(sp)
LOAD x18, 15 * REGBYTES(sp)
LOAD x19, 16 * REGBYTES(sp)
LOAD x20, 17 * REGBYTES(sp)
LOAD x21, 18 * REGBYTES(sp)
LOAD x22, 19 * REGBYTES(sp)
LOAD x23, 20 * REGBYTES(sp)
LOAD x24, 21 * REGBYTES(sp)
LOAD x25, 22 * REGBYTES(sp)
LOAD x26, 23 * REGBYTES(sp)
LOAD x27, 24 * REGBYTES(sp)
LOAD x28, 25 * REGBYTES(sp)
LOAD x29, 26 * REGBYTES(sp)
LOAD x30, 27 * REGBYTES(sp)
LOAD x31, 28 * REGBYTES(sp)
#endif
addi sp, sp, portCONTEXT_SIZE
#ifdef ARCH_RISCV_FPU
FPLOAD f0, 0 * FPREGBYTES(sp)
FPLOAD f1, 1 * FPREGBYTES(sp)
FPLOAD f2, 2 * FPREGBYTES(sp)
FPLOAD f3, 3 * FPREGBYTES(sp)
FPLOAD f4, 4 * FPREGBYTES(sp)
FPLOAD f5, 5 * FPREGBYTES(sp)
FPLOAD f6, 6 * FPREGBYTES(sp)
FPLOAD f7, 7 * FPREGBYTES(sp)
FPLOAD f8, 8 * FPREGBYTES(sp)
FPLOAD f9, 9 * FPREGBYTES(sp)
FPLOAD f10, 10 * FPREGBYTES(sp)
FPLOAD f11, 11 * FPREGBYTES(sp)
FPLOAD f12, 12 * FPREGBYTES(sp)
FPLOAD f13, 13 * FPREGBYTES(sp)
FPLOAD f14, 14 * FPREGBYTES(sp)
FPLOAD f15, 15 * FPREGBYTES(sp)
FPLOAD f16, 16 * FPREGBYTES(sp)
FPLOAD f17, 17 * FPREGBYTES(sp)
FPLOAD f18, 18 * FPREGBYTES(sp)
FPLOAD f19, 19 * FPREGBYTES(sp)
FPLOAD f20, 20 * FPREGBYTES(sp)
FPLOAD f21, 21 * FPREGBYTES(sp)
FPLOAD f22, 22 * FPREGBYTES(sp)
FPLOAD f23, 23 * FPREGBYTES(sp)
FPLOAD f24, 24 * FPREGBYTES(sp)
FPLOAD f25, 25 * FPREGBYTES(sp)
FPLOAD f26, 26 * FPREGBYTES(sp)
FPLOAD f27, 27 * FPREGBYTES(sp)
FPLOAD f28, 28 * FPREGBYTES(sp)
FPLOAD f29, 29 * FPREGBYTES(sp)
FPLOAD f30, 30 * FPREGBYTES(sp)
FPLOAD f31, 31 * FPREGBYTES(sp)
addi sp, sp, portFloatRegNum * FPREGBYTES
#endif
mret
@@ -0,0 +1,181 @@
/*
* Copyright (c) 2021 Nuclei Limited. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LOS_EXC_S
#define _LOS_EXC_S
#include "riscv_encoding.h"
.section .text
.align 8
/**
* \brief Global interrupt disabled
* \details
* This function disable global interrupt.
* \remarks
* - All the interrupt requests will be ignored by CPU.
*/
.macro DISABLE_MIE
csrc CSR_MSTATUS, MSTATUS_MIE
.endm
/**
* \brief Macro for context save
* \details
* This macro save ABI defined caller saved registers in the stack.
* \remarks
* - This Macro could use to save context when you enter to interrupt
* or exception
*/
/* Save caller registers */
.macro SAVE_CONTEXT
csrrw sp, CSR_MSCRATCH, sp /* 把中断栈给sp,并把线程栈存到mscratch */
/* Allocate stack space for context saving */
#ifndef __riscv_32e
addi sp, sp, -20*REGBYTES
#else
addi sp, sp, -14*REGBYTES
#endif /* __riscv_32e */
STORE x1, 0*REGBYTES(sp)
STORE x4, 1*REGBYTES(sp)
STORE x5, 2*REGBYTES(sp)
STORE x6, 3*REGBYTES(sp)
STORE x7, 4*REGBYTES(sp)
STORE x10, 5*REGBYTES(sp)
STORE x11, 6*REGBYTES(sp)
STORE x12, 7*REGBYTES(sp)
STORE x13, 8*REGBYTES(sp)
STORE x14, 9*REGBYTES(sp)
STORE x15, 10*REGBYTES(sp)
#ifndef __riscv_32e
STORE x16, 14*REGBYTES(sp)
STORE x17, 15*REGBYTES(sp)
STORE x28, 16*REGBYTES(sp)
STORE x29, 17*REGBYTES(sp)
STORE x30, 18*REGBYTES(sp)
STORE x31, 19*REGBYTES(sp)
#endif /* __riscv_32e */
.endm
/**
* \brief Macro for restore caller registers
* \details
* This macro restore ABI defined caller saved registers from stack.
* \remarks
* - You could use this macro to restore context before you want return
* from interrupt or exeception
*/
/* Restore caller registers */
.macro RESTORE_CONTEXT
LOAD x1, 0*REGBYTES(sp)
LOAD x4, 1*REGBYTES(sp)
LOAD x5, 2*REGBYTES(sp)
LOAD x6, 3*REGBYTES(sp)
LOAD x7, 4*REGBYTES(sp)
LOAD x10, 5*REGBYTES(sp)
LOAD x11, 6*REGBYTES(sp)
LOAD x12, 7*REGBYTES(sp)
LOAD x13, 8*REGBYTES(sp)
LOAD x14, 9*REGBYTES(sp)
LOAD x15, 10*REGBYTES(sp)
#ifndef __riscv_32e
LOAD x16, 14*REGBYTES(sp)
LOAD x17, 15*REGBYTES(sp)
LOAD x28, 16*REGBYTES(sp)
LOAD x29, 17*REGBYTES(sp)
LOAD x30, 18*REGBYTES(sp)
LOAD x31, 19*REGBYTES(sp)
/* De-allocate the stack space */
addi sp, sp, 20*REGBYTES
#else
/* De-allocate the stack space */
addi sp, sp, 14*REGBYTES
#endif /* __riscv_32e */
csrrw sp, CSR_MSCRATCH, sp /* 退出后把线程栈给sp,把中断栈存入mscratch */
.endm
/**
* \brief Interrupt Entry
* \details
* This function provide common entry functions for handling
* non-vector interrupts
* \remarks
* This function provide a default non-vector interrupt entry.
* ABI defined caller save register and some CSR registers need
* to be saved before enter interrupt handler and be restored before return.
*/
.section .text.vector_handler
/* In CLIC mode, the interrupt entry must be 4bytes aligned */
.align 2
.extern g_intCount
.global irq_entry
/* This label will be set to MTVT2 register */
irq_entry:
/* Save the caller saving registers (context) */
SAVE_CONTEXT
/* This special CSR read/write operation, which is actually
* claim the CLIC to find its pending highest ID, if the ID
* is not 0, then automatically enable the mstatus.MIE, and
* jump to its vector-entry-label, and update the link register
*/
la t0, g_intCount
lw t1, 0(t0)
add t1, t1, 0x1
sw t1, 0(t0)
/* Critical section with interrupts disabled */
DISABLE_MIE
/*************************/
/* jal irq_handler */ /* 跳转到中断处理函数 */
/*************************/
la t0, g_intCount
lw t1, 0(t0)
li t2, 0x1
sub t1, t1, t2
sw t1, 0(t0)
/* Restore the caller saving registers (context) */
RESTORE_CONTEXT
/* Return to regular code */
mret
/* Default Handler for Exceptions / Interrupts */
.global default_intexc_handler
Undef_Handler:
default_intexc_handler:
1:
j 1b
#endif /* _LOS_TRAP_S */
@@ -0,0 +1,176 @@
/*
* Copyright (c) 2021 Nuclei Limited. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdarg.h>
#include "los_arch.h"
#include "los_task.h"
#include "los_debug.h"
#include "ch32v20x.h"
#include "los_arch_context.h"
#include "los_arch_interrupt.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
UINT32 g_intCount = 0;
// LosExcInfo g_excInfo;
LITE_OS_SEC_TEXT_INIT VOID HalHwiInit(VOID)
{
// already setup interrupt vectors
}
/*****************************************************************************
Function : HalHwiCreate
Description : create hardware interrupt
Input : hwiNum --- hwi num to create
hwiPrio --- priority of the hwi
mode --- hwi interrupt mode, between vector or non-vector
handler --- hwi handler
arg --- set trig mode of the hwi handler
Level Triggerred = 0
Postive/Rising Edge Triggered = 1
Negtive/Falling Edge Triggered = 3
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
UINT32 HalHwiCreate(HWI_HANDLE_T hwiNum,
HWI_PRIOR_T hwiPrio,
HWI_MODE_T mode,
HWI_PROC_FUNC handler,
HWI_ARG_T arg)
{
if (hwiNum > SOC_INT_MAX){
return OS_ERRNO_HWI_NUM_INVALID;
}
// if (mode > ECLIC_VECTOR_INTERRUPT){
// return OS_ERRNO_HWI_MODE_INVALID;
// }
// if (arg > ECLIC_NEGTIVE_EDGE_TRIGGER){
// return OS_ERRNO_HWI_ARG_INVALID;
// }
NVIC_SetPriority(hwiNum,(UINT8)hwiPrio);
NVIC_EnableIRQ(hwiNum);
return LOS_OK;
}
/*****************************************************************************
Function : HalHwiDelete
Description : Delete hardware interrupt
Input : hwiNum --- hwi num to delete
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT UINT32 HalHwiDelete(HWI_HANDLE_T hwiNum)
{
NVIC_DisableIRQ(hwiNum);
return LOS_OK;
}
/* ****************************************************************************
Function : HalHwiDefaultHandler
Description : default handler of the hardware interrupt
Input : None
Output : None
Return : None
**************************************************************************** */
LITE_OS_SEC_TEXT_INIT VOID HalHwiDefaultHandler(VOID)
{
PRINT_ERR("default handler\n");
while (1) {
}
}
/* ****************************************************************************
Function : HalDisplayTaskInfo
Description : display the task list
Input : None
Output : None
Return : None
**************************************************************************** */
VOID HalDisplayTaskInfo(VOID)
{
TSK_INFO_S taskInfo;
UINT32 index;
UINT32 ret;
PRINTK("ID Pri Status name \r\n");
PRINTK("-- --- --------- ----\r\n");
for (index = 0; index < LOSCFG_BASE_CORE_TSK_LIMIT; index++) {
ret = LOS_TaskInfoGet(index, &taskInfo);
if (ret != LOS_OK) {
continue;
}
PRINTK("%d %d %s %s \r\n",
taskInfo.uwTaskID, taskInfo.usTaskPrio, OsConvertTskStatus(taskInfo.usTaskStatus), taskInfo.acName);
}
return;
}
/* ****************************************************************************
Function : HalUnalignedAccessFix
Description : Unaligned acess fixes are not supported by default
Input : None
Output : None
Return : None
**************************************************************************** */
WEAK UINT32 HalUnalignedAccessFix(UINTPTR mcause, UINTPTR mepc, UINTPTR mtval, VOID *sp)
{
/* Unaligned acess fixes are not supported by default */
PRINTK("Unaligned acess fixes are not support by default!\r\n");
return LOS_NOK;
}
__attribute__((always_inline)) inline VOID HalIntEnter(VOID)
{
g_intCount += 1;
}
__attribute__((always_inline)) inline VOID HalIntExit(VOID)
{
g_intCount -= 1;
}
__attribute__((always_inline)) inline UINT32 HalIsIntActive(VOID)
{
return (g_intCount > 0);
}
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
@@ -0,0 +1,132 @@
/*
* Copyright (c) 2013-2020, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
* Copyright (c) 2021 Nuclei Limited. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "los_timer.h"
#include "los_config.h"
#include "los_tick.h"
#include "los_reg.h"
#include "los_sched.h"
#include "core_riscv.h"
#include "los_arch_interrupt.h"
#include "los_arch_timer.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
#define configKERNEL_INTERRUPT_PRIORITY 0
//#define SYSTICK_TICK_CONST (OS_SYS_CLOCK / LOSCFG_BASE_CORE_TICK_PER_SECOND)
static OS_TICK_HANDLER systick_handler = (OS_TICK_HANDLER)NULL;
extern UINT32 g_intCount;
WEAK UINT32 HalTickStart(OS_TICK_HANDLER handler)
{
g_sysClock = OS_SYS_CLOCK;
g_cyclesPerTick = g_sysClock / LOSCFG_BASE_CORE_TICK_PER_SECOND;
g_intCount = 0;
NVIC_EnableIRQ(SysTicK_IRQn);
NVIC_EnableIRQ(Software_IRQn);
NVIC_SetPriority(SysTicK_IRQn,0xf0);
NVIC_SetPriority(Software_IRQn,0xf0);
systick_handler = handler;
SysTick->SR=0;
SysTick->CMP=g_cyclesPerTick-1;
SysTick->CNT=0;
SysTick->CTLR=0xf;
return LOS_OK; /* never return */
}
void SysTick_Handler(void) __attribute__((interrupt()));
#define HalTickSysTickHandler SysTick_Handler
void HalTickSysTickHandler( void )
{
/* Do systick handler registered in HalTickStart. */
if ((void *)systick_handler != NULL) {
systick_handler();
}
}
WEAK VOID HalSysTickReload(UINT64 nextResponseTime)
{
SysTick->CTLR &= ~(1<<0);
SysTick->CMP = nextResponseTime-1;
SysTick->CNT = 0;
SysTick->SR = 0;
NVIC_ClearPendingIRQ(SysTicK_IRQn);
SysTick->CTLR |= (1<<0);
}
WEAK UINT64 HalGetTickCycle(UINT32 *period)
{
UINT64 ticks;
UINT32 intSave = LOS_IntLock();
ticks = SysTick->CNT;
*period =SysTick->CMP;
LOS_IntRestore(intSave);
return ticks;
}
WEAK VOID HalTickLock(VOID)
{
SysTick->CTLR &= ~(1<<0);
}
WEAK VOID HalTickUnlock(VOID)
{
SysTick->CTLR |= (1<<0);
}
UINT32 HalEnterSleep(VOID)
{
__WFI();
return LOS_OK;
}
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2019 Nuclei Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __RISCV_BITS_H__
#define __RISCV_BITS_H__
#ifdef __cplusplus
extern "C" {
#endif
#if __riscv_xlen == 64
# define SLL32 sllw
# define STORE sd
# define LOAD ld
# define LWU lwu
# define LOG_REGBYTES 3
#else
# define SLL32 sll
# define STORE sw
# define LOAD lw
# define LWU lw
# define LOG_REGBYTES 2
#endif /* __riscv_xlen */
#define REGBYTES (1 << LOG_REGBYTES)
#if __riscv_flen == 64
# define FPSTORE fsd
# define FPLOAD fld
# define LOG_FPREGBYTES 3
#else
# define FPSTORE fsw
# define FPLOAD flw
# define LOG_FPREGBYTES 2
#endif /* __riscv_flen */
#define FPREGBYTES (1 << LOG_FPREGBYTES)
#define __rv_likely(x) __builtin_expect((x), 1)
#define __rv_unlikely(x) __builtin_expect((x), 0)
#define __RV_ROUNDUP(a, b) ((((a)-1)/(b)+1)*(b))
#define __RV_ROUNDDOWN(a, b) ((a)/(b)*(b))
#define __RV_MAX(a, b) ((a) > (b) ? (a) : (b))
#define __RV_MIN(a, b) ((a) < (b) ? (a) : (b))
#define __RV_CLAMP(a, lo, hi) MIN(MAX(a, lo), hi)
#define __RV_EXTRACT_FIELD(val, which) (((val) & (which)) / ((which) & ~((which)-1)))
#define __RV_INSERT_FIELD(val, which, fieldval) (((val) & ~(which)) | ((fieldval) * ((which) & ~((which)-1))))
#ifdef __ASSEMBLY__
#define _AC(X,Y) X
#define _AT(T,X) X
#else
#define __AC(X,Y) (X##Y)
#define _AC(X,Y) __AC(X,Y)
#define _AT(T,X) ((T)(X))
#endif /* __ASSEMBLY__ */
#define _UL(x) (_AC(x, UL))
#define _ULL(x) (_AC(x, ULL))
#define _BITUL(x) (_UL(1) << (x))
#define _BITULL(x) (_ULL(1) << (x))
#define UL(x) (_UL(x))
#define ULL(x) (_ULL(x))
#define STR(x) XSTR(x)
#define XSTR(x) #x
#define __STR(s) #s
#define STRINGIFY(s) __STR(s)
#ifdef __cplusplus
}
#endif
#endif /** __RISCV_BITS_H__ */
@@ -0,0 +1,617 @@
/*
* Copyright (c) 2019 Nuclei Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __RISCV_ENCODING_H__
#define __RISCV_ENCODING_H__
#include "../../V4A/gcc/riscv_bits.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* \defgroup NMSIS_Core_CSR_Encoding Core CSR Encodings
* \ingroup NMSIS_Core
* \brief NMSIS Core CSR Encodings
* \details
*
* The following macros are used for CSR encodings
* @{
*/
#define MSTATUS_UIE 0x00000001
#define MSTATUS_SIE 0x00000002
#define MSTATUS_HIE 0x00000004
#define MSTATUS_MIE 0x00000008
#define MSTATUS_UPIE 0x00000010
#define MSTATUS_SPIE 0x00000020
#define MSTATUS_HPIE 0x00000040
#define MSTATUS_MPIE 0x00000080
#define MSTATUS_SPP 0x00000100
#define MSTATUS_MPP 0x00001800
#define MSTATUS_FS 0x00006000
#define MSTATUS_XS 0x00018000
#define MSTATUS_MPRV 0x00020000
#define MSTATUS_PUM 0x00040000
#define MSTATUS_MXR 0x00080000
#define MSTATUS_VM 0x1F000000
#define MSTATUS32_SD 0x80000000
#define MSTATUS64_SD 0x8000000000000000
#define MSTATUS_FS_INITIAL 0x00002000
#define MSTATUS_FS_CLEAN 0x00004000
#define MSTATUS_FS_DIRTY 0x00006000
#define SSTATUS_UIE 0x00000001
#define SSTATUS_SIE 0x00000002
#define SSTATUS_UPIE 0x00000010
#define SSTATUS_SPIE 0x00000020
#define SSTATUS_SPP 0x00000100
#define SSTATUS_FS 0x00006000
#define SSTATUS_XS 0x00018000
#define SSTATUS_PUM 0x00040000
#define SSTATUS32_SD 0x80000000
#define SSTATUS64_SD 0x8000000000000000
#define CSR_MCACHE_CTL_IE 0x00000001
#define CSR_MCACHE_CTL_DE 0x00010000
#define DCSR_XDEBUGVER (3U<<30)
#define DCSR_NDRESET (1<<29)
#define DCSR_FULLRESET (1<<28)
#define DCSR_EBREAKM (1<<15)
#define DCSR_EBREAKH (1<<14)
#define DCSR_EBREAKS (1<<13)
#define DCSR_EBREAKU (1<<12)
#define DCSR_STOPCYCLE (1<<10)
#define DCSR_STOPTIME (1<<9)
#define DCSR_CAUSE (7<<6)
#define DCSR_DEBUGINT (1<<5)
#define DCSR_HALT (1<<3)
#define DCSR_STEP (1<<2)
#define DCSR_PRV (3<<0)
#define DCSR_CAUSE_NONE 0
#define DCSR_CAUSE_SWBP 1
#define DCSR_CAUSE_HWBP 2
#define DCSR_CAUSE_DEBUGINT 3
#define DCSR_CAUSE_STEP 4
#define DCSR_CAUSE_HALT 5
#define MCONTROL_TYPE(xlen) (0xfULL<<((xlen)-4))
#define MCONTROL_DMODE(xlen) (1ULL<<((xlen)-5))
#define MCONTROL_MASKMAX(xlen) (0x3fULL<<((xlen)-11))
#define MCONTROL_SELECT (1<<19)
#define MCONTROL_TIMING (1<<18)
#define MCONTROL_ACTION (0x3f<<12)
#define MCONTROL_CHAIN (1<<11)
#define MCONTROL_MATCH (0xf<<7)
#define MCONTROL_M (1<<6)
#define MCONTROL_H (1<<5)
#define MCONTROL_S (1<<4)
#define MCONTROL_U (1<<3)
#define MCONTROL_EXECUTE (1<<2)
#define MCONTROL_STORE (1<<1)
#define MCONTROL_LOAD (1<<0)
#define MCONTROL_TYPE_NONE 0
#define MCONTROL_TYPE_MATCH 2
#define MCONTROL_ACTION_DEBUG_EXCEPTION 0
#define MCONTROL_ACTION_DEBUG_MODE 1
#define MCONTROL_ACTION_TRACE_START 2
#define MCONTROL_ACTION_TRACE_STOP 3
#define MCONTROL_ACTION_TRACE_EMIT 4
#define MCONTROL_MATCH_EQUAL 0
#define MCONTROL_MATCH_NAPOT 1
#define MCONTROL_MATCH_GE 2
#define MCONTROL_MATCH_LT 3
#define MCONTROL_MATCH_MASK_LOW 4
#define MCONTROL_MATCH_MASK_HIGH 5
#define MIP_SSIP (1 << IRQ_S_SOFT)
#define MIP_HSIP (1 << IRQ_H_SOFT)
#define MIP_MSIP (1 << IRQ_M_SOFT)
#define MIP_STIP (1 << IRQ_S_TIMER)
#define MIP_HTIP (1 << IRQ_H_TIMER)
#define MIP_MTIP (1 << IRQ_M_TIMER)
#define MIP_SEIP (1 << IRQ_S_EXT)
#define MIP_HEIP (1 << IRQ_H_EXT)
#define MIP_MEIP (1 << IRQ_M_EXT)
#define MIE_SSIE MIP_SSIP
#define MIE_HSIE MIP_HSIP
#define MIE_MSIE MIP_MSIP
#define MIE_STIE MIP_STIP
#define MIE_HTIE MIP_HTIP
#define MIE_MTIE MIP_MTIP
#define MIE_SEIE MIP_SEIP
#define MIE_HEIE MIP_HEIP
#define MIE_MEIE MIP_MEIP
/* === Nuclei custom CSR bit mask === */
#define WFE_WFE (0x1)
#define TXEVT_TXEVT (0x1)
#define SLEEPVALUE_SLEEPVALUE (0x1)
#define MCOUNTINHIBIT_IR (1<<2)
#define MCOUNTINHIBIT_CY (1<<0)
#define MILM_CTL_ILM_BPA (((1ULL<<((__riscv_xlen)-10))-1)<<10)
#define MILM_CTL_ILM_EN (1<<0)
#define MDLM_CTL_DLM_BPA (((1ULL<<((__riscv_xlen)-10))-1)<<10)
#define MDLM_CTL_DLM_EN (1<<0)
#define MSUBM_PTYP (0x3<<8)
#define MSUBM_TYP (0x3<<6)
#define MDCAUSE_MDCAUSE (0x3)
#define MMISC_CTL_NMI_CAUSE_FFF (1<<9)
#define MMISC_CTL_MISALIGN (1<<6)
#define MMISC_CTL_BPU (1<<3)
#define MCACHE_CTL_IC_EN (1<<0)
#define MCACHE_CTL_IC_SCPD_MOD (1<<1)
#define MCACHE_CTL_DC_EN (1<<16)
#define MTVT2_MTVT2EN (1<<0)
#define MTVT2_COMMON_CODE_ENTRY (((1ULL<<((__riscv_xlen)-2))-1)<<2)
#define MCFG_INFO_TEE (1<<0)
#define MCFG_INFO_ECC (1<<1)
#define MCFG_INFO_CLIC (1<<2)
#define MCFG_INFO_PLIC (1<<3)
#define MCFG_INFO_FIO (1<<4)
#define MCFG_INFO_PPI (1<<5)
#define MCFG_INFO_NICE (1<<6)
#define MCFG_INFO_ILM (1<<7)
#define MCFG_INFO_DLM (1<<8)
#define MCFG_INFO_ICACHE (1<<9)
#define MCFG_INFO_DCACHE (1<<10)
#define MICFG_IC_SET (0xF<<0)
#define MICFG_IC_WAY (0x7<<4)
#define MICFG_IC_LSIZE (0x7<<7)
#define MICFG_ILM_SIZE (0x1F<<16)
#define MICFG_ILM_XONLY (1<<21)
#define MDCFG_DC_SET (0xF<<0)
#define MDCFG_DC_WAY (0x7<<4)
#define MDCFG_DC_LSIZE (0x7<<7)
#define MDCFG_DLM_SIZE (0x1F<<16)
#define MPPICFG_INFO_PPI_SIZE (0x1F<<1)
#define MPPICFG_INFO_PPI_BPA (((1ULL<<((__riscv_xlen)-10))-1)<<10)
#define MFIOCFG_INFO_FIO_SIZE (0x1F<<1)
#define MFIOCFG_INFO_FIO_BPA (((1ULL<<((__riscv_xlen)-10))-1)<<10)
#define SIP_SSIP MIP_SSIP
#define SIP_STIP MIP_STIP
#define PRV_U 0
#define PRV_S 1
#define PRV_H 2
#define PRV_M 3
#define VM_MBARE 0
#define VM_MBB 1
#define VM_MBBID 2
#define VM_SV32 8
#define VM_SV39 9
#define VM_SV48 10
#define IRQ_S_SOFT 1
#define IRQ_H_SOFT 2
#define IRQ_M_SOFT 3
#define IRQ_S_TIMER 5
#define IRQ_H_TIMER 6
#define IRQ_M_TIMER 7
#define IRQ_S_EXT 9
#define IRQ_H_EXT 10
#define IRQ_M_EXT 11
#define IRQ_COP 12
#define IRQ_HOST 13
#define DEFAULT_RSTVEC 0x00001000
#define DEFAULT_NMIVEC 0x00001004
#define DEFAULT_MTVEC 0x00001010
#define CONFIG_STRING_ADDR 0x0000100C
#define EXT_IO_BASE 0x40000000
#define DRAM_BASE 0x80000000
/* === FPU FRM Rounding Mode === */
/** FPU Round to Nearest, ties to Even*/
#define FRM_RNDMODE_RNE 0x0
/** FPU Round Towards Zero */
#define FRM_RNDMODE_RTZ 0x1
/** FPU Round Down (towards -inf) */
#define FRM_RNDMODE_RDN 0x2
/** FPU Round Up (towards +inf) */
#define FRM_RNDMODE_RUP 0x3
/** FPU Round to nearest, ties to Max Magnitude */
#define FRM_RNDMODE_RMM 0x4
/**
* In instruction's rm, selects dynamic rounding mode.
* In Rounding Mode register, Invalid */
#define FRM_RNDMODE_DYN 0x7
/* === FPU FFLAGS Accrued Exceptions === */
/** FPU Inexact */
#define FFLAGS_AE_NX (1<<0)
/** FPU Underflow */
#define FFLAGS_AE_UF (1<<1)
/** FPU Overflow */
#define FFLAGS_AE_OF (1<<2)
/** FPU Divide by Zero */
#define FFLAGS_AE_DZ (1<<3)
/** FPU Invalid Operation */
#define FFLAGS_AE_NV (1<<4)
/** Floating Point Register f0-f31, eg. f0 -> FREG(0) */
#define FREG(idx) f##idx
/* === PMP CFG Bits === */
#define PMP_R 0x01
#define PMP_W 0x02
#define PMP_X 0x04
#define PMP_A 0x18
#define PMP_A_TOR 0x08
#define PMP_A_NA4 0x10
#define PMP_A_NAPOT 0x18
#define PMP_L 0x80
#define PMP_SHIFT 2
#define PMP_COUNT 16
// page table entry (PTE) fields
#define PTE_V 0x001 // Valid
#define PTE_R 0x002 // Read
#define PTE_W 0x004 // Write
#define PTE_X 0x008 // Execute
#define PTE_U 0x010 // User
#define PTE_G 0x020 // Global
#define PTE_A 0x040 // Accessed
#define PTE_D 0x080 // Dirty
#define PTE_SOFT 0x300 // Reserved for Software
#define PTE_PPN_SHIFT 10
#define PTE_TABLE(PTE) (((PTE) & (PTE_V | PTE_R | PTE_W | PTE_X)) == PTE_V)
#ifdef __riscv
#ifdef __riscv64
# define MSTATUS_SD MSTATUS64_SD
# define SSTATUS_SD SSTATUS64_SD
# define RISCV_PGLEVEL_BITS 9
#else
# define MSTATUS_SD MSTATUS32_SD
# define SSTATUS_SD SSTATUS32_SD
# define RISCV_PGLEVEL_BITS 10
#endif /* __riscv64 */
#define RISCV_PGSHIFT 12
#define RISCV_PGSIZE (1 << RISCV_PGSHIFT)
#endif /* __riscv */
#define DOWNLOAD_MODE_FLASHXIP 0
#define DOWNLOAD_MODE_FLASH 1
#define DOWNLOAD_MODE_ILM 2
#define DOWNLOAD_MODE_DDR 3
/**
* \defgroup NMSIS_Core_CSR_Registers Core CSR Registers
* \ingroup NMSIS_Core
* \brief NMSIS Core CSR Register Definitions
* \details
*
* The following macros are used for CSR Register Defintions.
* @{
*/
/* === Standard RISC-V CSR Registers === */
#define CSR_USTATUS 0x0
#define CSR_FFLAGS 0x1
#define CSR_FRM 0x2
#define CSR_FCSR 0x3
#define CSR_CYCLE 0xc00
#define CSR_TIME 0xc01
#define CSR_INSTRET 0xc02
#define CSR_HPMCOUNTER3 0xc03
#define CSR_HPMCOUNTER4 0xc04
#define CSR_HPMCOUNTER5 0xc05
#define CSR_HPMCOUNTER6 0xc06
#define CSR_HPMCOUNTER7 0xc07
#define CSR_HPMCOUNTER8 0xc08
#define CSR_HPMCOUNTER9 0xc09
#define CSR_HPMCOUNTER10 0xc0a
#define CSR_HPMCOUNTER11 0xc0b
#define CSR_HPMCOUNTER12 0xc0c
#define CSR_HPMCOUNTER13 0xc0d
#define CSR_HPMCOUNTER14 0xc0e
#define CSR_HPMCOUNTER15 0xc0f
#define CSR_HPMCOUNTER16 0xc10
#define CSR_HPMCOUNTER17 0xc11
#define CSR_HPMCOUNTER18 0xc12
#define CSR_HPMCOUNTER19 0xc13
#define CSR_HPMCOUNTER20 0xc14
#define CSR_HPMCOUNTER21 0xc15
#define CSR_HPMCOUNTER22 0xc16
#define CSR_HPMCOUNTER23 0xc17
#define CSR_HPMCOUNTER24 0xc18
#define CSR_HPMCOUNTER25 0xc19
#define CSR_HPMCOUNTER26 0xc1a
#define CSR_HPMCOUNTER27 0xc1b
#define CSR_HPMCOUNTER28 0xc1c
#define CSR_HPMCOUNTER29 0xc1d
#define CSR_HPMCOUNTER30 0xc1e
#define CSR_HPMCOUNTER31 0xc1f
#define CSR_SSTATUS 0x100
#define CSR_SIE 0x104
#define CSR_STVEC 0x105
#define CSR_SSCRATCH 0x140
#define CSR_SEPC 0x141
#define CSR_SCAUSE 0x142
#define CSR_SBADADDR 0x143
#define CSR_SIP 0x144
#define CSR_SPTBR 0x180
#define CSR_MSTATUS 0x300
#define CSR_MISA 0x301
#define CSR_MEDELEG 0x302
#define CSR_MIDELEG 0x303
#define CSR_MIE 0x304
#define CSR_MTVEC 0x305
#define CSR_MCOUNTEREN 0x306
#define CSR_MSCRATCH 0x340
#define CSR_MEPC 0x341
#define CSR_MCAUSE 0x342
#define CSR_MBADADDR 0x343
#define CSR_MTVAL 0x343
#define CSR_MIP 0x344
#define CSR_PMPCFG0 0x3a0
#define CSR_PMPCFG1 0x3a1
#define CSR_PMPCFG2 0x3a2
#define CSR_PMPCFG3 0x3a3
#define CSR_PMPADDR0 0x3b0
#define CSR_PMPADDR1 0x3b1
#define CSR_PMPADDR2 0x3b2
#define CSR_PMPADDR3 0x3b3
#define CSR_PMPADDR4 0x3b4
#define CSR_PMPADDR5 0x3b5
#define CSR_PMPADDR6 0x3b6
#define CSR_PMPADDR7 0x3b7
#define CSR_PMPADDR8 0x3b8
#define CSR_PMPADDR9 0x3b9
#define CSR_PMPADDR10 0x3ba
#define CSR_PMPADDR11 0x3bb
#define CSR_PMPADDR12 0x3bc
#define CSR_PMPADDR13 0x3bd
#define CSR_PMPADDR14 0x3be
#define CSR_PMPADDR15 0x3bf
#define CSR_TSELECT 0x7a0
#define CSR_TDATA1 0x7a1
#define CSR_TDATA2 0x7a2
#define CSR_TDATA3 0x7a3
#define CSR_DCSR 0x7b0
#define CSR_DPC 0x7b1
#define CSR_DSCRATCH 0x7b2
#define CSR_MCYCLE 0xb00
#define CSR_MINSTRET 0xb02
#define CSR_MHPMCOUNTER3 0xb03
#define CSR_MHPMCOUNTER4 0xb04
#define CSR_MHPMCOUNTER5 0xb05
#define CSR_MHPMCOUNTER6 0xb06
#define CSR_MHPMCOUNTER7 0xb07
#define CSR_MHPMCOUNTER8 0xb08
#define CSR_MHPMCOUNTER9 0xb09
#define CSR_MHPMCOUNTER10 0xb0a
#define CSR_MHPMCOUNTER11 0xb0b
#define CSR_MHPMCOUNTER12 0xb0c
#define CSR_MHPMCOUNTER13 0xb0d
#define CSR_MHPMCOUNTER14 0xb0e
#define CSR_MHPMCOUNTER15 0xb0f
#define CSR_MHPMCOUNTER16 0xb10
#define CSR_MHPMCOUNTER17 0xb11
#define CSR_MHPMCOUNTER18 0xb12
#define CSR_MHPMCOUNTER19 0xb13
#define CSR_MHPMCOUNTER20 0xb14
#define CSR_MHPMCOUNTER21 0xb15
#define CSR_MHPMCOUNTER22 0xb16
#define CSR_MHPMCOUNTER23 0xb17
#define CSR_MHPMCOUNTER24 0xb18
#define CSR_MHPMCOUNTER25 0xb19
#define CSR_MHPMCOUNTER26 0xb1a
#define CSR_MHPMCOUNTER27 0xb1b
#define CSR_MHPMCOUNTER28 0xb1c
#define CSR_MHPMCOUNTER29 0xb1d
#define CSR_MHPMCOUNTER30 0xb1e
#define CSR_MHPMCOUNTER31 0xb1f
#define CSR_MUCOUNTEREN 0x320
#define CSR_MSCOUNTEREN 0x321
#define CSR_MHPMEVENT3 0x323
#define CSR_MHPMEVENT4 0x324
#define CSR_MHPMEVENT5 0x325
#define CSR_MHPMEVENT6 0x326
#define CSR_MHPMEVENT7 0x327
#define CSR_MHPMEVENT8 0x328
#define CSR_MHPMEVENT9 0x329
#define CSR_MHPMEVENT10 0x32a
#define CSR_MHPMEVENT11 0x32b
#define CSR_MHPMEVENT12 0x32c
#define CSR_MHPMEVENT13 0x32d
#define CSR_MHPMEVENT14 0x32e
#define CSR_MHPMEVENT15 0x32f
#define CSR_MHPMEVENT16 0x330
#define CSR_MHPMEVENT17 0x331
#define CSR_MHPMEVENT18 0x332
#define CSR_MHPMEVENT19 0x333
#define CSR_MHPMEVENT20 0x334
#define CSR_MHPMEVENT21 0x335
#define CSR_MHPMEVENT22 0x336
#define CSR_MHPMEVENT23 0x337
#define CSR_MHPMEVENT24 0x338
#define CSR_MHPMEVENT25 0x339
#define CSR_MHPMEVENT26 0x33a
#define CSR_MHPMEVENT27 0x33b
#define CSR_MHPMEVENT28 0x33c
#define CSR_MHPMEVENT29 0x33d
#define CSR_MHPMEVENT30 0x33e
#define CSR_MHPMEVENT31 0x33f
#define CSR_MVENDORID 0xf11
#define CSR_MARCHID 0xf12
#define CSR_MIMPID 0xf13
#define CSR_MHARTID 0xf14
#define CSR_CYCLEH 0xc80
#define CSR_TIMEH 0xc81
#define CSR_INSTRETH 0xc82
#define CSR_HPMCOUNTER3H 0xc83
#define CSR_HPMCOUNTER4H 0xc84
#define CSR_HPMCOUNTER5H 0xc85
#define CSR_HPMCOUNTER6H 0xc86
#define CSR_HPMCOUNTER7H 0xc87
#define CSR_HPMCOUNTER8H 0xc88
#define CSR_HPMCOUNTER9H 0xc89
#define CSR_HPMCOUNTER10H 0xc8a
#define CSR_HPMCOUNTER11H 0xc8b
#define CSR_HPMCOUNTER12H 0xc8c
#define CSR_HPMCOUNTER13H 0xc8d
#define CSR_HPMCOUNTER14H 0xc8e
#define CSR_HPMCOUNTER15H 0xc8f
#define CSR_HPMCOUNTER16H 0xc90
#define CSR_HPMCOUNTER17H 0xc91
#define CSR_HPMCOUNTER18H 0xc92
#define CSR_HPMCOUNTER19H 0xc93
#define CSR_HPMCOUNTER20H 0xc94
#define CSR_HPMCOUNTER21H 0xc95
#define CSR_HPMCOUNTER22H 0xc96
#define CSR_HPMCOUNTER23H 0xc97
#define CSR_HPMCOUNTER24H 0xc98
#define CSR_HPMCOUNTER25H 0xc99
#define CSR_HPMCOUNTER26H 0xc9a
#define CSR_HPMCOUNTER27H 0xc9b
#define CSR_HPMCOUNTER28H 0xc9c
#define CSR_HPMCOUNTER29H 0xc9d
#define CSR_HPMCOUNTER30H 0xc9e
#define CSR_HPMCOUNTER31H 0xc9f
#define CSR_MCYCLEH 0xb80
#define CSR_MINSTRETH 0xb82
#define CSR_MHPMCOUNTER3H 0xb83
#define CSR_MHPMCOUNTER4H 0xb84
#define CSR_MHPMCOUNTER5H 0xb85
#define CSR_MHPMCOUNTER6H 0xb86
#define CSR_MHPMCOUNTER7H 0xb87
#define CSR_MHPMCOUNTER8H 0xb88
#define CSR_MHPMCOUNTER9H 0xb89
#define CSR_MHPMCOUNTER10H 0xb8a
#define CSR_MHPMCOUNTER11H 0xb8b
#define CSR_MHPMCOUNTER12H 0xb8c
#define CSR_MHPMCOUNTER13H 0xb8d
#define CSR_MHPMCOUNTER14H 0xb8e
#define CSR_MHPMCOUNTER15H 0xb8f
#define CSR_MHPMCOUNTER16H 0xb90
#define CSR_MHPMCOUNTER17H 0xb91
#define CSR_MHPMCOUNTER18H 0xb92
#define CSR_MHPMCOUNTER19H 0xb93
#define CSR_MHPMCOUNTER20H 0xb94
#define CSR_MHPMCOUNTER21H 0xb95
#define CSR_MHPMCOUNTER22H 0xb96
#define CSR_MHPMCOUNTER23H 0xb97
#define CSR_MHPMCOUNTER24H 0xb98
#define CSR_MHPMCOUNTER25H 0xb99
#define CSR_MHPMCOUNTER26H 0xb9a
#define CSR_MHPMCOUNTER27H 0xb9b
#define CSR_MHPMCOUNTER28H 0xb9c
#define CSR_MHPMCOUNTER29H 0xb9d
#define CSR_MHPMCOUNTER30H 0xb9e
#define CSR_MHPMCOUNTER31H 0xb9f
/* === CLIC CSR Registers === */
#define CSR_MTVT 0x307
#define CSR_MNXTI 0x345
#define CSR_MINTSTATUS 0x346
#define CSR_MSCRATCHCSW 0x348
#define CSR_MSCRATCHCSWL 0x349
#define CSR_MCLICBASE 0x350
/* === Nuclei custom CSR Registers === */
#define CSR_MCOUNTINHIBIT 0x320
#define CSR_MILM_CTL 0x7C0
#define CSR_MDLM_CTL 0x7C1
#define CSR_MNVEC 0x7C3
#define CSR_MSUBM 0x7C4
#define CSR_MDCAUSE 0x7C9
#define CSR_MCACHE_CTL 0x7CA
#define CSR_MMISC_CTL 0x7D0
#define CSR_MSAVESTATUS 0x7D6
#define CSR_MSAVEEPC1 0x7D7
#define CSR_MSAVECAUSE1 0x7D8
#define CSR_MSAVEEPC2 0x7D9
#define CSR_MSAVECAUSE2 0x7DA
#define CSR_MSAVEDCAUSE1 0x7DB
#define CSR_MSAVEDCAUSE2 0x7DC
#define CSR_PUSHMSUBM 0x7EB
#define CSR_MTVT2 0x7EC
#define CSR_JALMNXTI 0x7ED
#define CSR_PUSHMCAUSE 0x7EE
#define CSR_PUSHMEPC 0x7EF
#define CSR_MPPICFG_INFO 0x7F0
#define CSR_MFIOCFG_INFO 0x7F1
#define CSR_SLEEPVALUE 0x811
#define CSR_TXEVT 0x812
#define CSR_WFE 0x810
#define CSR_MICFG_INFO 0xFC0
#define CSR_MDCFG_INFO 0xFC1
#define CSR_MCFG_INFO 0xFC2
/** @} */ /** End of Doxygen Group NMSIS_Core_CSR_Registers **/
/* Exception Code in MCAUSE CSR */
#define CAUSE_MISALIGNED_FETCH 0x0
#define CAUSE_FAULT_FETCH 0x1
#define CAUSE_ILLEGAL_INSTRUCTION 0x2
#define CAUSE_BREAKPOINT 0x3
#define CAUSE_MISALIGNED_LOAD 0x4
#define CAUSE_FAULT_LOAD 0x5
#define CAUSE_MISALIGNED_STORE 0x6
#define CAUSE_FAULT_STORE 0x7
#define CAUSE_USER_ECALL 0x8
#define CAUSE_SUPERVISOR_ECALL 0x9
#define CAUSE_HYPERVISOR_ECALL 0xa
#define CAUSE_MACHINE_ECALL 0xb
/* Exception Subcode in MDCAUSE CSR */
#define DCAUSE_FAULT_FETCH_PMP 0x1
#define DCAUSE_FAULT_FETCH_INST 0x2
#define DCAUSE_FAULT_LOAD_PMP 0x1
#define DCAUSE_FAULT_LOAD_INST 0x2
#define DCAUSE_FAULT_LOAD_NICE 0x3
#define DCAUSE_FAULT_STORE_PMP 0x1
#define DCAUSE_FAULT_STORE_INST 0x2
/** @} */ /** End of Doxygen Group NMSIS_Core_CSR_Encoding **/
#ifdef __cplusplus
}
#endif
#endif /* __RISCV_ENCODING_H__ */
@@ -0,0 +1,698 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup los_config System configuration items
* @ingroup kernel
*/
#ifndef _LOS_CONFIG_H
#define _LOS_CONFIG_H
#include "target_config.h"
#include "los_compiler.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
/* =============================================================================
System clock module configuration
============================================================================= */
/**
* @ingroup los_config
* System clock (unit: HZ)
*/
#ifndef OS_SYS_CLOCK
#error "OS_SYS_CLOCK is system clock rate which should be defined in target_config.h"
#endif
/**
* @ingroup los_config
* Number of Ticks in one second
*/
#ifndef LOSCFG_BASE_CORE_TICK_PER_SECOND
#define LOSCFG_BASE_CORE_TICK_PER_SECOND (100UL)
#endif
#if defined(LOSCFG_BASE_CORE_TICK_PER_SECOND) && \
((LOSCFG_BASE_CORE_TICK_PER_SECOND < 1UL) || (LOSCFG_BASE_CORE_TICK_PER_SECOND > 1000000000UL))
#error "LOSCFG_BASE_CORE_TICK_PER_SECOND SHOULD big than 0, and less than 1000000000UL"
#endif
#if (LOSCFG_BASE_CORE_TICK_PER_SECOND <= 1000UL)
/**
* @ingroup los_config
* How much time one tick spent (unit:ms)
*/
#ifndef LOSCFG_BASE_CORE_TICK_PERIOD_MS
#define LOSCFG_BASE_CORE_TICK_PERIOD_MS (1000UL / LOSCFG_BASE_CORE_TICK_PER_SECOND)
#endif
#elif (LOSCFG_BASE_CORE_TICK_PER_SECOND <= 1000000UL)
/**
* @ingroup los_config
* How much time one tick spent (unit:us)
*/
#ifndef LOSCFG_BASE_CORE_TICK_PERIOD_US
#define LOSCFG_BASE_CORE_TICK_PERIOD_US (1000000UL / LOSCFG_BASE_CORE_TICK_PER_SECOND)
#endif
#else
/**
* @ingroup los_config
* How much time one tick spent (unit:ns)
*/
#ifndef LOSCFG_BASE_CORE_TICK_PERIOD_NS
#define LOSCFG_BASE_CORE_TICK_PERIOD_NS (1000000000UL / LOSCFG_BASE_CORE_TICK_PER_SECOND)
#endif
#endif
#ifndef LOSCFG_BASE_CORE_TICK_HW_TIME
#define LOSCFG_BASE_CORE_TICK_HW_TIME 0
#endif
/**
* @ingroup los_config
* System timer is a 64/128 bit timer
*/
#ifndef LOSCFG_BASE_CORE_TICK_WTIMER
#define LOSCFG_BASE_CORE_TICK_WTIMER 0
#endif
/**
* @ingroup los_config
* System timer count maximum
*/
#ifndef LOSCFG_BASE_CORE_TICK_RESPONSE_MAX
#define LOSCFG_BASE_CORE_TICK_RESPONSE_MAX 0
#endif
/* =============================================================================
Hardware interrupt module configuration
============================================================================= */
/**
* @ingroup los_config
* Configuration item for hardware interrupt tailoring
*/
#ifndef LOSCFG_PLATFORM_HWI
#define LOSCFG_PLATFORM_HWI 1
#endif
/**
* @ingroup los_config
* Configuration item for using system defined vector base address and interrupt handlers.
* If LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT is set to 0, vector base address will not be
* modified by system. In arm, it should be noted that PendSV_Handler and SysTick_Handler should
* be redefined to HalPendSV and OsTickHandler respectly in this case, because system depends on
* these interrupt handlers to run normally. What's more, LOS_HwiCreate will not register handler.
*/
#ifndef LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT
#define LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT 0
#endif
#if (LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT == 1)
#if (LOSCFG_PLATFORM_HWI == 0)
#error "if LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT is set to 1, then LOSCFG_PLATFORM_HWI must also be set to 1"
#endif
#endif
/**
* @ingroup los_config
* Maximum number of used hardware interrupts, including Tick timer interrupts.
*/
#ifndef LOSCFG_PLATFORM_HWI_LIMIT
#define LOSCFG_PLATFORM_HWI_LIMIT 32
#endif
/* =============================================================================
Task module configuration
============================================================================= */
/**
* @ingroup los_config
* Minimum stack size.
*
* 0x80 bytes, aligned on a boundary of 8.
*/
#ifndef LOSCFG_BASE_CORE_TSK_MIN_STACK_SIZE
#define LOSCFG_BASE_CORE_TSK_MIN_STACK_SIZE (ALIGN(0x80, 4))
#endif
/**
* @ingroup los_config
* Default task priority
*/
#ifndef LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO
#define LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO 10
#endif
/**
* @ingroup los_config
* Maximum supported number of tasks except the idle task rather than the number of usable tasks
*/
#ifndef LOSCFG_BASE_CORE_TSK_LIMIT
#define LOSCFG_BASE_CORE_TSK_LIMIT 5
#endif
/**
* @ingroup los_config
* Size of the idle task stack
*/
#ifndef LOSCFG_BASE_CORE_TSK_IDLE_STACK_SIZE
#define LOSCFG_BASE_CORE_TSK_IDLE_STACK_SIZE 0x180UL
#endif
/**
* @ingroup los_config
* Default task stack size
*/
#ifndef LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE
#define LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE 0x400UL
#endif
/**
* @ingroup los_config
* Configuration item for task Robin tailoring
*/
#ifndef LOSCFG_BASE_CORE_TIMESLICE
#define LOSCFG_BASE_CORE_TIMESLICE 1
#endif
/**
* @ingroup los_config
* Longest execution time of tasks with the same priorities
*/
#ifndef LOSCFG_BASE_CORE_TIMESLICE_TIMEOUT
#define LOSCFG_BASE_CORE_TIMESLICE_TIMEOUT 20000 /* 20ms */
#endif
/**
* @ingroup los_config
* Configuration item for task (stack) monitoring module tailoring
*/
#ifndef LOSCFG_BASE_CORE_TSK_MONITOR
#define LOSCFG_BASE_CORE_TSK_MONITOR 0
#endif
/**
* @ingroup los_config
* Configuration item for task perf task filter hook
*/
#ifndef LOSCFG_BASE_CORE_EXC_TSK_SWITCH
#define LOSCFG_BASE_CORE_EXC_TSK_SWITCH 0
#endif
/**
* @ingroup los_config
* Define a usable task priority.Highest task priority.
*/
#ifndef LOS_TASK_PRIORITY_HIGHEST
#define LOS_TASK_PRIORITY_HIGHEST 0
#endif
/**
* @ingroup los_config
* Define a usable task priority.Lowest task priority.
*/
#ifndef LOS_TASK_PRIORITY_LOWEST
#define LOS_TASK_PRIORITY_LOWEST 31
#endif
/**
* @ingroup los_config
* Configuration item for task stack independent
*/
#ifndef LOSCFG_BASE_CORE_TASKSTACK_INDEPENDENT
#define LOSCFG_BASE_CORE_TASKSTACK_INDEPENDENT 0
#endif
/**
* @ingroup los_config
* SP align size
*/
#ifndef LOSCFG_STACK_POINT_ALIGN_SIZE
#define LOSCFG_STACK_POINT_ALIGN_SIZE 8
#endif
/* =============================================================================
Semaphore module configuration
============================================================================= */
/**
* @ingroup los_config
* Configuration item for semaphore module tailoring
*/
#ifndef LOSCFG_BASE_IPC_SEM
#define LOSCFG_BASE_IPC_SEM 1
#endif
/**
* @ingroup los_config
* Maximum supported number of semaphores
*/
#ifndef LOSCFG_BASE_IPC_SEM_LIMIT
#define LOSCFG_BASE_IPC_SEM_LIMIT 6
#endif
/**
* @ingroup los_config
* Maximum number of semaphores.
*/
#ifndef OS_SEM_COUNTING_MAX_COUNT
#define OS_SEM_COUNTING_MAX_COUNT 0xFFFF
#endif
/* =============================================================================
Mutex module configuration
============================================================================= */
/**
* @ingroup los_config
* Configuration item for mutex module tailoring
*/
#ifndef LOSCFG_BASE_IPC_MUX
#define LOSCFG_BASE_IPC_MUX 1
#endif
/**
* @ingroup los_config
* Maximum supported number of mutexes
*/
#ifndef LOSCFG_BASE_IPC_MUX_LIMIT
#define LOSCFG_BASE_IPC_MUX_LIMIT 6
#endif
/* =============================================================================
Queue module configuration
============================================================================= */
/**
* @ingroup los_config
* Configuration item for queue module tailoring
*/
#ifndef LOSCFG_BASE_IPC_QUEUE
#define LOSCFG_BASE_IPC_QUEUE 1
#endif
/**
* @ingroup los_config
* Maximum supported number of queues rather than the number of usable queues
*/
#ifndef LOSCFG_BASE_IPC_QUEUE_LIMIT
#define LOSCFG_BASE_IPC_QUEUE_LIMIT 6
#endif
/* =============================================================================
Software timer module configuration
============================================================================= */
/**
* @ingroup los_config
* Configuration item for software timer module tailoring
*/
#ifndef LOSCFG_BASE_CORE_SWTMR
#define LOSCFG_BASE_CORE_SWTMR 1
#endif
/**
* @ingroup los_config
* Maximum supported number of software timers rather than the number of usable software timers
*/
#ifndef LOSCFG_BASE_CORE_SWTMR_LIMIT
#define LOSCFG_BASE_CORE_SWTMR_LIMIT 5
#endif
/**
* @ingroup los_config
* Software timer task stack size
*/
#ifndef LOSCFG_BASE_CORE_TSK_SWTMR_STACK_SIZE
#define LOSCFG_BASE_CORE_TSK_SWTMR_STACK_SIZE LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE
#endif
/**
* @ingroup los_config
* Configurate item for software timer align tailoring
*/
#ifndef LOSCFG_BASE_CORE_SWTMR_ALIGN
#define LOSCFG_BASE_CORE_SWTMR_ALIGN 0
#endif
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
#if (LOSCFG_BASE_CORE_SWTMR == 0)
#error "if LOSCFG_BASE_CORE_SWTMR_ALIGN is set to 1, then LOSCFG_BASE_CORE_SWTMR must also be set to 1"
#endif
#endif
/**
* @ingroup los_config
* Maximum size of a software timer queue
*/
#ifndef OS_SWTMR_HANDLE_QUEUE_SIZE
#define OS_SWTMR_HANDLE_QUEUE_SIZE (LOSCFG_BASE_CORE_SWTMR_LIMIT + 0)
#endif
/**
* @ingroup los_config
* Minimum divisor of software timer multiple alignment
*/
#ifndef LOS_COMMON_DIVISOR
#define LOS_COMMON_DIVISOR 10
#endif
#if (LOSCFG_BASE_CORE_SWTMR == 1)
#if (LOSCFG_BASE_IPC_QUEUE == 0)
#error "if LOSCFG_BASE_CORE_SWTMR is set to 1, then LOSCFG_BASE_IPC_QUEUE must also be set to 1"
#endif
#endif
/* =============================================================================
Memory module configuration ---- to be refactored
============================================================================= */
extern UINT8 *m_aucSysMem0;
/**
* @ingroup los_config
* Configure whether the kernel uses external heap memory
*/
#ifndef LOSCFG_SYS_EXTERNAL_HEAP
#define LOSCFG_SYS_EXTERNAL_HEAP 0
#endif
/**
* @ingroup los_config
* Starting address of the memory
*/
#ifndef LOSCFG_SYS_HEAP_ADDR
#define LOSCFG_SYS_HEAP_ADDR (&m_aucSysMem0[0])
#endif
/**
* @ingroup los_config
* Starting address of the task stack
*/
#ifndef OS_TASK_STACK_ADDR
#define OS_TASK_STACK_ADDR LOSCFG_SYS_HEAP_ADDR
#endif
/**
* @ingroup los_config
* Memory size
*/
#ifndef LOSCFG_SYS_HEAP_SIZE
#define LOSCFG_SYS_HEAP_SIZE 0x00013000
#endif
/**
* @ingroup los_config
* Configuration module tailoring of more mempry pool checking
*/
#ifndef LOSCFG_MEM_MUL_POOL
#define LOSCFG_MEM_MUL_POOL 1
#endif
/**
* @ingroup los_config
* Configuration module tailoring of memory released by task id
*/
#ifndef LOSCFG_MEM_FREE_BY_TASKID
#define LOSCFG_MEM_FREE_BY_TASKID 0
#endif
/**
* @ingroup los_config
* Configuration module tailoring of mem node integrity checking
*/
#ifndef LOSCFG_BASE_MEM_NODE_INTEGRITY_CHECK
#define LOSCFG_BASE_MEM_NODE_INTEGRITY_CHECK 0
#endif
/**
* @ingroup los_config
* Configuration memory leak detection
* @attention
* Need to enable backtrace module synchronously by configuration LOSCFG_BACKTRACE_TYPE,
* and call OSBackTraceInit to complete initialization before the memory pool is initialized.
*/
#ifndef LOSCFG_MEM_LEAKCHECK
#define LOSCFG_MEM_LEAKCHECK 0
#endif
#if (LOSCFG_MEM_LEAKCHECK == 1) && (LOSCFG_BACKTRACE_TYPE == 0)
#error "if LOSCFG_MEM_LEAKCHECK is set to 1, then LOSCFG_BACKTRACE_TYPE must be set to 1, 2 or 3."
#endif
/**
* @ingroup los_config
* The default is 4, which means that the function call stack is recorded from the kernel interface,
* such as LOS_MemAlloc/LOS_MemAllocAlign/LOS_MemRealloc/LOS_MemFree. If you want to further ignore
* the number of function call layers, you can increase this value appropriately.
* @attention
* The default is in the IAR tool. Under different compilation environments, this value needs to be adjusted.
*/
#ifndef LOSCFG_MEM_OMIT_LR_CNT
#define LOSCFG_MEM_OMIT_LR_CNT 4
#endif
/**
* @ingroup los_config
* The record number of layers of the function call relationship starting from the number of
* ignored layers(LOSCFG_MEM_OMIT_LR_CNT).
*/
#ifndef LOSCFG_MEM_RECORD_LR_CNT
#define LOSCFG_MEM_RECORD_LR_CNT 3
#endif
/**
* @ingroup los_config
* Configuration memory leak recorded num
*/
#ifndef LOSCFG_MEM_LEAKCHECK_RECORD_MAX_NUM
#define LOSCFG_MEM_LEAKCHECK_RECORD_MAX_NUM 1024
#endif
/**
* @ingroup los_config
* Configuration of memory pool record memory consumption waterline
*/
#ifndef LOSCFG_MEM_WATERLINE
#define LOSCFG_MEM_WATERLINE 1
#endif
/**
* @ingroup los_config
* Number of memory checking blocks
*/
#ifndef OS_SYS_MEM_NUM
#define OS_SYS_MEM_NUM 20
#endif
/**
* @ingroup los_config
* Size of unaligned memory
*/
#ifndef OS_SYS_NOCACHEMEM_SIZE
#define OS_SYS_NOCACHEMEM_SIZE 0x0UL
#endif
/**
* @ingroup los_config
* Starting address of the unaligned memory
*/
#if (OS_SYS_NOCACHEMEM_SIZE > 0)
#define OS_SYS_NOCACHEMEM_ADDR (&g_sysNoCacheMem0[0])
#endif
/* =============================================================================
Exception module configuration
============================================================================= */
/**
* @ingroup los_config
* Configuration item for exception tailoring
*/
#ifndef LOSCFG_PLATFORM_EXC
#define LOSCFG_PLATFORM_EXC 0
#endif
/**
* @ingroup los_config
* Configuration of hardware stack protection
*/
#ifndef LOSCFG_EXC_HARDWARE_STACK_PROTECTION
#define LOSCFG_EXC_HARDWARE_STACK_PROTECTION 0
#endif
/* =============================================================================
CPUP configuration
============================================================================= */
/**
* @ingroup los_config
* Configuration item for CPU usage tailoring
*/
#ifndef LOSCFG_BASE_CORE_CPUP
#define LOSCFG_BASE_CORE_CPUP 0
#endif
/* =============================================================================
Test module configuration
============================================================================= */
/**
* @ingroup los_config
* Configuration test case to open
*/
/**
* @ingroup los_config
* Configuration CMSIS_OS_VER
*/
#ifndef CMSIS_OS_VER
#define CMSIS_OS_VER 2
#endif
/* =============================================================================
trace configuration
============================================================================= */
/**
* @ingroup los_config
* Configuration liteos trace
*/
#ifndef LOSCFG_KERNEL_TRACE
#define LOSCFG_KERNEL_TRACE 0
#endif
#ifndef LOSCFG_SUPPORT_LITTLEFS
#define LOSCFG_SUPPORT_LITTLEFS 1
#endif
#ifndef LOSCFG_LFS_MAX_MOUNT_SIZE
#define LOSCFG_LFS_MAX_MOUNT_SIZE 3
#endif
/**
* @ingroup los_config
* Configuration trace tool
*/
#ifndef LOSCFG_DEBUG_HOOK
#define LOSCFG_DEBUG_HOOK 0
#endif
/* =============================================================================
printf configuration
============================================================================= */
/**
* @ingroup los_config
* Configuration liteos printf
*/
#ifndef LOSCFG_KERNEL_PRINTF
#define LOSCFG_KERNEL_PRINTF 1
#endif
/* =============================================================================
backtrace configuration
============================================================================= */
/**
* @ingroup los_config
* Configuration backtrace type
* 0: Close stack analysis module.
* 1: Call stack analysis for cortex-m series by scanning the stack.
* 2: Call stack analysis for risc-v by using frame pointer.
* 3: Call stack analysis for risc-v by scanning the stack.
* others: Not currently supported.
*/
#ifndef LOSCFG_BACKTRACE_TYPE
#define LOSCFG_BACKTRACE_TYPE 0
#endif
/**
* @ingroup los_config
* Configuration backtrace depth.
*/
#ifndef LOSCFG_BACKTRACE_DEPTH
#define LOSCFG_BACKTRACE_DEPTH 15
#endif
/**
* @ingroup los_config
* Configuration item for low power frame tailoring
*/
#ifndef LOSCFG_KERNEL_PM
#define LOSCFG_KERNEL_PM 1
#endif
/**
* @ingroup los_config
* Configuration item for priority of low-power task.
*/
#ifndef LOSCFG_KERNEL_PM_TASK_PTIORITY
#define LOSCFG_KERNEL_PM_TASK_PTIORITY 1
#endif
/**
* @ingroup los_config
* Configuration item for stack size of low-power task.
*/
#ifndef LOSCFG_KERNEL_PM_TASK_STACKSIZE
#define LOSCFG_KERNEL_PM_TASK_STACKSIZE 0x800
#endif
/**
* @ingroup los_config
* Configuration item for low power frame debug tailoring
*/
#ifndef LOSCFG_KERNEL_PM_DEBUG
#define LOSCFG_KERNEL_PM_DEBUG 0
#endif
/**
* @ingroup los_config
* Configuration trustzone secure heap size.
*/
#ifndef LOSCFG_SECURE_HEAP_SIZE
#define LOSCFG_SECURE_HEAP_SIZE 2048
#endif
/**
* @ingroup los_config
* Configuration trustzone secure stack default size.
* The secure stack must be allocated before the task calls non-secure callble functions.
*/
#ifndef LOSCFG_SECURE_STACK_DEFAULT_SIZE
#define LOSCFG_SECURE_STACK_DEFAULT_SIZE 512
#endif
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_CONFIG_H */
@@ -0,0 +1,332 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup los_event Event
* @ingroup kernel
*/
#ifndef _LOS_EVENT_H
#define _LOS_EVENT_H
#include "los_list.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
/**
* @ingroup los_event
* Event reading mode: The task waits for all its expected events to occur.
*/
#define LOS_WAITMODE_AND (4) /* all bits must be set */
/**
* @ingroup los_event
* Event reading mode: The task waits for any of its expected events to occur.
*/
#define LOS_WAITMODE_OR (2) /* any bit must be set */
/**
* @ingroup los_event
* Event reading mode: The event flag is immediately cleared after the event is read.
*/
#define LOS_WAITMODE_CLR (1) /* clear when satisfied */
/**
* @ingroup los_event
* Bit 25 of the event mask cannot be set to an event because it is set to an error code.
*
* Value: 0x02001c00
*
* Solution: Set bits excluding bit 25 of the event mask to events.
*/
#define LOS_ERRNO_EVENT_SETBIT_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_EVENT, 0x00)
/**
* @ingroup los_event
* Event reading error code: Event reading times out.
*
* Value: 0x02001c01
*
* Solution: Increase the waiting time for event reading, or make another task write a mask for the event.
*/
#define LOS_ERRNO_EVENT_READ_TIMEOUT LOS_ERRNO_OS_ERROR(LOS_MOD_EVENT, 0x01)
/**
* @ingroup los_event
* Event reading error code: The EVENTMASK input parameter value is valid. The input parameter value must not be 0.
*
* Value: 0x02001c02
*
* Solution: Pass in a valid EVENTMASK value.
*/
#define LOS_ERRNO_EVENT_EVENTMASK_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_EVENT, 0x02)
/**
* @ingroup los_event
* Event reading error code: The event is being read during an interrupt.
*
* Value: 0x02001c03
*
* Solution: Read the event in a task.
*/
#define LOS_ERRNO_EVENT_READ_IN_INTERRUPT LOS_ERRNO_OS_ERROR(LOS_MOD_EVENT, 0x03)
/**
* @ingroup los_event
* Event reading error code: The uwFlags input parameter value used in the event reading API is invalid.
* This input parameter value is obtained by performing an OR operation on corresponding bits of either OS_EVENT_ANY or
* OS_EVENT_ANY and corresponding bits of either OS_EVENT_WAIT or OS_EVENT_NOWAIT. The waiting time must be set to
* a nonzero value when an event is read in the mode of OS_EVENT_WAIT.
*
* Value: 0x02001c04
*
* Solution: Pass in a valid uwFlags value.
*/
#define LOS_ERRNO_EVENT_FLAGS_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_EVENT, 0x04)
/**
* @ingroup los_event
* Event reading error code: The task is locked and is unable to read the event.
*
* Value: 0x02001c05
*
* Solution: Unlock the task and read the event.
*/
#define LOS_ERRNO_EVENT_READ_IN_LOCK LOS_ERRNO_OS_ERROR(LOS_MOD_EVENT, 0x05)
/**
* @ingroup los_event
* Event reading error code: Null pointer.
*
* Value: 0x02001c06
*
* Solution: Check whether the input parameter is null.
*/
#define LOS_ERRNO_EVENT_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_EVENT, 0x06)
/**
* @ingroup los_event
* Event reading error code: no initialized.
*
* Value: 0x02001c07
*
* Solution: Check whether the event is initialized.
*/
#define LOS_ERRNO_EVENT_NOT_INITIALIZED LOS_ERRNO_OS_ERROR(LOS_MOD_EVENT, 0x07)
/**
* @ingroup los_event
* Event reading error code: should not be destroyed.
*
* Value: 0x02001c08
*
* Solution: Check whether the event list is not empty.
*/
#define LOS_ERRNO_EVENT_SHOULD_NOT_DESTROYED LOS_ERRNO_OS_ERROR(LOS_MOD_EVENT, 0x08)
/**
* @ingroup los_event
* Event control structure
*/
typedef struct tagEvent {
UINT32 uwEventID; /**< Event mask in the event control block,
indicating the event that has been logically processed. */
LOS_DL_LIST stEventList; /**< Event control block linked list */
} EVENT_CB_S, *PEVENT_CB_S;
/**
* @ingroup los_event
* @brief Initialize an event control block.
*
* @par Description:
* This API is used to initialize the event control block pointed to by eventCB.
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param eventCB [IN/OUT] Pointer to the event control block to be initialized.
*
* @retval #LOS_ERRNO_EVENT_PTR_NULL Null pointer.
* @retval #LOS_OK The event control block is successfully initialized.
* @par Dependency:
* <ul><li>los_event.h: the header file that contains the API declaration.</li></ul>
* @see LOS_EventClear
*/
extern UINT32 LOS_EventInit(PEVENT_CB_S eventCB);
/**
* @ingroup los_event
* @brief Obtain an event specified by the event ID.
*
* @par Description:
* This API is used to check whether an event expected by the user occurs according to the event ID, event mask,
* and event reading mode, and process the event based on the event reading mode. The event ID must point to valid memory.
* @attention
* <ul>
* <li>When the mode is LOS_WAITMODE_CLR, the eventID is passed-out.</li>
* <li>Otherwise the eventID is passed-in.</li>
* </ul>
*
* @param eventID [IN/OUT] Pointer to the ID of the event to be checked.
* @param eventMask [IN] Mask of the event expected to occur by the user, indicating the event obtained after
* it is logically processed that matches the ID pointed to by mode.
* @param mode [IN] Event reading mode. The modes include LOS_WAITMODE_AND, LOS_WAITMODE_OR, LOS_WAITMODE_CLR.
*
* @retval 0 The event expected by the user does not occur.
* @retval #UINT32 The event expected by the user occurs.
* @par Dependency:
* <ul><li>los_event.h: the header file that contains the API declaration.</li></ul>
* @see LOS_EventRead | LOS_EventWrite
*/
extern UINT32 LOS_EventPoll(UINT32 *eventID, UINT32 eventMask, UINT32 mode);
/**
* @ingroup los_event
* @brief Read an event.
*
* @par Description:
* This API is used to block or schedule a task that reads an event of which the event control block, event mask, reading mode,
* and timeout information are specified.
* </ul>
* @attention
* <ul>
* <li>An error code and an event return value can be same. To differentiate the error code and return value, bit 25 of
* the event mask is forbidden to be used.</li>
* </ul>
*
* @param eventCB [IN/OUT] Pointer to the event control block to be checked. This parameter must point to valid memory.
* @param eventMask [IN] Mask of the event expected to occur by the user, indicating the event obtained after
* it is logically processed that matches the ID pointed to by eventID.
* @param mode [IN] Event reading mode.
* @param timeOut [IN] Timeout interval of event reading (unit: Tick).
*
* @retval #LOS_ERRNO_EVENT_SETBIT_INVALID Bit 25 of the event mask cannot be set because it is set to an error number.
* @retval #LOS_ERRNO_EVENT_EVENTMASK_INVALID The passed-in event reading mode is incorrect.
* @retval #LOS_ERRNO_EVENT_READ_IN_INTERRUPT The event is being read during an interrupt.
* @retval #LOS_ERRNO_EVENT_FLAGS_INVALID The event mode is invalid.
* @retval #LOS_ERRNO_EVENT_READ_IN_LOCK The event reading task is locked.
* @retval #LOS_ERRNO_EVENT_PTR_NULL The passed-in pointer is null.
* @retval 0 The event expected by the user does not occur.
* @retval #UINT32 The event expected by the user occurs.
* @par Dependency:
* <ul><li>los_event.h: the header file that contains the API declaration.</li></ul>
* @see LOS_EventPoll | LOS_EventWrite
*/
extern UINT32 LOS_EventRead(PEVENT_CB_S eventCB, UINT32 eventMask, UINT32 mode, UINT32 timeOut);
/**
* @ingroup los_event
* @brief Write an event.
*
* @par Description:
* This API is used to write an event specified by the passed-in event mask into an event control block
* pointed to by eventCB.
* @attention
* <ul>
* <li>To determine whether the LOS_EventRead API returns an event or an error code, bit 25 of the event mask
* is forbidden to be used.</li>
* </ul>
*
* @param eventCB [IN/OUT] Pointer to the event control block into which an event is to be written.
* This parameter must point to valid memory.
* @param events [IN] Event mask to be written.
*
* @retval #LOS_ERRNO_EVENT_SETBIT_INVALID Bit 25 of the event mask cannot be set to an event
* because it is set to an error code.
* @retval #LOS_ERRNO_EVENT_PTR_NULL Null pointer.
* @retval #LOS_OK The event is successfully written.
* @par Dependency:
* <ul><li>los_event.h: the header file that contains the API declaration.</li></ul>
* @see LOS_EventPoll | LOS_EventRead
*/
extern UINT32 LOS_EventWrite(PEVENT_CB_S eventCB, UINT32 events);
/**
* @ingroup los_event
* @brief Clear the event of the eventCB by a specified eventMask.
*
* @par Description:
* <ul>
* <li>This API is used to set the ID of an event that has a specified mask and of which the information is stored in
* an event control block pointed to by eventCB to 0. eventCB must point to valid memory.</li>
* </ul>
* @attention
* <ul>
* <li>The value of events needs to be reversed when it is passed-in.</li>
* </ul>
*
* @param eventCB [IN/OUT] Pointer to the event control block to be cleared.
* @param eventMask [IN] Mask of the event to be cleared.
*
* @retval #LOS_ERRNO_EVENT_PTR_NULL Null pointer.
* @retval #LOS_OK The event is successfully cleared.
* @par Dependency:
* <ul><li>los_event.h: the header file that contains the API declaration.</li></ul>
* @see LOS_EventPoll | LOS_EventRead | LOS_EventWrite
*/
extern UINT32 LOS_EventClear(PEVENT_CB_S eventCB, UINT32 eventMask);
/**
* @ingroup los_event
* @brief Destroy an event.
*
* @par Description:
* <ul>
* <li>This API is used to Destroy an event.</li>
* </ul>
* @attention
* <ul>
* <li>The specific event should be a valid one.</li>
* </ul>
*
* @param eventCB [IN/OUT] Pointer to the event control block to be Destroyed.
*
* @retval #LOS_ERRNO_EVENT_PTR_NULL Null pointer.
* @retval #LOS_OK The event is successfully cleared.
* @par Dependency:
* <ul><li>los_event.h: the header file that contains the API declaration.</li></ul>
* @see LOS_EventPoll | LOS_EventRead | LOS_EventWrite
*/
extern UINT32 LOS_EventDestroy(PEVENT_CB_S eventCB);
extern UINT32 OsEventReadOnce(PEVENT_CB_S eventCB, UINT32 eventMask, UINT32 mode, UINT32 timeOut);
extern UINT32 OsEventWriteOnce(PEVENT_CB_S eventCB, UINT32 events);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_EVENT_H */
@@ -0,0 +1,258 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup los_membox Static memory
* @ingroup kernel
*/
#ifndef _LOS_MEMBOX_H
#define _LOS_MEMBOX_H
#include "los_config.h"
#include "los_debug.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
#define OS_MEMBOX_NEXT(addr, blkSize) (LOS_MEMBOX_NODE *)(VOID *)((UINT8 *)(addr) + (blkSize))
#define OS_MEMBOX_NODE_HEAD_SIZE sizeof(LOS_MEMBOX_NODE)
/**
* @ingroup los_membox
* Structure of a free node in a memory pool
*/
typedef struct tagMEMBOX_NODE {
struct tagMEMBOX_NODE *pstNext; /**< Free node's pointer to the next node in a memory pool */
} LOS_MEMBOX_NODE;
/**
* @ingroup los_membox
* Memory pool information structure
*/
typedef struct LOS_MEMBOX_INFO {
UINT32 uwBlkSize; /**< Block size */
UINT32 uwBlkNum; /**< Block number */
UINT32 uwBlkCnt; /**< The number of allocated blocks */
#if (LOSCFG_PLATFORM_EXC == 1)
struct LOS_MEMBOX_INFO *nextMemBox; /**< Point to the next membox */
#endif
LOS_MEMBOX_NODE stFreeList; /**< Free list */
} LOS_MEMBOX_INFO;
typedef LOS_MEMBOX_INFO OS_MEMBOX_S;
#if (LOSCFG_PLATFORM_EXC == 1)
UINT32 OsMemboxExcInfoGet(UINT32 memNumMax, MemInfoCB *memExcInfo);
#endif
/**
* @ingroup los_membox
* Memory pool alignment
*/
#define LOS_MEMBOX_ALIGNED(memAddr) (((UINTPTR)(memAddr) + sizeof(UINTPTR) - 1) & (~(sizeof(UINTPTR) - 1)))
/**
* @ingroup los_membox
* Memory pool size
*/
#define LOS_MEMBOX_SIZE(blkSize, blkNum) \
(sizeof(LOS_MEMBOX_INFO) + (LOS_MEMBOX_ALIGNED((blkSize) + OS_MEMBOX_NODE_HEAD_SIZE) * (blkNum)))
/**
* @ingroup los_membox
* @brief Initialize a memory pool.
*
* @par Description:
* <ul>
* <li>This API is used to initialize a memory pool.</li>
* </ul>
* @attention
* <ul>
* <li>The poolSize parameter value should match the following two conditions :
* 1) Be less than or equal to the Memory pool size;
* 2) Be greater than the size of LOS_MEMBOX_INFO.</li>
* </ul>
*
* @param pool [IN] Memory pool address.
* @param poolSize [IN] Memory pool size.
* @param blkSize [IN] Memory block size.
*
* @retval #LOS_NOK The memory pool fails to be initialized.
* @retval #LOS_OK The memory pool is successfully initialized.
* @par Dependency:
* <ul>
* <li>los_membox.h: the header file that contains the API declaration.</li>
* </ul>
* @see None.
*/
extern UINT32 LOS_MemboxInit(VOID *pool, UINT32 poolSize, UINT32 blkSize);
/**
* @ingroup los_membox
* @brief Request a memory block.
*
* @par Description:
* <ul>
* <li>This API is used to request a memory block.</li>
* </ul>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemboxInit.</li>
* </ul>
*
* @param pool [IN] Memory pool address.
*
* @retval #VOID* The request is accepted, and return a memory block address.
* @retval #NULL The request fails.
* @par Dependency:
* <ul>
* <li>los_membox.h: the header file that contains the API declaration.</li>
* </ul>
* @see LOS_MemboxFree
*/
extern VOID *LOS_MemboxAlloc(VOID *pool);
/**
* @ingroup los_membox
* @brief Free a memory block.
*
* @par Description:
* <ul>
* <li>This API is used to free a memory block.</li>
* </ul>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemboxInit.</li>
* <li>The input box parameter must be allocated by LOS_MemboxAlloc.</li>
* </ul>
*
* @param pool [IN] Memory pool address.
* @param box [IN] Memory block address.
*
* @retval #LOS_NOK This memory block fails to be freed.
* @retval #LOS_OK This memory block is successfully freed.
* @par Dependency:
* <ul>
* <li>los_membox.h: the header file that contains the API declaration.</li>
* </ul>
* @see LOS_MemboxAlloc
*/
extern UINT32 LOS_MemboxFree(VOID *pool, VOID *box);
/**
* @ingroup los_membox
* @brief Clear a memory block.
*
* @par Description:
* <ul>
* <li>This API is used to set the memory block value to be 0.</li>
* </ul>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemboxInit.</li>
* <li>The input box parameter must be allocated by LOS_MemboxAlloc.</li>
* </ul>
*
* @param pool [IN] Memory pool address.
* @param box [IN] Memory block address.
*
* @retval VOID
* @par Dependency:
* <ul>
* <li>los_membox.h: the header file that contains the API declaration.</li>
* </ul>
* @see None.
*/
extern VOID LOS_MemboxClr(VOID *pool, VOID *box);
/**
* @ingroup los_membox
* @brief show membox info.
*
* @par Description:
* <ul>
* <li>This API is used to show the memory pool info.</li>
* </ul>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemboxInit.</li>
* </ul>
*
* @param pool [IN] Memory pool address.
*
* @retval VOID
* @par Dependency:
* <ul>
* <li>los_membox.h: the header file that contains the API declaration.</li>
* </ul>
* @see None.
*/
extern VOID LOS_ShowBox(VOID *pool);
/**
* @ingroup los_membox
* @brief calculate membox information.
*
* @par Description:
* <ul>
* <li>This API is used to calculate membox information.</li>
* </ul>
* @attention
* <ul>
* <li>One parameter of this interface is a pointer, it should be a correct value, otherwise, the system may
* be abnormal.</li>
* </ul>
*
* @param boxMem [IN] Type #VOID* Pointer to the calculate membox.
* @param maxBlk [OUT] Type #UINT32* Record membox max block.
* @param blkCnt [OUT] Type #UINT32* Record membox block count alreay allocated.
* @param blkSize [OUT] Type #UINT32* Record membox block size.
*
* @retval #LOS_OK The heap status calculate success.
* @retval #LOS_NOK The membox status calculate with some error.
* @par Dependency:
* <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
* @see LOS_MemAlloc | LOS_MemRealloc | LOS_MemFree
*/
extern UINT32 LOS_MemboxStatisticsGet(const VOID *boxMem, UINT32 *maxBlk, UINT32 *blkCnt, UINT32 *blkSize);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_MEMBOX_H */
@@ -0,0 +1,438 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup los_memory Dynamic memory
* @ingroup kernel
*/
#ifndef _LOS_MEMORY_H
#define _LOS_MEMORY_H
#include "los_config.h"
#include "los_list.h"
#include "los_debug.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
#if (LOSCFG_PLATFORM_EXC == 1)
UINT32 OsMemExcInfoGet(UINT32 memNumMax, MemInfoCB *memExcInfo);
#endif
/**
* @ingroup los_memory
* Starting address of the memory.
*/
#define OS_SYS_MEM_ADDR LOSCFG_SYS_HEAP_ADDR
#if (LOSCFG_MEM_LEAKCHECK == 1)
/**
* @ingroup los_memory
* @brief Print function call stack information of all used nodes.
*
* @par Description:
* <ul>
* <li>This API is used to print function call stack information of all used nodes.</li>
* </ul>
*
* @param pool [IN] Starting address of memory.
*
* @retval none.
* @par Dependency:
* <ul>
* <li>los_memory.h: the header file that contains the API declaration.</li>
* </ul>
* @see None.
*/
extern VOID LOS_MemUsedNodeShow(VOID *pool);
#endif
#if (LOSCFG_MEM_MUL_POOL == 1)
/**
* @ingroup los_memory
* @brief Deinitialize dynamic memory.
*
* @par Description:
* <ul>
* <li>This API is used to deinitialize the dynamic memory of a doubly linked list.</li>
* </ul>
*
* @param pool [IN] Starting address of memory.
*
* @retval #OS_ERROR The dynamic memory fails to be deinitialized.
* @retval #LOS_OK The dynamic memory is successfully deinitialized.
* @par Dependency:
* <ul>
* <li>los_memory.h: the header file that contains the API declaration.</li>
* </ul>
* @see None.
*/
extern UINT32 LOS_MemDeInit(VOID *pool);
/**
* @ingroup los_memory
* @brief Print infomation about all pools.
*
* @par Description:
* <ul>
* <li>This API is used to print infomation about all pools.</li>
* </ul>
*
* @retval #UINT32 The pool number.
* @par Dependency:
* <ul>
* <li>los_memory.h: the header file that contains the API declaration.</li>
* </ul>
* @see None.
*/
extern UINT32 LOS_MemPoolList(VOID);
#endif
/**
* @ingroup los_memory
* Memory pool extern information structure
*/
typedef struct {
UINT32 totalUsedSize;
UINT32 totalFreeSize;
UINT32 maxFreeNodeSize;
UINT32 usedNodeNum;
UINT32 freeNodeNum;
#if (LOSCFG_MEM_WATERLINE == 1)
UINT32 usageWaterLine;
#endif
} LOS_MEM_POOL_STATUS;
/**
* @ingroup los_memory
* @brief Initialize dynamic memory.
*
* @par Description:
* <ul>
* <li>This API is used to initialize the dynamic memory of a doubly linked list.</li>
* </ul>
* @attention
* <ul>
* <li>The size parameter value should match the following two conditions :
* 1) Be less than or equal to the Memory pool size;
* 2) Be greater than the size of OS_MEM_MIN_POOL_SIZE.</li>
* <li>Call this API when dynamic memory needs to be initialized during the startup of Huawei LiteOS.</li>
* <li>The parameter input must be four byte-aligned.</li>
* <li>The init area [pool, pool + size] should not conflict with other pools.</li>
* </ul>
*
* @param pool [IN] Starting address of memory.
* @param size [IN] Memory size.
*
* @retval #OS_ERROR The dynamic memory fails to be initialized.
* @retval #LOS_OK The dynamic memory is successfully initialized.
* @par Dependency:
* <ul>
* <li>los_memory.h: the header file that contains the API declaration.</li>
* </ul>
* @see None.
*/
extern UINT32 LOS_MemInit(VOID *pool, UINT32 size);
/**
* @ingroup los_memory
* @brief Allocate dynamic memory.
*
* @par Description:
* <ul>
* <li>This API is used to allocate a memory block of which the size is specified.</li>
* </ul>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
* <li>The size of the input parameter size can not be greater than the memory pool size that specified at the second
* input parameter of LOS_MemInit.</li>
* <li>The size of the input parameter size must be four byte-aligned.</li>
* </ul>
*
* @param pool [IN] Pointer to the memory pool that contains the memory block to be allocated.
* @param size [IN] Size of the memory block to be allocated (unit: byte).
*
* @retval #NULL The memory fails to be allocated.
* @retval #VOID* The memory is successfully allocated with the starting address of the allocated memory block
* returned.
* @par Dependency:
* <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
* @see LOS_MemRealloc | LOS_MemAllocAlign | LOS_MemFree
*/
extern VOID *LOS_MemAlloc(VOID *pool, UINT32 size);
/**
* @ingroup los_memory
* @brief Free dynamic memory.
*
* @par Description:
* <li>This API is used to free specified dynamic memory that has been allocated.</li>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
* <li>The input ptr parameter must be allocated by LOS_MemAlloc or LOS_MemAllocAlign or LOS_MemRealloc.</li>
* </ul>
*
* @param pool [IN] Pointer to the memory pool that contains the dynamic memory block to be freed.
* @param ptr [IN] Starting address of the memory block to be freed.
*
* @retval #LOS_NOK The memory block fails to be freed because the starting address of the memory block is
* invalid, or the memory overwriting occurs.
* @retval #LOS_OK The memory block is successfully freed.
* @par Dependency:
* <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
* @see LOS_MemAlloc | LOS_MemRealloc | LOS_MemAllocAlign
*/
extern UINT32 LOS_MemFree(VOID *pool, VOID *ptr);
/**
* @ingroup los_memory
* @brief Re-allocate a memory block.
*
* @par Description:
* <ul>
* <li>This API is used to allocate a new memory block of which the size is specified by size if the original memory
* block size is insufficient. The new memory block will copy the data in the original memory block of which the
* address is specified by ptr. The size of the new memory block determines the maximum size of data to be copied.
* After the new memory block is created, the original one is freed.</li>
* </ul>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
* <li>The input ptr parameter must be allocated by LOS_MemAlloc or LOS_MemAllocAlign.</li>
* <li>The size of the input parameter size can not be greater than the memory pool size that specified at the second
* input parameter of LOS_MemInit.</li>
* <li>The size of the input parameter size must be aligned as follows: 1) if the ptr is allocated by LOS_MemAlloc,
* it must be four byte-aligned; 2) if the ptr is allocated by LOS_MemAllocAlign, it must be aligned with the size of
* the input parameter boundary of LOS_MemAllocAlign.</li>
* </ul>
*
* @param pool [IN] Pointer to the memory pool that contains the original and new memory blocks.
* @param ptr [IN] Address of the original memory block.
* @param size [IN] Size of the new memory block.
*
* @retval #NULL The memory fails to be re-allocated.
* @retval #VOID* The memory is successfully re-allocated with the starting address of the new memory block returned.
* @par Dependency:
* <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
* @see LOS_MemAlloc | LOS_MemAllocAlign | LOS_MemFree
*/
extern VOID *LOS_MemRealloc(VOID *pool, VOID *ptr, UINT32 size);
/**
* @ingroup los_memory
* @brief Allocate aligned memory.
*
* @par Description:
* <ul>
* <li>This API is used to allocate memory blocks of specified size and of which the starting addresses are aligned on
* a specified boundary.</li>
* </ul>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
* <li>The size of the input parameter size can not be greater than the memory pool size that specified at the second
* input parameter of LOS_MemInit.</li>
* <li>The alignment parameter value must be a power of 2 with the minimum value being 4.</li>
* </ul>
*
* @param pool [IN] Pointer to the memory pool that contains the memory blocks to be allocated.
* @param size [IN] Size of the memory to be allocated.
* @param boundary [IN] Boundary on which the memory is aligned.
*
* @retval #NULL The memory fails to be allocated.
* @retval #VOID* The memory is successfully allocated with the starting address of the allocated memory returned.
* @par Dependency:
* <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
* @see LOS_MemAlloc | LOS_MemRealloc | LOS_MemFree
*/
extern VOID *LOS_MemAllocAlign(VOID *pool, UINT32 size, UINT32 boundary);
/**
* @ingroup los_memory
* @brief Get the size of memory pool's size.
*
* @par Description:
* <ul>
* <li>This API is used to get the size of memory pool' total size.</li>
* </ul>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
* </ul>
*
* @param pool [IN] A pointer pointed to the memory pool.
*
* @retval #LOS_NOK The incoming parameter pool is NULL.
* @retval #UINT32 The size of the memory pool.
* @par Dependency:
* <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 LOS_MemPoolSizeGet(const VOID *pool);
/**
* @ingroup los_memory
* @brief Get the size of memory totally used.
*
* @par Description:
* <ul>
* <li>This API is used to get the size of memory totally used in memory pool.</li>
* </ul>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
* </ul>
*
* @param pool [IN] A pointer pointed to the memory pool.
*
* @retval #LOS_NOK The incoming parameter pool is NULL.
* @retval #UINT32 The size of the memory pool used.
* @par Dependency:
* <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 LOS_MemTotalUsedGet(VOID *pool);
/**
* @ingroup los_memory
* @brief Get the infomation of memory pool.
*
* @par Description:
* <ul>
* <li>This API is used to get the infomation of memory pool.</li>
* </ul>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
* </ul>
*
* @param pool [IN] A pointer pointed to the memory pool.
* @param poolStatus [IN] A pointer for storage the pool status
*
* @retval #LOS_NOK The incoming parameter pool is NULL or invalid.
* @retval #LOS_OK Success to get memory infomation.
* @par Dependency:
* <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 LOS_MemInfoGet(VOID *pool, LOS_MEM_POOL_STATUS *poolStatus);
/**
* @ingroup los_memory
* @brief Get the number of free node in every size.
*
* @par Description:
* <ul>
* <li>This API is used to get the number of free node in every size.</li>
* </ul>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
* </ul>
*
* @param pool [IN] A pointer pointed to the memory pool.
*
* @retval #LOS_NOK The incoming parameter pool is NULL.
* @retval #UINT32 The address of the last used node that casts to UINT32.
* @par Dependency:
* <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 LOS_MemFreeNodeShow(VOID *pool);
/**
* @ingroup los_memory
* @brief Check the memory pool integrity.
*
* @par Description:
* <ul>
* <li>This API is used to check the memory pool integrity.</li>
* </ul>
* @attention
* <ul>
* <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
* <li>LOS_MemIntegrityCheck will be called by malloc function when the macro of LOSCFG_BASE_MEM_NODE_INTEGRITY_CHECK
* is defined in LiteOS.</li>
* <li>LOS_MemIntegrityCheck function can be called by user anytime.</li>
* </ul>
*
* @param pool [IN] A pointer pointed to the memory pool.
*
* @retval #LOS_NOK The memory pool (pool) is impaired.
* @retval #LOS_OK The memory pool (pool) is integrated.
* @par Dependency:
* <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 LOS_MemIntegrityCheck(const VOID *pool);
/**
* @ingroup los_memory
* @brief Enable memory pool to support no internal lock during using interfaces.
*
* @par Description:
* <ul>
* <li>This API is used to enable memory pool to support no internal lock during using interfaces,</li>
* <li>such as LOS_MemAlloc/LOS_MemAllocAlign/LOS_MemRealloc/LOS_MemFree and so on.
* </ul>
* @attention
* <ul>
* <li>The memory pool does not support multi-threaded concurrent application scenarios.
* <li>If you want to use this function, you need to call this interface before the memory
* pool is used, it cannot be called during the trial period.</li>
* </ul>
*
* @param pool [IN] Starting address of memory.
*
* @retval node.
* @par Dependency:
* <ul>
* <li>los_memory.h: the header file that contains the API declaration.</li>
* </ul>
* @see None.
*/
extern VOID LOS_MemUnlockEnable(VOID *pool);
extern UINT32 OsMemSystemInit(VOID);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_MEMORY_H */
@@ -0,0 +1,350 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup los_mux Mutex
* @ingroup kernel
*/
#ifndef _LOS_MUX_H
#define _LOS_MUX_H
#include "los_task.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
/**
* @ingroup los_mux
* Mutex error code: The memory request fails.
*
* Value: 0x02001d00
*
* Solution: Decrease the number of mutexes defined by LOSCFG_BASE_IPC_MUX_LIMIT.
*/
#define LOS_ERRNO_MUX_NO_MEMORY LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x00)
/**
* @ingroup los_mux
* Mutex error code: The mutex is not usable.
*
* Value: 0x02001d01
*
* Solution: Check whether the mutex ID and the mutex state are applicable for the current operation.
*/
#define LOS_ERRNO_MUX_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x01)
/**
* @ingroup los_mux
* Mutex error code: Null pointer.
*
* Value: 0x02001d02
*
* Solution: Check whether the input parameter is usable.
*/
#define LOS_ERRNO_MUX_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x02)
/**
* @ingroup los_mux
* Mutex error code: No mutex is available and the mutex request fails.
*
* Value: 0x02001d03
*
* Solution: Increase the number of mutexes defined by LOSCFG_BASE_IPC_MUX_LIMIT.
*/
#define LOS_ERRNO_MUX_ALL_BUSY LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x03)
/**
* @ingroup los_mux
* Mutex error code: The mutex fails to be locked in non-blocking mode because it is locked by another thread.
*
* Value: 0x02001d04
*
* Solution: Lock the mutex after it is unlocked by the thread that owns it, or set a waiting time.
*/
#define LOS_ERRNO_MUX_UNAVAILABLE LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x04)
/**
* @ingroup los_mux
* Mutex error code: The mutex is being locked during an interrupt.
*
* Value: 0x02001d05
*
* Solution: Check whether the mutex is being locked during an interrupt.
*/
#define LOS_ERRNO_MUX_PEND_INTERR LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x05)
/**
* @ingroup los_mux
* Mutex error code: A thread locks a mutex after waiting for the mutex to be unlocked by another thread
* when the task scheduling is disabled.
*
* Value: 0x02001d06
*
* Solution: Check whether the task scheduling is disabled, or set uwtimeout to 0, which means that the
* thread will not wait for the mutex to become available.
*/
#define LOS_ERRNO_MUX_PEND_IN_LOCK LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x06)
/**
* @ingroup los_mux
* Mutex error code: The mutex locking times out.
*
* Value: 0x02001d07
*
* Solution: Increase the waiting time or set the waiting time to LOS_WAIT_FOREVER (forever-blocking mode).
*/
#define LOS_ERRNO_MUX_TIMEOUT LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x07)
/**
* @ingroup los_mux
*
* Value: 0x02001d08
* Not in use temporarily.
*/
#define LOS_ERRNO_MUX_OVERFLOW LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x08)
/**
* @ingroup los_mux
* Mutex error code: The mutex to be deleted is being locked.
*
* Value: 0x02001d09
*
* Solution: Delete the mutex after it is unlocked.
*/
#define LOS_ERRNO_MUX_PENDED LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x09)
/**
* @ingroup los_mux
*
* Value: 0x02001d0A
* Not in use temporarily.
*/
#define LOS_ERRNO_MUX_GET_COUNT_ERR LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x0A)
/**
* @ingroup los_mux
*
* Value: 0x02001d0B
* Not in use temporarily.
*/
#define LOS_ERRNO_MUX_REG_ERROR LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x0B)
/**
* @ingroup los_mux
*
* Mutex error code: LOS_ERRNO_MUX_MAXNUM_ZERO is zero.
* Value: 0x02001d0C
*
* Solution: LOS_ERRNO_MUX_MAXNUM_ZERO should not be zero.
*/
#define LOS_ERRNO_MUX_MAXNUM_ZERO LOS_ERRNO_OS_ERROR(LOS_MOD_MUX, 0x0C)
/**
* @ingroup los_mux
* @brief Create a mutex.
*
* @par Description:
* This API is used to create a mutex. A mutex handle is assigned to muxHandle when the mutex is created successfully.
* Return LOS_OK on creating successful, return specific error code otherwise.
* @attention
* <ul>
* <li>The total number of mutexes is pre-configured. If there are no available mutexes, the mutex creation fails.</li>
* </ul>
*
* @param muxHandle [OUT] Handle pointer of the successfully created mutex. The value of handle should be in
* [0, LOSCFG_BASE_IPC_MUX_LIMIT - 1].
*
* @retval #LOS_ERRNO_MUX_PTR_NULL The muxHandle pointer is NULL.
* @retval #LOS_ERRNO_MUX_ALL_BUSY No available mutex.
* @retval #LOS_OK The mutex is successfully created.
* @par Dependency:
* <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
* @see LOS_MuxDelete
*/
extern UINT32 LOS_MuxCreate(UINT32 *muxHandle);
/**
* @ingroup los_mux
* @brief Delete a mutex.
*
* @par Description:
* This API is used to delete a specified mutex. Return LOS_OK on deleting successfully, return specific error code
* otherwise.
* @attention
* <ul>
* <li>The specific mutex should be created firstly.</li>
* <li>The mutex can be deleted successfully only if no other tasks pend on it.</li>
* </ul>
*
* @param muxHandle [IN] Handle of the mutex to be deleted. The value of handle should be in
* [0, LOSCFG_BASE_IPC_MUX_LIMIT - 1].
*
* @retval #LOS_ERRNO_MUX_INVALID Invalid handle or mutex in use.
* @retval #LOS_ERRNO_MUX_PENDED Tasks pended on this mutex.
* @retval #LOS_OK The mutex is successfully deleted.
* @par Dependency:
* <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
* @see LOS_MuxCreate
*/
extern UINT32 LOS_MuxDelete(UINT32 muxHandle);
/**
* @ingroup los_mux
* @brief Wait to lock a mutex.
*
* @par Description:
* This API is used to wait for a specified period of time to lock a mutex.
* @attention
* <ul>
* <li>The specific mutex should be created firstly.</li>
* <li>The function fails if the mutex that is waited on is already locked by another thread when the task scheduling
* is disabled.</li>
* <li>Do not wait on a mutex during an interrupt.</li>
* <li>The priority inheritance protocol is supported. If a higher-priority thread is waiting on a mutex, it changes
* the priority of the thread that owns the mutex to avoid priority inversion.</li>
* <li>A recursive mutex can be locked more than once by the same thread.</li>
* </ul>
*
* @param muxHandle [IN] Handle of the mutex to be waited on. The value of handle should be
* in [0, LOSCFG_BASE_IPC_MUX_LIMIT - 1].
* @param timeout [IN] Waiting time. The value range is [0, LOS_WAIT_FOREVER](unit: Tick).
*
* @retval #LOS_ERRNO_MUX_INVALID The mutex state (for example, the mutex does not exist or is not in use)
* is not applicable for the current operation.
* @retval #LOS_ERRNO_MUX_UNAVAILABLE The mutex fails to be locked because it is locked by another thread and
* a period of time is not set for waiting for the mutex to become available.
* @retval #LOS_ERRNO_MUX_PEND_INTERR The mutex is being locked during an interrupt.
* @retval #LOS_ERRNO_MUX_PEND_IN_LOCK The mutex is waited on when the task scheduling is disabled.
* @retval #LOS_ERRNO_MUX_TIMEOUT The mutex waiting times out.
* @retval #LOS_OK The mutex is successfully locked.
* @par Dependency:
* <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
* @see LOS_MuxCreate | LOS_MuxPost
*/
extern UINT32 LOS_MuxPend(UINT32 muxHandle, UINT32 timeout);
/**
* @ingroup los_mux
* @brief Release a mutex.
*
* @par Description:
* This API is used to release a specified mutex.
* @attention
* <ul>
* <li>The specific mutex should be created firstly.</li>
* <li>Do not release a mutex during an interrupt.</li>
* <li>If a recursive mutex is locked for many times, it must be unlocked for the same times to be released.</li>
* </ul>
*
* @param muxHandle [IN] Handle of the mutex to be released. The value of handle should be in
* [0, LOSCFG_BASE_IPC_MUX_LIMIT - 1].
*
* @retval #LOS_ERRNO_MUX_INVALID The mutex state (for example, the mutex does not exist or is not in use
* or owned by other thread) is not applicable for the current operation.
* @retval #LOS_ERRNO_MUX_PEND_INTERR The mutex is being released during an interrupt.
* @retval #LOS_OK The mutex is successfully released.
* @par Dependency:
* <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
* @see LOS_MuxCreate | LOS_MuxPend
*/
extern UINT32 LOS_MuxPost(UINT32 muxHandle);
/**
* @ingroup los_mux
* Mutex object.
*/
typedef struct {
UINT8 muxStat; /**< State OS_MUX_UNUSED,OS_MUX_USED */
UINT16 muxCount; /**< Times of locking a mutex */
UINT32 muxID; /**< Handle ID */
LOS_DL_LIST muxList; /**< Mutex linked list */
LosTaskCB *owner; /**< The current thread that is locking a mutex */
UINT16 priority; /**< Priority of the thread that is locking a mutex */
} LosMuxCB;
/**
* @ingroup los_mux
* Mutex state: not in use.
*/
#define OS_MUX_UNUSED 0
/**
* @ingroup los_mux
* Mutex state: in use.
*/
#define OS_MUX_USED 1
extern LosMuxCB *g_allMux;
/**
* @ingroup los_mux
* Obtain the pointer to a mutex object of the mutex that has a specified handle.
*/
#define GET_MUX(muxid) (((LosMuxCB *)g_allMux) + (muxid))
/**
* @ingroup los_mux
* @brief Initializes the mutex.
*
* @par Description:
* This API is used to initializes the mutex.
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param None.
*
* @retval UINT32 Initialization result.
* @par Dependency:
* <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
* @see LOS_MuxDelete
*/
extern UINT32 OsMuxInit(VOID);
/**
* @ingroup los_mux
* Obtain the pointer to the linked list in the mutex pointed to by a specified pointer.
*/
#define GET_MUX_LIST(ptr) LOS_DL_LIST_ENTRY(ptr, LosMuxCB, muxList)
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* _LOS_MUX_H */
@@ -0,0 +1,912 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup los_queue Queue
* @ingroup kernel
*/
#ifndef _LOS_QUEUE_H
#define _LOS_QUEUE_H
#include "los_list.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
/**
* @ingroup los_queue
* Queue error code: The maximum number of queue resources is configured to 0.
*
* Value: 0x02000600
*
* Solution: Configure the maximum number of queue resources to be greater than 0. If queue modules are not used,
* set the configuration item for the tailoring of the maximum number of queue resources to NO.
*/
#define LOS_ERRNO_QUEUE_MAXNUM_ZERO LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x00)
/**
* @ingroup los_queue
* Queue error code: The queue block memory fails to be initialized.
*
* Value: 0x02000601
*
* Solution: Allocate the queue block bigger memory partition, or decrease the maximum number of queue resources.
*/
#define LOS_ERRNO_QUEUE_NO_MEMORY LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x01)
/**
* @ingroup los_queue
* Queue error code: The memory for queue creation fails to be requested.
*
* Value: 0x02000602
*
* Solution: Allocate more memory for queue creation, or decrease the queue length and the number of nodes
* in the queue to be created.
*/
#define LOS_ERRNO_QUEUE_CREATE_NO_MEMORY LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x02)
/**
* @ingroup los_queue
* Queue error code: The size of the biggest message in the created queue is too big.
*
* Value: 0x02000603
*
* Solution: Change the size of the biggest message in the created queue.
*/
#define LOS_ERRNO_QUEUE_SIZE_TOO_BIG LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x03)
/**
* @ingroup los_queue
* Queue error code: The upper limit of the number of created queues is exceeded.
*
* Value: 0x02000604
*
* Solution: Increase the configured number of resources for queues.
*/
#define LOS_ERRNO_QUEUE_CB_UNAVAILABLE LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x04)
/**
* @ingroup los_queue
* Queue error code: Invalid queue.
*
* Value: 0x02000605
*
* Solution: Ensure that the passed-in queue ID is valid.
*/
#define LOS_ERRNO_QUEUE_NOT_FOUND LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x05)
/**
* @ingroup los_queue
* Queue error code: The task is forbidden to be blocked on a queue when the task is locked.
*
* Value: 0x02000606
*
* Solution: Unlock the task before using a queue.
*/
#define LOS_ERRNO_QUEUE_PEND_IN_LOCK LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x06)
/**
* @ingroup los_queue
* Queue error code: The time set for waiting to processing the queue expires.
*
* Value: 0x02000607
*
* Solution: Check whether the expiry time setting is appropriate.
*/
#define LOS_ERRNO_QUEUE_TIMEOUT LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x07)
/**
* @ingroup los_queue
* Queue error code: The queue that blocks a task cannot be deleted.
*
* Value: 0x02000608
*
* Solution: Enable the task to obtain resources rather than be blocked on the queue.
*/
#define LOS_ERRNO_QUEUE_IN_TSKUSE LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x08)
/**
* @ingroup los_queue
* Queue error code: The queue cannot be written during an interrupt when the time for waiting to
* processing the queue expires.
*
* Value: 0x02000609
*
* Solution: Set the expiry time to the never-waiting mode, or use asynchronous queues.
*/
#define LOS_ERRNO_QUEUE_WRITE_IN_INTERRUPT LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x09)
/**
* @ingroup los_queue
* Queue error code: The queue is not created.
*
* Value: 0x0200060a
*
* Solution: Check whether the passed-in queue handle value is valid.
*/
#define LOS_ERRNO_QUEUE_NOT_CREATE LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x0a)
/**
* @ingroup los_queue
* Queue error code: Queue reading and writing are not synchronous.
*
* Value: 0x0200060b
*
* Solution: Synchronize queue reading with queue writing.
*/
#define LOS_ERRNO_QUEUE_IN_TSKWRITE LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x0b)
/**
* @ingroup los_queue
* Queue error code: Parameters passed in during queue creation are null pointers.
*
* Value: 0x0200060c
*
* Solution: Ensure the passed-in parameters are not null pointers.
*/
#define LOS_ERRNO_QUEUE_CREAT_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x0c)
/**
* @ingroup los_queue
* Queue error code: The queue length or message node size passed in during queue creation is 0.
*
* Value: 0x0200060d
*
* Solution: Pass in correct queue length and message node size.
*/
#define LOS_ERRNO_QUEUE_PARA_ISZERO LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x0d)
/**
* @ingroup los_queue
* Queue error code: The handle of the queue is invalid.
*
* Value: 0x0200060e
*
* Solution: Check whether the passed-in queue handle value is valid.
*/
#define LOS_ERRNO_QUEUE_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x0e)
/**
* @ingroup los_queue
* Queue error code: The pointer passed in during queue reading is null.
*
* Value: 0x0200060f
*
* Solution: Check whether the passed-in pointer is null.
*/
#define LOS_ERRNO_QUEUE_READ_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x0f)
/**
* @ingroup los_queue
* Queue error code: The buffer size passed in during queue reading is 0.
*
* Value: 0x02000610
*
* Solution: Pass in a correct buffer size.
*/
#define LOS_ERRNO_QUEUE_READSIZE_ISZERO LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x10)
/**
* @ingroup los_queue
* Queue error code: The pointer passed in during queue writing is null.
*
* Value: 0x02000612
*
* Solution: Check whether the passed-in pointer is null.
*/
#define LOS_ERRNO_QUEUE_WRITE_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x12)
/**
* @ingroup los_queue
* Queue error code: The buffer size passed in during queue writing is 0.
*
* Value: 0x02000613
*
* Solution: Pass in a correct buffer size.
*/
#define LOS_ERRNO_QUEUE_WRITESIZE_ISZERO LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x13)
/**
* @ingroup los_queue
* Queue error code: The buffer size passed in during queue writing is bigger than the queue size.
*
* Value: 0x02000615
*
* Solution: Decrease the buffer size, or use a queue in which nodes are bigger.
*/
#define LOS_ERRNO_QUEUE_WRITE_SIZE_TOO_BIG LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x15)
/**
* @ingroup los_queue
* Queue error code: No free node is available during queue writing.
*
* Value: 0x02000616
*
* Solution: Ensure that free nodes are available before queue writing.
*/
#define LOS_ERRNO_QUEUE_ISFULL LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x16)
/**
* @ingroup los_queue
* Queue error code: The pointer passed in when the queue information is being obtained is null.
*
* Value: 0x02000617
*
* Solution: Check whether the passed-in pointer is null.
*/
#define LOS_ERRNO_QUEUE_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x17)
/**
* @ingroup los_queue
* Queue error code: The queue cannot be read during an interrupt
* when the time for waiting to processing the queue expires.
*
* Value: 0x02000618
*
* Solution: Set the expiry time to the never-waiting mode, or use asynchronous queues.
*/
#define LOS_ERRNO_QUEUE_READ_IN_INTERRUPT LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x18)
/**
* @ingroup los_queue
* Queue error code: The handle of the queue passed-in when the memory for the queue is being freed is invalid.
*
* Value: 0x02000619
*
* Solution: Check whether the passed-in queue handle value is valid.
*/
#define LOS_ERRNO_QUEUE_MAIL_HANDLE_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x19)
/**
* @ingroup los_queue
* Queue error code: The pointer to the memory to be freed is null.
*
* Value: 0x0200061a
*
* Solution: Check whether the passed-in pointer is null.
*/
#define LOS_ERRNO_QUEUE_MAIL_PTR_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x1a)
/**
* @ingroup los_queue
* Queue error code: The memory for the queue fails to be freed.
*
* Value: 0x0200061b
*
* Solution: Pass in correct input parameters.
*/
#define LOS_ERRNO_QUEUE_MAIL_FREE_ERROR LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x1b)
/**
* @ingroup los_queue
* Queue error code: No resource is in the queue that is being read when the
* time for waiting to processing the queue expires.
*
* Value: 0x0200061d
*
* Solution: Ensure that the queue contains messages when it is being read.
*/
#define LOS_ERRNO_QUEUE_ISEMPTY LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x1d)
/**
* @ingroup los_queue
* Queue error code: The buffer size passed in during queue readding is smaller than the queue size.
*
* Value: 0x0200061f
*
* Solution: Increase the buffer size, or use a queue in which nodes are smaller.
*/
#define LOS_ERRNO_QUEUE_READ_SIZE_TOO_SMALL LOS_ERRNO_OS_ERROR(LOS_MOD_QUE, 0x1f)
/**
* @ingroup los_queue
* Structure of the block for queue information query
*/
typedef struct tagQueueInfo {
UINT32 queueID; /**< Queue ID */
UINT16 queueLen; /**< Queue length */
UINT16 queueSize; /**< Node size */
UINT16 queueHead; /**< Node head */
UINT16 queueTail; /**< Node tail */
UINT16 writableCnt; /**< Count of writable resources */
UINT16 readableCnt; /**< Count of readable resources */
UINT32 waitReadTask; /**< Resource reading task */
UINT32 waitWriteTask; /**< Resource writing task */
UINT32 waitMemTask; /**< Memory task */
} QUEUE_INFO_S;
/**
* @ingroup los_queue
* @brief Create a message queue.
*
* @par Description:
* This API is used to create a message queue.
* @attention
* <ul>
* <li>There are LOSCFG_BASE_IPC_QUEUE_LIMIT queues available, change it's value when necessary.</li>
* </ul>
* @param queueName [IN] Message queue name. Reserved parameter, not used for now.
* @param len [IN] Queue length. The value range is [1,0xffff].
* @param queueID [OUT] ID of the queue control structure that is successfully created.
* @param flags [IN] Queue mode. Reserved parameter, not used for now.
* @param maxMsgSize [IN] Node size. The value range is [1,0xffff-4].
*
* @retval #LOS_OK The message queue is successfully created.
* @retval #LOS_ERRNO_QUEUE_CB_UNAVAILABLE The upper limit of the number of created queues is exceeded.
* @retval #LOS_ERRNO_QUEUE_CREATE_NO_MEMORY Insufficient memory for queue creation.
* @retval #LOS_ERRNO_QUEUE_CREAT_PTR_NULL Null pointer, queueID is NULL.
* @retval #LOS_ERRNO_QUEUE_PARA_ISZERO The queue length or message node size passed in during queue
* creation is 0.
* @retval #LOS_ERRNO_QUEUE_SIZE_TOO_BIG The parameter maxMsgSize is larger than 0xffff - 4.
* @par Dependency:
* <ul><li>los_queue.h: the header file that contains the API declaration.</li></ul>
* @see LOS_QueueDelete
*/
extern UINT32 LOS_QueueCreate(CHAR *queueName,
UINT16 len,
UINT32 *queueID,
UINT32 flags,
UINT16 maxMsgSize);
/**
* @ingroup los_queue
* @brief Read a queue.
*
* @par Description:
* This API is used to read data in a specified queue, and store the obtained data to the address specified
* by bufferAddr. The address and the size of the data to be read are defined by users.
* @attention
* <ul>
* <li>The specific queue should be created firstly.</li>
* <li>Queue reading adopts the fist in first out (FIFO) mode. The data that is first stored in the queue is read
* first.</li>
* <li>bufferAddr stores the obtained data.</li>
* <li>Do not read or write a queue in unblocking modes such as an interrupt.</li>
* <li>This API cannot be called before the Huawei LiteOS is initialized.</li>
* <li>The argument timeOut is a relative time.</li>
* </ul>
*
* @param queueID [IN] Queue ID created by LOS_QueueCreate. The value range is
* [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
* @param bufferAddr [OUT] Starting address that stores the obtained data. The starting address must not be
* null.
* @param bufferSize [IN/OUT] Where to maintain the buffer expected-size before read, and the real-size after read.
* @param timeOut [IN] Expiry time. The value range is [0,LOS_WAIT_FOREVER](unit: Tick).
*
* @retval #LOS_OK The queue is successfully read.
* @retval #LOS_ERRNO_QUEUE_INVALID The handle of the queue that is being read is invalid.
* @retval #LOS_ERRNO_QUEUE_READ_PTR_NULL The pointer passed in during queue reading is null.
* @retval #LOS_ERRNO_QUEUE_READSIZE_ISZERO The buffer size passed in during queue reading is 0.
* @retval #LOS_ERRNO_QUEUE_READ_IN_INTERRUPT The queue cannot be read during an interrupt when the time for
* waiting to processing the queue expires.
* @retval #LOS_ERRNO_QUEUE_NOT_CREATE The queue to be read is not created.
* @retval #LOS_ERRNO_QUEUE_ISEMPTY No resource is in the queue that is being read when the time for
* waiting to processing the queue expires.
* @retval #LOS_ERRNO_QUEUE_PEND_IN_LOCK The task is forbidden to be blocked on a queue when the task is
* locked.
* @retval #LOS_ERRNO_QUEUE_TIMEOUT The time set for waiting to processing the queue expires.
* @retval #LOS_ERRNO_QUEUE_READ_SIZE_TOO_SMALL The buffer size passed in during queue reading is less than
* the queue size.
* @par Dependency:
* <ul><li>los_queue.h: the header file that contains the API declaration.</li></ul>
* @see LOS_QueueWriteCopy | LOS_QueueCreate
*/
extern UINT32 LOS_QueueReadCopy(UINT32 queueID,
VOID *bufferAddr,
UINT32 *bufferSize,
UINT32 timeOut);
/**
* @ingroup los_queue
* @brief Write data into a queue.
*
* @par Description:
* This API is used to write the data of the size specified by bufferSize and stored at the address specified by
* bufferAddr into a queue.
* @attention
* <ul>
* <li>The specific queue should be created firstly.</li>
* <li>Do not read or write a queue in unblocking modes such as interrupt.</li>
* <li>This API cannot be called before the Huawei LiteOS is initialized.</li>
* <li>The data to be written is of the size specified by bufferSize and is stored at the address specified by
* BufferAddr.</li>
* <li>The argument timeOut is a relative time.</li>
* </ul>
*
* @param queueID [IN] Queue ID created by LOS_QueueCreate. The value range is
* [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
* @param bufferAddr [IN] Starting address that stores the data to be written.The starting address must
* not be null.
* @param bufferSize [IN] Passed-in buffer size. The value range is [1,USHRT_MAX - sizeof(UINT32)].
* @param timeOut [IN] Expiry time. The value range is [0,LOS_WAIT_FOREVER](unit: Tick).
*
* @retval #LOS_OK The data is successfully written into the queue.
* @retval #LOS_ERRNO_QUEUE_INVALID The queue handle passed in during queue writing is invalid.
* @retval #LOS_ERRNO_QUEUE_WRITE_PTR_NULL The pointer passed in during queue writing is null.
* @retval #LOS_ERRNO_QUEUE_WRITESIZE_ISZERO The buffer size passed in during queue writing is 0.
* @retval #LOS_ERRNO_QUEUE_WRITE_IN_INTERRUPT The queue cannot be written during an interrupt when the time
* for waiting to processing the queue expires.
* @retval #LOS_ERRNO_QUEUE_NOT_CREATE The queue into which the data is written is not created.
* @retval #LOS_ERRNO_QUEUE_WRITE_SIZE_TOO_BIG The buffer size passed in during queue writing is bigger than
* the queue size.
* @retval #LOS_ERRNO_QUEUE_ISFULL No free node is available during queue writing.
* @retval #LOS_ERRNO_QUEUE_PEND_IN_LOCK The task is forbidden to be blocked on a queue when
* the task is locked.
* @retval #LOS_ERRNO_QUEUE_TIMEOUT The time set for waiting to processing the queue expires.
* @par Dependency:
* <ul><li>los_queue.h: the header file that contains the API declaration.</li></ul>
* @see LOS_QueueReadCopy | LOS_QueueCreate
*/
extern UINT32 LOS_QueueWriteCopy(UINT32 queueID,
VOID *bufferAddr,
UINT32 bufferSize,
UINT32 timeOut);
/**
* @ingroup los_queue
* @brief Read a queue.
*
* @par Description:
* This API is used to read the address of data in a specified queue, and store it to the address specified by
* bufferAddr.
* @attention
* <ul>
* <li>The specific queue should be created firstly.</li>
* <li>Queue reading adopts the fist in first out (FIFO) mode. The data that is first stored in the queue is
* read first.</li>
* <li>bufferAddr stores the obtained data address.</li>
* <li>Do not read or write a queue in unblocking modes such as an interrupt.</li>
* <li>This API cannot be called before the Huawei LiteOS is initialized.</li>
* <li>The argument timeOut is a relative time.</li>
* <li>The bufferSize is not really used in LOS_QueueRead, because the interface is only used to
* obtain the address of data.</li>
* <li>The buffer which the bufferAddr pointing to must be greater than or equal to 4 bytes.</li>
* </ul>
*
* @param queueID [IN] Queue ID created by LOS_QueueCreate. The value range is
* [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
* @param bufferAddr [OUT] Starting address that stores the obtained data. The starting address must
* not be null.
* @param bufferSize [IN] Passed-in buffer size, which must not be 0. The value range is [1,0xffffffff].
* @param timeOut [IN] Expiry time. The value range is [0,LOS_WAIT_FOREVER](unit: Tick).
*
* @retval #LOS_OK The queue is successfully read.
* @retval #LOS_ERRNO_QUEUE_INVALID The handle of the queue that is being read is invalid.
* @retval #LOS_ERRNO_QUEUE_READ_PTR_NULL The pointer passed in during queue reading is null.
* @retval #LOS_ERRNO_QUEUE_READSIZE_ISZERO The buffer size passed in during queue reading is 0.
* @retval #LOS_ERRNO_QUEUE_READ_IN_INTERRUPT The queue cannot be read during an interrupt when the time for
* waiting to processing the queue expires.
* @retval #LOS_ERRNO_QUEUE_NOT_CREATE The queue to be read is not created.
* @retval #LOS_ERRNO_QUEUE_ISEMPTY No resource is in the queue that is being read when the time for
* waiting to processing the queue expires.
* @retval #LOS_ERRNO_QUEUE_PEND_IN_LOCK The task is forbidden to be blocked on a queue when the task is
* locked.
* @retval #LOS_ERRNO_QUEUE_TIMEOUT The time set for waiting to processing the queue expires.
* @par Dependency:
* <ul><li>los_queue.h: The header file that contains the API declaration.</li></ul>
* @see LOS_QueueWrite | LOS_QueueCreate
*/
extern UINT32 LOS_QueueRead(UINT32 queueID,
VOID *bufferAddr,
UINT32 bufferSize,
UINT32 timeOut);
/**
* @ingroup los_queue
* @brief Write data into a queue.
*
* @par Description:
* This API is used to write the address of data specified by bufferAddr into a queue.
* @attention
* <ul>
* <li>The specific queue should be created firstly.</li>
* <li>Do not read or write a queue in unblocking modes such as an interrupt.</li>
* <li>This API cannot be called before the Huawei LiteOS is initialized.</li>
* <li>The address of the data of the size specified by bufferSize and stored at the address specified by
* BufferAddr is to be written.</li>
* <li>The argument timeOut is a relative time.</li>
* <li>The bufferSize is not really used in LOS_QueueWrite, because the interface is only used to write the address
* of data specified by bufferAddr into a queue.</li>
* </ul>
*
* @param queueID [IN] Queue ID created by LOS_QueueCreate. The value range is
* [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
* @param bufferAddr [IN] Starting address that stores the data to be written. The starting address
* must not be null.
* @param bufferSize [IN] Passed-in buffer size, which must not be 0. The value range is [1,0xffffffff].
* @param timeOut [IN] Expiry time. The value range is [0,LOS_WAIT_FOREVER](unit: Tick).
*
* @retval #LOS_OK The data is successfully written into the queue.
* @retval #LOS_ERRNO_QUEUE_INVALID The queue handle passed in during queue writing is invalid.
* @retval #LOS_ERRNO_QUEUE_WRITE_PTR_NULL The pointer passed in during queue writing is null.
* @retval #LOS_ERRNO_QUEUE_WRITESIZE_ISZERO The buffer size passed in during queue writing is 0.
* @retval #LOS_ERRNO_QUEUE_WRITE_IN_INTERRUPT The queue cannot be written during an interrupt when the time for
* waiting to processing the queue expires.
* @retval #LOS_ERRNO_QUEUE_NOT_CREATE The queue into which the data is written is not created.
* @retval #LOS_ERRNO_QUEUE_WRITE_SIZE_TOO_BIG The buffer size passed in during queue writing is bigger than
* the queue size.
* @retval #LOS_ERRNO_QUEUE_ISFULL No free node is available during queue writing.
* @retval #LOS_ERRNO_QUEUE_PEND_IN_LOCK The task is forbidden to be blocked on a queue when the task is
* locked.
* @retval #LOS_ERRNO_QUEUE_TIMEOUT The time set for waiting to processing the queue expires.
* @par Dependency:
* <ul><li>los_queue.h: The header file that contains the API declaration.</li></ul>
* @see LOS_QueueRead | LOS_QueueCreate
*/
extern UINT32 LOS_QueueWrite(UINT32 queueID,
VOID *bufferAddr,
UINT32 bufferSize,
UINT32 timeOut);
/**
* @ingroup los_queue
* @brief Write data into a queue header.
*
* @par Description:
* This API is used to write the data of the size specified by bufferSize and stored at the address specified by
* bufferAddr into a queue header.
* @attention
* <ul>
* <li>Do not read or write a queue in unblocking modes such as an interrupt.</li>
* <li>This API cannot be called before the Huawei LiteOS is initialized.</li>
* <li>The address of the data of the size specified by bufferSize and stored at the address specified by
* BufferAddr is to be written.</li>
* <li>The argument timeOut is a relative time.</li>
* <li>LOS_QueueRead and LOS_QueueWriteHead are a set of interfaces, and the two groups of interfaces need to be used.
* <li>
* </ul>
*
* @param queueID [IN] Queue ID created by LOS_QueueCreate. The value range is
* [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
* @param bufferAddr [OUT] Starting address that stores the data to be written. The starting address
* must not be null.
* @param bufferSize [IN] Passed-in buffer size, which must not be 0. The value range is [1,0xffffffff].
* @param timeOut [IN] Expiry time. The value range is [0,LOS_WAIT_FOREVER](unit: Tick).
*
* @retval #LOS_OK The data is successfully written into the queue.
* @retval #LOS_ERRNO_QUEUE_INVALID The queue handle passed in during queue writing is invalid.
* @retval #LOS_ERRNO_QUEUE_WRITE_PTR_NULL The pointer passed in during queue writing is null.
* @retval #LOS_ERRNO_QUEUE_WRITESIZE_ISZERO The buffer size passed in during queue writing is 0.
* @retval #LOS_ERRNO_QUEUE_WRITE_IN_INTERRUPT The queue cannot be written during an interrupt when the time for
* waiting to processing the queue expires.
* @retval #LOS_ERRNO_QUEUE_NOT_CREATE The queue into which the data is written is not created.
* @retval #LOS_ERRNO_QUEUE_WRITE_SIZE_TOO_BIG The buffer size passed in during queue writing is bigger than
* the queue size.
* @retval #LOS_ERRNO_QUEUE_ISFULL No free node is available during queue writing.
* @retval #LOS_ERRNO_QUEUE_PEND_IN_LOCK The task is forbidden to be blocked on a queue when the task is
* locked.
* @retval #LOS_ERRNO_QUEUE_TIMEOUT The time set for waiting to processing the queue expires.
* @par Dependency:
* <ul><li>los_queue.h: The header file that contains the API declaration.</li></ul>
* @see LOS_QueueRead | LOS_QueueCreate
*/
extern UINT32 LOS_QueueWriteHead(UINT32 queueID,
VOID *bufferAddr,
UINT32 bufferSize,
UINT32 timeOut);
/**
* @ingroup los_queue
* @brief Write data into a queue header.
*
* @par Description:
* This API is used to write the data of the size specified by bufferSize and stored at the address specified by
* bufferAddr into a queue header.
* @attention
* <ul>
* <li>Do not read or write a queue in unblocking modes such as an interrupt.</li>
* <li>This API cannot be called before the Huawei LiteOS is initialized.</li>
* <li>The address of the data of the size specified by bufferSize and stored at the address specified by
* BufferAddr is to be written.</li>
* <li>The argument timeOut is a relative time.</li>
* <li>LOS_QueueRead and LOS_QueueWriteHead are a set of interfaces, and the two groups of interfaces need to be
* used.<li>
* </ul>
*
* @param queueID [IN] Queue ID created by LOS_QueueCreate. The value range is
* [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
* @param bufferAddr [OUT] Starting address that stores the data to be written.
* The starting address must not be null.
* @param bufferSize [IN] Passed-in buffer size, which must not be 0. The value range is [1,0xffffffff].
* @param timeOut [IN] Expiry time. The value range is [0,LOS_WAIT_FOREVER](unit: Tick).
*
* @retval #LOS_OK The data is successfully written into the queue.
* @retval #LOS_ERRNO_QUEUE_INVALID The queue handle passed in during queue writing is invalid.
* @retval #LOS_ERRNO_QUEUE_WRITE_PTR_NULL The pointer passed in during queue writing is null.
* @retval #LOS_ERRNO_QUEUE_WRITESIZE_ISZERO The buffer size passed in during queue writing is 0.
* @retval #LOS_ERRNO_QUEUE_WRITE_IN_INTERRUPT The queue cannot be written during an interrupt when the time for
* waiting to processing the queue expires.
* @retval #LOS_ERRNO_QUEUE_NOT_CREATE The queue into which the data is written is not created.
* @retval #LOS_ERRNO_QUEUE_WRITE_SIZE_TOO_BIG The buffer size passed in during queue writing is bigger than
* the queue size.
* @retval #LOS_ERRNO_QUEUE_ISFULL No free node is available during queue writing.
* @retval #LOS_ERRNO_QUEUE_PEND_IN_LOCK The task is forbidden to be blocked on a queue when the task is
* locked.
* @retval #LOS_ERRNO_QUEUE_TIMEOUT The time set for waiting to processing the queue expires.
* @par Dependency:
* <ul><li>los_queue.h: The header file that contains the API declaration.</li></ul>
* @see LOS_QueueWrite | LOS_QueueWriteHead
*/
extern UINT32 LOS_QueueWriteHeadCopy(UINT32 queueID,
VOID *bufferAddr,
UINT32 bufferSize,
UINT32 timeOut);
/**
* @ingroup los_queue
* @brief Delete a queue.
*
* @par Description:
* This API is used to delete a queue.
* @attention
* <ul>
* <li>This API cannot be used to delete a queue that is not created.</li>
* <li>A synchronous queue fails to be deleted if any tasks are blocked on it, or some queues are being read or
* written.</li>
* </ul>
*
* @param queueID [IN] Queue ID created by LOS_QueueCreate. The value range is
* [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
*
* @retval #LOS_OK The queue is successfully deleted.
* @retval #LOS_ERRNO_QUEUE_NOT_FOUND The queue cannot be found.
* @retval #LOS_ERRNO_QUEUE_NOT_CREATE The queue handle passed in when the queue is being deleted is
* incorrect.
* @retval #LOS_ERRNO_QUEUE_IN_TSKUSE The queue that blocks a task cannot be deleted.
* @retval #LOS_ERRNO_QUEUE_IN_TSKWRITE Queue reading and writing are not synchronous.
* @par Dependency:
* <ul><li>los_queue.h: the header file that contains the API declaration.</li></ul>
* @see LOS_QueueCreate | LOS_QueueCreate
*/
extern UINT32 LOS_QueueDelete(UINT32 queueID);
/**
* @ingroup los_queue
* @brief Obtain queue information.
*
* @par Description:
* This API is used to obtain queue information.
* @attention
* <ul>
* <li>The specific queue should be created firstly.</li>
* </ul>
* @param queueID [IN] Queue ID created by LOS_QueueCreate. The value range is
* [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
* @param queueInfo [OUT] The queue information to be read must not be null.
*
* @retval #LOS_OK The queue information is successfully obtained.
* @retval #LOS_ERRNO_QUEUE_PTR_NULL The pointer to the queue information to be obtained is null.
* @retval #LOS_ERRNO_QUEUE_INVALID The handle of the queue that is being read is invalid.
* @retval #LOS_ERRNO_QUEUE_NOT_CREATE The queue in which the information to be obtained is stored is
* not created.
*
* @par Dependency:
* <ul><li>los_queue.h: the header file that contains the API declaration.</li></ul>
* @see LOS_QueueCreate
*/
extern UINT32 LOS_QueueInfoGet(UINT32 queueID, QUEUE_INFO_S *queueInfo);
typedef enum {
OS_QUEUE_READ,
OS_QUEUE_WRITE
} QueueReadWrite;
typedef enum {
OS_QUEUE_HEAD,
OS_QUEUE_TAIL
} QueueHeadTail;
typedef enum {
OS_QUEUE_NOT_POINT,
OS_QUEUE_POINT
} QueuePointOrNot;
#define OS_QUEUE_OPERATE_TYPE(ReadOrWrite, HeadOrTail, PointOrNot) \
(((UINT32)(PointOrNot) << 2) | ((UINT32)(HeadOrTail) << 1) | (ReadOrWrite))
#define OS_QUEUE_READ_WRITE_GET(type) ((type) & (0x01))
#define OS_QUEUE_READ_HEAD (OS_QUEUE_READ | (OS_QUEUE_HEAD << 1))
#define OS_QUEUE_READ_TAIL (OS_QUEUE_READ | (OS_QUEUE_TAIL << 1))
#define OS_QUEUE_WRITE_HEAD (OS_QUEUE_WRITE | (OS_QUEUE_HEAD << 1))
#define OS_QUEUE_WRITE_TAIL (OS_QUEUE_WRITE | (OS_QUEUE_TAIL << 1))
#define OS_QUEUE_OPERATE_GET(type) ((type) & (0x03))
#define OS_QUEUE_IS_POINT(type) ((type) & (0x04))
#define OS_QUEUE_IS_READ(type) (OS_QUEUE_READ_WRITE_GET(type) == OS_QUEUE_READ)
#define OS_QUEUE_IS_WRITE(type) (OS_QUEUE_READ_WRITE_GET(type) == OS_QUEUE_WRITE)
#define OS_READWRITE_LEN 2
/**
* @ingroup los_queue
* Queue information block structure
*/
typedef struct {
UINT8 *queue; /**< Pointer to a queue handle */
UINT16 queueState; /**< Queue state */
UINT16 queueLen; /**< Queue length */
UINT16 queueSize; /**< Node size */
UINT16 queueID; /**< queueID */
UINT16 queueHead; /**< Node head */
UINT16 queueTail; /**< Node tail */
UINT16 readWriteableCnt[OS_READWRITE_LEN]; /**< Count of readable or writable resources, 0:readable, 1:writable */
LOS_DL_LIST readWriteList[OS_READWRITE_LEN]; /**< Pointer to the linked list to be read or written,
0:readlist, 1:writelist */
LOS_DL_LIST memList; /**< Pointer to the memory linked list */
} LosQueueCB;
/* queue state */
/**
* @ingroup los_queue
* Message queue state: not in use.
*/
#define OS_QUEUE_UNUSED 0
/**
* @ingroup los_queue
* Message queue state: used.
*/
#define OS_QUEUE_INUSED 1
/**
* @ingroup los_queue
* Not in use.
*/
#define OS_QUEUE_WAIT_FOR_POOL 1
/**
* @ingroup los_queue
* Normal message queue.
*/
#define OS_QUEUE_NORMAL 0
/**
* @ingroup los_queue
* Queue information control block
*/
extern LosQueueCB *g_allQueue;
/**
* @ingroup los_queue
* Obtain a handle of the queue that has a specified ID.
*/
#define GET_QUEUE_HANDLE(QueueID) (((LosQueueCB *)g_allQueue) + (QueueID))
/**
* @ingroup los_queue
* Obtain the head node in a queue doubly linked list.
*/
#define GET_QUEUE_LIST(ptr) LOS_DL_LIST_ENTRY(ptr, LosQueueCB, readWriteList[OS_QUEUE_WRITE])
/**
* @ingroup los_queue
* @brief Alloc a stationary memory for a mail.
*
* @par Description:
* This API is used to alloc a stationary memory for a mail according to queueID.
* @attention
* <ul>
* <li>Do not alloc memory in unblocking modes such as interrupt.</li>
* <li>This API cannot be called before the Huawei LiteOS is initialized.</li>
* <li>The argument timeOut is a relative time.</li>
* </ul>
*
* @param queueID [IN] Queue ID. The value range is [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
* @param mailPool [IN] The memory poll that stores the mail.
* @param timeOut [IN] Expiry time. The value range is [0,LOS_WAIT_FOREVER].
*
* @retval #NULL The memory allocation is failed.
* @retval #mem The address of alloc memory.
* @par Dependency:
* <ul><li>los_queue.h: the header file that contains the API declaration.</li></ul>
* @see OsQueueMailFree
*/
extern VOID *OsQueueMailAlloc(UINT32 queueID, VOID *mailPool, UINT32 timeOut);
/**
* @ingroup los_queue
* @brief Free a stationary memory of a mail.
*
* @par Description:
* This API is used to free a stationary memory for a mail according to queueID.
* @attention
* <ul>
* <li>This API cannot be called before the Huawei LiteOS is initialized.</li>
* </ul>
*
* @param queueID [IN] Queue ID. The value range is [1,LOSCFG_BASE_IPC_QUEUE_LIMIT].
* @param mailPool [IN] The mail memory poll address.
* @param mailMem [IN] The mail memory block address.
*
* @retval #LOS_OK 0x00000000: The memory free successfully.
* @retval #OS_ERRNO_QUEUE_MAIL_HANDLE_INVALID 0x02000619: The handle of the queue passed-in when the memory for
the queue is being freed is invalid.
* @retval #OS_ERRNO_QUEUE_MAIL_PTR_INVALID 0x0200061a: The pointer to the memory to be freed is null.
* @retval #OS_ERRNO_QUEUE_MAIL_FREE_ERROR 0x0200061b: The memory for the queue fails to be freed.
* @par Dependency:
* <ul><li>los_queue.h: the header file that contains the API declaration.</li></ul>
* @see OsQueueMailAlloc
*/
extern UINT32 OsQueueMailFree(UINT32 queueID, VOID *mailPool, VOID *mailMem);
/**
* @ingroup los_queue
* @brief Initialization queue.
*
* @par Description:
* This API is used to initialization queue.
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param None.
*
* @retval UINT32 Initialization result.
* @par Dependency:
* <ul><li>los_queue.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 OsQueueInit(VOID);
/**
* @ingroup los_queue
* @brief Handle when read or write queue.
*
* @par Description:
* This API is used to handle when read or write queue.
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param queueID [IN] Queue id.
* @param operateType [IN] Operate type
* @param bufferAddr [IN] Buffer address.
* @param bufferSize [IN] Buffer size.
* @param timeOut [IN] Timeout.
*
* @retval UINT32 Handle result.
* @par Dependency:
* <ul><li>los_queue.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 OsQueueOperate(UINT32 queueID, UINT32 operateType, VOID *bufferAddr, UINT32 *bufferSize,
UINT32 timeOut);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_QUEUE_H */
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LOS_SCHED_H
#define _LOS_SCHED_H
#include "los_task.h"
#include "los_interrupt.h"
#include "los_timer.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
#define OS_SCHED_MAX_RESPONSE_TIME (UINT64)(((UINT64)-1) - 1U)
extern UINT32 g_taskScheduled;
typedef BOOL (*SchedScan)(VOID);
VOID OsSchedUpdateSchedTimeBase(VOID);
UINT64 OsGetCurrSchedTimeCycle(VOID);
VOID OsSchedSetIdleTaskSchedParam(LosTaskCB *idleTask);
UINT32 OsSchedSwtmrScanRegister(SchedScan func);
VOID OsSchedUpdateExpireTime(UINT64 startTime, BOOL timeUpdate);
VOID OsSchedTaskDeQueue(LosTaskCB *taskCB);
VOID OsSchedTaskEnQueue(LosTaskCB *taskCB);
VOID OsSchedTaskWait(LOS_DL_LIST *list, UINT32 timeout);
VOID OsSchedTaskWake(LosTaskCB *resumedTask);
BOOL OsSchedModifyTaskSchedParam(LosTaskCB *taskCB, UINT16 priority);
VOID OsSchedDelay(LosTaskCB *runTask, UINT32 tick);
VOID OsSchedYield(VOID);
VOID OsSchedTaskExit(LosTaskCB *taskCB);
VOID OsSchedTick(VOID);
UINT32 OsSchedInit(VOID);
VOID OsSchedStart(VOID);
BOOL OsSchedTaskSwitch(VOID);
LosTaskCB *OsGetTopTask(VOID);
UINT32 OsSchedRealSleepTimeSet(VOID (*func)(UINT64));
VOID OsSchedTimerBaseReset(UINT64 currTime);
/**
* @ingroup los_sched
* @brief Get the time, in nanoseconds, remaining before the next tick interrupt response.
*
* @par Description:
* This API is used to get the time, in nanoseconds, remaining before the next tick interrupt response.
*
* @attention None.
*
* @param None.
*
* @retval #time, in nanoseconds.
* @par Dependency:
* <ul><li>los_sched.h: the header file that contains the API declaration.</li></ul>
* @see
*/
extern UINT64 LOS_SchedTickTimeoutNsGet(VOID);
/**
* @ingroup los_sched
* @brief The system-provided tick interrupt handler.
*
* @par Description:
* This API is used to wake up a task that is blocked by time.
*
* @attention None.
*
* @param None.
*
* @retval None.
* @par Dependency:
* <ul><li>los_sched.h: the header file that contains the API declaration.</li></ul>
* @see
*/
extern VOID LOS_SchedTickHandler(VOID);
/**
* @ingroup los_sched
* @brief Trigger a system dispatch.
*
* @par Description:
* This API is used to trigger a system dispatch.
*
* @attention None.
*
* @param None.
*
* @retval None.
* @par Dependency:
* <ul><li>los_sched.h: the header file that contains the API declaration.</li></ul>
* @see
*/
extern VOID LOS_Schedule(VOID);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_SCHED_H */
@@ -0,0 +1,382 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup los_sem Semaphore
* @ingroup kernel
*/
#ifndef _LOS_SEM_H
#define _LOS_SEM_H
#include "los_task.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
/**
* @ingroup los_sem
* Semaphore error code: The memory is insufficient.
*
* Value: 0x02000700
*
* Solution: Allocate more memory.
*/
#define LOS_ERRNO_SEM_NO_MEMORY LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x00)
/**
* @ingroup los_sem
* Semaphore error code: Invalid parameter.
*
* Value: 0x02000701
*
* Solution: Change the passed-in invalid parameter value to a valid value.
*/
#define LOS_ERRNO_SEM_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x01)
/**
* @ingroup los_sem
* Semaphore error code: Null pointer.
*
* Value: 0x02000702
*
* Solution: Change the passed-in null pointer to a valid non-null pointer.
*/
#define LOS_ERRNO_SEM_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x02)
/**
* @ingroup los_sem
* Semaphore error code: No semaphore control structure is available.
*
* Value: 0x02000703
*
* Solution: Perform corresponding operations based on the requirements in the code context.
*/
#define LOS_ERRNO_SEM_ALL_BUSY LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x03)
/**
* @ingroup los_sem
* Semaphore error code: Invalid parameter that specifies the timeout interval.
*
* Value: 0x02000704
*
*
* Solution: Change the passed-in parameter value to a valid nonzero value.
*/
#define LOS_ERRNO_SEM_UNAVAILABLE LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x04)
/**
* @ingroup los_sem
* Semaphore error code: The API is called during an interrupt, which is forbidden.
*
* Value: 0x02000705
*
* Solution: Do not call the API during an interrupt.
*/
#define LOS_ERRNO_SEM_PEND_INTERR LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x05)
/**
* @ingroup los_sem
* Semaphore error code: The task is unable to request a semaphore because task scheduling is locked.
*
* Value: 0x02000706
*
* Solution: Do not call LOS_SemPend when task scheduling is locked.
*/
#define LOS_ERRNO_SEM_PEND_IN_LOCK LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x06)
/**
* @ingroup los_sem
* Semaphore error code: The request for a semaphore times out.
*
* Value: 0x02000707
*
* Solution: Change the passed-in parameter value to the value within the valid range.
*/
#define LOS_ERRNO_SEM_TIMEOUT LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x07)
/**
* @ingroup los_sem
* Semaphore error code: The times of semaphore release exceed the maximum times permitted.
*
* Value: 0x02000708
*
* Solution: Perform corresponding operations based on the requirements in the code context.
*/
#define LOS_ERRNO_SEM_OVERFLOW LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x08)
/**
* @ingroup los_sem
* Semaphore error code: The queue of the tasks that are waiting on the semaphore control structure is not null.
*
* Value: 0x02000709
*
* Solution: Delete the semaphore after awaking all tasks that are waiting on the semaphore.
*/
#define LOS_ERRNO_SEM_PENDED LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x09)
/**
* @ingroup los_sem
* Semaphore error code: LOS_ERRNO_SEM_MAXNUM_ZERO is error.
*
* Value: 0x0200070A
*
* Solution: LOS_ERRNO_SEM_MAXNUM_ZERO should not be error.
*/
#define LOS_ERRNO_SEM_MAXNUM_ZERO LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x0A)
/**
* @ingroup los_sem
* @brief Create a Counting semaphore.
*
* @par Description:
* This API is used to create a semaphore control structure according to the initial number of available semaphores
* specified by count and return the ID of this semaphore control structure.
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param count [IN] Initial number of available semaphores. The value range is [0, OS_SEM_COUNTING_MAX_COUNT).
* @param semHandle [OUT] ID of the semaphore control structure that is initialized.
*
* @retval #LOS_ERRNO_SEM_PTR_NULL The passed-in semHandle value is NULL.
* @retval #LOS_ERRNO_SEM_OVERFLOW The passed-in count value is greater than the maximum number of available
* semaphores.
* @retval #LOS_ERRNO_SEM_ALL_BUSY No semaphore control structure is available.
* @retval #LOS_OK The semaphore is successfully created.
* @par Dependency:
* <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
* @see LOS_SemDelete
*/
extern UINT32 LOS_SemCreate(UINT16 count, UINT32 *semHandle);
/**
* @ingroup los_sem
* @brief Create a binary semaphore.
*
* @par Description:
* This API is used to create a binary semaphore control structure according to the initial number of available
* semaphores specified by count and return the ID of this semaphore control structure.
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param count [IN] Initial number of available semaphores. The value range is [0, 1].
* @param semHandle [OUT] ID of the semaphore control structure that is initialized.
*
* @retval #LOS_ERRNO_SEM_PTR_NULL The passed-in semHandle value is NULL.
* @retval #LOS_ERRNO_SEM_OVERFLOW The passed-in count value is greater than the maximum number of available
* semaphores.
* @retval #LOS_ERRNO_SEM_ALL_BUSY No semaphore control structure is available.
* @retval #LOS_OK The semaphore is successfully created.
* @par Dependency:
* <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
* @see LOS_SemDelete
*/
extern UINT32 LOS_BinarySemCreate(UINT16 count, UINT32 *semHandle);
/**
* @ingroup los_sem
* @brief Delete a semaphore.
*
* @par Description:
* This API is used to delete a semaphore control structure that has an ID specified by semHandle.
* @attention
* <ul>
* <li>The specified sem id must be created first. </li>
* </ul>
*
* @param semHandle [IN] ID of the semaphore control structure to be deleted. The ID of the semaphore
* control structure is obtained from semaphore creation.
*
* @retval #LOS_ERRNO_SEM_INVALID The passed-in semHandle value is invalid.
* @retval #LOS_ERRNO_SEM_PENDED The queue of the tasks that are waiting on the semaphore control structure is
* not null.
* @retval #LOS_OK The semaphore control structure is successfully deleted.
* @par Dependency:
* <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
* @see LOS_SemCreate
*/
extern UINT32 LOS_SemDelete(UINT32 semHandle);
/**
* @ingroup los_sem
* @brief Request a semaphore.
*
* @par Description:
* This API is used to request a semaphore based on the semaphore control structure ID specified by semHandle and the
* parameter that specifies the timeout period.
* @attention
* <ul>
* <li>The specified sem id must be created first. </li>
* </ul>
*
* @param semHandle [IN] ID of the semaphore control structure to be requested. The ID of the semaphore control
* structure is obtained from semaphore creation.
* @param timeout [IN] Timeout interval for waiting on the semaphore. The value range is [0, 0xFFFFFFFF].
* If the value is set to 0, the semaphore is not waited on. If the value is set to 0xFFFFFFFF,
* the semaphore is waited on forever(unit: Tick).
*
* @retval #LOS_ERRNO_SEM_INVALID The passed-in semHandle value is invalid.
* @retval #LOS_ERRNO_SEM_UNAVAILABLE There is no available semaphore resource.
* @retval #LOS_ERRNO_SEM_PEND_INTERR The API is called during an interrupt, which is forbidden.
* @retval #LOS_ERRNO_SEM_PEND_IN_LOCK The task is unable to request a semaphore because task scheduling is locked.
* @retval #LOS_ERRNO_SEM_TIMEOUT The request for the semaphore times out.
* @retval #LOS_OK The semaphore request succeeds.
* @par Dependency:
* <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
* @see LOS_SemPost | LOS_SemCreate
*/
extern UINT32 LOS_SemPend(UINT32 semHandle, UINT32 timeout);
/**
* @ingroup los_sem
* @brief Release a semaphore.
*
* @par Description:
* This API is used to release a semaphore that has a semaphore control structure ID specified by semHandle.
* @attention
* <ul>
* <li>The specified sem id must be created first. </li>
* </ul>
*
* @param semHandle [IN] ID of the semaphore control structure to be released.The ID of the semaphore control
* structure is obtained from semaphore creation.
*
* @retval #LOS_ERRNO_SEM_INVALID The passed-in semHandle value is invalid.
* @retval #LOS_ERRNO_SEM_OVERFLOW The times of semaphore release exceed the maximum times permitted.
* @retval #LOS_OK The semaphore is successfully released.
* @par Dependency:
* <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
* @see LOS_SemPend | LOS_SemCreate
*/
extern UINT32 LOS_SemPost(UINT32 semHandle);
extern UINT32 LOS_SemGetValue(UINT32 semHandle, INT32 *currVal);
/**
* @ingroup los_sem
* Semaphore control structure.
*/
typedef struct {
UINT16 semStat; /**< Semaphore state */
UINT16 semCount; /**< Number of available semaphores */
UINT16 maxSemCount; /**< Max number of available semaphores */
UINT16 semID; /**< Semaphore control structure ID */
LOS_DL_LIST semList; /**< Queue of tasks that are waiting on a semaphore */
} LosSemCB;
/**
* @ingroup los_config
* Max count of binary semaphores.
*/
#define OS_SEM_BINARY_MAX_COUNT 1
/**
* @ingroup los_sem
* The semaphore is not in use.
*/
#define OS_SEM_UNUSED 0
/**
* @ingroup los_sem
* The semaphore is used.
*/
#define OS_SEM_USED 1
/**
* @ingroup los_sem
* Obtain the head node in a semaphore doubly linked list.
*/
#define GET_SEM_LIST(ptr) LOS_DL_LIST_ENTRY(ptr, LosSemCB, semList)
extern LosSemCB *g_allSem;
/**
* @ingroup los_sem
* Obtain a semaphore ID.
*
*/
#define GET_SEM(semid) (((LosSemCB *)g_allSem) + (semid))
/**
* @ingroup los_sem
* @brief Initialize the Semaphore doubly linked list.
*
* @par Description:
* This API is used to initialize the Semaphore doubly linked list.
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param None.
*
* @retval UINT32 Initialization result.
* @par Dependency:
* <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 OsSemInit(VOID);
/**
* @ingroup los_sem
* @brief Create Semaphore.
*
* @par Description:
* This API is used to create Semaphore.
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param count [IN]Type #UINT16 Semaphore count.
* @param maxCount [IN]Type #UINT16 Max semaphore count.
* @param semHandle [OUT]Type #UINT32 * Index of semaphore.
*
* @retval UINT32 Create result.
* @par Dependency:
* <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
UINT32 OsSemCreate(UINT16 count, UINT16 maxCount, UINT32 *semHandle);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_SEM_H */
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LOS_SORTLINK_H
#define _LOS_SORTLINK_H
#include "los_compiler.h"
#include "los_list.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
typedef enum {
OS_SORT_LINK_TASK = 1,
OS_SORT_LINK_SWTMR = 2,
} SortLinkType;
typedef struct {
LOS_DL_LIST sortLinkNode;
UINT64 responseTime;
} SortLinkList;
typedef struct {
LOS_DL_LIST sortLink;
} SortLinkAttribute;
#define OS_SORT_LINK_INVALID_TIME ((UINT64)-1)
#define SET_SORTLIST_VALUE(sortList, value) (((SortLinkList *)(sortList))->responseTime = (value))
SortLinkAttribute *OsGetSortLinkAttribute(SortLinkType type);
UINT64 OsGetNextExpireTime(UINT64 startTime);
UINT32 OsSortLinkInit(SortLinkAttribute *sortLinkHeader);
VOID OsDeleteNodeSortLink(SortLinkAttribute *sortLinkHeader, SortLinkList *sortList);
VOID OsAdd2SortLink(SortLinkList *node, UINT64 startTime, UINT32 waitTicks, SortLinkType type);
VOID OsDeleteSortLink(SortLinkList *node, SortLinkType type);
UINT32 OsSortLinkGetTargetExpireTime(UINT64 currTime, const SortLinkList *targetSortList);
UINT32 OsSortLinkGetNextExpireTime(const SortLinkAttribute *sortLinkHeader);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_SORTLINK_H */
@@ -0,0 +1,528 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup los_swtmr Software timer
* @ingroup kernel
*/
#ifndef _LOS_SWTMR_H
#define _LOS_SWTMR_H
#include "los_sortlink.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
/**
* @ingroup los_swtmr
* Software timer error code: The timeout handling function is NULL.
*
* Value: 0x02000300
*
* Solution: Define the timeout handling function.
*/
#define LOS_ERRNO_SWTMR_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x00)
/**
* @ingroup los_swtmr
* Software timer error code: The expiration time is 0.
*
* Value: 0x02000301
*
* Solution: Re-define the expiration time.
*/
#define LOS_ERRNO_SWTMR_INTERVAL_NOT_SUITED LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x01)
/**
* @ingroup los_swtmr
* Software timer error code: Invalid software timer mode.
*
* Value: 0x02000302
*
* Solution: Check the mode value. The value range is [0,3].
*/
#define LOS_ERRNO_SWTMR_MODE_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x02)
/**
* @ingroup los_swtmr
* Software timer error code: The passed-in software timer ID is NULL.
*
* Value: 0x02000303
*
* Solution: Define the software timer ID before passing it in.
*/
#define LOS_ERRNO_SWTMR_RET_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x03)
/**
* @ingroup los_swtmr
* Software timer error code: The number of software timers exceeds the configured permitted maximum number.
*
* Value: 0x02000304
*
* Solution: Re-configure the permitted maximum number of software timers, or wait for a software timer to become
* available.
*/
#define LOS_ERRNO_SWTMR_MAXSIZE LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x04)
/**
* @ingroup los_swtmr
* Software timer error code: Invalid software timer ID.
*
* Value: 0x02000305
*
* Solution: Pass in a valid software timer ID.
*/
#define LOS_ERRNO_SWTMR_ID_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x05)
/**
* @ingroup los_swtmr
* Software timer error code: The software timer is not created.
*
* Value: 0x02000306
*
* Solution: Create a software timer.
*/
#define LOS_ERRNO_SWTMR_NOT_CREATED LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x06)
/**
* @ingroup los_swtmr
* Software timer error code: Insufficient memory for software timer linked list creation.
*
* Value: 0x02000307
*
* Solution: Allocate bigger memory partition to software timer linked list creation.
*/
#define LOS_ERRNO_SWTMR_NO_MEMORY LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x07)
/**
* @ingroup los_swtmr
* Software timer error code: Invalid configured number of software timers.
*
* Value: 0x02000308
*
* Solution: Re-configure the number of software timers.
*/
#define LOS_ERRNO_SWTMR_MAXSIZE_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x08)
/**
* @ingroup los_swtmr
* Software timer error code: The software timer is being used during an interrupt.
*
* Value: 0x02000309
*
* Solution: Change the source code and do not use the software timer during an interrupt.
*/
#define LOS_ERRNO_SWTMR_HWI_ACTIVE LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x09)
/**
* @ingroup los_swtmr
* Software timer error code: Insufficient memory allocated by membox.
*
* Value: 0x0200030a
*
* Solution: Expand the memory allocated by membox.
*/
#define LOS_ERRNO_SWTMR_HANDLER_POOL_NO_MEM LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x0a)
/**
* @ingroup los_swtmr
* Software timer error code: The software timer queue fails to be created.
*
* Value: 0x0200030b
*
* Solution: Check whether more memory can be allocated to the queue to be created.
*/
#define LOS_ERRNO_SWTMR_QUEUE_CREATE_FAILED LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x0b)
/**
* @ingroup los_swtmr
* Software timer error code: The software timer task fails to be created.
*
* Value: 0x0200030c
*
* Solution: Check whether the memory is sufficient and re-create the task.
*/
#define LOS_ERRNO_SWTMR_TASK_CREATE_FAILED LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x0c)
/**
* @ingroup los_swtmr
* Software timer error code: The software timer is not started.
*
* Value: 0x0200030d
*
* Solution: Start the software timer.
*/
#define LOS_ERRNO_SWTMR_NOT_STARTED LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x0d)
/**
* @ingroup los_swtmr
* Software timer error code: Invalid software timer state.
*
* Value: 0x0200030e
*
* Solution: Check the software timer state.
*/
#define LOS_ERRNO_SWTMR_STATUS_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x0e)
/**
* @ingroup los_swtmr
* This error code is not in use temporarily.
*/
#define LOS_ERRNO_SWTMR_SORTLIST_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x0f)
/**
* @ingroup los_swtmr
* Software timer error code: The passed-in number of remaining Ticks configured on the software timer is NULL.
*
* Value: 0x02000310
*
* Solution: Define a variable of the number of remaining Ticks before passing in the number of remaining Ticks.
*/
#define LOS_ERRNO_SWTMR_TICK_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x10)
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
#define OS_ERRNO_SWTMR_ROUSES_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x11)
#define OS_ERRNO_SWTMR_ALIGN_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x12)
enum enSwTmrRousesType {
OS_SWTMR_ROUSES_IGNORE, /* timer don't need to wake up system */
OS_SWTMR_ROUSES_ALLOW, /* timer can wake up system */
};
enum enSwTmrAlignSensitive {
OS_SWTMR_ALIGN_SENSITIVE, /* timer don't need to align */
OS_SWTMR_ALIGN_INSENSITIVE, /* timer need to align */
};
#endif
/**
* @ingroup los_swtmr
* Software timer mode
*/
enum EnSwTmrType {
LOS_SWTMR_MODE_ONCE, /* One-off software timer */
LOS_SWTMR_MODE_PERIOD, /* Periodic software timer */
LOS_SWTMR_MODE_NO_SELFDELETE, /* One-off software timer, but not self-delete */
LOS_SWTMR_MODE_OPP, /* After the one-off timer finishes timing,
the periodic software timer is enabled.
This mode is not supported temporarily. */
};
/**
* @ingroup los_swtmr
* @brief Define the type of a callback function that handles software timer timeout.
*
* @par Description:
* This API is used to define the type of a callback function that handles software timer timeout, so that it can be
* called when software timer timeout.
*
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param para [IN] the parameter of the callback function that handles software timer timeout.
*
* @retval None.
* @par Dependency:
* <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
typedef VOID (*SWTMR_PROC_FUNC)(UINT32 para);
/**
* @ingroup los_swtmr
* Software timer control structure
*/
typedef struct tagSwTmrCtrl {
struct tagSwTmrCtrl *pstNext; /* Pointer to the next software timer */
UINT8 ucState; /* Software timer state */
UINT8 ucMode; /* Software timer mode */
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
UINT8 ucRouses; /* wake up enable */
UINT8 ucSensitive; /* align enable */
#endif
UINT32 usTimerID; /* Software timer ID */
UINT32 uwCount; /* Times that a software timer works */
UINT32 uwInterval; /* Timeout interval of a periodic software timer */
UINT32 uwArg; /* Parameter passed in when the callback function
that handles software timer timeout is called */
SWTMR_PROC_FUNC pfnHandler; /* Callback function that handles software timer timeout */
SortLinkList stSortList;
} SWTMR_CTRL_S;
/**
* @ingroup los_swtmr
* @brief Start a software timer.
*
* @par Description:
* This API is used to start a software timer that has a specified ID.
* @attention
* <ul>
* <li>The specific timer must be created first</li>
* </ul>
*
* @param swtmrID [IN] Software timer ID created by LOS_SwtmrCreate.
*
* @retval #LOS_ERRNO_SWTMR_ID_INVALID Invalid software timer ID.
* @retval #LOS_ERRNO_SWTMR_NOT_CREATED The software timer is not created.
* @retval #LOS_ERRNO_SWTMR_STATUS_INVALID Invalid software timer state.
* @retval #LOS_OK The software timer is successfully started.
* @par Dependency:
* <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
* @see LOS_SwtmrStop | LOS_SwtmrCreate
*/
extern UINT32 LOS_SwtmrStart(UINT32 swtmrID);
/**
* @ingroup los_swtmr
* @brief Stop a software timer.
*
* @par Description:
* This API is used to stop a software timer that has a specified ID.
* @attention
* <ul>
* <li>The specific timer should be created and started firstly.</li>
* </ul>
*
* @param swtmrID [IN] Software timer ID created by LOS_SwtmrCreate.
*
* @retval #LOS_ERRNO_SWTMR_ID_INVALID Invalid software timer ID.
* @retval #LOS_ERRNO_SWTMR_NOT_CREATED The software timer is not created.
* @retval #LOS_ERRNO_SWTMR_NOT_STARTED The software timer is not started.
* @retval #LOS_ERRNO_SWTMR_STATUS_INVALID Invalid software timer state.
* @retval #LOS_OK The software timer is successfully stopped.
* @par Dependency:
* <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
* @see LOS_SwtmrStart | LOS_SwtmrCreate
*/
extern UINT32 LOS_SwtmrStop(UINT32 swtmrID);
/**
* @ingroup los_swtmr
* @brief Obtain the number of remaining Ticks configured on a software timer.
*
* @par Description:
* This API is used to obtain the number of remaining Ticks configured on the software timer of which the ID is
* specified by usSwTmrID.
* @attention
* <ul>
* <li>The specific timer should be created and started successfully, error happends otherwise.</li>
* </ul>
*
* @param swtmrID [IN] Software timer ID created by LOS_SwtmrCreate.
* @param tick [OUT] Number of remaining Ticks configured on the software timer.
*
* @retval #LOS_ERRNO_SWTMR_ID_INVALID Invalid software timer ID.
* @retval #LOS_ERRNO_SWTMR_NOT_CREATED The software timer is not created.
* @retval #LOS_ERRNO_SWTMR_NOT_STARTED The software timer is not started.
* @retval #LOS_ERRNO_SWTMR_STATUS_INVALID Invalid software timer state.
* @retval #LOS_OK The number of remaining Ticks is successfully obtained.
* @par Dependency:
* <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
* @see LOS_SwtmrCreate
*/
extern UINT32 LOS_SwtmrTimeGet(UINT32 swtmrID, UINT32 *tick);
/**
* @ingroup los_swtmr
* @brief Create a software timer.
*
* @par Description:
* This API is used to create a software timer that has specified timing duration, timeout handling function,
* and trigger mode, and to return a handle by which the software timer can be referenced.
* @attention
* <ul>
* <li>Do not use the delay interface in the callback function that handles software timer timeout.</li>
* <li>There are LOSCFG_BASE_CORE_SWTMR_LIMIT timers available, change it's value when necessary.</li>
* </ul>
*
* @param interval [IN] Timing duration of the software timer to be created (unit: ms).
* @param mode [IN] Software timer mode. Pass in one of the modes specified by EnSwTmrType. There are three
* types of modes, one-off, periodic, and continuously periodic after one-off, of which the third mode is not
* supported temporarily.
* @param handler [IN] Callback function that handles software timer timeout.
* @param swtmrID [OUT] Software timer ID created by LOS_SwtmrCreate.
* @param arg [IN] Parameter passed in when the callback function that handles software timer timeout is
* called.
*
* @retval #LOS_ERRNO_SWTMR_INTERVAL_NOT_SUITED The software timer timeout interval is 0.
* @retval #LOS_ERRNO_SWTMR_MODE_INVALID Invalid software timer mode.
* @retval #LOS_ERRNO_SWTMR_PTR_NULL The callback function that handles software timer timeout is NULL.
* @retval #LOS_ERRNO_SWTMR_RET_PTR_NULL The passed-in software timer ID is NULL.
* @retval #LOS_ERRNO_SWTMR_MAXSIZE The number of software timers exceeds the configured permitted
* maximum number.
* @retval #LOS_OK The software timer is successfully created.
* @par Dependency:
* <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
* @see LOS_SwtmrDelete
*/
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
extern UINT32 LOS_SwtmrCreate(UINT32 interval,
UINT8 mode,
SWTMR_PROC_FUNC handler,
UINT32 *swtmrID,
UINT32 arg,
UINT8 rouses,
UINT8 sensitive);
#else
extern UINT32 LOS_SwtmrCreate(UINT32 interval,
UINT8 mode,
SWTMR_PROC_FUNC handler,
UINT32 *swtmrID,
UINT32 arg);
#endif
/**
* @ingroup los_swtmr
* @brief Delete a software timer.
*
* @par Description:
* This API is used to delete a software timer.
* @attention
* <ul>
* <li>The specific timer should be created and then stopped firstly.</li>
* </ul>
*
* @param swtmrID [IN] Software timer ID created by LOS_SwtmrCreate.
*
* @retval #LOS_ERRNO_SWTMR_ID_INVALID Invalid software timer ID.
* @retval #LOS_ERRNO_SWTMR_NOT_CREATED The software timer is not created.
* @retval #LOS_ERRNO_SWTMR_STATUS_INVALID Invalid software timer state.
* @retval #LOS_OK The software timer is successfully deleted.
* @par Dependency:
* <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
* @see LOS_SwtmrCreate
*/
extern UINT32 LOS_SwtmrDelete(UINT32 swtmrID);
/**
* @ingroup los_swtmr
* Software timer state
*/
enum SwtmrState {
OS_SWTMR_STATUS_UNUSED, /**< The software timer is not used. */
OS_SWTMR_STATUS_CREATED, /**< The software timer is created. */
OS_SWTMR_STATUS_TICKING /**< The software timer is timing. */
};
/**
* @ingroup los_swtmr
* Structure of the callback function that handles software timer timeout
*/
typedef struct {
SWTMR_PROC_FUNC handler; /**< Callback function that handles software timer timeout */
UINT32 arg; /**< Parameter passed in when the callback function
that handles software timer timeout is called */
} SwtmrHandlerItem;
extern SWTMR_CTRL_S *g_swtmrCBArray;
#define OS_SWT_FROM_SID(swtmrId) ((SWTMR_CTRL_S *)g_swtmrCBArray + ((swtmrId) % LOSCFG_BASE_CORE_SWTMR_LIMIT))
/**
* @ingroup los_swtmr
* @brief Initialization software timer.
*
* @par Description:
* <ul>
* <li>This API is used to initialization software.</li>
* </ul>
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param None.
*
* @retval None.
* @par Dependency:
* <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 OsSwtmrInit(VOID);
/**
* @ingroup los_swtmr
* @brief Get next timeout.
*
* @par Description:
* <ul>
* <li>This API is used to get next timeout.</li>
* </ul>
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param None.
*
* @retval None.
* @par Dependency:
* <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 OsSwtmrGetNextTimeout(VOID);
/**
* @ingroup los_swtmr
* @brief Adjust software timer list.
*
* @par Description:
* <ul>
* <li>This API is used to adjust software timer list.</li>
* </ul>
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param sleepTime [IN] UINT32 Sleep time.
*
* @retval UINT32 Sleep time.
* @par Dependency:
* <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern VOID OsSwtmrAdjust(UINT32 sleepTime);
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_SWTMR_H */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,472 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup los_tick
* @ingroup kernel
*/
#ifndef _LOS_TICK_H
#define _LOS_TICK_H
#include "los_error.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
/**
* @ingroup los_tick
* Tick error code: The Tick configuration is incorrect.
*
* Value: 0x02000400
*
* Solution: Change values of the OS_SYS_CLOCK and LOSCFG_BASE_CORE_TICK_PER_SECOND
* system time configuration modules in Los_config.h.
*/
#define LOS_ERRNO_TICK_CFG_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_TICK, 0x00)
/**
* @ingroup los_tick
* Tick error code: This error code is not in use temporarily.
*
* Value: 0x02000401
*
* Solution: None.
*/
#define LOS_ERRNO_TICK_NO_HWTIMER LOS_ERRNO_OS_ERROR(LOS_MOD_TICK, 0x01)
/**
* @ingroup los_tick
* Tick error code: The number of Ticks is too small.
*
* Value: 0x02000402
*
* Solution: Change values of the OS_SYS_CLOCK and LOSCFG_BASE_CORE_TICK_PER_SECOND
* system time configuration modules according to the SysTick_Config function.
*/
#define LOS_ERRNO_TICK_PER_SEC_TOO_SMALL LOS_ERRNO_OS_ERROR(LOS_MOD_TICK, 0x02)
/**
* @ingroup los_tick
* @brief: System timer cycles get function.
*
* @par Description:
* This API is used to get system timer cycles.
*
* @attention:
* <ul><li>None.</li></ul>
*
* @param: None.
*
* @retval: current system cycles.
*
* @par Dependency:
* <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
* @see None.
*
* */
extern UINT64 LOS_SysCycleGet(VOID);
/**
* @ingroup los_tick
* Number of milliseconds in one second.
*/
#define OS_SYS_MS_PER_SECOND 1000
/**
* @ingroup los_tick
* Number of microseconds in one second.
*/
#define OS_SYS_US_PER_SECOND 1000000
#define OS_SYS_NS_PER_SECOND 1000000000
#define OS_SYS_NS_PER_US 1000
#define OS_CYCLE_PER_TICK (OS_SYS_CLOCK / LOSCFG_BASE_CORE_TICK_PER_SECOND)
#define OS_NS_PER_CYCLE (OS_SYS_NS_PER_SECOND / OS_SYS_CLOCK)
#define OS_MS_PER_TICK (OS_SYS_MS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND)
#define OS_US_PER_TICK (OS_SYS_US_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND)
#define OS_NS_PER_TICK (OS_SYS_NS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND)
#define OS_SYS_CYCLE_TO_NS(cycle, freq) (((cycle) / (freq)) * OS_SYS_NS_PER_SECOND + \
((cycle) % OS_SYS_CLOCK) * OS_SYS_NS_PER_SECOND / (freq))
#define OS_SYS_NS_TO_CYCLE(time, freq) (((time) / OS_SYS_NS_PER_SECOND) * (freq) + \
(time % OS_SYS_NS_PER_SECOND) * (freq) / OS_SYS_NS_PER_SECOND)
/**
* @ingroup los_tick
* System time basic function error code: Null pointer.
*
* Value: 0x02000010
*
* Solution: Check whether the input parameter is null.
*/
#define LOS_ERRNO_SYS_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x10)
/**
* @ingroup los_tick
* System time basic function error code: Invalid system clock configuration.
*
* Value: 0x02000011
*
* Solution: Configure a valid system clock in los_config.h.
*/
#define LOS_ERRNO_SYS_CLOCK_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x11)
/**
* @ingroup los_tick
* System time basic function error code: This error code is not in use temporarily.
*
* Value: 0x02000012
*
* Solution: None.
*/
#define LOS_ERRNO_SYS_MAXNUMOFCORES_IS_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x12)
/**
* @ingroup los_tick
* System time error code: This error code is not in use temporarily.
*
* Value: 0x02000013
*
* Solution: None.
*/
#define LOS_ERRNO_SYS_PERIERRCOREID_IS_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x13)
/**
* @ingroup los_tick
* System time error code: This error code is not in use temporarily.
*
* Value: 0x02000014
*
* Solution: None.
*/
#define LOS_ERRNO_SYS_HOOK_IS_FULL LOS_ERRNO_OS_ERROR(LOS_MOD_SYS, 0x14)
/**
* @ingroup los_tick
* system time structure.
*/
typedef struct TagSysTime {
UINT16 uwYear; /**< value 1970 ~ 2038 or 1970 ~ 2100 */
UINT8 ucMonth; /**< value 1 - 12 */
UINT8 ucDay; /**< value 1 - 31 */
UINT8 ucHour; /**< value 0 - 23 */
UINT8 ucMinute; /**< value 0 - 59 */
UINT8 ucSecond; /**< value 0 - 59 */
UINT8 ucWeek; /**< value 0 - 6 */
} SYS_TIME_S;
/**
* @ingroup los_tick
* @brief Obtain the number of Ticks.
*
* @par Description:
* This API is used to obtain the number of Ticks.
* @attention
* <ul>
* <li>None</li>
* </ul>
*
* @param None
*
* @retval UINT64 The number of Ticks.
* @par Dependency:
* <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
* @see None
*/
extern UINT64 LOS_TickCountGet(VOID);
/**
* @ingroup los_tick
* @brief Obtain the number of cycles in one second.
*
* @par Description:
* This API is used to obtain the number of cycles in one second.
* @attention
* <ul>
* <li>None</li>
* </ul>
*
* @param None
*
* @retval UINT32 Number of cycles obtained in one second.
* @par Dependency:
* <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
* @see None
*/
extern UINT32 LOS_CyclePerTickGet(VOID);
/**
* @ingroup los_tick
* @brief Convert Ticks to milliseconds.
*
* @par Description:
* This API is used to convert Ticks to milliseconds.
* @attention
* <ul>
* <li>The number of milliseconds obtained through the conversion is 32-bit.</li>
* </ul>
*
* @param ticks [IN] Number of Ticks. The value range is (0,OS_SYS_CLOCK).
*
* @retval UINT32 Number of milliseconds obtained through the conversion. Ticks are successfully converted to
* milliseconds.
* @par Dependency:
* <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
* @see LOS_MS2Tick
*/
extern UINT32 LOS_Tick2MS(UINT32 ticks);
/**
* @ingroup los_tick
* @brief Convert milliseconds to Ticks.
*
* @par Description:
* This API is used to convert milliseconds to Ticks.
* @attention
* <ul>
* <li>If the parameter passed in is equal to 0xFFFFFFFF, the retval is 0xFFFFFFFF. Pay attention to the value to be
* converted because data possibly overflows.</li>
* </ul>
*
* @param millisec [IN] Number of milliseconds.
*
* @retval UINT32 Number of Ticks obtained through the conversion.
* @par Dependency:
* <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
* @see LOS_Tick2MS
*/
extern UINT32 LOS_MS2Tick(UINT32 millisec);
/**
* @ingroup los_tick
* Ticks per second
*/
extern UINT32 g_ticksPerSec;
/**
* @ingroup los_tick
* Cycles per Second
*/
extern UINT32 g_uwCyclePerSec;
/**
* @ingroup los_tick
* Cycles per Tick
*/
extern UINT32 g_cyclesPerTick;
/**
* @ingroup los_tick
* System Clock
*/
extern UINT32 g_sysClock;
/**
* @ingroup los_tick
* @brief Handle the system tick timeout.
*
* @par Description:
* This API is called when the system tick timeout and triggers the interrupt.
*
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param none.
*
* @retval None.
* @par Dependency:
* <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern VOID OsTickHandler(VOID);
/**
* @ingroup los_tick
* Define the CPU Tick structure.
*/
typedef struct TagCpuTick {
UINT32 cntHi; /* < Upper 32 bits of the tick value */
UINT32 cntLo; /* < Lower 32 bits of the tick value */
} CpuTick;
/**
* @ingroup los_tick
* Number of operable bits of a 32-bit operand
*/
#define OS_SYS_MV_32_BIT 32
/**
* @ingroup los_tick
* Number of milliseconds in one second.
*/
#define OS_SYS_MS_PER_SECOND 1000
/**
* @ingroup los_tick
* Number of microseconds in one second.
*/
#define OS_SYS_US_PER_SECOND 1000000
/**
* @ingroup los_tick
* The maximum length of name.
*/
#define OS_SYS_APPVER_NAME_MAX 64
/**
* @ingroup los_tick
* The magic word.
*/
#define OS_SYS_MAGIC_WORD 0xAAAAAAAA
/**
* @ingroup los_tick
* The initialization value of stack space.
*/
#define OS_SYS_EMPTY_STACK 0xCACACACA
/**
* @ingroup los_tick
* @brief Convert cycles to milliseconds.
*
* @par Description:
* This API is used to convert cycles to milliseconds.
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param cpuTick [IN] Number of CPU cycles.
* @param msHi [OUT] Upper 32 bits of the number of milliseconds.
* @param msLo [OUT] Lower 32 bits of the number of milliseconds.
*
* @retval #LOS_ERRNO_SYS_PTR_NULL 0x02000011: Invalid parameter.
* @retval #LOS_OK 0: Cycles are successfully converted to microseconds.
* @par Dependency:
* <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 OsCpuTick2MS(CpuTick *cpuTick, UINT32 *msHi, UINT32 *msLo);
/**
* @ingroup los_tick
* @brief Convert cycles to microseconds.
*
* @par Description:
* This API is used to convert cycles to microseconds.
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param cpuTick [IN] Number of CPU cycles.
* @param usHi [OUT] Upper 32 bits of the number of microseconds.
* @param usLo [OUT] Lower 32 bits of the number of microseconds.
*
* @retval #LOS_ERRNO_SYS_PTR_NULL 0x02000011: Invalid parameter.
* @retval #LOS_OK 0: Cycles are successfully converted to microseconds.
* @par Dependency:
* <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
extern UINT32 OsCpuTick2US(CpuTick *cpuTick, UINT32 *usHi, UINT32 *usLo);
/**
* @ingroup los_tick
* @brief Convert cycles to milliseconds.
*
* @par Description:
* This API is used to convert cycles to milliseconds.
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param cycle [IN] Number of cycles.
*
* @retval Number of milliseconds obtained through the conversion. Cycles are successfully converted to milliseconds.
* @par Dependency:
* <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
STATIC_INLINE UINT64 OsCycle2MS(UINT64 cycle)
{
return (UINT64)((cycle / (g_sysClock / OS_SYS_MS_PER_SECOND)));
}
/**
* @ingroup los_tick
* @brief Convert cycles to microseconds.
*
* @par Description:
* This API is used to convert cycles to microseconds.
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @param cycle [IN] Number of cycles.
*
* @retval Number of microseconds obtained through the conversion. Cycles are successfully converted to microseconds.
* @par Dependency:
* <ul><li>los_tick.h: the header file that contains the API declaration.</li></ul>
* @see None.
*/
STATIC_INLINE UINT64 OsCycle2US(UINT64 cycle)
{
UINT64 tmp = g_sysClock / OS_SYS_US_PER_SECOND;
if (tmp == 0) {
return 0;
}
return (UINT64)(cycle / tmp);
}
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_TICK_H */
@@ -0,0 +1,220 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "los_event.h"
#include "los_hook.h"
#include "los_interrupt.h"
#include "los_task.h"
#include "los_sched.h"
LITE_OS_SEC_TEXT_INIT UINT32 LOS_EventInit(PEVENT_CB_S eventCB)
{
if (eventCB == NULL) {
return LOS_ERRNO_EVENT_PTR_NULL;
}
eventCB->uwEventID = 0;
LOS_ListInit(&eventCB->stEventList);
OsHookCall(LOS_HOOK_TYPE_EVENT_INIT, eventCB);
return LOS_OK;
}
LITE_OS_SEC_TEXT UINT32 LOS_EventPoll(UINT32 *eventID, UINT32 eventMask, UINT32 mode)
{
UINT32 ret = 0;
UINT32 intSave;
if (eventID == NULL) {
return LOS_ERRNO_EVENT_PTR_NULL;
}
intSave = LOS_IntLock();
if (mode & LOS_WAITMODE_OR) {
if ((*eventID & eventMask) != 0) {
ret = *eventID & eventMask;
}
} else {
if ((eventMask != 0) && (eventMask == (*eventID & eventMask))) {
ret = *eventID & eventMask;
}
}
if (ret && (mode & LOS_WAITMODE_CLR)) {
*eventID = *eventID & ~(ret);
}
LOS_IntRestore(intSave);
return ret;
}
LITE_OS_SEC_TEXT STATIC_INLINE UINT32 OsEventReadParamCheck(PEVENT_CB_S eventCB, UINT32 eventMask, UINT32 mode)
{
if (eventCB == NULL) {
return LOS_ERRNO_EVENT_PTR_NULL;
}
if ((eventCB->stEventList.pstNext == NULL) || (eventCB->stEventList.pstPrev == NULL)) {
return LOS_ERRNO_EVENT_NOT_INITIALIZED;
}
if (eventMask == 0) {
return LOS_ERRNO_EVENT_EVENTMASK_INVALID;
}
if (eventMask & LOS_ERRTYPE_ERROR) {
return LOS_ERRNO_EVENT_SETBIT_INVALID;
}
if (((mode & LOS_WAITMODE_OR) && (mode & LOS_WAITMODE_AND)) ||
(mode & ~(LOS_WAITMODE_OR | LOS_WAITMODE_AND | LOS_WAITMODE_CLR)) ||
!(mode & (LOS_WAITMODE_OR | LOS_WAITMODE_AND))) {
return LOS_ERRNO_EVENT_FLAGS_INVALID;
}
return LOS_OK;
}
LITE_OS_SEC_TEXT UINT32 LOS_EventRead(PEVENT_CB_S eventCB, UINT32 eventMask, UINT32 mode, UINT32 timeOut)
{
UINT32 ret;
UINT32 intSave;
LosTaskCB *runTsk = NULL;
ret = OsEventReadParamCheck(eventCB, eventMask, mode);
if (ret != LOS_OK) {
return ret;
}
if (OS_INT_ACTIVE) {
return LOS_ERRNO_EVENT_READ_IN_INTERRUPT;
}
intSave = LOS_IntLock();
ret = LOS_EventPoll(&(eventCB->uwEventID), eventMask, mode);
OsHookCall(LOS_HOOK_TYPE_EVENT_READ, eventCB, eventMask, mode);
if (ret == 0) {
if (timeOut == 0) {
LOS_IntRestore(intSave);
return ret;
}
if (g_losTaskLock) {
LOS_IntRestore(intSave);
return LOS_ERRNO_EVENT_READ_IN_LOCK;
}
runTsk = g_losTask.runTask;
runTsk->eventMask = eventMask;
runTsk->eventMode = mode;
OsSchedTaskWait(&eventCB->stEventList, timeOut);
LOS_IntRestore(intSave);
LOS_Schedule();
intSave = LOS_IntLock();
if (runTsk->taskStatus & OS_TASK_STATUS_TIMEOUT) {
runTsk->taskStatus &= ~OS_TASK_STATUS_TIMEOUT;
LOS_IntRestore(intSave);
return LOS_ERRNO_EVENT_READ_TIMEOUT;
}
ret = LOS_EventPoll(&eventCB->uwEventID, eventMask, mode);
}
LOS_IntRestore(intSave);
return ret;
}
LITE_OS_SEC_TEXT UINT32 LOS_EventWrite(PEVENT_CB_S eventCB, UINT32 events)
{
LosTaskCB *resumedTask = NULL;
LosTaskCB *nextTask = (LosTaskCB *)NULL;
UINT32 intSave;
UINT8 exitFlag = 0;
if (eventCB == NULL) {
return LOS_ERRNO_EVENT_PTR_NULL;
}
if ((eventCB->stEventList.pstNext == NULL) || (eventCB->stEventList.pstPrev == NULL)) {
return LOS_ERRNO_EVENT_NOT_INITIALIZED;
}
if (events & LOS_ERRTYPE_ERROR) {
return LOS_ERRNO_EVENT_SETBIT_INVALID;
}
intSave = LOS_IntLock();
eventCB->uwEventID |= events;
OsHookCall(LOS_HOOK_TYPE_EVENT_WRITE, eventCB);
if (!LOS_ListEmpty(&eventCB->stEventList)) {
for (resumedTask = LOS_DL_LIST_ENTRY((&eventCB->stEventList)->pstNext, LosTaskCB, pendList);
&resumedTask->pendList != (&eventCB->stEventList);) {
nextTask = LOS_DL_LIST_ENTRY(resumedTask->pendList.pstNext, LosTaskCB, pendList);
if (((resumedTask->eventMode & LOS_WAITMODE_OR) && (resumedTask->eventMask & events) != 0) ||
((resumedTask->eventMode & LOS_WAITMODE_AND) &&
((resumedTask->eventMask & eventCB->uwEventID) == resumedTask->eventMask))) {
exitFlag = 1;
OsSchedTaskWake(resumedTask);
}
resumedTask = nextTask;
}
if (exitFlag == 1) {
LOS_IntRestore(intSave);
LOS_Schedule();
return LOS_OK;
}
}
LOS_IntRestore(intSave);
return LOS_OK;
}
LITE_OS_SEC_TEXT_INIT UINT32 LOS_EventDestroy(PEVENT_CB_S eventCB)
{
UINT32 intSave;
if (eventCB == NULL) {
return LOS_ERRNO_EVENT_PTR_NULL;
}
intSave = LOS_IntLock();
if (!LOS_ListEmpty(&eventCB->stEventList)) {
LOS_IntRestore(intSave);
return LOS_ERRNO_EVENT_SHOULD_NOT_DESTROYED;
}
eventCB->stEventList.pstNext = (LOS_DL_LIST *)NULL;
eventCB->stEventList.pstPrev = (LOS_DL_LIST *)NULL;
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_EVENT_DESTROY, eventCB);
return LOS_OK;
}
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_EventClear(PEVENT_CB_S eventCB, UINT32 eventMask)
{
UINT32 intSave;
if (eventCB == NULL) {
return LOS_ERRNO_EVENT_PTR_NULL;
}
intSave = LOS_IntLock();
eventCB->uwEventID &= eventMask;
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_EVENT_CLEAR, eventCB);
return LOS_OK;
}
@@ -0,0 +1,219 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stdarg.h"
#include "los_arch.h"
#include "los_config.h"
#include "los_debug.h"
#include "los_memory.h"
#include "los_mux.h"
#include "los_queue.h"
#include "los_sem.h"
#if (LOSCFG_PLATFORM_HWI == 1)
#include "los_interrupt.h"
#endif
#if (LOSCFG_KERNEL_TRACE == 1)
#include "los_debug.h"
#endif
#if (LOSCFG_BASE_CORE_SWTMR == 1)
#include "los_swtmr.h"
#endif
#if (LOSCFG_BASE_CORE_CPUP == 1)
#include "los_cpup.h"
#endif
#if (LOSCFG_PLATFORM_EXC == 1)
#include "los_exc_info.h"
#endif
#if (LOSCFG_BACKTRACE_TYPE != 0)
#include "los_backtrace.h"
#endif
#if (LOSCFG_KERNEL_PM == 1)
#include "los_pm.h"
#endif
/*****************************************************************************
Function : LOS_Reboot
Description : system exception, die in here, wait for watchdog.
Input : None
Output : None
Return : None
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT VOID LOS_Reboot(VOID)
{
OsDoExcHook(EXC_REBOOT);
HalSysExit();
}
LITE_OS_SEC_TEXT_INIT VOID LOS_Panic(const CHAR *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
PRINT_ERR(fmt, ap);
va_end(ap);
HalSysExit();
}
/*****************************************************************************
Function : OsRegister
Description : Configuring the maximum number of tasks
Input : None
Output : None
Return : None
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT static VOID OsRegister(VOID)
{
g_taskMaxNum = LOSCFG_BASE_CORE_TSK_LIMIT + 1; /* Reserved 1 for IDLE */
return;
}
LITE_OS_SEC_TEXT_INIT UINT32 LOS_Start(VOID)
{
return HalStartSchedule(OsTickHandler);
}
/*****************************************************************************
Function : LOS_KernelInit
Description : System kernel initialization function, configure all system modules
Input : None
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 LOS_KernelInit(VOID)
{
UINT32 ret;
PRINTK("entering kernel init...\n");
#if (LOSCFG_BACKTRACE_TYPE != 0)
OSBackTraceInit();
#endif
OsRegister();
ret = OsMemSystemInit();
if (ret != LOS_OK) {
PRINT_ERR("OsMemSystemInit error %d\n", ret);
return ret;
}
HalArchInit();
ret = OsTaskInit();
if (ret != LOS_OK) {
PRINT_ERR("OsTaskInit error\n");
return ret;
}
#if (LOSCFG_BASE_CORE_TSK_MONITOR == 1)
OsTaskMonInit();
#endif
#if (LOSCFG_BASE_CORE_CPUP == 1)
ret = OsCpupInit();
if (ret != LOS_OK) {
PRINT_ERR("OsCpupInit error\n");
return ret;
}
#endif
#if (LOSCFG_BASE_IPC_SEM == 1)
ret = OsSemInit();
if (ret != LOS_OK) {
return ret;
}
#endif
#if (LOSCFG_BASE_IPC_MUX == 1)
ret = OsMuxInit();
if (ret != LOS_OK) {
return ret;
}
#endif
#if (LOSCFG_BASE_IPC_QUEUE == 1)
ret = OsQueueInit();
if (ret != LOS_OK) {
PRINT_ERR("OsQueueInit error\n");
return ret;
}
#endif
#if (LOSCFG_BASE_CORE_SWTMR == 1)
ret = OsSwtmrInit();
if (ret != LOS_OK) {
PRINT_ERR("OsSwtmrInit error\n");
return ret;
}
#endif
ret = OsIdleTaskCreate();
if (ret != LOS_OK) {
return ret;
}
#if (LOSCFG_KERNEL_TRACE == 1)
ret = LOS_TraceInit();
if (ret != LOS_OK) {
PRINT_ERR("LOS_TraceInit error\n");
return ret;
}
#endif
#if (LOSCFG_KERNEL_PM == 1)
ret = OsPmInit();
if (ret != LOS_OK) {
PRINT_ERR("Pm init failed!\n");
return ret;
}
#endif
#ifdef LOSCFG_TEST
//ret = los_TestInit();
//if (ret != LOS_OK) {
// PRINT_ERR("los_TestInit error\n");
// return ret;
//}
#endif
#if (LOSCFG_PLATFORM_EXC == 1)
OsExcMsgDumpInit();
#endif
return LOS_OK;
}
@@ -0,0 +1,313 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "los_mux.h"
#include "los_config.h"
#include "los_debug.h"
#include "los_hook.h"
#include "los_interrupt.h"
#include "los_memory.h"
#include "los_sched.h"
#if (LOSCFG_BASE_IPC_MUX == 1)
LITE_OS_SEC_BSS LosMuxCB* g_allMux = NULL;
LITE_OS_SEC_DATA_INIT LOS_DL_LIST g_unusedMuxList;
/*****************************************************************************
Function : OsMuxInit
Description : Initializes the mutex
Input : None
Output : None
Return : LOS_OK on success, or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 OsMuxInit(VOID)
{
LosMuxCB *muxNode = NULL;
UINT32 index;
LOS_ListInit(&g_unusedMuxList);
if (LOSCFG_BASE_IPC_MUX_LIMIT == 0) {
return LOS_ERRNO_MUX_MAXNUM_ZERO;
}
g_allMux = (LosMuxCB *)LOS_MemAlloc(m_aucSysMem0, (LOSCFG_BASE_IPC_MUX_LIMIT * sizeof(LosMuxCB)));
if (g_allMux == NULL) {
return LOS_ERRNO_MUX_NO_MEMORY;
}
for (index = 0; index < LOSCFG_BASE_IPC_MUX_LIMIT; index++) {
muxNode = ((LosMuxCB *)g_allMux) + index;
muxNode->muxID = index;
muxNode->muxStat = OS_MUX_UNUSED;
LOS_ListTailInsert(&g_unusedMuxList, &muxNode->muxList);
}
return LOS_OK;
}
/*****************************************************************************
Function : LOS_MuxCreate
Description : Create a mutex
Input : None
Output : muxHandle ------ Mutex operation handle
Return : LOS_OK on success, or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 LOS_MuxCreate(UINT32 *muxHandle)
{
UINT32 intSave;
LosMuxCB *muxCreated = NULL;
LOS_DL_LIST *unusedMux = NULL;
UINT32 errNo;
UINT32 errLine;
if (muxHandle == NULL) {
return LOS_ERRNO_MUX_PTR_NULL;
}
intSave = LOS_IntLock();
if (LOS_ListEmpty(&g_unusedMuxList)) {
LOS_IntRestore(intSave);
OS_GOTO_ERR_HANDLER(LOS_ERRNO_MUX_ALL_BUSY);
}
unusedMux = LOS_DL_LIST_FIRST(&(g_unusedMuxList));
LOS_ListDelete(unusedMux);
muxCreated = (GET_MUX_LIST(unusedMux));
muxCreated->muxCount = 0;
muxCreated->muxStat = OS_MUX_USED;
muxCreated->priority = 0;
muxCreated->owner = (LosTaskCB *)NULL;
LOS_ListInit(&muxCreated->muxList);
*muxHandle = (UINT32)muxCreated->muxID;
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_MUX_CREATE, muxCreated);
return LOS_OK;
ERR_HANDLER:
OS_RETURN_ERROR_P2(errLine, errNo);
}
/*****************************************************************************
Function : LOS_MuxDelete
Description : Delete a mutex
Input : muxHandle ------Mutex operation handle
Output : None
Return : LOS_OK on success, or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 LOS_MuxDelete(UINT32 muxHandle)
{
UINT32 intSave;
LosMuxCB *muxDeleted = NULL;
UINT32 errNo;
UINT32 errLine;
if (muxHandle >= (UINT32)LOSCFG_BASE_IPC_MUX_LIMIT) {
OS_GOTO_ERR_HANDLER(LOS_ERRNO_MUX_INVALID);
}
muxDeleted = GET_MUX(muxHandle);
intSave = LOS_IntLock();
if (muxDeleted->muxStat == OS_MUX_UNUSED) {
LOS_IntRestore(intSave);
OS_GOTO_ERR_HANDLER(LOS_ERRNO_MUX_INVALID);
}
if ((!LOS_ListEmpty(&muxDeleted->muxList)) || muxDeleted->muxCount) {
LOS_IntRestore(intSave);
OS_GOTO_ERR_HANDLER(LOS_ERRNO_MUX_PENDED);
}
LOS_ListAdd(&g_unusedMuxList, &muxDeleted->muxList);
muxDeleted->muxStat = OS_MUX_UNUSED;
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_MUX_DELETE, muxDeleted);
return LOS_OK;
ERR_HANDLER:
OS_RETURN_ERROR_P2(errLine, errNo);
}
STATIC_INLINE UINT32 OsMuxValidCheck(LosMuxCB *muxPended)
{
if (muxPended->muxStat == OS_MUX_UNUSED) {
return LOS_ERRNO_MUX_INVALID;
}
if (OS_INT_ACTIVE) {
return LOS_ERRNO_MUX_PEND_INTERR;
}
if (g_losTaskLock) {
PRINT_ERR("!!!LOS_ERRNO_MUX_PEND_IN_LOCK!!!\n");
return LOS_ERRNO_MUX_PEND_IN_LOCK;
}
return LOS_OK;
}
/*****************************************************************************
Function : LOS_MuxPend
Description : Specify the mutex P operation
Input : muxHandle ------ Mutex operation handleone
: timeOut ------- waiting time
Output : None
Return : LOS_OK on success, or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT UINT32 LOS_MuxPend(UINT32 muxHandle, UINT32 timeout)
{
UINT32 intSave;
LosMuxCB *muxPended = NULL;
UINT32 retErr;
LosTaskCB *runningTask = NULL;
if (muxHandle >= (UINT32)LOSCFG_BASE_IPC_MUX_LIMIT) {
OS_RETURN_ERROR(LOS_ERRNO_MUX_INVALID);
}
muxPended = GET_MUX(muxHandle);
intSave = LOS_IntLock();
retErr = OsMuxValidCheck(muxPended);
if (retErr) {
goto ERROR_MUX_PEND;
}
runningTask = (LosTaskCB *)g_losTask.runTask;
if (muxPended->muxCount == 0) {
muxPended->muxCount++;
muxPended->owner = runningTask;
muxPended->priority = runningTask->priority;
LOS_IntRestore(intSave);
goto HOOK;
}
if (muxPended->owner == runningTask) {
muxPended->muxCount++;
LOS_IntRestore(intSave);
goto HOOK;
}
if (!timeout) {
retErr = LOS_ERRNO_MUX_UNAVAILABLE;
goto ERROR_MUX_PEND;
}
runningTask->taskMux = (VOID *)muxPended;
if (muxPended->owner->priority > runningTask->priority) {
(VOID)OsSchedModifyTaskSchedParam(muxPended->owner, runningTask->priority);
}
OsSchedTaskWait(&muxPended->muxList, timeout);
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_MUX_PEND, muxPended);
LOS_Schedule();
intSave = LOS_IntLock();
if (runningTask->taskStatus & OS_TASK_STATUS_TIMEOUT) {
runningTask->taskStatus &= (~OS_TASK_STATUS_TIMEOUT);
retErr = LOS_ERRNO_MUX_TIMEOUT;
goto ERROR_MUX_PEND;
}
LOS_IntRestore(intSave);
return LOS_OK;
HOOK:
OsHookCall(LOS_HOOK_TYPE_MUX_PEND, muxPended);
return LOS_OK;
ERROR_MUX_PEND:
LOS_IntRestore(intSave);
OS_RETURN_ERROR(retErr);
}
/*****************************************************************************
Function : LOS_MuxPost
Description : Specify the mutex V operation,
Input : muxHandle ------ Mutex operation handle
Output : None
Return : LOS_OK on success, or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT UINT32 LOS_MuxPost(UINT32 muxHandle)
{
UINT32 intSave;
LosMuxCB *muxPosted = GET_MUX(muxHandle);
LosTaskCB *resumedTask = NULL;
LosTaskCB *runningTask = NULL;
intSave = LOS_IntLock();
if ((muxHandle >= (UINT32)LOSCFG_BASE_IPC_MUX_LIMIT) ||
(muxPosted->muxStat == OS_MUX_UNUSED)) {
LOS_IntRestore(intSave);
OS_RETURN_ERROR(LOS_ERRNO_MUX_INVALID);
}
runningTask = (LosTaskCB *)g_losTask.runTask;
if ((muxPosted->muxCount == 0) || (muxPosted->owner != runningTask)) {
LOS_IntRestore(intSave);
OS_RETURN_ERROR(LOS_ERRNO_MUX_INVALID);
}
if (--(muxPosted->muxCount) != 0) {
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_MUX_POST, muxPosted);
return LOS_OK;
}
if ((muxPosted->owner->priority) != muxPosted->priority) {
(VOID)OsSchedModifyTaskSchedParam(muxPosted->owner, muxPosted->priority);
}
if (!LOS_ListEmpty(&muxPosted->muxList)) {
resumedTask = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&(muxPosted->muxList)));
muxPosted->muxCount = 1;
muxPosted->owner = resumedTask;
muxPosted->priority = resumedTask->priority;
resumedTask->taskMux = NULL;
OsSchedTaskWake(resumedTask);
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_MUX_POST, muxPosted);
LOS_Schedule();
} else {
LOS_IntRestore(intSave);
}
return LOS_OK;
}
#endif /* (LOSCFG_BASE_IPC_MUX == 1) */
@@ -0,0 +1,671 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "los_queue.h"
#include "securec.h"
#include "los_config.h"
#include "los_debug.h"
#include "los_hook.h"
#include "los_interrupt.h"
#include "los_membox.h"
#include "los_memory.h"
#include "los_task.h"
#include "los_sched.h"
#if (LOSCFG_BASE_IPC_QUEUE == 1)
LITE_OS_SEC_BSS LosQueueCB *g_allQueue = NULL ;
LITE_OS_SEC_BSS LOS_DL_LIST g_freeQueueList;
/**************************************************************************
Function : OsQueueInit
Description : queue initial
Input : None
Output : None
Return : LOS_OK on success or error code on failure
**************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 OsQueueInit(VOID)
{
LosQueueCB *queueNode = NULL;
UINT16 index;
if (LOSCFG_BASE_IPC_QUEUE_LIMIT == 0) {
return LOS_ERRNO_QUEUE_MAXNUM_ZERO;
}
g_allQueue = (LosQueueCB *)LOS_MemAlloc(m_aucSysMem0, LOSCFG_BASE_IPC_QUEUE_LIMIT * sizeof(LosQueueCB));
if (g_allQueue == NULL) {
return LOS_ERRNO_QUEUE_NO_MEMORY;
}
(VOID)memset_s(g_allQueue, LOSCFG_BASE_IPC_QUEUE_LIMIT * sizeof(LosQueueCB),
0, LOSCFG_BASE_IPC_QUEUE_LIMIT * sizeof(LosQueueCB));
LOS_ListInit(&g_freeQueueList);
for (index = 0; index < LOSCFG_BASE_IPC_QUEUE_LIMIT; index++) {
queueNode = ((LosQueueCB *)g_allQueue) + index;
queueNode->queueID = index;
LOS_ListTailInsert(&g_freeQueueList, &queueNode->readWriteList[OS_QUEUE_WRITE]);
}
return LOS_OK;
}
/*****************************************************************************
Function : LOS_QueueCreate
Description : Create a queue
Input : queueName --- Queue name, less than 4 characters
: len --- Queue length
: flags --- Queue type, FIFO or PRIO
: maxMsgSize --- Maximum message size in byte
Output : queueID --- Queue ID
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 LOS_QueueCreate(CHAR *queueName,
UINT16 len,
UINT32 *queueID,
UINT32 flags,
UINT16 maxMsgSize)
{
LosQueueCB *queueCB = NULL;
UINT32 intSave;
LOS_DL_LIST *unusedQueue = NULL;
UINT8 *queue = NULL;
UINT16 msgSize;
(VOID)queueName;
(VOID)flags;
if (queueID == NULL) {
return LOS_ERRNO_QUEUE_CREAT_PTR_NULL;
}
if (maxMsgSize > (OS_NULL_SHORT - sizeof(UINT32))) {
return LOS_ERRNO_QUEUE_SIZE_TOO_BIG;
}
if ((len == 0) || (maxMsgSize == 0)) {
return LOS_ERRNO_QUEUE_PARA_ISZERO;
}
msgSize = maxMsgSize + sizeof(UINT32);
/* Memory allocation is time-consuming, to shorten the time of disable interrupt,
move the memory allocation to here. */
queue = (UINT8 *)LOS_MemAlloc(m_aucSysMem0, len * msgSize);
if (queue == NULL) {
return LOS_ERRNO_QUEUE_CREATE_NO_MEMORY;
}
intSave = LOS_IntLock();
if (LOS_ListEmpty(&g_freeQueueList)) {
LOS_IntRestore(intSave);
(VOID)LOS_MemFree(m_aucSysMem0, queue);
return LOS_ERRNO_QUEUE_CB_UNAVAILABLE;
}
unusedQueue = LOS_DL_LIST_FIRST(&(g_freeQueueList));
LOS_ListDelete(unusedQueue);
queueCB = (GET_QUEUE_LIST(unusedQueue));
queueCB->queueLen = len;
queueCB->queueSize = msgSize;
queueCB->queue = queue;
queueCB->queueState = OS_QUEUE_INUSED;
queueCB->readWriteableCnt[OS_QUEUE_READ] = 0;
queueCB->readWriteableCnt[OS_QUEUE_WRITE] = len;
queueCB->queueHead = 0;
queueCB->queueTail = 0;
LOS_ListInit(&queueCB->readWriteList[OS_QUEUE_READ]);
LOS_ListInit(&queueCB->readWriteList[OS_QUEUE_WRITE]);
LOS_ListInit(&queueCB->memList);
LOS_IntRestore(intSave);
*queueID = queueCB->queueID;
OsHookCall(LOS_HOOK_TYPE_QUEUE_CREATE, queueCB);
return LOS_OK;
}
static INLINE LITE_OS_SEC_TEXT UINT32 OsQueueReadParameterCheck(UINT32 queueID, VOID *bufferAddr,
UINT32 *bufferSize, UINT32 timeOut)
{
if (queueID >= LOSCFG_BASE_IPC_QUEUE_LIMIT) {
return LOS_ERRNO_QUEUE_INVALID;
}
if ((bufferAddr == NULL) || (bufferSize == NULL)) {
return LOS_ERRNO_QUEUE_READ_PTR_NULL;
}
if (*bufferSize == 0) {
return LOS_ERRNO_QUEUE_READSIZE_ISZERO;
}
if (timeOut != LOS_NO_WAIT) {
if (OS_INT_ACTIVE) {
return LOS_ERRNO_QUEUE_READ_IN_INTERRUPT;
}
}
return LOS_OK;
}
static INLINE LITE_OS_SEC_TEXT UINT32 OsQueueWriteParameterCheck(UINT32 queueID, VOID *bufferAddr,
UINT32 *bufferSize, UINT32 timeOut)
{
if (queueID >= LOSCFG_BASE_IPC_QUEUE_LIMIT) {
return LOS_ERRNO_QUEUE_INVALID;
}
if (bufferAddr == NULL) {
return LOS_ERRNO_QUEUE_WRITE_PTR_NULL;
}
if (*bufferSize == 0) {
return LOS_ERRNO_QUEUE_WRITESIZE_ISZERO;
}
if (timeOut != LOS_NO_WAIT) {
if (OS_INT_ACTIVE) {
return LOS_ERRNO_QUEUE_WRITE_IN_INTERRUPT;
}
}
return LOS_OK;
}
static INLINE VOID OsQueueBufferOperate(LosQueueCB *queueCB, UINT32 operateType,
VOID *bufferAddr, UINT32 *bufferSize)
{
UINT8 *queueNode = NULL;
UINT32 msgDataSize;
UINT16 queuePosition;
errno_t rc;
/* get the queue position */
switch (OS_QUEUE_OPERATE_GET(operateType)) {
case OS_QUEUE_READ_HEAD:
queuePosition = queueCB->queueHead;
((queueCB->queueHead + 1) == queueCB->queueLen) ? (queueCB->queueHead = 0) : (queueCB->queueHead++);
break;
case OS_QUEUE_WRITE_HEAD:
(queueCB->queueHead == 0) ? (queueCB->queueHead = (queueCB->queueLen - 1)) : (--queueCB->queueHead);
queuePosition = queueCB->queueHead;
break;
case OS_QUEUE_WRITE_TAIL:
queuePosition = queueCB->queueTail;
((queueCB->queueTail + 1) == queueCB->queueLen) ? (queueCB->queueTail = 0) : (queueCB->queueTail++);
break;
default:
PRINT_ERR("invalid queue operate type!\n");
return;
}
queueNode = &(queueCB->queue[(queuePosition * (queueCB->queueSize))]);
if (OS_QUEUE_IS_POINT(operateType)) {
if (OS_QUEUE_IS_READ(operateType)) {
*(UINT32 *)bufferAddr = *(UINT32 *)(VOID *)queueNode;
} else {
*(UINT32 *)(VOID *)queueNode = *(UINT32 *)bufferAddr; // change to pp when calling OsQueueOperate
}
} else {
if (OS_QUEUE_IS_READ(operateType)) {
msgDataSize = *((UINT32 *)(UINTPTR)((queueNode + queueCB->queueSize) - sizeof(UINT32)));
rc = memcpy_s((VOID *)bufferAddr, *bufferSize, (VOID *)queueNode, msgDataSize);
if (rc != EOK) {
PRINT_ERR("%s[%d] memcpy failed, error type = %u\n", __FUNCTION__, __LINE__, rc);
return;
}
*bufferSize = msgDataSize;
} else {
*((UINT32 *)(UINTPTR)((queueNode + queueCB->queueSize) - sizeof(UINT32))) = *bufferSize;
rc = memcpy_s((VOID *)queueNode, queueCB->queueSize, (VOID *)bufferAddr, *bufferSize);
if (rc != EOK) {
PRINT_ERR("%s[%d] memcpy failed, error type = %u\n", __FUNCTION__, __LINE__, rc);
return;
}
}
}
}
static INLINE UINT32 OsQueueOperateParamCheck(const LosQueueCB *queueCB, UINT32 operateType, const UINT32 *bufferSize)
{
if (queueCB->queueState == OS_QUEUE_UNUSED) {
return LOS_ERRNO_QUEUE_NOT_CREATE;
}
if (OS_QUEUE_IS_READ(operateType) && (*bufferSize < (queueCB->queueSize - sizeof(UINT32)))) {
return LOS_ERRNO_QUEUE_READ_SIZE_TOO_SMALL;
} else if (OS_QUEUE_IS_WRITE(operateType) && (*bufferSize > (queueCB->queueSize - sizeof(UINT32)))) {
return LOS_ERRNO_QUEUE_WRITE_SIZE_TOO_BIG;
}
return LOS_OK;
}
UINT32 OsQueueOperate(UINT32 queueID, UINT32 operateType, VOID *bufferAddr, UINT32 *bufferSize, UINT32 timeOut)
{
LosQueueCB *queueCB = NULL;
LosTaskCB *resumedTask = NULL;
UINT32 ret;
UINT32 readWrite = OS_QUEUE_READ_WRITE_GET(operateType);
UINT32 readWriteTmp = !readWrite;
UINT32 intSave = LOS_IntLock();
queueCB = (LosQueueCB *)GET_QUEUE_HANDLE(queueID);
ret = OsQueueOperateParamCheck(queueCB, operateType, bufferSize);
if (ret != LOS_OK) {
goto QUEUE_END;
}
if (queueCB->readWriteableCnt[readWrite] == 0) {
if (timeOut == LOS_NO_WAIT) {
ret = OS_QUEUE_IS_READ(operateType) ? LOS_ERRNO_QUEUE_ISEMPTY : LOS_ERRNO_QUEUE_ISFULL;
goto QUEUE_END;
}
if (g_losTaskLock) {
ret = LOS_ERRNO_QUEUE_PEND_IN_LOCK;
goto QUEUE_END;
}
LosTaskCB *runTsk = (LosTaskCB *)g_losTask.runTask;
OsSchedTaskWait(&queueCB->readWriteList[readWrite], timeOut);
LOS_IntRestore(intSave);
LOS_Schedule();
intSave = LOS_IntLock();
if (runTsk->taskStatus & OS_TASK_STATUS_TIMEOUT) {
runTsk->taskStatus &= ~OS_TASK_STATUS_TIMEOUT;
ret = LOS_ERRNO_QUEUE_TIMEOUT;
goto QUEUE_END;
}
} else {
queueCB->readWriteableCnt[readWrite]--;
}
OsQueueBufferOperate(queueCB, operateType, bufferAddr, bufferSize);
if (!LOS_ListEmpty(&queueCB->readWriteList[readWriteTmp])) {
resumedTask = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&queueCB->readWriteList[readWriteTmp]));
OsSchedTaskWake(resumedTask);
LOS_IntRestore(intSave);
LOS_Schedule();
return LOS_OK;
} else {
queueCB->readWriteableCnt[readWriteTmp]++;
}
QUEUE_END:
LOS_IntRestore(intSave);
return ret;
}
LITE_OS_SEC_TEXT UINT32 LOS_QueueReadCopy(UINT32 queueID,
VOID *bufferAddr,
UINT32 *bufferSize,
UINT32 timeOut)
{
UINT32 ret;
UINT32 operateType;
ret = OsQueueReadParameterCheck(queueID, bufferAddr, bufferSize, timeOut);
if (ret != LOS_OK) {
return ret;
}
operateType = OS_QUEUE_OPERATE_TYPE(OS_QUEUE_READ, OS_QUEUE_HEAD, OS_QUEUE_NOT_POINT);
return OsQueueOperate(queueID, operateType, bufferAddr, bufferSize, timeOut);
}
LITE_OS_SEC_TEXT UINT32 LOS_QueueWriteHeadCopy(UINT32 queueID,
VOID *bufferAddr,
UINT32 bufferSize,
UINT32 timeOut)
{
UINT32 ret;
UINT32 operateType;
ret = OsQueueWriteParameterCheck(queueID, bufferAddr, &bufferSize, timeOut);
if (ret != LOS_OK) {
return ret;
}
operateType = OS_QUEUE_OPERATE_TYPE(OS_QUEUE_WRITE, OS_QUEUE_HEAD, OS_QUEUE_NOT_POINT);
return OsQueueOperate(queueID, operateType, bufferAddr, &bufferSize, timeOut);
}
LITE_OS_SEC_TEXT UINT32 LOS_QueueWriteCopy(UINT32 queueID,
VOID *bufferAddr,
UINT32 bufferSize,
UINT32 timeOut)
{
UINT32 ret;
UINT32 operateType;
ret = OsQueueWriteParameterCheck(queueID, bufferAddr, &bufferSize, timeOut);
if (ret != LOS_OK) {
return ret;
}
operateType = OS_QUEUE_OPERATE_TYPE(OS_QUEUE_WRITE, OS_QUEUE_TAIL, OS_QUEUE_NOT_POINT);
return OsQueueOperate(queueID, operateType, bufferAddr, &bufferSize, timeOut);
}
LITE_OS_SEC_TEXT UINT32 LOS_QueueRead(UINT32 queueID, VOID *bufferAddr, UINT32 bufferSize, UINT32 timeOut)
{
UINT32 ret;
UINT32 operateType;
ret = OsQueueReadParameterCheck(queueID, bufferAddr, &bufferSize, timeOut);
if (ret != LOS_OK) {
return ret;
}
operateType = OS_QUEUE_OPERATE_TYPE(OS_QUEUE_READ, OS_QUEUE_HEAD, OS_QUEUE_POINT);
OsHookCall(LOS_HOOK_TYPE_QUEUE_READ, (LosQueueCB *)GET_QUEUE_HANDLE(queueID));
return OsQueueOperate(queueID, operateType, bufferAddr, &bufferSize, timeOut);
}
LITE_OS_SEC_TEXT UINT32 LOS_QueueWrite(UINT32 queueID, VOID *bufferAddr, UINT32 bufferSize, UINT32 timeOut)
{
UINT32 ret;
UINT32 operateType;
UINT32 size = sizeof(UINT32 *);
(VOID)bufferSize;
ret = OsQueueWriteParameterCheck(queueID, bufferAddr, &size, timeOut);
if (ret != LOS_OK) {
return ret;
}
operateType = OS_QUEUE_OPERATE_TYPE(OS_QUEUE_WRITE, OS_QUEUE_TAIL, OS_QUEUE_POINT);
OsHookCall(LOS_HOOK_TYPE_QUEUE_WRITE, (LosQueueCB *)GET_QUEUE_HANDLE(queueID));
return OsQueueOperate(queueID, operateType, &bufferAddr, &size, timeOut);
}
LITE_OS_SEC_TEXT UINT32 LOS_QueueWriteHead(UINT32 queueID,
VOID *bufferAddr,
UINT32 bufferSize,
UINT32 timeOut)
{
UINT32 size = sizeof(UINT32 *);
(VOID)bufferSize;
if (bufferAddr == NULL) {
return LOS_ERRNO_QUEUE_WRITE_PTR_NULL;
}
return LOS_QueueWriteHeadCopy(queueID, &bufferAddr, size, timeOut);
}
/*****************************************************************************
Function : OsQueueMailAlloc
Description : Mail allocate memory
Input : queueID --- QueueID
: mailPool --- MailPool
: timeOut --- TimeOut
Output : None
Return : mem:pointer if success otherwise NULL
*****************************************************************************/
LITE_OS_SEC_TEXT VOID *OsQueueMailAlloc(UINT32 queueID, VOID *mailPool, UINT32 timeOut)
{
VOID *mem = (VOID *)NULL;
UINT32 intSave;
LosQueueCB *queueCB = (LosQueueCB *)NULL;
LosTaskCB *runTsk = (LosTaskCB *)NULL;
if (queueID >= LOSCFG_BASE_IPC_QUEUE_LIMIT) {
return NULL;
}
if (mailPool == NULL) {
return NULL;
}
if (timeOut != LOS_NO_WAIT) {
if (OS_INT_ACTIVE) {
return NULL;
}
}
intSave = LOS_IntLock();
queueCB = GET_QUEUE_HANDLE(queueID);
if (queueCB->queueState == OS_QUEUE_UNUSED) {
goto END;
}
mem = LOS_MemboxAlloc(mailPool);
if (mem == NULL) {
if (timeOut == LOS_NO_WAIT) {
goto END;
}
runTsk = (LosTaskCB *)g_losTask.runTask;
OsSchedTaskWait(&queueCB->memList, timeOut);
LOS_IntRestore(intSave);
LOS_Schedule();
intSave = LOS_IntLock();
if (runTsk->taskStatus & OS_TASK_STATUS_TIMEOUT) {
runTsk->taskStatus &= (~OS_TASK_STATUS_TIMEOUT);
goto END;
} else {
/* When enters the current branch, means the current task already got a available membox,
* so the runTsk->msg can not be NULL.
*/
mem = runTsk->msg;
runTsk->msg = NULL;
}
}
END:
LOS_IntRestore(intSave);
return mem;
}
/*****************************************************************************
Function : OsQueueMailFree
Description : Mail free memory
Input : queueID --- QueueID
: mailPool --- MailPool
: mailMem --- MailMem
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT UINT32 OsQueueMailFree(UINT32 queueID, VOID *mailPool, VOID *mailMem)
{
VOID *mem = (VOID *)NULL;
UINT32 intSave;
LosQueueCB *queueCB = (LosQueueCB *)NULL;
LosTaskCB *resumedTask = (LosTaskCB *)NULL;
if (queueID >= LOSCFG_BASE_IPC_QUEUE_LIMIT) {
return LOS_ERRNO_QUEUE_MAIL_HANDLE_INVALID;
}
if (mailPool == NULL) {
return LOS_ERRNO_QUEUE_MAIL_PTR_INVALID;
}
intSave = LOS_IntLock();
if (LOS_MemboxFree(mailPool, mailMem)) {
LOS_IntRestore(intSave);
return LOS_ERRNO_QUEUE_MAIL_FREE_ERROR;
}
queueCB = GET_QUEUE_HANDLE(queueID);
if (queueCB->queueState == OS_QUEUE_UNUSED) {
LOS_IntRestore(intSave);
return LOS_ERRNO_QUEUE_NOT_CREATE;
}
if (!LOS_ListEmpty(&queueCB->memList)) {
resumedTask = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&queueCB->memList));
OsSchedTaskWake(resumedTask);
mem = LOS_MemboxAlloc(mailPool);
if (mem == NULL) {
LOS_IntRestore(intSave);
return LOS_ERRNO_QUEUE_NO_MEMORY;
}
resumedTask->msg = mem;
LOS_IntRestore(intSave);
LOS_Schedule();
} else {
LOS_IntRestore(intSave);
}
return LOS_OK;
}
/*****************************************************************************
Function : LOS_QueueDelete
Description : Delete a queue
Input : queueID --- QueueID
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 LOS_QueueDelete(UINT32 queueID)
{
LosQueueCB *queueCB = NULL;
UINT8 *queue = NULL;
UINT32 intSave;
UINT32 ret;
if (queueID >= LOSCFG_BASE_IPC_QUEUE_LIMIT) {
return LOS_ERRNO_QUEUE_NOT_FOUND;
}
intSave = LOS_IntLock();
queueCB = (LosQueueCB *)GET_QUEUE_HANDLE(queueID);
if (queueCB->queueState == OS_QUEUE_UNUSED) {
ret = LOS_ERRNO_QUEUE_NOT_CREATE;
goto QUEUE_END;
}
if (!LOS_ListEmpty(&queueCB->readWriteList[OS_QUEUE_READ])) {
ret = LOS_ERRNO_QUEUE_IN_TSKUSE;
goto QUEUE_END;
}
if (!LOS_ListEmpty(&queueCB->readWriteList[OS_QUEUE_WRITE])) {
ret = LOS_ERRNO_QUEUE_IN_TSKUSE;
goto QUEUE_END;
}
if (!LOS_ListEmpty(&queueCB->memList)) {
ret = LOS_ERRNO_QUEUE_IN_TSKUSE;
goto QUEUE_END;
}
if ((queueCB->readWriteableCnt[OS_QUEUE_WRITE] + queueCB->readWriteableCnt[OS_QUEUE_READ]) !=
queueCB->queueLen) {
ret = LOS_ERRNO_QUEUE_IN_TSKWRITE;
goto QUEUE_END;
}
queue = queueCB->queue;
queueCB->queue = (UINT8 *)NULL;
queueCB->queueState = OS_QUEUE_UNUSED;
LOS_ListAdd(&g_freeQueueList, &queueCB->readWriteList[OS_QUEUE_WRITE]);
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_QUEUE_DELETE, queueCB);
ret = LOS_MemFree(m_aucSysMem0, (VOID *)queue);
return ret;
QUEUE_END:
LOS_IntRestore(intSave);
return ret;
}
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_QueueInfoGet(UINT32 queueID, QUEUE_INFO_S *queueInfo)
{
UINT32 intSave;
UINT32 ret = LOS_OK;
LosQueueCB *queueCB = NULL;
LosTaskCB *tskCB = NULL;
if (queueInfo == NULL) {
return LOS_ERRNO_QUEUE_PTR_NULL;
}
if (queueID >= LOSCFG_BASE_IPC_QUEUE_LIMIT) {
return LOS_ERRNO_QUEUE_INVALID;
}
(VOID)memset_s((VOID *)queueInfo, sizeof(QUEUE_INFO_S), 0, sizeof(QUEUE_INFO_S));
intSave = LOS_IntLock();
queueCB = (LosQueueCB *)GET_QUEUE_HANDLE(queueID);
if (queueCB->queueState == OS_QUEUE_UNUSED) {
ret = LOS_ERRNO_QUEUE_NOT_CREATE;
goto QUEUE_END;
}
queueInfo->queueID = queueID;
queueInfo->queueLen = queueCB->queueLen;
queueInfo->queueSize = queueCB->queueSize;
queueInfo->queueHead = queueCB->queueHead;
queueInfo->queueTail = queueCB->queueTail;
queueInfo->readableCnt = queueCB->readWriteableCnt[OS_QUEUE_READ];
queueInfo->writableCnt = queueCB->readWriteableCnt[OS_QUEUE_WRITE];
LOS_DL_LIST_FOR_EACH_ENTRY(tskCB, &queueCB->readWriteList[OS_QUEUE_READ], LosTaskCB, pendList) {
queueInfo->waitReadTask |= (1 << tskCB->taskID);
}
LOS_DL_LIST_FOR_EACH_ENTRY(tskCB, &queueCB->readWriteList[OS_QUEUE_WRITE], LosTaskCB, pendList) {
queueInfo->waitWriteTask |= (1 << tskCB->taskID);
}
LOS_DL_LIST_FOR_EACH_ENTRY(tskCB, &queueCB->memList, LosTaskCB, pendList) {
queueInfo->waitMemTask |= (1 << tskCB->taskID);
}
QUEUE_END:
LOS_IntRestore(intSave);
return ret;
}
#endif /* (LOSCFG_BASE_IPC_QUEUE == 1) */
@@ -0,0 +1,566 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "los_sched.h"
#include "los_task.h"
#include "los_tick.h"
#include "los_debug.h"
#include "los_hook.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
#define OS_PRIORITY_QUEUE_NUM 32
#define PRIQUEUE_PRIOR0_BIT 0x80000000U
#define OS_SCHED_TIME_SLICES ((LOSCFG_BASE_CORE_TIMESLICE_TIMEOUT * OS_SYS_NS_PER_US) / OS_NS_PER_CYCLE)
#define OS_TIME_SLICE_MIN (INT32)((50 * OS_SYS_NS_PER_US) / OS_NS_PER_CYCLE) /* 50us */
#define OS_TICK_RESPONSE_TIME_MAX LOSCFG_BASE_CORE_TICK_RESPONSE_MAX
#if (LOSCFG_BASE_CORE_TICK_RESPONSE_MAX == 0)
#error "Must specify the maximum value that tick timer counter supports!"
#endif
STATIC SchedScan g_swtmrScan = NULL;
STATIC SortLinkAttribute *g_taskSortLinkList = NULL;
STATIC LOS_DL_LIST g_priQueueList[OS_PRIORITY_QUEUE_NUM];
STATIC UINT32 g_queueBitmap;
STATIC UINT32 g_schedResponseID = 0;
STATIC UINT64 g_schedResponseTime = OS_SCHED_MAX_RESPONSE_TIME;
STATIC VOID (*SchedRealSleepTimeSet)(UINT64) = NULL;
UINT32 OsSchedRealSleepTimeSet(VOID (*func)(UINT64))
{
if (func == NULL) {
return LOS_NOK;
}
SchedRealSleepTimeSet = func;
return LOS_OK;
}
#if (LOSCFG_BASE_CORE_TICK_WTIMER == 0)
STATIC UINT64 g_schedTimerBase;
VOID OsSchedUpdateSchedTimeBase(VOID)
{
UINT32 period = 0;
(VOID)HalGetTickCycle(&period);
g_schedTimerBase += period;
}
VOID OsSchedTimerBaseReset(UINT64 currTime)
{
LOS_ASSERT(currTime > g_schedTimerBase);
g_schedTimerBase = currTime;
g_schedResponseTime = OS_SCHED_MAX_RESPONSE_TIME;
}
#endif
UINT64 OsGetCurrSchedTimeCycle(VOID)
{
if (!g_taskScheduled) {
return 0;
}
#if (LOSCFG_BASE_CORE_TICK_WTIMER == 1)
return HalGetTickCycle(NULL);
#else
STATIC UINT64 oldSchedTime = 0;
UINT32 period = 0;
UINT32 intSave = LOS_IntLock();
UINT64 time = HalGetTickCycle(&period);
UINT64 schedTime = g_schedTimerBase + time;
if (schedTime < oldSchedTime) {
/* Turn the timer count */
g_schedTimerBase += period;
schedTime = g_schedTimerBase + time;
}
LOS_ASSERT(schedTime >= oldSchedTime);
oldSchedTime = schedTime;
LOS_IntRestore(intSave);
return schedTime;
#endif
}
STATIC INLINE VOID OsTimeSliceUpdate(LosTaskCB *taskCB, UINT64 currTime)
{
LOS_ASSERT(currTime >= taskCB->startTime);
INT32 incTime = currTime - taskCB->startTime;
if (taskCB->taskID != g_idleTaskID) {
taskCB->timeSlice -= incTime;
}
taskCB->startTime = currTime;
}
STATIC INLINE VOID OsSchedSetNextExpireTime(UINT64 startTime, UINT32 responseID, UINT64 taskEndTime, BOOL timeUpdate)
{
UINT64 nextExpireTime = OsGetNextExpireTime(startTime);
UINT64 nextResponseTime;
BOOL isTimeSlice = FALSE;
/* The current thread's time slice has been consumed, but the current system lock task cannot
* trigger the schedule to release the CPU
*/
if (taskEndTime < nextExpireTime) {
nextExpireTime = taskEndTime;
isTimeSlice = TRUE;
}
if ((g_schedResponseTime > nextExpireTime) && ((g_schedResponseTime - nextExpireTime) >= OS_CYCLE_PER_TICK)) {
nextResponseTime = nextExpireTime - startTime;
if (nextResponseTime > OS_TICK_RESPONSE_TIME_MAX) {
if (SchedRealSleepTimeSet != NULL) {
SchedRealSleepTimeSet(nextResponseTime);
}
nextResponseTime = OS_TICK_RESPONSE_TIME_MAX;
nextExpireTime = startTime + nextResponseTime;
} else if (nextResponseTime < OS_CYCLE_PER_TICK) {
if (SchedRealSleepTimeSet != NULL) {
SchedRealSleepTimeSet(0);
}
nextResponseTime = OS_CYCLE_PER_TICK;
nextExpireTime = startTime + nextResponseTime;
if (nextExpireTime >= g_schedResponseTime) {
return;
}
}
} else {
/* There is no point earlier than the current expiration date */
return;
}
if (isTimeSlice) {
/* The expiration time of the current system is the thread's slice expiration time */
g_schedResponseID = responseID;
} else {
g_schedResponseID = OS_INVALID;
}
g_schedResponseTime = nextExpireTime;
#if (LOSCFG_BASE_CORE_TICK_WTIMER == 0)
if (timeUpdate) {
g_schedTimerBase = OsGetCurrSchedTimeCycle();
}
#endif
HalSysTickReload(nextResponseTime);
}
VOID OsSchedUpdateExpireTime(UINT64 startTime, BOOL timeUpdate)
{
UINT64 endTime;
LosTaskCB *runTask = g_losTask.runTask;
if (runTask->taskID != g_idleTaskID) {
INT32 timeSlice = (runTask->timeSlice <= OS_TIME_SLICE_MIN) ? OS_SCHED_TIME_SLICES : runTask->timeSlice;
endTime = startTime + timeSlice;
} else {
endTime = OS_SCHED_MAX_RESPONSE_TIME - OS_CYCLE_PER_TICK;
}
OsSchedSetNextExpireTime(startTime, runTask->taskID, endTime, timeUpdate);
}
STATIC INLINE VOID OsSchedPriQueueEnHead(LOS_DL_LIST *priqueueItem, UINT32 priority)
{
/*
* Task control blocks are inited as zero. And when task is deleted,
* and at the same time would be deleted from priority queue or
* other lists, task pend node will restored as zero.
*/
if (LOS_ListEmpty(&g_priQueueList[priority])) {
g_queueBitmap |= PRIQUEUE_PRIOR0_BIT >> priority;
}
LOS_ListAdd(&g_priQueueList[priority], priqueueItem);
}
STATIC INLINE VOID OsSchedPriQueueEnTail(LOS_DL_LIST *priqueueItem, UINT32 priority)
{
if (LOS_ListEmpty(&g_priQueueList[priority])) {
g_queueBitmap |= PRIQUEUE_PRIOR0_BIT >> priority;
}
LOS_ListTailInsert(&g_priQueueList[priority], priqueueItem);
}
STATIC INLINE VOID OsSchedPriQueueDelete(LOS_DL_LIST *priqueueItem, UINT32 priority)
{
LOS_ListDelete(priqueueItem);
if (LOS_ListEmpty(&g_priQueueList[priority])) {
g_queueBitmap &= ~(PRIQUEUE_PRIOR0_BIT >> priority);
}
}
STATIC INLINE VOID OsSchedWakePendTimeTask(UINT64 currTime, LosTaskCB *taskCB, BOOL *needSchedule)
{
UINT16 tempStatus = taskCB->taskStatus;
if (tempStatus & (OS_TASK_STATUS_PEND | OS_TASK_STATUS_DELAY)) {
taskCB->taskStatus &= ~(OS_TASK_STATUS_PEND | OS_TASK_STATUS_PEND_TIME | OS_TASK_STATUS_DELAY);
if (tempStatus & OS_TASK_STATUS_PEND) {
taskCB->taskStatus |= OS_TASK_STATUS_TIMEOUT;
LOS_ListDelete(&taskCB->pendList);
taskCB->taskMux = NULL;
taskCB->taskSem = NULL;
}
if (!(tempStatus & OS_TASK_STATUS_SUSPEND)) {
OsSchedTaskEnQueue(taskCB);
*needSchedule = TRUE;
}
}
}
STATIC INLINE BOOL OsSchedScanTimerList(VOID)
{
BOOL needSchedule = FALSE;
LOS_DL_LIST *listObject = &g_taskSortLinkList->sortLink;
/*
* When task is pended with timeout, the task block is on the timeout sortlink
* (per cpu) and ipc(mutex,sem and etc.)'s block at the same time, it can be waken
* up by either timeout or corresponding ipc it's waiting.
*
* Now synchronize sortlink procedure is used, therefore the whole task scan needs
* to be protected, preventing another core from doing sortlink deletion at same time.
*/
if (LOS_ListEmpty(listObject)) {
return needSchedule;
}
SortLinkList *sortList = LOS_DL_LIST_ENTRY(listObject->pstNext, SortLinkList, sortLinkNode);
UINT64 currTime = OsGetCurrSchedTimeCycle();
while (sortList->responseTime <= currTime) {
LosTaskCB *taskCB = LOS_DL_LIST_ENTRY(sortList, LosTaskCB, sortList);
OsDeleteNodeSortLink(g_taskSortLinkList, &taskCB->sortList);
OsSchedWakePendTimeTask(currTime, taskCB, &needSchedule);
if (LOS_ListEmpty(listObject)) {
break;
}
sortList = LOS_DL_LIST_ENTRY(listObject->pstNext, SortLinkList, sortLinkNode);
}
return needSchedule;
}
VOID OsSchedTaskEnQueue(LosTaskCB *taskCB)
{
LOS_ASSERT(!(taskCB->taskStatus & OS_TASK_STATUS_READY));
if (taskCB->taskID != g_idleTaskID) {
if (taskCB->timeSlice > OS_TIME_SLICE_MIN) {
OsSchedPriQueueEnHead(&taskCB->pendList, taskCB->priority);
} else {
taskCB->timeSlice = OS_SCHED_TIME_SLICES;
OsSchedPriQueueEnTail(&taskCB->pendList, taskCB->priority);
}
OsHookCall(LOS_HOOK_TYPE_MOVEDTASKTOREADYSTATE, taskCB);
}
taskCB->taskStatus &= ~(OS_TASK_STATUS_PEND | OS_TASK_STATUS_SUSPEND |
OS_TASK_STATUS_DELAY | OS_TASK_STATUS_PEND_TIME);
taskCB->taskStatus |= OS_TASK_STATUS_READY;
}
VOID OsSchedTaskDeQueue(LosTaskCB *taskCB)
{
if (taskCB->taskStatus & OS_TASK_STATUS_READY) {
if (taskCB->taskID != g_idleTaskID) {
OsSchedPriQueueDelete(&taskCB->pendList, taskCB->priority);
}
taskCB->taskStatus &= ~OS_TASK_STATUS_READY;
}
}
VOID OsSchedTaskExit(LosTaskCB *taskCB)
{
if (taskCB->taskStatus & OS_TASK_STATUS_READY) {
OsSchedTaskDeQueue(taskCB);
} else if (taskCB->taskStatus & OS_TASK_STATUS_PEND) {
LOS_ListDelete(&taskCB->pendList);
taskCB->taskStatus &= ~OS_TASK_STATUS_PEND;
}
if (taskCB->taskStatus & (OS_TASK_STATUS_DELAY | OS_TASK_STATUS_PEND_TIME)) {
OsDeleteSortLink(&taskCB->sortList, OS_SORT_LINK_TASK);
taskCB->taskStatus &= ~(OS_TASK_STATUS_DELAY | OS_TASK_STATUS_PEND_TIME);
}
}
VOID OsSchedYield(VOID)
{
LosTaskCB *runTask = g_losTask.runTask;
runTask->timeSlice = 0;
}
VOID OsSchedDelay(LosTaskCB *runTask, UINT32 tick)
{
runTask->taskStatus |= OS_TASK_STATUS_DELAY;
runTask->waitTimes = tick;
}
VOID OsSchedTaskWait(LOS_DL_LIST *list, UINT32 ticks)
{
LosTaskCB *runTask = g_losTask.runTask;
runTask->taskStatus |= OS_TASK_STATUS_PEND;
LOS_ListTailInsert(list, &runTask->pendList);
if (ticks != LOS_WAIT_FOREVER) {
runTask->taskStatus |= OS_TASK_STATUS_PEND_TIME;
runTask->waitTimes = ticks;
}
}
VOID OsSchedTaskWake(LosTaskCB *resumedTask)
{
LOS_ListDelete(&resumedTask->pendList);
resumedTask->taskStatus &= ~OS_TASK_STATUS_PEND;
if (resumedTask->taskStatus & OS_TASK_STATUS_PEND_TIME) {
OsDeleteSortLink(&resumedTask->sortList, OS_SORT_LINK_TASK);
resumedTask->taskStatus &= ~OS_TASK_STATUS_PEND_TIME;
}
if (!(resumedTask->taskStatus & OS_TASK_STATUS_SUSPEND) &&
!(resumedTask->taskStatus & OS_TASK_STATUS_RUNNING)) {
OsSchedTaskEnQueue(resumedTask);
}
}
BOOL OsSchedModifyTaskSchedParam(LosTaskCB *taskCB, UINT16 priority)
{
if (taskCB->taskStatus & OS_TASK_STATUS_READY) {
OsSchedTaskDeQueue(taskCB);
taskCB->priority = priority;
OsSchedTaskEnQueue(taskCB);
return TRUE;
}
taskCB->priority = priority;
OsHookCall(LOS_HOOK_TYPE_TASK_PRIMODIFY, taskCB, taskCB->priority);
if (taskCB->taskStatus & OS_TASK_STATUS_RUNNING) {
return TRUE;
}
return FALSE;
}
VOID OsSchedSetIdleTaskSchedParam(LosTaskCB *idleTask)
{
OsSchedTaskEnQueue(idleTask);
}
UINT32 OsSchedSwtmrScanRegister(SchedScan func)
{
if (func == NULL) {
return LOS_NOK;
}
g_swtmrScan = func;
return LOS_OK;
}
UINT32 OsTaskNextSwitchTimeGet(VOID)
{
UINT32 intSave = LOS_IntLock();
UINT32 ticks = OsSortLinkGetNextExpireTime(g_taskSortLinkList);
LOS_IntRestore(intSave);
return ticks;
}
UINT32 OsSchedInit(VOID)
{
UINT16 pri;
for (pri = 0; pri < OS_PRIORITY_QUEUE_NUM; pri++) {
LOS_ListInit(&g_priQueueList[pri]);
}
g_queueBitmap = 0;
g_taskSortLinkList = OsGetSortLinkAttribute(OS_SORT_LINK_TASK);
if (g_taskSortLinkList == NULL) {
return LOS_NOK;
}
OsSortLinkInit(g_taskSortLinkList);
g_schedResponseTime = OS_SCHED_MAX_RESPONSE_TIME;
return LOS_OK;
}
LosTaskCB *OsGetTopTask(VOID)
{
UINT32 priority;
LosTaskCB *newTask = NULL;
if (g_queueBitmap) {
priority = CLZ(g_queueBitmap);
newTask = LOS_DL_LIST_ENTRY(((LOS_DL_LIST *)&g_priQueueList[priority])->pstNext, LosTaskCB, pendList);
} else {
newTask = OS_TCB_FROM_TID(g_idleTaskID);
}
return newTask;
}
VOID OsSchedStart(VOID)
{
(VOID)LOS_IntLock();
LosTaskCB *newTask = OsGetTopTask();
newTask->taskStatus |= OS_TASK_STATUS_RUNNING;
g_losTask.newTask = newTask;
g_losTask.runTask = g_losTask.newTask;
g_taskScheduled = 1;
newTask->startTime = OsGetCurrSchedTimeCycle();
OsSchedTaskDeQueue(newTask);
g_schedResponseTime = OS_SCHED_MAX_RESPONSE_TIME;
g_schedResponseID = OS_INVALID;
OsSchedSetNextExpireTime(newTask->startTime, newTask->taskID, newTask->startTime + newTask->timeSlice, TRUE);
PRINTK("Entering scheduler\n");
}
BOOL OsSchedTaskSwitch(VOID)
{
UINT64 endTime;
BOOL isTaskSwitch = FALSE;
LosTaskCB *runTask = g_losTask.runTask;
OsTimeSliceUpdate(runTask, OsGetCurrSchedTimeCycle());
if (runTask->taskStatus & (OS_TASK_STATUS_PEND_TIME | OS_TASK_STATUS_DELAY)) {
OsAdd2SortLink(&runTask->sortList, runTask->startTime, runTask->waitTimes, OS_SORT_LINK_TASK);
} else if (!(runTask->taskStatus & (OS_TASK_STATUS_PEND | OS_TASK_STATUS_SUSPEND | OS_TASK_STATUS_UNUSED))) {
OsSchedTaskEnQueue(runTask);
}
LosTaskCB *newTask = OsGetTopTask();
g_losTask.newTask = newTask;
if (runTask != newTask) {
#if (LOSCFG_BASE_CORE_TSK_MONITOR == 1)
OsTaskSwitchCheck();
#endif
runTask->taskStatus &= ~OS_TASK_STATUS_RUNNING;
newTask->taskStatus |= OS_TASK_STATUS_RUNNING;
newTask->startTime = runTask->startTime;
isTaskSwitch = TRUE;
OsHookCall(LOS_HOOK_TYPE_TASK_SWITCHEDIN);
}
OsSchedTaskDeQueue(newTask);
if (newTask->taskID != g_idleTaskID) {
endTime = newTask->startTime + newTask->timeSlice;
} else {
endTime = OS_SCHED_MAX_RESPONSE_TIME - OS_CYCLE_PER_TICK;
}
OsSchedSetNextExpireTime(newTask->startTime, newTask->taskID, endTime, TRUE);
return isTaskSwitch;
}
UINT64 LOS_SchedTickTimeoutNsGet(VOID)
{
UINT32 intSave;
UINT64 responseTime;
UINT64 currTime;
intSave = LOS_IntLock();
responseTime = g_schedResponseTime;
currTime = OsGetCurrSchedTimeCycle();
LOS_IntRestore(intSave);
if (responseTime > currTime) {
responseTime = responseTime - currTime;
} else {
responseTime = 0; /* Tick interrupt already timeout */
}
return OS_SYS_CYCLE_TO_NS(responseTime, OS_SYS_CLOCK);
}
VOID LOS_SchedTickHandler(VOID)
{
UINT64 currTime;
BOOL needSched = FALSE;
LOS_ASSERT(g_taskScheduled);
UINT32 intSave = LOS_IntLock();
if (g_schedResponseID == OS_INVALID) {
if (g_swtmrScan != NULL) {
needSched = g_swtmrScan();
}
needSched |= OsSchedScanTimerList();
}
g_schedResponseTime = OS_SCHED_MAX_RESPONSE_TIME;
if (needSched && LOS_CHECK_SCHEDULE) {
HalTaskSchedule();
} else {
currTime = OsGetCurrSchedTimeCycle();
OsTimeSliceUpdate(g_losTask.runTask, currTime);
OsSchedUpdateExpireTime(currTime, TRUE);
}
// printf("3\r\n");
LOS_IntRestore(intSave);
}
VOID LOS_Schedule(VOID)
{
if (g_taskScheduled && LOS_CHECK_SCHEDULE) {
HalTaskSchedule();
}
}
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
@@ -0,0 +1,327 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "los_sem.h"
#include "los_arch.h"
#include "los_config.h"
#include "los_debug.h"
#include "los_hook.h"
#include "los_interrupt.h"
#include "los_memory.h"
#include "los_sched.h"
#if (LOSCFG_BASE_IPC_SEM == 1)
LITE_OS_SEC_DATA_INIT LOS_DL_LIST g_unusedSemList;
LITE_OS_SEC_BSS LosSemCB *g_allSem = NULL;
/*****************************************************************************
Function : OsSemInit
Description : Initialize the Semaphore doubly linked list
Input : None
Output : None
Return : LOS_OK on success, or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 OsSemInit(VOID)
{
LosSemCB *semNode = NULL;
UINT16 index;
LOS_ListInit(&g_unusedSemList);
if (LOSCFG_BASE_IPC_SEM_LIMIT == 0) {
return LOS_ERRNO_SEM_MAXNUM_ZERO;
}
g_allSem = (LosSemCB *)LOS_MemAlloc(m_aucSysMem0, (LOSCFG_BASE_IPC_SEM_LIMIT * sizeof(LosSemCB)));
if (g_allSem == NULL) {
return LOS_ERRNO_SEM_NO_MEMORY;
}
/* Connect all the semaphore CBs in a doubly linked list. */
for (index = 0; index < LOSCFG_BASE_IPC_SEM_LIMIT; index++) {
semNode = ((LosSemCB *)g_allSem) + index;
semNode->semID = index;
semNode->semStat = OS_SEM_UNUSED;
LOS_ListTailInsert(&g_unusedSemList, &semNode->semList);
}
return LOS_OK;
}
/*****************************************************************************
Function : OsSemCreate
Description : create the Semaphore
Input : count --- Semaphore count
: maxCount --- Max semaphore count for check
Output : semHandle --- Index of semaphore
Return : LOS_OK on success, or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 OsSemCreate(UINT16 count, UINT16 maxCount, UINT32 *semHandle)
{
UINT32 intSave;
LosSemCB *semCreated = NULL;
LOS_DL_LIST *unusedSem = NULL;
UINT32 errNo;
UINT32 errLine;
if (semHandle == NULL) {
return LOS_ERRNO_SEM_PTR_NULL;
}
if (count > maxCount) {
OS_GOTO_ERR_HANDLER(LOS_ERRNO_SEM_OVERFLOW);
}
intSave = LOS_IntLock();
if (LOS_ListEmpty(&g_unusedSemList)) {
LOS_IntRestore(intSave);
OS_GOTO_ERR_HANDLER(LOS_ERRNO_SEM_ALL_BUSY);
}
unusedSem = LOS_DL_LIST_FIRST(&(g_unusedSemList));
LOS_ListDelete(unusedSem);
semCreated = (GET_SEM_LIST(unusedSem));
semCreated->semCount = count;
semCreated->semStat = OS_SEM_USED;
semCreated->maxSemCount = maxCount;
LOS_ListInit(&semCreated->semList);
*semHandle = (UINT32)semCreated->semID;
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_SEM_CREATE, semCreated);
return LOS_OK;
ERR_HANDLER:
OS_RETURN_ERROR_P2(errLine, errNo);
}
/*****************************************************************************
Function : LOS_SemCreate
Description : Create a semaphore
Input : count--------- semaphore count
Output : semHandle-----Index of semaphore
Return : LOS_OK on success, or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 LOS_SemCreate(UINT16 count, UINT32 *semHandle)
{
return OsSemCreate(count, OS_SEM_COUNTING_MAX_COUNT, semHandle);
}
/*****************************************************************************
Function : LOS_BinarySemCreate
Description : Create a binary semaphore
Input : count--------- semaphore count
Output : semHandle-----Index of semaphore
Return : LOS_OK on success, or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 LOS_BinarySemCreate(UINT16 count, UINT32 *semHandle)
{
return OsSemCreate(count, OS_SEM_BINARY_MAX_COUNT, semHandle);
}
/*****************************************************************************
Function : LOS_SemDelete
Description : Delete a semaphore
Input : semHandle--------- semaphore operation handle
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 LOS_SemDelete(UINT32 semHandle)
{
UINT32 intSave;
LosSemCB *semDeleted = NULL;
UINT32 errNo;
UINT32 errLine;
if (semHandle >= (UINT32)LOSCFG_BASE_IPC_SEM_LIMIT) {
OS_GOTO_ERR_HANDLER(LOS_ERRNO_SEM_INVALID);
}
semDeleted = GET_SEM(semHandle);
intSave = LOS_IntLock();
if (semDeleted->semStat == OS_SEM_UNUSED) {
LOS_IntRestore(intSave);
OS_GOTO_ERR_HANDLER(LOS_ERRNO_SEM_INVALID);
}
if (!LOS_ListEmpty(&semDeleted->semList)) {
LOS_IntRestore(intSave);
OS_GOTO_ERR_HANDLER(LOS_ERRNO_SEM_PENDED);
}
LOS_ListAdd(&g_unusedSemList, &semDeleted->semList);
semDeleted->semStat = OS_SEM_UNUSED;
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_SEM_DELETE, semDeleted);
return LOS_OK;
ERR_HANDLER:
OS_RETURN_ERROR_P2(errLine, errNo);
}
STATIC_INLINE UINT32 OsSemValidCheck(LosSemCB *semPended)
{
if (semPended->semStat == OS_SEM_UNUSED) {
return LOS_ERRNO_SEM_INVALID;
}
if (OS_INT_ACTIVE) {
PRINT_ERR("!!!LOS_ERRNO_SEM_PEND_INTERR!!!\n");
return LOS_ERRNO_SEM_PEND_INTERR;
}
if (g_losTaskLock) {
PRINT_ERR("!!!LOS_ERRNO_SEM_PEND_IN_LOCK!!!\n");
return LOS_ERRNO_SEM_PEND_IN_LOCK;
}
return LOS_OK;
}
/*****************************************************************************
Function : LOS_SemPend
Description : Specified semaphore P operation
Input : semHandle --------- semaphore operation handle
: timeout --------- waitting time
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT UINT32 LOS_SemPend(UINT32 semHandle, UINT32 timeout)
{
UINT32 intSave;
LosSemCB *semPended = NULL;
UINT32 retErr;
LosTaskCB *runningTask = NULL;
if (semHandle >= (UINT32)LOSCFG_BASE_IPC_SEM_LIMIT) {
OS_RETURN_ERROR(LOS_ERRNO_SEM_INVALID);
}
semPended = GET_SEM(semHandle);
intSave = LOS_IntLock();
retErr = OsSemValidCheck(semPended);
if (retErr) {
goto ERROR_SEM_PEND;
}
if (semPended->semCount > 0) {
semPended->semCount--;
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_SEM_PEND, semPended, runningTask);
return LOS_OK;
}
if (!timeout) {
retErr = LOS_ERRNO_SEM_UNAVAILABLE;
goto ERROR_SEM_PEND;
}
runningTask = (LosTaskCB *)g_losTask.runTask;
runningTask->taskSem = (VOID *)semPended;
OsSchedTaskWait(&semPended->semList, timeout);
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_SEM_PEND, semPended, runningTask);
LOS_Schedule();
intSave = LOS_IntLock();
if (runningTask->taskStatus & OS_TASK_STATUS_TIMEOUT) {
runningTask->taskStatus &= (~OS_TASK_STATUS_TIMEOUT);
retErr = LOS_ERRNO_SEM_TIMEOUT;
goto ERROR_SEM_PEND;
}
LOS_IntRestore(intSave);
return LOS_OK;
ERROR_SEM_PEND:
LOS_IntRestore(intSave);
OS_RETURN_ERROR(retErr);
}
/*****************************************************************************
Function : LOS_SemPost
Description : Specified semaphore V operation
Input : semHandle--------- semaphore operation handle
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT UINT32 LOS_SemPost(UINT32 semHandle)
{
UINT32 intSave;
LosSemCB *semPosted = GET_SEM(semHandle);
LosTaskCB *resumedTask = NULL;
if (semHandle >= LOSCFG_BASE_IPC_SEM_LIMIT) {
return LOS_ERRNO_SEM_INVALID;
}
intSave = LOS_IntLock();
if (semPosted->semStat == OS_SEM_UNUSED) {
LOS_IntRestore(intSave);
OS_RETURN_ERROR(LOS_ERRNO_SEM_INVALID);
}
if (semPosted->maxSemCount == semPosted->semCount) {
LOS_IntRestore(intSave);
OS_RETURN_ERROR(LOS_ERRNO_SEM_OVERFLOW);
}
if (!LOS_ListEmpty(&semPosted->semList)) {
resumedTask = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&(semPosted->semList)));
resumedTask->taskSem = NULL;
OsSchedTaskWake(resumedTask);
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_SEM_POST, semPosted, resumedTask);
LOS_Schedule();
} else {
semPosted->semCount++;
LOS_IntRestore(intSave);
OsHookCall(LOS_HOOK_TYPE_SEM_POST, semPosted, resumedTask);
}
return LOS_OK;
}
LITE_OS_SEC_TEXT UINT32 LOS_SemGetValue(UINT32 semHandle, INT32 *currVal)
{
LosSemCB *sem = GET_SEM(semHandle);
if (semHandle >= (UINT32)LOSCFG_BASE_IPC_SEM_LIMIT) {
OS_RETURN_ERROR(LOS_ERRNO_SEM_INVALID);
}
*currVal = sem->semCount;
return 0;
}
#endif /* (LOSCFG_BASE_IPC_SEM == 1) */
@@ -0,0 +1,214 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "los_sortlink.h"
#include "los_sched.h"
#include "los_debug.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */
STATIC SortLinkAttribute g_taskSortLink;
STATIC SortLinkAttribute g_swtmrSortLink;
UINT32 OsSortLinkInit(SortLinkAttribute *sortLinkHeader)
{
LOS_ListInit(&sortLinkHeader->sortLink);
return LOS_OK;
}
STATIC INLINE VOID OsAddNode2SortLink(SortLinkAttribute *sortLinkHeader, SortLinkList *sortList)
{
LOS_DL_LIST *head = (LOS_DL_LIST *)&sortLinkHeader->sortLink;
if (LOS_ListEmpty(head)) {
LOS_ListAdd(head, &sortList->sortLinkNode);
return;
}
SortLinkList *listSorted = LOS_DL_LIST_ENTRY(head->pstNext, SortLinkList, sortLinkNode);
if (listSorted->responseTime > sortList->responseTime) {
LOS_ListAdd(head, &sortList->sortLinkNode);
return;
} else if (listSorted->responseTime == sortList->responseTime) {
LOS_ListAdd(head->pstNext, &sortList->sortLinkNode);
return;
}
LOS_DL_LIST *prevNode = head->pstPrev;
do {
listSorted = LOS_DL_LIST_ENTRY(prevNode, SortLinkList, sortLinkNode);
if (listSorted->responseTime <= sortList->responseTime) {
LOS_ListAdd(prevNode, &sortList->sortLinkNode);
break;
}
prevNode = prevNode->pstPrev;
} while (1);
}
VOID OsDeleteNodeSortLink(SortLinkAttribute *sortLinkHeader, SortLinkList *sortList)
{
LOS_ListDelete(&sortList->sortLinkNode);
SET_SORTLIST_VALUE(sortList, OS_SORT_LINK_INVALID_TIME);
}
STATIC INLINE UINT64 OsGetSortLinkNextExpireTime(SortLinkAttribute *sortHeader, UINT64 startTime)
{
UINT64 expirTime = 0;
UINT64 nextExpirTime = 0;
LOS_DL_LIST *head = &sortHeader->sortLink;
LOS_DL_LIST *list = head->pstNext;
if (LOS_ListEmpty(head)) {
return OS_SCHED_MAX_RESPONSE_TIME - OS_CYCLE_PER_TICK;
}
do {
SortLinkList *listSorted = LOS_DL_LIST_ENTRY(list, SortLinkList, sortLinkNode);
if (listSorted->responseTime <= startTime) {
expirTime = startTime;
list = list->pstNext;
} else {
nextExpirTime = listSorted->responseTime;
break;
}
} while (list != head);
if (expirTime == 0) {
return nextExpirTime;
}
if (nextExpirTime == 0) {
return expirTime;
}
if ((nextExpirTime - expirTime) <= OS_US_PER_TICK) {
return nextExpirTime;
}
return expirTime;
}
VOID OsAdd2SortLink(SortLinkList *node, UINT64 startTime, UINT32 waitTicks, SortLinkType type)
{
UINT32 intSave;
SortLinkAttribute *sortLinkHeader = NULL;
if (type == OS_SORT_LINK_TASK) {
sortLinkHeader = &g_taskSortLink;
} else if (type == OS_SORT_LINK_SWTMR) {
sortLinkHeader = &g_swtmrSortLink;
} else {
LOS_Panic("Sort link type error : %u\n", type);
}
intSave = LOS_IntLock();
SET_SORTLIST_VALUE(node, startTime + (UINT64)waitTicks * OS_CYCLE_PER_TICK);
OsAddNode2SortLink(sortLinkHeader, node);
LOS_IntRestore(intSave);
}
VOID OsDeleteSortLink(SortLinkList *node, SortLinkType type)
{
UINT32 intSave;
SortLinkAttribute *sortLinkHeader = NULL;
if (type == OS_SORT_LINK_TASK) {
sortLinkHeader = &g_taskSortLink;
} else if (type == OS_SORT_LINK_SWTMR) {
sortLinkHeader = &g_swtmrSortLink;
} else {
LOS_Panic("Sort link type error : %u\n", type);
}
intSave = LOS_IntLock();
if (node->responseTime != OS_SORT_LINK_INVALID_TIME) {
OsDeleteNodeSortLink(sortLinkHeader, node);
}
LOS_IntRestore(intSave);
}
SortLinkAttribute *OsGetSortLinkAttribute(SortLinkType type)
{
if (type == OS_SORT_LINK_TASK) {
return &g_taskSortLink;
} else if (type == OS_SORT_LINK_SWTMR) {
return &g_swtmrSortLink;
}
PRINT_ERR("Invalid sort link type!\n");
return NULL;
}
UINT64 OsGetNextExpireTime(UINT64 startTime)
{
UINT32 intSave;
SortLinkAttribute *taskHeader = &g_taskSortLink;
SortLinkAttribute *swtmrHeader = &g_swtmrSortLink;
intSave = LOS_IntLock();
UINT64 taskExpirTime = OsGetSortLinkNextExpireTime(taskHeader, startTime);
UINT64 swtmrExpirTime = OsGetSortLinkNextExpireTime(swtmrHeader, startTime);
LOS_IntRestore(intSave);
return (taskExpirTime < swtmrExpirTime) ? taskExpirTime : swtmrExpirTime;
}
UINT32 OsSortLinkGetTargetExpireTime(UINT64 currTime, const SortLinkList *targetSortList)
{
if (currTime >= targetSortList->responseTime) {
return 0;
}
return (UINT32)(((targetSortList->responseTime - currTime) * LOSCFG_BASE_CORE_TICK_PER_SECOND) / OS_SYS_CLOCK);
}
UINT32 OsSortLinkGetNextExpireTime(const SortLinkAttribute *sortLinkHeader)
{
LOS_DL_LIST *head = (LOS_DL_LIST *)&sortLinkHeader->sortLink;
if (LOS_ListEmpty(head)) {
return 0;
}
SortLinkList *listSorted = LOS_DL_LIST_ENTRY(head->pstNext, SortLinkList, sortLinkNode);
return OsSortLinkGetTargetExpireTime(OsGetCurrSchedTimeCycle(), listSorted);
}
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
@@ -0,0 +1,666 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "los_config.h"
#include "securec.h"
#include "los_interrupt.h"
#include "los_swtmr.h"
#include "los_task.h"
#include "los_memory.h"
#include "los_queue.h"
#include "los_debug.h"
#include "los_sched.h"
#if (LOSCFG_BASE_CORE_SWTMR == 1)
LITE_OS_SEC_BSS UINT32 g_swtmrHandlerQueue; /* Software Timer timeout queue ID */
LITE_OS_SEC_BSS SWTMR_CTRL_S *g_swtmrCBArray = NULL; /* first address in Timer memory space */
LITE_OS_SEC_BSS SWTMR_CTRL_S *g_swtmrFreeList = NULL; /* Free list of Software Timer */
LITE_OS_SEC_BSS SortLinkAttribute *g_swtmrSortLinkList = NULL; /* The software timer count list */
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
typedef struct SwtmrAlignDataStr {
UINT32 times : 24;
UINT32 : 5;
UINT32 canMultiple : 1;
UINT32 canAlign : 1;
UINT32 isAligned : 1;
} SwtmrAlignData;
LITE_OS_SEC_BSS SwtmrAlignData g_swtmrAlignID[LOSCFG_BASE_CORE_SWTMR_LIMIT] = {0}; /* store swtmr align */
#endif
#define SWTMR_MAX_RUNNING_TICKS 2
#define OS_SWTMR_MAX_TIMERID ((0xFFFFFFFF / LOSCFG_BASE_CORE_SWTMR_LIMIT) * LOSCFG_BASE_CORE_SWTMR_LIMIT)
/*****************************************************************************
Function : OsSwtmrTask
Description : Swtmr task main loop, handle time-out timer.
Input : None
Output : None
Return : None
*****************************************************************************/
LITE_OS_SEC_TEXT VOID OsSwtmrTask(VOID)
{
SwtmrHandlerItem swtmrHandle;
UINT32 readSize;
UINT32 ret;
UINT64 tick;
readSize = sizeof(SwtmrHandlerItem);
for (;;) {
ret = LOS_QueueReadCopy(g_swtmrHandlerQueue, &swtmrHandle, &readSize, LOS_WAIT_FOREVER);
if ((ret == LOS_OK) && (readSize == sizeof(SwtmrHandlerItem))) {
if (swtmrHandle.handler == NULL) {
continue;
}
tick = LOS_TickCountGet();
swtmrHandle.handler(swtmrHandle.arg);
tick = LOS_TickCountGet() - tick;
if (tick >= SWTMR_MAX_RUNNING_TICKS) {
PRINT_WARN("timer_handler(%p) cost too many ms(%d)\n",
swtmrHandle.handler,
(UINT32)((tick * OS_SYS_MS_PER_SECOND) / LOSCFG_BASE_CORE_TICK_PER_SECOND));
}
}
}
}
/*****************************************************************************
Function : OsSwtmrTaskCreate
Description : Create Software Timer
Input : None
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 OsSwtmrTaskCreate(VOID)
{
UINT32 ret;
TSK_INIT_PARAM_S swtmrTask;
// Ignore the return code when matching CSEC rule 6.6(4).
(VOID)memset_s(&swtmrTask, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
swtmrTask.pfnTaskEntry = (TSK_ENTRY_FUNC)OsSwtmrTask;
swtmrTask.uwStackSize = LOSCFG_BASE_CORE_TSK_SWTMR_STACK_SIZE;
swtmrTask.pcName = "Swt_Task";
swtmrTask.usTaskPrio = 0;
ret = LOS_TaskCreate(&g_swtmrTaskID, &swtmrTask);
return ret;
}
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
STATIC_INLINE UINT32 OsSwtmrCalcAlignCount(UINT64 currTime, UINT32 interval, UINT32 timerId)
{
UINT32 count;
if (interval == 0) {
return interval;
}
SWTMR_CTRL_S *cur = g_swtmrCBArray + timerId % LOSCFG_BASE_CORE_SWTMR_LIMIT;
count = OsSortLinkGetTargetExpireTime(currTime, &cur->stSortList);
return (interval - (cur->uwInterval - count) % interval);
}
VOID OsSwtmrFindAlignPos(UINT64 currTime, SWTMR_CTRL_S *swtmr)
{
SWTMR_CTRL_S *minInLarge = (SWTMR_CTRL_S *)NULL;
SWTMR_CTRL_S *maxInLittle = (SWTMR_CTRL_S *)NULL;
UINT32 currSwtmrTimes, swtmrTimes;
UINT32 minInLargeVal = OS_NULL_INT;
UINT32 maxInLittleVal = OS_NULL_INT;
LOS_DL_LIST *listHead = &g_swtmrSortLinkList->sortLink;
if (LOS_ListEmpty(listHead)) {
return;
}
SwtmrAlignData currSwtmrAlgInfo = g_swtmrAlignID[swtmr->usTimerID % LOSCFG_BASE_CORE_SWTMR_LIMIT];
currSwtmrTimes = currSwtmrAlgInfo.times;
LOS_DL_LIST *listObject = listHead->pstNext;
do {
SortLinkList *sortList = LOS_DL_LIST_ENTRY(listObject, SortLinkList, sortLinkNode);
SWTMR_CTRL_S *cur = LOS_DL_LIST_ENTRY(sortList, SWTMR_CTRL_S, stSortList);
SwtmrAlignData swtmrAlgInfo = g_swtmrAlignID[cur->usTimerID % LOSCFG_BASE_CORE_SWTMR_LIMIT];
/* swtmr not start */
if ((swtmrAlgInfo.isAligned == 0) || (swtmrAlgInfo.canAlign == 0)) {
goto CONTINUE_NEXT_NODE;
}
/* find same interval timer, directly return */
if (cur->uwInterval == swtmr->uwInterval) {
swtmr->uwCount = OsSortLinkGetTargetExpireTime(currTime, &cur->stSortList);
return;
}
if ((currSwtmrAlgInfo.canMultiple != 1) || (swtmrAlgInfo.times == 0)) {
goto CONTINUE_NEXT_NODE;
}
swtmrTimes = swtmrAlgInfo.times;
if (currSwtmrTimes == 0) {
return;
}
if ((swtmrTimes >= currSwtmrTimes) && ((swtmrTimes % currSwtmrTimes) == 0)) {
if (minInLargeVal > (swtmrTimes / currSwtmrTimes)) {
minInLargeVal = swtmrTimes / currSwtmrTimes;
minInLarge = cur;
}
} else if ((swtmrTimes < currSwtmrTimes) && ((currSwtmrTimes % swtmrTimes) == 0)) {
if (maxInLittleVal > (currSwtmrTimes / swtmrTimes)) {
maxInLittleVal = currSwtmrTimes / swtmrTimes;
maxInLittle = cur;
}
}
CONTINUE_NEXT_NODE:
listObject = listObject->pstNext;
} while (listObject != listHead);
if (minInLarge != NULL) {
swtmr->uwCount = OsSwtmrCalcAlignCount(currTime, swtmr->uwInterval, minInLarge->usTimerID);
} else if (maxInLittle != NULL) {
swtmr->uwCount = OsSortLinkGetTargetExpireTime(currTime, &maxInLittle->stSortList);
}
return;
}
#endif
/*****************************************************************************
Function : OsSwtmrStart
Description : Start Software Timer
Input : swtmr ---------- Need to start Software Timer
Output : None
Return : None
*****************************************************************************/
LITE_OS_SEC_TEXT VOID OsSwtmrStart(SWTMR_CTRL_S *swtmr)
{
UINT64 currTime = OsGetCurrSchedTimeCycle();
swtmr->uwCount = swtmr->uwInterval;
swtmr->ucState = OS_SWTMR_STATUS_TICKING;
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
if ((g_swtmrAlignID[swtmr->usTimerID % LOSCFG_BASE_CORE_SWTMR_LIMIT].canAlign == 1) &&
(g_swtmrAlignID[swtmr->usTimerID % LOSCFG_BASE_CORE_SWTMR_LIMIT].isAligned == 0)) {
g_swtmrAlignID[swtmr->usTimerID % LOSCFG_BASE_CORE_SWTMR_LIMIT].isAligned = 1;
OsSwtmrFindAlignPos(currTime, swtmr);
}
#endif
OsAdd2SortLink(&swtmr->stSortList, currTime, swtmr->uwCount, OS_SORT_LINK_SWTMR);
if (LOS_TaskIsRunning()) {
OsSchedUpdateExpireTime(currTime, TRUE);
}
}
/*****************************************************************************
Function : OsSwtmrDelete
Description : Delete Software Timer
Input : swtmr --- Need to delete Software Timer, When using, Ensure that it can't be NULL.
Output : None
Return : None
*****************************************************************************/
STATIC_INLINE VOID OsSwtmrDelete(SWTMR_CTRL_S *swtmr)
{
/* insert to free list */
swtmr->pstNext = g_swtmrFreeList;
g_swtmrFreeList = swtmr;
swtmr->ucState = OS_SWTMR_STATUS_UNUSED;
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
(VOID)memset_s((VOID *)&g_swtmrAlignID[swtmr->usTimerID % LOSCFG_BASE_CORE_SWTMR_LIMIT],
sizeof(SwtmrAlignData), 0, sizeof(SwtmrAlignData));
#endif
}
LITE_OS_SEC_TEXT VOID OsSwtmrStop(SWTMR_CTRL_S *swtmr)
{
OsDeleteSortLink(&swtmr->stSortList, OS_SORT_LINK_SWTMR);
swtmr->ucState = OS_SWTMR_STATUS_CREATED;
if (LOS_TaskIsRunning()) {
OsSchedUpdateExpireTime(OsGetCurrSchedTimeCycle(), TRUE);
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
g_swtmrAlignID[swtmr->usTimerID % LOSCFG_BASE_CORE_SWTMR_LIMIT].isAligned = 0;
#endif
}
}
STATIC VOID OsSwtmrTimeoutHandle(SWTMR_CTRL_S *swtmr)
{
SwtmrHandlerItem swtmrHandler;
swtmrHandler.handler = swtmr->pfnHandler;
swtmrHandler.arg = swtmr->uwArg;
(VOID)LOS_QueueWriteCopy(g_swtmrHandlerQueue, &swtmrHandler, sizeof(SwtmrHandlerItem), LOS_NO_WAIT);
if (swtmr->ucMode == LOS_SWTMR_MODE_ONCE) {
OsSwtmrDelete(swtmr);
if (swtmr->usTimerID < (OS_SWTMR_MAX_TIMERID - LOSCFG_BASE_CORE_SWTMR_LIMIT)) {
swtmr->usTimerID += LOSCFG_BASE_CORE_SWTMR_LIMIT;
} else {
swtmr->usTimerID %= LOSCFG_BASE_CORE_SWTMR_LIMIT;
}
} else if (swtmr->ucMode == LOS_SWTMR_MODE_PERIOD) {
OsSwtmrStart(swtmr);
} else if (swtmr->ucMode == LOS_SWTMR_MODE_NO_SELFDELETE) {
swtmr->ucState = OS_SWTMR_STATUS_CREATED;
}
}
STATIC BOOL OsSwtmrScan(VOID)
{
BOOL needSchedule = FALSE;
LOS_DL_LIST *listObject = &g_swtmrSortLinkList->sortLink;
if (LOS_ListEmpty(listObject)) {
return needSchedule;
}
SortLinkList *sortList = LOS_DL_LIST_ENTRY(listObject->pstNext, SortLinkList, sortLinkNode);
UINT64 currTime = OsGetCurrSchedTimeCycle();
while (sortList->responseTime <= currTime) {
OsDeleteNodeSortLink(g_swtmrSortLinkList, sortList);
SWTMR_CTRL_S *swtmr = LOS_DL_LIST_ENTRY(sortList, SWTMR_CTRL_S, stSortList);
OsSwtmrTimeoutHandle(swtmr);
needSchedule = TRUE;
if (LOS_ListEmpty(listObject)) {
break;
}
sortList = LOS_DL_LIST_ENTRY(listObject->pstNext, SortLinkList, sortLinkNode);
}
return needSchedule;
}
/*****************************************************************************
Function : OsSwtmrGetNextTimeout
Description : Get next timeout
Input : None
Output : None
Return : Count of the Timer list
*****************************************************************************/
LITE_OS_SEC_TEXT UINT32 OsSwtmrGetNextTimeout(VOID)
{
UINT32 intSave = LOS_IntLock();
UINT32 ticks = OsSortLinkGetNextExpireTime(g_swtmrSortLinkList);
LOS_IntRestore(intSave);
return ticks;
}
LITE_OS_SEC_TEXT UINT32 OsSwtmrTimeGet(const SWTMR_CTRL_S *swtmr)
{
return OsSortLinkGetTargetExpireTime(OsGetCurrSchedTimeCycle(), &swtmr->stSortList);
}
/*****************************************************************************
Function : OsSwtmrInit
Description : Initializes Software Timer
Input : None
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 OsSwtmrInit(VOID)
{
UINT32 size;
UINT16 index;
UINT32 ret;
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
// Ignore the return code when matching CSEC rule 6.6(1).
(VOID)memset_s((VOID *)g_swtmrAlignID, sizeof(SwtmrAlignData) * LOSCFG_BASE_CORE_SWTMR_LIMIT,
0, sizeof(SwtmrAlignData) * LOSCFG_BASE_CORE_SWTMR_LIMIT);
#endif
size = sizeof(SWTMR_CTRL_S) * LOSCFG_BASE_CORE_SWTMR_LIMIT;
SWTMR_CTRL_S *swtmr = (SWTMR_CTRL_S *)LOS_MemAlloc(m_aucSysMem0, size);
if (swtmr == NULL) {
return LOS_ERRNO_SWTMR_NO_MEMORY;
}
// Ignore the return code when matching CSEC rule 6.6(3).
(VOID)memset_s((VOID *)swtmr, size, 0, size);
g_swtmrCBArray = swtmr;
g_swtmrFreeList = swtmr;
swtmr->usTimerID = 0;
SWTMR_CTRL_S *temp = swtmr;
swtmr++;
for (index = 1; index < LOSCFG_BASE_CORE_SWTMR_LIMIT; index++, swtmr++) {
swtmr->usTimerID = index;
temp->pstNext = swtmr;
temp = swtmr;
}
ret = LOS_QueueCreate((CHAR *)NULL, OS_SWTMR_HANDLE_QUEUE_SIZE,
&g_swtmrHandlerQueue, 0, sizeof(SwtmrHandlerItem));
if (ret != LOS_OK) {
(VOID)LOS_MemFree(m_aucSysMem0, swtmr);
return LOS_ERRNO_SWTMR_QUEUE_CREATE_FAILED;
}
ret = OsSwtmrTaskCreate();
if (ret != LOS_OK) {
(VOID)LOS_MemFree(m_aucSysMem0, swtmr);
return LOS_ERRNO_SWTMR_TASK_CREATE_FAILED;
}
g_swtmrSortLinkList = OsGetSortLinkAttribute(OS_SORT_LINK_SWTMR);
if (g_swtmrSortLinkList == NULL) {
(VOID)LOS_MemFree(m_aucSysMem0, swtmr);
return LOS_NOK;
}
ret = OsSortLinkInit(g_swtmrSortLinkList);
if (ret != LOS_OK) {
(VOID)LOS_MemFree(m_aucSysMem0, swtmr);
return LOS_NOK;
}
ret = OsSchedSwtmrScanRegister((SchedScan)OsSwtmrScan);
if (ret != LOS_OK) {
(VOID)LOS_MemFree(m_aucSysMem0, swtmr);
return LOS_NOK;
}
return LOS_OK;
}
/*****************************************************************************
Function : LOS_SwtmrCreate
Description : Create software timer
Input : interval
mode
handler
arg
Output : swtmrId
Return : LOS_OK on success or error code on failure
*****************************************************************************/
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
LITE_OS_SEC_TEXT_INIT UINT32 LOS_SwtmrCreate(UINT32 interval,
UINT8 mode,
SWTMR_PROC_FUNC handler,
UINT32 *swtmrId,
UINT32 arg,
UINT8 rouses,
UINT8 sensitive)
#else
LITE_OS_SEC_TEXT_INIT UINT32 LOS_SwtmrCreate(UINT32 interval,
UINT8 mode,
SWTMR_PROC_FUNC handler,
UINT32 *swtmrId,
UINT32 arg)
#endif
{
SWTMR_CTRL_S *swtmr = NULL;
UINT32 intSave;
if (interval == 0) {
return LOS_ERRNO_SWTMR_INTERVAL_NOT_SUITED;
}
if ((mode != LOS_SWTMR_MODE_ONCE) &&
(mode != LOS_SWTMR_MODE_PERIOD) &&
(mode != LOS_SWTMR_MODE_NO_SELFDELETE)) {
return LOS_ERRNO_SWTMR_MODE_INVALID;
}
if (handler == NULL) {
return LOS_ERRNO_SWTMR_PTR_NULL;
}
if (swtmrId == NULL) {
return LOS_ERRNO_SWTMR_RET_PTR_NULL;
}
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
if ((rouses != OS_SWTMR_ROUSES_IGNORE) && (rouses != OS_SWTMR_ROUSES_ALLOW)) {
return OS_ERRNO_SWTMR_ROUSES_INVALID;
}
if ((sensitive != OS_SWTMR_ALIGN_INSENSITIVE) && (sensitive != OS_SWTMR_ALIGN_SENSITIVE)) {
return OS_ERRNO_SWTMR_ALIGN_INVALID;
}
#endif
intSave = LOS_IntLock();
if (g_swtmrFreeList == NULL) {
LOS_IntRestore(intSave);
return LOS_ERRNO_SWTMR_MAXSIZE;
}
swtmr = g_swtmrFreeList;
g_swtmrFreeList = swtmr->pstNext;
LOS_IntRestore(intSave);
swtmr->pfnHandler = handler;
swtmr->ucMode = mode;
swtmr->uwInterval = interval;
swtmr->pstNext = (SWTMR_CTRL_S *)NULL;
swtmr->uwCount = 0;
swtmr->uwArg = arg;
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
swtmr->ucRouses = rouses;
swtmr->ucSensitive = sensitive;
#endif
swtmr->ucState = OS_SWTMR_STATUS_CREATED;
*swtmrId = swtmr->usTimerID;
SET_SORTLIST_VALUE(&swtmr->stSortList, OS_SORT_LINK_INVALID_TIME);
return LOS_OK;
}
/*****************************************************************************
Function : LOS_SwtmrStart
Description : Start software timer
Input : swtmrId ------- Software timer ID
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT UINT32 LOS_SwtmrStart(UINT32 swtmrId)
{
UINT32 intSave;
UINT32 ret = LOS_OK;
if (swtmrId >= OS_SWTMR_MAX_TIMERID) {
return LOS_ERRNO_SWTMR_ID_INVALID;
}
intSave = LOS_IntLock();
SWTMR_CTRL_S *swtmr = g_swtmrCBArray + swtmrId % LOSCFG_BASE_CORE_SWTMR_LIMIT;
if (swtmr->usTimerID != swtmrId) {
LOS_IntRestore(intSave);
return LOS_ERRNO_SWTMR_ID_INVALID;
}
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
if ((swtmr->ucSensitive == OS_SWTMR_ALIGN_INSENSITIVE) && (swtmr->ucMode == LOS_SWTMR_MODE_PERIOD)) {
UINT32 swtmrAlignIdIndex = swtmr->usTimerID % LOSCFG_BASE_CORE_SWTMR_LIMIT;
g_swtmrAlignID[swtmrAlignIdIndex].canAlign = 1;
if ((swtmr->uwInterval % LOS_COMMON_DIVISOR) == 0) {
g_swtmrAlignID[swtmrAlignIdIndex].canMultiple = 1;
g_swtmrAlignID[swtmrAlignIdIndex].times = swtmr->uwInterval / LOS_COMMON_DIVISOR;
}
}
#endif
switch (swtmr->ucState) {
case OS_SWTMR_STATUS_UNUSED:
ret = LOS_ERRNO_SWTMR_NOT_CREATED;
break;
case OS_SWTMR_STATUS_TICKING:
OsSwtmrStop(swtmr);
/* fall through */
case OS_SWTMR_STATUS_CREATED:
OsSwtmrStart(swtmr);
break;
default:
ret = LOS_ERRNO_SWTMR_STATUS_INVALID;
break;
}
LOS_IntRestore(intSave);
return ret;
}
/*****************************************************************************
Function : LOS_SwtmrStop
Description : Stop software timer
Input : swtmrId ------- Software timer ID
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT UINT32 LOS_SwtmrStop(UINT32 swtmrId)
{
SWTMR_CTRL_S *swtmr = NULL;
UINT32 intSave;
UINT16 swtmrCbId;
UINT32 ret = LOS_OK;
if (swtmrId >= OS_SWTMR_MAX_TIMERID) {
return LOS_ERRNO_SWTMR_ID_INVALID;
}
intSave = LOS_IntLock();
swtmrCbId = swtmrId % LOSCFG_BASE_CORE_SWTMR_LIMIT;
swtmr = g_swtmrCBArray + swtmrCbId;
if (swtmr->usTimerID != swtmrId) {
LOS_IntRestore(intSave);
return LOS_ERRNO_SWTMR_ID_INVALID;
}
switch (swtmr->ucState) {
case OS_SWTMR_STATUS_UNUSED:
ret = LOS_ERRNO_SWTMR_NOT_CREATED;
break;
case OS_SWTMR_STATUS_CREATED:
ret = LOS_ERRNO_SWTMR_NOT_STARTED;
break;
case OS_SWTMR_STATUS_TICKING:
OsSwtmrStop(swtmr);
break;
default:
ret = LOS_ERRNO_SWTMR_STATUS_INVALID;
break;
}
LOS_IntRestore(intSave);
return ret;
}
LITE_OS_SEC_TEXT UINT32 LOS_SwtmrTimeGet(UINT32 swtmrId, UINT32 *tick)
{
SWTMR_CTRL_S *swtmr = NULL;
UINT32 intSave;
UINT32 ret = LOS_OK;
UINT16 swtmrCbId;
if (swtmrId >= OS_SWTMR_MAX_TIMERID) {
return LOS_ERRNO_SWTMR_ID_INVALID;
}
if (tick == NULL) {
return LOS_ERRNO_SWTMR_TICK_PTR_NULL;
}
intSave = LOS_IntLock();
swtmrCbId = swtmrId % LOSCFG_BASE_CORE_SWTMR_LIMIT;
swtmr = g_swtmrCBArray + swtmrCbId;
if (swtmr->usTimerID != swtmrId) {
LOS_IntRestore(intSave);
return LOS_ERRNO_SWTMR_ID_INVALID;
}
switch (swtmr->ucState) {
case OS_SWTMR_STATUS_UNUSED:
ret = LOS_ERRNO_SWTMR_NOT_CREATED;
break;
case OS_SWTMR_STATUS_CREATED:
ret = LOS_ERRNO_SWTMR_NOT_STARTED;
break;
case OS_SWTMR_STATUS_TICKING:
*tick = OsSwtmrTimeGet(swtmr);
break;
default:
ret = LOS_ERRNO_SWTMR_STATUS_INVALID;
break;
}
LOS_IntRestore(intSave);
return ret;
}
/*****************************************************************************
Function : LOS_SwtmrDelete
Description : Delete software timer
Input : swtmrId ------- Software timer ID
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT UINT32 LOS_SwtmrDelete(UINT32 swtmrId)
{
SWTMR_CTRL_S *swtmr = NULL;
UINT32 intSave;
UINT32 ret = LOS_OK;
UINT16 swtmrCbId;
if (swtmrId >= OS_SWTMR_MAX_TIMERID) {
return LOS_ERRNO_SWTMR_ID_INVALID;
}
intSave = LOS_IntLock();
swtmrCbId = swtmrId % LOSCFG_BASE_CORE_SWTMR_LIMIT;
swtmr = g_swtmrCBArray + swtmrCbId;
if (swtmr->usTimerID != swtmrId) {
LOS_IntRestore(intSave);
return LOS_ERRNO_SWTMR_ID_INVALID;
}
switch (swtmr->ucState) {
case OS_SWTMR_STATUS_UNUSED:
ret = LOS_ERRNO_SWTMR_NOT_CREATED;
break;
case OS_SWTMR_STATUS_TICKING:
OsSwtmrStop(swtmr);
/* fall through */
case OS_SWTMR_STATUS_CREATED:
OsSwtmrDelete(swtmr);
break;
default:
ret = LOS_ERRNO_SWTMR_STATUS_INVALID;
break;
}
LOS_IntRestore(intSave);
return ret;
}
#endif /* (LOSCFG_BASE_CORE_SWTMR == 1) */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,180 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "los_tick.h"
#include "los_config.h"
#include "los_task.h"
#include "los_swtmr.h"
#include "los_sched.h"
LITE_OS_SEC_BSS UINT32 g_ticksPerSec;
LITE_OS_SEC_BSS UINT32 g_uwCyclePerSec;
LITE_OS_SEC_BSS UINT32 g_cyclesPerTick;
LITE_OS_SEC_BSS UINT32 g_sysClock;
#if (LOSCFG_BASE_CORE_TICK_HW_TIME == 1)
//extern VOID platform_tick_handler(VOID);
#endif
LITE_OS_SEC_TEXT VOID OsTickHandler(VOID)
{
#if (LOSCFG_BASE_CORE_TICK_WTIMER == 0)
OsSchedUpdateSchedTimeBase();
#endif
#if (LOSCFG_BASE_CORE_TICK_HW_TIME == 1)
// platform_tick_handler();
#endif
LOS_SchedTickHandler();
}
UINT64 LOS_SysCycleGet(VOID)
{
return OsGetCurrSchedTimeCycle();
}
/*****************************************************************************
Function : LOS_TickCountGet
Description : get current tick
Input : None
Output : None
Return : current tick
*****************************************************************************/
LITE_OS_SEC_TEXT_MINOR UINT64 LOS_TickCountGet(VOID)
{
return OsGetCurrSchedTimeCycle() / OS_CYCLE_PER_TICK;
}
/*****************************************************************************
Function : LOS_CyclePerTickGet
Description : Get System cycle number corresponding to each tick
Input : None
Output : None
Return : cycle number corresponding to each tick
*****************************************************************************/
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_CyclePerTickGet(VOID)
{
return g_cyclesPerTick;
}
/*****************************************************************************
Function : LOS_MS2Tick
Description : milliseconds convert to Tick
Input : millisec ---------- milliseconds
Output : None
Return : Tick
*****************************************************************************/
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_MS2Tick(UINT32 millisec)
{
if (millisec == OS_NULL_INT) {
return OS_NULL_INT;
}
return ((UINT64)millisec * LOSCFG_BASE_CORE_TICK_PER_SECOND) / OS_SYS_MS_PER_SECOND;
}
/*****************************************************************************
Function : LOS_Tick2MS
Description : Tick convert to milliseconds
Input : ticks ---------- ticks
Output : None
Return : milliseconds
*****************************************************************************/
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_Tick2MS(UINT32 ticks)
{
return ((UINT64)ticks * OS_SYS_MS_PER_SECOND) / LOSCFG_BASE_CORE_TICK_PER_SECOND;
}
/*****************************************************************************
Function : OsCpuTick2MS
Description : cycle convert to milliseconds
Input : cpuTick ---------- cycle
Output : msHi ---------- High 32 milliseconds
msLo ---------- Low 32 milliseconds
Return : LOS_OK on success ,or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 OsCpuTick2MS(CpuTick *cpuTick, UINT32 *msHi, UINT32 *msLo)
{
UINT64 tmpCpuTick;
DOUBLE temp;
if ((cpuTick == NULL) || (msHi == NULL) || (msLo == NULL)) {
return LOS_ERRNO_SYS_PTR_NULL;
}
if (g_sysClock == 0) {
return LOS_ERRNO_SYS_CLOCK_INVALID;
}
tmpCpuTick = ((UINT64)cpuTick->cntHi << OS_SYS_MV_32_BIT) | cpuTick->cntLo;
temp = tmpCpuTick / ((DOUBLE)g_sysClock / OS_SYS_MS_PER_SECOND);
tmpCpuTick = (UINT64)temp;
*msLo = (UINT32)tmpCpuTick;
*msHi = (UINT32)(tmpCpuTick >> OS_SYS_MV_32_BIT);
return LOS_OK;
}
/*****************************************************************************
Function : OsCpuTick2US
Description : cycle convert to Microsecond
Input : cpuTick ---------- cycle
Output : usHi ---------- High 32 Microsecond
usLo ---------- Low 32 Microsecond
Return : LOS_OK on success ,or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 OsCpuTick2US(CpuTick *cpuTick, UINT32 *usHi, UINT32 *usLo)
{
UINT64 tmpCpuTick;
DOUBLE temp;
if ((cpuTick == NULL) || (usHi == NULL) || (usLo == NULL)) {
return LOS_ERRNO_SYS_PTR_NULL;
}
if (g_sysClock == 0) {
return LOS_ERRNO_SYS_CLOCK_INVALID;
}
tmpCpuTick = ((UINT64)cpuTick->cntHi << OS_SYS_MV_32_BIT) | cpuTick->cntLo;
temp = tmpCpuTick / ((DOUBLE)g_sysClock / OS_SYS_US_PER_SECOND);
tmpCpuTick = (UINT64)temp;
*usLo = (UINT32)tmpCpuTick;
*usHi = (UINT32)(tmpCpuTick >> OS_SYS_MV_32_BIT);
return LOS_OK;
}
@@ -0,0 +1,317 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "los_membox.h"
#include "securec.h"
#include "los_interrupt.h"
#include "los_context.h"
#include "los_debug.h"
#include "los_task.h"
/* The magic length is 32 bits, the lower 8 bits are used to save the owner task ID,
and the other 24 bits are used to set the magic number for verification. */
#define OS_MEMBOX_MAGIC 0xa55a5a00
#define OS_MEMBOX_TASKID_BITS 8
#define OS_MEMBOX_MAX_TASKID ((1 << OS_MEMBOX_TASKID_BITS) - 1)
#define OS_MEMBOX_TASKID_GET(addr) (((UINTPTR)(addr)) & OS_MEMBOX_MAX_TASKID)
STATIC INLINE VOID OsMemBoxSetMagic(LOS_MEMBOX_NODE *node)
{
UINT8 taskID = (UINT8)LOS_CurTaskIDGet();
node->pstNext = (LOS_MEMBOX_NODE *)(OS_MEMBOX_MAGIC | taskID);
}
STATIC INLINE UINT32 OsMemBoxCheckMagic(LOS_MEMBOX_NODE *node)
{
UINT32 taskID = OS_MEMBOX_TASKID_GET(node->pstNext);
if (taskID > (LOSCFG_BASE_CORE_TSK_LIMIT + 1)) {
return LOS_NOK;
} else {
return (node->pstNext == (LOS_MEMBOX_NODE *)(OS_MEMBOX_MAGIC | taskID)) ? LOS_OK : LOS_NOK;
}
}
#define OS_MEMBOX_USER_ADDR(addr) \
((VOID *)((UINT8 *)(addr) + OS_MEMBOX_NODE_HEAD_SIZE))
#define OS_MEMBOX_NODE_ADDR(addr) \
((LOS_MEMBOX_NODE *)(VOID *)((UINT8 *)(addr) - OS_MEMBOX_NODE_HEAD_SIZE))
#define MEMBOX_LOCK(state) ((state) = HalIntLock())
#define MEMBOX_UNLOCK(state) HalIntRestore(state)
STATIC INLINE UINT32 OsCheckBoxMem(const LOS_MEMBOX_INFO *boxInfo, const VOID *node)
{
UINT32 offset;
if (boxInfo->uwBlkSize == 0) {
return LOS_NOK;
}
offset = (UINT32)((UINTPTR)node - (UINTPTR)(boxInfo + 1));
if ((offset % boxInfo->uwBlkSize) != 0) {
return LOS_NOK;
}
if ((offset / boxInfo->uwBlkSize) >= boxInfo->uwBlkNum) {
return LOS_NOK;
}
return OsMemBoxCheckMagic((LOS_MEMBOX_NODE *)node);
}
#if (LOSCFG_PLATFORM_EXC == 1)
STATIC LOS_MEMBOX_INFO *g_memBoxHead = NULL;
STATIC VOID OsMemBoxAdd(VOID *pool)
{
LOS_MEMBOX_INFO *nextPool = g_memBoxHead;
LOS_MEMBOX_INFO *curPool = NULL;
while (nextPool != NULL) {
curPool = nextPool;
nextPool = nextPool->nextMemBox;
}
if (curPool == NULL) {
g_memBoxHead = pool;
} else {
curPool->nextMemBox = pool;
}
((LOS_MEMBOX_INFO *)pool)->nextMemBox = NULL;
}
#endif
UINT32 LOS_MemboxInit(VOID *pool, UINT32 poolSize, UINT32 blkSize)
{
LOS_MEMBOX_INFO *boxInfo = (LOS_MEMBOX_INFO *)pool;
LOS_MEMBOX_NODE *node = NULL;
UINT32 index;
UINT32 intSave;
if (pool == NULL) {
return LOS_NOK;
}
if (blkSize == 0) {
return LOS_NOK;
}
if (poolSize < sizeof(LOS_MEMBOX_INFO)) {
return LOS_NOK;
}
MEMBOX_LOCK(intSave);
boxInfo->uwBlkSize = LOS_MEMBOX_ALIGNED(blkSize + OS_MEMBOX_NODE_HEAD_SIZE);
boxInfo->uwBlkNum = (poolSize - sizeof(LOS_MEMBOX_INFO)) / boxInfo->uwBlkSize;
boxInfo->uwBlkCnt = 0;
if (boxInfo->uwBlkNum == 0) {
MEMBOX_UNLOCK(intSave);
return LOS_NOK;
}
node = (LOS_MEMBOX_NODE *)(boxInfo + 1);
boxInfo->stFreeList.pstNext = node;
for (index = 0; index < boxInfo->uwBlkNum - 1; ++index) {
node->pstNext = OS_MEMBOX_NEXT(node, boxInfo->uwBlkSize);
node = node->pstNext;
}
node->pstNext = NULL;
#if (LOSCFG_PLATFORM_EXC == 1)
OsMemBoxAdd(pool);
#endif
MEMBOX_UNLOCK(intSave);
return LOS_OK;
}
VOID *LOS_MemboxAlloc(VOID *pool)
{
LOS_MEMBOX_INFO *boxInfo = (LOS_MEMBOX_INFO *)pool;
LOS_MEMBOX_NODE *node = NULL;
LOS_MEMBOX_NODE *nodeTmp = NULL;
UINT32 intSave;
if (pool == NULL) {
return NULL;
}
MEMBOX_LOCK(intSave);
node = &(boxInfo->stFreeList);
if (node->pstNext != NULL) {
nodeTmp = node->pstNext;
node->pstNext = nodeTmp->pstNext;
OsMemBoxSetMagic(nodeTmp);
boxInfo->uwBlkCnt++;
}
MEMBOX_UNLOCK(intSave);
return (nodeTmp == NULL) ? NULL : OS_MEMBOX_USER_ADDR(nodeTmp);
}
UINT32 LOS_MemboxFree(VOID *pool, VOID *box)
{
LOS_MEMBOX_INFO *boxInfo = (LOS_MEMBOX_INFO *)pool;
UINT32 ret = LOS_NOK;
UINT32 intSave;
if ((pool == NULL) || (box == NULL)) {
return LOS_NOK;
}
MEMBOX_LOCK(intSave);
do {
LOS_MEMBOX_NODE *node = OS_MEMBOX_NODE_ADDR(box);
if (OsCheckBoxMem(boxInfo, node) != LOS_OK) {
break;
}
node->pstNext = boxInfo->stFreeList.pstNext;
boxInfo->stFreeList.pstNext = node;
boxInfo->uwBlkCnt--;
ret = LOS_OK;
} while (0);
MEMBOX_UNLOCK(intSave);
return ret;
}
VOID LOS_MemboxClr(VOID *pool, VOID *box)
{
LOS_MEMBOX_INFO *boxInfo = (LOS_MEMBOX_INFO *)pool;
if ((pool == NULL) || (box == NULL)) {
return;
}
(VOID)memset_s(box, (boxInfo->uwBlkSize - OS_MEMBOX_NODE_HEAD_SIZE), 0,
(boxInfo->uwBlkSize - OS_MEMBOX_NODE_HEAD_SIZE));
}
VOID LOS_ShowBox(VOID *pool)
{
UINT32 index;
UINT32 intSave;
LOS_MEMBOX_INFO *boxInfo = (LOS_MEMBOX_INFO *)pool;
LOS_MEMBOX_NODE *node = NULL;
if (pool == NULL) {
return;
}
MEMBOX_LOCK(intSave);
PRINT_INFO("membox(%p, 0x%x, 0x%x):\r\n", pool, boxInfo->uwBlkSize, boxInfo->uwBlkNum);
PRINT_INFO("free node list:\r\n");
for (node = boxInfo->stFreeList.pstNext, index = 0; node != NULL;
node = node->pstNext, ++index) {
PRINT_INFO("(%u, %p)\r\n", index, node);
}
PRINT_INFO("all node list:\r\n");
node = (LOS_MEMBOX_NODE *)(boxInfo + 1);
for (index = 0; index < boxInfo->uwBlkNum; ++index, node = OS_MEMBOX_NEXT(node, boxInfo->uwBlkSize)) {
PRINT_INFO("(%u, %p, %p)\r\n", index, node, node->pstNext);
}
MEMBOX_UNLOCK(intSave);
}
UINT32 LOS_MemboxStatisticsGet(const VOID *boxMem, UINT32 *maxBlk,
UINT32 *blkCnt, UINT32 *blkSize)
{
if ((boxMem == NULL) || (maxBlk == NULL) || (blkCnt == NULL) || (blkSize == NULL)) {
return LOS_NOK;
}
*maxBlk = ((OS_MEMBOX_S *)boxMem)->uwBlkNum;
*blkCnt = ((OS_MEMBOX_S *)boxMem)->uwBlkCnt;
*blkSize = ((OS_MEMBOX_S *)boxMem)->uwBlkSize;
return LOS_OK;
}
#if (LOSCFG_PLATFORM_EXC == 1)
STATIC VOID OsMemboxExcInfoGetSub(const LOS_MEMBOX_INFO *pool, MemInfoCB *memExcInfo)
{
LOS_MEMBOX_NODE *node = NULL;
UINTPTR poolStart, poolEnd;
UINT32 index;
UINT32 intSave;
(VOID)memset_s(memExcInfo, sizeof(MemInfoCB), 0, sizeof(MemInfoCB));
MEMBOX_LOCK(intSave);
memExcInfo->type = MEM_MANG_MEMBOX;
memExcInfo->startAddr = (UINTPTR)pool;
memExcInfo->size = pool->uwBlkNum * pool->uwBlkSize;
memExcInfo->blockSize = pool->uwBlkSize;
memExcInfo->size = pool->uwBlkNum; /* Block num */
memExcInfo->free = pool->uwBlkNum - pool->uwBlkCnt;
poolStart = (UINTPTR)pool;
poolEnd = poolStart + pool->uwBlkSize * pool->uwBlkNum + sizeof(LOS_MEMBOX_INFO);
node = (LOS_MEMBOX_NODE *)(pool + 1);
for (index = 0; index < pool->uwBlkNum; ++index, node = OS_MEMBOX_NEXT(node, pool->uwBlkSize)) {
if (((UINTPTR)node < poolStart) || ((UINTPTR)node >= poolEnd)) {
if (OsMemBoxCheckMagic(node)) {
memExcInfo->errorAddr = (UINT32)(UINTPTR)((CHAR *)node + OS_MEMBOX_NODE_HEAD_SIZE);
memExcInfo->errorLen = pool->uwBlkSize - OS_MEMBOX_NODE_HEAD_SIZE;
memExcInfo->errorOwner = OS_MEMBOX_TASKID_GET(node->pstNext);
break;
}
}
}
MEMBOX_UNLOCK(intSave);
}
UINT32 OsMemboxExcInfoGet(UINT32 memNumMax, MemInfoCB *memExcInfo)
{
LOS_MEMBOX_INFO *memBox = g_memBoxHead;
UINT32 count = 0;
UINT8 *buffer = (UINT8 *)memExcInfo;
while (memBox != NULL) {
OsMemboxExcInfoGetSub(memBox, (MemInfoCB *)buffer);
count++;
buffer += sizeof(MemInfoCB);
if (count >= memNumMax) {
break;
}
memBox = memBox->nextMemBox;
}
return count;
}
#endif
File diff suppressed because it is too large Load Diff