diff --git a/build_version.h b/build_version.h index 60d61df..5d827a4 100644 --- a/build_version.h +++ b/build_version.h @@ -12,11 +12,11 @@ extern "C" { #endif -#define ATOS_BUILD_TIME "2024-11-09,17:36" -#define ATOS_COMMIT_HEAD_ID "1b1d7b46ad853be05c9457cb5678884d09469fa3" +#define ATOS_BUILD_TIME "2025-01-04,14:34" +#define ATOS_COMMIT_HEAD_ID "0f76bfa999392681d06420ef38fe42551267c450" #define ATOS_VERSION_MAJOR_NUMBER (1u) #define ATOS_VERSION_MINOR_NUMBER (7u) -#define ATOS_VERSION_PATCH_NUMBER (6u) +#define ATOS_VERSION_PATCH_NUMBER (7u) #define ATOS_VERSION_MAJOR_NUMBER_MASK (0x03FFu) #define ATOS_VERSION_MAJOR_NUMBER_POS (22u) diff --git a/clock/clock_native_gcc.c b/clock/clock_native_gcc.c index 2766b13..f7cd7f9 100644 --- a/clock/clock_native_gcc.c +++ b/clock/clock_native_gcc.c @@ -4,14 +4,9 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "clock_tick.h" #include "configuration.h" -#ifdef __cplusplus -extern "C" { -#endif - /** * Data structure for location time clock */ @@ -137,6 +132,3 @@ void clock_time_init(time_report_handler_t pTime_function) /* Nothing need to do for kernel cmake sample build. */ } -#ifdef __cplusplus -} -#endif diff --git a/clock/clock_systick.c b/clock/clock_systick.c index 7e9450c..fc07a49 100644 --- a/clock/clock_systick.c +++ b/clock/clock_systick.c @@ -4,16 +4,11 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "clock_tick.h" #include "configuration.h" #include "arch.h" #include "ktype.h" -#ifdef __cplusplus -extern "C" { -#endif - /* Convert the microsecond to clock count */ #define _CONVERT_MICROSENCOND_TO_COUNT(us) ((u32_t)(us) * (PORTAL_SYSTEM_CORE_CLOCK_MHZ)-1u) @@ -259,6 +254,3 @@ void clock_time_init(time_report_handler_t pTime_function) SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk; } -#ifdef __cplusplus -} -#endif diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index a013377..94fe429 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -6,7 +6,7 @@ include_directories(${KERNEL_PATH}/include/kernel) target_sources(kernel_include PUBLIC ${KERNEL_PATH}/include/arch.h - + ${KERNEL_PATH}/include/kernel/at_rtos.h ${KERNEL_PATH}/include/kernel/compiler.h ${KERNEL_PATH}/include/kernel/configuration.h @@ -16,6 +16,6 @@ target_sources(kernel_include ${KERNEL_PATH}/include/kernel/postcode.h ${KERNEL_PATH}/include/kernel/timer.h ${KERNEL_PATH}/include/kernel/trace.h - ${KERNEL_PATH}/include/kernel/typedef.h + ${KERNEL_PATH}/include/kernel/type_def.h ${KERNEL_PATH}/include/kernel/ktype.h ) diff --git a/include/clock_tick.h b/include/clock_tick.h index 7e285c8..ea18722 100644 --- a/include/clock_tick.h +++ b/include/clock_tick.h @@ -4,15 +4,10 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #ifndef _CLOCK_TICK_H_ #define _CLOCK_TICK_H_ -#include "typedef.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "type_def.h" /** * Function pointer structure for the clock tells how much time has passed. @@ -30,8 +25,4 @@ void clock_time_enable(void); void clock_time_disable(void); void clock_time_init(time_report_handler_t pTime_function); -#ifdef __cplusplus -} -#endif - #endif /* _CLOCK_TICK_H_ */ diff --git a/include/kernel/at_rtos.h b/include/kernel/at_rtos.h index abeeaca..1396604 100644 --- a/include/kernel/at_rtos.h +++ b/include/kernel/at_rtos.h @@ -4,7 +4,6 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #ifndef _AT_RTOS_H_ #define _AT_RTOS_H_ @@ -13,10 +12,7 @@ #include "configuration.h" #include "postcode.h" #include "trace.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "init.h" #define OS_PC_OK (PC_OS_OK) #define OS_PC_TIMEOUT (PC_OS_WAIT_TIMEOUT) @@ -24,11 +20,12 @@ extern "C" { #define OS_PC_UNAVAILABLE (PC_OS_WAIT_UNAVAILABLE) #define OS_ID_INVALID (OS_INVALID_ID_VAL) + #define OS_SEM_BINARY (1u) -typedef u32_t os_timeout_t; #define OS_TIME_NOWAIT (OS_TIME_NOWAIT_VAL) #define OS_TIME_WAIT_FOREVER (OS_TIME_FOREVER_VAL) +typedef u32_t os_timeout_t; typedef enum { OS_TIMER_CTRL_ONCE = (TIMER_CTRL_ONCE_VAL), @@ -36,20 +33,35 @@ typedef enum { OS_TIMER_CTRL_TEMPORARY = (TIMER_CTRL_TEMPORARY_VAL), } os_timer_ctrl_t; +typedef struct os_id os_thread_id_t; +typedef struct os_id os_timer_id_t; +typedef struct os_id os_sem_id_t; +typedef struct os_id os_mutex_id_t; +typedef struct os_id os_evt_id_t; +typedef struct os_id os_msgq_id_t; +typedef struct os_id os_pool_id_t; +typedef struct os_id os_publish_id_t; +typedef struct os_id os_subscribe_id_t; + +typedef struct evt_val os_evt_val_t; + #define OS_PRIORITY_INVALID (OS_PRIOTITY_INVALID_LEVEL) -#define OS_PRIORITY_APPLICATION_LOWEST (OS_PRIOTITY_LOWEST_LEVEL - 1u) -#define OS_PRIORITY_APPLICATION_HIGHEST (OS_PRIOTITY_HIGHEST_LEVEL + 1u) -#define OS_PRIORITY_NUM_SET(n) ((n <= OS_PRIORITY_APPLICATION_LOWEST) ? (OS_PRIORITY_APPLICATION_LOWEST - n) : (OS_PRIORITY_INVALID)) - -#define OS_THREAD_DEFINE(thread_name, stack_size, prior) \ - static os_thread_symbol_t thread_name[((u32_t)(stack_size) / sizeof(u32_t))] = { \ - [0] = {.size = stack_size}, \ - [1] = {.priority = prior}, \ - [2] = {.pName = #thread_name}, \ - }; \ - BUILD_ASSERT(((stack_size) >= STACK_SIZE_MINIMUM), "The thread stack size must be higher than STACK_SIZE_MINIMUM"); \ - BUILD_ASSERT((((prior) >= OS_PRIORITY_APPLICATION_HIGHEST) || ((prior) <= OS_PRIORITY_APPLICATION_LOWEST)), \ - "The thread priority is out of the system design") +#define OS_PRIORITY_APPLICATION_HIGHEST (OS_PRIORITY_APPLICATION_HIGHEST_LEVEL) +#define OS_PRIORITY_APPLICATION_LOWEST (OS_PRIORITY_APPLICATION_LOWEST_LEVEL) + +#define OS_PRIORITY_PREEMPT_SET(p) (p) +#define OS_PRIORITY_COOPERATION_SET(c) (-(OS_PRIOTITY_COOPERATION_NUM - (c))) + +#define OS_STACK_INIT(name, size) STACK_STATIC_VALUE_DEFINE(name, size) +#define OS_THREAD_INIT(id_name, priority, stack_size, pEntryFn) INIT_OS_THREAD_DEFINE(id_name, priority, stack_size, pEntryFn) +#define OS_TIMER_INIT(id_name, pEntryFunc) INIT_OS_TIMER_DEFINE(id_name, pEntryFunc) +#define OS_SEMAPHORE_INIT(id_name, remain, limit) INIT_OS_SEMAPHORE_DEFINE(id_name, remain, limit) +#define OS_MUTEX_INIT(id_name) INIT_OS_MUTEX_DEFINE(id_name) +#define OS_EVT_INIT(id_name, anyMask, modeMask, dirMask, init) INIT_OS_EVT_DEFINE(id_name, anyMask, modeMask, dirMask, init) +#define OS_MSGQ_INIT(id_name, pBufAddr, len, num) INIT_OS_MSGQ_DEFINE(id_name, pBufAddr, len, num) +#define OS_POOL_INIT(id_name, pMemAddr, len, num) INIT_OS_POOL_DEFINE(id_name, pMemAddr, len, num) +#define OS_SUBSCRIBE_INIT(id_name, pDataAddr, size) INIT_OS_SUBSCRIBE_DEFINE(id_name, pDataAddr, size) +#define OS_PUBLISH_INIT(id_name, pDataAddr, size) INIT_OS_PUBLISH_DEFINE(id_name, pDataAddr, size) /** * @brief Initialize a thread, and put it to pending list that are ready to run. @@ -61,42 +73,14 @@ typedef enum { * @param pName The thread name. * * @return The value of thread unique id. - ** - * demo usage: - *#include "at_rtos.h" - * - * OS_THREAD_DEFINE(demo_thread, 512, 5); - * - * void thread_demo_function(void) - * { - * while(1) { - * os.thread_sleep(1000u); - * } - * } - * - * int main(void) - * { - * os_thread_id_t id = os_thread_init(demo_thread, thread_demo_function); - * if (os_id_is_invalid(id)) { - * printf("Thread %s init failed\n", id.pName); - * } - * ... - * } - * */ -static inline os_thread_id_t os_thread_init(os_thread_symbol_t *pThread_symbol, pThread_entryFunc_t pEntryFun) +static inline os_thread_id_t os_thread_init(u32_t *pStackAddr, u32_t size, i16_t priority, pThread_entryFunc_t pEntryFun, + const char_t *pName) { - extern u32_t _impl_thread_os_id_to_number(os_id_t id); - extern os_id_t _impl_thread_init(void (*pThread_entryFunc_t)(void), u32_t *pAddress, u32_t size, u16_t priority, const char_t *pName); + extern u32_t _impl_thread_init(pThread_entryFunc_t pEntryFun, u32_t * pAddress, u32_t size, i16_t priority, const char_t *pName); os_thread_id_t id = {0u}; - u32_t *pStackAddress = (u32_t *)pThread_symbol; - u32_t stackSize = (u32_t)pThread_symbol[0].size; - u8_t priority = (u8_t)pThread_symbol[1].priority; - const char_t *pName = (const char_t *)pThread_symbol[2].pName; - - id.val = _impl_thread_init(pEntryFun, pStackAddress, stackSize, priority, pName); - id.number = _impl_thread_os_id_to_number(id.val); + id.u32_val = _impl_thread_init(pEntryFun, pStackAddr, size, priority, pName); id.pName = pName; return id; @@ -108,15 +92,6 @@ static inline os_thread_id_t os_thread_init(os_thread_symbol_t *pThread_symbol, * @param timeout_ms The time user defined. * * @return The result of thread sleep operation. - ** - * demo usage: - * - * void thread_demo_function(void) - * { - * while(1) { - * os_thread_sleep(1000); // Put the thread to sleep mode 1 sec. - * } - * } */ static inline i32p_t os_thread_sleep(u32_t ms) { @@ -131,16 +106,12 @@ static inline i32p_t os_thread_sleep(u32_t ms) * @param id The thread unique id. * * @return The result of thread resume operation. - ** - * demo usage: - * - * os_thread_resume(id); // The variable id created in above thread init demo. */ static inline i32p_t os_thread_resume(os_thread_id_t id) { - extern i32p_t _impl_thread_resume(os_id_t id); + extern i32p_t _impl_thread_resume(u32_t ctx); - return (i32p_t)_impl_thread_resume(id.val); + return (i32p_t)_impl_thread_resume(id.u32_val); } /** @@ -149,16 +120,12 @@ static inline i32p_t os_thread_resume(os_thread_id_t id) * @param id The thread unique id. * * @return The result of thread suspend operation. - ** - * demo usage: - * - * os_thread_suspend(id); // The variable id created in above thread init demo. */ static inline i32p_t os_thread_suspend(os_thread_id_t id) { - extern i32p_t _impl_thread_suspend(os_id_t id); + extern i32p_t _impl_thread_suspend(u32_t ctx); - return (i32p_t)_impl_thread_suspend(id.val); + return (i32p_t)_impl_thread_suspend(id.u32_val); } /** @@ -167,15 +134,6 @@ static inline i32p_t os_thread_suspend(os_thread_id_t id) * @param id The thread unique id. * * @return The result of thread yield operation. - ** - * demo usage: - * - * void thread_demo_function(void) - * { - * while(1) { - * os_thread_yield(); // Put current thread to sleep mode manually. - * } - * } */ static inline i32p_t os_thread_yield(void) { @@ -190,16 +148,24 @@ static inline i32p_t os_thread_yield(void) * @param id The thread unique id. * * @return The result of thread delete operation. - ** - * demo usage: - * - * os_thread_delete(id); // The variable id created in above thread init demo. */ static inline i32p_t os_thread_delete(os_thread_id_t id) { - extern i32p_t _impl_thread_delete(os_id_t id); + extern i32p_t _impl_thread_delete(u32_t ctx); + + return (i32p_t)_impl_thread_delete(id.u32_val); +} + +/** + * @brief Idle thread callback function register. + * + * @param fn The invoke function. + */ +static inline void os_thread_idle_callback_register(const pThread_entryFunc_t loop_fn) +{ + extern void _impl_kthread_idle_user_callback_register(const pThread_entryFunc_t fn); - return (i32p_t)_impl_thread_delete(id.val); + _impl_kthread_idle_user_callback_register(loop_fn); } /** @@ -211,43 +177,32 @@ static inline i32p_t os_thread_delete(os_thread_id_t id) * @param pName The timer's name, it supported NULL pointer. * * @return The value of the timer unique id. - ** - * demo usage: - * - * void demo_timer_function(void) - * { - * // The function will be called per 1 seconds. - * } - * - * os_timer_id_t id = os_timer_init(demo_timer_function, "demo"); - * if (os_id_is_invalid(id)) { - * printf("Timer %s init failed\n", id.pName); - * } - * ... */ static inline os_timer_id_t os_timer_init(pTimer_callbackFunc_t pEntryFun, const char_t *pName) { - extern u32_t _impl_timer_os_id_to_number(u32_t id); - extern os_id_t _impl_timer_init(pTimer_callbackFunc_t pCallFun, const char_t *pName); + extern u32_t _impl_timer_init(pTimer_callbackFunc_t pCallFun, const char_t *pName); os_timer_id_t id = {0u}; - - id.val = _impl_timer_init(pEntryFun, pName); - id.number = _impl_timer_os_id_to_number(id.val); + id.u32_val = _impl_timer_init(pEntryFun, pName); id.pName = pName; return id; } +/** + * @brief Allocate a temporary timer to run and release it when it stops. + * + * @param pCallFun The timer entry function pointer. + * @param pName The timer's name, it supported NULL pointer. + * + * @return The value of the timer unique id. + */ static inline os_timer_id_t os_timer_automatic(pTimer_callbackFunc_t pEntryFun, const char_t *pName) { - extern u32_t _impl_timer_os_id_to_number(u32_t id); - extern os_id_t _impl_timer_automatic(pTimer_callbackFunc_t pCallFun, const char_t *pName); + extern u32_t _impl_timer_automatic(pTimer_callbackFunc_t pCallFun, const char_t *pName); os_timer_id_t id = {0u}; - - id.val = _impl_timer_automatic(pEntryFun, pName); - id.number = _impl_timer_os_id_to_number(id.val); + id.u32_val = _impl_timer_automatic(pEntryFun, pName); id.pName = pName; return id; @@ -262,19 +217,12 @@ static inline os_timer_id_t os_timer_automatic(pTimer_callbackFunc_t pEntryFun, * @param timeout_ms The timer expired time. * * @return The result of timer start operation. - ** - * demo usage: - * - * i32p_t postcode = os_timer_start(id, FALSE, 1000u); - * PC_IF(postcode, PC_PASS) { - * printf("Timer start successful\n"); - * } */ static inline i32p_t os_timer_start(os_timer_id_t id, os_timer_ctrl_t control, os_timeout_t timeout_ms) { - extern i32p_t _impl_timer_start(os_id_t id, u8_t control, u32_t timeout_ms); + extern i32p_t _impl_timer_start(u32_t ctx, u8_t control, u32_t timeout_ms); - return (i32p_t)_impl_timer_start(id.val, (u8_t)control, (u32_t)timeout_ms); + return (i32p_t)_impl_timer_start(id.u32_val, (u8_t)control, (u32_t)timeout_ms); } /** @@ -283,19 +231,12 @@ static inline i32p_t os_timer_start(os_timer_id_t id, os_timer_ctrl_t control, o * @param id The timer unique id. * * @return The result of timer stop operation. - ** - * demo usage: - * - * i32p_t postcode = os_timer_stop(id); - * PC_IF(postcode, PC_PASS) { - * printf("Timer stop successful\n"); - * } */ static inline i32p_t os_timer_stop(os_timer_id_t id) { - extern i32p_t _impl_timer_stop(os_id_t id); + extern i32p_t _impl_timer_stop(u32_t ctx); - return (i32p_t)_impl_timer_stop(id.val); + return (i32p_t)_impl_timer_stop(id.u32_val); } /** @@ -304,28 +245,18 @@ static inline i32p_t os_timer_stop(os_timer_id_t id) * @param id The timer unique id. * * @return The true result indicates time busy, otherwise is free status. - ** - * demo usage: - * - * if(os_timer_busy(id)) { - * printf("Timer %s is busy\n", id.pName); - * } */ static inline i32p_t os_timer_busy(os_timer_id_t id) { - extern b_t _impl_timer_busy(os_id_t id); + extern b_t _impl_timer_busy(u32_t ctx); - return (i32p_t)_impl_timer_busy(id.val); + return (i32p_t)_impl_timer_busy(id.u32_val); } /** * @brief Get the kernel RTOS system time (ms). * * @return The value of the total system time (ms). - ** - * demo usage: - * - * printf("The system consume time: %d\n", os_timer_system_total_ms()); */ static inline u32_t os_timer_system_total_ms(void) { @@ -342,25 +273,13 @@ static inline u32_t os_timer_system_total_ms(void) * @param pName The semaphore name. * * @return The semaphore unique id. - ** - * demo usage: - * - * // Init a binary semaphore count. - * os_sem_id_t id = os_sem_init(0u, 1u, FALSE, "demo"); - * if (os_id_is_invalid(id)) { - * printf("Semaphore %s init failed\n", id.pName); - * } - * ... */ static inline os_sem_id_t os_sem_init(u8_t remain, u8_t limit, const char_t *pName) { - extern u32_t _impl_semaphore_os_id_to_number(os_id_t id); - extern os_id_t _impl_semaphore_init(u8_t remainCount, u8_t limitCount, const char_t *pName); + extern u32_t _impl_semaphore_init(u8_t remainCount, u8_t limitCount, const char_t *pName); os_sem_id_t id = {0u}; - - id.val = _impl_semaphore_init(remain, limit, pName); - id.number = _impl_semaphore_os_id_to_number(id.val); + id.u32_val = _impl_semaphore_init(remain, limit, pName); id.pName = pName; return id; @@ -372,33 +291,12 @@ static inline os_sem_id_t os_sem_init(u8_t remain, u8_t limit, const char_t *pNa * @param id The semaphore unique id. * * @return The result of the operation. - ** - * demo usage: - * - * i32p_t postcode = os_sem_take(id, 1000u); - * PC_IF(postcode, PC_PASS_INFO) { - * if (postcode == OS_PC_TIMEOUT) { - * printf("Semaphore take wait timeout\n"); - * } else { - * printf("Semaphore take successful\n"); - * } - * } else { - * printf("Semaphore take error: 0x%x\n", postcode); - * } - * - * i32p_t postcode = os_sem_take(id, OS_TIME_WAIT_FOREVER); - * postcode = os_sem_take(id, 1000u); - * PC_IF(postcode, PC_PASS) { - * printf("Semaphore take successful\n"); - * } else { - * printf("Semaphore take error: 0x%x\n", postcode); - * } */ static inline i32p_t os_sem_take(os_sem_id_t id, u32_t timeout_ms) { - extern i32p_t _impl_semaphore_take(os_id_t id, u32_t timeout_ms); + extern i32p_t _impl_semaphore_take(u32_t ctx, u32_t timeout_ms); - return (i32p_t)_impl_semaphore_take(id.val, timeout_ms); + return (i32p_t)_impl_semaphore_take(id.u32_val, timeout_ms); } /** @@ -407,21 +305,12 @@ static inline i32p_t os_sem_take(os_sem_id_t id, u32_t timeout_ms) * @param id The semaphore unique id. * * @return The result of the operation. - ** - * demo usage: - * - * i32p_t postcode = os_sem_give(id); - * PC_IF(postcode, PC_PASS) { - * printf("Semaphore give successful\n"); - * } else { - * printf("Semaphore give error: 0x%x\n", postcode); - * } */ static inline i32p_t os_sem_give(os_sem_id_t id) { - extern u32_t _impl_semaphore_give(os_id_t id); + extern i32p_t _impl_semaphore_give(u32_t ctx); - return (i32p_t)_impl_semaphore_give(id.val); + return (i32p_t)_impl_semaphore_give(id.u32_val); } /** @@ -430,21 +319,12 @@ static inline i32p_t os_sem_give(os_sem_id_t id) * @param id The semaphore unique id. * * @return The result of the operation. - ** - * demo usage: - * - * i32p_t postcode = os_sem_flush(id); - * PC_IF(postcode, PC_PASS) { - * printf("Semaphore flush successful\n"); - * } else { - * printf("Semaphore flush error: 0x%x\n", postcode); - * } */ static inline i32p_t os_sem_flush(os_sem_id_t id) { - extern u32_t _impl_semaphore_flush(os_id_t id); + extern i32p_t _impl_semaphore_flush(u32_t ctx); - return (i32p_t)_impl_semaphore_flush(id.val); + return (i32p_t)_impl_semaphore_flush(id.u32_val); } /** @@ -453,24 +333,13 @@ static inline i32p_t os_sem_flush(os_sem_id_t id) * @param pName The mutex name. * * @return The mutex unique id. - ** - * demo usage: - * - * os_mutex_id_t id = os_mutex_init("demo"); - * if (os_id_is_invalid(id)) { - * printf("Mutex %s init failed\n", id.pName); - * } - * ... */ static inline os_mutex_id_t os_mutex_init(const char_t *pName) { - extern u32_t _impl_mutex_os_id_to_number(os_id_t id); - extern os_id_t _impl_mutex_init(const char_t *pName); + extern u32_t _impl_mutex_init(const char_t *pName); os_mutex_id_t id = {0u}; - - id.val = _impl_mutex_init(pName); - id.number = _impl_mutex_os_id_to_number(id.val); + id.u32_val = _impl_mutex_init(pName); id.pName = pName; return id; @@ -482,21 +351,12 @@ static inline os_mutex_id_t os_mutex_init(const char_t *pName) * @param id The mutex unique id. * * @return The result of the operation. - ** - * demo usage: - * - * i32p_t postcode = os_mutex_lock(id); - * PC_IF(postcode, PC_PASS) { - * printf("Mutex lock successful\n"); - * } else { - * printf("Mutex lock error: 0x%x\n", postcode); - * } */ static inline i32p_t os_mutex_lock(os_mutex_id_t id) { - extern i32p_t _impl_mutex_lock(os_id_t id); + extern i32p_t _impl_mutex_lock(u32_t ctx); - return (i32p_t)_impl_mutex_lock(id.val); + return (i32p_t)_impl_mutex_lock(id.u32_val); } /** @@ -505,21 +365,12 @@ static inline i32p_t os_mutex_lock(os_mutex_id_t id) * @param id The mutex unique id. * * @return The result of the operation. - ** - * demo usage: - * - * i32p_t postcode = os_mutex_unlock(id); - * PC_IF(postcode, PC_PASS) { - * printf("Mutex unlock successful\n"); - * } else { - * printf("Mutex unlock error: 0x%x\n", postcode); - * } */ static inline i32p_t os_mutex_unlock(os_mutex_id_t id) { - extern i32p_t _impl_mutex_unlock(os_id_t id); + extern i32p_t _impl_mutex_unlock(u32_t ctx); - return (i32p_t)_impl_mutex_unlock(id.val); + return (i32p_t)_impl_mutex_unlock(id.u32_val); } /** @@ -532,24 +383,14 @@ static inline i32p_t os_mutex_unlock(os_mutex_id_t id) * @param pName: The event name. * * @return The event unique id. - ** - * demo usage: - * - * os_evt_id_t id = os_evt_init(1u, 0u, 0u, 0u, "demo"); - * if (os_id_is_invalid(id)) { - * printf("Event %s init failed\n", id.pName); - * } * ... */ static inline os_evt_id_t os_evt_init(u32_t anyMask, u32_t modeMask, u32_t dirMask, u32_t init, const char_t *pName) { - extern u32_t _impl_event_os_id_to_number(os_id_t id); - extern os_id_t _impl_event_init(u32_t anyMask, u32_t modeMask, u32_t dirMask, u32_t init, const char_t *pName); + extern u32_t _impl_event_init(u32_t anyMask, u32_t modeMask, u32_t dirMask, u32_t init, const char_t *pName); os_msgq_id_t id = {0u}; - - id.val = _impl_event_init(anyMask, modeMask, dirMask, init, pName); - id.number = _impl_event_os_id_to_number(id.val); + id.u32_val = _impl_event_init(anyMask, modeMask, dirMask, init, pName); id.pName = pName; return id; @@ -565,9 +406,9 @@ static inline os_evt_id_t os_evt_init(u32_t anyMask, u32_t modeMask, u32_t dirMa */ static inline i32p_t os_evt_value_get(os_evt_id_t id, u32_t *pValue) { - extern i32p_t _impl_event_value_get(os_id_t id, u32_t * pValue); + extern i32p_t _impl_event_value_get(u32_t ctx, u32_t * pValue); - return (i32p_t)_impl_event_value_get(id.val, pValue); + return (i32p_t)_impl_event_value_get(id.u32_val, pValue); } /** @@ -579,21 +420,12 @@ static inline i32p_t os_evt_value_get(os_evt_id_t id, u32_t *pValue) * @param toggle The event value bits toggle. * * @return The result of the operation. - ** - * demo usage: - * - * i32p_t postcode = os_evt_set(id, 0u, 0u, 1u); - * PC_IF(postcode, PC_PASS) { - * printf("Event set successful\n"); - * } else { - * printf("Event set error: 0x%x\n", postcode); - * } */ static inline i32p_t os_evt_set(os_evt_id_t id, u32_t set, u32_t clear, u32_t toggle) { - extern i32p_t _impl_event_set(os_id_t id, u32_t set, u32_t clear, u32_t toggle); + extern i32p_t _impl_event_set(u32_t ctx, u32_t set, u32_t clear, u32_t toggle); - return (i32p_t)_impl_event_set(id.val, set, clear, toggle); + return (i32p_t)_impl_event_set(id.u32_val, set, clear, toggle); } /** @@ -605,62 +437,30 @@ static inline i32p_t os_evt_set(os_evt_id_t id, u32_t set, u32_t clear, u32_t to * @param timeout_ms The event wait timeout setting. * * @return The result of the operation. - ** - * demo usage: - * - * os_evt_val_t evt_val = {0u}; - * i32p_t postcode = os_evt_wait(id, &evt_val, 1u, OS_TIME_WAIT_FOREVER); - * PC_IF(postcode, PC_PASS) { - * printf("Event wait successful, The event value = %x, trigger = %x\n", evt_val->value, evt_val->trigger); - * } else { - * printf("Event wait error: 0x%x\n", postcode); - * } - * - * postcode = os_evt_wait(id, &evt_val, 1u, 1000u); - * PC_IF(postcode, PC_PASS_INFO) { - * if (postcode == OS_PC_TIMEOUT) { - * printf("Event wait timeout\n"); - * } else { - * printf("Event wait successful, The event value = %x, trigger = %x\n", evt_val->value, evt_val->trigger); - * } - * } else { - * printf("Event wait error: 0x%x\n", postcode); - * } */ static inline i32p_t os_evt_wait(os_evt_id_t id, os_evt_val_t *pEvtData, u32_t listen_mask, os_timeout_t timeout_ms) { - extern i32p_t _impl_event_wait(os_id_t id, os_evt_val_t * pEvtData, u32_t listen_mask, u32_t timeout_ms); + extern i32p_t _impl_event_wait(u32_t ctx, struct evt_val * pEvtData, u32_t listen_mask, u32_t timeout_ms); - return (i32p_t)_impl_event_wait(id.val, pEvtData, listen_mask, (u32_t)timeout_ms); + return (i32p_t)_impl_event_wait(id.u32_val, pEvtData, listen_mask, (u32_t)timeout_ms); } /** * @brief Initialize a new queue. * * @param pName The queue name. - * @param pQueueBufferAddr The pointer of the queue buffer. - * @param elementLen The element size. - * @param elementNum The element number. + * @param pBufferAddr The pointer of the queue buffer. + * @param len The element size. + * @param num The element number. * * @return The queue unique id. - ** - * demo usage: - * static u8_t g_demo_msgq[3 * 10] = {0u}; - * - * os_msgq_id_t id = os_msgq_init((u8_t*)g_demo_msgq, 3u, 10u, "demo"); - * if (os_id_is_invalid(id)) { - * printf("Message queue %s init failed\n", id.pName); - * } */ -static inline os_msgq_id_t os_msgq_init(const void *pQueueBufferAddr, u16_t elementLen, u16_t elementNum, const char_t *pName) +static inline os_msgq_id_t os_msgq_init(const void *pBufferAddr, u16_t len, u16_t num, const char_t *pName) { - extern u32_t _impl_queue_os_id_to_number(os_id_t id); - extern os_id_t _impl_queue_init(const void *pQueueBufferAddr, u16_t elementLen, u16_t elementNum, const char_t *pName); + extern u32_t _impl_queue_init(const void *pQueueBufferAddr, u16_t elementLen, u16_t elementNum, const char_t *pName); os_msgq_id_t id = {0u}; - - id.val = _impl_queue_init(pQueueBufferAddr, elementLen, elementNum, pName); - id.number = _impl_queue_os_id_to_number(id.val); + id.u32_val = _impl_queue_init(pBufferAddr, len, num, pName); id.pName = pName; return id; @@ -671,38 +471,17 @@ static inline os_msgq_id_t os_msgq_init(const void *pQueueBufferAddr, u16_t elem * * @param id The queue unique id. * @param pUserBuffer The pointer of the message buffer address. - * @param bufferSize The queue buffer size. + * @param size The queue buffer size. * @param isToFront The direction of the message operation. * @param timeout_ms The queue send timeout option. * * @return The result of the operation. - ** - * demo usage: - * - * u8_t txdata = 0u; - * i32p_t postcode = os_msgq_put(id, &txdata, 0x01u, FALSE, OS_TIME_WAIT_FOREVER); - * PC_IF(postcode, PC_PASS) { - * printf("Message queue send successful\n"); - * } else { - * printf("Message queue send error: 0x%x\n", postcode); - * } - * - * postcode = os_msgq_put(id, &txdata, 0x01u, TRUE, 1000u); - * PC_IF(postcode, PC_PASS_INFO) { - * if (postcode == OS_PC_TIMEOUT) { - * printf("Message queue send timeout\n"); - * } else { - * printf("Message queue send successful\n"); - * } - * } else { - * printf("Message queue send error: 0x%x\n", postcode); - * } */ -static inline i32p_t os_msgq_put(os_msgq_id_t id, const u8_t *pUserBuffer, u16_t bufferSize, b_t isToFront, os_timeout_t timeout_ms) +static inline i32p_t os_msgq_put(os_msgq_id_t id, const u8_t *pUserBuffer, u16_t size, b_t isToFront, os_timeout_t timeout_ms) { - extern i32p_t _impl_queue_send(os_id_t id, const u8_t *pUserBuffer, u16_t bufferSize, b_t isToFront, u32_t timeout_ms); + extern i32p_t _impl_queue_send(u32_t ctx, const u8_t *pUserBuffer, u16_t bufferSize, b_t isToFront, u32_t timeout_ms); - return (i32p_t)_impl_queue_send(id.val, pUserBuffer, bufferSize, isToFront, (u32_t)timeout_ms); + return (i32p_t)_impl_queue_send(id.u32_val, pUserBuffer, size, isToFront, (u32_t)timeout_ms); } /** @@ -710,67 +489,35 @@ static inline i32p_t os_msgq_put(os_msgq_id_t id, const u8_t *pUserBuffer, u16_t * * @param id The queue unique id. * @param pUserBuffer The pointer of the message buffer address. - * @param bufferSize The queue buffer size. + * @param size The queue buffer size. * @param isFromBack The direction of the message operation. * @param timeout_ms The queue send timeout option. * * @return The result of the operation. - ** - * demo usage: - * - * u8_t rxdata = 0u; - * i32p_t postcode = os_msgq_get(id, &rxdata, 0x01u, TRUE, OS_TIME_WAIT_FOREVER); - * PC_IF(postcode, PC_PASS) { - * printf("Message queue receive successful, the rx data is 0x%x\n", rxdata); - * } else { - * printf("Message queue receive error: 0x%x\n", postcode); - * } - * - * postcode = os_msgq_get(id, &rxdata, 0x01u, FALSE, 1000u); - * PC_IF(postcode, PC_PASS) { - * if (postcode == OS_PC_TIMEOUT) { - * printf("Message queue receive timeout\n"); - * } else { - * printf("Message queue receive successful, the rx data is 0x%x\n", rxdata); - * } - * } else { - * printf("Message queue receive error: 0x%x\n", postcode); - * } */ -static inline i32p_t os_msgq_get(os_msgq_id_t id, const u8_t *pUserBuffer, u16_t bufferSize, b_t isFromBack, os_timeout_t timeout_ms) +static inline i32p_t os_msgq_get(os_msgq_id_t id, const u8_t *pUserBuffer, u16_t size, b_t isFromBack, os_timeout_t timeout_ms) { - extern i32p_t _impl_queue_receive(os_id_t id, const u8_t *pUserBuffer, u16_t bufferSize, b_t isFromBack, u32_t timeout_ms); + extern i32p_t _impl_queue_receive(u32_t ctx, const u8_t *pUserBuffer, u16_t bufferSize, b_t isFromBack, u32_t timeout_ms); - return (i32p_t)_impl_queue_receive(id.val, pUserBuffer, bufferSize, isFromBack, (u32_t)timeout_ms); + return (i32p_t)_impl_queue_receive(id.u32_val, pUserBuffer, size, isFromBack, (u32_t)timeout_ms); } /** * @brief Initialize a new pool. * * @param pName The pool name. - * @param pMemAddress The pointer of the pool buffer. - * @param elementLen The element size. - * @param elementNum The element number. + * @param pMemAddr The pointer of the pool buffer. + * @param size The element size. + * @param num The element number. * * @return The pool unique id. - ** - * demo usage: - * static u8_t g_demo_pool[3 * 10] = {0u}; - * - * os_pool_id_t id = os_pool_init((const void*)g_demo_pool, 10u, 3u, "demo"); - * if (os_id_is_invalid(id)) { - * printf("Memory pool %s init failed\n", id.pName); - * } */ -static inline os_pool_id_t os_pool_init(const void *pMemAddress, u16_t elementLen, u16_t elementNum, const char_t *pName) +static inline os_pool_id_t os_pool_init(const void *pMemAddr, u16_t size, u16_t num, const char_t *pName) { - extern u32_t _impl_pool_os_id_to_number(os_id_t id); - extern os_id_t _impl_pool_init(const void *pMemAddr, u16_t elementLen, u16_t elementNum, const char_t *pName); + extern u32_t _impl_pool_init(const void *pMemAddr, u16_t elementLen, u16_t elementNum, const char_t *pName); os_pool_id_t id = {0u}; - - id.val = _impl_pool_init(pMemAddress, elementLen, elementNum, pName); - id.number = _impl_pool_os_id_to_number(id.val); + id.u32_val = _impl_pool_init(pMemAddr, size, num, pName); id.pName = pName; return id; @@ -780,71 +527,32 @@ static inline os_pool_id_t os_pool_init(const void *pMemAddress, u16_t elementLe * @brief Take a message pool resource. * * @param id The pool unique id. - * @param ppUserBuffer The dual pointer of the message memory address. - * @param pBufferSize The pointer of the message memory size. + * @param ppUserMem The dual pointer of the message memory address. + * @param size The pointer of the message memory size. * @param timeout_ms The pool take timeout option. * * @return The result of the operation. - ** - * demo usage: - * - * u8_t* pTakeMem = NULL; - * i32p_t postcode = os_pool_take(id, (void **)&pTakeMem, 10u, OS_TIME_WAIT_FOREVER); - * PC_IF(postcode, PC_PASS) { - * printf("Memory pool take successful\n"); - * } else { - * printf("Memory pool take error: 0x%x\n", postcode); - * } - * - * i32p_t postcode = os_pool_take(id, (void **)&pTakeMem, 10u, 2000u); - * PC_IF(postcode, PC_PASS_INFO) { - * if (postcode == OS_PC_TIMEOUT) { - * printf("Memory pool take timeout\n"); - * } else { - * printf("Memory pool take successful\n"); - * } - * } else { - * printf("Memory pool take error: 0x%x\n", postcode); - * } */ -static inline i32p_t os_pool_take(os_pool_id_t id, void **ppUserBuffer, u16_t bufferSize, os_timeout_t timeout_ms) +static inline i32p_t os_pool_take(os_pool_id_t id, void **ppUserMem, u16_t size, os_timeout_t timeout_ms) { - extern i32p_t _impl_pool_take(os_id_t id, void **ppUserBuffer, u16_t bufferSize, u32_t timeout_ms); + extern i32p_t _impl_pool_take(u32_t ctx, void **ppUserBuffer, u16_t bufferSize, u32_t timeout_ms); - return (i32p_t)_impl_pool_take(id.val, ppUserBuffer, bufferSize, (u32_t)timeout_ms); + return (i32p_t)_impl_pool_take(id.u32_val, ppUserMem, size, (u32_t)timeout_ms); } /** * @brief Release memory pool. * * @param id The pool unique id. - * @param ppUserBuffer The dual pointer of the message memory address. + * @param ppUserMem The dual pointer of the message memory address. * * @return The result of the operation. - ** - * demo usage: - * - * u8_t* pTakeMem = NULL; - * i32p_t postcode = os_pool_take(id, (void **)&pTakeMem, 10u, OS_TIME_WAIT_FOREVER); - * PC_IF(postcode, PC_PASS) { - * printf("Memory pool take successful\n"); - * - * i32p_t postcode = os_pool_release(id, (void **)&pTakeMem); - * PC_IF(postcode, PC_PASS) { - * printf("Memory pool release successful\n"); - * } else { - * printf("Memory pool release error: 0x%x\n", postcode); - * } - * - * } else { - * printf("Memory pool take error: 0x%x\n", postcode); - * } */ -static inline i32p_t os_pool_release(os_pool_id_t id, void **ppUserBuffer) +static inline i32p_t os_pool_release(os_pool_id_t id, void **ppUserMem) { - extern i32p_t _impl_pool_release(os_id_t id, void **ppUserBuffer); + extern i32p_t _impl_pool_release(u32_t ctx, void **ppUserBuffer); - return (i32p_t)_impl_pool_release(id.val, ppUserBuffer); + return (i32p_t)_impl_pool_release(id.u32_val, ppUserMem); } /** @@ -853,24 +561,13 @@ static inline i32p_t os_pool_release(os_pool_id_t id, void **ppUserBuffer) * @param pName The publish name. * * @return The publish unique id. - * - ** - * demo usage: - * - * os_publish_id_t id = os_publish_init("demo"); - * if (os_id_is_invalid(id)) { - * printf("Message publisher %s init failed\n", id.pName); - * } */ static inline os_publish_id_t os_publish_init(const char_t *pName) { - extern u32_t _impl_publish_os_id_to_number(os_id_t id); - extern os_id_t _impl_publish_init(const char_t *pName); + extern u32_t _impl_publish_init(const char_t *pName); os_publish_id_t id = {0u}; - - id.val = _impl_publish_init(pName); - id.number = _impl_publish_os_id_to_number(id.val); + id.u32_val = _impl_publish_init(pName); id.pName = pName; return id; @@ -880,50 +577,33 @@ static inline os_publish_id_t os_publish_init(const char_t *pName) * @brief Publisher Submits the report data. * * @param id The publish unique id. - * @param pPublishData The pointer of the data buffer address. - * @param publishSize The data buffer size. + * @param pData The pointer of the data buffer address. + * @param size The data buffer size. * * @return Value The result of the publisher data operation. - ** - * demo usage: - * u8_t publish_data = 0u; - * i32p_t ret = os_publish_data_submit(id, (u8_t*)&publish_data, 1u); - * PC_IF(postcode, PC_ERROR) { - * printf("Publisher %d data submit failed\n", id.pName); - * } */ -static inline i32p_t os_publish_data_submit(os_publish_id_t id, const void *pPublishData, u16_t publishSize) +static inline i32p_t os_publish_data_submit(os_publish_id_t id, const void *pData, u16_t size) { - extern i32p_t _impl_publish_data_submit(os_id_t id, const void *pPublishData, u16_t publishSize); + extern i32p_t _impl_publish_data_submit(u32_t pub_ctx, const void *pPublishData, u16_t publishSize); - return _impl_publish_data_submit(id.val, pPublishData, publishSize); + return _impl_publish_data_submit(id.u32_val, pData, size); } /** * @brief Initialize a new subscribe. * * @param pDataAddr The pointer of the data buffer address. - * @param dataSize The data buffer size. + * @param size The data buffer size. * @param pName The subscribe name. * * @return Value The result fo subscribe init operation. - ** - * demo usage: - * - * os_subscribe_id_t id = os_subscribe_init("demo"); - * if (os_id_is_invalid(id)) { - * printf("Message subscriber %s init failed\n", id.pName); - * } */ -static inline os_subscribe_id_t os_subscribe_init(void *pDataAddr, u16_t dataSize, const char_t *pName) +static inline os_subscribe_id_t os_subscribe_init(void *pDataAddr, u16_t size, const char_t *pName) { - extern u32_t _impl_subscribe_os_id_to_number(os_id_t id); - extern os_id_t _impl_subscribe_init(void *pDataAddr, u16_t dataSize, const char_t *pName); + extern u32_t _impl_subscribe_init(void *pDataAddr, u16_t dataSize, const char_t *pName); os_subscribe_id_t id = {0u}; - - id.val = _impl_subscribe_init(pDataAddr, dataSize, pName); - id.number = _impl_subscribe_os_id_to_number(id.val); + id.u32_val = _impl_subscribe_init(pDataAddr, size, pName); id.pName = pName; return id; @@ -935,16 +615,12 @@ static inline os_subscribe_id_t os_subscribe_init(void *pDataAddr, u16_t dataSiz * @param subscribe_id The subscribe unique id. * * @return Value The result of subscribe data is ready. - * demo usage: - * if (!os_subscribe_data_is_ready(id)) { - * printf("subscriber %d data is not ready\n", id.pName); - * } */ static inline b_t os_subscribe_data_is_ready(os_subscribe_id_t id) { - extern b_t _impl_subscribe_data_is_ready(os_id_t subscribe_id); + extern b_t _impl_subscribe_data_is_ready(u32_t sub_ctx); - return _impl_subscribe_data_is_ready(id.val); + return _impl_subscribe_data_is_ready(id.u32_val); } /** @@ -960,33 +636,25 @@ static inline b_t os_subscribe_data_is_ready(os_subscribe_id_t id) static inline i32p_t os_subscribe_register(os_subscribe_id_t subscribe_id, os_publish_id_t publish_id, b_t isMute, pSubscribe_callbackFunc_t pNotificationHandler) { - extern i32p_t _impl_subscribe_register(os_id_t subscribe_id, os_id_t publish_id, b_t isMute, - pSubscribe_callbackFunc_t pNotificationHandler); + extern i32p_t _impl_subscribe_register(u32_t sub_ctx, u32_t pub_ctx, b_t isMute, pSubscribe_callbackFunc_t pNotificationHandler); - return _impl_subscribe_register(subscribe_id.val, publish_id.val, isMute, pNotificationHandler); + return _impl_subscribe_register(subscribe_id.u32_val, publish_id.u32_val, isMute, pNotificationHandler); } /** * @brief The subscriber wait publisher put new data with a timeout option. * * @param subscribe_id The subscribe unique id. - * @param pDataBuffer The pointer of data buffer. + * @param pData The pointer of data buffer. * @param pDataLen The pointer of data buffer len. * * @return Value The result of subscribe init operation. - ** - * demo usage: - * u8_t subscribe_data = 0u; - * i32p_t ret = os_subscribe_data_apply(id, (u8_t*)&publish_data, 1u); - * PC_IF(postcode, PC_ERROR) { - * printf("subscriber %d data apply failed\n", id.pName); - * } */ -static inline i32p_t os_subscribe_data_apply(os_subscribe_id_t subscribe_id, void *pDataBuffer, u16_t *pDataLen) +static inline i32p_t os_subscribe_data_apply(os_subscribe_id_t subscribe_id, void *pData, u16_t *pDataLen) { - extern i32p_t _impl_subscribe_data_apply(os_id_t subscribe_id, void *pDataBuffer, u16_t *pDataLen); + extern i32p_t _impl_subscribe_data_apply(u32_t sub_ctx, void *pDataBuffer, u16_t *pDataLen); - return _impl_subscribe_data_apply(subscribe_id.val, pDataBuffer, pDataLen); + return _impl_subscribe_data_apply(subscribe_id.u32_val, pData, pDataLen); } /** @@ -995,14 +663,6 @@ static inline i32p_t os_subscribe_data_apply(os_subscribe_id_t subscribe_id, voi * @param id The provided unique id. * * @return The value of true is invalid, otherwise is valid. - ** - * demo usage: - * - * os_mutex_id_t id = mutex_init("demo"); - * if (os_id_is_invalid(id)) { - * printf("Mutex %s init failed\n", id.pName); - * } - * ... */ static inline b_t os_id_is_invalid(struct os_id id) { @@ -1010,37 +670,18 @@ static inline b_t os_id_is_invalid(struct os_id id) } /** - * @brief Get the current running thread id. + * @brief Get the current running thread context. * - * @return The id of current running thread. - ** - * demo usage: - * - * os_thread_id_t current = os_id_current_thread(); - * printf("At-RTOS kernel current running thread is %s\n", current.pName); - * ... + * @return The running thread context. */ -static inline os_thread_id_t os_id_current_thread(void) +static inline const thread_context_t *os_current_thread_probe(void) { - extern const char_t *_impl_thread_name_get(os_id_t id); - extern os_id_t kernel_thread_runIdGet(void); - extern u32_t _impl_thread_os_id_to_number(os_id_t id); - - os_thread_id_t id = {0u}; - id.val = kernel_thread_runIdGet(); - id.number = _impl_thread_os_id_to_number(id.val); - id.pName = _impl_thread_name_get(id.val); - - return id; + extern thread_context_t *kernel_thread_runContextGet(void); + return kernel_thread_runContextGet(); } /** * @brief The kernel OS start to run. - ** - * demo usage: - * - * os_kernel_run(); - * // Doesn't arrive */ static inline i32p_t os_kernel_run(void) { @@ -1053,13 +694,6 @@ static inline i32p_t os_kernel_run(void) * @brief To check if the kernel OS is running. * * return The true indicates the kernel OS is running. - ** - * demo usage: - * - * if (os_kernel_is_running()) { - * printf("At-RTOS kernel is running\n"); - * } - * ... */ static inline b_t os_kernel_is_running(void) { @@ -1069,50 +703,67 @@ static inline b_t os_kernel_is_running(void) } /** - * @brief Print At-RTOS firmware information. - ** - * demo usage: + * @brief Trace At-RTOS firmware version. + * + * @return The value of firmware version number. + */ +static inline void os_trace_firmware_version(void) +{ + u32_t _impl_trace_firmware_version_get(); +} + +/** + * @brief Trace At-RTOS failed postcode callback function register. + * + * @param fn The invoke function. + */ +static inline void os_trace_postcode_callback_register(const pTrace_postcodeFunc_t fn) +{ + _impl_trace_postcode_callback_register(fn); +} + +/** + * @brief Trace At-RTOS failed postcode value. + * + * @param fn The invoke function. * - * os_trace_firmware_print(); + * @return The value of true is failed, otherwise is pass. */ -static inline void os_trace_firmware_print(void) +static inline b_t os_trace_failed_postcode(const pTrace_postcodeFunc_t fn) { - _impl_trace_firmware_snapshot_print(); + return _impl_trace_postcode_failed_get(fn); } /** - * @brief Print At-RTOS failed postcode value. - ** - * demo usage: + * @brief Trace At-RTOS each thread context. * - * os_trace_postcode_print(); + * @param fn The invoke function. */ -static inline void os_trace_postcode_print(void) +static inline void os_trace_foreach_thread(const pTrace_threadFunc_t fn) { - _impl_trace_postcode_snapshot_print(); + _impl_trace_thread(fn); } /** - * @brief Print At-RTOS kernel information. - ** - * demo usage: + * @brief Trace At-RTOS kernel thread time usage. * - * os_trace_kernel_print(); + * @param fn The invoke function. */ -static inline void os_trace_kernel_print(void) +static inline void os_trace_analyze(const pTrace_analyzeFunc_t fn) { - _impl_trace_kernel_snapshot_print(); + _impl_trace_analyze(fn); } /* It defined the AtOS extern symbol for convenience use, but it has extra memory consumption */ -#if (OS_INTERFACE_EXTERN_USE_ENABLE) +#if (OS_API_ENABLE) typedef struct { - os_thread_id_t (*thread_init)(os_thread_symbol_t *, pThread_entryFunc_t); + os_thread_id_t (*thread_init)(u32_t *, u32_t, i16_t, pThread_entryFunc_t, const char_t *); i32p_t (*thread_sleep)(u32_t); i32p_t (*thread_resume)(os_thread_id_t); i32p_t (*thread_suspend)(os_thread_id_t); i32p_t (*thread_yield)(void); i32p_t (*thread_delete)(os_thread_id_t); + void (*thread_idle_fn_register)(const pThread_entryFunc_t); os_timer_id_t (*timer_init)(pTimer_callbackFunc_t, const char_t *); os_timer_id_t (*timer_automatic)(pTimer_callbackFunc_t, const char_t *); @@ -1150,20 +801,18 @@ typedef struct { b_t (*subscribe_data_is_ready)(os_subscribe_id_t); b_t (*id_isInvalid)(struct os_id); - os_thread_id_t (*id_current_thread)(void); + const thread_context_t *(*current_thread)(void); i32p_t (*schedule_run)(void); b_t (*schedule_is_running)(void); - void (*trace_firmware)(void); - void (*trace_postcode)(void); - void (*trace_kernel)(void); + void (*trace_versison)(void); + void (*trace_postcode_fn_register)(const pTrace_postcodeFunc_t); + b_t (*trace_postcode)(const pTrace_postcodeFunc_t); + void (*trace_thread)(const pTrace_threadFunc_t); + void (*trace_time)(const pTrace_analyzeFunc_t); } at_rtos_api_t; - -extern const at_rtos_api_t os; #endif -#ifdef __cplusplus -} -#endif +extern const at_rtos_api_t os; #endif /* _AT_RTOS_H_ */ diff --git a/include/kernel/compiler.h b/include/kernel/compiler.h index cb36fa9..67e0de4 100644 --- a/include/kernel/compiler.h +++ b/include/kernel/compiler.h @@ -12,14 +12,4 @@ #include "../arch/arch32/arm/cmsis/include/cmsis_compiler.h" #endif -#ifdef __cplusplus -extern "C" { -#endif - -/* TODO */ - -#ifdef __cplusplus -} -#endif - #endif /* _COMPILER_H_ */ diff --git a/include/kernel/configuration.h b/include/kernel/configuration.h index 2642232..42188fc 100644 --- a/include/kernel/configuration.h +++ b/include/kernel/configuration.h @@ -4,54 +4,49 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #ifndef _CONFIGURATION_H_ #define _CONFIGURATION_H_ #include "atos_configuration.h" #include "build_version.h" -#ifdef __cplusplus -extern "C" { -#endif - #define ENABLED (1u) #define DISABLED (0u) -#ifndef THREAD_INSTANCE_SUPPORTED_NUMBER -#define THREAD_INSTANCE_SUPPORTED_NUMBER (1u) +#ifndef THREAD_RUNTIME_NUMBER_SUPPORTED +#define THREAD_RUNTIME_NUMBER_SUPPORTED (1u) #endif -#ifndef SEMAPHORE_INSTANCE_SUPPORTED_NUMBER -#define SEMAPHORE_INSTANCE_SUPPORTED_NUMBER (1u) +#ifndef SEMAPHORE_RUNTIME_NUMBER_SUPPORTED +#define SEMAPHORE_RUNTIME_NUMBER_SUPPORTED (1u) #endif -#ifndef EVENT_INSTANCE_SUPPORTED_NUMBER -#define EVENT_INSTANCE_SUPPORTED_NUMBER (1u) +#ifndef EVENT_RUNTIME_NUMBER_SUPPORTED +#define EVENT_RUNTIME_NUMBER_SUPPORTED (1u) #endif -#ifndef MUTEX_INSTANCE_SUPPORTED_NUMBER -#define MUTEX_INSTANCE_SUPPORTED_NUMBER (1u) +#ifndef MUTEX_RUNTIME_NUMBER_SUPPORTED +#define MUTEX_RUNTIME_NUMBER_SUPPORTED (1u) #endif -#ifndef QUEUE_INSTANCE_SUPPORTED_NUMBER -#define QUEUE_INSTANCE_SUPPORTED_NUMBER (1u) +#ifndef QUEUE_RUNTIME_NUMBER_SUPPORTED +#define QUEUE_RUNTIME_NUMBER_SUPPORTED (1u) #endif -#ifndef TIMER_INSTANCE_SUPPORTED_NUMBER -#define TIMER_INSTANCE_SUPPORTED_NUMBER (1u) +#ifndef TIMER_RUNTIME_NUMBER_SUPPORTED +#define TIMER_RUNTIME_NUMBER_SUPPORTED (1u) #endif -#ifndef POOL_INSTANCE_SUPPORTED_NUMBER -#define POOL_INSTANCE_SUPPORTED_NUMBER (1u) +#ifndef POOL_RUNTIME_NUMBER_SUPPORTED +#define POOL_RUNTIME_NUMBER_SUPPORTED (1u) #endif -#ifndef PUBLISH_INSTANCE_SUPPORTED_NUMBER -#define PUBLISH_INSTANCE_SUPPORTED_NUMBER (1u) +#ifndef PUBLISH_RUNTIME_NUMBER_SUPPORTED +#define PUBLISH_RUNTIME_NUMBER_SUPPORTED (1u) #endif -#ifndef SUBSCRIBE_INSTANCE_SUPPORTED_NUMBER -#define SUBSCRIBE_INSTANCE_SUPPORTED_NUMBER (1u) +#ifndef SUBSCRIBE_RUNTIME_NUMBER_SUPPORTED +#define SUBSCRIBE_RUNTIME_NUMBER_SUPPORTED (1u) #endif #ifndef PORTAL_SYSTEM_CORE_CLOCK_MHZ @@ -90,12 +85,9 @@ extern "C" { #define KERNEL_THREAD_STACK_SIZE (1024u) #endif -#ifdef AT_RTOS_TRACE_USE_STANDARD_PRINTF_ENABLED -#define KTRACE(frmt, ...) printf(frmt, ##__VA_ARGS__) -#else -#ifndef KTRACE -// #define KTRACE(frmt, ...) UNUSED_MSG(UNUSED_ARG) -#endif +/* It defined the AtOS extern symbol for convenience use, but it has extra memory consumption */ +#ifndef OS_API_ENABLE +#define OS_API_ENABLE (ENABLED) #endif /* Configuration of the Cortex-M Processor and Core Peripherals. @@ -129,13 +121,4 @@ extern "C" { #warning Not supported compiler type #endif -/* It defined the AtOS extern symbol for convenience use, but it has extra memory consumption */ -#ifndef OS_INTERFACE_EXTERN_USE_ENABLE -#define OS_INTERFACE_EXTERN_USE_ENABLE (ENABLED) -#endif - -#ifdef __cplusplus -} -#endif - #endif /* _CONFIGURATION_H_ */ diff --git a/include/kernel/init.h b/include/kernel/init.h new file mode 100644 index 0000000..c140eb9 --- /dev/null +++ b/include/kernel/init.h @@ -0,0 +1,357 @@ +/** + * Copyright (c) Riven Zheng (zhengheiot@gmail.com). + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + **/ +#ifndef _INIT_H_ +#define _INIT_H_ + +#include "type_def.h" +#include "kstruct.h" + +#if defined(__CC_ARM) || defined(__CLANG_ARM) +#define INIT_SECTION_FUNC _INIT_FUNC_LIST +#define INIT_SECTION_OS_THREAD_STATIC _INIT_OS_THREAD_STATIC +#define INIT_SECTION_OS_THREAD_LIST _INIT_OS_THREAD_LIST +#define INIT_SECTION_OS_TIMER_LIST _INIT_OS_TIMER_LIST +#define INIT_SECTION_OS_SEMAPHORE_LIST _INIT_OS_SEMAPHORE_LIST +#define INIT_SECTION_OS_MUTEX_LIST _INIT_OS_MUTEX_LIST +#define INIT_SECTION_OS_EVENT_LIST _INIT_OS_EVENT_LIST +#define INIT_SECTION_OS_QUEUE_LIST _INIT_OS_QUEUE_LIST +#define INIT_SECTION_OS_POOL_LIST _INIT_OS_POOL_LIST +#define INIT_SECTION_OS_PUBLISH_LIST _INIT_OS_PUBLISH_LIST +#define INIT_SECTION_OS_SUBSCRIBE_LIST _INIT_OS_SUBSCRIBE_LIST +#elif defined(__ICCARM__) +#define INIT_SECTION_FUNC "_INIT_FUNC_LIST" +#pragma section = INIT_SECTION_FUNC +#define INIT_SECTION_OS_THREAD_STATIC "_INIT_OS_THREAD_STATIC" +#pragma section = INIT_SECTION_OS_THREAD_STATIC + +#define INIT_SECTION_OS_THREAD_LIST "_INIT_OS_THREAD_LIST" +#pragma section = INIT_SECTION_OS_THREAD_LIST + +#define INIT_SECTION_OS_TIMER_LIST "_INIT_OS_TIMER_LIST" +#pragma section = INIT_SECTION_OS_TIMER_LIST + +#define INIT_SECTION_OS_SEMAPHORE_LIST "_INIT_OS_SEMAPHORE_LIST" +#pragma section = INIT_SECTION_OS_SEMAPHORE_LIST + +#define INIT_SECTION_OS_MUTEX_LIST "_INIT_OS_MUTEX_LIST" +#pragma section = INIT_SECTION_OS_MUTEX_LIST + +#define INIT_SECTION_OS_EVENT_LIST "_INIT_OS_EVENT_LIST" +#pragma section = INIT_SECTION_OS_EVENT_LIST + +#define INIT_SECTION_OS_QUEUE_LIST "_INIT_OS_QUEUE_LIST" +#pragma section = INIT_SECTION_OS_QUEUE_LIST + +#define INIT_SECTION_OS_POOL_LIST "_INIT_OS_POOL_LIST" +#pragma section = INIT_SECTION_OS_POOL_LIST + +#define INIT_SECTION_OS_PUBLISH_LIST "_INIT_OS_PUBLISH_LIST" +#pragma section = INIT_SECTION_OS_PUBLISH_LIST + +#define INIT_SECTION_OS_SUBSCRIBE_LIST "_INIT_OS_SUBSCRIBE_LIST" +#pragma section = INIT_SECTION_OS_SUBSCRIBE_LIST + +#elif defined(__GNUC__) +#error "not supported __GNUC__ compiler" +#else +#error "not supported compiler" +#endif + +#if defined(__CC_ARM) || defined(__CLANG_ARM) +#define INIT_SECTION_BEGIN(name) name##$$Base +#define INIT_SECTION_END(name) name##$$Limit +#define INIT_SECTION(name) __attribute__((section(#name))) +#define INIT_USED __attribute__((used)) + +#define INIT_FUNC_DEFINE(handler, level) \ + INIT_USED init_func_t _init_##handler##_func INIT_SECTION(_INIT_FUNC_LIST) = {handler, level} + +#define INIT_OS_THREAD_RUNTIME_NUM_DEFINE(num) \ + INIT_USED thread_context_t _init_runtime_thread[num] INIT_SECTION(_INIT_OS_THREAD_LIST) = {0} + +#define INIT_OS_THREAD_DEFINE(id_name, priority, stack_size, pEntryFn) \ + STACK_STATIC_VALUE_DEFINE(id_name##_stack, stack_size); \ + INIT_USED thread_context_t _init_##id_name##_thread INIT_SECTION(_INIT_OS_THREAD_LIST) = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .pStackAddr = id_name##_stack, \ + .stackSize = stack_size, \ + .pEntryFunc = pEntryFn, \ + .task = {.prior = priority, .psp = 0u}}; \ + INIT_USED thread_context_init_t _init_##id_name##_thread_init INIT_SECTION(_INIT_OS_THREAD_STATIC) = \ + {.pThread = &_init_##id_name##_thread}; \ + os_thread_id_t id_name = {.p_val = (void*)&_init_##id_name##_thread, .pName = #id_name} + +#define INIT_OS_TIMER_RUNTIME_NUM_DEFINE(num) \ + INIT_USED timer_context_t _init_runtime_timer[num] INIT_SECTION(_INIT_OS_TIMER_LIST) = {0} + +extern void timer_callback_fromTimeOut(void *pNode); +#define INIT_OS_TIMER_DEFINE(id_name, pEntryFunc) \ + INIT_USED timer_context_t _init_##id_name##_timer INIT_SECTION(_INIT_OS_TIMER_LIST) = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .expire = {.fn = timer_callback_fromTimeOut}, \ + .call = {.pTimerCallEntry = pEntryFunc}}; \ + os_timer_id_t id_name = {.p_val = (void*)&_init_##id_name##_timer, .pName = #id_name} + +#define INIT_OS_SEM_RUNTIME_NUM_DEFINE(num) \ + INIT_USED semaphore_context_t _init_runtime_sem[num] INIT_SECTION(_INIT_OS_SEMAPHORE_LIST) = {0} + +#define INIT_OS_SEMAPHORE_DEFINE(id_name, remain, limit) \ + INIT_USED semaphore_context_t _init_##id_name##_sem INIT_SECTION(_INIT_OS_SEMAPHORE_LIST) = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .remains = remain, \ + .limits = limit}; \ + os_sem_id_t id_name = {.p_val = (void*)&_init_##id_name##_sem, .pName = #id_name} + +#define INIT_OS_MUTEX_RUNTIME_NUM_DEFINE(num) \ + INIT_USED mutex_context_t _init_runtime_mutex[num] INIT_SECTION(_INIT_OS_MUTEX_LIST) = {0} + +#define INIT_OS_MUTEX_DEFINE(id_name) \ + INIT_USED mutex_context_t _init_##id_name##_mutex INIT_SECTION(_INIT_OS_MUTEX_LIST) = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .locked = false, \ + .pHoldTask = NULL, \ + .originalPriority = OS_PRIOTITY_INVALID_LEVEL}; \ + os_mutex_id_t id_name = {.p_val = (void*)&_init_##id_name##_mutex, .pName = #id_name} + +#define INIT_OS_EVT_RUNTIME_NUM_DEFINE(num) \ + INIT_USED event_context_t _init_runtime_evt[num] INIT_SECTION(_INIT_OS_EVENT_LIST) = {0} + +#define INIT_OS_EVT_DEFINE(id_name, anyMask, modeMask, dirMask, init) \ + INIT_USED event_context_t _init_##id_name##_evt INIT_SECTION(_INIT_OS_EVENT_LIST) = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .value = init, \ + .triggered = 0u, \ + .anyMask = anyMask, \ + .modeMask = modeMask, \ + .dirMask = dirMask}; \ + os_evt_id_t id_name = {.p_val = (void*)&_init_##id_name##_evt, .pName = #id_name} + +#define INIT_OS_MSGQ_RUNTIME_NUM_DEFINE(num) \ + INIT_USED queue_context_t _init_runtime_msgq[num] INIT_SECTION(_INIT_OS_QUEUE_LIST) = {0} + +#define INIT_OS_MSGQ_DEFINE(id_name, pBufAddr, len, num) \ + INIT_USED queue_context_t _init_##id_name##_msgq INIT_SECTION(_INIT_OS_QUEUE_LIST) = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .pQueueBufferAddress = pBufAddr, \ + .elementLength = len, \ + .elementNumber = num, \ + .leftPosition = 0u, \ + .rightPosition = 0u, \ + .cacheSize = 0u}; \ + os_msgq_id_t id_name = {.p_val = (void*)&_init_##id_name##_msgq, .pName = #id_name} + +#define INIT_OS_POOL_RUNTIME_NUM_DEFINE(num) \ + INIT_USED pool_context_t _init_runtime_pool[num] INIT_SECTION(_INIT_OS_POOL_LIST) = {0} + +#define INIT_OS_POOL_DEFINE(id_name, pMemAddr, len, num) \ + INIT_USED pool_context_t _init_##id_name##_pool INIT_SECTION(_INIT_OS_POOL_LIST) = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .pMemAddress = pMemAddr, \ + .elementLength = len, \ + .elementNumber = num, \ + .elementFreeBits = Bs(0u, (num - 1u))}; \ + os_pool_id_t id_name = {.p_val = (void*)&_init_##id_name##_pool, .pName = #id_name} + +#define INIT_OS_SUBSCRIBE_RUNTIME_NUM_DEFINE(num) \ + INIT_USED subscribe_context_t _init_runtime_subscribe[num] INIT_SECTION(_INIT_OS_SUBSCRIBE_LIST) = {0} + +extern void subscribe_notification(void *pLinker); +#define INIT_OS_SUBSCRIBE_DEFINE(id_name, pDataAddr, size) \ + INIT_USED subscribe_context_t _init_##id_name##_subscribe INIT_SECTION(_INIT_OS_SUBSCRIBE_LIST) = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .pPublisher = NULL, \ + .accepted = 0u, \ + .notify = {.pData = pData, .len = size, muted = false, .fn = subscribe_notification}}; \ + os_subscribe_id_t id_name = {.p_val = (void*)&_init_##id_name##_subscribe, .pName = #id_name} + +#define INIT_OS_PUBLISH_RUNTIME_NUM_DEFINE(num) \ + INIT_USED publish_context_t _init_runtime_publish[num] INIT_SECTION(_INIT_OS_PUBLISH_LIST) = {0} + +#define INIT_OS_PUBLISH_DEFINE(id_name, pDataAddr, size) \ + INIT_USED publish_context_t _init_##id_name##_publish INIT_SECTION(_INIT_OS_PUBLISH_LIST) = \ + {.head = {.cs = CS_INITED, .pName = #id_name}}; \ + os_publish_id_t id_name = {.p_val = (void*)&_init_##id_name##_publish, .pName = #id_name} + +#elif defined(__ICCARM__) +#pragma diag_suppress = Pm086 +#define INIT_SECTION(name) @name +#define INIT_SECTION_BEGIN(name) __section_begin(name) +#define INIT_SECTION_END(name) __section_end(name) + +#define INIT_FUNC_DEFINE(handler, level) \ + static __root const init_func_t _init_##handler##_func @ "_INIT_FUNC_LIST" = {handler, level} + +#define INIT_OS_THREAD_RUNTIME_NUM_DEFINE(num) \ + static __root thread_context_t _init_runtime_thread[num] @ "_INIT_OS_THREAD_LIST" = {0} + +#define INIT_OS_THREAD_DEFINE(id_name, priority, stack_size, pEntryFn) \ + STACK_STATIC_VALUE_DEFINE(id_name##_stack, stack_size); \ + static __root thread_context_t _init_##id_name##_thread @ "_INIT_OS_THREAD_LIST" = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .pStackAddr = id_name##_stack, \ + .stackSize = stack_size, \ + .pEntryFunc = pEntryFn, \ + .task = {.prior = priority, .psp = 0u}}; \ + static __root thread_context_init_t _init_##id_name##_thread_init @ "_INIT_OS_THREAD_STATIC" = \ + {.pThread = &_init_##id_name##_thread}; \ + os_thread_id_t id_name = {.p_val = (void*)&_init_##id_name##_thread, .pName = #id_name} + +#define INIT_OS_TIMER_RUNTIME_NUM_DEFINE(num) \ + static __root timer_context_t _init_runtime_timer[num] @ "_INIT_OS_TIMER_LIST" = {0} + +extern void timer_callback_fromTimeOut(void *pNode); +#define INIT_OS_TIMER_DEFINE(id_name, pEntryFunc) \ + static __root timer_context_t _init_##id_name##_timer @ "_INIT_OS_TIMER_LIST" = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .expire = {.fn = timer_callback_fromTimeOut}, \ + .call = {.pTimerCallEntry = pEntryFunc}}; \ + os_timer_id_t id_name = {.p_val = (void*)&_init_##id_name##_timer, .pName = #id_name} + +#define INIT_OS_SEM_RUNTIME_NUM_DEFINE(num) \ + static __root semaphore_context_t _init_runtime_sem[num] @ "_INIT_OS_SEMAPHORE_LIST" = {0} + +#define INIT_OS_SEMAPHORE_DEFINE(id_name, remain, limit) \ + static __root semaphore_context_t _init_##id_name##_sem @ "_INIT_OS_SEMAPHORE_LIST" = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .remains = remain, \ + .limits = limit}; \ + os_sem_id_t id_name = {.p_val = (void*)&_init_##id_name##_sem, .pName = #id_name} + +#define INIT_OS_MUTEX_RUNTIME_NUM_DEFINE(num) \ + static __root mutex_context_t _init_runtime_mutex[num] @ "_INIT_OS_MUTEX_LIST" = {0} + +#define INIT_OS_MUTEX_DEFINE(id_name) \ + static __root mutex_context_t _init_##id_name##_mutex @ "_INIT_OS_MUTEX_LIST" = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .locked = false, \ + .pHoldTask = NULL, \ + .originalPriority = OS_PRIOTITY_INVALID_LEVEL}; \ + os_mutex_id_t id_name = {.p_val = (void*)&_init_##id_name##_mutex, .pName = #id_name} + +#define INIT_OS_EVT_RUNTIME_NUM_DEFINE(num) \ + static __root event_context_t _init_runtime_evt[num] @ "_INIT_OS_EVENT_LIST" = {0} + +#define INIT_OS_EVT_DEFINE(id_name, anyMask, modeMask, dirMask, init) \ + static __root event_context_t _init_##id_name##_evt @ "_INIT_OS_EVENT_LIST" = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .value = init, \ + .triggered = 0u, \ + .anyMask = anyMask, \ + .modeMask = modeMask, \ + .dirMask = dirMask}; \ + os_evt_id_t id_name = {.p_val = (void*)&_init_##id_name##_evt, .pName = #id_name} + +#define INIT_OS_MSGQ_RUNTIME_NUM_DEFINE(num) \ + static __root queue_context_t _init_runtime_msgq[num] @ "_INIT_OS_QUEUE_LIST" = {0} + +#define INIT_OS_MSGQ_DEFINE(id_name, pBufAddr, len, num) \ + static __root queue_context_t _init_##id_name##_msgq @ "_INIT_OS_QUEUE_LIST" = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .pQueueBufferAddress = pBufAddr, \ + .elementLength = len, \ + .elementNumber = num, \ + .leftPosition = 0u, \ + .rightPosition = 0u, \ + .cacheSize = 0u}; \ + os_msgq_id_t id_name = {.p_val = (void*)&_init_##id_name##_msgq, .pName = #id_name} + +#define INIT_OS_POOL_RUNTIME_NUM_DEFINE(num) \ + static __root pool_context_t _init_runtime_pool[num] @ "_INIT_OS_POOL_LIST" = {0} + +#define INIT_OS_POOL_DEFINE(id_name, pMemAddr, len, num) \ + static __root pool_context_t _init_##id_name##_pool @ "_INIT_OS_POOL_LIST" = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .pMemAddress = pMemAddr, \ + .elementLength = len, \ + .elementNumber = num, \ + .elementFreeBits = Bs(0u, (num - 1u))}; \ + os_pool_id_t id_name = {.p_val = (void*)&_init_##id_name##_pool, .pName = #id_name} + +#define INIT_OS_SUBSCRIBE_RUNTIME_NUM_DEFINE(num) \ + static __root subscribe_context_t _init_runtime_subscribe[num] @ "_INIT_OS_SUBSCRIBE_LIST" = {0} + + extern void subscribe_notification(void *pLinker); +#define INIT_OS_SUBSCRIBE_DEFINE(id_name, pDataAddr, size) \ + static __root subscribe_context_t _init_##id_name##_subscribe @ "_INIT_OS_SUBSCRIBE_LIST" = \ + {.head = {.cs = CS_INITED, .pName = #id_name}, \ + .pPublisher = NULL, \ + .accepted = 0u, \ + .notify = {.pData = pData, .len = size, muted = false, .fn = subscribe_notification}}; \ + os_subscribe_id_t id_name = {.p_val = (void*)&_init_##id_name##_subscribe, .pName = #id_name} + +#define INIT_OS_PUBLISH_RUNTIME_NUM_DEFINE(num) \ + static __root publish_context_t _init_runtime_publish[num] @ "_INIT_OS_PUBLISH_LIST" = {0} + +#define INIT_OS_PUBLISH_DEFINE(id_name, pDataAddr, size) \ + static __root publish_context_t _init_##id_name##_publish @ "_INIT_OS_PUBLISH_LIST" = \ + {.head = {.cs = CS_INITED, .pName = #id_name}}; \ + os_publish_id_t id_name = {.p_val = (void*)&_init_##id_name##_publish, .pName = #id_name} + +#pragma diag_default = Pm086 +#elif defined(__GNUC__) +#error "not supported __GNUC__ compiler" +#else +#error "not supported compiler" +#endif + +#if defined(__CC_ARM) || defined(__CLANG_ARM) +#define INIT_SECTION_FIRST(i_section, o_begin) \ + do { \ + extern const int INIT_SECTION_BEGIN(i_section); \ + o_begin = (u32_t)&INIT_SECTION_BEGIN(i_section); \ + } while(0) + +#define INIT_SECTION_LAST(i_section, o_end) \ + do { \ + extern const int INIT_SECTION_END(i_section); \ + o_end = (u32_t)&INIT_SECTION_END(i_section); \ + } while(0) + +#define INIT_SECTION_FOREACH(section, type, item) \ + extern const int INIT_SECTION_BEGIN(section); \ + extern const int INIT_SECTION_END(section); \ + for (type *item = (type *)&INIT_SECTION_BEGIN(section); item < (type *)&INIT_SECTION_END(section); item++) +#elif defined(__ICCARM__) +#define INIT_SECTION_FIRST(i_section, o_begin) \ + do { \ + o_begin = (u32_t)INIT_SECTION_BEGIN(i_section); \ + } while(0) + +#define INIT_SECTION_LAST(i_section, o_end) \ + do { \ + o_end = (u32_t)INIT_SECTION_END(i_section); \ + } while(0) + +#define INIT_SECTION_FOREACH(i_section, type, item) \ + for (type *item = (type *)INIT_SECTION_BEGIN(i_section); item < (type *)INIT_SECTION_END(i_section); item++) + +#elif defined(__GNUC__) +#error "not supported __GNUC__ compiler" +#else +#error "not supported compiler" +#endif + +enum { + INIT_LEVEL_0, + INIT_LEVEL_1, + INIT_LEVEL_2, + INIT_LEVEL_3, + INIT_LEVEL_4, + INIT_LEVEL_NUM, +}; + +typedef void (*init_func)(void); +typedef struct { + init_func func; + u8_t level; +} init_func_t; + +void init_func_list(void); +void init_func_level(u8_t level); +void init_static_thread_list(void); + +#endif diff --git a/include/kernel/kernel.h b/include/kernel/kernel.h index 19b9a32..fdeb038 100644 --- a/include/kernel/kernel.h +++ b/include/kernel/kernel.h @@ -4,7 +4,6 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #ifndef _KERNEL_H_ #define _KERNEL_H_ @@ -13,21 +12,6 @@ #include "ktype.h" #include "port.h" -#ifdef __cplusplus -extern "C" { -#endif - -enum { - KERNEL_SCHEDULE_THREAD_INSTANCE = (0u), - KERNEL_IDLE_THREAD_INSTANCE, - KERNEL_APPLICATION_THREAD_INSTANCE, -}; - -enum { - KERNEL_SCHEDULE_SEMAPHORE_INSTANCE = (0u), - KERNEL_APPLICATION_SEMAPHORE_INSTANCE, -}; - #ifndef KERNEL_THREAD_STACK_SIZE #define KERNEL_SCHEDULE_THREAD_STACK_SIZE (1024u) #else @@ -43,57 +27,28 @@ enum { #define ENTER_CRITICAL_SECTION() ARCH_ENTER_CRITICAL_SECTION() #define EXIT_CRITICAL_SECTION() ARCH_EXIT_CRITICAL_SECTION() -#define KERNEL_THREAD_MEMORY_SIZE (sizeof(thread_context_t) * (THREAD_INSTANCE_SUPPORTED_NUMBER + KERNEL_APPLICATION_THREAD_INSTANCE)) -#define KERNEL_TIMER_INTERNAL_MEMORY_SIZE \ - (sizeof(timer_context_t) * (THREAD_INSTANCE_SUPPORTED_NUMBER + KERNEL_APPLICATION_THREAD_INSTANCE)) -#define KERNEL_TIMER_MEMORY_SIZE (sizeof(timer_context_t) * TIMER_INSTANCE_SUPPORTED_NUMBER) -#define KERNEL_SEMAPHORE_MEMORY_SIZE \ - (sizeof(semaphore_context_t) * (SEMAPHORE_INSTANCE_SUPPORTED_NUMBER + KERNEL_APPLICATION_SEMAPHORE_INSTANCE)) -#define KERNEL_MUTEX_MEMORY_SIZE (sizeof(mutex_context_t) * MUTEX_INSTANCE_SUPPORTED_NUMBER) -#define KERNEL_EVENT_MEMORY_SIZE (sizeof(event_context_t) * EVENT_INSTANCE_SUPPORTED_NUMBER) -#define KERNEL_QUEUE_MEMORY_SIZE (sizeof(queue_context_t) * QUEUE_INSTANCE_SUPPORTED_NUMBER) -#define KERNEL_POOL_MEMORY_SIZE (sizeof(pool_context_t) * POOL_INSTANCE_SUPPORTED_NUMBER) -#define KERNEL_PUBLISH_MEMORY_SIZE (sizeof(publish_context_t) * PUBLISH_INSTANCE_SUPPORTED_NUMBER) -#define KERNEL_SUBSCIRBE_MEMORY_SIZE (sizeof(subscribe_context_t) * SUBSCRIBE_INSTANCE_SUPPORTED_NUMBER) - -#define KERNEL_MEMBER_MAP_1 (KERNEL_THREAD_MEMORY_SIZE) -#define KERNEL_MEMBER_MAP_2 (KERNEL_MEMBER_MAP_1 + KERNEL_TIMER_INTERNAL_MEMORY_SIZE) -#define KERNEL_MEMBER_MAP_3 (KERNEL_MEMBER_MAP_2 + KERNEL_TIMER_MEMORY_SIZE) -#define KERNEL_MEMBER_MAP_4 (KERNEL_MEMBER_MAP_3 + KERNEL_SEMAPHORE_MEMORY_SIZE) -#define KERNEL_MEMBER_MAP_5 (KERNEL_MEMBER_MAP_4 + KERNEL_MUTEX_MEMORY_SIZE) -#define KERNEL_MEMBER_MAP_6 (KERNEL_MEMBER_MAP_5 + KERNEL_EVENT_MEMORY_SIZE) -#define KERNEL_MEMBER_MAP_7 (KERNEL_MEMBER_MAP_6 + KERNEL_QUEUE_MEMORY_SIZE) -#define KERNEL_MEMBER_MAP_8 (KERNEL_MEMBER_MAP_7 + KERNEL_POOL_MEMORY_SIZE) -#define KERNEL_MEMBER_MAP_9 (KERNEL_MEMBER_MAP_8 + KERNEL_PUBLISH_MEMORY_SIZE) -#define KERNEL_MEMBER_MAP_10 (KERNEL_MEMBER_MAP_9 + KERNEL_SUBSCIRBE_MEMORY_SIZE) -#define KERNEL_MEMBER_MAP_NUMBER (KERNEL_MEMBER_MAP_10 + 1u) - thread_context_t *kernel_thread_runContextGet(void); list_t *kernel_member_list_get(u8_t member_id, u8_t list_id); void kernel_thread_list_transfer_toEntry(linker_head_t *pCurHead); -i32p_t kernel_thread_exit_trigger(thread_context_t *pCurThread, void* pHoldCtx, list_t *pToList, u32_t timeout_us); -i32p_t kernel_thread_entry_trigger(thread_context_t *pCurThread, u32_t result, void (*pCallback)(os_id_t)); +i32p_t schedule_exit_trigger(struct schedule_task *pTask, void *pHoldCtx, void *pHoldData, list_t *pToList, u32_t timeout_ms, + b_t immediately); +i32p_t schedule_entry_trigger(struct schedule_task *pTask, pTask_callbackFunc_t callback, u32_t result); +void schedule_callback_fromTimeOut(void *pNode); +void schedule_setPend(struct schedule_task *pTask); +list_t *schedule_waitList(void); +b_t schedule_hasTwoPendingItem(void); i32p_t kernel_schedule_result_take(void); -void kernel_thread_list_transfer_toPend(linker_head_t *pCurHead); -list_t *kernel_list_pendingHeadGet(void); u32_t kernel_stack_frame_init(void (*pEntryFunction)(void), u32_t *pAddress, u32_t size); b_t kernel_isInThreadMode(void); i32p_t kernel_thread_schedule_request(void); void kernel_message_notification(void); void kernel_scheduler_inPendSV_c(u32_t **ppCurPsp, u32_t **ppNextPSP); -u32_t kernel_schedule_time_get(void); -u32_t kernel_thread_use_percent_take(os_id_t id); void kernel_privilege_call_inSVC_c(u32_t *svc_args); i32p_t kernel_privilege_invoke(const void *pCallFun, arguments_t *pArgs); -void kernel_semaphore_list_transfer_toInit(linker_head_t *pCurHead); void kernel_schedule_thread(void); void kernel_idle_thread(void); -void kthread_init(void); void kthread_message_notification(void); i32p_t kthread_message_arrived(void); - -#ifdef __cplusplus -} -#endif +void kthread_message_idle_loop_fn(void); #endif /* _KERNEL_H_ */ diff --git a/include/kernel/kstruct.h b/include/kernel/kstruct.h index 2444d5b..fe64b4d 100644 --- a/include/kernel/kstruct.h +++ b/include/kernel/kstruct.h @@ -4,19 +4,14 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #ifndef _KSTRUCT_H_ #define _KSTRUCT_H_ -#include "typedef.h" +#include "type_def.h" #include "linker.h" #include "ktype.h" #include "configuration.h" -#ifdef __cplusplus -extern "C" { -#endif - /* Start of section using anonymous unions */ #if defined(__CC_ARM) #pragma push @@ -29,15 +24,13 @@ extern "C" { typedef void (*pCallbackFunc_t)(void); typedef void (*pTimer_callbackFunc_t)(void); -typedef void (*pThread_callbackFunc_t)(os_id_t); +typedef void (*pTask_callbackFunc_t)(void *); typedef void (*pThread_entryFunc_t)(void); typedef void (*pEvent_callbackFunc_t)(void); typedef void (*pSubscribe_callbackFunc_t)(const void *, u16_t); typedef void (*pTimeout_callbackFunc_t)(void *); typedef void (*pNotify_callbackFunc_t)(void *); -#define CS_INITED (1u) - struct base_head { u8_t cs; // control and status @@ -47,7 +40,7 @@ struct base_head { struct publish_context { struct base_head head; - list_t subscribeListHead; + list_t q_list; }; typedef struct publish_context publish_context_t; @@ -118,7 +111,7 @@ typedef struct { u32_t timeout_ms; - list_t blockingThreadHead; + list_t q_list; } semaphore_context_t; typedef struct { @@ -126,11 +119,11 @@ typedef struct { b_t locked; - struct thread_context *pHoldThread; + struct schedule_task *pHoldTask; - os_priority_t originalPriority; + i16_t originalPriority; - list_t blockingThreadHead; + list_t q_list; } mutex_context_t; typedef struct { @@ -154,11 +147,9 @@ typedef struct { u16_t cacheSize; - /* The blocked thread list */ - list_t inBlockingThreadListHead; + list_t in_QList; - /* The blocked thread list */ - list_t outBlockingThreadListHead; + list_t out_QList; } queue_context_t; typedef struct { @@ -172,14 +163,14 @@ typedef struct { u32_t elementFreeBits; - list_t blockingThreadHead; + list_t q_list; } pool_context_t; typedef struct { /* The listen bits*/ u32_t listen; - os_evt_val_t *pEvtVal; + struct evt_val *pEvtVal; } event_sch_t; struct event_callback { @@ -209,153 +200,83 @@ typedef struct { /* When the event change that meet with edge setting, the function will be called */ struct event_callback call; - /* The blocked thread list */ - list_t blockingThreadHead; + list_t q_list; } event_context_t; -typedef struct { +struct call_exit { list_t *pToList; - u32_t timeout_us; -} thread_exit_t; + u32_t timeout_ms; +}; -typedef struct { +struct call_entry { i32p_t result; - pThread_callbackFunc_t pEntryCallFun; -} thread_entry_t; - -typedef struct { - u32_t pend_ms; - - u32_t run_ms; - - u32_t exit_ms; + pTask_callbackFunc_t fun; +}; - u32_t active_ms; +struct call_analyze { + u32_t last_pend_ms; - u32_t cycle_ms; + u32_t last_active_ms; - u16_t percent; -} analyze_t; + u32_t last_run_ms; -typedef struct { - void *pPendCtx; - - void *pPendData; - -#if defined KTRACE - analyze_t analyze; -#endif + u32_t total_run_ms; +}; +struct call_exec { union { - thread_exit_t exit; + struct call_exit exit; - thread_entry_t entry; + struct call_entry entry; }; -} action_schedule_t; - + struct call_analyze analyze; +}; -struct thread_context { - /* A common struct head to link with other context */ - linker_head_t head; +struct schedule_task { + linker_t linker; - os_priority_t priority; + u32_t psp; - pThread_entryFunc_t pEntryFunc; + i16_t prior; - u32_t *pStackAddr; + void *pPendCtx; - u32_t stackSize; + void *pPendData; - u32_t psp; + struct call_exec exec; struct expired_time expire; - - action_schedule_t schedule; -}; -typedef struct thread_context thread_context_t; - -/** @brief The kernel object type enum. */ -enum { - KERNEL_MEMBER_THREAD = 0u, - KERNEL_MEMBER_TIMER_INTERNAL, - KERNEL_MEMBER_TIMER, - KERNEL_MEMBER_SEMAPHORE, - KERNEL_MEMBER_MUTEX, - KERNEL_MEMBER_EVENT, - KERNEL_MEMBER_QUEUE, - KERNEL_MEMBER_POOL, - KERNEL_MEMBER_PUBLISH, - KERNEL_MEMBER_SUBSCRIBE, - KERNEL_MEMBER_NUMBER, }; -enum { - KERNEL_MEMBER_LIST_THREAD_WAIT = 0u, - KERNEL_MEMBER_LIST_THREAD_ENTRY, - KERNEL_MEMBER_LIST_THREAD_EXIT, - - KERNEL_MEMBER_LIST_TIMER_STOP, - KERNEL_MEMBER_LIST_TIMER_WAIT, - KERNEL_MEMBER_LIST_TIMER_END, - KERNEL_MEMBER_LIST_TIMER_PEND, - KERNEL_MEMBER_LIST_TIMER_RUN, - - KERNEL_MEMBER_LIST_SEMAPHORE_INIT, - - KERNEL_MEMBER_LIST_MUTEX_LOCK, - KERNEL_MEMBER_LIST_MUTEX_UNLOCK, - - KERNEL_MEMBER_LIST_EVENT_INIT, +struct thread_context { + struct base_head head; - KERNEL_MEMBER_LIST_QUEUE_INIT, + pThread_entryFunc_t pEntryFunc; - KERNEL_MEMBER_LIST_POOL_INIT, + u32_t *pStackAddr; - KERNEL_MEMBER_LIST_PUBLISH_INIT, - KERNEL_MEMBER_LIST_PUBLISH_PEND, - KERNEL_MEMBER_LIST_SUBSCRIBE_INIT, + u32_t stackSize; - KERNEL_MEMBER_LIST_NUMBER, + struct schedule_task task; }; +typedef struct thread_context thread_context_t; typedef struct { - u32_t mem; - u32_t list; -} kernel_member_setting_t; - -typedef struct { - const u8_t *pMemoryContainer; - - const list_t *pListContainer; - - kernel_member_setting_t *pSetting; -} kernel_member_t; + struct thread_context *pThread; +} thread_context_init_t; /** @brief The rtos kernel structure. */ typedef struct { - /* The current running thread */ - os_id_t current; - - list_t list; - - kernel_member_t member; + struct schedule_task *pTask; - /* The kernel already start to do schedule */ b_t run; u32_t pendsv_ms; -} kernel_context_t; -typedef struct { - union { - u32_t size; - u8_t priority; - const char_t *pName; - }; -} os_thread_symbol_t; +} kernel_context_t; /* End of section using anonymous unions */ #if defined(__CC_ARM) @@ -364,8 +285,4 @@ typedef struct { #pragma warning restore #endif -#ifdef __cplusplus -} -#endif - #endif /* _KSTRUCT_H_ */ diff --git a/include/kernel/ktype.h b/include/kernel/ktype.h index c47e9e0..076f73a 100644 --- a/include/kernel/ktype.h +++ b/include/kernel/ktype.h @@ -8,46 +8,11 @@ #ifndef _KTYPE_H_ #define _KTYPE_H_ -#include "typedef.h" +#include "type_def.h" #include "linker.h" -#ifdef __cplusplus -extern "C" { -#endif - -typedef u32_t os_id_t; - -struct os_id { - u32_t number; - u32_t val; - const char_t *pName; -}; - -struct os_priority { - u8_t level; -}; - -struct os_time { - u32_t ms; -}; - -typedef struct os_id os_thread_id_t; -typedef struct os_id os_timer_id_t; -typedef struct os_id os_sem_id_t; -typedef struct os_id os_mutex_id_t; -typedef struct os_id os_evt_id_t; -typedef struct os_id os_msgq_id_t; -typedef struct os_id os_pool_id_t; -typedef struct os_id os_publish_id_t; -typedef struct os_id os_subscribe_id_t; - -typedef struct os_priority os_priority_t; -typedef struct os_time os_time_t; - -typedef struct { - u32_t value; - u32_t trigger; -} os_evt_val_t; +#define CS_INITED (1u) +#define STACK_STATIC_VALUE_DEFINE(stack, size) u32_t stack[((u32_t)(size) / sizeof(u32_t))] = {0}; #define OS_INVALID_ID_VAL (0xFFFFFFFFu) @@ -55,15 +20,17 @@ typedef struct { #define OS_TIME_FOREVER_VAL (0xFFFFFFFEu) #define OS_TIME_NOWAIT_VAL (0x0u) -#define OS_PRIOTITY_INVALID_LEVEL (0xFFu) -#define OS_PRIOTITY_LOWEST_LEVEL (0xFEu) -#define OS_PRIOTITY_HIGHEST_LEVEL (0x0u) +#define OS_PRIOTITY_PREEMPT_NUM (255) +#define OS_PRIOTITY_COOPERATION_NUM (256) + +#define OS_PRIOTITY_INVALID_LEVEL (OS_PRIOTITY_PREEMPT_NUM + 1) +#define OS_PRIOTITY_LOWEST_LEVEL (OS_PRIOTITY_PREEMPT_NUM) +#define OS_PRIOTITY_HIGHEST_LEVEL (-OS_PRIOTITY_COOPERATION_NUM) -#define OS_PRIORITY_INVALID (OS_PRIOTITY_INVALID_LEVEL) -#define OS_PRIORITY_KERNEL_THREAD_IDLE_LEVEL (OS_PRIOTITY_LOWEST_LEVEL) -#define OS_PRIORITY_KERNEL_THREAD_SCHEDULE_LEVEL (OS_PRIOTITY_HIGHEST_LEVEL) -#define OS_PRIORITY_USER_THREAD_LOWEST_LEVEL (OS_PRIOTITY_LOWEST_LEVEL - 1u) -#define OS_PRIORITY_USER_THREAD_HIGHEST_LEVEL (OS_PRIOTITY_HIGHEST_LEVEL + 1u) +#define OS_PRIORITY_KERNEL_IDLE_LEVEL (OS_PRIOTITY_LOWEST_LEVEL) +#define OS_PRIORITY_KERNEL_SCHEDULE_LEVEL (OS_PRIOTITY_HIGHEST_LEVEL) +#define OS_PRIORITY_APPLICATION_HIGHEST_LEVEL (OS_PRIOTITY_HIGHEST_LEVEL + 1) +#define OS_PRIORITY_APPLICATION_LOWEST_LEVEL (OS_PRIOTITY_LOWEST_LEVEL - 1) #define TIMER_CTRL_ONCE_VAL (0u) #define TIMER_CTRL_CYCLE_VAL (1u) @@ -76,21 +43,46 @@ enum { PC_OS_WAIT_UNAVAILABLE, }; -u8_t *kernel_member_unified_id_toContainerAddress(u32_t unified_id); -u32_t kernel_member_containerAddress_toUnifiedid(u32_t container_address); -u32_t kernel_member_id_toUnifiedIdStart(u8_t member_id); -u8_t *kernel_member_id_toContainerStartAddress(u32_t member_id); -u8_t *kernel_member_id_toContainerEndAddress(u32_t member_id); -b_t kernel_member_unified_id_isInvalid(u32_t member_id, u32_t unified_id); -u8_t kernel_member_unified_id_toId(u32_t unified_id); -u32_t kernel_member_unified_id_threadToTimer(u32_t unified_id); -u32_t kernel_member_unified_id_timerToThread(u32_t unified_id); -u32_t kernel_member_id_unifiedConvert(u8_t member_id, u32_t unified_id); -os_id_t kernel_thread_runIdGet(void); -b_t kernel_os_id_is_invalid(struct os_id id); - -#ifdef __cplusplus -} +#if defined(__CC_ARM) +#pragma push +#pragma anon_unions +#elif defined(__ICCARM__) +#pragma language = extended +#elif defined(__TASKING__) +#pragma warning 586 #endif +struct os_id { + union { + u32_t u32_val; + void *p_val; + }; + const char_t *pName; +}; + +struct evt_val { + u32_t value; + u32_t trigger; +}; + +/* End of section using anonymous unions */ +#if defined(__CC_ARM) +#pragma pop +#elif defined(__TASKING__) +#pragma warning restore +#endif + +static inline b_t kernel_os_id_is_invalid(struct os_id id) +{ + if (id.p_val == NULL) { + return true; + } + + if (id.u32_val == OS_INVALID_ID_VAL) { + return true; + } + + return false; +} + #endif /* _KTYPE_H_ */ diff --git a/include/kernel/linker.h b/include/kernel/linker.h index bfe4fce..50ad4ec 100644 --- a/include/kernel/linker.h +++ b/include/kernel/linker.h @@ -4,15 +4,10 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #ifndef _LINKER_H_ #define _LINKER_H_ -#include "typedef.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "type_def.h" /* Define the list null value */ #define LIST_NULL \ @@ -31,16 +26,18 @@ extern "C" { } /** @brief singly linked list node structure. */ -typedef struct list_node { +struct list_node { /* The pointer of the next node head. */ struct list_node *pNext; -} list_node_t; +}; +typedef struct list_node list_node_t; /** @brief singly linked list structure. */ -typedef struct list { +struct list { /* The pointer of the node head. */ - list_node_t *pHead; -} list_t; + struct list_node *pHead; +}; +typedef struct list list_t; /** @brief the direction of node operation structure. */ typedef enum { @@ -50,25 +47,26 @@ typedef enum { typedef struct { /* The pointer of the current node. */ - list_node_t *pCurNode; + struct list_node *pCurNode; /* The pointer of the current list. */ - list_t *pList; + struct list *pList; } list_iterator_t; /** @brief The linker structure help to mannage the singly-linked list. */ -typedef struct { +struct linker { /* The node */ - list_node_t node; + struct list_node node; /* The node in which list */ - list_t *pList; -} linker_t; + struct list *pList; +}; +typedef struct linker linker_t; /** @brief The linker structure head is to mannage the rtos context. */ typedef struct { /* The linker is an important symbol to connect with same status node */ - linker_t linker; + struct linker linker; /* The head id */ u32_t id; @@ -84,6 +82,7 @@ typedef b_t (*pLinkerSpecificConditionFunc_t)(list_node_t *, list_node_t *); b_t list_node_isExisted(list_t *pList, list_node_t *pNode); u32_t list_size(list_t *pList); +void *list_head(list_t *pList); b_t list_node_delete(list_t *pList, list_node_t *pTargetNode); b_t list_node_insertBefore(list_t *pList, list_node_t *pBefore, list_node_t *pTargetNode); b_t list_node_push(list_t *pList, list_node_t *pInNode, list_direction_t direction); @@ -98,8 +97,4 @@ void os_memset(void *dst, u8_t val, u32_t cnt); i32_t os_memcmp(const void *dst, const void *src, u32_t cnt); u32_t os_strlen(const uchar_t *str); -#ifdef __cplusplus -} -#endif - #endif /* _LINKER_H_ */ diff --git a/include/kernel/postcode.h b/include/kernel/postcode.h index 7071b6d..b8b6ea4 100644 --- a/include/kernel/postcode.h +++ b/include/kernel/postcode.h @@ -4,15 +4,10 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #ifndef _POSTCODE_H_ #define _POSTCODE_H_ -#include "typedef.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "type_def.h" /* The following table defined the At-RTOS component number */ enum { @@ -38,17 +33,18 @@ enum { */ static inline i32p_t _impl_postcode_cmpt_failed(i32p_t postcode) { - extern u32_t g_postcode_os_cmpt_failed_container[PC_OS_COMPONENT_NUMBER]; + extern void _impl_trace_postcode_set(u32_t cmpt, u32_t code); - if (postcode < 0) { - u32_t code = PC_NEGATIVE(postcode); - u32_t component = (((u32_t)code >> PC_COMPONENT_NUMBER_POS) & PC_COMPONENT_NUMBER_MSK); - if (component < PC_OS_COMPONENT_NUMBER) { - g_postcode_os_cmpt_failed_container[component] = code; - } + if (postcode >= 0) { + return postcode; + } - while (1) {}; + u32_t code = PC_NEGATIVE(postcode); + u32_t component = (((u32_t)code >> PC_COMPONENT_NUMBER_POS) & PC_COMPONENT_NUMBER_MSK); + if (component < PC_OS_COMPONENT_NUMBER) { + _impl_trace_postcode_set(component, code); } + return postcode; } @@ -75,8 +71,4 @@ static inline i32p_t _impl_postcode_cmpt_failed(i32p_t postcode) #define PC_ASSERT_RANGE(val, low, high) _CHECK_RANGE(val, low, high) #define PC_ASSERT_BOUND(val, high) _CHECK_BOUND(val, high) -#ifdef __cplusplus -} -#endif - #endif /* _POSTCODE_H_ */ diff --git a/include/kernel/timer.h b/include/kernel/timer.h index 5018595..1e65830 100644 --- a/include/kernel/timer.h +++ b/include/kernel/timer.h @@ -4,22 +4,13 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #ifndef _TIMER_H_ #define _TIMER_H_ #include "kstruct.h" -#ifdef __cplusplus -extern "C" { -#endif - -/** - * The implement function lists for rtos kernel internal use. - */ void timeout_set(struct expired_time *pExpire, u32_t timeout_ms, b_t immediately); void timeout_remove(struct expired_time *pExpire, b_t immediately); -b_t timer_busy(os_id_t id); u32_t timer_total_system_ms_get(void); u32_t timer_total_system_us_get(void); i32p_t timer_schedule(void); @@ -27,8 +18,4 @@ void timer_reamining_elapsed_handler(void); void timeout_handler(u32_t elapsed_us); void timeout_init(struct expired_time *pExpire, pTimeout_callbackFunc_t fun); -#ifdef __cplusplus -} -#endif - #endif /* _TIMER_H_ */ diff --git a/include/kernel/trace.h b/include/kernel/trace.h index dff82e9..67c95dd 100644 --- a/include/kernel/trace.h +++ b/include/kernel/trace.h @@ -4,120 +4,21 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #ifndef _TRACE_H_ #define _TRACE_H_ -#include "typedef.h" +#include "type_def.h" #include "linker.h" +#include "kstruct.h" -#ifdef __cplusplus -extern "C" { -#endif - -/* Start of section using anonymous unions */ -#if defined(__CC_ARM) -#pragma push -#pragma anon_unions -#elif defined(__ICCARM__) -#pragma language = extended -#elif defined(__TASKING__) -#pragma warning 586 -#endif - -typedef struct { - u32_t priority; - u32_t current_psp; - u32_t ram; - u32_t cpu; - u32_t delay; -} thread_snapshot_t; - -typedef struct { - u16_t initial_count; - u16_t limit_count; - u32_t timeout_ms; - list_t wait_list; -} semaphore_snapshot_t; - -typedef struct { - u32_t holdThreadId; - - u32_t originalPriority; - - list_t wait_list; -} mutex_snapshot_t; - -typedef struct { - u32_t set; - - u32_t edge; - - list_t wait_list; -} event_snapshot_t; - -typedef struct { - u32_t cacheSize; - - list_t in_list; - - list_t out_list; -} queue_snapshot_t; - -typedef struct { - u32_t free; - - list_t wait_list; -} pool_snapshot_t; - -typedef struct { - u8_t cycle; - - u32_t timeout_ms; -} timer_snapshot_t; - -typedef struct { - u32_t refresh_count; -} publish_snapshot_t; - -typedef struct { - u32_t id; - const char_t *pName; - const char_t *pState; - union { - thread_snapshot_t thread; - - semaphore_snapshot_t semaphore; - - mutex_snapshot_t mutex; - - event_snapshot_t event; - - queue_snapshot_t queue; - - pool_snapshot_t pool; - - timer_snapshot_t timer; - - publish_snapshot_t publish; - }; -} kernel_snapshot_t; - -/* End of section using anonymous unions */ -#if defined(__CC_ARM) -#pragma pop -#elif defined(__TASKING__) -#pragma warning restore -#endif - -b_t thread_snapshot(u32_t instance, kernel_snapshot_t *pMsgs); - -void _impl_trace_firmware_snapshot_print(void); -void _impl_trace_postcode_snapshot_print(void); -void _impl_trace_kernel_snapshot_print(void); +typedef void (*pTrace_postcodeFunc_t)(u32_t, u32_t); +typedef void (*pTrace_threadFunc_t)(const thread_context_t *pThread); +typedef void (*pTrace_analyzeFunc_t)(const struct call_analyze analyze); -#ifdef __cplusplus -} -#endif +u32_t _impl_trace_firmware_version_get(void); +void _impl_trace_postcode_callback_register(const pTrace_postcodeFunc_t fn); +b_t _impl_trace_postcode_failed_get(const pTrace_postcodeFunc_t fn); +void _impl_trace_thread(const pTrace_threadFunc_t fn); +void _impl_trace_analyze(const pTrace_analyzeFunc_t fn); #endif /* _TRACE_H_ */ diff --git a/include/kernel/type_def.h b/include/kernel/type_def.h new file mode 100644 index 0000000..569f6b9 --- /dev/null +++ b/include/kernel/type_def.h @@ -0,0 +1,198 @@ +/** + * Copyright (c) Riven Zheng (zhengheiot@gmail.com). + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + **/ +#ifndef _TYPE_DEF_H_ +#define _TYPE_DEF_H_ + +#include +#include +#include +#include + +typedef char char_t; +typedef unsigned char uchar_t; +typedef unsigned char u8_t; +typedef unsigned short u16_t; +typedef unsigned int u32_t; +typedef unsigned long long u64_t; +typedef signed char i8_t; +typedef signed short i16_t; +typedef signed int i32_t; +typedef signed long long i64_t; +typedef volatile char vchar_t; +typedef volatile unsigned char vuchar_t; +typedef volatile unsigned char vu8_t; +typedef volatile unsigned short vu16_t; +typedef volatile unsigned int vu32_t; +typedef volatile unsigned long long vu64_t; +typedef volatile signed char vi8_t; +typedef volatile signed short vi16_t; +typedef volatile signed int vi32_t; +typedef volatile signed long long vi64_t; +typedef bool b_t; +typedef i32_t i32p_t; + +#ifndef FALSE +#define FALSE false +#endif + +#ifndef TRUE +#define TRUE true +#endif + +#define U8_B (8u) +#define U16_B (16u) +#define U32_B (32u) +#define U8_MAX ((u8_t)-1) +#define U16_MAX ((u16_t)-1) +#define U32_MAX ((u32_t)-1) + +#define UNUSED_MSG(x) (void)(x) +#define UNUSED_ARG (0u) + +#define FLAG(x) (!!(x)) +#define UNFLAG(x) (!!!(x)) + +#define MAGIC(pre, post) pre##post + +#if defined __COUNTER__ +#define COMPLIER_UNIQUE_NUMBER __COUNTER__ +#else +#define COMPLIER_UNIQUE_NUMBER __LINE__ +#endif +#define COMPLIER_LINE_NUMBER __LINE__ + +/* To generating a unique token */ +#define _STATIC_MAGIC_3(pre, post) MAGIC(pre, post) +#define _STATIC_MAGIC_2(pre, post) _STATIC_MAGIC_3(pre, post) + +#if defined __COUNTER__ +#define _STATIC_MAGIC(pre) _STATIC_MAGIC_2(pre, COMPLIER_UNIQUE_NUMBER) +#else +#define _STATIC_MAGIC(pre) _STATIC_MAGIC_2(pre, COMPLIER_UNIQUE_NUMBER) +#endif +#define BUILD_ASSERT(cond, msg) typedef char_t _STATIC_MAGIC(static_assert)[FLAG(cond) * 2 - 1] + +#define RUN_ASSERT(c) \ + if (UNFLAG(c)) { \ + while (1) {}; \ + } + +#define RUN_UNREACHABLE() RUN_ASSERT(0) + +#define VREG32(addr) (*(volatile u32_t *)(u32_t)(addr)) +#define VREG16(addr) (*(volatile u16_t *)(u32_t)(addr)) +#define VREG8(addr) (*(volatile u8_t *)(u32_t)(addr)) + +#define B(x) (u32_t)((u32_t)1u << (x)) +#define MSK_B(x) (u32_t)(B(x) - 1u) +#define Bs(start, end) (u32_t)((0xFFFFFFFFul << (start)) & (0xFFFFFFFFul >> (31u - (u32_t)(end)))) +#define DUMP_Bs(regval, start, end) (u32_t)(((regval) & Bs((start), (end))) >> (start)) + +#define BV_GET(v, m, p) ((v >> ((p) * (m))) & (MSK_B(m))) +#define BV_CLR(v, m, p) (v &= ~((MSK_B(m)) << ((p) * (m)))) +#define BV_SET(v, m, p, n) (v |= ((n) << ((p) * (m)))) +#define BV_CS(v, m, p, n) \ + BV_CLR(v, m, p); \ + BV_SET(v, m, p, n) + +#define DEQUALIFY(s, v) ((s)(u32_t)(const volatile void *)(v)) +#define OFFSETOF(s, m) ((u32_t)(&((s *)0)->m)) +#define CONTAINEROF(p, s, m) (DEQUALIFY(s *, ((const vu8_t *)(p)-OFFSETOF(s, m)))) +#define SIZEOF(arr) (sizeof(arr)) +#define DIMOF(arr) (SIZEOF(arr) / SIZEOF(arr[0])) + +#define MINI_AB(a, b) (((a) < (b)) ? (a) : (b)) +#define MAX_AB(a, b) (((a) > (b)) ? (a) : (b)) + +#define ROUND_UP(size, align) (((u32_t)(size) + (align - 1u)) & (~(align - 1))) +#define ROUND_DOWN(size, align) (((u32_t)(size)) & (~(align - 1))) +#define RANGE_ADDRESS_CONDITION(address, pool) \ + (((u32_t)(address) >= (u32_t)(pool)) && ((u32_t)(address) < ((u32_t)(pool) + (u16_t)SIZEOF(pool)))) + +#define PC_COMMON_NUMBER_POS (0u) +#define PC_COMMON_NUMBER_MSK (MSK_B(8)) /* The maximum pass information number up to 31 */ +#define PC_LINE_NUMBER_POS (8u) +#define PC_LINE_NUMBER_MSK (MSK_B(13)) /* The maximum line number up to 8191 */ +#define PC_COMPONENT_NUMBER_POS (21u) +#define PC_COMPONENT_NUMBER_MSK (MSK_B(10)) /* The maximum component number up to 1024 */ + +#define _PC_COMMON_VALUE(n) (((n) & PC_COMMON_NUMBER_MSK) << PC_COMMON_NUMBER_POS) +#define _PC_LINE_VALUE(e) (((e) & PC_LINE_NUMBER_MSK) << PC_LINE_NUMBER_POS) +#define _PC_COMPONENT_VALUE(c) (((c) & PC_COMPONENT_NUMBER_MSK) << PC_COMPONENT_NUMBER_POS) +#define _PC_VALUE(c, n) (_PC_COMPONENT_VALUE(c) | _PC_LINE_VALUE(COMPLIER_LINE_NUMBER) | _PC_COMMON_VALUE(n)) +#define PC_NEGATIVE(v) (i32_t)(U32_MAX - (i32_t)(v)) +#define PC_IER(c) (PC_NEGATIVE(_PC_VALUE(c, 0))) +#define PC_NER(c, n) (PC_NEGATIVE(_PC_VALUE(c, n))) + +#define PC_CMPT_NUMBER_INIT (1u) +#define PC_CMPT_ASSERT (PC_CMPT_NUMBER_INIT) +#define PC_CMPT_NUMBER_USER (PC_CMPT_NUMBER_INIT + 1u) + +#define REVERSE_NUMBER_32 \ + 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 + +#define ARGS_INTERGRATION_32(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, \ + _24, _25, _26, _27, _28, _29, _30, _31, _32, N, ...) \ + N +#define ARGS_SHIFT_32(...) ARGS_INTERGRATION_32(__VA_ARGS__) +#define ARGS_NUM_32(...) ARGS_SHIFT_32(__VA_ARGS__, REVERSE_NUMBER_32) +#define ARGS_NUM(...) ARGS_NUM_32(__VA_ARGS__) + +#define ARGS_1(_1, ...) _1 +#define ARGS_2(_1, _2, ...) _2 +#define ARGS_3(_1, _2, _3, ...) _3 +#define ARGS_4(_1, _2, _3, _4, ...) _4 +#define ARGS_5(_1, _2, _3, _4, _5, ...) _5 +#define ARGS_6(_1, _2, _3, _4, _5, _6, ...) _6 +#define ARGS_7(_1, _2, _3, _4, _5, _6, _7, ...) _7 +#define ARGS_8(_1, _2, _3, _4, _5, _6, _7, _8, ...) _8 +#define ARGS_9(_1, _2, _3, _4, _5, _6, _7, _8, _9, ...) _9 +#define ARGS_10(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, ...) _10 +#define ARGS_11(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, ...) _11 +#define ARGS_12(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, ...) _12 +#define ARGS_13(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, ...) _13 +#define ARGS_14(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, ...) _14 +#define ARGS_15(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, ...) _15 +#define ARGS_16(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, ...) _16 +#define ARGS_17(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, ...) _17 +#define ARGS_18(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, ...) _18 +#define ARGS_19(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, ...) _19 +#define ARGS_20(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, ...) _20 +#define ARGS_21(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, ...) _21 +#define ARGS_22(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, ...) _22 +#define ARGS_23(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, ...) _23 +#define ARGS_24(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, ...) _24 +#define ARGS_25(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, ...) _25 +#define ARGS_26(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ + ...) \ + _26 +#define ARGS_27(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ + _27, ...) \ + _27 +#define ARGS_28(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ + _27, _28, ...) \ + _28 +#define ARGS_29(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ + _27, _28, _29, ...) \ + _29 +#define ARGS_30(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ + _27, _28, _29, _30, ...) \ + _30 +#define ARGS_31(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ + _27, _28, _29, _30, _31, ...) \ + _31 +#define ARGS_32(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ + _27, _28, _29, _30, _31, _32, ...) \ + _32 + +#define H_ARGS ARGS_ +#define AG_3(pre, post) MAGIC(pre, post) +#define AG_2(pre, post) AG_3(pre, post) +#define AG(m) AG_2(H_ARGS, m) +#define ARGS_N(N, ...) AG(N)(__VA_ARGS__) + +#endif /* _TYPE_DEF_H_ */ diff --git a/include/kernel/typedef.h b/include/kernel/typedef.h deleted file mode 100644 index 131d45f..0000000 --- a/include/kernel/typedef.h +++ /dev/null @@ -1,628 +0,0 @@ -/** - * Copyright (c) Riven Zheng (zhengheiot@gmail.com). - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - **/ - -#ifndef _TYPEDEF_H_ -#define _TYPEDEF_H_ - -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef char char_t; -typedef unsigned char uchar_t; -typedef unsigned char u8_t; -typedef unsigned short u16_t; -typedef unsigned int u32_t; -typedef unsigned long long u64_t; -typedef signed char i8_t; -typedef signed short i16_t; -typedef signed int i32_t; -typedef signed long long i64_t; -typedef volatile char vchar_t; -typedef volatile unsigned char vuchar_t; -typedef volatile unsigned char vu8_t; -typedef volatile unsigned short vu16_t; -typedef volatile unsigned int vu32_t; -typedef volatile unsigned long long vu64_t; -typedef volatile signed char vi8_t; -typedef volatile signed short vi16_t; -typedef volatile signed int vi32_t; -typedef volatile signed long long vi64_t; -typedef bool b_t; -typedef i32_t i32p_t; - -#ifndef FALSE -#define FALSE false -#endif - -#ifndef TRUE -#define TRUE true -#endif - -#define U8_B (8u) -#define U16_B (16u) -#define U32_B (32u) -#define U8_MAX ((u8_t)-1) -#define U16_MAX ((u16_t)-1) -#define U32_MAX ((u32_t)-1) - -#define UNUSED_MSG(x) (void)(x) -#define UNUSED_ARG (0u) - -#define FLAG(x) (!!(x)) -#define UNFLAG(x) (!!!(x)) - -#define MAGIC(pre, post) pre##post - -#if defined __COUNTER__ -#define COMPLIER_UNIQUE_NUMBER __COUNTER__ -#else -#define COMPLIER_UNIQUE_NUMBER __LINE__ -#endif -#define COMPLIER_LINE_NUMBER __LINE__ - -/* To generating a unique token */ -#define _STATIC_MAGIC_3(pre, post) MAGIC(pre, post) -#define _STATIC_MAGIC_2(pre, post) _STATIC_MAGIC_3(pre, post) - -#if defined __COUNTER__ -#define _STATIC_MAGIC(pre) _STATIC_MAGIC_2(pre, COMPLIER_UNIQUE_NUMBER) -#else -#define _STATIC_MAGIC(pre) _STATIC_MAGIC_2(pre, COMPLIER_UNIQUE_NUMBER) -#endif -#define BUILD_ASSERT(cond, msg) typedef char_t _STATIC_MAGIC(static_assert)[FLAG(cond) * 2 - 1] - -#define RUN_ASSERT(c) \ - if (UNFLAG(c)) { \ - while (1) {}; \ - } - -#define RUN_UNREACHABLE() RUN_ASSERT(0) - -#define VREG32(addr) (*(volatile u32_t *)(u32_t)(addr)) -#define VREG16(addr) (*(volatile u16_t *)(u32_t)(addr)) -#define VREG8(addr) (*(volatile u8_t *)(u32_t)(addr)) - -#define SET_BIT(x) (u32_t)((u32_t)1u << (x)) -#define MASK_BIT(x) (u32_t)(SET_BIT(x) - 1u) -#define SET_BITS(start, end) (u32_t)((0xFFFFFFFFul << (start)) & (0xFFFFFFFFul >> (31u - (u32_t)(end)))) -#define DUMP_BITS(regval, start, end) (u32_t)(((regval) & SET_BITS((start), (end))) >> (start)) - -#define BV_GET(v, m, p) ((v >> ((p) * (m))) & (MASK_BIT(m))) -#define BV_CLR(v, m, p) (v &= ~((MASK_BIT(m)) << ((p) * (m)))) -#define BV_SET(v, m, p, n) (v |= ((n) << ((p) * (m)))) -#define BV_CS(v, m, p, n) \ - BV_CLR(v, m, p); \ - BV_SET(v, m, p, n) - -#define DEQUALIFY(s, v) ((s)(u32_t)(const volatile void *)(v)) -#define OFFSETOF(s, m) ((u32_t)(&((s *)0)->m)) -#define CONTAINEROF(p, s, m) (DEQUALIFY(s *, ((const vu8_t *)(p)-OFFSETOF(s, m)))) -#define SIZEOF(arr) (sizeof(arr)) -#define DIMOF(arr) (SIZEOF(arr) / SIZEOF(arr[0])) - -#define MINI_AB(a, b) (((a) < (b)) ? (a) : (b)) -#define MAX_AB(a, b) (((a) > (b)) ? (a) : (b)) - -#define ROUND_UP(size, align) (((u32_t)(size) + (align - 1u)) & (~(align - 1))) -#define ROUND_DOWN(size, align) (((u32_t)(size)) & (~(align - 1))) -#define RANGE_ADDRESS_CONDITION(address, pool) \ - (((u32_t)(address) >= (u32_t)(pool)) && ((u32_t)(address) < ((u32_t)(pool) + (u16_t)SIZEOF(pool)))) - -#define PC_COMMON_NUMBER_POS (0u) -#define PC_COMMON_NUMBER_MSK (MASK_BIT(8)) /* The maximum pass information number up to 31 */ -#define PC_LINE_NUMBER_POS (8u) -#define PC_LINE_NUMBER_MSK (MASK_BIT(13)) /* The maximum line number up to 8191 */ -#define PC_COMPONENT_NUMBER_POS (21u) -#define PC_COMPONENT_NUMBER_MSK (MASK_BIT(10)) /* The maximum component number up to 1024 */ - -#define _PC_COMMON_VALUE(n) (((n) & PC_COMMON_NUMBER_MSK) << PC_COMMON_NUMBER_POS) -#define _PC_LINE_VALUE(e) (((e) & PC_LINE_NUMBER_MSK) << PC_LINE_NUMBER_POS) -#define _PC_COMPONENT_VALUE(c) (((c) & PC_COMPONENT_NUMBER_MSK) << PC_COMPONENT_NUMBER_POS) -#define _PC_VALUE(c, n) (_PC_COMPONENT_VALUE(c) | _PC_LINE_VALUE(COMPLIER_LINE_NUMBER) | _PC_COMMON_VALUE(n)) -#define PC_NEGATIVE(v) (i32_t)(U32_MAX - (i32_t)(v)) -#define PC_IER(c) (PC_NEGATIVE(_PC_VALUE(c, 0))) -#define PC_NER(c, n) (PC_NEGATIVE(_PC_VALUE(c, n))) - -#define PC_CMPT_NUMBER_INIT (1u) -#define PC_CMPT_ASSERT (PC_CMPT_NUMBER_INIT) -#define PC_CMPT_NUMBER_USER (PC_CMPT_NUMBER_INIT + 1u) - -#define REVERSE_NUMBER_32 \ - 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 - -#define ARGS_INTERGRATION_32(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, \ - _24, _25, _26, _27, _28, _29, _30, _31, _32, N, ...) \ - N -#define ARGS_SHIFT_32(...) ARGS_INTERGRATION_32(__VA_ARGS__) -#define ARGS_NUM_32(...) ARGS_SHIFT_32(__VA_ARGS__, REVERSE_NUMBER_32) -#define ARGS_NUM(...) ARGS_NUM_32(__VA_ARGS__) - -#define ARGS_1(_1, ...) _1 -#define ARGS_2(_1, _2, ...) _2 -#define ARGS_3(_1, _2, _3, ...) _3 -#define ARGS_4(_1, _2, _3, _4, ...) _4 -#define ARGS_5(_1, _2, _3, _4, _5, ...) _5 -#define ARGS_6(_1, _2, _3, _4, _5, _6, ...) _6 -#define ARGS_7(_1, _2, _3, _4, _5, _6, _7, ...) _7 -#define ARGS_8(_1, _2, _3, _4, _5, _6, _7, _8, ...) _8 -#define ARGS_9(_1, _2, _3, _4, _5, _6, _7, _8, _9, ...) _9 -#define ARGS_10(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, ...) _10 -#define ARGS_11(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, ...) _11 -#define ARGS_12(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, ...) _12 -#define ARGS_13(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, ...) _13 -#define ARGS_14(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, ...) _14 -#define ARGS_15(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, ...) _15 -#define ARGS_16(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, ...) _16 -#define ARGS_17(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, ...) _17 -#define ARGS_18(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, ...) _18 -#define ARGS_19(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, ...) _19 -#define ARGS_20(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, ...) _20 -#define ARGS_21(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, ...) _21 -#define ARGS_22(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, ...) _22 -#define ARGS_23(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, ...) _23 -#define ARGS_24(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, ...) _24 -#define ARGS_25(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, ...) _25 -#define ARGS_26(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ - ...) \ - _26 -#define ARGS_27(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ - _27, ...) \ - _27 -#define ARGS_28(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ - _27, _28, ...) \ - _28 -#define ARGS_29(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ - _27, _28, _29, ...) \ - _29 -#define ARGS_30(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ - _27, _28, _29, _30, ...) \ - _30 -#define ARGS_31(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ - _27, _28, _29, _30, _31, ...) \ - _31 -#define ARGS_32(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ - _27, _28, _29, _30, _31, _32, ...) \ - _32 - -#define H_ARGS ARGS_ -#define AG_3(pre, post) MAGIC(pre, post) -#define AG_2(pre, post) AG_3(pre, post) -#define AG(m) AG_2(H_ARGS, m) -#define ARGS_N(N, ...) AG(N)(__VA_ARGS__) - -#define CBITS .bits. -#define CV_3(pre, post) MAGIC(pre, post) -#define CV_2(pre, post) CV_3(pre, post) -#define CV(m) CV_2(CBITS, m) -#define CB(c, b) CV_2(c, CV(b)) - -#define CMV CM_V -#define CMV_3(pre, post) MAGIC(pre, post) -#define CMV_2(pre, post) CMV_3(pre, post) -#define CM(m) CMV_2(CMV, m) - -#define CM_V1(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(2, __VA_ARGS__) \ - } -#define CM_V2(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(3, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(4, __VA_ARGS__) \ - } -#define CM_V3(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(4, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(5, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(6, __VA_ARGS__) \ - } -#define CM_V4(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(5, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(6, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(7, __VA_ARGS__), CV(ARGS_N(4, __VA_ARGS__)) = ARGS_N(8, __VA_ARGS__) \ - } -#define CM_V5(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(6, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(7, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(8, __VA_ARGS__), CV(ARGS_N(4, __VA_ARGS__)) = ARGS_N(9, __VA_ARGS__), \ - CV(ARGS_N(5, __VA_ARGS__)) = ARGS_N(10, __VA_ARGS__) \ - } -#define CM_V6(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(7, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(8, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(9, __VA_ARGS__), CV(ARGS_N(4, __VA_ARGS__)) = ARGS_N(10, __VA_ARGS__), \ - CV(ARGS_N(5, __VA_ARGS__)) = ARGS_N(11, __VA_ARGS__), CV(ARGS_N(6, __VA_ARGS__)) = ARGS_N(12, __VA_ARGS__) \ - } -#define CM_V7(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(8, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(9, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(10, __VA_ARGS__), CV(ARGS_N(4, __VA_ARGS__)) = ARGS_N(11, __VA_ARGS__), \ - CV(ARGS_N(5, __VA_ARGS__)) = ARGS_N(12, __VA_ARGS__), CV(ARGS_N(6, __VA_ARGS__)) = ARGS_N(13, __VA_ARGS__), \ - CV(ARGS_N(7, __VA_ARGS__)) = ARGS_N(14, __VA_ARGS__) \ - } -#define CM_V8(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(9, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(10, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(11, __VA_ARGS__), CV(ARGS_N(4, __VA_ARGS__)) = ARGS_N(12, __VA_ARGS__), \ - CV(ARGS_N(5, __VA_ARGS__)) = ARGS_N(13, __VA_ARGS__), CV(ARGS_N(6, __VA_ARGS__)) = ARGS_N(14, __VA_ARGS__), \ - CV(ARGS_N(7, __VA_ARGS__)) = ARGS_N(15, __VA_ARGS__), CV(ARGS_N(8, __VA_ARGS__)) = ARGS_N(16, __VA_ARGS__) \ - } -#define CM_V9(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(10, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(11, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(12, __VA_ARGS__), CV(ARGS_N(4, __VA_ARGS__)) = ARGS_N(13, __VA_ARGS__), \ - CV(ARGS_N(5, __VA_ARGS__)) = ARGS_N(14, __VA_ARGS__), CV(ARGS_N(6, __VA_ARGS__)) = ARGS_N(15, __VA_ARGS__), \ - CV(ARGS_N(7, __VA_ARGS__)) = ARGS_N(16, __VA_ARGS__), CV(ARGS_N(8, __VA_ARGS__)) = ARGS_N(17, __VA_ARGS__), \ - CV(ARGS_N(9, __VA_ARGS__)) = ARGS_N(18, __VA_ARGS__) \ - } -#define CM_V10(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(11, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(12, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(13, __VA_ARGS__), CV(ARGS_N(4, __VA_ARGS__)) = ARGS_N(14, __VA_ARGS__), \ - CV(ARGS_N(5, __VA_ARGS__)) = ARGS_N(15, __VA_ARGS__), CV(ARGS_N(6, __VA_ARGS__)) = ARGS_N(16, __VA_ARGS__), \ - CV(ARGS_N(7, __VA_ARGS__)) = ARGS_N(17, __VA_ARGS__), CV(ARGS_N(8, __VA_ARGS__)) = ARGS_N(18, __VA_ARGS__), \ - CV(ARGS_N(9, __VA_ARGS__)) = ARGS_N(19, __VA_ARGS__), CV(ARGS_N(10, __VA_ARGS__)) = ARGS_N(20, __VA_ARGS__) \ - } -#define CM_V11(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(12, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(13, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(14, __VA_ARGS__), CV(ARGS_N(4, __VA_ARGS__)) = ARGS_N(15, __VA_ARGS__), \ - CV(ARGS_N(5, __VA_ARGS__)) = ARGS_N(16, __VA_ARGS__), CV(ARGS_N(6, __VA_ARGS__)) = ARGS_N(17, __VA_ARGS__), \ - CV(ARGS_N(7, __VA_ARGS__)) = ARGS_N(18, __VA_ARGS__), CV(ARGS_N(8, __VA_ARGS__)) = ARGS_N(19, __VA_ARGS__), \ - CV(ARGS_N(9, __VA_ARGS__)) = ARGS_N(20, __VA_ARGS__), CV(ARGS_N(10, __VA_ARGS__)) = ARGS_N(21, __VA_ARGS__), \ - CV(ARGS_N(11, __VA_ARGS__)) = ARGS_N(22, __VA_ARGS__) \ - } -#define CM_V12(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(13, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(14, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(15, __VA_ARGS__), CV(ARGS_N(4, __VA_ARGS__)) = ARGS_N(16, __VA_ARGS__), \ - CV(ARGS_N(5, __VA_ARGS__)) = ARGS_N(17, __VA_ARGS__), CV(ARGS_N(6, __VA_ARGS__)) = ARGS_N(18, __VA_ARGS__), \ - CV(ARGS_N(7, __VA_ARGS__)) = ARGS_N(19, __VA_ARGS__), CV(ARGS_N(8, __VA_ARGS__)) = ARGS_N(20, __VA_ARGS__), \ - CV(ARGS_N(9, __VA_ARGS__)) = ARGS_N(21, __VA_ARGS__), CV(ARGS_N(10, __VA_ARGS__)) = ARGS_N(22, __VA_ARGS__), \ - CV(ARGS_N(11, __VA_ARGS__)) = ARGS_N(23, __VA_ARGS__), CV(ARGS_N(12, __VA_ARGS__)) = ARGS_N(24, __VA_ARGS__) \ - } -#define CM_V13(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(14, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(15, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(16, __VA_ARGS__), CV(ARGS_N(4, __VA_ARGS__)) = ARGS_N(17, __VA_ARGS__), \ - CV(ARGS_N(5, __VA_ARGS__)) = ARGS_N(18, __VA_ARGS__), CV(ARGS_N(6, __VA_ARGS__)) = ARGS_N(19, __VA_ARGS__), \ - CV(ARGS_N(7, __VA_ARGS__)) = ARGS_N(20, __VA_ARGS__), CV(ARGS_N(8, __VA_ARGS__)) = ARGS_N(21, __VA_ARGS__), \ - CV(ARGS_N(9, __VA_ARGS__)) = ARGS_N(22, __VA_ARGS__), CV(ARGS_N(10, __VA_ARGS__)) = ARGS_N(23, __VA_ARGS__), \ - CV(ARGS_N(11, __VA_ARGS__)) = ARGS_N(24, __VA_ARGS__), CV(ARGS_N(12, __VA_ARGS__)) = ARGS_N(25, __VA_ARGS__), \ - CV(ARGS_N(13, __VA_ARGS__)) = ARGS_N(26, __VA_ARGS__), \ - } -#define CM_V14(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(15, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(16, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(17, __VA_ARGS__), CV(ARGS_N(4, __VA_ARGS__)) = ARGS_N(18, __VA_ARGS__), \ - CV(ARGS_N(5, __VA_ARGS__)) = ARGS_N(19, __VA_ARGS__), CV(ARGS_N(6, __VA_ARGS__)) = ARGS_N(20, __VA_ARGS__), \ - CV(ARGS_N(7, __VA_ARGS__)) = ARGS_N(21, __VA_ARGS__), CV(ARGS_N(8, __VA_ARGS__)) = ARGS_N(22, __VA_ARGS__), \ - CV(ARGS_N(9, __VA_ARGS__)) = ARGS_N(23, __VA_ARGS__), CV(ARGS_N(10, __VA_ARGS__)) = ARGS_N(24, __VA_ARGS__), \ - CV(ARGS_N(11, __VA_ARGS__)) = ARGS_N(25, __VA_ARGS__), CV(ARGS_N(12, __VA_ARGS__)) = ARGS_N(26, __VA_ARGS__), \ - CV(ARGS_N(13, __VA_ARGS__)) = ARGS_N(27, __VA_ARGS__), CV(ARGS_N(14, __VA_ARGS__)) = ARGS_N(28, __VA_ARGS__) \ - } -#define CM_V15(...) \ - { \ - CV(ARGS_N(1, __VA_ARGS__)) = ARGS_N(16, __VA_ARGS__), CV(ARGS_N(2, __VA_ARGS__)) = ARGS_N(17, __VA_ARGS__), \ - CV(ARGS_N(3, __VA_ARGS__)) = ARGS_N(18, __VA_ARGS__), CV(ARGS_N(4, __VA_ARGS__)) = ARGS_N(19, __VA_ARGS__), \ - CV(ARGS_N(5, __VA_ARGS__)) = ARGS_N(20, __VA_ARGS__), CV(ARGS_N(6, __VA_ARGS__)) = ARGS_N(21, __VA_ARGS__), \ - CV(ARGS_N(7, __VA_ARGS__)) = ARGS_N(22, __VA_ARGS__), CV(ARGS_N(8, __VA_ARGS__)) = ARGS_N(23, __VA_ARGS__), \ - CV(ARGS_N(9, __VA_ARGS__)) = ARGS_N(24, __VA_ARGS__), CV(ARGS_N(10, __VA_ARGS__)) = ARGS_N(25, __VA_ARGS__), \ - CV(ARGS_N(11, __VA_ARGS__)) = ARGS_N(26, __VA_ARGS__), CV(ARGS_N(12, __VA_ARGS__)) = ARGS_N(27, __VA_ARGS__), \ - CV(ARGS_N(13, __VA_ARGS__)) = ARGS_N(28, __VA_ARGS__), CV(ARGS_N(14, __VA_ARGS__)) = ARGS_N(29, __VA_ARGS__), \ - CV(ARGS_N(15, __VA_ARGS__)) = ARGS_N(30, __VA_ARGS__), \ - } - -#define BS_MISMATCH (0xFFFFFFFFu) - -#define BS_V2(c, ...) (c == ARGS_N(1, __VA_ARGS__)) ? (ARGS_N(2, __VA_ARGS__)) : BS_MISMATCH -#define BS_V4(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) ? (ARGS_N(2, __VA_ARGS__)) : ((c == ARGS_N(3, __VA_ARGS__)) ? (ARGS_N(4, __VA_ARGS__)) : BS_MISMATCH) - -#define BS_V6(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) ? (ARGS_N(6, __VA_ARGS__)) : BS_MISMATCH)) -#define BS_V8(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) ? (ARGS_N(8, __VA_ARGS__)) : BS_MISMATCH))) -#define BS_V10(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) \ - ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) ? (ARGS_N(8, __VA_ARGS__)) \ - : ((c == ARGS_N(9, __VA_ARGS__)) ? (ARGS_N(10, __VA_ARGS__)) : BS_MISMATCH)))) - -#define BS_V12(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) \ - ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) \ - ? (ARGS_N(8, __VA_ARGS__)) \ - : ((c == ARGS_N(9, __VA_ARGS__)) \ - ? (ARGS_N(10, __VA_ARGS__)) \ - : ((c == ARGS_N(11, __VA_ARGS__)) ? (ARGS_N(12, __VA_ARGS__)) : BS_MISMATCH))))) - -#define BS_V14(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) \ - ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) \ - ? (ARGS_N(8, __VA_ARGS__)) \ - : ((c == ARGS_N(9, __VA_ARGS__)) \ - ? (ARGS_N(10, __VA_ARGS__)) \ - : ((c == ARGS_N(11, __VA_ARGS__)) \ - ? (ARGS_N(12, __VA_ARGS__)) \ - : ((c == ARGS_N(13, __VA_ARGS__)) ? (ARGS_N(14, __VA_ARGS__)) : BS_MISMATCH)))))) - -#define BS_V16(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) \ - ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) \ - ? (ARGS_N(8, __VA_ARGS__)) \ - : ((c == ARGS_N(9, __VA_ARGS__)) \ - ? (ARGS_N(10, __VA_ARGS__)) \ - : ((c == ARGS_N(11, __VA_ARGS__)) \ - ? (ARGS_N(12, __VA_ARGS__)) \ - : ((c == ARGS_N(13, __VA_ARGS__)) \ - ? (ARGS_N(14, __VA_ARGS__)) \ - : ((c == ARGS_N(15, __VA_ARGS__)) ? (ARGS_N(16, __VA_ARGS__)) : BS_MISMATCH))))))) - -#define BS_V18(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) \ - ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) \ - ? (ARGS_N(8, __VA_ARGS__)) \ - : ((c == ARGS_N(9, __VA_ARGS__)) \ - ? (ARGS_N(10, __VA_ARGS__)) \ - : ((c == ARGS_N(11, __VA_ARGS__)) \ - ? (ARGS_N(12, __VA_ARGS__)) \ - : ((c == ARGS_N(13, __VA_ARGS__)) \ - ? (ARGS_N(14, __VA_ARGS__)) \ - : ((c == ARGS_N(15, __VA_ARGS__)) \ - ? (ARGS_N(16, __VA_ARGS__)) \ - : ((c == ARGS_N(17, __VA_ARGS__)) ? (ARGS_N(18, __VA_ARGS__)) \ - : BS_MISMATCH)))))))) -#define BS_V20(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) \ - ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) \ - ? (ARGS_N(8, __VA_ARGS__)) \ - : ((c == ARGS_N(9, __VA_ARGS__)) \ - ? (ARGS_N(10, __VA_ARGS__)) \ - : ((c == ARGS_N(11, __VA_ARGS__)) \ - ? (ARGS_N(12, __VA_ARGS__)) \ - : ((c == ARGS_N(13, __VA_ARGS__)) \ - ? (ARGS_N(14, __VA_ARGS__)) \ - : ((c == ARGS_N(15, __VA_ARGS__)) \ - ? (ARGS_N(16, __VA_ARGS__)) \ - : ((c == ARGS_N(17, __VA_ARGS__)) \ - ? (ARGS_N(18, __VA_ARGS__)) \ - : ((c == ARGS_N(19, __VA_ARGS__)) ? (ARGS_N(20, __VA_ARGS__)) \ - : BS_MISMATCH))))))))) - -#define BS_V22(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) \ - ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) \ - ? (ARGS_N(8, __VA_ARGS__)) \ - : ((c == ARGS_N(9, __VA_ARGS__)) \ - ? (ARGS_N(10, __VA_ARGS__)) \ - : ((c == ARGS_N(11, __VA_ARGS__)) \ - ? (ARGS_N(12, __VA_ARGS__)) \ - : ((c == ARGS_N(13, __VA_ARGS__)) \ - ? (ARGS_N(14, __VA_ARGS__)) \ - : ((c == ARGS_N(15, __VA_ARGS__)) \ - ? (ARGS_N(16, __VA_ARGS__)) \ - : ((c == ARGS_N(17, __VA_ARGS__)) \ - ? (ARGS_N(18, __VA_ARGS__)) \ - : ((c == ARGS_N(19, __VA_ARGS__)) \ - ? (ARGS_N(20, __VA_ARGS__)) \ - : ((c == ARGS_N(21, __VA_ARGS__)) ? (ARGS_N(22, __VA_ARGS__)) \ - : BS_MISMATCH)))))))))) - -#define BS_V24(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) \ - ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) \ - ? (ARGS_N(8, __VA_ARGS__)) \ - : ((c == ARGS_N(9, __VA_ARGS__)) ? (ARGS_N(10, __VA_ARGS__)) \ - : ((c == ARGS_N(11, __VA_ARGS__)) \ - ? (ARGS_N(12, __VA_ARGS__)) \ - : ((c == ARGS_N(13, __VA_ARGS__)) \ - ? (ARGS_N(14, __VA_ARGS__)) \ - : ((c == ARGS_N(15, __VA_ARGS__)) \ - ? (ARGS_N(16, __VA_ARGS__)) \ - : ((c == ARGS_N(17, __VA_ARGS__)) \ - ? (ARGS_N(18, __VA_ARGS__)) \ - : ((c == ARGS_N(19, __VA_ARGS__)) \ - ? (ARGS_N(20, __VA_ARGS__)) \ - : ((c == ARGS_N(21, __VA_ARGS__)) \ - ? (ARGS_N(22, __VA_ARGS__)) \ - : ((c == ARGS_N(23, __VA_ARGS__)) \ - ? (ARGS_N(24, __VA_ARGS__)) \ - : BS_MISMATCH))))))))))) - -#define BS_V26(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) \ - ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) \ - ? (ARGS_N(8, __VA_ARGS__)) \ - : ((c == ARGS_N(9, __VA_ARGS__)) \ - ? (ARGS_N(10, __VA_ARGS__)) \ - : ((c == ARGS_N(11, __VA_ARGS__)) \ - ? (ARGS_N(12, __VA_ARGS__)) \ - : ((c == ARGS_N(13, __VA_ARGS__)) \ - ? (ARGS_N(14, __VA_ARGS__)) \ - : ((c == ARGS_N(15, __VA_ARGS__)) \ - ? (ARGS_N(16, __VA_ARGS__)) \ - : ((c == ARGS_N(17, __VA_ARGS__)) \ - ? (ARGS_N(18, __VA_ARGS__)) \ - : ((c == ARGS_N(19, __VA_ARGS__)) \ - ? (ARGS_N(20, __VA_ARGS__)) \ - : ((c == ARGS_N(21, __VA_ARGS__)) \ - ? (ARGS_N(22, __VA_ARGS__)) \ - : ((c == ARGS_N(23, __VA_ARGS__)) \ - ? (ARGS_N(24, __VA_ARGS__)) \ - : ((c == ARGS_N(25, __VA_ARGS__)) \ - ? (ARGS_N(26, __VA_ARGS__)) \ - : BS_MISMATCH)))))))))))) - -#define BS_V28(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) \ - ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) \ - ? (ARGS_N(8, __VA_ARGS__)) \ - : ((c == ARGS_N(9, __VA_ARGS__)) \ - ? (ARGS_N(10, __VA_ARGS__)) \ - : ((c == ARGS_N(11, __VA_ARGS__)) \ - ? (ARGS_N(12, __VA_ARGS__)) \ - : ((c == ARGS_N(13, __VA_ARGS__)) \ - ? (ARGS_N(14, __VA_ARGS__)) \ - : ((c == ARGS_N(15, __VA_ARGS__)) \ - ? (ARGS_N(16, __VA_ARGS__)) \ - : ((c == ARGS_N(17, __VA_ARGS__)) \ - ? (ARGS_N(18, __VA_ARGS__)) \ - : ((c == ARGS_N(19, __VA_ARGS__)) \ - ? (ARGS_N(20, __VA_ARGS__)) \ - : ((c == ARGS_N(21, __VA_ARGS__)) \ - ? (ARGS_N(22, __VA_ARGS__)) \ - : ((c == ARGS_N(23, __VA_ARGS__)) \ - ? (ARGS_N(24, __VA_ARGS__)) \ - : ((c == ARGS_N(25, __VA_ARGS__)) \ - ? (ARGS_N(26, __VA_ARGS__)) \ - : ((c == ARGS_N(27, __VA_ARGS__)) \ - ? (ARGS_N(28, __VA_ARGS__)) \ - : BS_MISMATCH))))))))))))) - -#define BS_V30(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) \ - ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) \ - ? (ARGS_N(8, __VA_ARGS__)) \ - : ((c == ARGS_N(9, __VA_ARGS__)) \ - ? (ARGS_N(10, __VA_ARGS__)) \ - : ((c == ARGS_N(11, __VA_ARGS__)) \ - ? (ARGS_N(12, __VA_ARGS__)) \ - : ((c == ARGS_N(13, __VA_ARGS__)) \ - ? (ARGS_N(14, __VA_ARGS__)) \ - : ((c == ARGS_N(15, __VA_ARGS__)) \ - ? (ARGS_N(16, __VA_ARGS__)) \ - : ((c == ARGS_N(17, __VA_ARGS__)) \ - ? (ARGS_N(18, __VA_ARGS__)) \ - : ((c == ARGS_N(19, __VA_ARGS__)) \ - ? (ARGS_N(20, __VA_ARGS__)) \ - : ((c == ARGS_N(21, __VA_ARGS__)) \ - ? (ARGS_N(22, __VA_ARGS__)) \ - : ((c == ARGS_N(23, __VA_ARGS__)) \ - ? (ARGS_N(24, __VA_ARGS__)) \ - : ((c == ARGS_N(25, __VA_ARGS__)) \ - ? (ARGS_N(26, __VA_ARGS__)) \ - : ((c == ARGS_N(27, __VA_ARGS__)) \ - ? (ARGS_N(28, __VA_ARGS__)) \ - : ((c == ARGS_N(29, __VA_ARGS__)) \ - ? (ARGS_N(30, __VA_ARGS__)) \ - : BS_MISMATCH)))))))))))))) - -#define BS_V32(c, ...) \ - (c == ARGS_N(1, __VA_ARGS__)) \ - ? (ARGS_N(2, __VA_ARGS__)) \ - : ((c == ARGS_N(3, __VA_ARGS__)) \ - ? (ARGS_N(4, __VA_ARGS__)) \ - : ((c == ARGS_N(5, __VA_ARGS__)) \ - ? (ARGS_N(6, __VA_ARGS__)) \ - : ((c == ARGS_N(7, __VA_ARGS__)) \ - ? (ARGS_N(8, __VA_ARGS__)) \ - : ((c == ARGS_N(9, __VA_ARGS__)) \ - ? (ARGS_N(10, __VA_ARGS__)) \ - : ((c == ARGS_N(11, __VA_ARGS__)) \ - ? (ARGS_N(12, __VA_ARGS__)) \ - : ((c == ARGS_N(13, __VA_ARGS__)) \ - ? (ARGS_N(14, __VA_ARGS__)) \ - : ((c == ARGS_N(15, __VA_ARGS__)) \ - ? (ARGS_N(16, __VA_ARGS__)) \ - : ((c == ARGS_N(17, __VA_ARGS__)) \ - ? (ARGS_N(18, __VA_ARGS__)) \ - : ((c == ARGS_N(19, __VA_ARGS__)) \ - ? (ARGS_N(20, __VA_ARGS__)) \ - : ((c == ARGS_N(21, __VA_ARGS__)) \ - ? (ARGS_N(22, __VA_ARGS__)) \ - : ((c == ARGS_N(23, __VA_ARGS__)) \ - ? (ARGS_N(24, __VA_ARGS__)) \ - : ((c == ARGS_N(25, __VA_ARGS__)) \ - ? (ARGS_N(26, __VA_ARGS__)) \ - : ((c == ARGS_N(27, __VA_ARGS__)) \ - ? (ARGS_N(28, __VA_ARGS__)) \ - : ((c == ARGS_N(29, __VA_ARGS__)) \ - ? (ARGS_N(30, __VA_ARGS__)) \ - : ((c == \ - ARGS_N(31, __VA_ARGS__)) \ - ? (ARGS_N(32, \ - __VA_ARGS__)) \ - : BS_MISMATCH))))))))))))))) - -#define BSV BS_V -#define BSV_3(pre, post) MAGIC(pre, post) -#define BSV_2(pre, post) CMV_3(pre, post) -#define BS(m) CMV_2(BSV, m) -#define BS_MAP(c, ...) BS(ARGS_NUM(__VA_ARGS__))(c, __VA_ARGS__) - -#ifdef __cplusplus -} -#endif - -#endif /* _TYPEDEF_H_ */ diff --git a/include/port.h b/include/port.h index 7eab03f..d0280f1 100644 --- a/include/port.h +++ b/include/port.h @@ -4,17 +4,12 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #ifndef _PORT_H_ #define _PORT_H_ -#include "typedef.h" +#include "type_def.h" #include "arch.h" -#ifdef __cplusplus -extern "C" { -#endif - #define STACT_UNUSED_DATA (0xDEu) #define STACT_UNUSED_FRAME_MARK (0xDEDEDEDEu) @@ -143,15 +138,15 @@ static inline b_t port_isInInterruptContent(void) { register u32_t reg_ipsr __asm("ipsr"); if (reg_ipsr) { - return TRUE; + return true; } register u32_t reg_primask __asm("primask"); if (reg_primask == 0x01u) { - return TRUE; + return true; } - return FALSE; + return false; } /** @@ -161,10 +156,10 @@ static inline b_t port_isInThreadMode(void) { register u32_t reg_ipsr __asm("ipsr"); if (reg_ipsr) { - return FALSE; + return false; } - return TRUE; + return true; } #endif @@ -200,17 +195,17 @@ static inline b_t port_isInInterruptContent(void) __asm__ volatile("mrs %0, IPSR\n\t" : "=r"(ipsr)); if (ipsr) { - return TRUE; + return true; } u32_t primask; __ASM volatile("MRS %0, primask" : "=r"(primask)); if (primask) { - return TRUE; + return true; } - return FALSE; + return false; } /** @@ -222,10 +217,10 @@ static inline b_t port_isInThreadMode(void) __asm__ volatile("mrs %0, IPSR\n\t" : "=r"(ipsr)); if (reg_ipsr) { - return FALSE; + return false; } - return TRUE; + return true; } #endif @@ -250,15 +245,15 @@ static inline b_t port_isInInterruptContent(void) { register u32_t reg_ipsr = __arm_rsr("IPSR"); if (reg_ipsr) { - return TRUE; + return true; } register u32_t reg_primask = __arm_rsr("PRIMASK"); if (reg_primask == 0x01u) { - return TRUE; + return true; } - return FALSE; + return false; } /** @@ -268,10 +263,10 @@ static inline b_t port_isInThreadMode(void) { register u32_t reg_ipsr = __arm_rsr("IPSR"); if (reg_ipsr) { - return FALSE; + return false; } - return TRUE; + return true; } #endif @@ -301,8 +296,4 @@ void port_setPendSV(void); void port_interrupt_init(void); u32_t port_stack_frame_init(void (*pEntryFunction)(void), u32_t *pAddress, u32_t size); -#ifdef __cplusplus -} -#endif - #endif /* _PORT_H_ */ diff --git a/include/template/atos_configuration.h b/include/template/atos_configuration.h index ccec89f..796e56c 100644 --- a/include/template/atos_configuration.h +++ b/include/template/atos_configuration.h @@ -4,14 +4,9 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #ifndef _ATOS_CONFIGURATION_H_ #define _ATOS_CONFIGURATION_H_ -#ifdef __cplusplus -extern "C" { -#endif - /** * If you are use ARM Cortex M seiral architecture, the Cortex-M Core must be specificed as the following list. * ARCH_ARM_CORTEX_CM3 @@ -72,49 +67,49 @@ extern "C" { * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual thread instance number that you created. **/ -#define THREAD_INSTANCE_SUPPORTED_NUMBER (20u) +#define THREAD_RUNTIME_NUMBER_SUPPORTED (20u) /** * This symbol defined the semaphore instance number that your application is using. * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual semaphore instance number that you created. **/ -#define SEMAPHORE_INSTANCE_SUPPORTED_NUMBER (10u) +#define SEMAPHORE_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the event instance number that your application is using. * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual event instance number that you created. **/ -#define EVENT_INSTANCE_SUPPORTED_NUMBER (10u) +#define EVENT_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the mutex instance number that your application is using. * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual mutex instance number that you created. **/ -#define MUTEX_INSTANCE_SUPPORTED_NUMBER (10u) +#define MUTEX_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the queue instance number that your application is using. * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual queue instance number that you created. **/ -#define QUEUE_INSTANCE_SUPPORTED_NUMBER (10u) +#define QUEUE_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the timer instance number that your application is using. * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual timer instance number that you created. **/ -#define TIMER_INSTANCE_SUPPORTED_NUMBER (10u) +#define TIMER_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the timer instance number that your application is using. * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual timer instance number that you created. **/ -#define POOL_INSTANCE_SUPPORTED_NUMBER (10u) +#define POOL_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the timer instance number that your application is using. @@ -122,7 +117,7 @@ extern "C" { * different value so set this correctly. This is very often, but not always, * according to the actual timer instance number that you created. **/ -#define PUBLISH_INSTANCE_SUPPORTED_NUMBER (10u) +#define PUBLISH_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the timer instance number that your application is using. @@ -130,7 +125,7 @@ extern "C" { * different value so set this correctly. This is very often, but not always, * according to the actual timer instance number that you created. **/ -#define SUBSCRIBE_INSTANCE_SUPPORTED_NUMBER (10u) +#define SUBSCRIBE_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined your thread running mode, if the thread runs at the privileged mode. @@ -139,20 +134,4 @@ extern "C" { **/ #define THREAD_PSP_WITH_PRIVILEGED (10u) -/** - * This symbol defined your kernel trace print function, if your system implemented - * standard printf function, you can define this macro to use printf as the At-RTOS kernel trace use. - * The defaule standard printf macro is commented out. Or you can defined your customization-printf to replace - * the standard printf by use macro KTRACE, If you don't need the kernel trace feature, you can comment out both of them. - * Your application will certainly need a different value so set this correctly. - * This is very often, but not always, according to the security level that you want. - **/ -// Both of them commented out disabled the kernel trace feature -// #define AT_RTOS_TRACE_USE_STANDARD_PRINTF_ENABLED /* Comment out: default doesn't use the standard printf */ -// #define KTRACE /* Comment out: default doesn't use the customization-printf */ - -#ifdef __cplusplus -} -#endif - #endif /* _ATOS_CONFIGURATION_H_ */ diff --git a/include/template/native_gcc/atos_configuration.h b/include/template/native_gcc/atos_configuration.h index ccec89f..796e56c 100644 --- a/include/template/native_gcc/atos_configuration.h +++ b/include/template/native_gcc/atos_configuration.h @@ -4,14 +4,9 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #ifndef _ATOS_CONFIGURATION_H_ #define _ATOS_CONFIGURATION_H_ -#ifdef __cplusplus -extern "C" { -#endif - /** * If you are use ARM Cortex M seiral architecture, the Cortex-M Core must be specificed as the following list. * ARCH_ARM_CORTEX_CM3 @@ -72,49 +67,49 @@ extern "C" { * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual thread instance number that you created. **/ -#define THREAD_INSTANCE_SUPPORTED_NUMBER (20u) +#define THREAD_RUNTIME_NUMBER_SUPPORTED (20u) /** * This symbol defined the semaphore instance number that your application is using. * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual semaphore instance number that you created. **/ -#define SEMAPHORE_INSTANCE_SUPPORTED_NUMBER (10u) +#define SEMAPHORE_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the event instance number that your application is using. * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual event instance number that you created. **/ -#define EVENT_INSTANCE_SUPPORTED_NUMBER (10u) +#define EVENT_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the mutex instance number that your application is using. * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual mutex instance number that you created. **/ -#define MUTEX_INSTANCE_SUPPORTED_NUMBER (10u) +#define MUTEX_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the queue instance number that your application is using. * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual queue instance number that you created. **/ -#define QUEUE_INSTANCE_SUPPORTED_NUMBER (10u) +#define QUEUE_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the timer instance number that your application is using. * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual timer instance number that you created. **/ -#define TIMER_INSTANCE_SUPPORTED_NUMBER (10u) +#define TIMER_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the timer instance number that your application is using. * The defaule value is set to 1. Your application will certainly need a different value so set this correctly. * This is very often, but not always, according to the actual timer instance number that you created. **/ -#define POOL_INSTANCE_SUPPORTED_NUMBER (10u) +#define POOL_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the timer instance number that your application is using. @@ -122,7 +117,7 @@ extern "C" { * different value so set this correctly. This is very often, but not always, * according to the actual timer instance number that you created. **/ -#define PUBLISH_INSTANCE_SUPPORTED_NUMBER (10u) +#define PUBLISH_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined the timer instance number that your application is using. @@ -130,7 +125,7 @@ extern "C" { * different value so set this correctly. This is very often, but not always, * according to the actual timer instance number that you created. **/ -#define SUBSCRIBE_INSTANCE_SUPPORTED_NUMBER (10u) +#define SUBSCRIBE_RUNTIME_NUMBER_SUPPORTED (10u) /** * This symbol defined your thread running mode, if the thread runs at the privileged mode. @@ -139,20 +134,4 @@ extern "C" { **/ #define THREAD_PSP_WITH_PRIVILEGED (10u) -/** - * This symbol defined your kernel trace print function, if your system implemented - * standard printf function, you can define this macro to use printf as the At-RTOS kernel trace use. - * The defaule standard printf macro is commented out. Or you can defined your customization-printf to replace - * the standard printf by use macro KTRACE, If you don't need the kernel trace feature, you can comment out both of them. - * Your application will certainly need a different value so set this correctly. - * This is very often, but not always, according to the security level that you want. - **/ -// Both of them commented out disabled the kernel trace feature -// #define AT_RTOS_TRACE_USE_STANDARD_PRINTF_ENABLED /* Comment out: default doesn't use the standard printf */ -// #define KTRACE /* Comment out: default doesn't use the customization-printf */ - -#ifdef __cplusplus -} -#endif - #endif /* _ATOS_CONFIGURATION_H_ */ diff --git a/kernel/event.c b/kernel/event.c index 86df294..0c9f9d1 100644 --- a/kernel/event.c +++ b/kernel/event.c @@ -4,33 +4,17 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "kernel.h" #include "timer.h" #include "postcode.h" #include "trace.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "init.h" /** * Local unique postcode. */ #define PC_EOR PC_IER(PC_OS_CMPT_EVENT_7) -/** - * @brief Get the event context based on provided unique id. - * - * @param id The event unique id. - * - * @return The pointer of the current unique id event context. - */ -static event_context_t *_event_context_get(os_id_t id) -{ - return (event_context_t *)(kernel_member_unified_id_toContainerAddress(id)); -} - /** * @brief Check if the event unique id if is's invalid. * @@ -38,9 +22,13 @@ static event_context_t *_event_context_get(os_id_t id) * * @return The true is invalid, otherwise is valid. */ -static b_t _event_id_isInvalid(u32_t id) +static b_t _event_context_isInvalid(event_context_t *pCurEvt) { - return kernel_member_unified_id_isInvalid(KERNEL_MEMBER_EVENT, id); + u32_t start, end; + INIT_SECTION_FIRST(INIT_SECTION_OS_EVENT_LIST, start); + INIT_SECTION_LAST(INIT_SECTION_OS_EVENT_LIST, end); + + return ((u32_t)pCurEvt < start || (u32_t)pCurEvt >= end) ? true : false; } /** @@ -50,25 +38,9 @@ static b_t _event_id_isInvalid(u32_t id) * * @return The true is initialized, otherwise is uninitialized. */ -static b_t _event_id_isInit(u32_t id) +static b_t _event_context_isInit(event_context_t *pCurEvt) { - event_context_t *pCurEvent = _event_context_get(id); - - return ((pCurEvent) ? (((pCurEvent->head.cs) ? (TRUE) : (FALSE))) : FALSE); -} - -/** - * @brief Pick up a highest priority thread that blocking by the event pending list. - * - * @param The event unique id. - * - * @return The highest blocking thread head. - */ -static list_t *_event_list_blockingHeadGet(os_id_t id) -{ - event_context_t *pCurEvent = _event_context_get(id); - - return (list_t *)((pCurEvent) ? (&pCurEvent->blockingThreadHead) : (NULL)); + return ((pCurEvt) ? (((pCurEvt->head.cs) ? (true) : (false))) : false); } /** @@ -76,15 +48,15 @@ static list_t *_event_list_blockingHeadGet(os_id_t id) * * @param id The unique id of the entry thread. */ -static void _event_schedule(os_id_t id) +static void _event_schedule(void *pTask) { - thread_context_t *pEntryThread = (thread_context_t *)(kernel_member_unified_id_toContainerAddress(id)); + struct schedule_task *pCurTask = (struct schedule_task *)pTask; - timeout_remove(&pEntryThread->expire, true); + timeout_remove(&pCurTask->expire, true); u32_t changed, any, edge, level, trigger = 0u; - event_context_t *pCurEvent = (event_context_t*)pEntryThread->schedule.pPendCtx; - event_sch_t *pEvt_sche = (event_sch_t *)pEntryThread->schedule.pPendData; + event_context_t *pCurEvent = (event_context_t *)pCurTask->pPendCtx; + event_sch_t *pEvt_sche = (event_sch_t *)pCurTask->pPendData; if (!pEvt_sche) { return; } @@ -126,7 +98,7 @@ static void _event_schedule(os_id_t id) pCurEvent->triggered &= ~report; } - pEntryThread->schedule.entry.result = 0; + pCurTask->exec.entry.result = 0; } /** @@ -145,18 +117,14 @@ static u32_t _event_init_privilege_routine(arguments_t *pArgs) u32_t dirMask = (u32_t)(pArgs[2].u32_val); u32_t init = (u32_t)(pArgs[3].u32_val); const char_t *pName = (const char_t *)(pArgs[4].pch_val); - u32_t endAddr = 0u; - event_context_t *pCurEvent = NULL; - - pCurEvent = (event_context_t *)kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_EVENT); - endAddr = (u32_t)kernel_member_id_toContainerEndAddress(KERNEL_MEMBER_EVENT); - do { - os_id_t id = kernel_member_containerAddress_toUnifiedid((u32_t)pCurEvent); - if (_event_id_isInvalid(id)) { + + INIT_SECTION_FOREACH(INIT_SECTION_OS_EVENT_LIST, event_context_t, pCurEvent) + { + if (_event_context_isInvalid(pCurEvent)) { break; } - if (_event_id_isInit(id)) { + if (_event_context_isInit(pCurEvent)) { continue; } @@ -172,12 +140,11 @@ static u32_t _event_init_privilege_routine(arguments_t *pArgs) pCurEvent->call.pEvtCallEntry = NULL; EXIT_CRITICAL_SECTION(); - return id; - - } while ((u32_t)++pCurEvent < endAddr); + return (u32_t)pCurEvent; + }; EXIT_CRITICAL_SECTION(); - return OS_INVALID_ID_VAL; + return 0u; } /** @@ -191,9 +158,8 @@ static i32p_t _event_value_get_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; + event_context_t *pCurEvent = (event_context_t *)pArgs[0].u32_val; u32_t *pValue = (u32_t *)pArgs[1].pv_val; - event_context_t *pCurEvent = _event_context_get(id); *pValue = pCurEvent->value; EXIT_CRITICAL_SECTION(); @@ -211,12 +177,11 @@ static i32p_t _event_set_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; + event_context_t *pCurEvent = (event_context_t *)pArgs[0].u32_val; u32_t set = (u32_t)pArgs[1].u32_val; u32_t clear = (u32_t)pArgs[2].u32_val; u32_t toggle = (u32_t)pArgs[3].u32_val; - event_context_t *pCurEvent = _event_context_get(id); u32_t val = pCurEvent->value; u32_t changed, any, edge, level, trigger = 0u; i32p_t postcode = 0; @@ -261,10 +226,11 @@ static i32p_t _event_set_privilege_routine(arguments_t *pArgs) u32_t report, reported = 0u; list_iterator_t it = {0u}; - list_iterator_init(&it, _event_list_blockingHeadGet(id)); - thread_context_t *pCurThread = (thread_context_t *)list_iterator_next(&it); - while (pCurThread) { - event_sch_t *pEvt_sche = (event_sch_t *)pCurThread->schedule.pPendData; + list_t *pList = (list_t *)&pCurEvent->q_list; + list_iterator_init(&it, pList); + struct schedule_task *pCurTask = (struct schedule_task *)list_iterator_next(&it); + while (pCurTask) { + event_sch_t *pEvt_sche = (event_sch_t *)pCurTask->pPendData; if (!pEvt_sche) { return PC_EOR; } @@ -273,13 +239,13 @@ static i32p_t _event_set_privilege_routine(arguments_t *pArgs) reported |= report; pEvt_sche->pEvtVal->trigger = trigger; pEvt_sche->pEvtVal->value = val; - postcode = kernel_thread_entry_trigger(pCurThread, 0, _event_schedule); + postcode = schedule_entry_trigger(pCurTask, _event_schedule, 0u); PC_IF(postcode, PC_ERROR) { break; } } - pCurThread = (thread_context_t *)list_iterator_next(&it); + pCurTask = (struct schedule_task *)list_iterator_next(&it); } pCurEvent->triggered = (~reported) & trigger; pCurEvent->value = val; @@ -299,14 +265,13 @@ static i32p_t _event_wait_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; + event_context_t *pCurEvent = (event_context_t *)pArgs[0].u32_val; event_sch_t *pEvt_sch = (event_sch_t *)pArgs[1].pv_val; u32_t timeout_ms = (u32_t)pArgs[2].u32_val; i32p_t postcode = 0; thread_context_t *pCurThread = kernel_thread_runContextGet(); - event_context_t *pCurEvent = _event_context_get(id); - os_evt_val_t *pEvtData = pEvt_sch->pEvtVal; + struct evt_val *pEvtData = pEvt_sch->pEvtVal; u32_t changed, any, edge, level, trigger = 0u; changed = pEvtData->value ^ pCurEvent->value; @@ -350,9 +315,7 @@ static i32p_t _event_wait_privilege_routine(arguments_t *pArgs) EXIT_CRITICAL_SECTION(); return postcode; } - - pCurThread->schedule.pPendData = (void *)pEvt_sch; - postcode = kernel_thread_exit_trigger(pCurThread, pCurEvent, _event_list_blockingHeadGet(id), timeout_ms); + postcode = schedule_exit_trigger(&pCurThread->task, pCurEvent, pEvt_sch, &pCurEvent->q_list, timeout_ms, true); PC_IF(postcode, PC_PASS) { postcode = PC_OS_WAIT_UNAVAILABLE; @@ -362,22 +325,6 @@ static i32p_t _event_wait_privilege_routine(arguments_t *pArgs) return postcode; } -/** - * @brief Convert the internal os id to kernel member number. - * - * @param id The provided unique id. - * - * @return The value of member number. - */ -u32_t _impl_event_os_id_to_number(os_id_t id) -{ - if (_event_id_isInvalid(id)) { - return 0u; - } - - return (u32_t)((id - kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_EVENT)) / sizeof(event_context_t)); -} - /** * @brief Initialize a new event. * @@ -389,7 +336,7 @@ u32_t _impl_event_os_id_to_number(os_id_t id) * * @return The event unique id. */ -os_id_t _impl_event_init(u32_t anyMask, u32_t modeMask, u32_t dirMask, u32_t init, const char_t *pName) +u32_t _impl_event_init(u32_t anyMask, u32_t modeMask, u32_t dirMask, u32_t init, const char_t *pName) { arguments_t arguments[] = { [0] = {.u32_val = (u32_t)anyMask}, [1] = {.u32_val = (u32_t)modeMask}, [2] = {.u32_val = (u32_t)dirMask}, @@ -399,11 +346,6 @@ os_id_t _impl_event_init(u32_t anyMask, u32_t modeMask, u32_t dirMask, u32_t ini return kernel_privilege_invoke((const void *)_event_init_privilege_routine, arguments); } -i32p_t _impl_event_wait_callfunc_register(pEvent_callbackFunc_t pCallFun) -{ - return 0u; -} - /** * @brief Read or write the event signal value. * @@ -412,18 +354,19 @@ i32p_t _impl_event_wait_callfunc_register(pEvent_callbackFunc_t pCallFun) * * @return The result of the operation. */ -i32p_t _impl_event_value_get(os_id_t id, u32_t *pValue) +i32p_t _impl_event_value_get(u32_t ctx, u32_t *pValue) { - if (_event_id_isInvalid(id)) { + event_context_t *pCtx = (event_context_t *)ctx; + if (_event_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_event_id_isInit(id)) { + if (!_event_context_isInit(pCtx)) { return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, [1] = {.pv_val = (void *)pValue}, }; @@ -440,18 +383,19 @@ i32p_t _impl_event_value_get(os_id_t id, u32_t *pValue) * * @return The result of the operation. */ -i32p_t _impl_event_set(os_id_t id, u32_t set, u32_t clear, u32_t toggle) +i32p_t _impl_event_set(u32_t ctx, u32_t set, u32_t clear, u32_t toggle) { - if (_event_id_isInvalid(id)) { + event_context_t *pCtx = (event_context_t *)ctx; + if (_event_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_event_id_isInit(id)) { + if (!_event_context_isInit(pCtx)) { return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, [1] = {.u32_val = (u32_t)set}, [2] = {.u32_val = (u32_t)clear}, [3] = {.u32_val = (u32_t)toggle}, @@ -470,13 +414,14 @@ i32p_t _impl_event_set(os_id_t id, u32_t set, u32_t clear, u32_t toggle) * * @return The result of the operation. */ -i32p_t _impl_event_wait(os_id_t id, os_evt_val_t *pEvtData, u32_t listen_mask, u32_t timeout_ms) +i32p_t _impl_event_wait(u32_t ctx, struct evt_val *pEvtData, u32_t listen_mask, u32_t timeout_ms) { - if (_event_id_isInvalid(id)) { + event_context_t *pCtx = (event_context_t *)ctx; + if (_event_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_event_id_isInit(id)) { + if (!_event_context_isInit(pCtx)) { return PC_EOR; } @@ -497,7 +442,7 @@ i32p_t _impl_event_wait(os_id_t id, os_evt_val_t *pEvtData, u32_t listen_mask, u .pEvtVal = pEvtData, }; arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, [1] = {.pv_val = (void *)&evt_sch}, [2] = {.u32_val = (u32_t)timeout_ms}, }; @@ -520,7 +465,3 @@ i32p_t _impl_event_wait(os_id_t id, os_evt_val_t *pEvtData, u32_t listen_mask, u EXIT_CRITICAL_SECTION(); return postcode; } - -#ifdef __cplusplus -} -#endif diff --git a/kernel/init.c b/kernel/init.c new file mode 100644 index 0000000..e324981 --- /dev/null +++ b/kernel/init.c @@ -0,0 +1,51 @@ +/** + * Copyright (c) Riven Zheng (zhengheiot@gmail.com). + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + **/ +#include "init.h" + +//#pragma section = "_INIT_SECTION_FUNC"//#pragma section = "_INIT_SECTION_OS_THREAD_STATIC" + +void init_func_level(u8_t level) +{ +#if 1 +// #pragma section = INIT_SECTION_FUNC + + INIT_SECTION_FOREACH(INIT_SECTION_FUNC, init_func_t, ifun) + { + if (ifun->level == level && ifun) { + ifun->func(); + } + } +#endif + +#if 0 + #pragma section = "_INIT_SECTION_FUNC" + for (init_func_t *ifun = (init_func_t *)__section_begin("_INIT_SECTION_FUNC"); ifun < (init_func_t *)(__section_end("_INIT_SECTION_FUNC")); ifun++) { + if (ifun->level == level && ifun) { + ifun->func(); + } + } +#endif +} + +void init_func_list(void) +{ + for (u8_t lv = INIT_LEVEL_0; lv < INIT_LEVEL_NUM; lv++) { + init_func_level(lv); + } +} + +void init_static_thread_list(void) +{ + extern void _impl_thread_static_init(thread_context_t * pCurThread); + + INIT_SECTION_FOREACH(INIT_SECTION_OS_THREAD_STATIC, thread_context_init_t, pInit) + { + if (pInit->pThread) { + _impl_thread_static_init(pInit->pThread); + } + } +} diff --git a/kernel/kernel.c b/kernel/kernel.c index b3b2b95..e317db0 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -4,7 +4,6 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "at_rtos.h" #include "kernel.h" #include "timer.h" @@ -13,163 +12,40 @@ #include "ktype.h" #include "postcode.h" #include "trace.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "init.h" /** * Local unique postcode. */ -#define _PCER PC_IER(PC_OS_CMPT_KERNEL_2) - -/** - * Local kernel member setting container. - */ -static kernel_member_setting_t g_kernel_member_setting[KERNEL_MEMBER_NUMBER] = { - [KERNEL_MEMBER_THREAD] = {KERNEL_MEMBER_MAP_1, (SET_BITS(KERNEL_MEMBER_LIST_THREAD_WAIT, KERNEL_MEMBER_LIST_THREAD_EXIT))}, - [KERNEL_MEMBER_TIMER_INTERNAL] = {KERNEL_MEMBER_MAP_2, (SET_BITS(KERNEL_MEMBER_LIST_TIMER_STOP, KERNEL_MEMBER_LIST_TIMER_RUN))}, - [KERNEL_MEMBER_TIMER] = {KERNEL_MEMBER_MAP_3, (SET_BITS(KERNEL_MEMBER_LIST_TIMER_STOP, KERNEL_MEMBER_LIST_TIMER_RUN))}, - [KERNEL_MEMBER_SEMAPHORE] = {KERNEL_MEMBER_MAP_4, (SET_BIT(KERNEL_MEMBER_LIST_SEMAPHORE_INIT))}, - [KERNEL_MEMBER_MUTEX] = {KERNEL_MEMBER_MAP_5, (SET_BITS(KERNEL_MEMBER_LIST_MUTEX_LOCK, KERNEL_MEMBER_LIST_MUTEX_UNLOCK))}, - [KERNEL_MEMBER_EVENT] = {KERNEL_MEMBER_MAP_6, (SET_BIT(KERNEL_MEMBER_LIST_EVENT_INIT))}, - [KERNEL_MEMBER_QUEUE] = {KERNEL_MEMBER_MAP_7, (SET_BIT(KERNEL_MEMBER_LIST_QUEUE_INIT))}, - [KERNEL_MEMBER_POOL] = {KERNEL_MEMBER_MAP_8, (SET_BIT(KERNEL_MEMBER_LIST_POOL_INIT))}, - [KERNEL_MEMBER_PUBLISH] = {KERNEL_MEMBER_MAP_9, (SET_BITS(KERNEL_MEMBER_LIST_PUBLISH_INIT, KERNEL_MEMBER_LIST_PUBLISH_PEND))}, - [KERNEL_MEMBER_SUBSCRIBE] = {KERNEL_MEMBER_MAP_10, (SET_BIT(KERNEL_MEMBER_LIST_SUBSCRIBE_INIT))}, -}; - -/** - * Local kernel member container. - */ -static u8_t g_kernel_member_container[KERNEL_MEMBER_MAP_NUMBER] = {0u}; - -/** - * Local kernel supported member list container. - */ -static list_t g_kernel_member_list[KERNEL_MEMBER_LIST_NUMBER] = {LIST_NULL}; - -/** - * Local kernel resource - */ -static kernel_context_t g_kernel_resource = { - .current = 0u, - .list = LIST_NULL, - .run = FALSE, - .pendsv_ms = 0u, - .member = - { - .pListContainer = (list_t *)&g_kernel_member_list[0], - .pMemoryContainer = (u8_t *)&g_kernel_member_container[0], - .pSetting = (kernel_member_setting_t *)&g_kernel_member_setting[0], - }, -}; - -/** - * The local function lists for current file internal use. - */ -static i32p_t _kernel_start_privilege_routine(arguments_t *pArgs); - -/** - * @brief To set a PendSV. - */ -static void _kernel_setPendSV(void) -{ - port_setPendSV(); -} - -/** - * @brief Check if kernel is in privilege mode. - * - * @return The true indicates the kernel is in privilege mode. - */ -static b_t _kernel_isInPrivilegeMode(void) -{ - return port_isInInterruptContent(); -} - -/** - * @brief Update pendsv executed time. - */ -static void _kernel_pendsv_time_update(void) -{ - g_kernel_resource.pendsv_ms = timer_total_system_ms_get(); -} +#define PC_EOR PC_IER(PC_OS_CMPT_KERNEL_2) /** - * @brief Get pendsv executed time. - * - * @return The value of pendsv executed time. + * Data structure for location timer */ -static u32_t _kernel_pendsv_time_get(void) -{ - return g_kernel_resource.pendsv_ms; -} +typedef struct { + struct schedule_task *pTask; -/** - * @brief kernel schedule exit time analyze. - */ -static void _kernel_schedule_exit_time_analyze(os_id_t id) -{ - /* Nothing to do */ -} + b_t run; -/** - * @brief kernel schedule entry time analyze. - */ -static void _kernel_schedule_entry_time_analyze(os_id_t id) -{ -#if defined KTRACE - thread_context_t *pEntryThread = (thread_context_t *)kernel_member_unified_id_toContainerAddress(id); - pEntryThread->schedule.analyze.pend_ms = _kernel_pendsv_time_get(); -#endif -} + u32_t pendsv_ms; -/** - * @brief kernel schedule run time analyze. - */ -static void _kernel_schedule_run_time_analyze(os_id_t from, os_id_t to) -{ -#if defined KTRACE - thread_context_t *pFromThread = (thread_context_t *)kernel_member_unified_id_toContainerAddress(from); - thread_context_t *pToThread = (thread_context_t *)kernel_member_unified_id_toContainerAddress(to); - u32_t sv_ms = _kernel_pendsv_time_get(); + list_t sch_pend_list; - pFromThread->schedule.analyze.active_ms += (u32_t)(sv_ms - pFromThread->schedule.analyze.run_ms); + list_t sch_entry_list; - pFromThread->schedule.analyze.exit_ms = sv_ms; - pToThread->schedule.analyze.run_ms = sv_ms; -#endif -} + list_t sch_exit_list; -/** - * @brief Get the thread PSP stack address. - * - * @param id The thread unique id. - * - * @return The PSP stacke address. - */ -static u32_t *_kernel_thread_PSP_Get(os_id_t id) -{ - thread_context_t *pCurThread = (thread_context_t *)kernel_member_unified_id_toContainerAddress(id); - - return (u32_t *)&pCurThread->psp; -} + list_t sch_wait_list; +} _kernel_resource_t; /** - * @brief Get the next thread id. - * - * @return The next thread id. + * Local kernel resource */ -static os_id_t _kernel_thread_nextIdGet(void) -{ - ENTER_CRITICAL_SECTION(); - list_t *pListPending = (list_t *)kernel_list_pendingHeadGet(); - linker_head_t *pHead = (linker_head_t *)(pListPending->pHead); - EXIT_CRITICAL_SECTION(); - - return pHead->id; -} +static _kernel_resource_t g_kernel_rsc = { + .pTask = NULL, + .run = false, + .pendsv_ms = 0u, +}; /** * @brief Compare the priority between the current and extract thread. @@ -179,606 +55,330 @@ static os_id_t _kernel_thread_nextIdGet(void) * * @return The false indicates it's a right position and it can kill the loop calling. */ -static b_t _kernel_thread_node_Order_compare_condition(list_node_t *pCurNode, list_node_t *pExtractNode) +static b_t _schedule_priority_node_order_compare_condition(list_node_t *pCurNode, list_node_t *pExtractNode) { - thread_context_t *pCurThread = (thread_context_t *)pCurNode; - thread_context_t *pExtractThread = (thread_context_t *)pExtractNode; + struct schedule_task *pCurTask = (struct schedule_task *)pCurNode; + struct schedule_task *pExtractTask = (struct schedule_task *)pExtractNode; - if ((!pCurThread) || (!pExtractThread)) { + if ((!pCurTask) || (!pExtractTask)) { /* no available thread */ - return FALSE; + return false; } - if (pCurThread->priority.level <= pExtractThread->priority.level) { + if (pCurTask->prior <= pExtractTask->prior) { /* Find a right position and doesn't has to do schedule */ - return FALSE; + return false; } - return TRUE; + return true; } -/** - * @brief Push one thread context into target list. - * - * @param pCurHead The pointer of the thread linker head. - */ -static void _kernel_thread_list_transfer_toTargetBlocking(linker_head_t *pCurHead, list_t *pToBlockingList) +static void _schedule_transfer_toEntryList(linker_t *pLinker) { ENTER_CRITICAL_SECTION(); - if (pToBlockingList) { - linker_list_transaction_specific(&pCurHead->linker, pToBlockingList, _kernel_thread_node_Order_compare_condition); - } + list_t *pToList = (list_t *)&g_kernel_rsc.sch_entry_list; + linker_list_transaction_common(pLinker, pToList, LIST_TAIL); EXIT_CRITICAL_SECTION(); } -/** - * @brief kernel thread try to do entry schedule in the PendSV routine. - */ -static void _kernel_thread_entry_schedule(void) +static void _schedule_transfer_toNullList(linker_t *pLinker) { - thread_context_t *pCurThread = NULL; - list_iterator_t it = ITERATION_NULL; - thread_entry_t *pEntry = NULL; - - list_t *pList = (list_t *)kernel_member_list_get(KERNEL_MEMBER_THREAD, KERNEL_MEMBER_LIST_THREAD_ENTRY); - list_iterator_init(&it, pList); - while (list_iterator_next_condition(&it, (void *)&pCurThread)) { - pEntry = &pCurThread->schedule.entry; + ENTER_CRITICAL_SECTION(); - if (pEntry->pEntryCallFun) { - pEntry->pEntryCallFun(pCurThread->head.id); - pEntry->pEntryCallFun = NULL; - } - pCurThread->schedule.pPendCtx = NULL; + linker_list_transaction_common(pLinker, NULL, LIST_TAIL); - _kernel_schedule_entry_time_analyze(pCurThread->head.id); - kernel_thread_list_transfer_toPend((linker_head_t *)&pCurThread->head); - } + EXIT_CRITICAL_SECTION(); } /** - * @brief kernel thread try to do exit schedule in the PendSV routine. + * @brief Push one thread context into target list. * - * @return The true indicates the kernel request a timer schedule. + * @param pCurHead The pointer of the thread linker head. */ -static b_t _kernel_thread_exit_schedule(void) +static void _schedule_transfer_toTargetList(linker_t *pLinker, list_t *pToList) { - list_iterator_t it = ITERATION_NULL; - thread_context_t *pCurThread = NULL; - thread_exit_t *pExit = NULL; - b_t need = false; - - /* The thread block */ - list_t *pList = (list_t *)kernel_member_list_get(KERNEL_MEMBER_THREAD, KERNEL_MEMBER_LIST_THREAD_EXIT); - list_iterator_init(&it, pList); - while (list_iterator_next_condition(&it, (void *)&pCurThread)) { - pExit = &pCurThread->schedule.exit; + ENTER_CRITICAL_SECTION(); - if (pExit->timeout_us) { - os_id_t id = kernel_member_unified_id_threadToTimer(pCurThread->head.id); - timeout_set(&pCurThread->expire, pExit->timeout_us, false); - if (pExit->timeout_us != OS_TIME_FOREVER_VAL) { - need = true; - } - } - _kernel_schedule_exit_time_analyze(pCurThread->head.id); - _kernel_thread_list_transfer_toTargetBlocking((linker_head_t *)&pCurThread->head, (list_t *)pExit->pToList); - pCurThread->schedule.entry.result = _PCER; + if (pToList) { + linker_list_transaction_specific(pLinker, pToList, _schedule_priority_node_order_compare_condition); } - return need; + EXIT_CRITICAL_SECTION(); } -/** - * @brief It's sub-routine running at privilege mode. - * - * @param pArgs The function argument packages. - * - * @return The result of privilege routine. - */ -static i32p_t _kernel_start_privilege_routine(arguments_t *pArgs) +static void _schedule_transfer_toExitList(linker_t *pLinker) { - UNUSED_MSG(pArgs); - ENTER_CRITICAL_SECTION(); - kthread_init(); - port_interrupt_init(); - clock_time_init(timeout_handler); - - g_kernel_resource.current = _kernel_thread_nextIdGet(); - g_kernel_resource.run = TRUE; + list_t *pToList = (list_t *)&g_kernel_rsc.sch_exit_list; + linker_list_transaction_specific(pLinker, pToList, _schedule_priority_node_order_compare_condition); EXIT_CRITICAL_SECTION(); - - port_run_theFirstThread(*(_kernel_thread_PSP_Get(g_kernel_resource.current))); - - // nothing arrive - return _PCER; -} - -/** - * @brief Get the kernel member ending unified id according to the member id. - * - * @param member_id The kernel member id. - * - * @return The value of the kernel member ending unified id. - */ -static u32_t _kernel_member_id_toUnifiedIdEnd(u8_t member_id) -{ - if (member_id >= KERNEL_MEMBER_NUMBER) { - return OS_INVALID_ID_VAL; - } - - return (u32_t)g_kernel_resource.member.pSetting[member_id].mem; -} - -/** - * @brief Get the kernel member unified id range according to the member id. - * - * @param member_id The kernel member id. - * - * @return The value of the kernel member unified id range. - */ -static u32_t _kernel_member_id_toUnifiedIdRange(u8_t member_id) -{ - if (member_id >= KERNEL_MEMBER_NUMBER) { - return 0u; - } - - return (u32_t)(_kernel_member_id_toUnifiedIdEnd(member_id) - kernel_member_id_toUnifiedIdStart(member_id)); -} - -/** - * @brief To check if the kernel message arrived. - */ -static i32p_t _kernel_message_arrived(void) -{ - return kthread_message_arrived(); } -/** - * @brief Get pendsv executed time. - * - * @return The value of pendsv executed time. - */ -u32_t kernel_schedule_time_get(void) -{ - return _kernel_pendsv_time_get(); -} - -/** - * @brief Push one semaphore context into lock list. - * - * @param pCurHead The pointer of the semaphore linker head. - */ -void kernel_semaphore_list_transfer_toInit(linker_head_t *pCurHead) +static void _schedule_transfer_toPendList(linker_t *pLinker) { ENTER_CRITICAL_SECTION(); - list_t *pToLockList = (list_t *)(list_t *)kernel_member_list_get(KERNEL_MEMBER_SEMAPHORE, KERNEL_MEMBER_LIST_SEMAPHORE_INIT); - linker_list_transaction_common(&pCurHead->linker, pToLockList, LIST_TAIL); + list_t *pToList = (list_t *)&g_kernel_rsc.sch_pend_list; + linker_list_transaction_specific(pLinker, pToList, _schedule_priority_node_order_compare_condition); EXIT_CRITICAL_SECTION(); } -/** - * @brief kernel thread use percent value take. - * - * @return The value of thread use percent. - */ -u32_t kernel_thread_use_percent_take(os_id_t id) +static struct schedule_task *_schedule_nextTaskGet(void) { -#if defined KTRACE - thread_context_t *pCurThread = (thread_context_t *)kernel_member_unified_id_toContainerAddress(id); - - pCurThread->schedule.analyze.percent = - (pCurThread->schedule.analyze.active_ms * 1000u) / (_kernel_pendsv_time_get() - pCurThread->schedule.analyze.cycle_ms); - pCurThread->schedule.analyze.active_ms = 0u; - pCurThread->schedule.analyze.cycle_ms = _kernel_pendsv_time_get(); - - return pCurThread->schedule.analyze.percent; -#else - return 0u; -#endif + return (struct schedule_task *)g_kernel_rsc.sch_pend_list.pHead; } -/** - * @brief kernel schedule in PendSV interrupt content. - * - * @param ppCurThreadPsp The current thread psp address. - * @param ppNextThreadPSP The next thread psp address. - */ -void kernel_scheduler_inPendSV_c(u32_t **ppCurPsp, u32_t **ppNextPSP) +static void _schedule_time_analyze(struct schedule_task *pFrom, struct schedule_task *pTo, u32_t ms) { - _kernel_pendsv_time_update(); - - if (_kernel_thread_exit_schedule()) { - timer_schedule(); - } - _kernel_thread_entry_schedule(); - - os_id_t next = _kernel_thread_nextIdGet(); - - *ppCurPsp = (u32_t *)_kernel_thread_PSP_Get(g_kernel_resource.current); - *ppNextPSP = (u32_t *)_kernel_thread_PSP_Get(next); - - _kernel_schedule_run_time_analyze(g_kernel_resource.current, next); - g_kernel_resource.current = next; + pFrom->exec.analyze.last_run_ms = ms - pFrom->exec.analyze.last_active_ms; + pFrom->exec.analyze.total_run_ms += pFrom->exec.analyze.last_run_ms; + pTo->exec.analyze.last_active_ms = ms; } -/** - * @brief Get the kernel member list according to the member id and list id. - * - * @param member_id The kernel member unique id. - * @param list_id The list unique id. - * - * @return The pointer of the list pointer. - */ -list_t *kernel_member_list_get(u8_t member_id, u8_t list_id) +static void _schedule_exit(u32_t ms) { - if (member_id >= KERNEL_MEMBER_NUMBER) { - return NULL; - } - - if (list_id >= KERNEL_MEMBER_LIST_NUMBER) { - return NULL; - } + b_t need = false; + struct schedule_task *pCurTask = NULL; + list_iterator_t it = ITERATION_NULL; + list_t *pList = (list_t *)&g_kernel_rsc.sch_exit_list; - if (!(g_kernel_resource.member.pSetting[member_id].list & SET_BIT(list_id))) { - return NULL; - } + list_iterator_init(&it, pList); + while (list_iterator_next_condition(&it, (void *)&pCurTask)) { + struct call_exit *pExit = &pCurTask->exec.exit; - return (list_t *)&g_kernel_resource.member.pListContainer[list_id]; -} + if (pExit->timeout_ms) { + timeout_set(&pCurTask->expire, pExit->timeout_ms, false); + if (pExit->timeout_ms != OS_TIME_FOREVER_VAL) { + need = true; + } + } + if (pExit->pToList) { + _schedule_transfer_toTargetList((linker_t *)&pCurTask->linker, (list_t *)pExit->pToList); + } else { + thread_context_t *pDelThread = (thread_context_t *)CONTAINEROF(pCurTask, thread_context_t, task); + + _schedule_transfer_toNullList((linker_t *)&pCurTask->linker); + os_memset((char_t *)pDelThread->pStackAddr, STACT_UNUSED_DATA, pDelThread->stackSize); + os_memset((char_t *)pDelThread, 0x0u, sizeof(thread_context_t)); + } -/** - * @brief Get the kernel member address according to the unified id. - * - * @param unified_id The kernel member unified id. - * - * @return The pointer of the memeber address. - */ -u8_t *kernel_member_unified_id_toContainerAddress(u32_t unified_id) -{ - if (unified_id >= KERNEL_MEMBER_MAP_NUMBER) { - return NULL; + os_memset(pExit, 0x0, sizeof(struct call_exit)); + pCurTask->exec.entry.result = PC_EOR; } - return (u8_t *)(&g_kernel_resource.member.pMemoryContainer[unified_id]); + if (need) { + timer_schedule(); + } } -/** - * @brief Get the kernel unified id according to the member address. - * - * @param container_address The kernel member address. - * - * @return The value of the kernel unified id. - */ -u32_t kernel_member_containerAddress_toUnifiedid(u32_t container_address) +static void _schedule_entry(u32_t ms) { - u32_t start = (u32_t)(u8_t *)&g_kernel_resource.member.pMemoryContainer[0]; + struct schedule_task *pCurTask = NULL; + list_iterator_t it = ITERATION_NULL; + list_t *pList = (list_t *)&g_kernel_rsc.sch_entry_list; - if (container_address < start) { - return OS_INVALID_ID_VAL; - } + list_iterator_init(&it, pList); + while (list_iterator_next_condition(&it, (void *)&pCurTask)) { + struct call_entry *pEntry = &pCurTask->exec.entry; + if (pEntry->fun) { + pEntry->fun(pCurTask); + pEntry->fun = NULL; + } + pCurTask->pPendCtx = NULL; + pCurTask->exec.analyze.last_pend_ms = ms; - if (container_address >= (start + KERNEL_MEMBER_MAP_NUMBER)) { - return OS_INVALID_ID_VAL; + _schedule_transfer_toPendList((linker_t *)&pCurTask->linker); } - - return (u32_t)(container_address - start); } -/** - * @brief Get the kernel member start unified id according to the member id. - * - * @param member_id The kernel member id. - * - * @return The value of the kernel member unified id. - */ -u32_t kernel_member_id_toUnifiedIdStart(u8_t member_id) +i32p_t schedule_exit_trigger(struct schedule_task *pTask, void *pHoldCtx, void *pHoldData, list_t *pToList, u32_t timeout_ms, + b_t immediately) { - if (member_id >= KERNEL_MEMBER_NUMBER) { - return OS_INVALID_ID_VAL; - } + pTask->pPendCtx = pHoldCtx; + pTask->pPendData = pHoldData; - if (!member_id) { - return 0u; + if (immediately) { + timeout_set(&pTask->expire, timeout_ms, true); + _schedule_transfer_toTargetList((linker_t *)&pTask->linker, pToList); + } else { + pTask->exec.exit.pToList = pToList; + pTask->exec.exit.timeout_ms = timeout_ms; + _schedule_transfer_toExitList((linker_t *)&pTask->linker); } - - return (u32_t)(g_kernel_resource.member.pSetting[member_id - 1].mem); + return kernel_thread_schedule_request(); } -/** - * @brief Get the kernel member start address according to the member unique id. - * - * @param member_id The kernel member id. - * - * @return The value of the kernel member address range. - */ -u8_t *kernel_member_id_toContainerStartAddress(u32_t member_id) +i32p_t schedule_entry_trigger(struct schedule_task *pTask, pTask_callbackFunc_t callback, u32_t result) { - if (member_id >= KERNEL_MEMBER_NUMBER) { - return NULL; - } - - return (u8_t *)kernel_member_unified_id_toContainerAddress(kernel_member_id_toUnifiedIdStart(member_id)); + pTask->exec.entry.result = result; + pTask->exec.entry.fun = callback; + _schedule_transfer_toEntryList((linker_t *)&pTask->linker); + return kernel_thread_schedule_request(); } -/** - * @brief Get the kernel member ending address according to the member unique id. - * - * @param member_id The kernel member id. - * - * @return The value of the kernel member ending address. - */ -u8_t *kernel_member_id_toContainerEndAddress(u32_t member_id) +void schedule_callback_fromTimeOut(void *pNode) { - if (member_id >= KERNEL_MEMBER_NUMBER) { - return NULL; - } - - return (u8_t *)kernel_member_unified_id_toContainerAddress(_kernel_member_id_toUnifiedIdEnd(member_id)); + struct schedule_task *pCurTask = (struct schedule_task *)CONTAINEROF(pNode, struct schedule_task, expire); + schedule_entry_trigger(pCurTask, NULL, PC_OS_WAIT_TIMEOUT); } -/** - * @brief Get the kernel member number according to the member id and unified id. - * - * @param member_id The kernel member id. - * @param unified_id The kernel member unified id. - * - * @return The value of the kernel member number. - */ -u32_t kernel_member_id_unifiedConvert(u8_t member_id, u32_t unified_id) +b_t schedule_hasTwoPendingItem(void) { - if (member_id >= KERNEL_MEMBER_NUMBER) { - return 0u; + if (!g_kernel_rsc.sch_pend_list.pHead) { + return false; } - u32_t diff = kernel_member_id_toUnifiedIdStart(member_id); - if (unified_id >= diff) { - diff = unified_id - diff; - } else { - diff = 0u; + if (!g_kernel_rsc.sch_pend_list.pHead->pNext) { + return true; } - return (u32_t)(diff / _kernel_member_id_toUnifiedIdRange(member_id)); + return false; } -/** - * @brief Check if the kernel member unique id if is's invalid. - * - * @param member_id The kernel member unique id. - * @param unified_id The kernel member unified id. - * - * @return The value of true is invalid, otherwise is valid. - */ -b_t kernel_member_unified_id_isInvalid(u32_t member_id, u32_t unified_id) +void schedule_setPend(struct schedule_task *pTask) { - if (member_id >= KERNEL_MEMBER_NUMBER) { - return TRUE; - } - - if (unified_id == OS_INVALID_ID_VAL) { - return TRUE; - } - - if (unified_id < kernel_member_id_toUnifiedIdStart(member_id)) { - return TRUE; - } + ENTER_CRITICAL_SECTION(); - if (unified_id >= _kernel_member_id_toUnifiedIdEnd(member_id)) { - return TRUE; - } + _schedule_transfer_toPendList((linker_t *)&pTask->linker); - return FALSE; + EXIT_CRITICAL_SECTION(); } -/** - * @brief Check if the thread unique id if is's invalid. - * - * @param id The provided unique id. - * - * @return The value of true is invalid, otherwise is valid. - */ -b_t kernel_os_id_is_invalid(struct os_id id) +list_t *schedule_waitList(void) { - if (id.val == OS_INVALID_ID_VAL) { - return TRUE; - } - - if (id.val == KERNEL_MEMBER_MAP_NUMBER) { - return TRUE; - } - - return FALSE; + return (list_t *)&g_kernel_rsc.sch_wait_list; } -/** - * @brief Convert the thread id into timer id. - * - * @param unified_id The thread unified id. - * - * @return The value of timer unified id. - */ -u32_t kernel_member_unified_id_threadToTimer(u32_t unified_id) +b_t _schedule_can_preempt(struct schedule_task *pCurrent) { - u32_t uid = OS_INVALID_ID_VAL; + struct schedule_task *pTmpTask = NULL; + list_iterator_t it = ITERATION_NULL; + list_t *pList = (list_t *)&g_kernel_rsc.sch_pend_list; - if (kernel_member_unified_id_isInvalid(KERNEL_MEMBER_THREAD, unified_id)) { - return OS_INVALID_ID_VAL; - } + list_iterator_init(&it, pList); + while (list_iterator_next_condition(&it, (void *)&pTmpTask)) { + if (pTmpTask->prior >= 0) { + break; + } - uid = (unified_id - kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_THREAD)) / sizeof(thread_context_t); + if (pTmpTask == pCurrent) { + return false; + } + } - return (u32_t)((uid * sizeof(timer_context_t)) + kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_TIMER_INTERNAL)); + return true; } /** - * @brief Convert the timer id into thread id. - * - * @param unified_id The timer unified id. + * @brief kernel schedule in PendSV interrupt content. * - * @return The value of thread unified id. + * @param ppCurThreadPsp The current thread psp address. + * @param ppNextThreadPSP The next thread psp address. */ -u32_t kernel_member_unified_id_timerToThread(u32_t unified_id) +void kernel_scheduler_inPendSV_c(u32_t **ppCurPsp, u32_t **ppNextPSP) { - u32_t uid = OS_INVALID_ID_VAL; + u32_t ms = timer_total_system_ms_get(); - if (kernel_member_unified_id_isInvalid(KERNEL_MEMBER_TIMER_INTERNAL, unified_id)) { - return OS_INVALID_ID_VAL; - } + _schedule_exit(ms); + _schedule_entry(ms); - uid = (unified_id - kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_TIMER_INTERNAL)) / sizeof(timer_context_t); - return (u32_t)((uid * sizeof(thread_context_t)) + kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_THREAD)); -} - -/** - * @brief Get the kernel member id. - * - * @param unified_id The provided unified id. - * - * @return The value of the kernel member id. - */ -u8_t kernel_member_unified_id_toId(u32_t unified_id) -{ - u8_t member_id = KERNEL_MEMBER_THREAD; + struct schedule_task *pCurrent = g_kernel_rsc.pTask; + struct schedule_task *pNext = _schedule_nextTaskGet(); - while ((member_id < KERNEL_MEMBER_NUMBER) && - ((unified_id < kernel_member_id_toUnifiedIdStart(member_id)) || (unified_id >= _kernel_member_id_toUnifiedIdEnd(member_id)))) { + if (_schedule_can_preempt(pCurrent)) { + *ppCurPsp = (u32_t *)&pCurrent->psp; + *ppNextPSP = (u32_t *)&pNext->psp; - member_id++; + _schedule_time_analyze(pCurrent, pNext, ms); + g_kernel_rsc.pTask = pNext; + g_kernel_rsc.pendsv_ms = ms; + } else { + *ppCurPsp = (u32_t *)&pCurrent->psp; + *ppNextPSP = (u32_t *)&pCurrent->psp; } - - return (u8_t)member_id; } /** - * @brief Initialize a thread stack frame. - * - * @param pEntryFunction The entry function pointer. - * @param pAddress The stack address. - * @param size The stack size. - * - * @return The PSP stack address. + * @brief To set a PendSV. */ -u32_t kernel_stack_frame_init(void (*pEntryFunction)(void), u32_t *pAddress, u32_t size) +static void _kernel_setPendSV(void) { - return (u32_t)port_stack_frame_init(pEntryFunction, pAddress, size); + port_setPendSV(); } /** - * @brief Get the kernel thread pending list head. + * @brief Check if kernel is in privilege mode. * - * @return The pending list head. + * @return The true indicates the kernel is in privilege mode. */ -list_t *kernel_list_pendingHeadGet(void) +static b_t _kernel_isInPrivilegeMode(void) { - return (list_t *)(&g_kernel_resource.list); + return port_isInInterruptContent(); } /** - * @brief Get the current running thread id. + * @brief It's sub-routine running at privilege mode. * - * @return The id of current running thread. - */ -os_id_t kernel_thread_runIdGet(void) -{ - return (os_id_t)g_kernel_resource.current; -} - -/** - * @brief Get the current running thread context. + * @param pArgs The function argument packages. * - * @return The context pointer of current running thread. + * @return The result of privilege routine. */ -thread_context_t *kernel_thread_runContextGet(void) +static i32p_t _kernel_start_privilege_routine(arguments_t *pArgs) { - return (void *)kernel_member_unified_id_toContainerAddress(kernel_thread_runIdGet()); -} + UNUSED_MSG(pArgs); -/** - * @brief Push one thread context into exit list. - * - * @param pCurHead The pointer of the thread linker head. - */ -static void kernel_thread_list_transfer_toExit(linker_head_t *pCurHead) -{ ENTER_CRITICAL_SECTION(); - list_t *pToExitList = (list_t *)kernel_member_list_get(KERNEL_MEMBER_THREAD, KERNEL_MEMBER_LIST_THREAD_EXIT); - linker_list_transaction_common(&pCurHead->linker, pToExitList, LIST_TAIL); + init_static_thread_list(); + port_interrupt_init(); + clock_time_init(timeout_handler); - EXIT_CRITICAL_SECTION(); -} + g_kernel_rsc.pTask = _schedule_nextTaskGet(); + g_kernel_rsc.run = true; -/** - * @brief Push one thread context into entry list. - * - * @param pCurHead The pointer of the thread linker head. - */ -void kernel_thread_list_transfer_toEntry(linker_head_t *pCurHead) -{ - ENTER_CRITICAL_SECTION(); + EXIT_CRITICAL_SECTION(); - list_t *pToEntryList = (list_t *)kernel_member_list_get(KERNEL_MEMBER_THREAD, KERNEL_MEMBER_LIST_THREAD_ENTRY); - linker_list_transaction_common(&pCurHead->linker, pToEntryList, LIST_TAIL); + port_run_theFirstThread(g_kernel_rsc.pTask->psp); - EXIT_CRITICAL_SECTION(); + // Unreachable. + return PC_EOR; } /** - * @brief Push one thread context into pending list. - * - * @param pCurHead The pointer of the thread linker head. + * @brief To check if the kernel message arrived. */ -void kernel_thread_list_transfer_toPend(linker_head_t *pCurHead) +static i32p_t _kernel_message_arrived(void) { - ENTER_CRITICAL_SECTION(); - - list_t *pToPendList = (list_t *)kernel_list_pendingHeadGet(); - linker_list_transaction_specific(&pCurHead->linker, pToPendList, _kernel_thread_node_Order_compare_condition); - - EXIT_CRITICAL_SECTION(); + return kthread_message_arrived(); } /** - * @brief The thread is trying to exit into suspend. + * @brief Initialize a thread stack frame. * - * @param The thread pointer. - * @param hold The member unique id hold on the thread. - * @param pToList The blocking list. - * @param timeout If the thread has sleep time setting. + * @param pEntryFunction The entry function pointer. + * @param pAddress The stack address. + * @param size The stack size. * - * @return The result of exit operation. + * @return The PSP stack address. */ -i32p_t kernel_thread_exit_trigger(thread_context_t *pCurThread, void* pHoldCtx, list_t *pToList, u32_t timeout_us) +u32_t kernel_stack_frame_init(void (*pEntryFunction)(void), u32_t *pAddress, u32_t size) { - pCurThread->schedule.pPendCtx = pHoldCtx; - pCurThread->schedule.exit.pToList = pToList; - pCurThread->schedule.exit.timeout_us = timeout_us; - kernel_thread_list_transfer_toExit((linker_head_t *)&pCurThread->head); - return kernel_thread_schedule_request(); + return (u32_t)port_stack_frame_init(pEntryFunction, pAddress, size); } /** - * @brief Try to trigger one thread active. - * - * @param The thread pointer. - * @param result The thread entry result. - * @param pCallback The timeout callback function pointer. + * @brief Get the current running thread context. * - * @return The result of entry operation. + * @return The context pointer of current running thread. */ -i32p_t kernel_thread_entry_trigger(thread_context_t *pCurThread, u32_t result, void (*pCallback)(os_id_t)) +thread_context_t *kernel_thread_runContextGet(void) { - pCurThread->schedule.entry.result = result; - pCurThread->schedule.entry.pEntryCallFun = pCallback; - kernel_thread_list_transfer_toEntry((linker_head_t *)&pCurThread->head); - return kernel_thread_schedule_request(); + return (thread_context_t *)CONTAINEROF(g_kernel_rsc.pTask, thread_context_t, task); } /** @@ -792,8 +392,8 @@ i32p_t kernel_schedule_result_take(void) { thread_context_t *pCurThread = kernel_thread_runContextGet(); - i32p_t ret = (i32p_t)pCurThread->schedule.entry.result; - pCurThread->schedule.entry.result = _PCER; + i32p_t ret = (i32p_t)pCurThread->task.exec.entry.result; + pCurThread->task.exec.entry.result = PC_EOR; return ret; } @@ -816,7 +416,7 @@ b_t kernel_isInThreadMode(void) i32p_t kernel_thread_schedule_request(void) { if (!_kernel_isInPrivilegeMode()) { - return _PCER; + return PC_EOR; } _kernel_setPendSV(); @@ -853,7 +453,7 @@ void kernel_schedule_thread(void) void kernel_idle_thread(void) { while (1) { - /* TODO: Power Management */ + kthread_message_idle_loop_fn(); } } @@ -890,7 +490,7 @@ void kernel_privilege_call_inSVC_c(u32_t *svc_args) i32p_t kernel_privilege_invoke(const void *pCallFun, arguments_t *pArgs) { if (!pCallFun) { - return _PCER; + return PC_EOR; } if (!_kernel_isInPrivilegeMode()) { @@ -912,7 +512,7 @@ i32p_t kernel_privilege_invoke(const void *pCallFun, arguments_t *pArgs) */ b_t _impl_kernel_rtos_isRun(void) { - return (b_t)((g_kernel_resource.run) ? (TRUE) : (FALSE)); + return (b_t)((g_kernel_rsc.run) ? (true) : (false)); } /** @@ -926,7 +526,3 @@ i32p_t _impl_kernel_at_rtos_run(void) return kernel_privilege_invoke((const void *)_kernel_start_privilege_routine, NULL); } - -#ifdef __cplusplus -} -#endif diff --git a/kernel/kthread.c b/kernel/kthread.c index d7ca528..bc0a526 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -4,27 +4,32 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "at_rtos.h" #include "ktype.h" #include "kernel.h" #include "timer.h" -#include "postcode.h" +#include "init.h" -#ifdef __cplusplus -extern "C" { -#endif +INIT_OS_THREAD_RUNTIME_NUM_DEFINE(THREAD_RUNTIME_NUMBER_SUPPORTED); +INIT_OS_TIMER_RUNTIME_NUM_DEFINE(TIMER_RUNTIME_NUMBER_SUPPORTED); +INIT_OS_SEM_RUNTIME_NUM_DEFINE(SEMAPHORE_RUNTIME_NUMBER_SUPPORTED); +INIT_OS_MUTEX_RUNTIME_NUM_DEFINE(MUTEX_RUNTIME_NUMBER_SUPPORTED); +INIT_OS_EVT_RUNTIME_NUM_DEFINE(EVENT_RUNTIME_NUMBER_SUPPORTED); +INIT_OS_MSGQ_RUNTIME_NUM_DEFINE(QUEUE_RUNTIME_NUMBER_SUPPORTED); +INIT_OS_POOL_RUNTIME_NUM_DEFINE(POOL_RUNTIME_NUMBER_SUPPORTED); +INIT_OS_PUBLISH_RUNTIME_NUM_DEFINE(PUBLISH_RUNTIME_NUMBER_SUPPORTED); +INIT_OS_SUBSCRIBE_RUNTIME_NUM_DEFINE(SUBSCRIBE_RUNTIME_NUMBER_SUPPORTED); -#define _PCER PC_IER(PC_OS_CMPT_KERNEL_2) +INIT_OS_THREAD_DEFINE(kernel_th, OS_PRIORITY_KERNEL_SCHEDULE_LEVEL, KERNEL_SCHEDULE_THREAD_STACK_SIZE, kernel_schedule_thread); +INIT_OS_THREAD_DEFINE(idle_th, OS_PRIORITY_KERNEL_IDLE_LEVEL, KERNEL_IDLE_THREAD_STACK_SIZE, kernel_idle_thread); +INIT_OS_SEMAPHORE_DEFINE(kernel_sem, 0u, OS_SEM_BINARY); -/* Local defined the kernel thread stack */ -static u32_t _kernel_schedule[((u32_t)(KERNEL_SCHEDULE_THREAD_STACK_SIZE) / sizeof(u32_t))] = {0u}; -static u32_t _kernel_idle[((u32_t)(KERNEL_IDLE_THREAD_STACK_SIZE) / sizeof(u32_t))] = {0u}; +static pThread_entryFunc_t g_idle_thread_user_entry_fn = NULL; /** * Global At_RTOS application interface init. */ -#if (OS_INTERFACE_EXTERN_USE_ENABLE) +#if (OS_API_ENABLE) const at_rtos_api_t os = { .thread_init = os_thread_init, .thread_sleep = os_thread_sleep, @@ -32,6 +37,7 @@ const at_rtos_api_t os = { .thread_suspend = os_thread_suspend, .thread_yield = os_thread_yield, .thread_delete = os_thread_delete, + .thread_idle_fn_register = os_thread_idle_callback_register, .timer_init = os_timer_init, .timer_automatic = os_timer_automatic, @@ -71,57 +77,22 @@ const at_rtos_api_t os = { .id_isInvalid = os_id_is_invalid, .schedule_run = os_kernel_run, .schedule_is_running = os_kernel_is_running, - .id_current_thread = os_id_current_thread, + .current_thread = os_current_thread_probe, - .trace_firmware = os_trace_firmware_print, - .trace_postcode = os_trace_postcode_print, - .trace_kernel = os_trace_kernel_print, + .trace_versison = os_trace_firmware_version, + .trace_postcode_fn_register = os_trace_postcode_callback_register, + .trace_postcode = os_trace_failed_postcode, + .trace_thread = os_trace_foreach_thread, + .trace_time = os_trace_analyze, }; #endif -/** - * Data structure for location kernel thread. - */ -typedef struct { - /* kernel schedule thread id */ - os_thread_id_t schedule_id; - - /* kernel idle thread id */ - os_thread_id_t idle_id; - - /* kernel schedule semaphore id */ - os_sem_id_t sem_id; -} _kthread_resource_t; - -/** - * Local timer resource - */ -static _kthread_resource_t g_kernel_thread_resource = { - .schedule_id = - { - .pName = "kernel", - .val = 0u, - .number = KERNEL_SCHEDULE_THREAD_INSTANCE, - }, - .idle_id = - { - .pName = "idle", - .val = sizeof(thread_context_t), - .number = KERNEL_IDLE_THREAD_INSTANCE, - }, - .sem_id = - { - .pName = "kernel", - .number = KERNEL_SCHEDULE_SEMAPHORE_INSTANCE, - }, -}; - /** * @brief To issue a kernel message notification. */ void kthread_message_notification(void) { - PC_IF(os_sem_give(g_kernel_thread_resource.sem_id), PC_PASS) + PC_IF(os_sem_give(kernel_sem), PC_PASS) { /* TODO */ } @@ -132,90 +103,23 @@ void kthread_message_notification(void) */ i32p_t kthread_message_arrived(void) { - return os_sem_take(g_kernel_thread_resource.sem_id, OS_TIME_FOREVER_VAL); + return os_sem_take(kernel_sem, OS_TIME_FOREVER_VAL); } -static void _thread_callback_fromTimeOut(void *pLinker) +/** + * @brief User register idle function callback. + */ +void kthread_message_idle_loop_fn(void) { - thread_context_t* pCurThread = (thread_context_t*)CONTAINEROF(pLinker, thread_context_t, expire); - kernel_thread_entry_trigger(pCurThread, PC_OS_WAIT_TIMEOUT, NULL); + if (g_idle_thread_user_entry_fn) { + g_idle_thread_user_entry_fn(); + } } /** - * @brief The AtOS kernel internal use thread and semaphore init. + * @brief Register idle thread user fucntion. */ -void kthread_init(void) +void _impl_kthread_idle_user_callback_register(const pThread_entryFunc_t fn) { - ENTER_CRITICAL_SECTION(); - - thread_context_t kernel_thread[KERNEL_APPLICATION_THREAD_INSTANCE] = { - [KERNEL_SCHEDULE_THREAD_INSTANCE] = - { - .head = - { - .id = g_kernel_thread_resource.schedule_id.val, - .pName = g_kernel_thread_resource.schedule_id.pName, - .linker = LINKER_NULL, - }, - .priority = - { - .level = OS_PRIOTITY_HIGHEST_LEVEL, - }, - .pEntryFunc = kernel_schedule_thread, - .pStackAddr = (u32_t *)&_kernel_schedule[0], - .stackSize = KERNEL_SCHEDULE_THREAD_STACK_SIZE, - .psp = (u32_t)kernel_stack_frame_init(kernel_schedule_thread, (u32_t *)&_kernel_schedule[0], - KERNEL_SCHEDULE_THREAD_STACK_SIZE), - }, - - [KERNEL_IDLE_THREAD_INSTANCE] = - { - .head = - { - .id = g_kernel_thread_resource.idle_id.val, - .pName = g_kernel_thread_resource.idle_id.pName, - .linker = LINKER_NULL, - }, - .priority = - { - .level = OS_PRIOTITY_LOWEST_LEVEL, - }, - .pEntryFunc = kernel_idle_thread, - .pStackAddr = (u32_t *)&_kernel_idle[0], - .stackSize = KERNEL_IDLE_THREAD_STACK_SIZE, - .psp = (u32_t)kernel_stack_frame_init(kernel_idle_thread, (u32_t *)&_kernel_idle[0], KERNEL_IDLE_THREAD_STACK_SIZE), - }, - }; - - thread_context_t *pCurThread = (thread_context_t *)kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_THREAD); - os_memcpy((u8_t *)pCurThread, (u8_t *)kernel_thread, (sizeof(thread_context_t) * KERNEL_APPLICATION_THREAD_INSTANCE)); - - pCurThread = (thread_context_t *)kernel_member_unified_id_toContainerAddress(kernel_thread[KERNEL_SCHEDULE_THREAD_INSTANCE].head.id); - timeout_init(&pCurThread->expire, _thread_callback_fromTimeOut); - kernel_thread_list_transfer_toPend((linker_head_t *)&pCurThread->head); - - pCurThread = (thread_context_t *)kernel_member_unified_id_toContainerAddress(kernel_thread[KERNEL_IDLE_THREAD_INSTANCE].head.id); - timeout_init(&pCurThread->expire, _thread_callback_fromTimeOut); - kernel_thread_list_transfer_toPend((linker_head_t *)&pCurThread->head); - - semaphore_context_t kernel_semaphore[KERNEL_APPLICATION_SEMAPHORE_INSTANCE] = { - [KERNEL_SCHEDULE_SEMAPHORE_INSTANCE] = - { - .head = - { - .pName = g_kernel_thread_resource.schedule_id.pName, - .cs = CS_INITED, - }, - .remains = 0u, - .limits = OS_SEM_BINARY, - }, - }; - - semaphore_context_t *pCurSemaphore = (semaphore_context_t *)kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_SEMAPHORE); - g_kernel_thread_resource.sem_id.val = kernel_member_containerAddress_toUnifiedid((u32_t)pCurSemaphore); - os_memcpy((u8_t *)pCurSemaphore, (u8_t *)kernel_semaphore, (sizeof(semaphore_context_t) * KERNEL_APPLICATION_SEMAPHORE_INSTANCE)); -} - -#ifdef __cplusplus + g_idle_thread_user_entry_fn = fn; } -#endif diff --git a/kernel/linker.c b/kernel/linker.c index 0a8cc90..e6dfb6d 100644 --- a/kernel/linker.c +++ b/kernel/linker.c @@ -4,13 +4,8 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "linker.h" -#ifdef __cplusplus -extern "C" { -#endif - /** * @brief Copy the character from src to dst. * @@ -91,22 +86,22 @@ u32_t os_strlen(const uchar_t *str) b_t list_node_isExisted(list_t *pList, list_node_t *pNode) { if (!pList) { - return FALSE; + return false; } if (!pNode) { - return FALSE; + return false; } list_node_t *pCurNode_temp = pList->pHead; while (pCurNode_temp) { if (pCurNode_temp == pNode) { - return TRUE; + return true; } pCurNode_temp = pCurNode_temp->pNext; } - return FALSE; + return false; } /** @@ -134,6 +129,22 @@ u32_t list_size(list_t *pList) return size; } +/** + * @brief Get the list head. + * + * @param pList The pointer of the list. + * + * @return Value The pointer of the list head + */ +void *list_head(list_t *pList) +{ + if (!pList) { + return NULL; + } + + return (void *)(pList->pHead); +} + /** * @brief To delete a node form the provided list. * @@ -148,11 +159,11 @@ u32_t list_size(list_t *pList) b_t list_node_delete(list_t *pList, list_node_t *pTargetNode) { if (!pList) { - return FALSE; + return false; } if (!pTargetNode) { - return FALSE; + return false; } list_node_t *pCurNode_tmp = pList->pHead; @@ -164,7 +175,7 @@ b_t list_node_delete(list_t *pList, list_node_t *pTargetNode) if (!pCurNode_tmp) { /* The target pNode is not in the list */ - return FALSE; + return false; } if (!pPrevNode_tmp) { @@ -174,7 +185,7 @@ b_t list_node_delete(list_t *pList, list_node_t *pTargetNode) } pCurNode_tmp->pNext = NULL; - return TRUE; + return true; } /** @@ -191,15 +202,15 @@ b_t list_node_delete(list_t *pList, list_node_t *pTargetNode) b_t list_node_insertBefore(list_t *pList, list_node_t *pBefore, list_node_t *pTargetNode) { if (!pList) { - return FALSE; + return false; } if (!pBefore) { - return FALSE; + return false; } if (!pTargetNode) { - return FALSE; + return false; } list_node_t *pCurNode_tmp = pList->pHead; @@ -211,7 +222,7 @@ b_t list_node_insertBefore(list_t *pList, list_node_t *pBefore, list_node_t *pTa if (!pCurNode_tmp) { /* The pBefore is not in the list */ - return FALSE; + return false; } if (!pPrevNode_tmp) { @@ -221,7 +232,7 @@ b_t list_node_insertBefore(list_t *pList, list_node_t *pBefore, list_node_t *pTa } pTargetNode->pNext = pBefore; - return TRUE; + return true; } /** @@ -238,15 +249,15 @@ b_t list_node_insertBefore(list_t *pList, list_node_t *pBefore, list_node_t *pTa b_t list_node_push(list_t *pList, list_node_t *pInNode, list_direction_t direction) { if (!pList) { - return FALSE; + return false; } if (!pInNode) { - return FALSE; + return false; } if ((direction != LIST_HEAD) && (direction != LIST_TAIL)) { - return FALSE; + return false; } if (direction == LIST_TAIL) { @@ -265,7 +276,7 @@ b_t list_node_push(list_t *pList, list_node_t *pInNode, list_direction_t directi pList->pHead = pInNode; } - return TRUE; + return true; } /** @@ -336,22 +347,22 @@ list_node_t *list_node_pop(list_t *pList, list_direction_t direction) b_t list_iterator_init(list_iterator_t *pIterator, list_t *pList) { if (!pIterator) { - return FALSE; + return false; } if (!pList) { - return FALSE; + return false; } os_memset((char_t *)pIterator, 0x0u, sizeof(list_iterator_t)); if (!pList->pHead) { - return FALSE; + return false; } pIterator->pCurNode = pList->pHead; pIterator->pList = pList; - return TRUE; + return true; } /** @@ -391,7 +402,7 @@ b_t list_iterator_next_condition(list_iterator_t *pIterator, list_node_t **ppOut { *ppOutNode = list_iterator_next(pIterator); - return (b_t)((*ppOutNode) ? TRUE : FALSE); + return (b_t)((*ppOutNode) ? true : false); } /** @@ -466,7 +477,3 @@ void linker_list_transaction_specific(linker_t *pLinker, list_t *pToList, pLinke list_node_push(pToList, &pLinker->node, LIST_TAIL); } } - -#ifdef __cplusplus -} -#endif diff --git a/kernel/mutex.c b/kernel/mutex.c index 5ac587b..31ae711 100644 --- a/kernel/mutex.c +++ b/kernel/mutex.c @@ -4,33 +4,17 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "kernel.h" #include "timer.h" #include "postcode.h" #include "trace.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "init.h" /** * Local unique postcode. */ #define PC_EOR PC_IER(PC_OS_CMPT_MUTEX_5) -/** - * @brief Get the mutex context based on provided unique id. - * - * @param id The mutex unique id. - * - * @return The pointer of the current unique id timer context. - */ -static mutex_context_t *_mutex_context_get(os_id_t id) -{ - return (mutex_context_t *)(kernel_member_unified_id_toContainerAddress(id)); -} - /** * @brief Check if the mutex unique id if is's invalid. * @@ -38,9 +22,13 @@ static mutex_context_t *_mutex_context_get(os_id_t id) * * @return The true is invalid, otherwise is valid. */ -static b_t _mutex_id_isInvalid(u32_t id) +static b_t _mutex_context_isInvalid(mutex_context_t *pCurMutex) { - return kernel_member_unified_id_isInvalid(KERNEL_MEMBER_MUTEX, id); + u32_t start, end; + INIT_SECTION_FIRST(INIT_SECTION_OS_MUTEX_LIST, start); + INIT_SECTION_LAST(INIT_SECTION_OS_MUTEX_LIST, end); + + return ((u32_t)pCurMutex < start || (u32_t)pCurMutex >= end) ? true : false; } /** @@ -50,40 +38,9 @@ static b_t _mutex_id_isInvalid(u32_t id) * * @return The true is initialized, otherwise is uninitialized. */ -static b_t _mutex_id_isInit(i32_t id) -{ - mutex_context_t *pCurMutex = _mutex_context_get(id); - - return ((pCurMutex) ? (((pCurMutex->head.cs) ? (TRUE) : (FALSE))) : FALSE); -} - -/** - * @brief Get the mutex blocking thread list head address. - * - * @return The blocking thread list head address. - */ -static list_t *_mutex_list_blockingHeadGet(os_id_t id) -{ - mutex_context_t *pCurMutex = _mutex_context_get(id); - - return (list_t *)((pCurMutex) ? (&pCurMutex->blockingThreadHead) : (NULL)); -} - -/** - * @brief Pick up a highest priority thread from the mutex blocking list. - * - * @param id The mutex context id - * - * @return The highest thread head. - */ -static linker_head_t *_mutex_linker_head_fromBlocking(os_id_t id) +static b_t _mutex_context_isInit(mutex_context_t *pCurMutex) { - ENTER_CRITICAL_SECTION(); - - list_t *pListBlocking = (list_t *)_mutex_list_blockingHeadGet(id); - - EXIT_CRITICAL_SECTION(); - return (linker_head_t *)(pListBlocking->pHead); + return ((pCurMutex) ? (((pCurMutex->head.cs) ? (true) : (false))) : false); } /** @@ -98,18 +55,14 @@ static u32_t _mutex_init_privilege_routine(arguments_t *pArgs) ENTER_CRITICAL_SECTION(); const char_t *pName = (const char_t *)(pArgs[0].pch_val); - u32_t endAddr = 0u; - mutex_context_t *pCurMutex = NULL; - - pCurMutex = (mutex_context_t *)kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_MUTEX); - endAddr = (u32_t)kernel_member_id_toContainerEndAddress(KERNEL_MEMBER_MUTEX); - do { - os_id_t id = kernel_member_containerAddress_toUnifiedid((u32_t)pCurMutex); - if (_mutex_id_isInvalid(id)) { + + INIT_SECTION_FOREACH(INIT_SECTION_OS_MUTEX_LIST, mutex_context_t, pCurMutex) + { + if (_mutex_context_isInvalid(pCurMutex)) { break; } - if (_mutex_id_isInit(id)) { + if (_mutex_context_isInit(pCurMutex)) { continue; } @@ -118,15 +71,15 @@ static u32_t _mutex_init_privilege_routine(arguments_t *pArgs) pCurMutex->head.pName = pName; pCurMutex->locked = false; - pCurMutex->pHoldThread = NULL; - pCurMutex->originalPriority.level = OS_PRIOTITY_INVALID_LEVEL; + pCurMutex->pHoldTask = NULL; + pCurMutex->originalPriority = OS_PRIOTITY_INVALID_LEVEL; EXIT_CRITICAL_SECTION(); - return id; - } while ((u32_t)++pCurMutex < endAddr); + return (u32_t)pCurMutex; + } EXIT_CRITICAL_SECTION(); - return OS_INVALID_ID_VAL; + return 0u; } /** @@ -140,27 +93,25 @@ static i32p_t _mutex_lock_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; - mutex_context_t *pCurMutex = NULL; + mutex_context_t *pCurMutex = (mutex_context_t *)pArgs[0].u32_val; thread_context_t *pCurThread = NULL; i32p_t postcode = 0; - pCurMutex = _mutex_context_get(id); pCurThread = kernel_thread_runContextGet(); if (pCurMutex->locked == true) { - thread_context_t *pLockThread = pCurMutex->pHoldThread; - if (pCurThread->priority.level < pLockThread->priority.level) { - pLockThread->priority = pCurThread->priority; + struct schedule_task *pLockTask = pCurMutex->pHoldTask; + if (pCurThread->task.prior < pLockTask->prior) { + pLockTask->prior = pCurThread->task.prior; } - postcode = kernel_thread_exit_trigger(pCurThread, pCurMutex, _mutex_list_blockingHeadGet(id), 0u); + postcode = schedule_exit_trigger(&pCurThread->task, pCurMutex, NULL, &pCurMutex->q_list, 0u, true); EXIT_CRITICAL_SECTION(); return postcode; } /* Highest priority inheritance */ - pCurMutex->pHoldThread = pCurThread; - pCurMutex->originalPriority = pCurThread->priority; + pCurMutex->pHoldTask = &pCurThread->task; + pCurMutex->originalPriority = pCurThread->task.prior; pCurMutex->locked = true; EXIT_CRITICAL_SECTION(); @@ -178,48 +129,29 @@ static i32p_t _mutex_unlock_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; - mutex_context_t *pCurMutex = NULL; - thread_context_t *pMutexHighestBlockingThread = NULL; + mutex_context_t *pCurMutex = (mutex_context_t *)pArgs[0].u32_val; i32p_t postcode = 0; - pCurMutex = _mutex_context_get(id); - pMutexHighestBlockingThread = (thread_context_t *)_mutex_linker_head_fromBlocking(id); - thread_context_t *pLockThread = pCurMutex->pHoldThread; + struct schedule_task *pCurTask = (struct schedule_task *)list_head(&pCurMutex->q_list); + struct schedule_task *pLockTask = pCurMutex->pHoldTask; /* priority recovery */ - pLockThread->priority = pCurMutex->originalPriority; - if (!pMutexHighestBlockingThread) { + pLockTask->prior = pCurMutex->originalPriority; + if (!pCurTask) { // no blocking thread - pCurMutex->originalPriority.level = OS_PRIOTITY_INVALID_LEVEL; - pCurMutex->pHoldThread = NULL; + pCurMutex->originalPriority = OS_PRIOTITY_INVALID_LEVEL; + pCurMutex->pHoldTask = NULL; pCurMutex->locked = false; } else { /* The next thread take the ticket */ - pCurMutex->pHoldThread = pMutexHighestBlockingThread; - pCurMutex->originalPriority = pMutexHighestBlockingThread->priority; - postcode = kernel_thread_entry_trigger(pMutexHighestBlockingThread, 0, NULL); + pCurMutex->pHoldTask = pCurTask; + pCurMutex->originalPriority = pCurTask->prior; + postcode = schedule_entry_trigger(pCurTask, NULL, 0u); } EXIT_CRITICAL_SECTION(); return postcode; } -/** - * @brief Convert the internal os id to kernel member number. - * - * @param id The provided unique id. - * - * @return The value of member number. - */ -u32_t _impl_mutex_os_id_to_number(os_id_t id) -{ - if (_mutex_id_isInvalid(id)) { - return 0u; - } - - return (u32_t)((id - kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_MUTEX)) / sizeof(mutex_context_t)); -} - /** * @brief Initialize a new mutex. * @@ -227,7 +159,7 @@ u32_t _impl_mutex_os_id_to_number(os_id_t id) * * @return The mutex unique id. */ -os_id_t _impl_mutex_init(const char_t *pName) +u32_t _impl_mutex_init(const char_t *pName) { arguments_t arguments[] = { [0] = {.pch_val = (const char_t *)pName}, @@ -243,13 +175,14 @@ os_id_t _impl_mutex_init(const char_t *pName) * * @return The result of the operation. */ -i32p_t _impl_mutex_lock(os_id_t id) +i32p_t _impl_mutex_lock(u32_t ctx) { - if (_mutex_id_isInvalid(id)) { + mutex_context_t *pCtx = (mutex_context_t *)ctx; + if (_mutex_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_mutex_id_isInit(id)) { + if (!_mutex_context_isInit(pCtx)) { return PC_EOR; } @@ -258,7 +191,7 @@ i32p_t _impl_mutex_lock(os_id_t id) } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, }; return kernel_privilege_invoke((const void *)_mutex_lock_privilege_routine, arguments); @@ -271,23 +204,20 @@ i32p_t _impl_mutex_lock(os_id_t id) * * @return The result of the operation. */ -i32p_t _impl_mutex_unlock(os_id_t id) +i32p_t _impl_mutex_unlock(u32_t ctx) { - if (_mutex_id_isInvalid(id)) { + mutex_context_t *pCtx = (mutex_context_t *)ctx; + if (_mutex_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_mutex_id_isInit(id)) { + if (!_mutex_context_isInit(pCtx)) { return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, }; return kernel_privilege_invoke((const void *)_mutex_unlock_privilege_routine, arguments); } - -#ifdef __cplusplus -} -#endif diff --git a/kernel/pool.c b/kernel/pool.c index c61a02a..9e1840f 100644 --- a/kernel/pool.c +++ b/kernel/pool.c @@ -4,33 +4,17 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "kernel.h" #include "timer.h" #include "postcode.h" #include "trace.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "init.h" /** * Local unique postcode. */ #define PC_EOR PC_IER(PC_OS_CMPT_POOL_9) -/** - * @brief Get the pool context based on provided unique id. - * - * @param id The timer unique id. - * - * @return The pointer of the current unique id timer context. - */ -static pool_context_t *_pool_context_get(os_id_t id) -{ - return (pool_context_t *)(kernel_member_unified_id_toContainerAddress(id)); -} - /** * @brief Check if the pool unique id if is's invalid. * @@ -38,9 +22,13 @@ static pool_context_t *_pool_context_get(os_id_t id) * * @return The true is invalid, otherwise is valid. */ -static b_t _pool_id_isInvalid(u32_t id) +static b_t _pool_context_isInvalid(pool_context_t *pCurPool) { - return kernel_member_unified_id_isInvalid(KERNEL_MEMBER_POOL, id); + u32_t start, end; + INIT_SECTION_FIRST(INIT_SECTION_OS_POOL_LIST, start); + INIT_SECTION_LAST(INIT_SECTION_OS_POOL_LIST, end); + + return ((u32_t)pCurPool < start || (u32_t)pCurPool >= end) ? true : false; } /** @@ -50,25 +38,9 @@ static b_t _pool_id_isInvalid(u32_t id) * * @return The true is initialized, otherwise is uninitialized. */ -static b_t _pool_id_isInit(i32_t id) +static b_t _pool_context_isInit(pool_context_t *pCurPool) { - pool_context_t *pCurPool = _pool_context_get(id); - - return ((pCurPool) ? (((pCurPool->head.cs) ? (TRUE) : (FALSE))) : FALSE); -} - -/** - * @brief Pick up a highest priority thread that blocking by the pool pending list. - * - * @param The pool unique id. - * - * @return The highest blocking thread head. - */ -static list_t *_pool_list_blockingHeadGet(os_id_t id) -{ - pool_context_t *pCurPool = _pool_context_get(id); - - return (list_t *)((pCurPool) ? (&pCurPool->blockingThreadHead) : (NULL)); + return ((pCurPool) ? (((pCurPool->head.cs) ? (true) : (false))) : false); } /** @@ -86,13 +58,13 @@ static void *_mem_take(pool_context_t *pCurPool) u8_t i = 0u; do { - if (!(free & SET_BIT(i))) { + if (!(free & B(i))) { continue; } pMemTake = (void *)((u32_t)((i * pCurPool->elementLength) + (u32_t)pCurPool->pMemAddress)); os_memset((char_t *)pMemTake, 0x0u, pCurPool->elementLength); - pCurPool->elementFreeBits &= ~SET_BIT(i); + pCurPool->elementFreeBits &= ~B(i); break; } while (i++ < num); @@ -115,19 +87,19 @@ static bool _mem_release(pool_context_t *pCurPool, void *pUserMem) u8_t i = 0u; do { - if (free & SET_BIT(i)) { + if (free & B(i)) { continue; } pMemTake = (void *)((u32_t)((i * pCurPool->elementLength) + (u32_t)pCurPool->pMemAddress)); if (pMemTake == pUserMem) { os_memset((char_t *)pMemTake, 0x0u, pCurPool->elementLength); - pCurPool->elementFreeBits |= SET_BIT(i); + pCurPool->elementFreeBits |= B(i); break; } } while (i++ < num); - return (pMemTake == pUserMem) ? (TRUE) : (FALSE); + return (pMemTake == pUserMem) ? (true) : (false); } /** @@ -135,20 +107,20 @@ static bool _mem_release(pool_context_t *pCurPool, void *pUserMem) * * @param id The unique id of the entry thread. */ -static void _pool_schedule(os_id_t id) +static void _pool_schedule(void *pTask) { - thread_context_t *pEntryThread = (thread_context_t *)(kernel_member_unified_id_toContainerAddress(id)); - timeout_remove(&pEntryThread->expire, true); - pool_context_t *pCurPool = (pool_context_t*)pEntryThread->schedule.pPendCtx; - void **ppUserMemAddress = (void **)pEntryThread->schedule.pPendData; + struct schedule_task *pCurTask = (struct schedule_task *)pTask; + timeout_remove(&pCurTask->expire, true); + pool_context_t *pCurPool = (pool_context_t *)pCurTask->pPendCtx; + void **ppUserMemAddress = (void **)pCurTask->pPendData; if (!*ppUserMemAddress) { return; } *ppUserMemAddress = _mem_take(pCurPool); if (!*ppUserMemAddress) { - pEntryThread->schedule.entry.result = PC_EOR; + pCurTask->exec.entry.result = PC_EOR; } else { - pEntryThread->schedule.entry.result = 0; + pCurTask->exec.entry.result = 0; } } @@ -167,18 +139,14 @@ static u32_t _pool_init_privilege_routine(arguments_t *pArgs) u16_t elementLen = (u16_t)(pArgs[1].u16_val); u16_t elementNum = (u16_t)(pArgs[2].u16_val); const char_t *pName = (const char_t *)(pArgs[3].pch_val); - u32_t endAddr = 0u; - pool_context_t *pCurPool = NULL; - pCurPool = (pool_context_t *)kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_POOL); - endAddr = (u32_t)kernel_member_id_toContainerEndAddress(KERNEL_MEMBER_POOL); - do { - os_id_t id = kernel_member_containerAddress_toUnifiedid((u32_t)pCurPool); - if (_pool_id_isInvalid(id)) { + INIT_SECTION_FOREACH(INIT_SECTION_OS_POOL_LIST, pool_context_t, pCurPool) + { + if (_pool_context_isInvalid(pCurPool)) { break; } - if (_pool_id_isInit(id)) { + if (_pool_context_isInit(pCurPool)) { continue; } os_memset((char_t *)pCurPool, 0x0u, sizeof(pool_context_t)); @@ -188,14 +156,14 @@ static u32_t _pool_init_privilege_routine(arguments_t *pArgs) pCurPool->pMemAddress = pMemAddr; pCurPool->elementLength = elementLen; pCurPool->elementNumber = elementNum; - pCurPool->elementFreeBits = SET_BITS(0u, (elementNum - 1u)); + pCurPool->elementFreeBits = Bs(0u, (elementNum - 1u)); EXIT_CRITICAL_SECTION(); - return id; - } while ((u32_t)++pCurPool < endAddr); + return (u32_t)pCurPool; + }; EXIT_CRITICAL_SECTION(); - return OS_INVALID_ID_VAL; + return 0u; } /** @@ -209,17 +177,14 @@ static i32p_t _pool_take_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; + pool_context_t *pCurPool = (pool_context_t *)pArgs[0].u32_val; void **ppUserBuffer = (void **)pArgs[1].pv_val; u16_t bufferSize = (u16_t)pArgs[2].u16_val; u32_t timeout_ms = (u32_t)pArgs[3].u32_val; - pool_context_t *pCurPool = NULL; thread_context_t *pCurThread = NULL; i32p_t postcode = 0; - pCurPool = _pool_context_get(id); pCurThread = kernel_thread_runContextGet(); - if (bufferSize > pCurPool->elementLength) { EXIT_CRITICAL_SECTION(); return PC_EOR; @@ -230,8 +195,7 @@ static i32p_t _pool_take_privilege_routine(arguments_t *pArgs) EXIT_CRITICAL_SECTION(); return PC_EOR; } - pCurThread->schedule.pPendData = (void *)ppUserBuffer; - postcode = kernel_thread_exit_trigger(pCurThread, pCurPool, _pool_list_blockingHeadGet(id), timeout_ms); + postcode = schedule_exit_trigger(&pCurThread->task, pCurPool, ppUserBuffer, &pCurPool->q_list, timeout_ms, true); PC_IF(postcode, PC_PASS) { postcode = PC_OS_WAIT_UNAVAILABLE; @@ -259,15 +223,10 @@ static i32p_t _pool_release_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; + pool_context_t *pCurPool = (pool_context_t *)pArgs[0].u32_val; void **ppUserBuffer = (void **)pArgs[1].ptr_val; - pool_context_t *pCurPool = NULL; - thread_context_t *pCurThread = NULL; i32p_t postcode = 0; - pCurPool = _pool_context_get(id); - pCurThread = (thread_context_t *)kernel_thread_runContextGet(); - if (!_mem_release(pCurPool, *ppUserBuffer)) { EXIT_CRITICAL_SECTION(); return PC_EOR; @@ -276,32 +235,17 @@ static i32p_t _pool_release_privilege_routine(arguments_t *pArgs) /* Try to wakeup a blocking thread */ list_iterator_t it = {0u}; - list_iterator_init(&it, _pool_list_blockingHeadGet(id)); - pCurThread = (thread_context_t *)list_iterator_next(&it); - if (pCurThread) { - postcode = kernel_thread_entry_trigger(pCurThread, 0, _pool_schedule); + list_t *pList = (list_t *)&pCurPool->q_list; + list_iterator_init(&it, pList); + struct schedule_task *pCurTask = (struct schedule_task *)list_iterator_next(&it); + if (pCurTask) { + postcode = schedule_entry_trigger(pCurTask, _pool_schedule, 0u); } EXIT_CRITICAL_SECTION(); return postcode; } -/** - * @brief Convert the internal os id to kernel member number. - * - * @param id The provided unique id. - * - * @return The value of member number. - */ -u32_t _impl_pool_os_id_to_number(os_id_t id) -{ - if (_pool_id_isInvalid(id)) { - return 0u; - } - - return (u32_t)((id - kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_POOL)) / sizeof(pool_context_t)); -} - /** * @brief Initialize a new pool. * @@ -312,7 +256,7 @@ u32_t _impl_pool_os_id_to_number(os_id_t id) * * @return The pool unique id. */ -os_id_t _impl_pool_init(const void *pMemAddr, u16_t elementLen, u16_t elementNum, const char_t *pName) +u32_t _impl_pool_init(const void *pMemAddr, u16_t elementLen, u16_t elementNum, const char_t *pName) { if (!pMemAddr) { return OS_INVALID_ID_VAL; @@ -350,13 +294,14 @@ os_id_t _impl_pool_init(const void *pMemAddr, u16_t elementLen, u16_t elementNum * * @return The result of the operation. */ -i32p_t _impl_pool_take(os_id_t id, void **ppUserBuffer, u16_t bufferSize, u32_t timeout_ms) +i32p_t _impl_pool_take(u32_t ctx, void **ppUserBuffer, u16_t bufferSize, u32_t timeout_ms) { - if (_pool_id_isInvalid(id)) { + pool_context_t *pCtx = (pool_context_t *)ctx; + if (_pool_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_pool_id_isInit(id)) { + if (!_pool_context_isInit(pCtx)) { return PC_EOR; } @@ -367,7 +312,7 @@ i32p_t _impl_pool_take(os_id_t id, void **ppUserBuffer, u16_t bufferSize, u32_t } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, [1] = {.pv_val = (void **)ppUserBuffer}, [2] = {.u16_val = (u16_t)bufferSize}, [3] = {.u32_val = (u32_t)timeout_ms}, @@ -399,13 +344,14 @@ i32p_t _impl_pool_take(os_id_t id, void **ppUserBuffer, u16_t bufferSize, u32_t * * @return The result of the operation. */ -i32p_t _impl_pool_release(os_id_t id, void **ppUserBuffer) +i32p_t _impl_pool_release(u32_t ctx, void **ppUserBuffer) { - if (_pool_id_isInvalid(id)) { + pool_context_t *pCtx = (pool_context_t *)ctx; + if (_pool_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_pool_id_isInit(id)) { + if (!_pool_context_isInit(pCtx)) { return PC_EOR; } @@ -414,13 +360,9 @@ i32p_t _impl_pool_release(os_id_t id, void **ppUserBuffer) } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, [1] = {.pv_val = (void **)ppUserBuffer}, }; return kernel_privilege_invoke((const void *)_pool_release_privilege_routine, arguments); } - -#ifdef __cplusplus -} -#endif diff --git a/kernel/queue.c b/kernel/queue.c index f98acb3..f18f417 100644 --- a/kernel/queue.c +++ b/kernel/queue.c @@ -4,15 +4,11 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "kernel.h" #include "timer.h" #include "postcode.h" #include "trace.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "init.h" /** * Local unique postcode. @@ -21,18 +17,6 @@ extern "C" { #define _QUEUE_WAKEUP_SENDER (10u) #define _QUEUE_WAKEUP_RECEIVER (11u) -/** - * @brief Get the queue context based on provided unique id. - * - * @param id The timer unique id. - * - * @return The pointer of the current unique id timer context. - */ -static queue_context_t *_queue_context_get(os_id_t id) -{ - return (queue_context_t *)(kernel_member_unified_id_toContainerAddress(id)); -} - /** * @brief Check if the queue unique id if is's invalid. * @@ -40,9 +24,13 @@ static queue_context_t *_queue_context_get(os_id_t id) * * @return The true is invalid, otherwise is valid. */ -static b_t _queue_id_isInvalid(u32_t id) +static b_t _queue_context_isInvalid(queue_context_t *pCurQue) { - return kernel_member_unified_id_isInvalid(KERNEL_MEMBER_QUEUE, id); + u32_t start, end; + INIT_SECTION_FIRST(INIT_SECTION_OS_QUEUE_LIST, start); + INIT_SECTION_LAST(INIT_SECTION_OS_QUEUE_LIST, end); + + return ((u32_t)pCurQue < start || (u32_t)pCurQue >= end) ? true : false; } /** @@ -52,33 +40,9 @@ static b_t _queue_id_isInvalid(u32_t id) * * @return The true is initialized, otherwise is uninitialized. */ -static b_t _queue_id_isInit(i32_t id) -{ - queue_context_t *pCurQueue = _queue_context_get(id); - - return ((pCurQueue) ? (((pCurQueue->head.cs) ? (TRUE) : (FALSE))) : FALSE); -} - -/** - * @brief Get the queue blocking thread list head address. - * - * @return The blocking thread list head address. - */ -static list_t *_queue_list_inBlockingHeadGet(os_id_t id) +static b_t _queue_context_isInit(queue_context_t *pCurQue) { - queue_context_t *pCurQueue = _queue_context_get(id); - return (list_t *)(&pCurQueue->inBlockingThreadListHead); -} - -/** - * @brief Get the queue blocking thread list head address. - * - * @return The blocking thread list head address. - */ -static list_t *_queue_list_OutBlockingHeadGet(os_id_t id) -{ - queue_context_t *pCurQueue = _queue_context_get(id); - return (list_t *)(&pCurQueue->outBlockingThreadListHead); + return ((pCurQue) ? (((pCurQue->head.cs) ? (true) : (false))) : false); } /** @@ -176,31 +140,32 @@ static void _message_receive_behind(queue_context_t *pCurQueue, const u8_t *pUse * * @param id The unique id of the entry thread. */ -static void _queue_schedule(os_id_t id) +static void _queue_schedule(void *pTask) { - thread_context_t *pEntryThread = (thread_context_t *)(kernel_member_unified_id_toContainerAddress(id)); + struct schedule_task *pCurTask = (struct schedule_task *)pTask; + struct call_entry *pEntry = &pCurTask->exec.entry; - timeout_remove(&pEntryThread->expire, true); + timeout_remove(&pCurTask->expire, true); - queue_context_t *pCurQueue = (queue_context_t*)pEntryThread->schedule.pPendCtx; - queue_sch_t *pQue_sche = (queue_sch_t *)pEntryThread->schedule.pPendData; + queue_context_t *pCurQueue = (queue_context_t *)pCurTask->pPendCtx; + queue_sch_t *pQue_sche = (queue_sch_t *)pCurTask->pPendData; if (!pQue_sche) { return; } - if (pEntryThread->schedule.entry.result == _QUEUE_WAKEUP_RECEIVER) { + if (pEntry->result == _QUEUE_WAKEUP_RECEIVER) { if (pQue_sche->reverse) { _message_receive_behind((queue_context_t *)pCurQueue, pQue_sche->pUsrBuf, pQue_sche->size); } else { _message_receive((queue_context_t *)pCurQueue, pQue_sche->pUsrBuf, pQue_sche->size); } - pEntryThread->schedule.entry.result = 0; - } else if (pEntryThread->schedule.entry.result == _QUEUE_WAKEUP_SENDER) { + pEntry->result = 0; + } else if (pEntry->result == _QUEUE_WAKEUP_SENDER) { if (pQue_sche->reverse) { _message_send_front((queue_context_t *)pCurQueue, pQue_sche->pUsrBuf, pQue_sche->size); } else { _message_send((queue_context_t *)pCurQueue, pQue_sche->pUsrBuf, pQue_sche->size); } - pEntryThread->schedule.entry.result = 0; + pEntry->result = 0; } } @@ -219,18 +184,14 @@ static u32_t _queue_init_privilege_routine(arguments_t *pArgs) u16_t elementLen = (u16_t)(pArgs[1].u16_val); u16_t elementNum = (u16_t)(pArgs[2].u16_val); const char_t *pName = (const char_t *)(pArgs[3].pch_val); - u32_t endAddr = 0u; - queue_context_t *pCurQueue = NULL; - - pCurQueue = (queue_context_t *)kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_QUEUE); - endAddr = (u32_t)kernel_member_id_toContainerEndAddress(KERNEL_MEMBER_QUEUE); - do { - os_id_t id = kernel_member_containerAddress_toUnifiedid((u32_t)pCurQueue); - if (_queue_id_isInvalid(id)) { + + INIT_SECTION_FOREACH(INIT_SECTION_OS_QUEUE_LIST, queue_context_t, pCurQueue) + { + if (_queue_context_isInvalid(pCurQueue)) { break; } - if (_queue_id_isInit(id)) { + if (_queue_context_isInit(pCurQueue)) { continue; } @@ -246,11 +207,11 @@ static u32_t _queue_init_privilege_routine(arguments_t *pArgs) pCurQueue->cacheSize = 0u; EXIT_CRITICAL_SECTION(); - return id; - } while ((u32_t)++pCurQueue < endAddr); + return (u32_t)pCurQueue; + }; EXIT_CRITICAL_SECTION(); - return OS_INVALID_ID_VAL; + return 0u; } /** @@ -264,12 +225,11 @@ static i32p_t _queue_send_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; + queue_context_t *pCurQueue = (queue_context_t *)pArgs[0].u32_val; queue_sch_t *pQue_sch = (queue_sch_t *)pArgs[1].ptr_val; u32_t timeout_ms = (u32_t)pArgs[2].u32_val; i32p_t postcode = 0; - queue_context_t *pCurQueue = _queue_context_get(id); thread_context_t *pCurThread = kernel_thread_runContextGet(); if (pQue_sch->size > pCurQueue->elementLength) { EXIT_CRITICAL_SECTION(); @@ -281,9 +241,7 @@ static i32p_t _queue_send_privilege_routine(arguments_t *pArgs) EXIT_CRITICAL_SECTION(); return PC_EOR; } - - pCurThread->schedule.pPendData = (void *)pQue_sch; - postcode = kernel_thread_exit_trigger(pCurThread, pCurQueue, _queue_list_inBlockingHeadGet(id), timeout_ms); + postcode = schedule_exit_trigger(&pCurThread->task, pCurQueue, pQue_sch, &pCurQueue->in_QList, timeout_ms, true); PC_IF(postcode, PC_PASS) { postcode = PC_OS_WAIT_UNAVAILABLE; @@ -297,10 +255,11 @@ static i32p_t _queue_send_privilege_routine(arguments_t *pArgs) /* Try to wakeup a blocking thread */ list_iterator_t it = {0u}; - list_iterator_init(&it, _queue_list_OutBlockingHeadGet(id)); - pCurThread = (thread_context_t *)list_iterator_next(&it); - if (pCurThread) { - postcode = kernel_thread_entry_trigger(pCurThread, _QUEUE_WAKEUP_RECEIVER, _queue_schedule); + list_t *plist = (list_t *)&pCurQueue->out_QList; + list_iterator_init(&it, plist); + struct schedule_task *pCurTask = (struct schedule_task *)list_iterator_next(&it); + if (pCurTask) { + postcode = schedule_entry_trigger(pCurTask, _queue_schedule, _QUEUE_WAKEUP_RECEIVER); } } @@ -319,12 +278,11 @@ static i32p_t _queue_receive_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; + queue_context_t *pCurQueue = (queue_context_t *)pArgs[0].u32_val; queue_sch_t *pQue_sch = (queue_sch_t *)pArgs[1].ptr_val; u32_t timeout_ms = (u32_t)pArgs[2].u32_val; i32p_t postcode = 0; - queue_context_t *pCurQueue = _queue_context_get(id); thread_context_t *pCurThread = (thread_context_t *)kernel_thread_runContextGet(); if (pQue_sch->size > pCurQueue->elementLength) { EXIT_CRITICAL_SECTION(); @@ -336,8 +294,7 @@ static i32p_t _queue_receive_privilege_routine(arguments_t *pArgs) EXIT_CRITICAL_SECTION(); return PC_EOR; } - pCurThread->schedule.pPendData = (void *)pQue_sch; - postcode = kernel_thread_exit_trigger(pCurThread, pCurQueue, _queue_list_OutBlockingHeadGet(id), timeout_ms); + postcode = schedule_exit_trigger(&pCurThread->task, pCurQueue, pQue_sch, &pCurQueue->out_QList, timeout_ms, true); PC_IF(postcode, PC_PASS) { postcode = PC_OS_WAIT_UNAVAILABLE; @@ -349,12 +306,13 @@ static i32p_t _queue_receive_privilege_routine(arguments_t *pArgs) _message_receive(pCurQueue, (const u8_t *)pQue_sch->pUsrBuf, pQue_sch->size); } - /* Try to wakeup a blocking thread */ + /* Try to wakeup a blocking task */ list_iterator_t it = {0u}; - list_iterator_init(&it, _queue_list_inBlockingHeadGet(id)); - pCurThread = (thread_context_t *)list_iterator_next(&it); - if (pCurThread) { - postcode = kernel_thread_entry_trigger(pCurThread, _QUEUE_WAKEUP_SENDER, _queue_schedule); + list_t *plist = (list_t *)&pCurQueue->in_QList; + list_iterator_init(&it, plist); + struct schedule_task *pCurTask = (struct schedule_task *)list_iterator_next(&it); + if (pCurTask) { + postcode = schedule_entry_trigger(pCurTask, _queue_schedule, _QUEUE_WAKEUP_SENDER); } } @@ -362,22 +320,6 @@ static i32p_t _queue_receive_privilege_routine(arguments_t *pArgs) return postcode; } -/** - * @brief Convert the internal os id to kernel member number. - * - * @param id The provided unique id. - * - * @return The value of member number. - */ -u32_t _impl_queue_os_id_to_number(os_id_t id) -{ - if (_queue_id_isInvalid(id)) { - return 0u; - } - - return (u32_t)((id - kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_QUEUE)) / sizeof(queue_context_t)); -} - /** * @brief Initialize a new queue. * @@ -388,7 +330,7 @@ u32_t _impl_queue_os_id_to_number(os_id_t id) * * @return The queue unique id. */ -os_id_t _impl_queue_init(const void *pQueueBufferAddr, u16_t elementLen, u16_t elementNum, const char_t *pName) +u32_t _impl_queue_init(const void *pQueueBufferAddr, u16_t elementLen, u16_t elementNum, const char_t *pName) { if (!pQueueBufferAddr) { return OS_INVALID_ID_VAL; @@ -415,7 +357,7 @@ os_id_t _impl_queue_init(const void *pQueueBufferAddr, u16_t elementLen, u16_t e /** * @brief Send a queue message. * - * @param id The queue unique id. + * @param ctx The queue unique id. * @param pUserBuffer The pointer of the message buffer address. * @param bufferSize The queue buffer size. * @param isToFront The direction of the message operation. @@ -423,13 +365,14 @@ os_id_t _impl_queue_init(const void *pQueueBufferAddr, u16_t elementLen, u16_t e * * @return The result of the operation. */ -i32p_t _impl_queue_send(os_id_t id, const u8_t *pUserBuffer, u16_t bufferSize, b_t isToFront, u32_t timeout_ms) +i32p_t _impl_queue_send(u32_t ctx, const u8_t *pUserBuffer, u16_t bufferSize, b_t isToFront, u32_t timeout_ms) { - if (_queue_id_isInvalid(id)) { + queue_context_t *pCtx = (queue_context_t *)ctx; + if (_queue_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_queue_id_isInit(id)) { + if (!_queue_context_isInit(pCtx)) { return PC_EOR; } @@ -441,7 +384,7 @@ i32p_t _impl_queue_send(os_id_t id, const u8_t *pUserBuffer, u16_t bufferSize, b queue_sch_t que_sch = {.pUsrBuf = pUserBuffer, .size = bufferSize, .reverse = isToFront}; arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, [1] = {.ptr_val = (void *)&que_sch}, [2] = {.u32_val = (u32_t)timeout_ms}, }; @@ -474,13 +417,14 @@ i32p_t _impl_queue_send(os_id_t id, const u8_t *pUserBuffer, u16_t bufferSize, b * * @return The result of the operation. */ -i32p_t _impl_queue_receive(os_id_t id, const u8_t *pUserBuffer, u16_t bufferSize, b_t isFromBack, u32_t timeout_ms) +i32p_t _impl_queue_receive(u32_t ctx, const u8_t *pUserBuffer, u16_t bufferSize, b_t isFromBack, u32_t timeout_ms) { - if (_queue_id_isInvalid(id)) { + queue_context_t *pCtx = (queue_context_t *)ctx; + if (_queue_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_queue_id_isInit(id)) { + if (!_queue_context_isInit(pCtx)) { return PC_EOR; } @@ -492,7 +436,7 @@ i32p_t _impl_queue_receive(os_id_t id, const u8_t *pUserBuffer, u16_t bufferSize queue_sch_t que_sch = {.pUsrBuf = pUserBuffer, .size = bufferSize, .reverse = isFromBack}; arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, [1] = {.ptr_val = (void *)&que_sch}, [2] = {.u32_val = (u32_t)timeout_ms}, }; @@ -501,10 +445,7 @@ i32p_t _impl_queue_receive(os_id_t id, const u8_t *pUserBuffer, u16_t bufferSize ENTER_CRITICAL_SECTION(); if (postcode == PC_OS_WAIT_UNAVAILABLE) { - thread_context_t *pCurThread = (thread_context_t *)kernel_thread_runContextGet(); - - postcode = (i32p_t)pCurThread->schedule.entry.result; - pCurThread->schedule.entry.result = 0u; + postcode = kernel_schedule_result_take(); } PC_IF(postcode, PC_PASS_INFO) @@ -517,7 +458,3 @@ i32p_t _impl_queue_receive(os_id_t id, const u8_t *pUserBuffer, u16_t bufferSize EXIT_CRITICAL_SECTION(); return postcode; } - -#ifdef __cplusplus -} -#endif diff --git a/kernel/semaphore.c b/kernel/semaphore.c index 763d7ac..4437657 100644 --- a/kernel/semaphore.c +++ b/kernel/semaphore.c @@ -4,36 +4,17 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "kernel.h" #include "timer.h" #include "postcode.h" #include "trace.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "init.h" /** * Local unique postcode. */ #define PC_EOR PC_IER(PC_OS_CMPT_SEMAPHORE_4) -static void _semaphore_schedule(os_id_t id); - -/** - * @brief Get the semaphore context based on provided unique id. - * - * @param id The timer unique id. - * - * @return The pointer of the current unique id timer context. - */ - -static semaphore_context_t *_semaphore_context_get(os_id_t id) -{ - return (semaphore_context_t *)(kernel_member_unified_id_toContainerAddress(id)); -} - /** * @brief Check if the semaphore unique id if is's invalid. * @@ -41,9 +22,13 @@ static semaphore_context_t *_semaphore_context_get(os_id_t id) * * @return The true is invalid, otherwise is valid. */ -static b_t _semaphore_id_isInvalid(u32_t id) +static b_t _semaphore_context_isInvalid(semaphore_context_t *pCurSemaphore) { - return kernel_member_unified_id_isInvalid(KERNEL_MEMBER_SEMAPHORE, id); + u32_t start, end; + INIT_SECTION_FIRST(INIT_SECTION_OS_SEMAPHORE_LIST, start); + INIT_SECTION_LAST(INIT_SECTION_OS_SEMAPHORE_LIST, end); + + return ((u32_t)pCurSemaphore < start || (u32_t)pCurSemaphore >= end) ? true : false; } /** @@ -53,40 +38,9 @@ static b_t _semaphore_id_isInvalid(u32_t id) * * @return The true is initialized, otherwise is uninitialized. */ -static b_t _semaphore_id_isInit(i32_t id) -{ - semaphore_context_t *pCurSem = _semaphore_context_get(id); - - return ((pCurSem) ? (((pCurSem->head.cs) ? (TRUE) : (FALSE))) : FALSE); -} - -/** - * @brief Get the semaphore blocking thread list head address. - * - * @return The blocking thread list head address. - */ -static list_t *_semaphore_list_blockingHeadGet(os_id_t id) -{ - semaphore_context_t *pCurSemaphore = _semaphore_context_get(id); - - return (list_t *)((pCurSemaphore) ? (&pCurSemaphore->blockingThreadHead) : (NULL)); -} - -/** - * @brief Pick up a highest priority thread that blocking by the semaphore pending list. - * - * @param The semaphore unique id. - * - * @return The highest blocking thread head. - */ -static linker_head_t *_semaphore_linker_head_fromBlocking(os_id_t id) +static b_t _semaphore_context_isInit(semaphore_context_t *pCurSemaphore) { - ENTER_CRITICAL_SECTION(); - - list_t *pListPending = (list_t *)_semaphore_list_blockingHeadGet(id); - - EXIT_CRITICAL_SECTION(); - return (linker_head_t *)(pListPending->pHead); + return ((pCurSemaphore) ? (((pCurSemaphore->head.cs) ? (true) : (false))) : false); } /** @@ -94,14 +48,14 @@ static linker_head_t *_semaphore_linker_head_fromBlocking(os_id_t id) * * @param id The unique id of the entry thread. */ -static void _semaphore_schedule(os_id_t id) +static void _semaphore_schedule(void *pTask) { - thread_context_t *pEntryThread = (thread_context_t *)(kernel_member_unified_id_toContainerAddress(id)); - timeout_remove(&pEntryThread->expire, true); - semaphore_context_t *pCurSemaphore = (semaphore_context_t *)pEntryThread->schedule.pPendCtx; + struct schedule_task *pCurTask = (struct schedule_task *)pTask; + timeout_remove(&pCurTask->expire, true); + semaphore_context_t *pCurSemaphore = (semaphore_context_t *)pCurTask->pPendCtx; /* If the PC arrive, the semaphore will be available and can be acquired */ pCurSemaphore->remains--; // The semaphore has available count - pEntryThread->schedule.entry.result = 0; + pCurTask->exec.entry.result = 0; } /** @@ -118,21 +72,14 @@ static u32_t _semaphore_init_privilege_routine(arguments_t *pArgs) u8_t initialCount = (u8_t)(pArgs[0].u8_val); u8_t limitCount = (u8_t)(pArgs[1].u8_val); const char_t *pName = (const char_t *)(pArgs[2].pch_val); - u32_t internal = 0u; - u32_t endAddr = 0u; - semaphore_context_t *pCurSemaphore = NULL; - - internal = (sizeof(semaphore_context_t) * KERNEL_APPLICATION_SEMAPHORE_INSTANCE); - pCurSemaphore = (semaphore_context_t *)(kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_SEMAPHORE) + internal); - endAddr = (u32_t)kernel_member_id_toContainerEndAddress(KERNEL_MEMBER_SEMAPHORE); - do { - os_id_t id = kernel_member_containerAddress_toUnifiedid((u32_t)pCurSemaphore); - if (_semaphore_id_isInvalid(id)) { + INIT_SECTION_FOREACH(INIT_SECTION_OS_SEMAPHORE_LIST, semaphore_context_t, pCurSemaphore) + { + if (_semaphore_context_isInvalid(pCurSemaphore)) { break; } - if (_semaphore_id_isInit(id)) { + if (_semaphore_context_isInit(pCurSemaphore)) { continue; } @@ -143,11 +90,11 @@ static u32_t _semaphore_init_privilege_routine(arguments_t *pArgs) pCurSemaphore->limits = limitCount; EXIT_CRITICAL_SECTION(); - return id; - } while ((u32_t)++pCurSemaphore < endAddr); + return (u32_t)pCurSemaphore; + } EXIT_CRITICAL_SECTION(); - return OS_INVALID_ID_VAL; + return 0u; } /** @@ -161,17 +108,15 @@ static i32p_t _semaphore_take_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; + semaphore_context_t *pCurSemaphore = (semaphore_context_t *)pArgs[0].u32_val; u32_t timeout_ms = (u32_t)pArgs[1].u32_val; - semaphore_context_t *pCurSemaphore = NULL; thread_context_t *pCurThread = NULL; i32p_t postcode = PC_OS_WAIT_AVAILABLE; pCurThread = kernel_thread_runContextGet(); - pCurSemaphore = _semaphore_context_get(id); if (!pCurSemaphore->remains) { /* No availabe count */ - postcode = kernel_thread_exit_trigger(pCurThread, pCurSemaphore, _semaphore_list_blockingHeadGet(id), timeout_ms); + postcode = schedule_exit_trigger(&pCurThread->task, pCurSemaphore, NULL, &pCurSemaphore->q_list, timeout_ms, true); PC_IF(postcode, PC_PASS) { postcode = PC_OS_WAIT_UNAVAILABLE; @@ -199,16 +144,15 @@ static i32p_t _semaphore_give_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; + semaphore_context_t *pCurSemaphore = (semaphore_context_t *)pArgs[0].u32_val; i32p_t postcode = 0; - semaphore_context_t *pCurSemaphore = _semaphore_context_get(id); if (pCurSemaphore->remains < pCurSemaphore->limits) { pCurSemaphore->remains++; - thread_context_t *pSemaphoreHighestBlockingThread = (thread_context_t *)_semaphore_linker_head_fromBlocking(id); - if (pSemaphoreHighestBlockingThread) { - postcode = kernel_thread_entry_trigger(pSemaphoreHighestBlockingThread, 0, _semaphore_schedule); + struct schedule_task *pCurTask = (struct schedule_task *)list_head(&pCurSemaphore->q_list); + if (pCurTask) { + postcode = schedule_entry_trigger(pCurTask, _semaphore_schedule, 0u); } } @@ -227,44 +171,26 @@ static i32p_t _semaphore_flush_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - semaphore_context_t *pCurSemaphore = NULL; - thread_context_t *pCurThread = NULL; - os_id_t id = (os_id_t)pArgs[0].u32_val; + semaphore_context_t *pCurSemaphore = (semaphore_context_t *)pArgs[0].u32_val; i32p_t postcode = 0; - pCurSemaphore = _semaphore_context_get(id); list_iterator_t it = {0u}; - list_iterator_init(&it, _semaphore_list_blockingHeadGet(id)); - pCurThread = (thread_context_t *)list_iterator_next(&it); - while (pCurThread) { + list_t *pQList = (list_t *)&pCurSemaphore->q_list; + list_iterator_init(&it, pQList); + struct schedule_task *pCurTask = (struct schedule_task *)list_iterator_next(&it); + while (pCurTask) { pCurSemaphore->remains++; - postcode = kernel_thread_entry_trigger(pCurThread, 0, _semaphore_schedule); + postcode = schedule_entry_trigger(pCurTask, _semaphore_schedule, 0u); if (PC_IER(postcode)) { break; } - pCurThread = (thread_context_t *)list_iterator_next(&it); + pCurTask = (struct schedule_task *)list_iterator_next(&it); } EXIT_CRITICAL_SECTION(); return postcode; } -/** - * @brief Convert the internal os id to kernel member number. - * - * @param id The provided unique id. - * - * @return The semaphore member's number. - */ -u32_t _impl_semaphore_os_id_to_number(os_id_t id) -{ - if (_semaphore_id_isInvalid(id)) { - return 0u; - } - - return (u32_t)((id - kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_SEMAPHORE)) / sizeof(semaphore_context_t)); -} - /** * @brief Initialize a new semaphore. * @@ -274,7 +200,7 @@ u32_t _impl_semaphore_os_id_to_number(os_id_t id) * * @return The semaphore unique id. */ -os_id_t _impl_semaphore_init(u8_t remainCount, u8_t limitCount, const char_t *pName) +u32_t _impl_semaphore_init(u8_t remainCount, u8_t limitCount, const char_t *pName) { if (!limitCount) { return OS_INVALID_ID_VAL; @@ -296,17 +222,18 @@ os_id_t _impl_semaphore_init(u8_t remainCount, u8_t limitCount, const char_t *pN /** * @brief Take the semaphore away with timeout option. * - * @param id The semaphore unique id. + * @param ctx The semaphore unique id. * * @return The result of the operation. */ -i32p_t _impl_semaphore_take(os_id_t id, u32_t timeout_ms) +i32p_t _impl_semaphore_take(u32_t ctx, u32_t timeout_ms) { - if (_semaphore_id_isInvalid(id)) { + semaphore_context_t *pCtx = (semaphore_context_t *)ctx; + if (_semaphore_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_semaphore_id_isInit(id)) { + if (!_semaphore_context_isInit(pCtx)) { return PC_EOR; } @@ -319,7 +246,7 @@ i32p_t _impl_semaphore_take(os_id_t id, u32_t timeout_ms) } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, [1] = {.u32_val = (u32_t)timeout_ms}, }; @@ -349,18 +276,19 @@ i32p_t _impl_semaphore_take(os_id_t id, u32_t timeout_ms) * * @return The result of the operation. */ -i32p_t _impl_semaphore_give(os_id_t id) +i32p_t _impl_semaphore_give(u32_t ctx) { - if (_semaphore_id_isInvalid(id)) { + semaphore_context_t *pCtx = (semaphore_context_t *)ctx; + if (_semaphore_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_semaphore_id_isInit(id)) { + if (!_semaphore_context_isInit(pCtx)) { return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, }; return kernel_privilege_invoke((const void *)_semaphore_give_privilege_routine, arguments); @@ -373,23 +301,20 @@ i32p_t _impl_semaphore_give(os_id_t id) * * @return The result of the operation. */ -i32p_t _impl_semaphore_flush(os_id_t id) +i32p_t _impl_semaphore_flush(u32_t ctx) { - if (_semaphore_id_isInvalid(id)) { + semaphore_context_t *pCtx = (semaphore_context_t *)ctx; + if (_semaphore_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_semaphore_id_isInit(id)) { + if (!_semaphore_context_isInit(pCtx)) { return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, }; return kernel_privilege_invoke((const void *)_semaphore_flush_privilege_routine, arguments); } - -#ifdef __cplusplus -} -#endif diff --git a/kernel/subscribe.c b/kernel/subscribe.c index cdcbc24..b73cedc 100644 --- a/kernel/subscribe.c +++ b/kernel/subscribe.c @@ -4,19 +4,15 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "kernel.h" #include "postcode.h" #include "trace.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "init.h" /** * Local unique postcode. */ -#define _PCER PC_IER(PC_OS_CMPT_PUBLISH_10) +#define PC_EOR PC_IER(PC_OS_CMPT_PUBLISH_10) /** * Data structure for location timer @@ -28,19 +24,7 @@ typedef struct { /** * Local timer resource */ -_sp_resource_t g_sp_resource = {0u}; - -/** - * @brief Get the publish context based on provided unique id. - * - * @param id The publish unique id. - * - * @return The pointer of the current unique id publish context. - */ -static publish_context_t *_publish_context_get(os_id_t id) -{ - return (publish_context_t *)(kernel_member_unified_id_toContainerAddress(id)); -} +_sp_resource_t g_sp_rsc = {0u}; /** * @brief Check if the publish unique id if is's invalid. @@ -49,9 +33,13 @@ static publish_context_t *_publish_context_get(os_id_t id) * * @return The true is invalid, otherwise is valid. */ -static b_t _publish_id_isInvalid(u32_t id) +static b_t _publish_context_isInvalid(publish_context_t *pCurPub) { - return kernel_member_unified_id_isInvalid(KERNEL_MEMBER_PUBLISH, id); + u32_t start, end; + INIT_SECTION_FIRST(INIT_SECTION_OS_PUBLISH_LIST, start); + INIT_SECTION_LAST(INIT_SECTION_OS_PUBLISH_LIST, end); + + return ((u32_t)pCurPub < start || (u32_t)pCurPub >= end) ? true : false; } /** @@ -61,23 +49,9 @@ static b_t _publish_id_isInvalid(u32_t id) * * @return The true is initialized, otherwise is uninitialized. */ -static b_t _publish_id_isInit(u32_t id) -{ - publish_context_t *pCurPublish = _publish_context_get(id); - - return ((pCurPublish) ? (((pCurPublish->head.cs) ? (TRUE) : (FALSE))) : FALSE); -} - -/** - * @brief Get the subscribe context based on provided unique id. - * - * @param id The subscribe unique id. - * - * @return The pointer of the current unique id subscribe context. - */ -static subscribe_context_t *_subscribe_context_get(os_id_t id) +static b_t _publish_context_isInit(publish_context_t *pCurPub) { - return (subscribe_context_t *)(kernel_member_unified_id_toContainerAddress(id)); + return ((pCurPub) ? (((pCurPub->head.cs) ? (true) : (false))) : false); } /** @@ -87,9 +61,13 @@ static subscribe_context_t *_subscribe_context_get(os_id_t id) * * @return The true is invalid, otherwise is valid. */ -static b_t _subscribe_id_isInvalid(u32_t id) +static b_t _subscribe_context_isInvalid(subscribe_context_t *pCurSub) { - return kernel_member_unified_id_isInvalid(KERNEL_MEMBER_SUBSCRIBE, id); + u32_t start, end; + INIT_SECTION_FIRST(INIT_SECTION_OS_SUBSCRIBE_LIST, start); + INIT_SECTION_LAST(INIT_SECTION_OS_SUBSCRIBE_LIST, end); + + return ((u32_t)pCurSub < start || (u32_t)pCurSub >= end) ? true : false; } /** @@ -99,25 +77,9 @@ static b_t _subscribe_id_isInvalid(u32_t id) * * @return The true is initialized, otherwise is uninitialized. */ -static b_t _subscribe_id_isInit(u32_t id) -{ - subscribe_context_t *pCurSubscribe = _subscribe_context_get(id); - - return ((pCurSubscribe) ? (((pCurSubscribe->head.cs) ? (TRUE) : (FALSE))) : FALSE); -} - -/** - * @brief Pick up a highest priority thread that blocking by the event pending list. - * - * @param The event unique id. - * - * @return The highest blocking thread head. - */ -static list_t *_publish_list_subscribeHeadGet(os_id_t id) +static b_t _subscribe_context_isInit(subscribe_context_t *pCurSub) { - publish_context_t *pCurPublish = _publish_context_get(id); - - return (list_t *)((pCurPublish) ? (&pCurPublish->subscribeListHead) : (NULL)); + return ((pCurSub) ? (((pCurSub->head.cs) ? (true) : (false))) : false); } /** @@ -126,11 +88,11 @@ static list_t *_publish_list_subscribeHeadGet(os_id_t id) * @param pCurHead The pointer of the publish subscribe linker head. * @param pCurHead The pointer of the publish subscribe linker head. */ -static void _subscribe_list_transfer_toTargetHead(linker_t *pLinker, os_id_t pub) +static void _subscribe_list_transfer_toTargetHead(linker_t *pLinker, publish_context_t *pCurPub) { ENTER_CRITICAL_SECTION(); - list_t *pToPubList = (list_t *)_publish_list_subscribeHeadGet(pub); + list_t *pToPubList = (list_t *)&pCurPub->q_list; linker_list_transaction_common(pLinker, pToPubList, LIST_TAIL); EXIT_CRITICAL_SECTION(); @@ -148,18 +110,14 @@ static u32_t _publish_init_privilege_routine(arguments_t *pArgs) ENTER_CRITICAL_SECTION(); const char_t *pName = (const char_t *)(pArgs[0].pch_val); - u32_t endAddr = 0u; - publish_context_t *pCurPublish = NULL; - - pCurPublish = (publish_context_t *)kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_PUBLISH); - endAddr = (u32_t)kernel_member_id_toContainerEndAddress(KERNEL_MEMBER_PUBLISH); - do { - os_id_t id = kernel_member_containerAddress_toUnifiedid((u32_t)pCurPublish); - if (_publish_id_isInvalid(id)) { + + INIT_SECTION_FOREACH(INIT_SECTION_OS_PUBLISH_LIST, publish_context_t, pCurPublish) + { + if (_publish_context_isInvalid(pCurPublish)) { break; } - if (_publish_id_isInit(id)) { + if (_publish_context_isInit(pCurPublish)) { continue; } @@ -168,12 +126,11 @@ static u32_t _publish_init_privilege_routine(arguments_t *pArgs) pCurPublish->head.pName = pName; EXIT_CRITICAL_SECTION(); - return id; - - } while ((u32_t)++pCurPublish < endAddr); + return (u32_t)pCurPublish; + }; EXIT_CRITICAL_SECTION(); - return OS_INVALID_ID_VAL; + return 0u; } /** @@ -187,22 +144,22 @@ static u32_t _publish_data_submit_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; + publish_context_t *pCurSub = (publish_context_t *)pArgs[0].u32_val; const void *pPublishData = (const void *)pArgs[1].ptr_val; u16_t publishSize = (u16_t)pArgs[2].u16_val; - b_t need = FALSE; + b_t need = false; i32p_t postcode = 0; - publish_context_t *pCurPublish = _publish_context_get(id); struct notify_callback *pNotify = NULL; list_iterator_t it = {0u}; - list_iterator_init(&it, _publish_list_subscribeHeadGet(id)); + list_t *pList = (list_t *)&pCurSub->q_list; + list_iterator_init(&it, pList); while (list_iterator_next_condition(&it, (void *)&pNotify)) { pNotify->updated++; os_memcpy((u8_t *)pNotify->pData, (const u8_t *)pPublishData, MINI_AB(publishSize, pNotify->len)); if ((!pNotify->muted) && (pNotify->fn)) { need = true; - pNotify->fn(pNotify); + pNotify->fn((void *)&pNotify->linker.node); } } @@ -214,13 +171,13 @@ static u32_t _publish_data_submit_privilege_routine(arguments_t *pArgs) return postcode; } -static void _subscribe_notification(void *pLinker) +void subscribe_notification(void *pNode) { - subscribe_context_t *pCurSubscribe = (subscribe_context_t *)CONTAINEROF(pLinker, subscribe_context_t, notify); + subscribe_context_t *pCurSubscribe = (subscribe_context_t *)CONTAINEROF(pNode, subscribe_context_t, notify); - list_t *pCallback_list = (list_t *)&g_sp_resource.callback_list; + list_t *pCallback_list = (list_t *)&g_sp_rsc.callback_list; if (!list_node_isExisted(pCallback_list, &pCurSubscribe->call.node)) { - list_node_push((list_t *)&g_sp_resource.callback_list, &pCurSubscribe->call.node, LIST_HEAD); + list_node_push((list_t *)&g_sp_rsc.callback_list, &pCurSubscribe->call.node, LIST_HEAD); } } @@ -238,39 +195,35 @@ static u32_t _subscribe_init_privilege_routine(arguments_t *pArgs) void *pData = (void *)pArgs[0].pv_val; u16_t size = (u16_t)pArgs[1].u16_val; const char_t *pName = (const char_t *)(pArgs[2].pch_val); - u32_t endAddr = 0u; - subscribe_context_t *pCurSubscribe = NULL; - - pCurSubscribe = (subscribe_context_t *)kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_SUBSCRIBE); - endAddr = (u32_t)kernel_member_id_toContainerEndAddress(KERNEL_MEMBER_SUBSCRIBE); - do { - os_id_t id = kernel_member_containerAddress_toUnifiedid((u32_t)pCurSubscribe); - if (_subscribe_id_isInvalid(id)) { + + INIT_SECTION_FOREACH(INIT_SECTION_OS_SUBSCRIBE_LIST, subscribe_context_t, pCurSubscribe) + { + if (_subscribe_context_isInvalid(pCurSubscribe)) { break; } - if (_subscribe_id_isInit(id)) { + if (_subscribe_context_isInit(pCurSubscribe)) { continue; } os_memset((char_t *)pCurSubscribe, 0x0u, sizeof(subscribe_context_t)); pCurSubscribe->head.cs = CS_INITED; pCurSubscribe->head.pName = pName; + pCurSubscribe->pPublisher = NULL; pCurSubscribe->accepted = 0u; pCurSubscribe->notify.pData = pData; pCurSubscribe->notify.len = size; pCurSubscribe->notify.muted = false; - pCurSubscribe->notify.fn = _subscribe_notification; + pCurSubscribe->notify.fn = subscribe_notification; EXIT_CRITICAL_SECTION(); - return id; - - } while ((u32_t)++pCurSubscribe < endAddr); + return (u32_t)pCurSubscribe; + }; EXIT_CRITICAL_SECTION(); - return OS_INVALID_ID_VAL; + return 0u; } /** @@ -284,18 +237,17 @@ static u32_t _subscribe_register_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t sub = (os_id_t)pArgs[0].u32_val; - os_id_t pub = (os_id_t)pArgs[1].u32_val; + subscribe_context_t *pCurSub = (subscribe_context_t *)pArgs[0].u32_val; + publish_context_t *pCurPub = (publish_context_t *)pArgs[1].u32_val; b_t isMute = (b_t)pArgs[2].b_val; pSubscribe_callbackFunc_t pCallFun = (pSubscribe_callbackFunc_t)(pArgs[3].ptr_val); - subscribe_context_t *pCurSubscribe = _subscribe_context_get(sub); - pCurSubscribe->pPublisher = _publish_context_get(pub); + pCurSub->pPublisher = pCurPub; - pCurSubscribe->notify.muted = isMute; - pCurSubscribe->call.pSubCallEntry = pCallFun; + pCurSub->notify.muted = isMute; + pCurSub->call.pSubCallEntry = pCallFun; - _subscribe_list_transfer_toTargetHead(&pCurSubscribe->notify.linker, pub); + _subscribe_list_transfer_toTargetHead(&pCurSub->notify.linker, pCurPub); EXIT_CRITICAL_SECTION(); return 0; @@ -312,12 +264,11 @@ static u32_t _subscribe_data_is_ready_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - u32_t id = (u32_t)pArgs[0].u32_val; - - subscribe_context_t *pCurSubscribe = _subscribe_context_get(id); + subscribe_context_t *pCurSub = (subscribe_context_t *)pArgs[0].u32_val; + b_t ready = (pCurSub->accepted == pCurSub->notify.updated) ? (true) : (false); EXIT_CRITICAL_SECTION(); - return (pCurSubscribe->accepted == pCurSubscribe->notify.updated) ? (true) : (false); + return ready; } /** @@ -331,16 +282,14 @@ static u32_t _subscribe_apply_data_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - u32_t id = (u32_t)pArgs[0].u32_val; + subscribe_context_t *pCurSub = (subscribe_context_t *)pArgs[0].u32_val; u8_t *pDataBuffer = (u8_t *)pArgs[1].pv_val; u16_t *pDataLen = (u16_t *)pArgs[2].pv_val; - subscribe_context_t *pCurSubscribe = _subscribe_context_get(id); - - if (pCurSubscribe->accepted < pCurSubscribe->notify.updated) { - *pDataLen = MINI_AB(*pDataLen, pCurSubscribe->notify.len); - os_memcpy(pDataBuffer, pCurSubscribe->notify.pData, *pDataLen); - pCurSubscribe->accepted = pCurSubscribe->notify.updated; + if (pCurSub->accepted < pCurSub->notify.updated) { + *pDataLen = MINI_AB(*pDataLen, pCurSub->notify.len); + os_memcpy(pDataBuffer, pCurSub->notify.pData, *pDataLen); + pCurSub->accepted = pCurSub->notify.updated; EXIT_CRITICAL_SECTION(); return 0; @@ -350,38 +299,6 @@ static u32_t _subscribe_apply_data_privilege_routine(arguments_t *pArgs) return PC_OS_WAIT_UNAVAILABLE; } -/** - * @brief Convert the internal os id to kernel member number. - * - * @param id The provided unique id. - * - * @return The value of member number. - */ -u32_t _impl_publish_os_id_to_number(os_id_t id) -{ - if (_publish_id_isInvalid(id)) { - return 0u; - } - - return (u32_t)((id - kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_PUBLISH)) / sizeof(publish_context_t)); -} - -/** - * @brief Convert the internal os id to kernel member number. - * - * @param id The provided unique id. - * - * @return The value of member number. - */ -u32_t _impl_subscribe_os_id_to_number(os_id_t id) -{ - if (_subscribe_id_isInvalid(id)) { - return 0u; - } - - return (u32_t)((id - kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_SUBSCRIBE)) / sizeof(subscribe_context_t)); -} - /** * @brief Initialize a new publish. * @@ -389,7 +306,7 @@ u32_t _impl_subscribe_os_id_to_number(os_id_t id) * * @return The publish unique id. */ -os_id_t _impl_publish_init(const char_t *pName) +u32_t _impl_publish_init(const char_t *pName) { arguments_t arguments[] = { [0] = {.pch_val = (const char_t *)pName}, @@ -402,24 +319,24 @@ os_id_t _impl_publish_init(const char_t *pName) * @brief Initialize a new subscribe. * * @param pDataAddr The pointer of the data buffer address. - * @param dataSize The data buffer size. + * @param size The data buffer size. * @param pName The subscribe name. * * @return Value The result fo subscribe init operation. */ -os_id_t _impl_subscribe_init(void *pDataAddr, u16_t dataSize, const char_t *pName) +u32_t _impl_subscribe_init(void *pDataAddr, u16_t size, const char_t *pName) { if (!pDataAddr) { return OS_INVALID_ID_VAL; } - if (!dataSize) { + if (!size) { return OS_INVALID_ID_VAL; } arguments_t arguments[] = { [0] = {.pv_val = (void *)pDataAddr}, - [1] = {.u16_val = (u16_t)dataSize}, + [1] = {.u16_val = (u16_t)size}, [2] = {.pch_val = (const char_t *)pName}, }; @@ -429,34 +346,37 @@ os_id_t _impl_subscribe_init(void *pDataAddr, u16_t dataSize, const char_t *pNam /** * @brief The subscribe register the corresponding publish. * - * @param subscribe_id The subscribe unique id. - * @param publish_id The publish unique id. + * @param sub_ctx The subscribe unique id. + * @param pub_ctx The publish unique id. * @param isMute The set of notification operation. * @param pFuncHandler The notification function handler pointer. * * @return Value The result fo subscribe init operation. */ -i32p_t _impl_subscribe_register(os_id_t subscribe_id, os_id_t publish_id, b_t isMute, pSubscribe_callbackFunc_t pNotificationHandler) +i32p_t _impl_subscribe_register(u32_t sub_ctx, u32_t pub_ctx, b_t isMute, pSubscribe_callbackFunc_t pNotificationHandler) { - if (_subscribe_id_isInvalid(subscribe_id)) { - return _PCER; + subscribe_context_t *pCtx_sub = (subscribe_context_t *)sub_ctx; + publish_context_t *pCtx_pub = (publish_context_t *)pub_ctx; + + if (_subscribe_context_isInvalid(pCtx_sub)) { + return PC_EOR; } - if (!_subscribe_id_isInit(subscribe_id)) { - return _PCER; + if (!_subscribe_context_isInit(pCtx_sub)) { + return PC_EOR; } - if (_publish_id_isInvalid(publish_id)) { - return _PCER; + if (_publish_context_isInvalid(pCtx_pub)) { + return PC_EOR; } - if (!_publish_id_isInit(publish_id)) { - return _PCER; + if (!_publish_context_isInit(pCtx_pub)) { + return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)subscribe_id}, - [1] = {.u32_val = (u32_t)publish_id}, + [0] = {.u32_val = (u32_t)sub_ctx}, + [1] = {.u32_val = (u32_t)pub_ctx}, [2] = {.b_val = (b_t)isMute}, [3] = {.ptr_val = (const void *)pNotificationHandler}, }; @@ -467,32 +387,34 @@ i32p_t _impl_subscribe_register(os_id_t subscribe_id, os_id_t publish_id, b_t is /** * @brief The subscriber wait publisher put new data with a timeout option. * - * @param subscribe_id The subscribe unique id. + * @param sub_ctx The subscribe unique id. * @param pDataBuffer The pointer of data buffer. * @param pDataLen The pointer of data buffer len. * * @return Value The result of subscribe init operation. */ -i32p_t _impl_subscribe_data_apply(os_id_t subscribe_id, void *pDataBuffer, u16_t *pDataLen) +i32p_t _impl_subscribe_data_apply(u32_t sub_ctx, void *pDataBuffer, u16_t *pDataLen) { - if (_subscribe_id_isInvalid(subscribe_id)) { - return _PCER; + subscribe_context_t *pCtx_sub = (subscribe_context_t *)sub_ctx; + + if (_subscribe_context_isInvalid(pCtx_sub)) { + return PC_EOR; } - if (!_subscribe_id_isInit(subscribe_id)) { - return _PCER; + if (!_subscribe_context_isInit(pCtx_sub)) { + return PC_EOR; } if (!pDataBuffer) { - return _PCER; + return PC_EOR; } if (!pDataLen) { - return _PCER; + return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)subscribe_id}, + [0] = {.u32_val = (u32_t)sub_ctx}, [1] = {.pv_val = (void *)pDataBuffer}, [2] = {.pv_val = (void *)pDataLen}, }; @@ -503,22 +425,24 @@ i32p_t _impl_subscribe_data_apply(os_id_t subscribe_id, void *pDataBuffer, u16_t /** * @brief To check if the publisher submits new data and that is not applied by subscriber. * - * @param subscribe_id The subscribe unique id. + * @param sub_ctx The subscribe unique id. * * @return Value The result of subscribe data is ready. */ -b_t _impl_subscribe_data_is_ready(os_id_t subscribe_id) +b_t _impl_subscribe_data_is_ready(u32_t sub_ctx) { - if (_subscribe_id_isInvalid(subscribe_id)) { - return _PCER; + subscribe_context_t *pCtx_sub = (subscribe_context_t *)sub_ctx; + + if (_subscribe_context_isInvalid(pCtx_sub)) { + return PC_EOR; } - if (!_subscribe_id_isInit(subscribe_id)) { - return _PCER; + if (!_subscribe_context_isInit(pCtx_sub)) { + return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)subscribe_id}, + [0] = {.u32_val = (u32_t)sub_ctx}, }; return kernel_privilege_invoke((const void *)_subscribe_data_is_ready_privilege_routine, arguments); @@ -527,24 +451,26 @@ b_t _impl_subscribe_data_is_ready(os_id_t subscribe_id) /** * @brief Publisher Submits the report data. * - * @param id The publish unique id. + * @param pub_ctx The publish unique id. * @param pDataAddr The pointer of the data buffer address. * @param dataSize The data buffer size. * * @return Value The result of the publisher data operation. */ -i32p_t _impl_publish_data_submit(os_id_t id, const void *pPublishData, u16_t publishSize) +i32p_t _impl_publish_data_submit(u32_t pub_ctx, const void *pPublishData, u16_t publishSize) { - if (_publish_id_isInvalid(id)) { - return _PCER; + publish_context_t *pCtx_sub = (publish_context_t *)pub_ctx; + + if (_publish_context_isInvalid(pCtx_sub)) { + return PC_EOR; } - if (!_publish_id_isInit(id)) { - return _PCER; + if (!_publish_context_isInit(pCtx_sub)) { + return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)pub_ctx}, [1] = {.ptr_val = (const void *)pPublishData}, [2] = {.u16_val = (u16_t)publishSize}, }; @@ -557,7 +483,7 @@ i32p_t _impl_publish_data_submit(os_id_t id, const void *pPublishData, u16_t pub */ void _impl_publish_pending_handler(void) { - list_t *pListPending = (list_t *)&g_sp_resource.callback_list; + list_t *pListPending = (list_t *)&g_sp_rsc.callback_list; ENTER_CRITICAL_SECTION(); struct subscribe_callback *pCallFuncEntry = (struct subscribe_callback *)list_node_pop(pListPending, LIST_TAIL); @@ -574,7 +500,3 @@ void _impl_publish_pending_handler(void) EXIT_CRITICAL_SECTION(); } } - -#ifdef __cplusplus -} -#endif diff --git a/kernel/thread.c b/kernel/thread.c index 312bc5f..88320a8 100644 --- a/kernel/thread.c +++ b/kernel/thread.c @@ -4,128 +4,17 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "kernel.h" #include "timer.h" #include "postcode.h" #include "trace.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "init.h" /** * Local unique postcode. */ #define PC_EOR PC_IER(PC_OS_CMPT_THREAD_3) -/** - * @brief Get the thread context based on provided unique id. - * - * @param id Thread unique id. - * - * @return The pointer of the current unique id thread context. - */ -static thread_context_t *_thread_object_contextGet(os_id_t id) -{ - return (thread_context_t *)kernel_member_unified_id_toContainerAddress(id); -} - -/** - * @brief Get the current runtime thread id. - * - * @return The id of current running thread. - */ -static os_id_t _thread_id_runtime_get(void) -{ - return (os_id_t)kernel_thread_runIdGet(); -} - -/** - * @brief Get the current runtime thread object context. - * - * @return The pointer of current running thread. - */ -static thread_context_t *_thread_object_runtime_get(void) -{ - return _thread_object_contextGet(kernel_thread_runIdGet()); -} - -/** - * @brief Get the waiting thread list head. - * - * @return The value of the waiting list head. - */ -static list_t *_thread_list_waitingHeadGet(void) -{ - return (list_t *)kernel_member_list_get(KERNEL_MEMBER_THREAD, KERNEL_MEMBER_LIST_THREAD_WAIT); -} - -/** - * @brief Get the pending thread list head. - * - * @return The value of the pending list head. - */ -static list_t *_thread_list_pendingHeadGet(void) -{ - return (list_t *)kernel_list_pendingHeadGet(); -} - -/** - * @brief Push one thread into uninitialized status. - * - * @param pCurHead The pointer of the thread linker head. - */ -static void _thread_list_transfer_toUninitialized(linker_head_t *pCurHead) -{ - ENTER_CRITICAL_SECTION(); - - linker_list_transaction_common(&pCurHead->linker, NULL, LIST_TAIL); - - EXIT_CRITICAL_SECTION(); -} - -/** - * @brief Push one thread context into pending list. - * - * @param pCurHead The pointer of the thread linker head. - */ -static void _thread_list_transfer_toPend(linker_head_t *pCurHead) -{ - ENTER_CRITICAL_SECTION(); - - kernel_thread_list_transfer_toPend(pCurHead); - - EXIT_CRITICAL_SECTION(); -} - -/** - * @brief Pick up a highest priority thread from the pending list. - * - * @return The pointer of the pending thread head. - */ -static linker_head_t *_thread_linker_head_fromPending(void) -{ - ENTER_CRITICAL_SECTION(); - - list_t *pListPending = (list_t *)_thread_list_pendingHeadGet(); - - EXIT_CRITICAL_SECTION(); - return (linker_head_t *)(pListPending->pHead); -} - -/** - * @brief Pick up the next priority thread from the pending list. - * - * @return The pointer of the next pending thread head. - */ -static linker_head_t *_thread_linker_Head_next_fromPending(void) -{ - linker_head_t *pHead = _thread_linker_head_fromPending(); - - return (linker_head_t *)((pHead) ? (pHead->linker.node.pNext) : (NULL)); -} - /** * @brief Check if the thread unique id if is's invalid. * @@ -133,9 +22,13 @@ static linker_head_t *_thread_linker_Head_next_fromPending(void) * * @return The true indicates the id is invalid, otherwise it valid. */ -static b_t _thread_id_isInvalid(u32_t id) +static b_t _thread_context_isInvalid(thread_context_t *pCurThread) { - return kernel_member_unified_id_isInvalid(KERNEL_MEMBER_THREAD, id); + u32_t start, end; + INIT_SECTION_FIRST(INIT_SECTION_OS_THREAD_LIST, start); + INIT_SECTION_LAST(INIT_SECTION_OS_THREAD_LIST, end); + + return ((u32_t)pCurThread < start || (u32_t)pCurThread >= end) ? true : false; } /** @@ -145,17 +38,9 @@ static b_t _thread_id_isInvalid(u32_t id) * * @return The true is initialized, otherwise is uninitialized. */ -static b_t _thread_object_isInit(os_id_t id) +static b_t _thread_context_isInit(thread_context_t *pCurThread) { - thread_context_t *pCurThread = _thread_object_contextGet(id); - - return ((pCurThread) ? (((pCurThread->head.linker.pList) ? (TRUE) : (FALSE))) : FALSE); -} - -static void _thread_callback_fromTimeOut(void *pLinker) -{ - thread_context_t* pCurThread = (thread_context_t*)CONTAINEROF(pLinker, thread_context_t, expire); - kernel_thread_entry_trigger(pCurThread, PC_OS_WAIT_TIMEOUT, NULL); + return ((pCurThread) ? (((pCurThread->head.cs) ? (true) : (false))) : false); } /** @@ -165,53 +50,46 @@ static void _thread_callback_fromTimeOut(void *pLinker) * * @return The result of privilege routine. */ -static os_id_t _thread_init_privilege_routine(arguments_t *pArgs) +static u32_t _thread_init_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); pThread_entryFunc_t pEntryFun = (pThread_entryFunc_t)pArgs[0].ptr_val; u32_t *pAddress = (u32_t *)pArgs[1].u32_val; u32_t size = (u32_t)pArgs[2].u32_val; - u8_t priority = (u8_t)pArgs[3].u8_val; + i16_t priority = (i16_t)pArgs[3].u16_val; const char_t *pName = (const char_t *)pArgs[4].pch_val; - u32_t internal = 0u; - u32_t endAddr = 0u; - thread_context_t *pCurThread = NULL; - internal = sizeof(thread_context_t) * KERNEL_APPLICATION_THREAD_INSTANCE; - pCurThread = (thread_context_t *)(kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_THREAD) + internal); - endAddr = (u32_t)kernel_member_id_toContainerEndAddress(KERNEL_MEMBER_THREAD); - do { - os_id_t id = kernel_member_containerAddress_toUnifiedid((u32_t)pCurThread); - if (_thread_id_isInvalid(id)) { + INIT_SECTION_FOREACH(INIT_SECTION_OS_THREAD_LIST, thread_context_t, pCurThread) + { + if (_thread_context_isInvalid(pCurThread)) { break; } - if (_thread_object_isInit(id)) { + if (_thread_context_isInit(pCurThread)) { continue; } os_memset((char_t *)pCurThread, 0x0u, sizeof(thread_context_t)); - pCurThread->head.id = id; + pCurThread->head.cs = CS_INITED; pCurThread->head.pName = pName; - pCurThread->priority.level = priority; pCurThread->pEntryFunc = pEntryFun; pCurThread->pStackAddr = pAddress; pCurThread->stackSize = size; - pCurThread->psp = (u32_t)kernel_stack_frame_init(pEntryFun, pCurThread->pStackAddr, pCurThread->stackSize); - timeout_init(&pCurThread->expire, _thread_callback_fromTimeOut); - _thread_list_transfer_toPend((linker_head_t *)&pCurThread->head); + pCurThread->task.prior = priority; + pCurThread->task.psp = (u32_t)kernel_stack_frame_init(pEntryFun, pAddress, size); + timeout_init(&pCurThread->task.expire, schedule_callback_fromTimeOut); + schedule_setPend(&pCurThread->task); EXIT_CRITICAL_SECTION(); - return id; - } while ((u32_t)++pCurThread < endAddr); + return (u32_t)pCurThread; + } EXIT_CRITICAL_SECTION(); - return OS_INVALID_ID_VAL; + return 0u; } - /** * @brief It's sub-routine running at privilege mode. * @@ -222,17 +100,14 @@ static os_id_t _thread_init_privilege_routine(arguments_t *pArgs) static i32p_t _thread_resume_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; - thread_context_t *pCurThread = NULL; + thread_context_t *pCurThread = (thread_context_t *)pArgs[0].u32_val; i32p_t postcode = 0; - if (_thread_id_runtime_get() == id) { + if (pCurThread == kernel_thread_runContextGet()) { EXIT_CRITICAL_SECTION(); return postcode; } - - pCurThread = _thread_object_contextGet(id); - postcode = kernel_thread_entry_trigger(pCurThread, 0, NULL); + postcode = schedule_entry_trigger(&pCurThread->task, NULL, 0u); EXIT_CRITICAL_SECTION(); return postcode; @@ -248,16 +123,14 @@ static i32p_t _thread_resume_privilege_routine(arguments_t *pArgs) static i32p_t _thread_suspend_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; - thread_context_t *pCurThread = NULL; + thread_context_t *pCurThread = (thread_context_t *)pArgs[0].u32_val; i32p_t postcode = PC_EOR; - pCurThread = _thread_object_contextGet(id); - if (!_thread_linker_Head_next_fromPending()) { + if (!schedule_hasTwoPendingItem()) { EXIT_CRITICAL_SECTION(); return postcode; } - postcode = kernel_thread_exit_trigger(pCurThread, NULL, _thread_list_waitingHeadGet(), OS_TIME_FOREVER_VAL); + postcode = schedule_exit_trigger(&pCurThread->task, NULL, NULL, schedule_waitList(), OS_TIME_FOREVER_VAL, false); EXIT_CRITICAL_SECTION(); return postcode; @@ -277,12 +150,12 @@ static i32p_t _thread_yield_privilege_routine(arguments_t *pArgs) thread_context_t *pCurThread = NULL; i32p_t postcode = 0; - pCurThread = (thread_context_t *)_thread_object_runtime_get(); - if (!_thread_linker_Head_next_fromPending()) { + pCurThread = (thread_context_t *)kernel_thread_runContextGet(); + if (!schedule_hasTwoPendingItem()) { EXIT_CRITICAL_SECTION(); return postcode; } - postcode = kernel_thread_exit_trigger(pCurThread, NULL, _thread_list_waitingHeadGet(), OS_TIME_FOREVER_VAL); + postcode = schedule_exit_trigger(&pCurThread->task, NULL, NULL, schedule_waitList(), OS_TIME_FOREVER_VAL, false); EXIT_CRITICAL_SECTION(); return postcode; @@ -298,26 +171,19 @@ static i32p_t _thread_yield_privilege_routine(arguments_t *pArgs) static i32p_t _thread_delete_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; - thread_context_t *pCurThread = NULL; + thread_context_t *pCurThread = (thread_context_t *)pArgs[0].u32_val; i32p_t postcode = PC_EOR; - pCurThread = _thread_object_contextGet(id); - if (id == _thread_id_runtime_get()) { + if (pCurThread == kernel_thread_runContextGet()) { EXIT_CRITICAL_SECTION(); return postcode; } - if (!_thread_linker_Head_next_fromPending()) { + if (!schedule_hasTwoPendingItem()) { EXIT_CRITICAL_SECTION(); return postcode; } - - _thread_list_transfer_toUninitialized((linker_head_t *)&pCurThread->head); - os_memset((char_t *)pCurThread->pStackAddr, STACT_UNUSED_DATA, pCurThread->stackSize); - os_memset((char_t *)pCurThread, 0x0u, sizeof(thread_context_t)); - postcode = kernel_thread_schedule_request(); - + postcode = schedule_exit_trigger(&pCurThread->task, NULL, NULL, NULL, OS_TIME_FOREVER_VAL, true); EXIT_CRITICAL_SECTION(); return postcode; } @@ -337,46 +203,42 @@ static i32p_t _thread_sleep_privilege_routine(arguments_t *pArgs) i32p_t postcode = PC_EOR; pCurThread = kernel_thread_runContextGet(); - postcode = kernel_thread_exit_trigger(pCurThread, NULL, _thread_list_waitingHeadGet(), timeout_ms); + postcode = schedule_exit_trigger(&pCurThread->task, NULL, NULL, schedule_waitList(), timeout_ms, true); EXIT_CRITICAL_SECTION(); return postcode; } -/** - * @brief Convert the internal os id to kernel member number. - * - * @return The Member number. - */ -u32_t _impl_thread_os_id_to_number(os_id_t id) -{ - if (_thread_id_isInvalid(id)) { - return 0u; - } - - return (u32_t)((id - kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_THREAD)) / sizeof(thread_context_t)); -} - /** * @brief Get the thread name based on provided unique id. * - * @param id Thread unique id. + * @param ctx Thread unique id. * * @return The name of current thread. */ -const char_t *_impl_thread_name_get(os_id_t id) +const char_t *_impl_thread_name_get(u32_t ctx) { - if (_thread_id_isInvalid(id)) { + thread_context_t *pCtx = (thread_context_t *)ctx; + if (_thread_context_isInvalid(pCtx)) { return NULL; } - if (!_thread_object_isInit(id)) { + if (!_thread_context_isInit(pCtx)) { return NULL; } - thread_context_t *pCurThread = _thread_object_contextGet(id); + return (const char_t *)((pCtx) ? (pCtx->head.pName) : (NULL)); +} - return (const char_t *)((pCurThread) ? (pCurThread->head.pName) : (NULL)); +void _impl_thread_static_init(thread_context_t *pCurThread) +{ + ENTER_CRITICAL_SECTION(); + + pCurThread->task.psp = (u32_t)kernel_stack_frame_init(pCurThread->pEntryFunc, pCurThread->pStackAddr, pCurThread->stackSize); + timeout_init(&pCurThread->task.expire, schedule_callback_fromTimeOut); + schedule_setPend(&pCurThread->task); + + EXIT_CRITICAL_SECTION(); } /** @@ -390,7 +252,7 @@ const char_t *_impl_thread_name_get(os_id_t id) * * @return The value of thread unique id. */ -os_id_t _impl_thread_init(pThread_entryFunc_t pEntryFun, u32_t *pAddress, u32_t size, u16_t priority, const char_t *pName) +u32_t _impl_thread_init(pThread_entryFunc_t pEntryFun, u32_t *pAddress, u32_t size, i16_t priority, const char_t *pName) { if (!pEntryFun) { return OS_INVALID_ID_VAL; @@ -404,7 +266,7 @@ os_id_t _impl_thread_init(pThread_entryFunc_t pEntryFun, u32_t *pAddress, u32_t return OS_INVALID_ID_VAL; } - if (priority > 0xFFu) { + if (priority > 0xFF) { return OS_INVALID_ID_VAL; } @@ -414,7 +276,7 @@ os_id_t _impl_thread_init(pThread_entryFunc_t pEntryFun, u32_t *pAddress, u32_t arguments_t arguments[] = { [0] = {.ptr_val = (void *)pEntryFun}, [1] = {.u32_val = (u32_t)pAddress}, [2] = {.u32_val = (u32_t)size}, - [3] = {.u8_val = (u8_t)priority}, [4] = {.pch_val = (const void *)pName}, + [3] = {.u16_val = (i16_t)priority}, [4] = {.pch_val = (const void *)pName}, }; return kernel_privilege_invoke((const void *)_thread_init_privilege_routine, arguments); @@ -427,18 +289,19 @@ os_id_t _impl_thread_init(pThread_entryFunc_t pEntryFun, u32_t *pAddress, u32_t * * @return The result of thread resume operation. */ -i32p_t _impl_thread_resume(os_id_t id) +i32p_t _impl_thread_resume(u32_t ctx) { - if (_thread_id_isInvalid(id)) { + thread_context_t *pCtx = (thread_context_t *)ctx; + if (_thread_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_thread_object_isInit(id)) { + if (!_thread_context_isInit(pCtx)) { return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, }; return kernel_privilege_invoke((const void *)_thread_resume_privilege_routine, arguments); @@ -451,18 +314,19 @@ i32p_t _impl_thread_resume(os_id_t id) * * @return The result of thread suspend operation. */ -i32p_t _impl_thread_suspend(os_id_t id) +i32p_t _impl_thread_suspend(u32_t ctx) { - if (_thread_id_isInvalid(id)) { + thread_context_t *pCtx = (thread_context_t *)ctx; + if (_thread_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_thread_object_isInit(id)) { + if (!_thread_context_isInit(pCtx)) { return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, }; return kernel_privilege_invoke((const void *)_thread_suspend_privilege_routine, arguments); @@ -491,18 +355,19 @@ i32p_t _impl_thread_yield(void) * * @return The result of thread delete operation. */ -i32p_t _impl_thread_delete(os_id_t id) +i32p_t _impl_thread_delete(u32_t ctx) { - if (_thread_id_isInvalid(id)) { + thread_context_t *pCtx = (thread_context_t *)ctx; + if (_thread_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_thread_object_isInit(id)) { + if (!_thread_context_isInit(pCtx)) { return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, }; return kernel_privilege_invoke((const void *)_thread_delete_privilege_routine, arguments); @@ -531,81 +396,3 @@ i32p_t _impl_thread_sleep(u32_t timeout_ms) return kernel_privilege_invoke((const void *)_thread_sleep_privilege_routine, arguments); } - -/** - * @brief Get thread snapshot informations. - * - * @param instance The thread instance number. - * @param pMsgs The kernel snapshot information pointer. - * - * @return TRUE: Operation pass, FALSE: Operation failed. - */ -b_t thread_snapshot(u32_t instance, kernel_snapshot_t *pMsgs) -{ -#if defined KTRACE - thread_context_t *pCurThread = NULL; - u32_t offset = 0u; - os_id_t id = OS_INVALID_ID_VAL; - u32_t sv_ms = 0u; - - ENTER_CRITICAL_SECTION(); - - offset = sizeof(thread_context_t) * instance; - pCurThread = (thread_context_t *)(kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_THREAD) + offset); - id = kernel_member_containerAddress_toUnifiedid((u32_t)pCurThread); - os_memset((u8_t *)pMsgs, 0x0u, sizeof(kernel_snapshot_t)); - - if (_thread_id_isInvalid(id)) { - EXIT_CRITICAL_SECTION(); - return FALSE; - } - - sv_ms = kernel_schedule_time_get(); - if (pCurThread->head.linker.pList == _thread_list_waitingHeadGet()) { - pMsgs->pState = "wait"; - pMsgs->thread.delay = sv_ms - pCurThread->schedule.analyze.exit_ms; - } else if (pCurThread->head.linker.pList == _thread_list_pendingHeadGet()) { - if (id == _thread_id_runtime_get()) { - pMsgs->pState = "run"; - pMsgs->thread.delay = sv_ms - pCurThread->schedule.analyze.run_ms; - } else { - pMsgs->pState = "pend"; - pMsgs->thread.delay = sv_ms - pCurThread->schedule.analyze.pend_ms; - } - } else if (pCurThread->head.linker.pList) { - pMsgs->pState = "wait"; - pMsgs->thread.delay = sv_ms - pCurThread->schedule.analyze.exit_ms; - } else { - pMsgs->pState = "unused"; - - EXIT_CRITICAL_SECTION(); - return FALSE; - } - - pMsgs->id = pCurThread->head.id; - pMsgs->pName = pCurThread->head.pName; - - pMsgs->thread.priority = pCurThread->priority.level; - pMsgs->thread.current_psp = (u32_t)pCurThread->psp; - - u32_t *pData_u32 = (u32_t *)pCurThread->pStackAddr; - u32_t unused = 0u; - - while ((*pData_u32 == STACT_UNUSED_FRAME_MARK) && (unused < pCurThread->stackSize)) { - pData_u32++; - unused++; - } - unused *= sizeof(u32_t); - pMsgs->thread.ram = ((pCurThread->stackSize - unused) * 100u) / pCurThread->stackSize; - pMsgs->thread.cpu = kernel_thread_use_percent_take(pCurThread->head.id); - - EXIT_CRITICAL_SECTION(); - return TRUE; -#else - return FALSE; -#endif -} - -#ifdef __cplusplus -} -#endif diff --git a/kernel/timer.c b/kernel/timer.c index 8c009aa..1173740 100644 --- a/kernel/timer.c +++ b/kernel/timer.c @@ -4,16 +4,12 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "kernel.h" #include "clock_tick.h" #include "timer.h" #include "postcode.h" #include "trace.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "init.h" /** * Local unique postcode. @@ -42,19 +38,7 @@ typedef struct { /** * Local timer resource */ -_timer_resource_t g_timer_resource = {0u}; - -/** - * @brief Get the timer context based on provided unique id. - * - * @param id The timer unique id. - * - * @return The pointer of the current unique id timer context. - */ -static timer_context_t *_timer_context_get(os_id_t id) -{ - return (timer_context_t *)(kernel_member_unified_id_toContainerAddress(id)); -} +_timer_resource_t g_timer_rsc = {0u}; /** * @brief Check if the timer unique id if is's invalid. @@ -63,17 +47,13 @@ static timer_context_t *_timer_context_get(os_id_t id) * * @return The true is invalid, otherwise is valid. */ -static b_t _timer_id_isInvalid(u32_t id) +static b_t _timer_context_isInvalid(timer_context_t *pCurTimer) { - if (!kernel_member_unified_id_isInvalid(KERNEL_MEMBER_TIMER_INTERNAL, id)) { - return FALSE; - } - - if (!kernel_member_unified_id_isInvalid(KERNEL_MEMBER_TIMER, id)) { - return FALSE; - } + u32_t start, end; + INIT_SECTION_FIRST(INIT_SECTION_OS_TIMER_LIST, start); + INIT_SECTION_LAST(INIT_SECTION_OS_TIMER_LIST, end); - return TRUE; + return ((u32_t)pCurTimer < start || (u32_t)pCurTimer >= end) ? true : false; } /** @@ -83,11 +63,9 @@ static b_t _timer_id_isInvalid(u32_t id) * * @return The true is initialized, otherwise is uninitialized. */ -static b_t _timer_id_isInit(u32_t id) +static b_t _timer_context_isInit(timer_context_t *pCurTimer) { - timer_context_t *pCurTimer = _timer_context_get(id); - - return ((pCurTimer) ? (((pCurTimer->head.cs) ? (TRUE) : (FALSE))) : FALSE); + return (pCurTimer) ? (((pCurTimer->head.cs) ? (true) : (false))) : false; } /** @@ -99,7 +77,7 @@ static b_t _timer_id_isInit(u32_t id) * @return The false value indicates it is right position and it can kiil the loop routine, * otherwise is not a correct position. */ -static b_t _timeout_node_Order_compare_condition(list_node_t *pCurNode, list_node_t *pExtractNode) +static b_t _timeout_node_order_compare_condition(list_node_t *pCurNode, list_node_t *pExtractNode) { struct expired_time *pCurTime = (struct expired_time *)pCurNode; struct expired_time *pExtractTime = (struct expired_time *)pExtractNode; @@ -128,8 +106,8 @@ static void _timeout_transfer_toWaitList(linker_t *pLinker) { ENTER_CRITICAL_SECTION(); - list_t *pToTimeoutList = (list_t *)&g_timer_resource.tt_wait_list; - linker_list_transaction_specific(pLinker, pToTimeoutList, _timeout_node_Order_compare_condition); + list_t *pToTimeoutList = (list_t *)&g_timer_rsc.tt_wait_list; + linker_list_transaction_specific(pLinker, pToTimeoutList, _timeout_node_order_compare_condition); EXIT_CRITICAL_SECTION(); } @@ -144,7 +122,7 @@ static void _timeout_transfer_toPendList(linker_t *pLinker) { ENTER_CRITICAL_SECTION(); - list_t *pToTimeoutList = (list_t *)&g_timer_resource.tt_pend_list; + list_t *pToTimeoutList = (list_t *)&g_timer_rsc.tt_pend_list; linker_list_transaction_common(pLinker, pToTimeoutList, LIST_TAIL); EXIT_CRITICAL_SECTION(); @@ -160,7 +138,7 @@ static void _timeout_transfer_toIdleList(linker_t *pLinker) { ENTER_CRITICAL_SECTION(); - list_t *pToTimeoutList = (list_t *)&g_timer_resource.tt_idle_list; + list_t *pToTimeoutList = (list_t *)&g_timer_rsc.tt_idle_list; linker_list_transaction_common(pLinker, pToTimeoutList, LIST_TAIL); EXIT_CRITICAL_SECTION(); @@ -208,7 +186,7 @@ static void _timeout_schedule(void) { ENTER_CRITICAL_SECTION(); - struct expired_time *pCurExpired = (struct expired_time *)g_timer_resource.tt_wait_list.pHead; + struct expired_time *pCurExpired = (struct expired_time *)g_timer_rsc.tt_wait_list.pHead; if (pCurExpired) { clock_time_interval_set(pCurExpired->duration_us); } else { @@ -218,14 +196,14 @@ static void _timeout_schedule(void) EXIT_CRITICAL_SECTION(); } -static void _timer_callback_fromTimeOut(void *pLinker) +void timer_callback_fromTimeOut(void *pNode) { - timer_context_t* pCurTimer = (timer_context_t*)CONTAINEROF(pLinker, timer_context_t, expire); + timer_context_t *pCurTimer = (timer_context_t *)CONTAINEROF(pNode, timer_context_t, expire); struct expired_time *pExpired = (struct expired_time *)&pCurTimer->expire; if (pCurTimer->control == TIMER_CTRL_CYCLE_VAL) { u64_t timeout_us = pCurTimer->timeout_ms * 1000u; - u64_t elapsed_us = g_timer_resource.system_us - pExpired->duration_us; + u64_t elapsed_us = g_timer_rsc.system_us - pExpired->duration_us; while (elapsed_us >= timeout_us) { elapsed_us -= timeout_us; @@ -239,9 +217,9 @@ static void _timer_callback_fromTimeOut(void *pLinker) os_memset((u8_t *)pCurTimer, 0u, sizeof(timer_context_t)); } - list_t *pCallback_list = (list_t *)&g_timer_resource.callback_list; + list_t *pCallback_list = (list_t *)&g_timer_rsc.callback_list; if (!list_node_isExisted(pCallback_list, &pCurTimer->call.node)) { - list_node_push((list_t *)&g_timer_resource.callback_list, &pCurTimer->call.node, LIST_HEAD); + list_node_push((list_t *)&g_timer_rsc.callback_list, &pCurTimer->call.node, LIST_HEAD); } } @@ -285,18 +263,14 @@ static u32_t _timer_init_privilege_routine(arguments_t *pArgs) pTimer_callbackFunc_t pCallFun = (pTimer_callbackFunc_t)(pArgs[0].ptr_val); const char_t *pName = (const char_t *)pArgs[1].pch_val; - u32_t endAddr = 0u; - timer_context_t *pCurTimer = NULL; - - pCurTimer = (timer_context_t *)kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_TIMER); - endAddr = (u32_t)kernel_member_id_toContainerEndAddress(KERNEL_MEMBER_TIMER); - do { - os_id_t id = kernel_member_containerAddress_toUnifiedid((u32_t)pCurTimer); - if (_timer_id_isInvalid(id)) { + + INIT_SECTION_FOREACH(INIT_SECTION_OS_TIMER_LIST, timer_context_t, pCurTimer) + { + if (_timer_context_isInvalid(pCurTimer)) { break; } - if (_timer_id_isInit(id)) { + if (_timer_context_isInit(pCurTimer)) { continue; } @@ -304,15 +278,14 @@ static u32_t _timer_init_privilege_routine(arguments_t *pArgs) pCurTimer->head.cs = CS_INITED; pCurTimer->head.pName = pName; pCurTimer->call.pTimerCallEntry = pCallFun; - timeout_init(&pCurTimer->expire, _timer_callback_fromTimeOut); + timeout_init(&pCurTimer->expire, timer_callback_fromTimeOut); EXIT_CRITICAL_SECTION(); - return id; + return (u32_t)pCurTimer; } - while ((u32_t)++pCurTimer < endAddr); EXIT_CRITICAL_SECTION(); - return OS_INVALID_ID_VAL; + return 0u; } /** @@ -328,18 +301,14 @@ static u32_t _timer_automatic_privilege_routine(arguments_t *pArgs) pTimer_callbackFunc_t pCallFun = (pTimer_callbackFunc_t)(pArgs[0].ptr_val); const char_t *pName = (const char_t *)pArgs[1].pch_val; - u32_t endAddr = 0u; - timer_context_t *pCurTimer = NULL; - - pCurTimer = (timer_context_t *)kernel_member_id_toContainerStartAddress(KERNEL_MEMBER_TIMER); - endAddr = (u32_t)kernel_member_id_toContainerEndAddress(KERNEL_MEMBER_TIMER); - do { - os_id_t id = kernel_member_containerAddress_toUnifiedid((u32_t)pCurTimer); - if (_timer_id_isInvalid(id)) { + + INIT_SECTION_FOREACH(INIT_SECTION_OS_TIMER_LIST, timer_context_t, pCurTimer) + { + if (_timer_context_isInvalid(pCurTimer)) { break; } - if (_timer_id_isInit(id)) { + if (_timer_context_isInit(pCurTimer)) { continue; } @@ -348,15 +317,14 @@ static u32_t _timer_automatic_privilege_routine(arguments_t *pArgs) pCurTimer->head.pName = pName; pCurTimer->control = TIMER_CTRL_TEMPORARY_VAL; pCurTimer->call.pTimerCallEntry = pCallFun; - timeout_init(&pCurTimer->expire, _timer_callback_fromTimeOut); + timeout_init(&pCurTimer->expire, timer_callback_fromTimeOut); EXIT_CRITICAL_SECTION(); - return id; + return (u32_t)pCurTimer; } - while ((u32_t)++pCurTimer < endAddr); EXIT_CRITICAL_SECTION(); - return OS_INVALID_ID_VAL; + return 0u; } /** @@ -370,11 +338,10 @@ static u32_t _timer_start_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; + timer_context_t *pCurTimer = (timer_context_t *)pArgs[0].u32_val; u8_t ctrl = (u8_t)pArgs[1].u8_val; u32_t timeout_ms = (u32_t)pArgs[2].u32_val; - timer_context_t *pCurTimer = _timer_context_get(id); pCurTimer->timeout_ms = timeout_ms; pCurTimer->control = ctrl; timeout_set(&pCurTimer->expire, timeout_ms, true); @@ -394,9 +361,7 @@ static u32_t _timer_stop_privilege_routine(arguments_t *pArgs) { ENTER_CRITICAL_SECTION(); - os_id_t id = (os_id_t)pArgs[0].u32_val; - - timer_context_t *pCurTimer = _timer_context_get(id); + timer_context_t *pCurTimer = (timer_context_t *)pArgs[0].u32_val; timeout_remove(&pCurTimer->expire, true); EXIT_CRITICAL_SECTION(); @@ -416,9 +381,9 @@ static u32_t _timer_total_system_ms_get_privilege_routine(arguments_t *pArgs) UNUSED_MSG(pArgs); - u64_t us = (!g_timer_resource.remaining_us) ? (clock_time_elapsed_get()) : (0u); + u64_t us = (!g_timer_rsc.remaining_us) ? (clock_time_elapsed_get()) : (0u); - us += g_timer_resource.system_us; + us += g_timer_rsc.system_us; u32_t ms = ((us / 1000u) & 0xFFFFFFFFu); @@ -439,34 +404,14 @@ static u32_t _timer_total_system_us_get_privilege_routine(arguments_t *pArgs) UNUSED_MSG(pArgs); - u32_t us = (u32_t)((!g_timer_resource.remaining_us) ? (clock_time_elapsed_get()) : (0u)); + u32_t us = (u32_t)((!g_timer_rsc.remaining_us) ? (clock_time_elapsed_get()) : (0u)); - us += g_timer_resource.system_us; + us += g_timer_rsc.system_us; EXIT_CRITICAL_SECTION(); return us; } -/** - * @brief Convert the internal os id to kernel member number. - * - * @param id The provided unique id. - * - * @return The value of member number. - */ -u32_t _impl_timer_os_id_to_number(u32_t id) -{ - if (!kernel_member_unified_id_isInvalid(KERNEL_MEMBER_TIMER_INTERNAL, id)) { - return (u32_t)((id - kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_TIMER_INTERNAL)) / sizeof(timer_context_t)); - } - - if (!kernel_member_unified_id_isInvalid(KERNEL_MEMBER_TIMER, id)) { - return (u32_t)((id - kernel_member_id_toUnifiedIdStart(KERNEL_MEMBER_TIMER)) / sizeof(timer_context_t)); - } - - return 0u; -} - /** * @brief Initialize a new timer, or allocate a temporary timer to run. * @@ -475,7 +420,7 @@ u32_t _impl_timer_os_id_to_number(u32_t id) * * @return The value of the timer unique id. */ -os_id_t _impl_timer_init(pTimer_callbackFunc_t pCallFun, const char_t *pName) +u32_t _impl_timer_init(pTimer_callbackFunc_t pCallFun, const char_t *pName) { arguments_t arguments[] = { [0] = {.ptr_val = (const void *)pCallFun}, @@ -493,7 +438,7 @@ os_id_t _impl_timer_init(pTimer_callbackFunc_t pCallFun, const char_t *pName) * * @return The value of the timer unique id. */ -os_id_t _impl_timer_automatic(pTimer_callbackFunc_t pCallFun, const char_t *pName) +u32_t _impl_timer_automatic(pTimer_callbackFunc_t pCallFun, const char_t *pName) { arguments_t arguments[] = { [0] = {.ptr_val = (const void *)pCallFun}, @@ -503,24 +448,24 @@ os_id_t _impl_timer_automatic(pTimer_callbackFunc_t pCallFun, const char_t *pNam return kernel_privilege_invoke((const void *)_timer_automatic_privilege_routine, arguments); } - /** * @brief Timer starts operation, be careful if the timer's last time isn't expired or be handled, * the newer start will override it. * - * @param id The timer unique id. + * @param ctx The timer unique id. * @param control It defines the timer running mode. * @param timeout_ms The timer expired time. * * @return The result of timer start operation. */ -i32p_t _impl_timer_start(os_id_t id, u8_t control, u32_t timeout_ms) +i32p_t _impl_timer_start(u32_t ctx, u8_t control, u32_t timeout_ms) { - if (_timer_id_isInvalid(id)) { + timer_context_t *pCtx = (timer_context_t *)ctx; + if (_timer_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_timer_id_isInit(id)) { + if (!_timer_context_isInit(pCtx)) { return PC_EOR; } @@ -533,7 +478,7 @@ i32p_t _impl_timer_start(os_id_t id, u8_t control, u32_t timeout_ms) } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, [1] = {.u8_val = (u8_t)control}, [2] = {.u32_val = (u32_t)timeout_ms}, }; @@ -544,22 +489,23 @@ i32p_t _impl_timer_start(os_id_t id, u8_t control, u32_t timeout_ms) /** * @brief timer stops operation. * - * @param id The timer unique id. + * @param ctx The timer unique id. * * @return The result of timer stop operation. */ -i32p_t _impl_timer_stop(os_id_t id) +i32p_t _impl_timer_stop(u32_t ctx) { - if (_timer_id_isInvalid(id)) { + timer_context_t *pCtx = (timer_context_t *)ctx; + if (_timer_context_isInvalid(pCtx)) { return PC_EOR; } - if (!_timer_id_isInit(id)) { + if (!_timer_context_isInit(pCtx)) { return PC_EOR; } arguments_t arguments[] = { - [0] = {.u32_val = (u32_t)id}, + [0] = {.u32_val = (u32_t)ctx}, }; return kernel_privilege_invoke((const void *)_timer_stop_privilege_routine, arguments); @@ -568,23 +514,24 @@ i32p_t _impl_timer_stop(os_id_t id) /** * @brief Check the timer to confirm if it's already scheduled in the waiting list. * - * @param id The timer unique id. + * @param ctx The timer unique id. * * @return The true result indicates time busy, otherwise is free status. */ -b_t _impl_timer_busy(os_id_t id) +b_t _impl_timer_busy(u32_t ctx) { - if (_timer_id_isInvalid(id)) { - return FALSE; + timer_context_t *pCtx = (timer_context_t *)ctx; + if (_timer_context_isInvalid(pCtx)) { + return false; } - if (!_timer_id_isInit(id)) { - return FALSE; + if (!_timer_context_isInit(pCtx)) { + return false; } ENTER_CRITICAL_SECTION(); - timer_context_t* pCurTimer = _timer_context_get(id); - b_t isBusy = (pCurTimer->expire.linker.pList == (list_t *)&g_timer_resource.tt_wait_list) ? TRUE : FALSE; + timer_context_t *pCurTimer = (timer_context_t *)ctx; + b_t isBusy = (pCurTimer->expire.linker.pList == (list_t *)&g_timer_rsc.tt_wait_list) ? true : false; EXIT_CRITICAL_SECTION(); return isBusy; @@ -610,18 +557,6 @@ u32_t _impl_timer_total_system_us_get(void) return kernel_privilege_invoke((const void *)_timer_total_system_us_get_privilege_routine, NULL); } -/** - * @brief Check the timer to confirm if it's already scheduled in the waiting list. - * - * @param id The timer unique id. - * - * @return The true result indicates time busy, otherwise is free status. - */ -b_t timer_busy(os_id_t id) -{ - return _impl_timer_busy(id); -} - /** * @brief Get the kernel RTOS system time (ms). * @@ -657,7 +592,7 @@ i32p_t timer_schedule(void) */ void timer_reamining_elapsed_handler(void) { - list_t *pListRunning = (list_t *)&g_timer_resource.callback_list; + list_t *pListRunning = (list_t *)&g_timer_rsc.callback_list; ENTER_CRITICAL_SECTION(); struct timer_callback *pCallFunEntry = (struct timer_callback *)list_node_pop(pListRunning, LIST_TAIL); @@ -685,13 +620,15 @@ void timeout_set(struct expired_time *pExpire, u32_t timeout_ms, b_t immediately { ENTER_CRITICAL_SECTION(); b_t need = false; - if (pExpire->linker.pList == &g_timer_resource.tt_wait_list) { + if (pExpire->linker.pList == &g_timer_rsc.tt_wait_list) { _timeout_remove_fromWaitList((linker_t *)&pExpire->linker); need = true; } - if (timeout_ms == OS_TIME_FOREVER_VAL) { - _timeout_transfer_toIdleList((linker_t *)&pExpire->linker); + if ((timeout_ms == OS_TIME_FOREVER_VAL) || (timeout_ms == 0)) { + if (pExpire->linker.pList != &g_timer_rsc.tt_idle_list) { + _timeout_transfer_toIdleList((linker_t *)&pExpire->linker); + } } else { pExpire->duration_us = timeout_ms * 1000u; _timeout_transfer_toWaitList((linker_t *)&pExpire->linker); @@ -709,7 +646,7 @@ void timeout_remove(struct expired_time *pExpire, b_t immediately) ENTER_CRITICAL_SECTION(); b_t need = false; - if (pExpire->linker.pList == &g_timer_resource.tt_wait_list) { + if (pExpire->linker.pList == &g_timer_rsc.tt_wait_list) { _timeout_remove_fromWaitList((linker_t *)&pExpire->linker); need = true; } @@ -732,41 +669,40 @@ void timeout_handler(u32_t elapsed_us) ENTER_CRITICAL_SECTION(); struct expired_time *pCurExpired = NULL; - g_timer_resource.remaining_us = elapsed_us; + g_timer_rsc.remaining_us = elapsed_us; list_iterator_t it = {0u}; - list_t *pListWaiting = (list_t *)&g_timer_resource.tt_wait_list; + list_t *pListWaiting = (list_t *)&g_timer_rsc.tt_wait_list; list_iterator_init(&it, pListWaiting); while (list_iterator_next_condition(&it, (void *)&pCurExpired)) { - if (g_timer_resource.remaining_us >= pCurExpired->duration_us) { - g_timer_resource.remaining_us -= pCurExpired->duration_us; - g_timer_resource.system_us += pCurExpired->duration_us; + if (g_timer_rsc.remaining_us >= pCurExpired->duration_us) { + g_timer_rsc.remaining_us -= pCurExpired->duration_us; + g_timer_rsc.system_us += pCurExpired->duration_us; pCurExpired->duration_us = 0u; - if (pCurExpired->fn != _timer_callback_fromTimeOut) { - pCurExpired->fn((void *)pCurExpired); + if (pCurExpired->fn != timer_callback_fromTimeOut) { + pCurExpired->fn((void *)&pCurExpired->linker.node); _timeout_transfer_toIdleList((linker_t *)&pCurExpired->linker); } else { - pCurExpired->duration_us = g_timer_resource.system_us; + pCurExpired->duration_us = g_timer_rsc.system_us; _timeout_transfer_toPendList((linker_t *)&pCurExpired->linker); } } else { - pCurExpired->duration_us -= g_timer_resource.remaining_us; + pCurExpired->duration_us -= g_timer_rsc.remaining_us; break; } } + g_timer_rsc.system_us += g_timer_rsc.remaining_us; + g_timer_rsc.remaining_us = 0u; - g_timer_resource.system_us += g_timer_resource.remaining_us; - g_timer_resource.remaining_us = 0u; - - b_t need = FALSE; - list_t *pListPending = (list_t *)&g_timer_resource.tt_pend_list; + b_t need = false; + list_t *pListPending = (list_t *)&g_timer_rsc.tt_pend_list; list_iterator_init(&it, pListPending); while (list_iterator_next_condition(&it, (void *)&pCurExpired)) { if (pCurExpired->fn != NULL) { - pCurExpired->fn((void *)pCurExpired); - need = TRUE; + pCurExpired->fn((void *)&pCurExpired->linker.node); + need = true; } } @@ -777,7 +713,3 @@ void timeout_handler(u32_t elapsed_us) EXIT_CRITICAL_SECTION(); } - -#ifdef __cplusplus -} -#endif diff --git a/kernel/trace.c b/kernel/trace.c index a457005..fae5026 100644 --- a/kernel/trace.c +++ b/kernel/trace.c @@ -4,93 +4,88 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "trace.h" #include "configuration.h" #include "postcode.h" #include "linker.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "init.h" /** * Local trace postcode contrainer */ u32_t g_postcode_os_cmpt_failed_container[PC_OS_COMPONENT_NUMBER] = {0u}; +pTrace_postcodeFunc_t g_postcode_failed_callback_fn = NULL; /** * @brief Take firmare snapshot information. */ -void _impl_trace_firmware_snapshot_print(void) +u32_t _impl_trace_firmware_version_get(void) { -#if defined KTRACE - KTRACE(">> At-RTOS Version: v%d.%d.%d snaphost!!!\n", ATOS_VERSION_MAJOR_NUMBER, ATOS_VERSION_MINOR_NUMBER, ATOS_VERSION_PATCH_NUMBER); - KTRACE(">> %-16s %-40s\n", "Build Time", "Commit ID"); - KTRACE(" %-16s %-40s\n", ATOS_BUILD_TIME, ATOS_COMMIT_HEAD_ID); -#endif + u32_t version = ATOS_VERSION_PATCH_NUMBER_POS; + version |= (ATOS_VERSION_MINOR_NUMBER & ATOS_VERSION_MINOR_NUMBER_MASK) < ATOS_VERSION_MINOR_NUMBER_POS; + version |= (ATOS_VERSION_MAJOR_NUMBER & ATOS_VERSION_MAJOR_NUMBER_MASK) < ATOS_VERSION_MAJOR_NUMBER_POS; + return version; } /** - * @brief Take postcode snapshot information. + * @brief Failed postcode call function register. */ -void _impl_trace_postcode_snapshot_print(void) +void _impl_trace_postcode_callback_register(const pTrace_postcodeFunc_t fn) { - b_t is_failed = FALSE; + g_postcode_failed_callback_fn = fn; +} - for (u32_t i = 0u; i < PC_OS_COMPONENT_NUMBER; i++) { - if (g_postcode_os_cmpt_failed_container[i]) { - is_failed = TRUE; -#if defined KTRACE - KTRACE(">> Postcode CMPT %d indicates it is failed at %d\n", i, - (((g_postcode_os_cmpt_failed_container[i] >> PC_LINE_NUMBER_POS) & PC_LINE_NUMBER_MSK))); -#endif - } +/** + * @brief Failed postcode calls. + */ +void _impl_trace_postcode_set(u32_t cmpt, u32_t code) +{ + g_postcode_os_cmpt_failed_container[cmpt] = code; + if (g_postcode_failed_callback_fn) { + g_postcode_failed_callback_fn(cmpt, ((code >> PC_LINE_NUMBER_POS) & PC_LINE_NUMBER_MSK)); } +} - if (!is_failed) { -#if defined KTRACE - KTRACE(">> Postcode CMPT succeeded\n"); -#endif +/** + * @brief Take postcode snapshot information. + */ +b_t _impl_trace_postcode_failed_get(const pTrace_postcodeFunc_t fn) +{ + b_t failed = false; + for (u32_t i = 0u; i < PC_OS_COMPONENT_NUMBER; i++) { + u32_t code = g_postcode_os_cmpt_failed_container[i]; + if (code) { + failed = true; + if (fn) { + fn(i, ((code >> PC_LINE_NUMBER_POS) & PC_LINE_NUMBER_MSK)); + } + } } + return failed; } /** - * @brief Take kernel snapshot information. + * @brief Take thread snapshot information. */ -void _impl_trace_kernel_snapshot_print(void) +void _impl_trace_thread(const pTrace_threadFunc_t fn) { -#if defined KTRACE - u32_t unused[8] = {0u}; - kernel_snapshot_t snapshot_data; - - _impl_trace_firmware_snapshot_print(); - _impl_trace_postcode_snapshot_print(); - - KTRACE(">> %-6s %-15s %-5s %-7s %-3s %-10s %-7s %-9s %-10s\n", "Thread", "Name", "ID", "STATE", "PRI", "PSP_ADDR", "RAM(1%)", - "CPU(0.1%)", "W/P/R(ms)"); - for (u32_t i = 0u; i < THREAD_INSTANCE_SUPPORTED_NUMBER; i++) { - if (thread_snapshot(i, &snapshot_data)) { - KTRACE(" %-6d %-15s %-5d %-7s %-3d 0x%-8x %-7d %-9d %-10d\n", (i + 1u), snapshot_data.pName, snapshot_data.id, - snapshot_data.pState, snapshot_data.thread.priority, snapshot_data.thread.current_psp, snapshot_data.thread.ram, - snapshot_data.thread.cpu, snapshot_data.thread.delay); - } else { - unused[0]++; + INIT_SECTION_FOREACH(INIT_SECTION_OS_THREAD_LIST, thread_context_t, pCurThread) + { + if (fn) { + fn((const thread_context_t *)pCurThread); } } - - KTRACE(">> %-9s %-6s %-6s\n", "Statistic", "Totals", "Remain"); - KTRACE(" %-9s %-6d %-6d\n", "Thread", THREAD_INSTANCE_SUPPORTED_NUMBER, unused[0]); - KTRACE(" %-9s %-6d %-6d\n", "Semaphore", SEMAPHORE_INSTANCE_SUPPORTED_NUMBER, unused[1]); - KTRACE(" %-9s %-6d %-6d\n", "Mutex", MUTEX_INSTANCE_SUPPORTED_NUMBER, unused[2]); - KTRACE(" %-9s %-6d %-6d\n", "Event", EVENT_INSTANCE_SUPPORTED_NUMBER, unused[3]); - KTRACE(" %-9s %-6d %-6d\n", "Queue", QUEUE_INSTANCE_SUPPORTED_NUMBER, unused[4]); - KTRACE(" %-9s %-6d %-6d\n", "Timer", TIMER_INSTANCE_SUPPORTED_NUMBER, unused[5]); - KTRACE(" %-9s %-6d %-6d\n", "Timer", PUBLISH_INSTANCE_SUPPORTED_NUMBER, unused[6]); - KTRACE(" %-9s %-6d %-6d\n", "Pool", POOL_INSTANCE_SUPPORTED_NUMBER, unused[7]); -#endif } -#ifdef __cplusplus +/** + * @brief Take thread usage snapshot information. + */ +void _impl_trace_analyze(const pTrace_analyzeFunc_t fn) +{ + INIT_SECTION_FOREACH(INIT_SECTION_OS_THREAD_LIST, thread_context_t, pCurThread) + { + if (fn) { + fn(pCurThread->task.exec.analyze); + } + } } -#endif diff --git a/package.json b/package.json index 5ed49d3..2d3c223 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "At-RTOS", "homepage": "https://github.com/At-EC/At-RTOS", - "version": "1.7.6", - "timestamp": "2024-11-09,17:36", - "commit_id": "a60a1a349cce093dc6558368f70570484e2e3e4e" + "version": "1.7.7", + "timestamp": "2025-01-04,14:34", + "commit_id": "1b1d7b46ad853be05c9457cb5678884d09469fa3" } diff --git a/port/port_common.c b/port/port_common.c index 98bb8d6..b8b7e83 100644 --- a/port/port_common.c +++ b/port/port_common.c @@ -4,15 +4,10 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "linker.h" #include "clock_tick.h" #include "port.h" -#ifdef __cplusplus -extern "C" { -#endif - /** * @brief ARM core systick interrupt handle function. */ @@ -35,14 +30,14 @@ void HardFault_Handler(void) b_t port_isInInterruptContent(void) { if (__get_IPSR()) { - return TRUE; + return true; } - if (__get_PRIMASK() == SET_BIT(0)) { - return TRUE; + if (__get_PRIMASK() == B(0)) { + return true; } - return FALSE; + return false; } /** @@ -51,9 +46,9 @@ b_t port_isInInterruptContent(void) b_t port_isInThreadMode(void) { if (__get_IPSR()) { - return FALSE; + return false; } - return TRUE; + return true; } /** @@ -91,7 +86,7 @@ u32_t port_stack_frame_init(void (*pEntryFunction)(void), u32_t *pAddress, u32_t psp_frame = STACK_ADDRESS_DOWN(psp_frame); - ((stack_snapshot_t *)psp_frame)->xPSR = SET_BIT(24); /* xPSR */ + ((stack_snapshot_t *)psp_frame)->xPSR = B(24); /* xPSR */ ((stack_snapshot_t *)psp_frame)->R15_PC = (u32_t)pEntryFunction; /* PC */ ((stack_snapshot_t *)psp_frame)->R14_LR = 0xFFFFFFFDu; /* LR */ @@ -134,6 +129,3 @@ u32_t port_stack_frame_init(void (*pEntryFunction)(void), u32_t *pAddress, u32_t return (u32_t)psp_frame; } -#ifdef __cplusplus -} -#endif diff --git a/port/port_keil_ac5.c b/port/port_keil_ac5.c index 6d61d4a..dda57ed 100644 --- a/port/port_keil_ac5.c +++ b/port/port_keil_ac5.c @@ -4,13 +4,8 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - #include "port.h" -#ifdef __cplusplus -extern "C" { -#endif - #if defined ( __FPU_PRESENT ) #define FPU_ENABLED __FPU_PRESENT #else @@ -141,7 +136,3 @@ __asm void port_run_theFirstThread(u32_t sp) ALIGN 4 } - -#ifdef __cplusplus -} -#endif diff --git a/port/port_keil_ac6.c b/port/port_keil_ac6.c index 64360e2..eddac7a 100644 --- a/port/port_keil_ac6.c +++ b/port/port_keil_ac6.c @@ -4,11 +4,6 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - -#ifdef __cplusplus -extern "C" { -#endif - #include "port.h" #if defined ( __FPU_PRESENT ) @@ -150,6 +145,3 @@ void port_run_theFirstThread(u32_t sp) */ } -#ifdef __cplusplus -} -#endif diff --git a/port/port_native_gcc.c b/port/port_native_gcc.c index 1f039e7..ab58289 100644 --- a/port/port_native_gcc.c +++ b/port/port_native_gcc.c @@ -4,14 +4,9 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. **/ - -#include "typedef.h" +#include "type_def.h" #include "arch.h" -#ifdef __cplusplus -extern "C" { -#endif - /** * @brief ARM core trigger the svc call interrupt. */ @@ -108,7 +103,3 @@ u32_t port_stack_frame_init(void (*pEntryFunction)(void), u32_t *pAddress, u32_t { /* TODO */ } - -#ifdef __cplusplus -} -#endif