71#include "klibc/kerrno.h"
84#define RT_VERSION_MAJOR 5
85#define RT_VERSION_MINOR 2
86#define RT_VERSION_PATCH 0
89#define RT_VERSION_CHECK(major, minor, revise) ((major * 10000U) + (minor * 100U) + revise)
92#define RTTHREAD_VERSION RT_VERSION_CHECK(RT_VERSION_MAJOR, RT_VERSION_MINOR, RT_VERSION_PATCH)
98#define RT_UINT8_MAX UINT8_MAX
99#define RT_UINT16_MAX UINT16_MAX
100#define RT_UINT32_MAX UINT32_MAX
101#define RT_UINT64_MAX UINT64_MAX
103#define RT_UINT8_MAX 0xFFU
104#define RT_UINT16_MAX 0xFFFFU
105#define RT_UINT32_MAX 0xFFFFFFFFUL
106#define RT_UINT64_MAX 0xFFFFFFFFFFFFFFFFULL
109#define RT_TICK_MAX RT_UINT32_MAX
112#define RT_SEM_VALUE_MAX RT_UINT16_MAX
113#define RT_MUTEX_VALUE_MAX RT_UINT16_MAX
114#define RT_MUTEX_HOLD_MAX RT_UINT8_MAX
115#define RT_MB_ENTRY_MAX RT_UINT16_MAX
116#define RT_MQ_ENTRY_MAX RT_UINT16_MAX
120#define RT_UNUSED(x) ((void)(x))
123#define RT_STATIC_ASSERT(name, expn) typedef char _static_assert_##name[(expn)?1:-1]
129#ifdef RT_USING_COMPONENTS_INIT
130typedef int (*init_fn_t)(void);
132#pragma section("rti_fn$f",read)
133 #ifdef RT_DEBUGING_AUTO_INIT
140 #define INIT_EXPORT(fn, level) \
141 const char __rti_level_##fn[] = ".rti_fn." level; \
142 const char __rti_##fn##_name[] = #fn; \
143 __declspec(allocate("rti_fn$f")) \
144 rt_used const struct rt_init_desc __rt_init_msc_##fn = \
145 {__rti_level_##fn, fn, __rti_##fn##_name};
152 #define INIT_EXPORT(fn, level) \
153 const char __rti_level_##fn[] = ".rti_fn." level; \
154 __declspec(allocate("rti_fn$f")) \
155 rt_used const struct rt_init_desc __rt_init_msc_##fn = \
156 {__rti_level_##fn, fn };
159 #ifdef RT_DEBUGING_AUTO_INIT
165 #define INIT_EXPORT(fn, level) \
166 const char __rti_##fn##_name[] = #fn; \
167 rt_used const struct rt_init_desc __rt_init_desc_##fn rt_section(".rti_fn." level) = \
168 { __rti_##fn##_name, fn};
170 #define INIT_EXPORT(fn, level) \
171 rt_used const init_fn_t __rt_init_##fn rt_section(".rti_fn." level) = fn
175#define INIT_EXPORT(fn, level)
179#define INIT_BOARD_EXPORT(fn) INIT_EXPORT(fn, "1")
182#define INIT_CORE_EXPORT(fn) INIT_EXPORT(fn, "1.0")
184#define INIT_SUBSYS_EXPORT(fn) INIT_EXPORT(fn, "1.1")
186#define INIT_PLATFORM_EXPORT(fn) INIT_EXPORT(fn, "1.2")
190#define INIT_PREV_EXPORT(fn) INIT_EXPORT(fn, "2")
192#define INIT_DEVICE_EXPORT(fn) INIT_EXPORT(fn, "3")
194#define INIT_COMPONENT_EXPORT(fn) INIT_EXPORT(fn, "4")
196#define INIT_ENV_EXPORT(fn) INIT_EXPORT(fn, "5")
198#define INIT_APP_EXPORT(fn) INIT_EXPORT(fn, "6")
201#define INIT_FS_EXPORT(fn) INIT_EXPORT(fn, "6.0")
203#define INIT_SECONDARY_CPU_EXPORT(fn) INIT_EXPORT(fn, "7")
205#if !defined(RT_USING_FINSH)
207#define FINSH_FUNCTION_EXPORT(name, desc)
208#define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc)
210#define MSH_CMD_EXPORT(command, desc)
211#define MSH_CMD_EXPORT_ALIAS(command, alias, desc)
212#elif !defined(FINSH_USING_SYMTAB)
213#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc)
217#define RT_EVENT_LENGTH 32
220#define RT_MM_PAGE_SIZE 4096
221#define RT_MM_PAGE_MASK (RT_MM_PAGE_SIZE - 1)
222#define RT_MM_PAGE_BITS 12
225#ifndef RT_KERNEL_MALLOC
226#define RT_KERNEL_MALLOC(sz) rt_malloc(sz)
229#ifndef RT_KERNEL_FREE
230#define RT_KERNEL_FREE(ptr) rt_free(ptr)
233#ifndef RT_KERNEL_REALLOC
234#define RT_KERNEL_REALLOC(ptr, size) rt_realloc(ptr, size)
246#define RT_IS_ALIGN(addr, align) ((!(addr & (align - 1))) && (addr != RT_NULL))
255#define RT_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))
264#define RT_ALIGN_DOWN(size, align) ((size) & ~((align) - 1))
275#define RT_OBJECT_FLAG_MODULE 0x80
283 char name[RT_NAME_MAX];
290#ifdef RT_USING_MODULE
366#define RT_OBJECT_HOOK_CALL(func, argv)
381#define _RT_OBJECT_HOOK_CALL(func, argv) __ON_HOOK_ARGS(func, argv)
382#define RT_OBJECT_HOOK_CALL(func, argv) _RT_OBJECT_HOOK_CALL(func, argv)
384 #ifdef RT_HOOK_USING_FUNC_PTR
385 #define __ON_HOOK_ARGS(__hook, argv) do {if ((__hook) != RT_NULL) __hook argv; } while (0)
387 #define __ON_HOOK_ARGS(__hook, argv)
391#ifdef RT_USING_HOOKLIST
404#define RT_OBJECT_HOOKLIST_DECLARE(handler_type, name) \
405 typedef struct name##_hooklistnode \
407 handler_type handler; \
408 rt_list_t list_node; \
409 } *name##_hooklistnode_t; \
410 extern volatile rt_ubase_t name##_nested; \
411 void name##_sethook(name##_hooklistnode_t node); \
412 void name##_rmhook(name##_hooklistnode_t node)
433#define RT_OBJECT_HOOKLIST_DEFINE_NODE(hookname, nodename, hooker_handler) \
434 struct hookname##_hooklistnode nodename = { \
435 .handler = hooker_handler, \
436 .list_node = RT_LIST_OBJECT_INIT(nodename.list_node), \
443#define RT_OBJECT_HOOKLIST_DEFINE(name) \
444 static rt_list_t name##_hooklist = RT_LIST_OBJECT_INIT(name##_hooklist); \
445 static struct rt_spinlock name##lock = RT_SPINLOCK_INIT; \
446 volatile rt_ubase_t name##_nested = 0; \
447 void name##_sethook(name##_hooklistnode_t node) \
449 rt_ubase_t level = rt_spin_lock_irqsave(&name##lock); \
450 while (name##_nested) \
452 rt_spin_unlock_irqrestore(&name##lock, level); \
453 level = rt_spin_lock_irqsave(&name##lock); \
455 rt_list_insert_before(&name##_hooklist, &node->list_node); \
456 rt_spin_unlock_irqrestore(&name##lock, level); \
458 void name##_rmhook(name##_hooklistnode_t node) \
460 rt_ubase_t level = rt_spin_lock_irqsave(&name##lock); \
461 while (name##_nested) \
463 rt_spin_unlock_irqrestore(&name##lock, level); \
464 level = rt_spin_lock_irqsave(&name##lock); \
466 rt_list_remove(&node->list_node); \
467 rt_spin_unlock_irqrestore(&name##lock, level); \
483#define _RT_OBJECT_HOOKLIST_CALL(nodetype, nested, list, lock, argv) \
486 nodetype iter, next; \
487 rt_ubase_t level = rt_spin_lock_irqsave(&lock); \
489 rt_spin_unlock_irqrestore(&lock, level); \
490 if (!rt_list_isempty(&list)) \
492 rt_list_for_each_entry_safe(iter, next, &list, list_node) \
494 iter->handler argv; \
497 level = rt_spin_lock_irqsave(&lock); \
499 rt_spin_unlock_irqrestore(&lock, level); \
501#define RT_OBJECT_HOOKLIST_CALL(name, argv) \
502 _RT_OBJECT_HOOKLIST_CALL(name##_hooklistnode_t, name##_nested, \
503 name##_hooklist, name##lock, argv)
507#define RT_OBJECT_HOOKLIST_DECLARE(handler_type, name)
508#define RT_OBJECT_HOOKLIST_DEFINE_NODE(hookname, nodename, hooker_handler)
509#define RT_OBJECT_HOOKLIST_DEFINE(name)
510#define RT_OBJECT_HOOKLIST_CALL(name, argv)
524#define RT_TIMER_FLAG_DEACTIVATED 0x0
525#define RT_TIMER_FLAG_ACTIVATED 0x1
526#define RT_TIMER_FLAG_ONE_SHOT 0x0
527#define RT_TIMER_FLAG_PERIODIC 0x2
529#define RT_TIMER_FLAG_HARD_TIMER 0x0
530#define RT_TIMER_FLAG_SOFT_TIMER 0x4
531#define RT_TIMER_FLAG_THREAD_TIMER \
532 (0x8 | RT_TIMER_FLAG_HARD_TIMER)
534#define RT_TIMER_CTRL_SET_TIME 0x0
535#define RT_TIMER_CTRL_GET_TIME 0x1
536#define RT_TIMER_CTRL_SET_ONESHOT 0x2
537#define RT_TIMER_CTRL_SET_PERIODIC 0x3
538#define RT_TIMER_CTRL_GET_STATE 0x4
539#define RT_TIMER_CTRL_GET_REMAIN_TIME 0x5
540#define RT_TIMER_CTRL_GET_FUNC 0x6
541#define RT_TIMER_CTRL_SET_FUNC 0x7
542#define RT_TIMER_CTRL_GET_PARM 0x8
543#define RT_TIMER_CTRL_SET_PARM 0x9
545#ifndef RT_TIMER_SKIP_LIST_LEVEL
546#define RT_TIMER_SKIP_LIST_LEVEL 1
550#ifndef RT_TIMER_SKIP_LIST_MASK
551#define RT_TIMER_SKIP_LIST_MASK 0x3
583#ifdef RT_USING_SIGNALS
585typedef unsigned long rt_sigset_t;
586typedef siginfo_t rt_siginfo_t;
587typedef void (*rt_sighandler_t)(
int signo);
604#define RT_THREAD_INIT 0x00
605#define RT_THREAD_CLOSE 0x01
606#define RT_THREAD_READY 0x02
607#define RT_THREAD_RUNNING 0x03
619#define RT_THREAD_SUSPEND_MASK 0x04
620#define RT_SIGNAL_COMMON_WAKEUP_MASK 0x02
621#define RT_SIGNAL_KILL_WAKEUP_MASK 0x01
623#define RT_THREAD_SUSPEND_INTERRUPTIBLE (RT_THREAD_SUSPEND_MASK)
624#define RT_THREAD_SUSPEND RT_THREAD_SUSPEND_INTERRUPTIBLE
625#define RT_THREAD_SUSPEND_KILLABLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK)
626#define RT_THREAD_SUSPEND_UNINTERRUPTIBLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK | RT_SIGNAL_KILL_WAKEUP_MASK)
627#define RT_THREAD_STAT_MASK 0x07
629#define RT_THREAD_STAT_YIELD 0x08
630#define RT_THREAD_STAT_YIELD_MASK RT_THREAD_STAT_YIELD
632#define RT_THREAD_STAT_SIGNAL 0x10
633#define RT_THREAD_STAT_SIGNAL_READY (RT_THREAD_STAT_SIGNAL | RT_THREAD_READY)
634#define RT_THREAD_STAT_SIGNAL_WAIT 0x20
635#define RT_THREAD_STAT_SIGNAL_PENDING 0x40
636#define RT_THREAD_STAT_SIGNAL_MASK 0xf0
641#define RT_THREAD_CTRL_STARTUP 0x00
642#define RT_THREAD_CTRL_CLOSE 0x01
643#define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02
644#define RT_THREAD_CTRL_INFO 0x03
645#define RT_THREAD_CTRL_BIND_CPU 0x04
661#define RT_CPU_DETACHED RT_CPUS_NR
662#define RT_CPU_MASK ((1 << RT_CPUS_NR) - 1)
664#ifndef RT_SCHEDULE_IPI
665#define RT_SCHEDULE_IPI 0
672#ifndef RT_SMP_CALL_IPI
673#define RT_SMP_CALL_IPI 2
678#define _SCHEDULER_CONTEXT(fileds) fileds
696#ifndef ARCH_USING_HW_THREAD_SELF
701 rt_list_t priority_table[RT_THREAD_PRIORITY_MAX];
702 #if RT_THREAD_PRIORITY_MAX > 32
718#ifdef RT_USING_CPU_USAGE_TRACER
721#ifdef ARCH_USING_IRQ_CTX_LIST
732#ifdef RT_USING_CPU_USAGE_TRACER
735#ifdef ARCH_USING_IRQ_CTX_LIST
744#define rt_current_thread rt_thread_self()
763 rt_wakeup_func_t func;
770#define _LWP_NSIG_BPW 64
772#define _LWP_NSIG_BPW 32
775#define _LWP_NSIG_WORDS (RT_ALIGN(_LWP_NSIG, _LWP_NSIG_BPW) / _LWP_NSIG_BPW)
777typedef void (*lwp_sighandler_t)(int);
778typedef void (*lwp_sigaction_t)(
int signo, siginfo_t *info,
void *context);
781 unsigned long sig[_LWP_NSIG_WORDS];
785#define lwp_sigmask(signo) ((lwp_sigset_t){.sig = {[0] = ((long)(1u << ((signo)-1)))}})
786#define lwp_sigset_init(mask) ((lwp_sigset_t){.sig = {[0] = (long)(mask)}})
789struct lwp_sigaction {
791 void (*_sa_handler)(int);
792 void (*_sa_sigaction)(int, siginfo_t *,
void *);
794 lwp_sigset_t sa_mask;
796 void (*sa_restorer)(void);
799typedef struct lwp_siginfo_ext {
810typedef struct lwp_siginfo {
822 struct lwp_siginfo_ext *ext;
825typedef struct lwp_sigqueue {
827 lwp_sigset_t sigset_pending;
830struct lwp_thread_signal {
831 lwp_sigset_t sigset_mask;
832 struct lwp_sigqueue sig_queue;
835struct rt_user_context
885#ifdef RT_USING_SIGNALS
886 rt_sigset_t sig_pending;
887 rt_sigset_t sig_mask;
892 rt_sighandler_t *sig_vectors;
896#ifdef RT_USING_CPU_USAGE
900#ifdef RT_USING_PTHREADS
916 struct lwp_thread_signal signal;
917 struct rt_user_context user_ctx;
918 struct rt_wakeup wakeup_handle;
926 lwp_sighandler_t signal_handler[32];
929 int debug_attach_req;
932 struct rt_hw_exp_stack *regs;
934 int *clear_child_tid;
938#ifdef RT_USING_CPU_USAGE_TRACER
943#ifdef RT_USING_MEM_PROTECTION
945#ifdef RT_USING_HW_STACK_GUARD
956#define LWP_IS_USER_MODE(t) ((t)->user_ctx.ctx == RT_NULL)
958#define LWP_IS_USER_MODE(t) (0)
972#define RT_IPC_FLAG_FIFO 0x00
973#define RT_IPC_FLAG_PRIO 0x01
975#define RT_IPC_CMD_UNKNOWN 0x00
976#define RT_IPC_CMD_RESET 0x01
977#define RT_IPC_CMD_GET_STATE 0x02
978#define RT_IPC_CMD_SET_VLIMIT 0x03
980#define RT_WAITING_FOREVER -1
981#define RT_WAITING_NO 0
998#ifdef RT_USING_SEMAPHORE
1020#ifdef RT_USING_MUTEX
1047#ifdef RT_USING_EVENT
1051#define RT_EVENT_FLAG_AND 0x01
1052#define RT_EVENT_FLAG_OR 0x02
1053#define RT_EVENT_FLAG_CLEAR 0x04
1075#ifdef RT_USING_MAILBOX
1104#ifdef RT_USING_MESSAGEQUEUE
1160#ifdef RT_USING_SMALL_MEM
1168#ifdef RT_USING_MEMHEAP
1172struct rt_memheap_item
1175 struct rt_memheap *pool_ptr;
1177 struct rt_memheap_item *next;
1178 struct rt_memheap_item *prev;
1180 struct rt_memheap_item *next_free;
1181 struct rt_memheap_item *prev_free;
1182#ifdef RT_USING_MEMTRACE
1192 struct rt_object parent;
1200 struct rt_memheap_item *block_list;
1202 struct rt_memheap_item *free_list;
1203 struct rt_memheap_item free_header;
1205 struct rt_semaphore lock;
1210#ifdef RT_USING_MEMPOOL
1235#ifdef RT_USING_DEVICE
1284#define RT_DEVICE_FLAG_DEACTIVATE 0x000
1286#define RT_DEVICE_FLAG_RDONLY 0x001
1287#define RT_DEVICE_FLAG_WRONLY 0x002
1288#define RT_DEVICE_FLAG_RDWR 0x003
1290#define RT_DEVICE_FLAG_REMOVABLE 0x004
1291#define RT_DEVICE_FLAG_STANDALONE 0x008
1292#define RT_DEVICE_FLAG_ACTIVATED 0x010
1293#define RT_DEVICE_FLAG_SUSPENDED 0x020
1294#define RT_DEVICE_FLAG_STREAM 0x040
1295#define RT_DEVICE_FLAG_DYNAMIC 0x080
1297#define RT_DEVICE_FLAG_INT_RX 0x100
1298#define RT_DEVICE_FLAG_DMA_RX 0x200
1299#define RT_DEVICE_FLAG_INT_TX 0x400
1300#define RT_DEVICE_FLAG_DMA_TX 0x800
1302#define RT_DEVICE_OFLAG_CLOSE 0x000
1303#define RT_DEVICE_OFLAG_RDONLY 0x001
1304#define RT_DEVICE_OFLAG_WRONLY 0x002
1305#define RT_DEVICE_OFLAG_RDWR 0x003
1306#define RT_DEVICE_OFLAG_OPEN 0x008
1307#define RT_DEVICE_OFLAG_MASK 0xf0f
1315#define RT_DEVICE_CTRL_RESUME 0x01
1316#define RT_DEVICE_CTRL_SUSPEND 0x02
1317#define RT_DEVICE_CTRL_CONFIG 0x03
1318#define RT_DEVICE_CTRL_CLOSE 0x04
1319#define RT_DEVICE_CTRL_NOTIFY_SET 0x05
1320#define RT_DEVICE_CTRL_SET_INT 0x06
1321#define RT_DEVICE_CTRL_CLR_INT 0x07
1322#define RT_DEVICE_CTRL_GET_INT 0x08
1323#define RT_DEVICE_CTRL_CONSOLE_OFLAG 0x09
1324#define RT_DEVICE_CTRL_MASK 0x1f
1329#define RT_DEVICE_CTRL_BASE(Type) ((RT_Device_Class_##Type + 1) * 0x100)
1334#ifdef RT_USING_DEVICE_OPS
1376 struct rt_driver *drv;
1380 void *power_domain_unit;
1382 const void *dma_ops;
1400#ifdef RT_USING_DEVICE_OPS
1401 const struct rt_device_ops *ops;
1412#ifdef RT_USING_POSIX_DEVIO
1432#ifdef RT_USING_SMART
1444typedef struct rt_channel *rt_channel_t;
void(* rt_timer_func_t)(void *parameter)
struct rt_timer * rt_timer_t
#define RT_TIMER_SKIP_LIST_LEVEL
struct rt_device * rt_device_t
struct rt_driver * rt_driver_t
struct rt_wqueue rt_wqueue_t
@ RT_Device_Class_SPIDevice
@ RT_Device_Class_Miscellaneous
@ RT_Device_Class_Graphic
@ RT_Device_Class_USBDevice
@ RT_Device_Class_Security
@ RT_Device_Class_USBHost
@ RT_Device_Class_Unknown
ssize_t write(int fd, const void *buf, size_t len)
ssize_t read(int fd, void *buf, size_t len)
int open(const char *file, int flags,...)
int stat(const char *file, struct stat *buf)
struct rt_object * rt_object_t
rt_err_t(* rt_object_iter_t)(rt_object_t object, void *data)
@ RT_Object_Class_MemHeap
@ RT_Object_Class_MessageQueue
@ RT_Object_Class_ProcessGroup
@ RT_Object_Class_MemPool
@ RT_Object_Class_MailBox
@ RT_Object_Class_Unknown
@ RT_Object_Class_Channel
@ RT_Object_Class_Semaphore
@ RT_Object_Class_Session
struct rt_mempool * rt_mp_t
struct rt_memory * rt_mem_t
struct rt_cpu_usage_stats * rt_cpu_usage_stats_t
void(* rt_thread_cleanup_t)(struct rt_thread *tid)
struct rt_thread * rt_thread_t
struct rt_interrupt_context * rt_interrupt_context_t
struct rt_event * rt_event_t
struct rt_mailbox * rt_mailbox_t
struct rt_messagequeue * rt_mq_t
struct rt_mutex * rt_mutex_t
struct rt_semaphore * rt_sem_t
#define RT_SCHED_THREAD_CTX
unsigned short rt_uint16_t
struct rt_list_node rt_list_t
unsigned long long rt_uint64_t
struct rt_slist_node rt_slist_t
struct rt_thread * current_thread
struct rt_thread * idle_thread
void(* notify)(rt_device_t dev)
enum rt_device_class_type type
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(* write)(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
rt_ssize_t(* read)(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
rt_err_t(* control)(rt_device_t dev, int cmd, void *args)
rt_err_t(* tx_complete)(rt_device_t dev, void *buffer)
rt_err_t(* rx_indicate)(rt_device_t dev, rt_size_t size)
rt_err_t(* readlink)(rt_device_t dev, char *buf, int len)
struct rt_spinlock spinlock
struct rt_ipc_object parent
rt_list_t suspend_sender_thread
struct rt_ipc_object parent
struct rt_spinlock spinlock
struct rt_spinlock spinlock
rt_size_t block_total_count
rt_size_t block_free_count
struct rt_ipc_object parent
rt_list_t suspend_sender_thread
struct rt_spinlock spinlock
struct rt_spinlock spinlock
rt_uint8_t ceiling_priority
struct rt_ipc_object parent
struct rt_spinlock spinlock
struct rt_ipc_object parent
struct rt_spinlock spinlock
rt_object_t pending_object
rt_thread_cleanup_t cleanup
RT_SCHED_THREAD_CTX struct rt_timer thread_timer
rt_list_t taken_object_list
rt_timer_func_t timeout_func
rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL]
struct rt_spinlock spinlock