From d8ad231b9068c91915f58795a3097edb1b362529 Mon Sep 17 00:00:00 2001 From: zhangzheng <1358745329@qq.com> Date: Mon, 20 Nov 2023 23:37:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BArtt=E9=A9=B1=E5=8A=A8=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E6=9B=B4=E5=A4=9A=E7=9A=84=E5=80=9F=E5=8F=A3=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +- mkrtos_script/build.sh | 8 +- mkrtos_user/lib/util/inc/u_thread_util.h | 2 + mkrtos_user/lib/util/src/u_thread_util.c | 6 +- .../server/drv/rtthread_drv/CMakeLists.txt | 2 + .../server/drv/rtthread_drv/inc/rtdef.h | 1744 ++++++++--------- .../drv/rtthread_drv/inc/rtthread_inter.h | 4 + .../server/drv/rtthread_drv/src/main.c | 4 +- .../server/drv/rtthread_drv/src/object.c | 635 ++++++ .../drv/rtthread_drv/src/rtthread_inter.c | 281 ++- 10 files changed, 1713 insertions(+), 976 deletions(-) create mode 100644 mkrtos_user/server/drv/rtthread_drv/inc/rtthread_inter.h create mode 100644 mkrtos_user/server/drv/rtthread_drv/src/object.c diff --git a/.vscode/settings.json b/.vscode/settings.json index ca3232c5c..afd1b0303 100755 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -247,7 +247,8 @@ "limits.h": "c", "ctime": "c", "pthread_impl.h": "c", - "time.h": "c" + "time.h": "c", + "u_thread_util.h": "c" }, "cortex-debug.showRTOS": false, "cortex-debug.variableUseNaturalFormat": false, diff --git a/mkrtos_script/build.sh b/mkrtos_script/build.sh index b9d98b338..2ac29a08e 100755 --- a/mkrtos_script/build.sh +++ b/mkrtos_script/build.sh @@ -1,9 +1,9 @@ #!/bin/bash -export TOOLCHAIN=/home/zhangzheng/gcc-arm-none-eabi-5_4-2016q3/bin/ -export TOOLCHAIN_LIB=/home/zhangzheng/gcc-arm-none-eabi-5_4-2016q3/lib/gcc/arm-none-eabi/5.4.1/armv7-m -# export TOOLCHAIN=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/bin/ -# export TOOLCHAIN_LIB=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/lib/gcc/arm-none-eabi/10.3.1/thumb/v7-m/nofp +# export TOOLCHAIN=/home/zhangzheng/gcc-arm-none-eabi-5_4-2016q3/bin/ +# export TOOLCHAIN_LIB=/home/zhangzheng/gcc-arm-none-eabi-5_4-2016q3/lib/gcc/arm-none-eabi/5.4.1/armv7-m +export TOOLCHAIN=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/bin/ +export TOOLCHAIN_LIB=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/lib/gcc/arm-none-eabi/10.3.1/thumb/v7-m/nofp export KEN_OFFSET=0x2000 export INIT_OFFSET=0x10000 export BOOTFS_ADDR_OFFSET=0x20000 diff --git a/mkrtos_user/lib/util/inc/u_thread_util.h b/mkrtos_user/lib/util/inc/u_thread_util.h index 121f686d1..92dbbbfbd 100644 --- a/mkrtos_user/lib/util/inc/u_thread_util.h +++ b/mkrtos_user/lib/util/inc/u_thread_util.h @@ -1,3 +1,5 @@ #pragma once #include + +void u_thread_del(obj_handler_t th_hd); int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void *msg_buf, void (*thread_func)(void), int prio); diff --git a/mkrtos_user/lib/util/src/u_thread_util.c b/mkrtos_user/lib/util/src/u_thread_util.c index 83f3a3bb9..e3a505c33 100644 --- a/mkrtos_user/lib/util/src/u_thread_util.c +++ b/mkrtos_user/lib/util/src/u_thread_util.c @@ -7,6 +7,10 @@ #include #include #include +void u_thread_del(obj_handler_t th_hd) +{ + task_unmap(TASK_THIS, vpage_create_raw3(KOBJ_DELETE_RIGHT, 0, th_hd)); +} int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void *msg_buf, void (*thread_func)(void), int prio) { assert(th_hd); @@ -31,7 +35,7 @@ int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void handler_free_umap(th1_hd); return msg_tag_get_prot(tag); } - tag = thread_exec_regs(th1_hd, (umword_t)thread_func, (umword_t)stack + stack_size, RAM_BASE(), 0); + tag = thread_exec_regs(th1_hd, (umword_t)thread_func, (umword_t)stack + stack_size - sizeof(void *), RAM_BASE(), 0); if (msg_tag_get_prot(tag) < 0) { handler_free_umap(th1_hd); diff --git a/mkrtos_user/server/drv/rtthread_drv/CMakeLists.txt b/mkrtos_user/server/drv/rtthread_drv/CMakeLists.txt index b6e5e6503..25812979a 100644 --- a/mkrtos_user/server/drv/rtthread_drv/CMakeLists.txt +++ b/mkrtos_user/server/drv/rtthread_drv/CMakeLists.txt @@ -12,6 +12,8 @@ include_directories( ${CMAKE_CURRENT_LIST_DIR}/ ${CMAKE_CURRENT_LIST_DIR}/inc ${CMAKE_CURRENT_LIST_DIR}/components/drivers/include + ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/util/inc + ${CMAKE_SOURCE_DIR}/mkrtos_user/server/drv/rtthread_drv/bsp/stm32/stm32f103-onenet-nbiot/board ${CMAKE_SOURCE_DIR}/mkrtos_user/server/drv/rtthread_drv/bsp/stm32/libraries/STM32F1xx_HAL/CMSIS/Device/ST/STM32F1xx/Include ${CMAKE_SOURCE_DIR}/mkrtos_user/server/drv/rtthread_drv/bsp/stm32/libraries/HAL_Drivers diff --git a/mkrtos_user/server/drv/rtthread_drv/inc/rtdef.h b/mkrtos_user/server/drv/rtthread_drv/inc/rtdef.h index e129b41c5..b1851a859 100644 --- a/mkrtos_user/server/drv/rtthread_drv/inc/rtdef.h +++ b/mkrtos_user/server/drv/rtthread_drv/inc/rtdef.h @@ -63,9 +63,9 @@ #ifdef RT_USING_LIBC #if !defined(RT_USING_LIBC_ISO_ONLY) && !defined(RT_VER_NUM) /* If RT_VER_NUM is not defined, there is no extra libc support. */ -#define RT_USING_LIBC_ISO_ONLY (1) +#define RT_USING_LIBC_ISO_ONLY (1) #else -#define RT_USING_LIBC_ISO_ONLY (0) +#define RT_USING_LIBC_ISO_ONLY (0) #endif /* !defined(RT_USING_LIBC_ISO_ONLY) && !defined(RT_VER_NUM) */ #include #include @@ -86,7 +86,8 @@ #endif /* RT_USING_LIBC */ #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /** @@ -96,279 +97,278 @@ extern "C" { /**@{*/ /* RT-Thread version information */ -#define RT_VERSION_MAJOR 5 /**< Major version number (X.x.x) */ -#define RT_VERSION_MINOR 1 /**< Minor version number (x.X.x) */ -#define RT_VERSION_PATCH 0 /**< Patch version number (x.x.X) */ +#define RT_VERSION_MAJOR 5 /**< Major version number (X.x.x) */ +#define RT_VERSION_MINOR 1 /**< Minor version number (x.X.x) */ +#define RT_VERSION_PATCH 0 /**< Patch version number (x.x.X) */ /* e.g. #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(4, 1, 0) */ -#define RT_VERSION_CHECK(major, minor, revise) ((major * 10000) + (minor * 100) + revise) +#define RT_VERSION_CHECK(major, minor, revise) ((major * 10000) + (minor * 100) + revise) /* RT-Thread version */ -#define RTTHREAD_VERSION RT_VERSION_CHECK(RT_VERSION_MAJOR, RT_VERSION_MINOR, RT_VERSION_PATCH) +#define RTTHREAD_VERSION RT_VERSION_CHECK(RT_VERSION_MAJOR, RT_VERSION_MINOR, RT_VERSION_PATCH) - -/* RT-Thread basic data type definitions */ -typedef int rt_bool_t; /**< boolean type */ -typedef signed long rt_base_t; /**< Nbit CPU related date type */ -typedef unsigned long rt_ubase_t; /**< Nbit unsigned CPU related data type */ + /* RT-Thread basic data type definitions */ + typedef int rt_bool_t; /**< boolean type */ + typedef signed long rt_base_t; /**< Nbit CPU related date type */ + typedef unsigned long rt_ubase_t; /**< Nbit unsigned CPU related data type */ #ifndef RT_USING_ARCH_DATA_TYPE #ifdef RT_USING_LIBC -typedef int8_t rt_int8_t; /**< 8bit integer type */ -typedef int16_t rt_int16_t; /**< 16bit integer type */ -typedef int32_t rt_int32_t; /**< 32bit integer type */ -typedef uint8_t rt_uint8_t; /**< 8bit unsigned integer type */ -typedef uint16_t rt_uint16_t; /**< 16bit unsigned integer type */ -typedef uint32_t rt_uint32_t; /**< 32bit unsigned integer type */ -typedef int64_t rt_int64_t; /**< 64bit integer type */ -typedef uint64_t rt_uint64_t; /**< 64bit unsigned integer type */ + typedef int8_t rt_int8_t; /**< 8bit integer type */ + typedef int16_t rt_int16_t; /**< 16bit integer type */ + typedef int32_t rt_int32_t; /**< 32bit integer type */ + typedef uint8_t rt_uint8_t; /**< 8bit unsigned integer type */ + typedef uint16_t rt_uint16_t; /**< 16bit unsigned integer type */ + typedef uint32_t rt_uint32_t; /**< 32bit unsigned integer type */ + typedef int64_t rt_int64_t; /**< 64bit integer type */ + typedef uint64_t rt_uint64_t; /**< 64bit unsigned integer type */ #else -typedef signed char rt_int8_t; /**< 8bit integer type */ -typedef signed short rt_int16_t; /**< 16bit integer type */ -typedef signed int rt_int32_t; /**< 32bit integer type */ -typedef unsigned char rt_uint8_t; /**< 8bit unsigned integer type */ -typedef unsigned short rt_uint16_t; /**< 16bit unsigned integer type */ -typedef unsigned int rt_uint32_t; /**< 32bit unsigned integer type */ + typedef signed char rt_int8_t; /**< 8bit integer type */ + typedef signed short rt_int16_t; /**< 16bit integer type */ + typedef signed int rt_int32_t; /**< 32bit integer type */ + typedef unsigned char rt_uint8_t; /**< 8bit unsigned integer type */ + typedef unsigned short rt_uint16_t; /**< 16bit unsigned integer type */ + typedef unsigned int rt_uint32_t; /**< 32bit unsigned integer type */ #ifdef ARCH_CPU_64BIT -typedef signed long rt_int64_t; /**< 64bit integer type */ -typedef unsigned long rt_uint64_t; /**< 64bit unsigned integer type */ + typedef signed long rt_int64_t; /**< 64bit integer type */ + typedef unsigned long rt_uint64_t; /**< 64bit unsigned integer type */ #else -typedef signed long long rt_int64_t; /**< 64bit integer type */ -typedef unsigned long long rt_uint64_t; /**< 64bit unsigned integer type */ + typedef signed long long rt_int64_t; /**< 64bit integer type */ + typedef unsigned long long rt_uint64_t; /**< 64bit unsigned integer type */ #endif /* ARCH_CPU_64BIT */ #endif /* RT_USING_LIBC */ #endif /* RT_USING_ARCH_DATA_TYPE */ #if defined(RT_USING_LIBC) && !RT_USING_LIBC_ISO_ONLY -typedef size_t rt_size_t; /**< Type for size number */ -typedef ssize_t rt_ssize_t; /**< Used for a count of bytes or an error indication */ + typedef size_t rt_size_t; /**< Type for size number */ + typedef ssize_t rt_ssize_t; /**< Used for a count of bytes or an error indication */ #else -typedef rt_ubase_t rt_size_t; /**< Type for size number */ -typedef rt_base_t rt_ssize_t; /**< Used for a count of bytes or an error indication */ +typedef rt_ubase_t rt_size_t; /**< Type for size number */ +typedef rt_base_t rt_ssize_t; /**< Used for a count of bytes or an error indication */ #endif /* defined(RT_USING_LIBC) && !RT_USING_LIBC_ISO_ONLY */ -typedef rt_base_t rt_err_t; /**< Type for error number */ -typedef rt_uint32_t rt_time_t; /**< Type for time stamp */ -typedef rt_uint32_t rt_tick_t; /**< Type for tick count */ -typedef rt_base_t rt_flag_t; /**< Type for flags */ -typedef rt_ubase_t rt_dev_t; /**< Type for device */ -typedef rt_base_t rt_off_t; /**< Type for offset */ + typedef rt_base_t rt_err_t; /**< Type for error number */ + typedef rt_uint32_t rt_time_t; /**< Type for time stamp */ + typedef rt_uint32_t rt_tick_t; /**< Type for tick count */ + typedef rt_base_t rt_flag_t; /**< Type for flags */ + typedef rt_ubase_t rt_dev_t; /**< Type for device */ + typedef rt_base_t rt_off_t; /**< Type for offset */ #ifdef __cplusplus typedef rt_base_t rt_atomic_t; #else #if defined(RT_USING_HW_ATOMIC) - typedef rt_base_t rt_atomic_t; +typedef rt_base_t rt_atomic_t; #elif defined(RT_USING_STDC_ATOMIC) - #include - typedef atomic_size_t rt_atomic_t; +#include +typedef atomic_size_t rt_atomic_t; #else - typedef rt_base_t rt_atomic_t; +typedef rt_base_t rt_atomic_t; #endif /* RT_USING_STDC_ATOMIC */ #endif /* __cplusplus */ /* boolean type definitions */ -#define RT_TRUE 1 /**< boolean true */ -#define RT_FALSE 0 /**< boolean fails */ +#define RT_TRUE 1 /**< boolean true */ +#define RT_FALSE 0 /**< boolean fails */ /* null pointer definition */ -#define RT_NULL 0 +#define RT_NULL 0 /**@}*/ /* maximum value of base type */ #ifdef RT_USING_LIBC -#define RT_UINT8_MAX UINT8_MAX /**< Maximum number of UINT8 */ -#define RT_UINT16_MAX UINT16_MAX /**< Maximum number of UINT16 */ -#define RT_UINT32_MAX UINT32_MAX /**< Maximum number of UINT32 */ -#define RT_UINT64_MAX UINT64_MAX /**< Maximum number of UINT64 */ +#define RT_UINT8_MAX UINT8_MAX /**< Maximum number of UINT8 */ +#define RT_UINT16_MAX UINT16_MAX /**< Maximum number of UINT16 */ +#define RT_UINT32_MAX UINT32_MAX /**< Maximum number of UINT32 */ +#define RT_UINT64_MAX UINT64_MAX /**< Maximum number of UINT64 */ #else -#define RT_UINT8_MAX 0xff /**< Maximum number of UINT8 */ -#define RT_UINT16_MAX 0xffff /**< Maximum number of UINT16 */ -#define RT_UINT32_MAX 0xffffffff /**< Maximum number of UINT32 */ -#define RT_UINT64_MAX 0xffffffffffffffff +#define RT_UINT8_MAX 0xff /**< Maximum number of UINT8 */ +#define RT_UINT16_MAX 0xffff /**< Maximum number of UINT16 */ +#define RT_UINT32_MAX 0xffffffff /**< Maximum number of UINT32 */ +#define RT_UINT64_MAX 0xffffffffffffffff #endif /* RT_USING_LIBC */ -#define RT_TICK_MAX RT_UINT32_MAX /**< Maximum number of tick */ +#define RT_TICK_MAX RT_UINT32_MAX /**< Maximum number of tick */ /* maximum value of ipc type */ -#define RT_SEM_VALUE_MAX RT_UINT16_MAX /**< Maximum number of semaphore .value */ -#define RT_MUTEX_VALUE_MAX RT_UINT16_MAX /**< Maximum number of mutex .value */ -#define RT_MUTEX_HOLD_MAX RT_UINT8_MAX /**< Maximum number of mutex .hold */ -#define RT_MB_ENTRY_MAX RT_UINT16_MAX /**< Maximum number of mailbox .entry */ -#define RT_MQ_ENTRY_MAX RT_UINT16_MAX /**< Maximum number of message queue .entry */ +#define RT_SEM_VALUE_MAX RT_UINT16_MAX /**< Maximum number of semaphore .value */ +#define RT_MUTEX_VALUE_MAX RT_UINT16_MAX /**< Maximum number of mutex .value */ +#define RT_MUTEX_HOLD_MAX RT_UINT8_MAX /**< Maximum number of mutex .hold */ +#define RT_MB_ENTRY_MAX RT_UINT16_MAX /**< Maximum number of mailbox .entry */ +#define RT_MQ_ENTRY_MAX RT_UINT16_MAX /**< Maximum number of message queue .entry */ -/* Common Utilities */ + /* Common Utilities */ -#define RT_UNUSED(x) ((void)x) +#define RT_UNUSED(x) ((void)x) /* compile time assertion */ -#define RT_STATIC_ASSERT(name, expn) typedef char _static_assert_##name[(expn)?1:-1] +#define RT_STATIC_ASSERT(name, expn) typedef char _static_assert_##name[(expn) ? 1 : -1] /* Compiler Related Definitions */ -#if defined(__ARMCC_VERSION) /* ARM Compiler */ -#define rt_section(x) __attribute__((section(x))) -#define rt_used __attribute__((used)) -#define rt_align(n) __attribute__((aligned(n))) -#define rt_weak __attribute__((weak)) -#define rt_inline static __inline +#if defined(__ARMCC_VERSION) /* ARM Compiler */ +#define rt_section(x) __attribute__((section(x))) +#define rt_used __attribute__((used)) +#define rt_align(n) __attribute__((aligned(n))) +#define rt_weak __attribute__((weak)) +#define rt_inline static __inline /* module compiling */ #ifdef RT_USING_MODULE -#define RTT_API __declspec(dllimport) +#define RTT_API __declspec(dllimport) #else -#define RTT_API __declspec(dllexport) -#endif /* RT_USING_MODULE */ -#elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */ -#define rt_section(x) @ x -#define rt_used __root -#define PRAGMA(x) _Pragma(#x) -#define rt_align(n) PRAGMA(data_alignment=n) -#define rt_weak __weak -#define rt_inline static inline +#define RTT_API __declspec(dllexport) +#endif /* RT_USING_MODULE */ +#elif defined(__IAR_SYSTEMS_ICC__) /* for IAR Compiler */ +#define rt_section(x) @x +#define rt_used __root +#define PRAGMA(x) _Pragma(#x) +#define rt_align(n) PRAGMA(data_alignment = n) +#define rt_weak __weak +#define rt_inline static inline #define RTT_API -#elif defined (__GNUC__) /* GNU GCC Compiler */ +#elif defined(__GNUC__) /* GNU GCC Compiler */ #ifndef RT_USING_LIBC /* the version of GNU GCC must be greater than 4.x */ -typedef __builtin_va_list __gnuc_va_list; -typedef __gnuc_va_list va_list; -#define va_start(v,l) __builtin_va_start(v,l) -#define va_end(v) __builtin_va_end(v) -#define va_arg(v,l) __builtin_va_arg(v,l) +typedef __builtin_va_list __gnuc_va_list; +typedef __gnuc_va_list va_list; +#define va_start(v, l) __builtin_va_start(v, l) +#define va_end(v) __builtin_va_end(v) +#define va_arg(v, l) __builtin_va_arg(v, l) #endif /* RT_USING_LIBC */ -#define __RT_STRINGIFY(x...) #x -#define RT_STRINGIFY(x...) __RT_STRINGIFY(x) -#define rt_section(x) __attribute__((section(x))) -#define rt_used __attribute__((used)) -#define rt_align(n) __attribute__((aligned(n))) -#define rt_weak weak//__attribute__((weak)) -#define rt_noreturn __attribute__ ((noreturn)) -#define rt_inline static __inline +#define __RT_STRINGIFY(x...) #x +#define RT_STRINGIFY(x...) __RT_STRINGIFY(x) +#define rt_section(x) __attribute__((section(x))) +#define rt_used __attribute__((used)) +#define rt_align(n) __attribute__((aligned(n))) +#define rt_weak weak //__attribute__((weak)) +#define rt_noreturn __attribute__((noreturn)) +#define rt_inline static __inline #define RTT_API -#elif defined (__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */ -#define rt_section(x) __attribute__((section(x))) -#define rt_used __attribute__((used)) -#define rt_align(n) __attribute__((aligned(n))) -#define rt_weak __attribute__((weak)) -#define rt_inline static inline +#elif defined(__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */ +#define rt_section(x) __attribute__((section(x))) +#define rt_used __attribute__((used)) +#define rt_align(n) __attribute__((aligned(n))) +#define rt_weak __attribute__((weak)) +#define rt_inline static inline #define RTT_API -#elif defined (_MSC_VER) +#elif defined(_MSC_VER) #define rt_section(x) #define rt_used -#define rt_align(n) __declspec(align(n)) +#define rt_align(n) __declspec(align(n)) #define rt_weak -#define rt_inline static __inline +#define rt_inline static __inline #define RTT_API -#elif defined (__TI_COMPILER_VERSION__) +#elif defined(__TI_COMPILER_VERSION__) /* The way that TI compiler set section is different from other(at least - * GCC and MDK) compilers. See ARM Optimizing C/C++ Compiler 5.9.3 for more - * details. */ -#define rt_section(x) __attribute__((section(x))) + * GCC and MDK) compilers. See ARM Optimizing C/C++ Compiler 5.9.3 for more + * details. */ +#define rt_section(x) __attribute__((section(x))) #ifdef __TI_EABI__ -#define rt_used __attribute__((retain)) __attribute__((used)) +#define rt_used __attribute__((retain)) __attribute__((used)) #else -#define rt_used __attribute__((used)) +#define rt_used __attribute__((used)) #endif -#define PRAGMA(x) _Pragma(#x) -#define rt_align(n) __attribute__((aligned(n))) +#define PRAGMA(x) _Pragma(#x) +#define rt_align(n) __attribute__((aligned(n))) #ifdef __TI_EABI__ -#define rt_weak __attribute__((weak)) +#define rt_weak __attribute__((weak)) #else #define rt_weak #endif -#define rt_inline static inline +#define rt_inline static inline #define RTT_API -#elif defined (__TASKING__) -#define rt_section(x) __attribute__((section(x))) -#define rt_used __attribute__((used, protect)) -#define PRAGMA(x) _Pragma(#x) -#define rt_align(n) __attribute__((__align(n))) -#define rt_weak __attribute__((weak)) -#define rt_inline static inline +#elif defined(__TASKING__) +#define rt_section(x) __attribute__((section(x))) +#define rt_used __attribute__((used, protect)) +#define PRAGMA(x) _Pragma(#x) +#define rt_align(n) __attribute__((__align(n))) +#define rt_weak __attribute__((weak)) +#define rt_inline static inline #define RTT_API #else - #error not supported tool chain +#error not supported tool chain #endif /* __ARMCC_VERSION */ /* initialization export */ #ifdef RT_USING_COMPONENTS_INIT -typedef int (*init_fn_t)(void); + typedef int (*init_fn_t)(void); #ifdef _MSC_VER -#pragma section("rti_fn$f",read) - #ifdef RT_DEBUGING_AUTO_INIT - struct rt_init_desc - { - const char* level; - const init_fn_t fn; - const char* fn_name; - }; - #define INIT_EXPORT(fn, level) \ - const char __rti_level_##fn[] = ".rti_fn." level; \ - const char __rti_##fn##_name[] = #fn; \ - __declspec(allocate("rti_fn$f")) \ - rt_used const struct rt_init_desc __rt_init_msc_##fn = \ - {__rti_level_##fn, fn, __rti_##fn##_name}; - #else - struct rt_init_desc - { - const char* level; - const init_fn_t fn; - }; - #define INIT_EXPORT(fn, level) \ - const char __rti_level_##fn[] = ".rti_fn." level; \ - __declspec(allocate("rti_fn$f")) \ - rt_used const struct rt_init_desc __rt_init_msc_##fn = \ - {__rti_level_##fn, fn }; - #endif /* RT_DEBUGING_AUTO_INIT */ +#pragma section("rti_fn$f", read) +#ifdef RT_DEBUGING_AUTO_INIT + struct rt_init_desc + { + const char *level; + const init_fn_t fn; + const char *fn_name; + }; +#define INIT_EXPORT(fn, level) \ + const char __rti_level_##fn[] = ".rti_fn." level; \ + const char __rti_##fn##_name[] = #fn; \ + __declspec(allocate("rti_fn$f")) \ + rt_used const struct rt_init_desc __rt_init_msc_##fn = \ + {__rti_level_##fn, fn, __rti_##fn##_name}; #else - #ifdef RT_DEBUGING_AUTO_INIT - struct rt_init_desc - { - const char* fn_name; - const init_fn_t fn; - }; - #define INIT_EXPORT(fn, level) \ - const char __rti_##fn##_name[] = #fn; \ - rt_used const struct rt_init_desc __rt_init_desc_##fn rt_section(".rti_fn." level) = \ - { __rti_##fn##_name, fn}; - #else - #define INIT_EXPORT(fn, level) \ - rt_used const init_fn_t __rt_init_##fn rt_section(".rti_fn." level) = fn - #endif /* RT_DEBUGING_AUTO_INIT */ + struct rt_init_desc + { + const char *level; + const init_fn_t fn; + }; +#define INIT_EXPORT(fn, level) \ + const char __rti_level_##fn[] = ".rti_fn." level; \ + __declspec(allocate("rti_fn$f")) \ + rt_used const struct rt_init_desc __rt_init_msc_##fn = \ + {__rti_level_##fn, fn}; +#endif /* RT_DEBUGING_AUTO_INIT */ +#else +#ifdef RT_DEBUGING_AUTO_INIT + struct rt_init_desc + { + const char *fn_name; + const init_fn_t fn; + }; +#define INIT_EXPORT(fn, level) \ + const char __rti_##fn##_name[] = #fn; \ + rt_used const struct rt_init_desc __rt_init_desc_##fn rt_section(".rti_fn." level) = \ + {__rti_##fn##_name, fn}; +#else +#define INIT_EXPORT(fn, level) \ + rt_used const init_fn_t __rt_init_##fn rt_section(".rti_fn." level) = fn +#endif /* RT_DEBUGING_AUTO_INIT */ #endif #else #define INIT_EXPORT(fn, level) #endif /* board init routines will be called in board_init() function */ -#define INIT_BOARD_EXPORT(fn) INIT_EXPORT(fn, "1") +#define INIT_BOARD_EXPORT(fn) INIT_EXPORT(fn, "1") /* init cpu, memory, interrupt-controller, bus... */ -#define INIT_CORE_EXPORT(fn) INIT_EXPORT(fn, "1.0") +#define INIT_CORE_EXPORT(fn) INIT_EXPORT(fn, "1.0") /* init pci/pcie, usb platform driver... */ -#define INIT_FRAMEWORK_EXPORT(fn) INIT_EXPORT(fn, "1.1") +#define INIT_FRAMEWORK_EXPORT(fn) INIT_EXPORT(fn, "1.1") /* init platform, user code... */ -#define INIT_PLATFORM_EXPORT(fn) INIT_EXPORT(fn, "1.2") +#define INIT_PLATFORM_EXPORT(fn) INIT_EXPORT(fn, "1.2") /* init sys-timer, clk, pinctrl... */ -#define INIT_SUBSYS_EXPORT(fn) INIT_EXPORT(fn, "1.3") +#define INIT_SUBSYS_EXPORT(fn) INIT_EXPORT(fn, "1.3") /* init early drivers */ -#define INIT_DRIVER_EARLY_EXPORT(fn) INIT_EXPORT(fn, "1.4") +#define INIT_DRIVER_EARLY_EXPORT(fn) INIT_EXPORT(fn, "1.4") /* pre/device/component/env/app init routines will be called in init_thread */ /* components pre-initialization (pure software initialization) */ -#define INIT_PREV_EXPORT(fn) INIT_EXPORT(fn, "2") +#define INIT_PREV_EXPORT(fn) INIT_EXPORT(fn, "2") /* device initialization */ -#define INIT_DEVICE_EXPORT(fn) INIT_EXPORT(fn, "3") +#define INIT_DEVICE_EXPORT(fn) INIT_EXPORT(fn, "3") /* components initialization (dfs, lwip, ...) */ -#define INIT_COMPONENT_EXPORT(fn) INIT_EXPORT(fn, "4") +#define INIT_COMPONENT_EXPORT(fn) INIT_EXPORT(fn, "4") /* environment initialization (mount disk, ...) */ -#define INIT_ENV_EXPORT(fn) INIT_EXPORT(fn, "5") +#define INIT_ENV_EXPORT(fn) INIT_EXPORT(fn, "5") /* application initialization (rtgui application etc ...) */ -#define INIT_APP_EXPORT(fn) INIT_EXPORT(fn, "6") +#define INIT_APP_EXPORT(fn) INIT_EXPORT(fn, "6") /* init after mount fs */ -#define INIT_FS_EXPORT(fn) INIT_EXPORT(fn, "6.0") +#define INIT_FS_EXPORT(fn) INIT_EXPORT(fn, "6.0") /* init in secondary_cpu_c_start */ -#define INIT_SECONDARY_CPU_EXPORT(fn) INIT_EXPORT(fn, "7") +#define INIT_SECONDARY_CPU_EXPORT(fn) INIT_EXPORT(fn, "7") #if !defined(RT_USING_FINSH) /* define these to empty, even if not include finsh.h file */ @@ -382,24 +382,24 @@ typedef int (*init_fn_t)(void); #endif /* event length */ -#define RT_EVENT_LENGTH 32 +#define RT_EVENT_LENGTH 32 /* memory management option */ -#define RT_MM_PAGE_SIZE 4096 -#define RT_MM_PAGE_MASK (RT_MM_PAGE_SIZE - 1) -#define RT_MM_PAGE_BITS 12 +#define RT_MM_PAGE_SIZE 4096 +#define RT_MM_PAGE_MASK (RT_MM_PAGE_SIZE - 1) +#define RT_MM_PAGE_BITS 12 /* kernel malloc definitions */ #ifndef RT_KERNEL_MALLOC -#define RT_KERNEL_MALLOC(sz) rt_malloc(sz) +#define RT_KERNEL_MALLOC(sz) rt_malloc(sz) #endif #ifndef RT_KERNEL_FREE -#define RT_KERNEL_FREE(ptr) rt_free(ptr) +#define RT_KERNEL_FREE(ptr) rt_free(ptr) #endif #ifndef RT_KERNEL_REALLOC -#define RT_KERNEL_REALLOC(ptr, size) rt_realloc(ptr, size) +#define RT_KERNEL_REALLOC(ptr, size) rt_realloc(ptr, size) #endif /** @@ -411,40 +411,40 @@ typedef int (*init_fn_t)(void); /* RT-Thread error code definitions */ #if defined(RT_USING_LIBC) && !RT_USING_LIBC_ISO_ONLY /* POSIX error code compatible */ -#define RT_EOK 0 /**< There is no error */ -#define RT_ERROR 255 /**< A generic/unknown error happens */ -#define RT_ETIMEOUT ETIMEDOUT /**< Timed out */ -#define RT_EFULL ENOSPC /**< The resource is full */ -#define RT_EEMPTY ENODATA /**< The resource is empty */ -#define RT_ENOMEM ENOMEM /**< No memory */ -#define RT_ENOSYS ENOSYS /**< Function not implemented */ -#define RT_EBUSY EBUSY /**< Busy */ -#define RT_EIO EIO /**< IO error */ -#define RT_EINTR EINTR /**< Interrupted system call */ -#define RT_EINVAL EINVAL /**< Invalid argument */ -#define RT_ENOENT ENOENT /**< No entry */ -#define RT_ENOSPC ENOSPC /**< No space left */ -#define RT_EPERM EPERM /**< Operation not permitted */ -#define RT_EFAULT EFAULT /**< Bad address */ -#define RT_ETRAP 254 /**< Trap event */ +#define RT_EOK 0 /**< There is no error */ +#define RT_ERROR 255 /**< A generic/unknown error happens */ +#define RT_ETIMEOUT ETIMEDOUT /**< Timed out */ +#define RT_EFULL ENOSPC /**< The resource is full */ +#define RT_EEMPTY ENODATA /**< The resource is empty */ +#define RT_ENOMEM ENOMEM /**< No memory */ +#define RT_ENOSYS ENOSYS /**< Function not implemented */ +#define RT_EBUSY EBUSY /**< Busy */ +#define RT_EIO EIO /**< IO error */ +#define RT_EINTR EINTR /**< Interrupted system call */ +#define RT_EINVAL EINVAL /**< Invalid argument */ +#define RT_ENOENT ENOENT /**< No entry */ +#define RT_ENOSPC ENOSPC /**< No space left */ +#define RT_EPERM EPERM /**< Operation not permitted */ +#define RT_EFAULT EFAULT /**< Bad address */ +#define RT_ETRAP 254 /**< Trap event */ #else -#define RT_EOK 0 /**< There is no error */ -#define RT_ERROR 1 /**< A generic/unknown error happens */ -#define RT_ETIMEOUT 2 /**< Timed out */ -#define RT_EFULL 3 /**< The resource is full */ -#define RT_EEMPTY 4 /**< The resource is empty */ -#define RT_ENOMEM 5 /**< No memory */ -#define RT_ENOSYS 6 /**< Function not implemented */ -#define RT_EBUSY 7 /**< Busy */ -#define RT_EIO 8 /**< IO error */ -#define RT_EINTR 9 /**< Interrupted system call */ -#define RT_EINVAL 10 /**< Invalid argument */ -#define RT_ENOENT 11 /**< No entry */ -#define RT_ENOSPC 12 /**< No space left */ -#define RT_EPERM 13 /**< Operation not permitted */ -#define RT_ETRAP 14 /**< Trap event */ -#define RT_EFAULT 15 /**< Bad address */ -#endif /* defined(RT_USING_LIBC) && !RT_USING_LIBC_ISO_ONLY */ +#define RT_EOK 0 /**< There is no error */ +#define RT_ERROR 1 /**< A generic/unknown error happens */ +#define RT_ETIMEOUT 2 /**< Timed out */ +#define RT_EFULL 3 /**< The resource is full */ +#define RT_EEMPTY 4 /**< The resource is empty */ +#define RT_ENOMEM 5 /**< No memory */ +#define RT_ENOSYS 6 /**< Function not implemented */ +#define RT_EBUSY 7 /**< Busy */ +#define RT_EIO 8 /**< IO error */ +#define RT_EINTR 9 /**< Interrupted system call */ +#define RT_EINVAL 10 /**< Invalid argument */ +#define RT_ENOENT 11 /**< No entry */ +#define RT_ENOSPC 12 /**< No space left */ +#define RT_EPERM 13 /**< Operation not permitted */ +#define RT_ETRAP 14 /**< Trap event */ +#define RT_EFAULT 15 /**< Bad address */ +#endif /* defined(RT_USING_LIBC) && !RT_USING_LIBC_ISO_ONLY */ /**@}*/ @@ -466,7 +466,7 @@ typedef int (*init_fn_t)(void); * Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4) * would return 16. */ -#define RT_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1)) +#define RT_ALIGN(size, align) (((size) + (align)-1) & ~((align)-1)) /** * @ingroup BasicDef @@ -475,42 +475,47 @@ typedef int (*init_fn_t)(void); * Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4) * would return 12. */ -#define RT_ALIGN_DOWN(size, align) ((size) & ~((align) - 1)) +#define RT_ALIGN_DOWN(size, align) ((size) & ~((align)-1)) -/** - * Double List structure - */ -struct rt_list_node -{ - struct rt_list_node *next; /**< point to next node. */ - struct rt_list_node *prev; /**< point to prev node. */ -}; -typedef struct rt_list_node rt_list_t; /**< Type for lists. */ + /** + * Double List structure + */ + struct rt_list_node + { + struct rt_list_node *next; /**< point to next node. */ + struct rt_list_node *prev; /**< point to prev node. */ + }; + typedef struct rt_list_node rt_list_t; /**< Type for lists. */ -/** - * Single List structure - */ -struct rt_slist_node -{ - struct rt_slist_node *next; /**< point to next node. */ -}; -typedef struct rt_slist_node rt_slist_t; /**< Type for single list. */ + /** + * Single List structure + */ + struct rt_slist_node + { + struct rt_slist_node *next; /**< point to next node. */ + }; + typedef struct rt_slist_node rt_slist_t; /**< Type for single list. */ #ifdef RT_USING_SMP #include /* for spinlock from arch */ -struct rt_spinlock -{ - rt_hw_spinlock_t lock; + struct rt_spinlock + { + rt_hw_spinlock_t lock; #if defined(RT_DEBUGING_SPINLOCK) - void *owner; - void *pc; + void *owner; + void *pc; #endif /* RT_DEBUGING_SPINLOCK */ -}; -typedef struct rt_spinlock rt_spinlock_t; + }; + typedef struct rt_spinlock rt_spinlock_t; #ifndef RT_SPINLOCK_INIT -#define RT_SPINLOCK_INIT {{0}} // default +#define RT_SPINLOCK_INIT \ + { \ + { \ + 0 \ + } \ + } // default #endif /* RT_SPINLOCK_INIT */ #else @@ -519,10 +524,13 @@ struct rt_spinlock { rt_spinlock_t lock; }; -#define RT_SPINLOCK_INIT {0} +#define RT_SPINLOCK_INIT \ + { \ + 0 \ + } #endif /* RT_USING_SMP */ -#define RT_DEFINE_SPINLOCK(x) struct rt_spinlock x = RT_SPINLOCK_INIT +#define RT_DEFINE_SPINLOCK(x) struct rt_spinlock x = RT_SPINLOCK_INIT /** * @addtogroup KernelObject @@ -533,108 +541,112 @@ struct rt_spinlock /* * kernel object macros */ -#define RT_OBJECT_FLAG_MODULE 0x80 /**< is module object. */ +#define RT_OBJECT_FLAG_MODULE 0x80 /**< is module object. */ -/** - * Base structure of Kernel object - */ -struct rt_object -{ + /** + * Base structure of Kernel object + */ + struct rt_object + { #if RT_NAME_MAX > 0 - char name[RT_NAME_MAX]; /**< dynamic name of kernel object */ + char name[RT_NAME_MAX]; /**< dynamic name of kernel object */ #else - const char *name; /**< static name of kernel object */ -#endif /* RT_NAME_MAX > 0 */ - rt_uint8_t type; /**< type of kernel object */ - rt_uint8_t flag; /**< flag of kernel object */ + const char *name; /**< static name of kernel object */ +#endif /* RT_NAME_MAX > 0 */ + rt_uint8_t type; /**< type of kernel object */ + rt_uint8_t flag; /**< flag of kernel object */ #ifdef RT_USING_MODULE - void * module_id; /**< id of application module */ -#endif /* RT_USING_MODULE */ + void *module_id; /**< id of application module */ +#endif /* RT_USING_MODULE */ #ifdef RT_USING_SMART - rt_atomic_t lwp_ref_count; /**< ref count for lwp */ -#endif /* RT_USING_SMART */ + rt_atomic_t lwp_ref_count; /**< ref count for lwp */ +#endif /* RT_USING_SMART */ - rt_list_t list; /**< list node of kernel object */ -}; -typedef struct rt_object *rt_object_t; /**< Type for kernel objects. */ + rt_list_t list; /**< list node of kernel object */ + }; + typedef struct rt_object *rt_object_t; /**< Type for kernel objects. */ -/** - * The object type can be one of the follows with specific - * macros enabled: - * - Thread - * - Semaphore - * - Mutex - * - Event - * - MailBox - * - MessageQueue - * - MemHeap - * - MemPool - * - Device - * - Timer - * - Module - * - Unknown - * - Static - */ -enum rt_object_class_type -{ - RT_Object_Class_Null = 0x00, /**< The object is not used. */ - RT_Object_Class_Thread = 0x01, /**< The object is a thread. */ - RT_Object_Class_Semaphore = 0x02, /**< The object is a semaphore. */ - RT_Object_Class_Mutex = 0x03, /**< The object is a mutex. */ - RT_Object_Class_Event = 0x04, /**< The object is a event. */ - RT_Object_Class_MailBox = 0x05, /**< The object is a mail box. */ - RT_Object_Class_MessageQueue = 0x06, /**< The object is a message queue. */ - RT_Object_Class_MemHeap = 0x07, /**< The object is a memory heap. */ - RT_Object_Class_MemPool = 0x08, /**< The object is a memory pool. */ - RT_Object_Class_Device = 0x09, /**< The object is a device. */ - RT_Object_Class_Timer = 0x0a, /**< The object is a timer. */ - RT_Object_Class_Module = 0x0b, /**< The object is a module. */ - RT_Object_Class_Memory = 0x0c, /**< The object is a memory. */ - RT_Object_Class_Channel = 0x0d, /**< The object is a channel */ - RT_Object_Class_Custom = 0x0e, /**< The object is a custom object */ - RT_Object_Class_Unknown = 0x0f, /**< The object is unknown. */ - RT_Object_Class_Static = 0x80 /**< The object is a static object. */ -}; + /** + * The object type can be one of the follows with specific + * macros enabled: + * - Thread + * - Semaphore + * - Mutex + * - Event + * - MailBox + * - MessageQueue + * - MemHeap + * - MemPool + * - Device + * - Timer + * - Module + * - Unknown + * - Static + */ + enum rt_object_class_type + { + RT_Object_Class_Null = 0x00, /**< The object is not used. */ + RT_Object_Class_Thread = 0x01, /**< The object is a thread. */ + RT_Object_Class_Semaphore = 0x02, /**< The object is a semaphore. */ + RT_Object_Class_Mutex = 0x03, /**< The object is a mutex. */ + RT_Object_Class_Event = 0x04, /**< The object is a event. */ + RT_Object_Class_MailBox = 0x05, /**< The object is a mail box. */ + RT_Object_Class_MessageQueue = 0x06, /**< The object is a message queue. */ + RT_Object_Class_MemHeap = 0x07, /**< The object is a memory heap. */ + RT_Object_Class_MemPool = 0x08, /**< The object is a memory pool. */ + RT_Object_Class_Device = 0x09, /**< The object is a device. */ + RT_Object_Class_Timer = 0x0a, /**< The object is a timer. */ + RT_Object_Class_Module = 0x0b, /**< The object is a module. */ + RT_Object_Class_Memory = 0x0c, /**< The object is a memory. */ + RT_Object_Class_Channel = 0x0d, /**< The object is a channel */ + RT_Object_Class_Custom = 0x0e, /**< The object is a custom object */ + RT_Object_Class_Unknown = 0x0f, /**< The object is unknown. */ + RT_Object_Class_Static = 0x80 /**< The object is a static object. */ + }; -/** - * The information of the kernel object - */ -struct rt_object_information -{ - enum rt_object_class_type type; /**< object class type */ - rt_list_t object_list; /**< object list */ - rt_size_t object_size; /**< object size */ - struct rt_spinlock spinlock; -}; + /** + * The information of the kernel object + */ + struct rt_object_information + { + enum rt_object_class_type type; /**< object class type */ + rt_list_t object_list; /**< object list */ + rt_size_t object_size; /**< object size */ + struct rt_spinlock spinlock; + }; /** * The hook function call macro */ #ifndef RT_USING_HOOK - #define __ON_HOOK_ARGS(__hook, argv) - #define RT_OBJECT_HOOK_CALL(func, argv) +#define __ON_HOOK_ARGS(__hook, argv) +#define RT_OBJECT_HOOK_CALL(func, argv) #else - #define RT_OBJECT_HOOK_CALL(func, argv) __on_##func argv - #ifdef RT_HOOK_USING_FUNC_PTR - #define __ON_HOOK_ARGS(__hook, argv) do {if ((__hook) != RT_NULL) __hook argv; } while (0) - #else - #define __ON_HOOK_ARGS(__hook, argv) - #endif /* RT_HOOK_USING_FUNC_PTR */ +#define RT_OBJECT_HOOK_CALL(func, argv) __on_##func argv +#ifdef RT_HOOK_USING_FUNC_PTR +#define __ON_HOOK_ARGS(__hook, argv) \ + do \ + { \ + if ((__hook) != RT_NULL) \ + __hook argv; \ + } while (0) +#else +#define __ON_HOOK_ARGS(__hook, argv) +#endif /* RT_HOOK_USING_FUNC_PTR */ #endif /* RT_USING_HOOK */ #ifndef __on_rt_interrupt_switch_hook - #define __on_rt_interrupt_switch_hook() __ON_HOOK_ARGS(rt_interrupt_switch_hook, ()) +#define __on_rt_interrupt_switch_hook() __ON_HOOK_ARGS(rt_interrupt_switch_hook, ()) #endif #ifndef __on_rt_malloc_hook - #define __on_rt_malloc_hook(addr, size) __ON_HOOK_ARGS(rt_malloc_hook, (addr, size)) +#define __on_rt_malloc_hook(addr, size) __ON_HOOK_ARGS(rt_malloc_hook, (addr, size)) #endif #ifndef __on_rt_free_hook - #define __on_rt_free_hook(rmem) __ON_HOOK_ARGS(rt_free_hook, (rmem)) +#define __on_rt_free_hook(rmem) __ON_HOOK_ARGS(rt_free_hook, (rmem)) #endif - /**@}*/ /** @@ -646,63 +658,60 @@ struct rt_object_information /** * clock & timer macros */ -#define RT_TIMER_FLAG_DEACTIVATED 0x0 /**< timer is deactive */ -#define RT_TIMER_FLAG_ACTIVATED 0x1 /**< timer is active */ -#define RT_TIMER_FLAG_ONE_SHOT 0x0 /**< one shot timer */ -#define RT_TIMER_FLAG_PERIODIC 0x2 /**< periodic timer */ +#define RT_TIMER_FLAG_DEACTIVATED 0x0 /**< timer is deactive */ +#define RT_TIMER_FLAG_ACTIVATED 0x1 /**< timer is active */ +#define RT_TIMER_FLAG_ONE_SHOT 0x0 /**< one shot timer */ +#define RT_TIMER_FLAG_PERIODIC 0x2 /**< periodic timer */ -#define RT_TIMER_FLAG_HARD_TIMER 0x0 /**< hard timer,the timer's callback function will be called in tick isr. */ -#define RT_TIMER_FLAG_SOFT_TIMER 0x4 /**< soft timer,the timer's callback function will be called in timer thread. */ +#define RT_TIMER_FLAG_HARD_TIMER 0x0 /**< hard timer,the timer's callback function will be called in tick isr. */ +#define RT_TIMER_FLAG_SOFT_TIMER 0x4 /**< soft timer,the timer's callback function will be called in timer thread. */ -#define RT_TIMER_CTRL_SET_TIME 0x0 /**< set timer control command */ -#define RT_TIMER_CTRL_GET_TIME 0x1 /**< get timer control command */ -#define RT_TIMER_CTRL_SET_ONESHOT 0x2 /**< change timer to one shot */ -#define RT_TIMER_CTRL_SET_PERIODIC 0x3 /**< change timer to periodic */ -#define RT_TIMER_CTRL_GET_STATE 0x4 /**< get timer run state active or deactive*/ -#define RT_TIMER_CTRL_GET_REMAIN_TIME 0x5 /**< get the remaining hang time */ -#define RT_TIMER_CTRL_GET_FUNC 0x6 /**< get timer timeout func */ -#define RT_TIMER_CTRL_SET_FUNC 0x7 /**< set timer timeout func */ -#define RT_TIMER_CTRL_GET_PARM 0x8 /**< get timer parameter */ -#define RT_TIMER_CTRL_SET_PARM 0x9 /**< get timer parameter */ +#define RT_TIMER_CTRL_SET_TIME 0x0 /**< set timer control command */ +#define RT_TIMER_CTRL_GET_TIME 0x1 /**< get timer control command */ +#define RT_TIMER_CTRL_SET_ONESHOT 0x2 /**< change timer to one shot */ +#define RT_TIMER_CTRL_SET_PERIODIC 0x3 /**< change timer to periodic */ +#define RT_TIMER_CTRL_GET_STATE 0x4 /**< get timer run state active or deactive*/ +#define RT_TIMER_CTRL_GET_REMAIN_TIME 0x5 /**< get the remaining hang time */ +#define RT_TIMER_CTRL_GET_FUNC 0x6 /**< get timer timeout func */ +#define RT_TIMER_CTRL_SET_FUNC 0x7 /**< set timer timeout func */ +#define RT_TIMER_CTRL_GET_PARM 0x8 /**< get timer parameter */ +#define RT_TIMER_CTRL_SET_PARM 0x9 /**< get timer parameter */ #ifndef RT_TIMER_SKIP_LIST_LEVEL -#define RT_TIMER_SKIP_LIST_LEVEL 1 +#define RT_TIMER_SKIP_LIST_LEVEL 1 #endif /* 1 or 3 */ #ifndef RT_TIMER_SKIP_LIST_MASK -#define RT_TIMER_SKIP_LIST_MASK 0x3 /**< Timer skips the list mask */ +#define RT_TIMER_SKIP_LIST_MASK 0x3 /**< Timer skips the list mask */ #endif -/** - * timer structure - */ -struct rt_timer -{ - struct rt_object parent; /**< inherit from rt_object */ + /** + * timer structure + */ + struct rt_timer + { + void (*func_cb)(void *arg); //!< 回调函数 + size_t start_times; //!< 开始时间 + size_t exp_times; //!< 期待值 + void *data; //!< 回调数据 + int inx; //!< 索引 + uint8_t flags; //!< flags + }; + typedef struct rt_timer *rt_timer_t; - rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL]; + /**@}*/ - void (*timeout_func)(void *parameter); /**< timeout function */ - void *parameter; /**< timeout function's parameter */ - - rt_tick_t init_tick; /**< timer timeout tick */ - rt_tick_t timeout_tick; /**< timeout tick */ -}; -typedef struct rt_timer *rt_timer_t; - -/**@}*/ - -/** - * @addtogroup Signal - */ -/**@{*/ + /** + * @addtogroup Signal + */ + /**@{*/ #ifdef RT_USING_SIGNALS -#define RT_SIG_MAX 32 -typedef unsigned long rt_sigset_t; -typedef siginfo_t rt_siginfo_t; -typedef void (*rt_sighandler_t)(int signo); +#define RT_SIG_MAX 32 + typedef unsigned long rt_sigset_t; + typedef siginfo_t rt_siginfo_t; + typedef void (*rt_sighandler_t)(int signo); #endif /* RT_USING_SIGNALS */ /**@}*/ @@ -719,315 +728,224 @@ typedef void (*rt_sighandler_t)(int signo); /* * thread state definitions */ -#define RT_THREAD_INIT 0x00 /**< Initialized status */ -#define RT_THREAD_CLOSE 0x01 /**< Closed status */ -#define RT_THREAD_READY 0x02 /**< Ready status */ -#define RT_THREAD_RUNNING 0x03 /**< Running status */ +#define RT_THREAD_INIT 0x00 /**< Initialized status */ +#define RT_THREAD_CLOSE 0x01 /**< Closed status */ +#define RT_THREAD_READY 0x02 /**< Ready status */ +#define RT_THREAD_RUNNING 0x03 /**< Running status */ -/* - * for rt_thread_suspend_with_flag() - */ -enum -{ - RT_INTERRUPTIBLE = 0, - RT_KILLABLE, - RT_UNINTERRUPTIBLE, -}; + /* + * for rt_thread_suspend_with_flag() + */ + enum + { + RT_INTERRUPTIBLE = 0, + RT_KILLABLE, + RT_UNINTERRUPTIBLE, + }; -#define RT_THREAD_SUSPEND_MASK 0x04 -#define RT_SIGNAL_COMMON_WAKEUP_MASK 0x02 -#define RT_SIGNAL_KILL_WAKEUP_MASK 0x01 +#define RT_THREAD_SUSPEND_MASK 0x04 +#define RT_SIGNAL_COMMON_WAKEUP_MASK 0x02 +#define RT_SIGNAL_KILL_WAKEUP_MASK 0x01 -#define RT_THREAD_SUSPEND_INTERRUPTIBLE (RT_THREAD_SUSPEND_MASK) /**< Suspend interruptable 0x4 */ -#define RT_THREAD_SUSPEND RT_THREAD_SUSPEND_INTERRUPTIBLE -#define RT_THREAD_SUSPEND_KILLABLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK) /**< Suspend with killable 0x6 */ -#define RT_THREAD_SUSPEND_UNINTERRUPTIBLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK | RT_SIGNAL_KILL_WAKEUP_MASK) /**< Suspend with uninterruptable 0x7 */ -#define RT_THREAD_STAT_MASK 0x07 +#define RT_THREAD_SUSPEND_INTERRUPTIBLE (RT_THREAD_SUSPEND_MASK) /**< Suspend interruptable 0x4 */ +#define RT_THREAD_SUSPEND RT_THREAD_SUSPEND_INTERRUPTIBLE +#define RT_THREAD_SUSPEND_KILLABLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK) /**< Suspend with killable 0x6 */ +#define RT_THREAD_SUSPEND_UNINTERRUPTIBLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK | RT_SIGNAL_KILL_WAKEUP_MASK) /**< Suspend with uninterruptable 0x7 */ +#define RT_THREAD_STAT_MASK 0x07 -#define RT_THREAD_STAT_YIELD 0x08 /**< indicate whether remaining_tick has been reloaded since last schedule */ -#define RT_THREAD_STAT_YIELD_MASK RT_THREAD_STAT_YIELD +#define RT_THREAD_STAT_YIELD 0x08 /**< indicate whether remaining_tick has been reloaded since last schedule */ +#define RT_THREAD_STAT_YIELD_MASK RT_THREAD_STAT_YIELD -#define RT_THREAD_STAT_SIGNAL 0x10 /**< task hold signals */ -#define RT_THREAD_STAT_SIGNAL_READY (RT_THREAD_STAT_SIGNAL | RT_THREAD_READY) -#define RT_THREAD_STAT_SIGNAL_WAIT 0x20 /**< task is waiting for signals */ -#define RT_THREAD_STAT_SIGNAL_PENDING 0x40 /**< signals is held and it has not been procressed */ -#define RT_THREAD_STAT_SIGNAL_MASK 0xf0 +#define RT_THREAD_STAT_SIGNAL 0x10 /**< task hold signals */ +#define RT_THREAD_STAT_SIGNAL_READY (RT_THREAD_STAT_SIGNAL | RT_THREAD_READY) +#define RT_THREAD_STAT_SIGNAL_WAIT 0x20 /**< task is waiting for signals */ +#define RT_THREAD_STAT_SIGNAL_PENDING 0x40 /**< signals is held and it has not been procressed */ +#define RT_THREAD_STAT_SIGNAL_MASK 0xf0 /** * thread control command definitions */ -#define RT_THREAD_CTRL_STARTUP 0x00 /**< Startup thread. */ -#define RT_THREAD_CTRL_CLOSE 0x01 /**< Close thread. */ -#define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */ -#define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */ -#define RT_THREAD_CTRL_BIND_CPU 0x04 /**< Set thread bind cpu. */ +#define RT_THREAD_CTRL_STARTUP 0x00 /**< Startup thread. */ +#define RT_THREAD_CTRL_CLOSE 0x01 /**< Close thread. */ +#define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */ +#define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */ +#define RT_THREAD_CTRL_BIND_CPU 0x04 /**< Set thread bind cpu. */ #ifdef RT_USING_SMP -#define RT_CPU_DETACHED RT_CPUS_NR /**< The thread not running on cpu. */ -#define RT_CPU_MASK ((1 << RT_CPUS_NR) - 1) /**< All CPUs mask bit. */ +#define RT_CPU_DETACHED RT_CPUS_NR /**< The thread not running on cpu. */ +#define RT_CPU_MASK ((1 << RT_CPUS_NR) - 1) /**< All CPUs mask bit. */ #ifndef RT_SCHEDULE_IPI -#define RT_SCHEDULE_IPI 0 +#define RT_SCHEDULE_IPI 0 #endif /* RT_SCHEDULE_IPI */ #ifndef RT_STOP_IPI -#define RT_STOP_IPI 1 +#define RT_STOP_IPI 1 #endif /* RT_STOP_IPI */ -struct rt_cpu_usage_stats -{ - rt_uint64_t user; - rt_uint64_t system; - rt_uint64_t irq; - rt_uint64_t idle; -}; -typedef struct rt_cpu_usage_stats *rt_cpu_usage_stats_t; + struct rt_cpu_usage_stats + { + rt_uint64_t user; + rt_uint64_t system; + rt_uint64_t irq; + rt_uint64_t idle; + }; + typedef struct rt_cpu_usage_stats *rt_cpu_usage_stats_t; -/** - * CPUs definitions - * - */ -struct rt_cpu -{ - struct rt_thread *current_thread; - struct rt_thread *idle_thread; - rt_atomic_t irq_nest; - rt_uint8_t irq_switch_flag; + /** + * CPUs definitions + * + */ + struct rt_cpu + { + struct rt_thread *current_thread; + struct rt_thread *idle_thread; + rt_atomic_t irq_nest; + rt_uint8_t irq_switch_flag; - rt_uint8_t current_priority; - rt_list_t priority_table[RT_THREAD_PRIORITY_MAX]; + rt_uint8_t current_priority; + rt_list_t priority_table[RT_THREAD_PRIORITY_MAX]; #if RT_THREAD_PRIORITY_MAX > 32 - rt_uint32_t priority_group; - rt_uint8_t ready_table[32]; + rt_uint32_t priority_group; + rt_uint8_t ready_table[32]; #else - rt_uint32_t priority_group; + rt_uint32_t priority_group; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ - rt_atomic_t tick; + rt_atomic_t tick; - struct rt_spinlock spinlock; + struct rt_spinlock spinlock; #ifdef RT_USING_SMART - struct rt_cpu_usage_stats cpu_stat; + struct rt_cpu_usage_stats cpu_stat; #endif -}; -typedef struct rt_cpu *rt_cpu_t; + }; + typedef struct rt_cpu *rt_cpu_t; #endif /* RT_USING_SMP */ -struct rt_thread; + struct rt_thread; #ifdef RT_USING_SMART -typedef rt_err_t (*rt_wakeup_func_t)(void *object, struct rt_thread *thread); + typedef rt_err_t (*rt_wakeup_func_t)(void *object, struct rt_thread *thread); -struct rt_wakeup -{ - rt_wakeup_func_t func; - void *user_data; -}; + struct rt_wakeup + { + rt_wakeup_func_t func; + void *user_data; + }; -#define _LWP_NSIG 64 +#define _LWP_NSIG 64 #ifdef ARCH_CPU_64BIT -#define _LWP_NSIG_BPW 64 +#define _LWP_NSIG_BPW 64 #else -#define _LWP_NSIG_BPW 32 +#define _LWP_NSIG_BPW 32 #endif #define _LWP_NSIG_WORDS (RT_ALIGN(_LWP_NSIG, _LWP_NSIG_BPW) / _LWP_NSIG_BPW) -typedef void (*lwp_sighandler_t)(int); -typedef void (*lwp_sigaction_t)(int signo, siginfo_t *info, void *context); + typedef void (*lwp_sighandler_t)(int); + typedef void (*lwp_sigaction_t)(int signo, siginfo_t *info, void *context); -typedef struct { - unsigned long sig[_LWP_NSIG_WORDS]; -} lwp_sigset_t; + typedef struct + { + unsigned long sig[_LWP_NSIG_WORDS]; + } lwp_sigset_t; #if _LWP_NSIG <= 64 -#define lwp_sigmask(signo) ((lwp_sigset_t){.sig = {[0] = ((long)(1u << ((signo)-1)))}}) -#define lwp_sigset_init(mask) ((lwp_sigset_t){.sig = {[0] = (long)(mask)}}) +#define lwp_sigmask(signo) ((lwp_sigset_t){.sig = {[0] = ((long)(1u << ((signo)-1)))}}) +#define lwp_sigset_init(mask) ((lwp_sigset_t){.sig = {[0] = (long)(mask)}}) #endif -struct lwp_sigaction { - union { - void (*_sa_handler)(int); - void (*_sa_sigaction)(int, siginfo_t *, void *); - } __sa_handler; - lwp_sigset_t sa_mask; - int sa_flags; - void (*sa_restorer)(void); -}; + struct lwp_sigaction + { + union + { + void (*_sa_handler)(int); + void (*_sa_sigaction)(int, siginfo_t *, void *); + } __sa_handler; + lwp_sigset_t sa_mask; + int sa_flags; + void (*sa_restorer)(void); + }; -typedef struct lwp_siginfo { - rt_list_t node; + typedef struct lwp_siginfo + { + rt_list_t node; - struct { - int signo; - int code; - long value; + struct + { + int signo; + int code; + long value; - int from_tid; - pid_t from_pid; - } ksiginfo; -} *lwp_siginfo_t; + int from_tid; + pid_t from_pid; + } ksiginfo; + } *lwp_siginfo_t; -typedef struct lwp_sigqueue { - rt_list_t siginfo_list; - lwp_sigset_t sigset_pending; -} *lwp_sigqueue_t; + typedef struct lwp_sigqueue + { + rt_list_t siginfo_list; + lwp_sigset_t sigset_pending; + } *lwp_sigqueue_t; -struct lwp_thread_signal { - lwp_sigset_t sigset_mask; - struct lwp_sigqueue sig_queue; -}; + struct lwp_thread_signal + { + lwp_sigset_t sigset_mask; + struct lwp_sigqueue sig_queue; + }; -struct rt_user_context -{ - void *sp; - void *pc; - void *flag; + struct rt_user_context + { + void *sp; + void *pc; + void *flag; - void *ctx; -}; + void *ctx; + }; #endif -typedef void (*rt_thread_cleanup_t)(struct rt_thread *tid); + typedef void (*rt_thread_cleanup_t)(struct rt_thread *tid); -/** - * Thread structure - */ -struct rt_thread -{ - struct rt_object parent; - rt_list_t tlist; /**< the thread list */ - rt_list_t tlist_schedule; /**< the thread list */ + /** + * Thread structure + */ + struct rt_thread + { + pthread_t th; /**< use pthread*/ + rt_list_t tlist; /**< the thread list */ - /* stack point and entry */ - void *sp; /**< stack point */ - void *entry; /**< entry */ - void *parameter; /**< parameter */ - void *stack_addr; /**< stack address */ - rt_uint32_t stack_size; /**< stack size */ + int stat; + rt_err_t error; + struct rt_timer thread_timer; /**< built-in thread timer */ - /* error code */ - rt_err_t error; /**< error code */ - - rt_uint8_t stat; /**< thread status */ - -#ifdef RT_USING_SMP - rt_uint8_t bind_cpu; /**< thread is bind to cpu */ - rt_uint8_t oncpu; /**< process on cpu */ - - rt_atomic_t cpus_lock_nest; /**< cpus lock count */ - rt_atomic_t critical_lock_nest; /**< critical lock count */ -#endif /*RT_USING_SMP*/ - - /* priority */ - rt_uint8_t current_priority; /**< current priority */ - rt_uint8_t init_priority; /**< initialized priority */ -#if RT_THREAD_PRIORITY_MAX > 32 - rt_uint8_t number; - rt_uint8_t high_mask; -#endif /* RT_THREAD_PRIORITY_MAX > 32 */ - rt_uint32_t number_mask; /**< priority number mask */ - -#ifdef RT_USING_MUTEX - /* object for IPC */ - rt_list_t taken_object_list; - rt_object_t pending_object; -#endif - -#ifdef RT_USING_EVENT - /* thread event */ - rt_uint32_t event_set; - rt_uint8_t event_info; -#endif /* RT_USING_EVENT */ - -#ifdef RT_USING_SIGNALS - rt_sigset_t sig_pending; /**< the pending signals */ - rt_sigset_t sig_mask; /**< the mask bits of signal */ - -#ifndef RT_USING_SMP - void *sig_ret; /**< the return stack pointer from signal */ -#endif /* RT_USING_SMP */ - rt_sighandler_t *sig_vectors; /**< vectors of signal handler */ - void *si_list; /**< the signal infor list */ -#endif /* RT_USING_SIGNALS */ - - rt_atomic_t init_tick; /**< thread's initialized tick */ - rt_atomic_t remaining_tick; /**< remaining tick */ - -#ifdef RT_USING_CPU_USAGE - rt_uint64_t duration_tick; /**< cpu usage tick */ -#endif /* RT_USING_CPU_USAGE */ - -#ifdef RT_USING_PTHREADS - void *pthread_data; /**< the handle of pthread data, adapt 32/64bit */ -#endif /* RT_USING_PTHREADS */ - - struct rt_timer thread_timer; /**< built-in thread timer */ - - rt_thread_cleanup_t cleanup; /**< cleanup function when thread exit */ - - /* light weight process if present */ -#ifdef RT_USING_SMART - void *msg_ret; /**< the return msg */ - - void *lwp; /**< the lwp reference */ - /* for user create */ - void *user_entry; - void *user_stack; - rt_uint32_t user_stack_size; - rt_uint32_t *kernel_sp; /**< kernel stack point */ - rt_list_t sibling; /**< next thread of same process */ - - struct lwp_thread_signal signal; /**< lwp signal for user-space thread */ - struct rt_user_context user_ctx; /**< user space context */ - struct rt_wakeup wakeup; /**< wakeup data */ - int exit_request; /**< pending exit request of thread */ - int tid; /**< thread ID used by process */ - int tid_ref_count; /**< reference of tid */ - void *susp_recycler; /**< suspended recycler on this thread */ - - rt_uint64_t user_time; - rt_uint64_t system_time; - -#ifndef ARCH_MM_MMU - lwp_sighandler_t signal_handler[32]; -#else - int step_exec; - int debug_attach_req; - int debug_ret_user; - int debug_suspend; - struct rt_hw_exp_stack *regs; - void *thread_idr; /** lwp thread indicator */ - int *clear_child_tid; -#endif /* ARCH_MM_MMU */ -#endif /* RT_USING_SMART */ - -#ifdef RT_USING_MEM_PROTECTION - void *mem_regions; -#ifdef RT_USING_HW_STACK_GUARD - void *stack_buf; -#endif -#endif - - rt_atomic_t ref_count; - struct rt_spinlock spinlock; - rt_ubase_t user_data; /**< private user data beyond this thread */ -}; -typedef struct rt_thread *rt_thread_t; + pthread_mutex_t suspend_lock; + // rt_atomic_t ref_count; + // struct rt_spinlock spinlock; + // rt_ubase_t user_data; /**< private user data beyond this thread */ + }; + typedef struct rt_thread *rt_thread_t; #ifdef RT_USING_SMART #define IS_USER_MODE(t) ((t)->user_ctx.ctx == RT_NULL) #endif /* RT_USING_SMART */ -/**@}*/ + /**@}*/ -#define rt_atomic_inc(v) rt_atomic_add((v), 1) -#define rt_atomic_dec(v) rt_atomic_sub((v), 1) -#define rt_get_thread_struct(object) do { rt_atomic_inc(&(object)->ref_count); } while(0) -#define rt_put_thread_struct(object) do { rt_atomic_dec(&(object)->ref_count); } while(0) +#define rt_atomic_inc(v) rt_atomic_add((v), 1) +#define rt_atomic_dec(v) rt_atomic_sub((v), 1) +#define rt_get_thread_struct(object) \ + do \ + { \ + rt_atomic_inc(&(object)->ref_count); \ + } while (0) +#define rt_put_thread_struct(object) \ + do \ + { \ + rt_atomic_dec(&(object)->ref_count); \ + } while (0) /** * @addtogroup IPC @@ -1038,296 +956,296 @@ typedef struct rt_thread *rt_thread_t; /** * IPC flags and control command definitions */ -#define RT_IPC_FLAG_FIFO 0x00 /**< FIFOed IPC. @ref IPC. */ -#define RT_IPC_FLAG_PRIO 0x01 /**< PRIOed IPC. @ref IPC. */ +#define RT_IPC_FLAG_FIFO 0x00 /**< FIFOed IPC. @ref IPC. */ +#define RT_IPC_FLAG_PRIO 0x01 /**< PRIOed IPC. @ref IPC. */ -#define RT_IPC_CMD_UNKNOWN 0x00 /**< unknown IPC command */ -#define RT_IPC_CMD_RESET 0x01 /**< reset IPC object */ -#define RT_IPC_CMD_GET_STATE 0x02 /**< get the state of IPC object */ +#define RT_IPC_CMD_UNKNOWN 0x00 /**< unknown IPC command */ +#define RT_IPC_CMD_RESET 0x01 /**< reset IPC object */ +#define RT_IPC_CMD_GET_STATE 0x02 /**< get the state of IPC object */ -#define RT_WAITING_FOREVER -1 /**< Block forever until get resource. */ -#define RT_WAITING_NO 0 /**< Non-block. */ +#define RT_WAITING_FOREVER -1 /**< Block forever until get resource. */ +#define RT_WAITING_NO 0 /**< Non-block. */ -/** - * Base structure of IPC object - */ -struct rt_ipc_object -{ - struct rt_object parent; /**< inherit from rt_object */ + /** + * Base structure of IPC object + */ + struct rt_ipc_object + { + struct rt_object parent; /**< inherit from rt_object */ - rt_list_t suspend_thread; /**< threads pended on this resource */ -}; + rt_list_t suspend_thread; /**< threads pended on this resource */ + }; #ifdef RT_USING_SEMAPHORE -/** - * Semaphore structure - */ -struct rt_semaphore -{ - struct rt_ipc_object parent; /**< inherit from ipc_object */ + /** + * Semaphore structure + */ + struct rt_semaphore + { + struct rt_ipc_object parent; /**< inherit from ipc_object */ - rt_uint16_t value; /**< value of semaphore. */ - rt_uint16_t reserved; /**< reserved field */ - struct rt_spinlock spinlock; -}; -typedef struct rt_semaphore *rt_sem_t; + rt_uint16_t value; /**< value of semaphore. */ + rt_uint16_t reserved; /**< reserved field */ + struct rt_spinlock spinlock; + }; + typedef struct rt_semaphore *rt_sem_t; #endif /* RT_USING_SEMAPHORE */ #ifdef RT_USING_MUTEX -/** - * Mutual exclusion (mutex) structure - */ -struct rt_mutex -{ - pthread_mutex_t lock; -}; -typedef struct rt_mutex *rt_mutex_t; + /** + * Mutual exclusion (mutex) structure + */ + struct rt_mutex + { + pthread_mutex_t lock; + }; + typedef struct rt_mutex *rt_mutex_t; #endif /* RT_USING_MUTEX */ #ifdef RT_USING_EVENT /** * flag definitions in event */ -#define RT_EVENT_FLAG_AND 0x01 /**< logic and */ -#define RT_EVENT_FLAG_OR 0x02 /**< logic or */ -#define RT_EVENT_FLAG_CLEAR 0x04 /**< clear flag */ +#define RT_EVENT_FLAG_AND 0x01 /**< logic and */ +#define RT_EVENT_FLAG_OR 0x02 /**< logic or */ +#define RT_EVENT_FLAG_CLEAR 0x04 /**< clear flag */ -/* - * event structure - */ -struct rt_event -{ - struct rt_ipc_object parent; /**< inherit from ipc_object */ + /* + * event structure + */ + struct rt_event + { + struct rt_ipc_object parent; /**< inherit from ipc_object */ - rt_uint32_t set; /**< event set */ - struct rt_spinlock spinlock; -}; -typedef struct rt_event *rt_event_t; + rt_uint32_t set; /**< event set */ + struct rt_spinlock spinlock; + }; + typedef struct rt_event *rt_event_t; #endif /* RT_USING_EVENT */ #ifdef RT_USING_MAILBOX -/** - * mailbox structure - */ -struct rt_mailbox -{ - struct rt_ipc_object parent; /**< inherit from ipc_object */ + /** + * mailbox structure + */ + struct rt_mailbox + { + struct rt_ipc_object parent; /**< inherit from ipc_object */ - rt_ubase_t *msg_pool; /**< start address of message buffer */ + rt_ubase_t *msg_pool; /**< start address of message buffer */ - rt_uint16_t size; /**< size of message pool */ + rt_uint16_t size; /**< size of message pool */ - rt_uint16_t entry; /**< index of messages in msg_pool */ - rt_uint16_t in_offset; /**< input offset of the message buffer */ - rt_uint16_t out_offset; /**< output offset of the message buffer */ + rt_uint16_t entry; /**< index of messages in msg_pool */ + rt_uint16_t in_offset; /**< input offset of the message buffer */ + rt_uint16_t out_offset; /**< output offset of the message buffer */ - rt_list_t suspend_sender_thread; /**< sender thread suspended on this mailbox */ - struct rt_spinlock spinlock; -}; -typedef struct rt_mailbox *rt_mailbox_t; + rt_list_t suspend_sender_thread; /**< sender thread suspended on this mailbox */ + struct rt_spinlock spinlock; + }; + typedef struct rt_mailbox *rt_mailbox_t; #endif /* RT_USING_MAILBOX */ #ifdef RT_USING_MESSAGEQUEUE -/** - * message queue structure - */ -struct rt_messagequeue -{ - struct rt_ipc_object parent; /**< inherit from ipc_object */ + /** + * message queue structure + */ + struct rt_messagequeue + { + struct rt_ipc_object parent; /**< inherit from ipc_object */ - void *msg_pool; /**< start address of message queue */ + void *msg_pool; /**< start address of message queue */ - rt_uint16_t msg_size; /**< message size of each message */ - rt_uint16_t max_msgs; /**< max number of messages */ + rt_uint16_t msg_size; /**< message size of each message */ + rt_uint16_t max_msgs; /**< max number of messages */ - rt_uint16_t entry; /**< index of messages in the queue */ + rt_uint16_t entry; /**< index of messages in the queue */ - void *msg_queue_head; /**< list head */ - void *msg_queue_tail; /**< list tail */ - void *msg_queue_free; /**< pointer indicated the free node of queue */ + void *msg_queue_head; /**< list head */ + void *msg_queue_tail; /**< list tail */ + void *msg_queue_free; /**< pointer indicated the free node of queue */ - rt_list_t suspend_sender_thread; /**< sender thread suspended on this message queue */ - struct rt_spinlock spinlock; -}; -typedef struct rt_messagequeue *rt_mq_t; + rt_list_t suspend_sender_thread; /**< sender thread suspended on this message queue */ + struct rt_spinlock spinlock; + }; + typedef struct rt_messagequeue *rt_mq_t; #endif /* RT_USING_MESSAGEQUEUE */ -/**@}*/ + /**@}*/ -/** - * @addtogroup MM - */ + /** + * @addtogroup MM + */ -/**@{*/ + /**@{*/ #ifdef RT_USING_HEAP -/* - * memory structure - */ -struct rt_memory -{ - struct rt_object parent; /**< inherit from rt_object */ - const char * algorithm; /**< Memory management algorithm name */ - rt_ubase_t address; /**< memory start address */ - rt_size_t total; /**< memory size */ - rt_size_t used; /**< size used */ - rt_size_t max; /**< maximum usage */ -}; -typedef struct rt_memory *rt_mem_t; + /* + * memory structure + */ + struct rt_memory + { + struct rt_object parent; /**< inherit from rt_object */ + const char *algorithm; /**< Memory management algorithm name */ + rt_ubase_t address; /**< memory start address */ + rt_size_t total; /**< memory size */ + rt_size_t used; /**< size used */ + rt_size_t max; /**< maximum usage */ + }; + typedef struct rt_memory *rt_mem_t; #endif /* RT_USING_HEAP */ -/* - * memory management - * heap & partition - */ + /* + * memory management + * heap & partition + */ #ifdef RT_USING_SMALL_MEM -typedef rt_mem_t rt_smem_t; + typedef rt_mem_t rt_smem_t; #endif /* RT_USING_SMALL_MEM */ #ifdef RT_USING_SLAB -typedef rt_mem_t rt_slab_t; + typedef rt_mem_t rt_slab_t; #endif /* RT_USING_SLAB */ #ifdef RT_USING_MEMHEAP -/** - * memory item on the heap - */ -struct rt_memheap_item -{ - rt_uint32_t magic; /**< magic number for memheap */ - struct rt_memheap *pool_ptr; /**< point of pool */ + /** + * memory item on the heap + */ + struct rt_memheap_item + { + rt_uint32_t magic; /**< magic number for memheap */ + struct rt_memheap *pool_ptr; /**< point of pool */ - struct rt_memheap_item *next; /**< next memheap item */ - struct rt_memheap_item *prev; /**< prev memheap item */ + struct rt_memheap_item *next; /**< next memheap item */ + struct rt_memheap_item *prev; /**< prev memheap item */ - struct rt_memheap_item *next_free; /**< next free memheap item */ - struct rt_memheap_item *prev_free; /**< prev free memheap item */ + struct rt_memheap_item *next_free; /**< next free memheap item */ + struct rt_memheap_item *prev_free; /**< prev free memheap item */ #ifdef RT_USING_MEMTRACE - rt_uint8_t owner_thread_name[4]; /**< owner thread name */ -#endif /* RT_USING_MEMTRACE */ -}; + rt_uint8_t owner_thread_name[4]; /**< owner thread name */ +#endif /* RT_USING_MEMTRACE */ + }; -/** - * Base structure of memory heap object - */ -struct rt_memheap -{ - struct rt_object parent; /**< inherit from rt_object */ + /** + * Base structure of memory heap object + */ + struct rt_memheap + { + struct rt_object parent; /**< inherit from rt_object */ - void *start_addr; /**< pool start address and size */ + void *start_addr; /**< pool start address and size */ - rt_size_t pool_size; /**< pool size */ - rt_size_t available_size; /**< available size */ - rt_size_t max_used_size; /**< maximum allocated size */ + rt_size_t pool_size; /**< pool size */ + rt_size_t available_size; /**< available size */ + rt_size_t max_used_size; /**< maximum allocated size */ - struct rt_memheap_item *block_list; /**< used block list */ + struct rt_memheap_item *block_list; /**< used block list */ - struct rt_memheap_item *free_list; /**< free block list */ - struct rt_memheap_item free_header; /**< free block list header */ + struct rt_memheap_item *free_list; /**< free block list */ + struct rt_memheap_item free_header; /**< free block list header */ - struct rt_semaphore lock; /**< semaphore lock */ - rt_bool_t locked; /**< External lock mark */ -}; + struct rt_semaphore lock; /**< semaphore lock */ + rt_bool_t locked; /**< External lock mark */ + }; #endif /* RT_USING_MEMHEAP */ #ifdef RT_USING_MEMPOOL -/** - * Base structure of Memory pool object - */ -struct rt_mempool -{ - struct rt_object parent; /**< inherit from rt_object */ + /** + * Base structure of Memory pool object + */ + struct rt_mempool + { + struct rt_object parent; /**< inherit from rt_object */ - void *start_address; /**< memory pool start */ - rt_size_t size; /**< size of memory pool */ + void *start_address; /**< memory pool start */ + rt_size_t size; /**< size of memory pool */ - rt_size_t block_size; /**< size of memory blocks */ - rt_uint8_t *block_list; /**< memory blocks list */ + rt_size_t block_size; /**< size of memory blocks */ + rt_uint8_t *block_list; /**< memory blocks list */ - rt_size_t block_total_count; /**< numbers of memory block */ - rt_size_t block_free_count; /**< numbers of free memory block */ + rt_size_t block_total_count; /**< numbers of memory block */ + rt_size_t block_free_count; /**< numbers of free memory block */ - rt_list_t suspend_thread; /**< threads pended on this resource */ - struct rt_spinlock spinlock; -}; -typedef struct rt_mempool *rt_mp_t; + rt_list_t suspend_thread; /**< threads pended on this resource */ + struct rt_spinlock spinlock; + }; + typedef struct rt_mempool *rt_mp_t; #endif /* RT_USING_MEMPOOL */ -/**@}*/ + /**@}*/ #ifdef RT_USING_DEVICE -/** - * @addtogroup Device - */ + /** + * @addtogroup Device + */ -/**@{*/ + /**@{*/ -/** - * device (I/O) class type - */ -enum rt_device_class_type -{ - RT_Device_Class_Char = 0, /**< character device */ - RT_Device_Class_Block, /**< block device */ - RT_Device_Class_NetIf, /**< net interface */ - RT_Device_Class_MTD, /**< memory device */ - RT_Device_Class_CAN, /**< CAN device */ - RT_Device_Class_RTC, /**< RTC device */ - RT_Device_Class_Sound, /**< Sound device */ - RT_Device_Class_Graphic, /**< Graphic device */ - RT_Device_Class_I2CBUS, /**< I2C bus device */ - RT_Device_Class_USBDevice, /**< USB slave device */ - RT_Device_Class_USBHost, /**< USB host bus */ - RT_Device_Class_USBOTG, /**< USB OTG bus */ - RT_Device_Class_SPIBUS, /**< SPI bus device */ - RT_Device_Class_SPIDevice, /**< SPI device */ - RT_Device_Class_SDIO, /**< SDIO bus device */ - RT_Device_Class_PM, /**< PM pseudo device */ - RT_Device_Class_Pipe, /**< Pipe device */ - RT_Device_Class_Portal, /**< Portal device */ - RT_Device_Class_Timer, /**< Timer device */ - RT_Device_Class_Miscellaneous, /**< Miscellaneous device */ - RT_Device_Class_Sensor, /**< Sensor device */ - RT_Device_Class_Touch, /**< Touch device */ - RT_Device_Class_PHY, /**< PHY device */ - RT_Device_Class_Security, /**< Security device */ - RT_Device_Class_WLAN, /**< WLAN device */ - RT_Device_Class_Pin, /**< Pin device */ - RT_Device_Class_ADC, /**< ADC device */ - RT_Device_Class_DAC, /**< DAC device */ - RT_Device_Class_WDT, /**< WDT device */ - RT_Device_Class_PWM, /**< PWM device */ - RT_Device_Class_Bus, /**< Bus device */ - RT_Device_Class_Unknown /**< unknown device */ -}; + /** + * device (I/O) class type + */ + enum rt_device_class_type + { + RT_Device_Class_Char = 0, /**< character device */ + RT_Device_Class_Block, /**< block device */ + RT_Device_Class_NetIf, /**< net interface */ + RT_Device_Class_MTD, /**< memory device */ + RT_Device_Class_CAN, /**< CAN device */ + RT_Device_Class_RTC, /**< RTC device */ + RT_Device_Class_Sound, /**< Sound device */ + RT_Device_Class_Graphic, /**< Graphic device */ + RT_Device_Class_I2CBUS, /**< I2C bus device */ + RT_Device_Class_USBDevice, /**< USB slave device */ + RT_Device_Class_USBHost, /**< USB host bus */ + RT_Device_Class_USBOTG, /**< USB OTG bus */ + RT_Device_Class_SPIBUS, /**< SPI bus device */ + RT_Device_Class_SPIDevice, /**< SPI device */ + RT_Device_Class_SDIO, /**< SDIO bus device */ + RT_Device_Class_PM, /**< PM pseudo device */ + RT_Device_Class_Pipe, /**< Pipe device */ + RT_Device_Class_Portal, /**< Portal device */ + RT_Device_Class_Timer, /**< Timer device */ + RT_Device_Class_Miscellaneous, /**< Miscellaneous device */ + RT_Device_Class_Sensor, /**< Sensor device */ + RT_Device_Class_Touch, /**< Touch device */ + RT_Device_Class_PHY, /**< PHY device */ + RT_Device_Class_Security, /**< Security device */ + RT_Device_Class_WLAN, /**< WLAN device */ + RT_Device_Class_Pin, /**< Pin device */ + RT_Device_Class_ADC, /**< ADC device */ + RT_Device_Class_DAC, /**< DAC device */ + RT_Device_Class_WDT, /**< WDT device */ + RT_Device_Class_PWM, /**< PWM device */ + RT_Device_Class_Bus, /**< Bus device */ + RT_Device_Class_Unknown /**< unknown device */ + }; /** * device flags definitions */ -#define RT_DEVICE_FLAG_DEACTIVATE 0x000 /**< device is not not initialized */ +#define RT_DEVICE_FLAG_DEACTIVATE 0x000 /**< device is not not initialized */ -#define RT_DEVICE_FLAG_RDONLY 0x001 /**< read only */ -#define RT_DEVICE_FLAG_WRONLY 0x002 /**< write only */ -#define RT_DEVICE_FLAG_RDWR 0x003 /**< read and write */ +#define RT_DEVICE_FLAG_RDONLY 0x001 /**< read only */ +#define RT_DEVICE_FLAG_WRONLY 0x002 /**< write only */ +#define RT_DEVICE_FLAG_RDWR 0x003 /**< read and write */ -#define RT_DEVICE_FLAG_REMOVABLE 0x004 /**< removable device */ -#define RT_DEVICE_FLAG_STANDALONE 0x008 /**< standalone device */ -#define RT_DEVICE_FLAG_ACTIVATED 0x010 /**< device is activated */ -#define RT_DEVICE_FLAG_SUSPENDED 0x020 /**< device is suspended */ -#define RT_DEVICE_FLAG_STREAM 0x040 /**< stream mode */ +#define RT_DEVICE_FLAG_REMOVABLE 0x004 /**< removable device */ +#define RT_DEVICE_FLAG_STANDALONE 0x008 /**< standalone device */ +#define RT_DEVICE_FLAG_ACTIVATED 0x010 /**< device is activated */ +#define RT_DEVICE_FLAG_SUSPENDED 0x020 /**< device is suspended */ +#define RT_DEVICE_FLAG_STREAM 0x040 /**< stream mode */ -#define RT_DEVICE_FLAG_INT_RX 0x100 /**< INT mode on Rx */ -#define RT_DEVICE_FLAG_DMA_RX 0x200 /**< DMA mode on Rx */ -#define RT_DEVICE_FLAG_INT_TX 0x400 /**< INT mode on Tx */ -#define RT_DEVICE_FLAG_DMA_TX 0x800 /**< DMA mode on Tx */ +#define RT_DEVICE_FLAG_INT_RX 0x100 /**< INT mode on Rx */ +#define RT_DEVICE_FLAG_DMA_RX 0x200 /**< DMA mode on Rx */ +#define RT_DEVICE_FLAG_INT_TX 0x400 /**< INT mode on Tx */ +#define RT_DEVICE_FLAG_DMA_TX 0x800 /**< DMA mode on Tx */ -#define RT_DEVICE_OFLAG_CLOSE 0x000 /**< device is closed */ -#define RT_DEVICE_OFLAG_RDONLY 0x001 /**< read only access */ -#define RT_DEVICE_OFLAG_WRONLY 0x002 /**< write only access */ -#define RT_DEVICE_OFLAG_RDWR 0x003 /**< read and write */ -#define RT_DEVICE_OFLAG_OPEN 0x008 /**< device is opened */ -#define RT_DEVICE_OFLAG_MASK 0xf0f /**< mask of open flag */ +#define RT_DEVICE_OFLAG_CLOSE 0x000 /**< device is closed */ +#define RT_DEVICE_OFLAG_RDONLY 0x001 /**< read only access */ +#define RT_DEVICE_OFLAG_WRONLY 0x002 /**< write only access */ +#define RT_DEVICE_OFLAG_RDWR 0x003 /**< read and write */ +#define RT_DEVICE_OFLAG_OPEN 0x008 /**< device is opened */ +#define RT_DEVICE_OFLAG_MASK 0xf0f /**< mask of open flag */ /** * general device commands @@ -1335,125 +1253,125 @@ enum rt_device_class_type * 0x20 - 0x3F udevice control commands * 0x40 - special device control commands */ -#define RT_DEVICE_CTRL_RESUME 0x01 /**< resume device */ -#define RT_DEVICE_CTRL_SUSPEND 0x02 /**< suspend device */ -#define RT_DEVICE_CTRL_CONFIG 0x03 /**< configure device */ -#define RT_DEVICE_CTRL_CLOSE 0x04 /**< close device */ -#define RT_DEVICE_CTRL_NOTIFY_SET 0x05 /**< set notify func */ -#define RT_DEVICE_CTRL_SET_INT 0x06 /**< set interrupt */ -#define RT_DEVICE_CTRL_CLR_INT 0x07 /**< clear interrupt */ -#define RT_DEVICE_CTRL_GET_INT 0x08 /**< get interrupt status */ -#define RT_DEVICE_CTRL_CONSOLE_OFLAG 0x09 /**< get console open flag */ -#define RT_DEVICE_CTRL_MASK 0x1f /**< mask for contrl commands */ +#define RT_DEVICE_CTRL_RESUME 0x01 /**< resume device */ +#define RT_DEVICE_CTRL_SUSPEND 0x02 /**< suspend device */ +#define RT_DEVICE_CTRL_CONFIG 0x03 /**< configure device */ +#define RT_DEVICE_CTRL_CLOSE 0x04 /**< close device */ +#define RT_DEVICE_CTRL_NOTIFY_SET 0x05 /**< set notify func */ +#define RT_DEVICE_CTRL_SET_INT 0x06 /**< set interrupt */ +#define RT_DEVICE_CTRL_CLR_INT 0x07 /**< clear interrupt */ +#define RT_DEVICE_CTRL_GET_INT 0x08 /**< get interrupt status */ +#define RT_DEVICE_CTRL_CONSOLE_OFLAG 0x09 /**< get console open flag */ +#define RT_DEVICE_CTRL_MASK 0x1f /**< mask for contrl commands */ /** * device control */ -#define RT_DEVICE_CTRL_BASE(Type) ((RT_Device_Class_##Type + 1) * 0x100) +#define RT_DEVICE_CTRL_BASE(Type) ((RT_Device_Class_##Type + 1) * 0x100) -typedef struct rt_device *rt_device_t; + typedef struct rt_device *rt_device_t; #ifdef RT_USING_DEVICE_OPS -/** - * operations set for device object - */ -struct rt_device_ops -{ - /* common device interface */ - rt_err_t (*init) (rt_device_t dev); - rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag); - rt_err_t (*close) (rt_device_t dev); - rt_ssize_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size); - rt_ssize_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size); - rt_err_t (*control)(rt_device_t dev, int cmd, void *args); -}; + /** + * operations set for device object + */ + struct rt_device_ops + { + /* common device interface */ + rt_err_t (*init)(rt_device_t dev); + rt_err_t (*open)(rt_device_t dev, rt_uint16_t oflag); + rt_err_t (*close)(rt_device_t dev); + rt_ssize_t (*read)(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size); + rt_ssize_t (*write)(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size); + rt_err_t (*control)(rt_device_t dev, int cmd, void *args); + }; #endif /* RT_USING_DEVICE_OPS */ -/** - * WaitQueue structure - */ -struct rt_wqueue -{ - rt_uint32_t flag; - rt_list_t waiting_list; - struct rt_spinlock spinlock; -}; -typedef struct rt_wqueue rt_wqueue_t; + /** + * WaitQueue structure + */ + struct rt_wqueue + { + rt_uint32_t flag; + rt_list_t waiting_list; + struct rt_spinlock spinlock; + }; + typedef struct rt_wqueue rt_wqueue_t; #ifdef RT_USING_DM -struct rt_driver; -struct rt_bus; + struct rt_driver; + struct rt_bus; #endif -/** - * Device structure - */ -struct rt_device -{ - struct rt_object parent; /**< inherit from rt_object */ + /** + * Device structure + */ + struct rt_device + { + struct rt_object parent; /**< inherit from rt_object */ #ifdef RT_USING_DM - struct rt_bus *bus; /**< the bus mounting to */ - rt_list_t node; /**< to mount on bus */ - struct rt_driver *drv; /**< driver for powering the device */ + struct rt_bus *bus; /**< the bus mounting to */ + rt_list_t node; /**< to mount on bus */ + struct rt_driver *drv; /**< driver for powering the device */ #ifdef RT_USING_OFW - void *ofw_node; /**< ofw node get from device tree */ + void *ofw_node; /**< ofw node get from device tree */ #endif #endif - enum rt_device_class_type type; /**< device type */ - rt_uint16_t flag; /**< device flag */ - rt_uint16_t open_flag; /**< device open flag */ + enum rt_device_class_type type; /**< device type */ + rt_uint16_t flag; /**< device flag */ + rt_uint16_t open_flag; /**< device open flag */ - rt_uint8_t ref_count; /**< reference count */ - rt_uint8_t device_id; /**< 0 - 255 */ + rt_uint8_t ref_count; /**< reference count */ + rt_uint8_t device_id; /**< 0 - 255 */ - /* device call back */ - rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size); - rt_err_t (*tx_complete)(rt_device_t dev, void *buffer); + /* device call back */ + rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size); + rt_err_t (*tx_complete)(rt_device_t dev, void *buffer); #ifdef RT_USING_DEVICE_OPS - const struct rt_device_ops *ops; + const struct rt_device_ops *ops; #else - /* common device interface */ - rt_err_t (*init) (rt_device_t dev); - rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag); - rt_err_t (*close) (rt_device_t dev); - rt_ssize_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size); - rt_ssize_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size); - rt_err_t (*control)(rt_device_t dev, int cmd, void *args); + /* common device interface */ + rt_err_t (*init)(rt_device_t dev); + rt_err_t (*open)(rt_device_t dev, rt_uint16_t oflag); + rt_err_t (*close)(rt_device_t dev); + rt_ssize_t (*read)(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size); + rt_ssize_t (*write)(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size); + rt_err_t (*control)(rt_device_t dev, int cmd, void *args); #endif /* RT_USING_DEVICE_OPS */ #ifdef RT_USING_POSIX_DEVIO - const struct dfs_file_ops *fops; - struct rt_wqueue wait_queue; + const struct dfs_file_ops *fops; + struct rt_wqueue wait_queue; #endif /* RT_USING_POSIX_DEVIO */ - void *user_data; /**< device private data */ -}; + void *user_data; /**< device private data */ + }; -/** - * Notify structure - */ -struct rt_device_notify -{ - void (*notify)(rt_device_t dev); - struct rt_device *dev; -}; + /** + * Notify structure + */ + struct rt_device_notify + { + void (*notify)(rt_device_t dev); + struct rt_device *dev; + }; #ifdef RT_USING_SMART -struct rt_channel -{ - struct rt_ipc_object parent; /**< inherit from object */ - struct rt_thread *reply; /**< the thread will be reply */ - struct rt_spinlock slock; /**< spinlock of this channel */ - rt_list_t wait_msg; /**< the wait queue of sender msg */ - rt_list_t wait_thread; /**< the wait queue of sender thread */ - rt_wqueue_t reader_queue; /**< channel poll queue */ - rt_uint8_t stat; /**< the status of this channel */ - rt_ubase_t ref; -}; -typedef struct rt_channel *rt_channel_t; + struct rt_channel + { + struct rt_ipc_object parent; /**< inherit from object */ + struct rt_thread *reply; /**< the thread will be reply */ + struct rt_spinlock slock; /**< spinlock of this channel */ + rt_list_t wait_msg; /**< the wait queue of sender msg */ + rt_list_t wait_thread; /**< the wait queue of sender thread */ + rt_wqueue_t reader_queue; /**< channel poll queue */ + rt_uint8_t stat; /**< the status of this channel */ + rt_ubase_t ref; + }; + typedef struct rt_channel *rt_channel_t; #endif /**@}*/ @@ -1465,12 +1383,14 @@ typedef struct rt_channel *rt_channel_t; #ifdef __cplusplus /* RT-Thread definitions for C++ */ -namespace rtthread { +namespace rtthread +{ -enum TICK_WAIT { - WAIT_NONE = 0, - WAIT_FOREVER = -1, -}; + enum TICK_WAIT + { + WAIT_NONE = 0, + WAIT_FOREVER = -1, + }; } diff --git a/mkrtos_user/server/drv/rtthread_drv/inc/rtthread_inter.h b/mkrtos_user/server/drv/rtthread_drv/inc/rtthread_inter.h new file mode 100644 index 000000000..2164a468c --- /dev/null +++ b/mkrtos_user/server/drv/rtthread_drv/inc/rtthread_inter.h @@ -0,0 +1,4 @@ +#pragma once + +#include +rt_err_t rt_thread_bind_mkrtos(rt_thread_t th); diff --git a/mkrtos_user/server/drv/rtthread_drv/src/main.c b/mkrtos_user/server/drv/rtthread_drv/src/main.c index 7c19031cd..d51b13274 100644 --- a/mkrtos_user/server/drv/rtthread_drv/src/main.c +++ b/mkrtos_user/server/drv/rtthread_drv/src/main.c @@ -4,14 +4,16 @@ #include #include #include - +#include /* defined the LED0 pin: PC0 */ #define LED0_PIN GET_PIN(C, 0) extern void rt_hw_board_init(void); extern int dfs_init(void); +static struct rt_thread main_rtt; int main(void) { printf("test\n"); + rt_thread_bind_mkrtos(&main_rtt); /* init board */ rt_hw_board_init(); dfs_init(); diff --git a/mkrtos_user/server/drv/rtthread_drv/src/object.c b/mkrtos_user/server/drv/rtthread_drv/src/object.c new file mode 100644 index 000000000..e2d529e15 --- /dev/null +++ b/mkrtos_user/server/drv/rtthread_drv/src/object.c @@ -0,0 +1,635 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2006-03-14 Bernard the first version + * 2006-04-21 Bernard change the scheduler lock to interrupt lock + * 2006-05-18 Bernard fix the object init bug + * 2006-08-03 Bernard add hook support + * 2007-01-28 Bernard rename RT_OBJECT_Class_Static to RT_Object_Class_Static + * 2010-10-26 yi.qiu add module support in rt_object_allocate and rt_object_free + * 2017-12-10 Bernard Add object_info enum. + * 2018-01-25 Bernard Fix the object find issue when enable MODULE. + * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to object.c + * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable + */ + +#include +#include + +#ifdef RT_USING_MODULE +#include +#endif /* RT_USING_MODULE */ + +#ifdef RT_USING_SMART +#include +#endif + +struct rt_custom_object +{ + struct rt_object parent; + rt_err_t (*destroy)(void *); + void *data; +}; + +/* + * define object_info for the number of _object_container items. + */ +enum rt_object_info_type +{ + RT_Object_Info_Thread = 0, /**< The object is a thread. */ +#ifdef RT_USING_DEVICE + RT_Object_Info_Device, /**< The object is a device */ +#endif + RT_Object_Info_Unknown, /**< The object is unknown. */ +}; + +#define _OBJ_CONTAINER_LIST_INIT(c) \ + {&(_object_container[c].object_list), &(_object_container[c].object_list)} + +static struct rt_object_information _object_container[RT_Object_Info_Unknown] = +{ + {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread), RT_SPINLOCK_INIT}, +#ifdef RT_USING_DEVICE + /* initialize object container - device */ + {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device), RT_SPINLOCK_INIT}, +#endif +}; + +#ifndef __on_rt_object_attach_hook + #define __on_rt_object_attach_hook(obj) __ON_HOOK_ARGS(rt_object_attach_hook, (obj)) +#endif +#ifndef __on_rt_object_detach_hook + #define __on_rt_object_detach_hook(obj) __ON_HOOK_ARGS(rt_object_detach_hook, (obj)) +#endif +#ifndef __on_rt_object_trytake_hook + #define __on_rt_object_trytake_hook(parent) __ON_HOOK_ARGS(rt_object_trytake_hook, (parent)) +#endif +#ifndef __on_rt_object_take_hook + #define __on_rt_object_take_hook(parent) __ON_HOOK_ARGS(rt_object_take_hook, (parent)) +#endif +#ifndef __on_rt_object_put_hook + #define __on_rt_object_put_hook(parent) __ON_HOOK_ARGS(rt_object_put_hook, (parent)) +#endif + +#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR) +static void (*rt_object_attach_hook)(struct rt_object *object); +static void (*rt_object_detach_hook)(struct rt_object *object); +void (*rt_object_trytake_hook)(struct rt_object *object); +void (*rt_object_take_hook)(struct rt_object *object); +void (*rt_object_put_hook)(struct rt_object *object); + +/** + * @addtogroup Hook + */ + +/**@{*/ + +/** + * @brief This function will set a hook function, which will be invoked when object + * attaches to kernel object system. + * + * @param hook is the hook function. + */ +void rt_object_attach_sethook(void (*hook)(struct rt_object *object)) +{ + rt_object_attach_hook = hook; +} + +/** + * @brief This function will set a hook function, which will be invoked when object + * detaches from kernel object system. + * + * @param hook is the hook function + */ +void rt_object_detach_sethook(void (*hook)(struct rt_object *object)) +{ + rt_object_detach_hook = hook; +} + +/** + * @brief This function will set a hook function, which will be invoked when object + * is taken from kernel object system. + * + * The object is taken means: + * semaphore - semaphore is taken by thread + * mutex - mutex is taken by thread + * event - event is received by thread + * mailbox - mail is received by thread + * message queue - message is received by thread + * + * @param hook is the hook function. + */ +void rt_object_trytake_sethook(void (*hook)(struct rt_object *object)) +{ + rt_object_trytake_hook = hook; +} + +/** + * @brief This function will set a hook function, which will be invoked when object + * have been taken from kernel object system. + * + * The object have been taken means: + * semaphore - semaphore have been taken by thread + * mutex - mutex have been taken by thread + * event - event have been received by thread + * mailbox - mail have been received by thread + * message queue - message have been received by thread + * timer - timer is started + * + * @param hook the hook function. + */ +void rt_object_take_sethook(void (*hook)(struct rt_object *object)) +{ + rt_object_take_hook = hook; +} + +/** + * @brief This function will set a hook function, which will be invoked when object + * is put to kernel object system. + * + * @param hook is the hook function + */ +void rt_object_put_sethook(void (*hook)(struct rt_object *object)) +{ + rt_object_put_hook = hook; +} + +/**@}*/ +#endif /* RT_USING_HOOK */ + +/** + * @addtogroup KernelObject + */ + +/**@{*/ + +/** + * @brief This function will return the specified type of object information. + * + * @param type is the type of object, which can be + * RT_Object_Class_Thread/Semaphore/Mutex... etc + * + * @return the object type information or RT_NULL + */ +struct rt_object_information * +rt_object_get_information(enum rt_object_class_type type) +{ + int index; + + type = type& ~RT_Object_Class_Static; + + for (index = 0; index < RT_Object_Info_Unknown; index ++) + if (_object_container[index].type == type) return &_object_container[index]; + + return RT_NULL; +} +RTM_EXPORT(rt_object_get_information); + +/** + * @brief This function will return the length of object list in object container. + * + * @param type is the type of object, which can be + * RT_Object_Class_Thread/Semaphore/Mutex... etc + * + * @return the length of object list + */ +int rt_object_get_length(enum rt_object_class_type type) +{ + int count = 0; + rt_base_t level; + struct rt_list_node *node = RT_NULL; + struct rt_object_information *information = RT_NULL; + + information = rt_object_get_information((enum rt_object_class_type)type); + if (information == RT_NULL) return 0; + + level = rt_spin_lock_irqsave(&(information->spinlock)); + rt_list_for_each(node, &(information->object_list)) + { + count ++; + } + rt_spin_unlock_irqrestore(&(information->spinlock), level); + + return count; +} +RTM_EXPORT(rt_object_get_length); + +/** + * @brief This function will copy the object pointer of the specified type, + * with the maximum size specified by maxlen. + * + * @param type is the type of object, which can be + * RT_Object_Class_Thread/Semaphore/Mutex... etc + * + * @param pointers is the pointer will be saved to. + * + * @param maxlen is the maximum number of pointers can be saved. + * + * @return the copied number of object pointers. + */ +int rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers, int maxlen) +{ + int index = 0; + rt_base_t level; + + struct rt_object *object; + struct rt_list_node *node = RT_NULL; + struct rt_object_information *information = RT_NULL; + + if (maxlen <= 0) return 0; + + information = rt_object_get_information((enum rt_object_class_type)type); + if (information == RT_NULL) return 0; + + level = rt_spin_lock_irqsave(&(information->spinlock)); + /* retrieve pointer of object */ + rt_list_for_each(node, &(information->object_list)) + { + object = rt_list_entry(node, struct rt_object, list); + + pointers[index] = object; + index ++; + + if (index >= maxlen) break; + } + rt_spin_unlock_irqrestore(&(information->spinlock), level); + + return index; +} +RTM_EXPORT(rt_object_get_pointers); + +/** + * @brief This function will initialize an object and add it to object system + * management. + * + * @param object is the specified object to be initialized. + * + * @param type is the object type. + * + * @param name is the object name. In system, the object's name must be unique. + */ +void rt_object_init(struct rt_object *object, + enum rt_object_class_type type, + const char *name) +{ + rt_base_t level; +#ifdef RT_USING_DEBUG + struct rt_list_node *node = RT_NULL; +#endif + struct rt_object_information *information; +#ifdef RT_USING_MODULE + struct rt_dlmodule *module = dlmodule_self(); +#endif /* RT_USING_MODULE */ + + /* get object information */ + information = rt_object_get_information(type); + RT_ASSERT(information != RT_NULL); + +#ifdef RT_USING_DEBUG + /* check object type to avoid re-initialization */ + + /* enter critical */ + level = rt_spin_lock_irqsave(&(information->spinlock)); + /* try to find object */ + for (node = information->object_list.next; + node != &(information->object_list); + node = node->next) + { + struct rt_object *obj; + + obj = rt_list_entry(node, struct rt_object, list); + RT_ASSERT(obj != object); + } + /* leave critical */ + rt_spin_unlock_irqrestore(&(information->spinlock), level); +#endif + + /* initialize object's parameters */ + /* set object type to static */ + object->type = type | RT_Object_Class_Static; +#if RT_NAME_MAX > 0 + rt_strncpy(object->name, name, RT_NAME_MAX); /* copy name */ +#else + object->name = name; +#endif /* RT_NAME_MAX > 0 */ + + RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object)); + + level = rt_spin_lock_irqsave(&(information->spinlock)); + +#ifdef RT_USING_MODULE + if (module) + { + rt_list_insert_after(&(module->object_list), &(object->list)); + object->module_id = (void *)module; + } + else +#endif /* RT_USING_MODULE */ + { + /* insert object into information object list */ + rt_list_insert_after(&(information->object_list), &(object->list)); + } + rt_spin_unlock_irqrestore(&(information->spinlock), level); +} + +/** + * @brief This function will detach a static object from object system, + * and the memory of static object is not freed. + * + * @param object the specified object to be detached. + */ +void rt_object_detach(rt_object_t object) +{ + rt_base_t level; + struct rt_object_information *information; + + /* object check */ + RT_ASSERT(object != RT_NULL); + + RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object)); + + information = rt_object_get_information(object->type); + RT_ASSERT(information != RT_NULL); + + level = rt_spin_lock_irqsave(&(information->spinlock)); + /* remove from old list */ + rt_list_remove(&(object->list)); + rt_spin_unlock_irqrestore(&(information->spinlock), level); + + object->type = 0; +} + +#ifdef RT_USING_HEAP +/** + * @brief This function will allocate an object from object system. + * + * @param type is the type of object. + * + * @param name is the object name. In system, the object's name must be unique. + * + * @return object + */ +rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name) +{ + struct rt_object *object; + rt_base_t level; + struct rt_object_information *information; +#ifdef RT_USING_MODULE + struct rt_dlmodule *module = dlmodule_self(); +#endif /* RT_USING_MODULE */ + + RT_DEBUG_NOT_IN_INTERRUPT; + + /* get object information */ + information = rt_object_get_information(type); + RT_ASSERT(information != RT_NULL); + + object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size); + if (object == RT_NULL) + { + /* no memory can be allocated */ + return RT_NULL; + } + + /* clean memory data of object */ + rt_memset(object, 0x0, information->object_size); + + /* initialize object's parameters */ + + /* set object type */ + object->type = type; + + /* set object flag */ + object->flag = 0; + +#if RT_NAME_MAX > 0 + rt_strncpy(object->name, name, RT_NAME_MAX); /* copy name */ +#else + object->name = name; +#endif /* RT_NAME_MAX > 0 */ + + RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object)); + + level = rt_spin_lock_irqsave(&(information->spinlock)); + +#ifdef RT_USING_MODULE + if (module) + { + rt_list_insert_after(&(module->object_list), &(object->list)); + object->module_id = (void *)module; + } + else +#endif /* RT_USING_MODULE */ + { + /* insert object into information object list */ + rt_list_insert_after(&(information->object_list), &(object->list)); + } + rt_spin_unlock_irqrestore(&(information->spinlock), level); + + return object; +} + +/** + * @brief This function will delete an object and release object memory. + * + * @param object is the specified object to be deleted. + */ +void rt_object_delete(rt_object_t object) +{ + rt_base_t level; + struct rt_object_information *information; + + /* object check */ + RT_ASSERT(object != RT_NULL); + RT_ASSERT(!(object->type & RT_Object_Class_Static)); + + RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object)); + + + information = rt_object_get_information(object->type); + RT_ASSERT(information != RT_NULL); + + level = rt_spin_lock_irqsave(&(information->spinlock)); + + /* remove from old list */ + rt_list_remove(&(object->list)); + + rt_spin_unlock_irqrestore(&(information->spinlock), level); + + /* reset object type */ + object->type = RT_Object_Class_Null; + + /* free the memory of object */ + RT_KERNEL_FREE(object); +} +#endif /* RT_USING_HEAP */ + +/** + * @brief This function will judge the object is system object or not. + * + * @note Normally, the system object is a static object and the type + * of object set to RT_Object_Class_Static. + * + * @param object is the specified object to be judged. + * + * @return RT_TRUE if a system object, RT_FALSE for others. + */ +rt_bool_t rt_object_is_systemobject(rt_object_t object) +{ + /* object check */ + RT_ASSERT(object != RT_NULL); + + if (object->type & RT_Object_Class_Static) + return RT_TRUE; + + return RT_FALSE; +} + +/** + * @brief This function will return the type of object without + * RT_Object_Class_Static flag. + * + * @param object is the specified object to be get type. + * + * @return the type of object. + */ +rt_uint8_t rt_object_get_type(rt_object_t object) +{ + /* object check */ + RT_ASSERT(object != RT_NULL); + + return object->type & ~RT_Object_Class_Static; +} + +/** + * @brief This function will find specified name object from object + * container. + * + * @param name is the specified name of object. + * + * @param type is the type of object + * + * @return the found object or RT_NULL if there is no this object + * in object container. + * + * @note this function shall not be invoked in interrupt status. + */ +rt_object_t rt_object_find(const char *name, rt_uint8_t type) +{ + struct rt_object *object = RT_NULL; + struct rt_list_node *node = RT_NULL; + struct rt_object_information *information = RT_NULL; + rt_base_t level; + + information = rt_object_get_information((enum rt_object_class_type)type); + + /* parameter check */ + if ((name == RT_NULL) || (information == RT_NULL)) return RT_NULL; + + /* which is invoke in interrupt status */ + RT_DEBUG_NOT_IN_INTERRUPT; + + /* enter critical */ + level = rt_spin_lock_irqsave(&(information->spinlock)); + + /* try to find object */ + rt_list_for_each(node, &(information->object_list)) + { + object = rt_list_entry(node, struct rt_object, list); + if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0) + { + rt_spin_unlock_irqrestore(&(information->spinlock), level); + + return object; + } + } + + rt_spin_unlock_irqrestore(&(information->spinlock), level); + + return RT_NULL; +} + +/** + * @brief This function will return the name of the specified object container + * + * @param object the specified object to be get name + * @param name buffer to store the object name string + * @param name_size maximum size of the buffer to store object name + * + * @return -RT_EINVAL if any parameter is invalid or RT_EOK if the operation is successfully executed + * + * @note this function shall not be invoked in interrupt status + */ +rt_err_t rt_object_get_name(rt_object_t object, char *name, rt_uint8_t name_size) +{ + rt_err_t result = -RT_EINVAL; + if ((object != RT_NULL) && (name != RT_NULL) && (name_size != 0U)) + { + const char *obj_name = object->name; + (void) rt_strncpy(name, obj_name, (rt_size_t)name_size); + result = RT_EOK; + } + + return result; +} + +#ifdef RT_USING_HEAP +/** + * This function will create a custom object + * container. + * + * @param name the specified name of object. + * @param data the custom data + * @param data_destroy the custom object destroy callback + * + * @return the found object or RT_NULL if there is no this object + * in object container. + * + * @note this function shall not be invoked in interrupt status. + */ + +rt_object_t rt_custom_object_create(const char *name, void *data, rt_err_t (*data_destroy)(void *)) +{ + struct rt_custom_object *cobj = RT_NULL; + + cobj = (struct rt_custom_object *)rt_object_allocate(RT_Object_Class_Custom, name); + if (!cobj) + { + return RT_NULL; + } + cobj->destroy = data_destroy; + cobj->data = data; + return (struct rt_object *)cobj; +} + +/** + * This function will destroy a custom object + * container. + * + * @param obj the specified name of object. + * + * @note this function shall not be invoked in interrupt status. + */ +rt_err_t rt_custom_object_destroy(rt_object_t obj) +{ + rt_err_t ret = -1; + + struct rt_custom_object *cobj = (struct rt_custom_object *)obj; + + if (obj && obj->type == RT_Object_Class_Custom) + { + if (cobj->destroy) + { + ret = cobj->destroy(cobj->data); + } + rt_object_delete(obj); + } + return ret; +} +#endif + +/**@}*/ diff --git a/mkrtos_user/server/drv/rtthread_drv/src/rtthread_inter.c b/mkrtos_user/server/drv/rtthread_drv/src/rtthread_inter.c index 6fe000121..5d39a5bcb 100644 --- a/mkrtos_user/server/drv/rtthread_drv/src/rtthread_inter.c +++ b/mkrtos_user/server/drv/rtthread_drv/src/rtthread_inter.c @@ -8,6 +8,9 @@ #include #include #include +#include +#include + extern unsigned long get_thread_area(void); rt_weak void *rt_calloc(rt_size_t count, rt_size_t size) @@ -91,69 +94,40 @@ rt_uint16_t rt_critical_level(void) } rt_thread_t rt_thread_self(void) { - printf("[drv]%s:%d\n", __func__, __LINE__); - return NULL; /*TODO:*/ + ipc_msg_t *i_msg; + + thread_msg_buf_get(-1, (umword_t *)(&i_msg), NULL); + + return (rt_thread_t)(i_msg->user[1]); +} +rt_err_t rt_thread_bind_mkrtos(rt_thread_t th) +{ + ipc_msg_t *i_msg; + + pthread_mutex_lock(&th->suspend_lock); + th->tlist.next = th->tlist.prev = &th->tlist; + th->stat = 0; + + thread_msg_buf_get(-1, (umword_t *)(&i_msg), NULL); + i_msg->user[1] = (umword_t)th; } rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag) { + //! 这里锁两次,第二次加锁将会导致挂起 printf("[drv]%s:%d\n", __func__, __LINE__); - - return 0; /*TODO:*/ + thread->stat = RT_THREAD_SUSPEND; + pthread_mutex_lock(&thread->suspend_lock); + thread->stat = 0; + return 0; } rt_err_t rt_thread_resume(rt_thread_t thread) { + pthread_mutex_unlock(&thread->suspend_lock); + pthread_mutex_lock(&thread->suspend_lock); printf("[drv]%s:%d\n", __func__, __LINE__); - - return 0; /*TODO:*/ -} -rt_err_t rt_timer_start(rt_timer_t timer) -{ - printf("[drv]%s:%d\n", __func__, __LINE__); - - return 0; /*TODO:*/ -} -rt_err_t rt_timer_stop(rt_timer_t timer) -{ - printf("[drv]%s:%d\n", __func__, __LINE__); - - return 0; /*TODO:*/ -} -rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg) -{ - printf("[drv]%s:%d\n", __func__, __LINE__); - - return 0; /*TODO:*/ -} -rt_tick_t rt_timer_next_timeout_tick(void) -{ - printf("[drv]%s:%d\n", __func__, __LINE__); - - return 0; /*TODO:*/ -} -void rt_timer_check(void) -{ - printf("[drv]%s:%d\n", __func__, __LINE__); - - /*TODO:*/ -} -void rt_object_init(struct rt_object *object, - enum rt_object_class_type type, - const char *name) -{ - printf("[drv]%s:%d\n", __func__, __LINE__); - - /*TODO:*/ -} -rt_object_t rt_object_find(const char *name, rt_uint8_t type) -{ - printf("[drv]%s:%d\n", __func__, __LINE__); - - return NULL; /*TODO:*/ -} -rt_uint8_t rt_object_get_type(rt_object_t object) -{ return 0; } + void rt_set_errno(rt_err_t no) { errno = no; @@ -226,9 +200,202 @@ rt_err_t rt_mq_delete(rt_mq_t mq) /*TODO:*/ return -1; } -struct rt_object_information * -rt_object_get_information(enum rt_object_class_type type) +#include +#include +#include +#include +#include +#include +#include +#define STACK_SIZE 1024 + +static obj_handler_t timer_hd = HANDLER_INVALID; +static char timer_thmsg_buf[MSG_BUG_LEN]; +static __attribute__((aligned(8))) uint8_t timer_stack[STACK_SIZE]; + +#define TIMER_LIST_NR 16 //!< TODO:放到kconfig中 +static struct rt_timer *timer_list[TIMER_LIST_NR]; +static pthread_spinlock_t timer_lock; + +static struct rt_timer *timer_alloc(struct rt_timer *tim, void (*func_cb)(void *arg), size_t exp_times, void *data, uint8_t flags) { - /*TODO:*/ + pthread_spin_lock(&timer_lock); + for (int i = 0; i < TIMER_LIST_NR; i++) + { + if (timer_list[i] == 0) + { + timer_list[i] = tim; + timer_list[i]->inx = i; + timer_list[i]->data = data; + timer_list[i]->func_cb = func_cb; + timer_list[i]->exp_times = exp_times; + timer_list[i]->flags = flags; + pthread_spin_unlock(&timer_lock); + return timer_list[i]; + } + } + pthread_spin_unlock(&timer_lock); return NULL; -} \ No newline at end of file +} +static void timer_free(struct rt_timer *entry) +{ + assert(entry); + timer_list[entry->inx] = NULL; +} +static void timer_func(void) +{ + while (1) + { + usleep(1000); + rt_timer_check(); + } +} +void rt_timer_check(void) +{ + umword_t now_tick; + + now_tick = sys_read_tick(); + pthread_spin_lock(&timer_lock); + for (int i = 0; i < TIMER_LIST_NR; i++) + { + if (timer_list[i] && + timer_list[i]->start_times != 0 && + timer_list[i]->exp_times + timer_list[i]->start_times >= now_tick) + { + if ((timer_list[i]->flags & RT_TIMER_FLAG_ONE_SHOT) == 0) + { + pthread_spin_unlock(&timer_lock); + timer_list[i]->func_cb(timer_list[i]->data); + pthread_spin_lock(&timer_lock); + timer_list[i] = NULL; + } + else if (timer_list[i]->flags & RT_TIMER_FLAG_PERIODIC) + { + pthread_spin_unlock(&timer_lock); + timer_list[i]->func_cb(timer_list[i]->data); + pthread_spin_lock(&timer_lock); + if (timer_list[i]) + { + timer_list[i]->start_times = sys_read_tick(); + } + } + else + { + /*Error.*/ + } + } + } + pthread_spin_unlock(&timer_lock); +} +void rt_timer_init(rt_timer_t timer, + const char *name, + void (*timeout)(void *parameter), + void *parameter, + rt_tick_t time, + rt_uint8_t flag) +{ + assert(timeout); + if (timer_hd == HANDLER_INVALID) + { + int ret = u_thread_create(&timer_hd, timer_stack, STACK_SIZE, timer_thmsg_buf, timer_func, 2); + + if (ret < 0) + { + errno = ret; + return; + } + } + struct rt_timer *entry = timer_alloc(timer, timeout, time, parameter, flag); + + if (!entry) + { + errno = -ENOMEM; + return; + } + printf("%s name %s tick %d flag 0x%x.\n", name, time, flag); +} +rt_err_t rt_timer_start(rt_timer_t timer) +{ + timer->start_times = sys_read_tick(); + return 0; +} +rt_err_t rt_timer_stop(rt_timer_t timer) +{ + timer_free(timer); + return 0; +} +rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg) +{ + assert(timer); + switch (cmd) + { + case RT_TIMER_CTRL_SET_TIME: + timer->exp_times = *(rt_tick_t *)arg; + break; + case RT_TIMER_CTRL_GET_TIME: + *(rt_tick_t *)arg = timer->exp_times; + break; + case RT_TIMER_CTRL_GET_PARM: + *(void **)arg = timer->data; + break; + case RT_TIMER_CTRL_SET_PARM: + timer->data = arg; + break; + case RT_TIMER_CTRL_GET_FUNC: + *(void **)arg = (void *)timer->func_cb; + break; + + case RT_TIMER_CTRL_SET_FUNC: + timer->func_cb = (void (*)(void *))arg; + break; + case RT_TIMER_CTRL_GET_REMAIN_TIME: + *(rt_tick_t *)arg = timer->exp_times - (sys_read_tick() - timer->start_times); + break; + case RT_TIMER_CTRL_GET_STATE: + if (timer->start_times) + { + /*timer is start and run*/ + *(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED; + } + else + { + /*timer is stop*/ + *(rt_uint32_t *)arg = RT_TIMER_FLAG_DEACTIVATED; + } + break; + case RT_TIMER_CTRL_SET_ONESHOT: + timer->flags &= ~RT_TIMER_FLAG_PERIODIC; + break; + case RT_TIMER_CTRL_SET_PERIODIC: + timer->flags |= RT_TIMER_FLAG_PERIODIC; + break; + default: + return -EINVAL; + } + + return 0; +} +rt_tick_t rt_timer_next_timeout_tick(void) +{ + pthread_spin_lock(&timer_lock); + umword_t sys_tick; + int min = INT32_MAX; + + sys_tick = sys_read_tick(); + for (int i = 0; i < TIMER_LIST_NR; i++) + { + if (timer_list[i] && + timer_list[i]->start_times != 0) + { + int val = timer_list[i]->exp_times + timer_list[i]->start_times - sys_tick; + + val = val < 0 ? 0 : val; + if (val < min) + { + min = val; + } + } + } + pthread_spin_unlock(&timer_lock); + return min; +}