rtdef.h 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. /**
  2. * RT-Thread RuiChing
  3. *
  4. * COPYRIGHT (C) 2024-2025 Shanghai Real-Thread Electronic Technology Co., Ltd.
  5. * All rights reserved.
  6. *
  7. * The license and distribution terms for this file may be
  8. * found in the file LICENSE in this distribution.
  9. */
  10. #ifndef __RTDEF_H__
  11. #define __RTDEF_H__
  12. #include <rtsched.h>
  13. #include <rttypes.h>
  14. #include <klibc/kerrno.h>
  15. /**
  16. * @addtogroup BasicDef
  17. */
  18. /**@{*/
  19. /* RT-Thread version information */
  20. #define RT_VERSION_MAJOR 5 /**< Major version number (X.x.x) */
  21. #define RT_VERSION_MINOR 2 /**< Minor version number (x.X.x) */
  22. #define RT_VERSION_PATCH 0 /**< Patch version number (x.x.X) */
  23. /* e.g. #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(4, 1, 0) */
  24. #define RT_VERSION_CHECK(major, minor, revise) ((major * 10000U) + (minor * 100U) + revise)
  25. /* RT-Thread version */
  26. #define RTTHREAD_VERSION RT_VERSION_CHECK(RT_VERSION_MAJOR, RT_VERSION_MINOR, RT_VERSION_PATCH)
  27. /**@}*/
  28. /* maximum value of base type */
  29. #ifdef RT_USING_LIBC
  30. #define RT_UINT8_MAX UINT8_MAX /**< Maximum number of UINT8 */
  31. #define RT_UINT16_MAX UINT16_MAX /**< Maximum number of UINT16 */
  32. #define RT_UINT32_MAX UINT32_MAX /**< Maximum number of UINT32 */
  33. #define RT_UINT64_MAX UINT64_MAX /**< Maximum number of UINT64 */
  34. #else
  35. #define RT_UINT8_MAX 0xFFU /**< Maximum number of UINT8 */
  36. #define RT_UINT16_MAX 0xFFFFU /**< Maximum number of UINT16 */
  37. #define RT_UINT32_MAX 0xFFFFFFFFUL /**< Maximum number of UINT32 */
  38. #define RT_UINT64_MAX 0xFFFFFFFFFFFFFFFFULL /**< Maximum number of UINT64 */
  39. #endif /* RT_USING_LIBC */
  40. #define RT_TICK_MAX RT_UINT32_MAX /**< Maximum number of tick */
  41. /* maximum value of ipc type */
  42. #define RT_SEM_VALUE_MAX RT_UINT16_MAX /**< Maximum number of semaphore .value */
  43. #define RT_MUTEX_VALUE_MAX RT_UINT16_MAX /**< Maximum number of mutex .value */
  44. #define RT_MUTEX_HOLD_MAX RT_UINT8_MAX /**< Maximum number of mutex .hold */
  45. #define RT_MB_ENTRY_MAX RT_UINT16_MAX /**< Maximum number of mailbox .entry */
  46. #define RT_MQ_ENTRY_MAX RT_UINT16_MAX /**< Maximum number of message queue .entry */
  47. /* Common Utilities */
  48. #define RT_UNUSED(x) ((void)(x))
  49. /* compile time assertion */
  50. #define RT_STATIC_ASSERT(name, expn) typedef char _static_assert_##name[(expn)?1:-1]
  51. /* Compiler Related Definitions */
  52. #include "rtcompiler.h"
  53. /* initialization export */
  54. #ifdef RT_USING_COMPONENTS_INIT
  55. typedef int (*init_fn_t)(void);
  56. #ifdef _MSC_VER
  57. #pragma section("rti_fn$f",read)
  58. #ifdef RT_DEBUGING_AUTO_INIT
  59. struct rt_init_desc
  60. {
  61. const char* level;
  62. const init_fn_t fn;
  63. const char* fn_name;
  64. };
  65. #define INIT_EXPORT(fn, level) \
  66. const char __rti_level_##fn[] = ".rti_fn." level; \
  67. const char __rti_##fn##_name[] = #fn; \
  68. __declspec(allocate("rti_fn$f")) \
  69. rt_used const struct rt_init_desc __rt_init_msc_##fn = \
  70. {__rti_level_##fn, fn, __rti_##fn##_name};
  71. #else
  72. struct rt_init_desc
  73. {
  74. const char* level;
  75. const init_fn_t fn;
  76. };
  77. #define INIT_EXPORT(fn, level) \
  78. const char __rti_level_##fn[] = ".rti_fn." level; \
  79. __declspec(allocate("rti_fn$f")) \
  80. rt_used const struct rt_init_desc __rt_init_msc_##fn = \
  81. {__rti_level_##fn, fn };
  82. #endif /* RT_DEBUGING_AUTO_INIT */
  83. #else
  84. #ifdef RT_DEBUGING_AUTO_INIT
  85. struct rt_init_desc
  86. {
  87. const char* fn_name;
  88. const init_fn_t fn;
  89. };
  90. #define INIT_EXPORT(fn, level) \
  91. const char __rti_##fn##_name[] = #fn; \
  92. rt_used const struct rt_init_desc __rt_init_desc_##fn rt_section(".rti_fn." level) = \
  93. { __rti_##fn##_name, fn};
  94. #else
  95. #define INIT_EXPORT(fn, level) \
  96. rt_used const init_fn_t __rt_init_##fn rt_section(".rti_fn." level) = fn
  97. #endif /* RT_DEBUGING_AUTO_INIT */
  98. #endif /* _MSC_VER */
  99. #else
  100. #define INIT_EXPORT(fn, level)
  101. #endif /* RT_USING_COMPONENTS_INIT */
  102. /* board init routines will be called in board_init() function */
  103. #define INIT_BOARD_EXPORT(fn) INIT_EXPORT(fn, "1")
  104. /* init cpu, memory, interrupt-controller, bus... */
  105. #define INIT_CORE_EXPORT(fn) INIT_EXPORT(fn, "1.0")
  106. /* init sys-timer, clk, pinctrl... */
  107. #define INIT_FRAMEWORK_EXPORT(fn) INIT_EXPORT(fn, "1.1")
  108. /* init platform, user code... */
  109. #define INIT_PLATFORM_EXPORT(fn) INIT_EXPORT(fn, "1.2.0")
  110. /* init fix-clock, clock... */
  111. #define INIT_FIX_CLK_EXPORT(fn) INIT_EXPORT(fn, "1.2.1")
  112. #define INIT_CLK_EXPORT(fn) INIT_EXPORT(fn, "1.2.2")
  113. #define INIT_SUBSYS_EARLY_EXPORT(fn) INIT_EXPORT(fn, "1.3.0")
  114. #define INIT_SUBSYS_EXPORT(fn) INIT_EXPORT(fn, "1.3.1")
  115. #define INIT_DRIVER_EARLY_EXPORT(fn) INIT_EXPORT(fn, "1.4")
  116. /* pre/device/component/env/app init routines will be called in init_thread */
  117. /* components pre-initialization (pure software initialization) */
  118. #define INIT_PREV_EXPORT(fn) INIT_EXPORT(fn, "2")
  119. /* device initialization */
  120. #define INIT_DEVICE_EXPORT(fn) INIT_EXPORT(fn, "3")
  121. /* components initialization (dfs, lwip, ...) */
  122. #define INIT_COMPONENT_EXPORT(fn) INIT_EXPORT(fn, "4")
  123. /* environment initialization (mount disk, ...) */
  124. #define INIT_ENV_EXPORT(fn) INIT_EXPORT(fn, "5")
  125. /* application initialization (rtgui application etc ...) */
  126. #define INIT_APP_EXPORT(fn) INIT_EXPORT(fn, "6")
  127. /* init after mount fs */
  128. #define INIT_FS_EXPORT(fn) INIT_EXPORT(fn, "6.0")
  129. /* init in secondary_cpu_c_start */
  130. #define INIT_SECONDARY_CPU_EXPORT(fn) INIT_EXPORT(fn, "7")
  131. #if !defined(RT_USING_FINSH)
  132. /* define these to empty, even if not include finsh.h file */
  133. #define FINSH_FUNCTION_EXPORT(name, desc)
  134. #define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc)
  135. #define MSH_CMD_EXPORT(command, desc)
  136. #define MSH_CMD_EXPORT_ALIAS(command, alias, desc)
  137. #elif !defined(FINSH_USING_SYMTAB)
  138. #define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc)
  139. #endif
  140. /* event length */
  141. #define RT_EVENT_LENGTH 32
  142. /* memory management option */
  143. #define RT_MM_PAGE_SIZE 4096
  144. #define RT_MM_PAGE_MASK (RT_MM_PAGE_SIZE - 1)
  145. #define RT_MM_PAGE_BITS 12
  146. /* kernel malloc definitions */
  147. #ifndef RT_KERNEL_MALLOC
  148. #define RT_KERNEL_MALLOC(sz) rt_malloc(sz)
  149. #endif /* RT_KERNEL_MALLOC */
  150. #ifndef RT_KERNEL_FREE
  151. #define RT_KERNEL_FREE(ptr) rt_free(ptr)
  152. #endif /* RT_KERNEL_FREE */
  153. #ifndef RT_KERNEL_REALLOC
  154. #define RT_KERNEL_REALLOC(ptr, size) rt_realloc(ptr, size)
  155. #endif /* RT_KERNEL_REALLOC */
  156. /**
  157. * @ingroup BasicDef
  158. *
  159. * @def RT_IS_ALIGN(addr, align)
  160. * Return true(1) or false(0).
  161. * RT_IS_ALIGN(128, 4) is judging whether 128 aligns with 4.
  162. * The result is 1, which means 128 aligns with 4.
  163. * @note If the address is NULL, false(0) will be returned
  164. */
  165. #define RT_IS_ALIGN(addr, align) ((!(addr & (align - 1))) && (addr != RT_NULL))
  166. /**
  167. * @ingroup BasicDef
  168. *
  169. * @def RT_ALIGN(size, align)
  170. * Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
  171. * would return 16.
  172. */
  173. #define RT_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))
  174. /**
  175. * @ingroup BasicDef
  176. *
  177. * @def RT_ALIGN_DOWN(size, align)
  178. * Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4)
  179. * would return 12.
  180. */
  181. #define RT_ALIGN_DOWN(size, align) ((size) & ~((align) - 1))
  182. /**
  183. * @addtogroup KernelObject
  184. */
  185. /**@{*/
  186. /*
  187. * kernel object macros
  188. */
  189. #define RT_OBJECT_FLAG_MODULE 0x80 /**< is module object. */
  190. /**
  191. * Base structure of Kernel object
  192. */
  193. struct rt_object
  194. {
  195. #if RT_NAME_MAX > 0
  196. char name[RT_NAME_MAX]; /**< dynamic name of kernel object */
  197. #else
  198. const char *name; /**< static name of kernel object */
  199. #endif /* RT_NAME_MAX > 0 */
  200. rt_uint8_t type; /**< type of kernel object */
  201. rt_uint8_t flag; /**< flag of kernel object */
  202. #ifdef RT_USING_MODULE
  203. void * module_id; /**< id of application module */
  204. #endif /* RT_USING_MODULE */
  205. #ifdef RT_USING_SMART
  206. rt_atomic_t lwp_ref_count; /**< ref count for lwp */
  207. #endif /* RT_USING_SMART */
  208. rt_list_t list; /**< list node of kernel object */
  209. };
  210. typedef struct rt_object *rt_object_t; /**< Type for kernel objects. */
  211. /**
  212. * iterator of rt_object_for_each()
  213. *
  214. * data is the data passing in to rt_object_for_each(). iterator can return
  215. * RT_EOK to continue the iteration; or any positive value to break the loop
  216. * successfully; or any negative errno to break the loop on failure.
  217. */
  218. typedef rt_err_t (*rt_object_iter_t)(rt_object_t object, void *data);
  219. /**
  220. * The object type can be one of the follows with specific
  221. * macros enabled:
  222. * - Thread
  223. * - Semaphore
  224. * - Mutex
  225. * - Event
  226. * - MailBox
  227. * - MessageQueue
  228. * - MemHeap
  229. * - MemPool
  230. * - Device
  231. * - Timer
  232. * - Module
  233. * - Unknown
  234. * - Static
  235. */
  236. enum rt_object_class_type
  237. {
  238. RT_Object_Class_Null = 0x00, /**< The object is not used. */
  239. RT_Object_Class_Thread = 0x01, /**< The object is a thread. */
  240. RT_Object_Class_Semaphore = 0x02, /**< The object is a semaphore. */
  241. RT_Object_Class_Mutex = 0x03, /**< The object is a mutex. */
  242. RT_Object_Class_Event = 0x04, /**< The object is a event. */
  243. RT_Object_Class_MailBox = 0x05, /**< The object is a mail box. */
  244. RT_Object_Class_MessageQueue = 0x06, /**< The object is a message queue. */
  245. RT_Object_Class_MemHeap = 0x07, /**< The object is a memory heap. */
  246. RT_Object_Class_MemPool = 0x08, /**< The object is a memory pool. */
  247. RT_Object_Class_Device = 0x09, /**< The object is a device. */
  248. RT_Object_Class_Timer = 0x0a, /**< The object is a timer. */
  249. RT_Object_Class_Module = 0x0b, /**< The object is a module. */
  250. RT_Object_Class_Memory = 0x0c, /**< The object is a memory. */
  251. RT_Object_Class_Channel = 0x0d, /**< The object is a channel */
  252. RT_Object_Class_ProcessGroup = 0x0e, /**< The object is a process group */
  253. RT_Object_Class_Session = 0x0f, /**< The object is a session */
  254. RT_Object_Class_Service = 0x10, /**< The object is a service */
  255. RT_Object_Class_Custom = 0x11, /**< The object is a custom object */
  256. RT_Object_Class_Unknown = 0x12, /**< The object is unknown. */
  257. RT_Object_Class_Static = 0x80 /**< The object is a static object. */
  258. };
  259. /**
  260. * The information of the kernel object
  261. */
  262. struct rt_object_information
  263. {
  264. enum rt_object_class_type type; /**< object class type */
  265. rt_list_t object_list; /**< object list */
  266. rt_size_t object_size; /**< object size */
  267. struct rt_spinlock spinlock;
  268. };
  269. /**
  270. * The hook function call macro
  271. */
  272. #ifndef RT_USING_HOOK
  273. #define RT_OBJECT_HOOK_CALL(func, argv)
  274. #else
  275. /**
  276. * @brief Add hook point in the routines
  277. * @note Usage:
  278. * void foo() {
  279. * do_something();
  280. *
  281. * RT_OBJECT_HOOK_CALL(foo);
  282. *
  283. * do_other_things();
  284. * }
  285. */
  286. #define _RT_OBJECT_HOOK_CALL(func, argv) __ON_HOOK_ARGS(func, argv)
  287. #define RT_OBJECT_HOOK_CALL(func, argv) _RT_OBJECT_HOOK_CALL(func, argv)
  288. #ifdef RT_HOOK_USING_FUNC_PTR
  289. #define __ON_HOOK_ARGS(__hook, argv) do {if ((__hook) != RT_NULL) __hook argv; } while (0)
  290. #else
  291. #define __ON_HOOK_ARGS(__hook, argv)
  292. #endif /* RT_HOOK_USING_FUNC_PTR */
  293. #endif /* RT_USING_HOOK */
  294. #ifdef RT_USING_HOOKLIST
  295. /**
  296. * @brief Add declaration for hook list types.
  297. *
  298. * @note Usage:
  299. * This is typically used in your header. In foo.h using this like:
  300. *
  301. * ```foo.h
  302. * typedef void (*bar_hook_proto_t)(arguments...);
  303. * RT_OBJECT_HOOKLIST_DECLARE(bar_hook_proto_t, bar_myhook);
  304. * ```
  305. */
  306. #define RT_OBJECT_HOOKLIST_DECLARE(handler_type, name) \
  307. typedef struct name##_hooklistnode \
  308. { \
  309. handler_type handler; \
  310. rt_list_t list_node; \
  311. } *name##_hooklistnode_t; \
  312. extern volatile rt_ubase_t name##_nested; \
  313. void name##_sethook(name##_hooklistnode_t node); \
  314. void name##_rmhook(name##_hooklistnode_t node)
  315. /**
  316. * @brief Add declaration for hook list node.
  317. *
  318. * @note Usage
  319. * You can add a hook like this.
  320. *
  321. * ```addhook.c
  322. * void myhook(arguments...) { do_something(); }
  323. * RT_OBJECT_HOOKLIST_DEFINE_NODE(bar_myhook, myhook_node, myhook);
  324. *
  325. * void addhook(void)
  326. * {
  327. * bar_myhook_sethook(myhook);
  328. * }
  329. * ```
  330. *
  331. * BTW, you can also find examples codes under
  332. * `examples/utest/testcases/kernel/hooklist_tc.c`.
  333. */
  334. #define RT_OBJECT_HOOKLIST_DEFINE_NODE(hookname, nodename, hooker_handler) \
  335. struct hookname##_hooklistnode nodename = { \
  336. .handler = hooker_handler, \
  337. .list_node = RT_LIST_OBJECT_INIT(nodename.list_node), \
  338. };
  339. /**
  340. * @note Usage
  341. * Add this macro to the source file where your hook point is inserted.
  342. */
  343. #define RT_OBJECT_HOOKLIST_DEFINE(name) \
  344. static rt_list_t name##_hooklist = RT_LIST_OBJECT_INIT(name##_hooklist); \
  345. static struct rt_spinlock name##lock = RT_SPINLOCK_INIT; \
  346. volatile rt_ubase_t name##_nested = 0; \
  347. void name##_sethook(name##_hooklistnode_t node) \
  348. { \
  349. rt_ubase_t level = rt_spin_lock_irqsave(&name##lock); \
  350. while (name##_nested) \
  351. { \
  352. rt_spin_unlock_irqrestore(&name##lock, level); \
  353. level = rt_spin_lock_irqsave(&name##lock); \
  354. } \
  355. rt_list_insert_before(&name##_hooklist, &node->list_node); \
  356. rt_spin_unlock_irqrestore(&name##lock, level); \
  357. } \
  358. void name##_rmhook(name##_hooklistnode_t node) \
  359. { \
  360. rt_ubase_t level = rt_spin_lock_irqsave(&name##lock); \
  361. while (name##_nested) \
  362. { \
  363. rt_spin_unlock_irqrestore(&name##lock, level); \
  364. level = rt_spin_lock_irqsave(&name##lock); \
  365. } \
  366. rt_list_remove(&node->list_node); \
  367. rt_spin_unlock_irqrestore(&name##lock, level); \
  368. }
  369. /**
  370. * @brief Add hook list point in the routines. Multiple hookers in the list will
  371. * be called one by one starting from head node.
  372. *
  373. * @note Usage:
  374. * void foo() {
  375. * do_something();
  376. *
  377. * RT_OBJECT_HOOKLIST_CALL(foo);
  378. *
  379. * do_other_things();
  380. * }
  381. */
  382. #define _RT_OBJECT_HOOKLIST_CALL(nodetype, nested, list, lock, argv) \
  383. do \
  384. { \
  385. nodetype iter, next; \
  386. rt_ubase_t level = rt_spin_lock_irqsave(&lock); \
  387. nested += 1; \
  388. rt_spin_unlock_irqrestore(&lock, level); \
  389. if (!rt_list_isempty(&list)) \
  390. { \
  391. rt_list_for_each_entry_safe(iter, next, &list, list_node) \
  392. { \
  393. iter->handler argv; \
  394. } \
  395. } \
  396. level = rt_spin_lock_irqsave(&lock); \
  397. nested -= 1; \
  398. rt_spin_unlock_irqrestore(&lock, level); \
  399. } while (0)
  400. #define RT_OBJECT_HOOKLIST_CALL(name, argv) \
  401. _RT_OBJECT_HOOKLIST_CALL(name##_hooklistnode_t, name##_nested, \
  402. name##_hooklist, name##lock, argv)
  403. #else
  404. #define RT_OBJECT_HOOKLIST_DECLARE(handler_type, name)
  405. #define RT_OBJECT_HOOKLIST_DEFINE_NODE(hookname, nodename, hooker_handler)
  406. #define RT_OBJECT_HOOKLIST_DEFINE(name)
  407. #define RT_OBJECT_HOOKLIST_CALL(name, argv)
  408. #endif /* RT_USING_HOOKLIST */
  409. /**@}*/
  410. /**
  411. * @addtogroup Clock
  412. */
  413. /**@{*/
  414. /**
  415. * clock & timer macros
  416. */
  417. #define RT_TIMER_FLAG_DEACTIVATED 0x0 /**< timer is deactive */
  418. #define RT_TIMER_FLAG_ACTIVATED 0x1 /**< timer is active */
  419. #define RT_TIMER_FLAG_ONE_SHOT 0x0 /**< one shot timer */
  420. #define RT_TIMER_FLAG_PERIODIC 0x2 /**< periodic timer */
  421. #define RT_TIMER_FLAG_HARD_TIMER 0x0 /**< hard timer,the timer's callback function will be called in tick isr. */
  422. #define RT_TIMER_FLAG_SOFT_TIMER 0x4 /**< soft timer,the timer's callback function will be called in timer thread. */
  423. #define RT_TIMER_FLAG_THREAD_TIMER \
  424. (0x8 | RT_TIMER_FLAG_HARD_TIMER) /**< thread timer that cooperates with scheduler directly */
  425. #define RT_TIMER_CTRL_SET_TIME 0x0 /**< set timer control command */
  426. #define RT_TIMER_CTRL_GET_TIME 0x1 /**< get timer control command */
  427. #define RT_TIMER_CTRL_SET_ONESHOT 0x2 /**< change timer to one shot */
  428. #define RT_TIMER_CTRL_SET_PERIODIC 0x3 /**< change timer to periodic */
  429. #define RT_TIMER_CTRL_GET_STATE 0x4 /**< get timer run state active or deactive*/
  430. #define RT_TIMER_CTRL_GET_REMAIN_TIME 0x5 /**< get the remaining hang time */
  431. #define RT_TIMER_CTRL_GET_FUNC 0x6 /**< get timer timeout func */
  432. #define RT_TIMER_CTRL_SET_FUNC 0x7 /**< set timer timeout func */
  433. #define RT_TIMER_CTRL_GET_PARM 0x8 /**< get timer parameter */
  434. #define RT_TIMER_CTRL_SET_PARM 0x9 /**< get timer parameter */
  435. #ifndef RT_TIMER_SKIP_LIST_LEVEL
  436. #define RT_TIMER_SKIP_LIST_LEVEL 1
  437. #endif
  438. /* 1 or 3 */
  439. #ifndef RT_TIMER_SKIP_LIST_MASK
  440. #define RT_TIMER_SKIP_LIST_MASK 0x3 /**< Timer skips the list mask */
  441. #endif
  442. /**
  443. * timeout handler of rt_timer
  444. */
  445. typedef void (*rt_timer_func_t)(void *parameter);
  446. /**
  447. * timer structure
  448. */
  449. struct rt_timer
  450. {
  451. struct rt_object parent; /**< inherit from rt_object */
  452. rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL];
  453. rt_timer_func_t timeout_func; /**< timeout function */
  454. void *parameter; /**< timeout function's parameter */
  455. rt_tick_t init_tick; /**< timer timeout tick */
  456. rt_tick_t timeout_tick; /**< timeout tick */
  457. };
  458. typedef struct rt_timer *rt_timer_t;
  459. /**@}*/
  460. /**
  461. * @addtogroup Signal
  462. */
  463. /**@{*/
  464. #ifdef RT_USING_SIGNALS
  465. #define RT_SIG_MAX 32
  466. typedef unsigned long rt_sigset_t;
  467. typedef siginfo_t rt_siginfo_t;
  468. typedef void (*rt_sighandler_t)(int signo);
  469. #endif /* RT_USING_SIGNALS */
  470. /**@}*/
  471. /**
  472. * @addtogroup Thread
  473. */
  474. /**@{*/
  475. /*
  476. * Thread
  477. */
  478. /*
  479. * thread state definitions
  480. */
  481. #define RT_THREAD_INIT 0x00 /**< Initialized status */
  482. #define RT_THREAD_CLOSE 0x01 /**< Closed status */
  483. #define RT_THREAD_READY 0x02 /**< Ready status */
  484. #define RT_THREAD_RUNNING 0x03 /**< Running status */
  485. /*
  486. * for rt_thread_suspend_with_flag()
  487. */
  488. enum
  489. {
  490. RT_INTERRUPTIBLE = 0,
  491. RT_KILLABLE,
  492. RT_UNINTERRUPTIBLE,
  493. };
  494. #define RT_THREAD_SUSPEND_MASK 0x04
  495. #define RT_SIGNAL_COMMON_WAKEUP_MASK 0x02
  496. #define RT_SIGNAL_KILL_WAKEUP_MASK 0x01
  497. #define RT_THREAD_SUSPEND_INTERRUPTIBLE (RT_THREAD_SUSPEND_MASK) /**< Suspend interruptable 0x4 */
  498. #define RT_THREAD_SUSPEND RT_THREAD_SUSPEND_INTERRUPTIBLE
  499. #define RT_THREAD_SUSPEND_KILLABLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK) /**< Suspend with killable 0x6 */
  500. #define RT_THREAD_SUSPEND_UNINTERRUPTIBLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK | RT_SIGNAL_KILL_WAKEUP_MASK) /**< Suspend with uninterruptable 0x7 */
  501. #define RT_THREAD_STAT_MASK 0x07
  502. #define RT_THREAD_STAT_YIELD 0x08 /**< indicate whether remaining_tick has been reloaded since last schedule */
  503. #define RT_THREAD_STAT_YIELD_MASK RT_THREAD_STAT_YIELD
  504. #define RT_THREAD_STAT_SIGNAL 0x10 /**< task hold signals */
  505. #define RT_THREAD_STAT_SIGNAL_READY (RT_THREAD_STAT_SIGNAL | RT_THREAD_READY)
  506. #define RT_THREAD_STAT_SIGNAL_WAIT 0x20 /**< task is waiting for signals */
  507. #define RT_THREAD_STAT_SIGNAL_PENDING 0x40 /**< signals is held and it has not been procressed */
  508. #define RT_THREAD_STAT_SIGNAL_MASK 0xf0
  509. /**
  510. * thread control command definitions
  511. */
  512. #define RT_THREAD_CTRL_STARTUP 0x00 /**< Startup thread. */
  513. #define RT_THREAD_CTRL_CLOSE 0x01 /**< Close thread. */
  514. #define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */
  515. #define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */
  516. #define RT_THREAD_CTRL_BIND_CPU 0x04 /**< Set thread bind cpu. */
  517. /**
  518. * CPU usage statistics data
  519. */
  520. struct rt_cpu_usage_stats
  521. {
  522. rt_ubase_t user;
  523. rt_ubase_t system;
  524. rt_ubase_t irq;
  525. rt_ubase_t idle;
  526. };
  527. typedef struct rt_cpu_usage_stats *rt_cpu_usage_stats_t;
  528. #ifdef RT_USING_SMP
  529. #define RT_CPU_DETACHED RT_CPUS_NR /**< The thread not running on cpu. */
  530. #define RT_CPU_MASK ((1 << RT_CPUS_NR) - 1) /**< All CPUs mask bit. */
  531. #ifndef RT_SCHEDULE_IPI
  532. #define RT_SCHEDULE_IPI 0
  533. #endif /* RT_SCHEDULE_IPI */
  534. #ifndef RT_STOP_IPI
  535. #define RT_STOP_IPI 1
  536. #endif /* RT_STOP_IPI */
  537. #ifndef RT_SMP_CALL_IPI
  538. #define RT_SMP_CALL_IPI 2
  539. #endif
  540. #define RT_MAX_IPI 3
  541. #define _SCHEDULER_CONTEXT(fileds) fileds
  542. /**
  543. * CPUs definitions
  544. *
  545. */
  546. struct rt_cpu
  547. {
  548. /**
  549. * protected by:
  550. * - other cores: accessing from other coress is undefined behaviour
  551. * - local core: rt_enter_critical()/rt_exit_critical()
  552. */
  553. _SCHEDULER_CONTEXT(
  554. struct rt_thread *current_thread;
  555. rt_uint8_t irq_switch_flag:1;
  556. rt_uint8_t sched_lock_flag:1;
  557. #ifndef ARCH_USING_HW_THREAD_SELF
  558. rt_uint8_t critical_switch_flag:1;
  559. #endif /* ARCH_USING_HW_THREAD_SELF */
  560. rt_uint8_t current_priority;
  561. rt_list_t priority_table[RT_THREAD_PRIORITY_MAX];
  562. #if RT_THREAD_PRIORITY_MAX > 32
  563. rt_uint32_t priority_group;
  564. rt_uint8_t ready_table[32];
  565. #else
  566. rt_uint32_t priority_group;
  567. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  568. rt_atomic_t tick; /**< Passing tickes on this core */
  569. );
  570. struct rt_thread *idle_thread;
  571. rt_atomic_t irq_nest;
  572. #ifdef RT_USING_SMART
  573. struct rt_spinlock spinlock;
  574. #endif /* RT_USING_SMART */
  575. #ifdef RT_USING_CPU_USAGE_TRACER
  576. struct rt_cpu_usage_stats cpu_stat;
  577. #endif /* RT_USING_CPU_USAGE_TRACER */
  578. #ifdef ARCH_USING_IRQ_CTX_LIST
  579. rt_slist_t irq_ctx_head;
  580. #endif /* ARCH_USING_IRQ_CTX_LIST */
  581. };
  582. #else /* !RT_USING_SMP */
  583. struct rt_cpu
  584. {
  585. struct rt_thread *current_thread;
  586. struct rt_thread *idle_thread;
  587. #ifdef RT_USING_CPU_USAGE_TRACER
  588. struct rt_cpu_usage_stats cpu_stat;
  589. #endif /* RT_USING_CPU_USAGE_TRACER */
  590. #ifdef ARCH_USING_IRQ_CTX_LIST
  591. rt_slist_t irq_ctx_head;
  592. #endif /* ARCH_USING_IRQ_CTX_LIST */
  593. };
  594. #endif /* RT_USING_SMP */
  595. typedef struct rt_cpu *rt_cpu_t;
  596. /* Noted: As API to reject writing to this variable from application codes */
  597. #define rt_current_thread rt_thread_self()
  598. struct rt_thread;
  599. /**
  600. * interrupt/exception frame handling
  601. *
  602. */
  603. typedef struct rt_interrupt_context {
  604. void *context; /**< arch specific context */
  605. rt_slist_t node; /**< node for nested interrupt */
  606. } *rt_interrupt_context_t;
  607. #ifdef RT_USING_SMART
  608. typedef rt_err_t (*rt_wakeup_func_t)(void *object, struct rt_thread *thread);
  609. struct rt_wakeup
  610. {
  611. rt_wakeup_func_t func;
  612. void *user_data;
  613. };
  614. #define _LWP_NSIG 64
  615. #ifdef ARCH_CPU_64BIT
  616. #define _LWP_NSIG_BPW 64
  617. #else
  618. #define _LWP_NSIG_BPW 32
  619. #endif
  620. #define _LWP_NSIG_WORDS (RT_ALIGN(_LWP_NSIG, _LWP_NSIG_BPW) / _LWP_NSIG_BPW)
  621. typedef void (*lwp_sighandler_t)(int);
  622. typedef void (*lwp_sigaction_t)(int signo, siginfo_t *info, void *context);
  623. typedef struct {
  624. unsigned long sig[_LWP_NSIG_WORDS];
  625. } lwp_sigset_t;
  626. #if _LWP_NSIG <= 64
  627. #define lwp_sigmask(signo) ((lwp_sigset_t){.sig = {[0] = ((long)(1u << ((signo)-1)))}})
  628. #define lwp_sigset_init(mask) ((lwp_sigset_t){.sig = {[0] = (long)(mask)}})
  629. #endif /* _LWP_NSIG <= 64 */
  630. struct lwp_sigaction {
  631. union {
  632. void (*_sa_handler)(int);
  633. void (*_sa_sigaction)(int, siginfo_t *, void *);
  634. } __sa_handler;
  635. lwp_sigset_t sa_mask;
  636. int sa_flags;
  637. void (*sa_restorer)(void);
  638. };
  639. typedef struct lwp_siginfo_ext {
  640. union {
  641. /* for SIGCHLD */
  642. struct {
  643. int status;
  644. clock_t utime;
  645. clock_t stime;
  646. } sigchld;
  647. };
  648. } *lwp_siginfo_ext_t;
  649. typedef struct lwp_siginfo {
  650. rt_list_t node;
  651. struct {
  652. int signo;
  653. int code;
  654. int from_tid;
  655. pid_t from_pid;
  656. } ksiginfo;
  657. /* the signal specified extension field */
  658. struct lwp_siginfo_ext *ext;
  659. } *lwp_siginfo_t;
  660. typedef struct lwp_sigqueue {
  661. rt_list_t siginfo_list;
  662. lwp_sigset_t sigset_pending;
  663. } *lwp_sigqueue_t;
  664. struct lwp_thread_signal {
  665. lwp_sigset_t sigset_mask;
  666. struct lwp_sigqueue sig_queue;
  667. };
  668. struct rt_user_context
  669. {
  670. void *sp;
  671. void *pc;
  672. void *flag;
  673. void *ctx;
  674. };
  675. #endif /* RT_USING_SMART */
  676. typedef void (*rt_thread_cleanup_t)(struct rt_thread *tid);
  677. /**
  678. * Thread structure
  679. */
  680. struct rt_thread
  681. {
  682. struct rt_object parent;
  683. /* stack point and entry */
  684. void *sp; /**< stack point */
  685. void *entry; /**< entry */
  686. void *parameter; /**< parameter */
  687. void *stack_addr; /**< stack address */
  688. rt_uint32_t stack_size; /**< stack size */
  689. /* error code */
  690. rt_err_t error; /**< error code */
  691. #ifdef RT_USING_SMP
  692. rt_atomic_t cpus_lock_nest; /**< cpus lock count */
  693. #endif
  694. RT_SCHED_THREAD_CTX
  695. struct rt_timer thread_timer; /**< built-in thread timer */
  696. rt_thread_cleanup_t cleanup; /**< cleanup function when thread exit */
  697. #ifdef RT_USING_MUTEX
  698. /* object for IPC */
  699. rt_list_t taken_object_list;
  700. rt_object_t pending_object;
  701. #endif /* RT_USING_MUTEX */
  702. #ifdef RT_USING_EVENT
  703. /* thread event */
  704. rt_uint32_t event_set;
  705. rt_uint8_t event_info;
  706. #endif /* RT_USING_EVENT */
  707. #ifdef RT_USING_SIGNALS
  708. rt_sigset_t sig_pending; /**< the pending signals */
  709. rt_sigset_t sig_mask; /**< the mask bits of signal */
  710. #ifndef RT_USING_SMP
  711. void *sig_ret; /**< the return stack pointer from signal */
  712. #endif /* RT_USING_SMP */
  713. rt_sighandler_t *sig_vectors; /**< vectors of signal handler */
  714. void *si_list; /**< the signal infor list */
  715. #endif /* RT_USING_SIGNALS */
  716. #ifdef RT_USING_CPU_USAGE
  717. rt_uint64_t duration_tick; /**< cpu usage tick */
  718. #endif /* RT_USING_CPU_USAGE */
  719. #ifdef RT_USING_PTHREADS
  720. void *pthread_data; /**< the handle of pthread data, adapt 32/64bit */
  721. #endif /* RT_USING_PTHREADS */
  722. /* light weight process if present */
  723. #ifdef RT_USING_SMART
  724. void *msg_ret; /**< the return msg */
  725. void *lwp; /**< the lwp reference */
  726. /* for user create */
  727. void *user_entry;
  728. void *user_stack;
  729. rt_uint32_t user_stack_size;
  730. rt_uint32_t *kernel_sp; /**< kernel stack point */
  731. rt_list_t sibling; /**< next thread of same process */
  732. struct lwp_thread_signal signal; /**< lwp signal for user-space thread */
  733. struct rt_user_context user_ctx; /**< user space context */
  734. struct rt_wakeup wakeup_handle; /**< wakeup handle for IPC */
  735. rt_atomic_t exit_request; /**< pending exit request of thread */
  736. int tid; /**< thread ID used by process */
  737. int tid_ref_count; /**< reference of tid */
  738. void *susp_recycler; /**< suspended recycler on this thread */
  739. void *robust_list; /**< pi lock, very carefully, it's a userspace list!*/
  740. #ifndef ARCH_MM_MMU
  741. lwp_sighandler_t signal_handler[32];
  742. #else
  743. int step_exec;
  744. int debug_attach_req;
  745. int debug_ret_user;
  746. int debug_suspend;
  747. struct rt_hw_exp_stack *regs;
  748. void *thread_idr; /** lwp thread indicator */
  749. int *clear_child_tid;
  750. #endif /* ARCH_MM_MMU */
  751. #endif /* RT_USING_SMART */
  752. #ifdef RT_USING_CPU_USAGE_TRACER
  753. rt_ubase_t user_time; /**< Ticks on user */
  754. rt_ubase_t system_time; /**< Ticks on system */
  755. #endif /* RT_USING_CPU_USAGE_TRACER */
  756. #ifdef RT_USING_MEM_PROTECTION
  757. void *mem_regions;
  758. #ifdef RT_USING_HW_STACK_GUARD
  759. void *stack_buf;
  760. #endif /* RT_USING_HW_STACK_GUARD */
  761. #endif /* RT_USING_MEM_PROTECTION */
  762. struct rt_spinlock spinlock;
  763. rt_ubase_t user_data; /**< private user data beyond this thread */
  764. };
  765. typedef struct rt_thread *rt_thread_t;
  766. #ifdef RT_USING_SMART
  767. #define LWP_IS_USER_MODE(t) ((t)->user_ctx.ctx == RT_NULL)
  768. #else
  769. #define LWP_IS_USER_MODE(t) (0)
  770. #endif /* RT_USING_SMART */
  771. /**@}*/
  772. /**
  773. * @addtogroup IPC
  774. */
  775. /**@{*/
  776. /**
  777. * IPC flags and control command definitions
  778. */
  779. #define RT_IPC_FLAG_FIFO 0x00 /**< FIFOed IPC. @ref IPC. */
  780. #define RT_IPC_FLAG_PRIO 0x01 /**< PRIOed IPC. @ref IPC. */
  781. #define RT_IPC_CMD_UNKNOWN 0x00 /**< unknown IPC command */
  782. #define RT_IPC_CMD_RESET 0x01 /**< reset IPC object */
  783. #define RT_IPC_CMD_GET_STATE 0x02 /**< get the state of IPC object */
  784. #define RT_IPC_CMD_SET_VLIMIT 0x03 /**< set max limit value of IPC value */
  785. #define RT_WAITING_FOREVER -1 /**< Block forever until get resource. */
  786. #define RT_WAITING_NO 0 /**< Non-block. */
  787. /**
  788. * Base structure of IPC object
  789. */
  790. struct rt_ipc_object
  791. {
  792. struct rt_object parent; /**< inherit from rt_object */
  793. rt_list_t suspend_thread; /**< threads pended on this resource */
  794. };
  795. /**
  796. * @addtogroup semaphore
  797. * @{
  798. */
  799. #ifdef RT_USING_SEMAPHORE
  800. /**
  801. * Semaphore structure
  802. */
  803. struct rt_semaphore
  804. {
  805. struct rt_ipc_object parent; /**< inherit from ipc_object */
  806. rt_uint16_t value; /**< value of semaphore. */
  807. rt_uint16_t max_value;
  808. struct rt_spinlock spinlock;
  809. };
  810. typedef struct rt_semaphore *rt_sem_t;
  811. #endif /* RT_USING_SEMAPHORE */
  812. /**@}*/
  813. /**
  814. * @addtogroup mutex
  815. * @{
  816. */
  817. #ifdef RT_USING_MUTEX
  818. /**
  819. * Mutual exclusion (mutex) structure
  820. */
  821. struct rt_mutex
  822. {
  823. struct rt_ipc_object parent; /**< inherit from ipc_object */
  824. rt_uint8_t ceiling_priority; /**< the priority ceiling of mutexe */
  825. rt_uint8_t priority; /**< the maximal priority for pending thread */
  826. rt_uint8_t hold; /**< numbers of thread hold the mutex */
  827. rt_uint8_t reserved; /**< reserved field */
  828. struct rt_thread *owner; /**< current owner of mutex */
  829. rt_list_t taken_list; /**< the object list taken by thread */
  830. struct rt_spinlock spinlock;
  831. };
  832. typedef struct rt_mutex *rt_mutex_t;
  833. #endif /* RT_USING_MUTEX */
  834. /**@}*/
  835. /**
  836. * @addtogroup event
  837. * @{
  838. */
  839. #ifdef RT_USING_EVENT
  840. /**
  841. * flag definitions in event
  842. */
  843. #define RT_EVENT_FLAG_AND 0x01 /**< logic and */
  844. #define RT_EVENT_FLAG_OR 0x02 /**< logic or */
  845. #define RT_EVENT_FLAG_CLEAR 0x04 /**< clear flag */
  846. /*
  847. * event structure
  848. */
  849. struct rt_event
  850. {
  851. struct rt_ipc_object parent; /**< inherit from ipc_object */
  852. rt_uint32_t set; /**< event set */
  853. struct rt_spinlock spinlock;
  854. };
  855. typedef struct rt_event *rt_event_t;
  856. #endif /* RT_USING_EVENT */
  857. /**@}*/
  858. /**
  859. * @addtogroup mailbox
  860. * @{
  861. */
  862. #ifdef RT_USING_MAILBOX
  863. /**
  864. * mailbox structure
  865. */
  866. struct rt_mailbox
  867. {
  868. struct rt_ipc_object parent; /**< inherit from ipc_object */
  869. rt_ubase_t *msg_pool; /**< start address of message buffer */
  870. rt_uint16_t size; /**< size of message pool */
  871. rt_uint16_t entry; /**< index of messages in msg_pool */
  872. rt_uint16_t in_offset; /**< input offset of the message buffer */
  873. rt_uint16_t out_offset; /**< output offset of the message buffer */
  874. rt_list_t suspend_sender_thread; /**< sender thread suspended on this mailbox */
  875. struct rt_spinlock spinlock;
  876. };
  877. typedef struct rt_mailbox *rt_mailbox_t;
  878. #endif /* RT_USING_MAILBOX */
  879. /**@}*/
  880. /**
  881. * @addtogroup messagequeue
  882. * @{
  883. */
  884. #ifdef RT_USING_MESSAGEQUEUE
  885. /**
  886. * message queue structure
  887. */
  888. struct rt_messagequeue
  889. {
  890. struct rt_ipc_object parent; /**< inherit from ipc_object */
  891. void *msg_pool; /**< start address of message queue */
  892. rt_uint16_t msg_size; /**< message size of each message */
  893. rt_uint16_t max_msgs; /**< max number of messages */
  894. rt_uint16_t entry; /**< index of messages in the queue */
  895. void *msg_queue_head; /**< list head */
  896. void *msg_queue_tail; /**< list tail */
  897. void *msg_queue_free; /**< pointer indicated the free node of queue */
  898. rt_list_t suspend_sender_thread; /**< sender thread suspended on this message queue */
  899. struct rt_spinlock spinlock;
  900. };
  901. typedef struct rt_messagequeue *rt_mq_t;
  902. #endif /* RT_USING_MESSAGEQUEUE */
  903. /**@}*/
  904. /**@}*/
  905. /**
  906. * @addtogroup MM
  907. */
  908. /**@{*/
  909. #ifdef RT_USING_HEAP
  910. /*
  911. * memory structure
  912. */
  913. struct rt_memory
  914. {
  915. struct rt_object parent; /**< inherit from rt_object */
  916. const char * algorithm; /**< Memory management algorithm name */
  917. rt_ubase_t address; /**< memory start address */
  918. rt_size_t total; /**< memory size */
  919. rt_size_t used; /**< size used */
  920. rt_size_t max; /**< maximum usage */
  921. };
  922. typedef struct rt_memory *rt_mem_t;
  923. #endif /* RT_USING_HEAP */
  924. /*
  925. * memory management
  926. * heap & partition
  927. */
  928. #ifdef RT_USING_SMALL_MEM
  929. typedef rt_mem_t rt_smem_t;
  930. #endif /* RT_USING_SMALL_MEM */
  931. #ifdef RT_USING_SLAB
  932. typedef rt_mem_t rt_slab_t;
  933. #endif /* RT_USING_SLAB */
  934. #ifdef RT_USING_MEMHEAP
  935. /**
  936. * memory item on the heap
  937. */
  938. struct rt_memheap_item
  939. {
  940. rt_uint32_t magic; /**< magic number for memheap */
  941. struct rt_memheap *pool_ptr; /**< point of pool */
  942. struct rt_memheap_item *next; /**< next memheap item */
  943. struct rt_memheap_item *prev; /**< prev memheap item */
  944. struct rt_memheap_item *next_free; /**< next free memheap item */
  945. struct rt_memheap_item *prev_free; /**< prev free memheap item */
  946. #ifdef RT_USING_MEMTRACE
  947. rt_uint8_t owner_thread_name[4]; /**< owner thread name */
  948. #endif /* RT_USING_MEMTRACE */
  949. };
  950. /**
  951. * Base structure of memory heap object
  952. */
  953. struct rt_memheap
  954. {
  955. struct rt_object parent; /**< inherit from rt_object */
  956. void *start_addr; /**< pool start address and size */
  957. rt_size_t pool_size; /**< pool size */
  958. rt_size_t available_size; /**< available size */
  959. rt_size_t max_used_size; /**< maximum allocated size */
  960. struct rt_memheap_item *block_list; /**< used block list */
  961. struct rt_memheap_item *free_list; /**< free block list */
  962. struct rt_memheap_item free_header; /**< free block list header */
  963. struct rt_semaphore lock; /**< semaphore lock */
  964. rt_bool_t locked; /**< External lock mark */
  965. };
  966. #endif /* RT_USING_MEMHEAP */
  967. #ifdef RT_USING_MEMPOOL
  968. /**
  969. * Base structure of Memory pool object
  970. */
  971. struct rt_mempool
  972. {
  973. struct rt_object parent; /**< inherit from rt_object */
  974. void *start_address; /**< memory pool start */
  975. rt_size_t size; /**< size of memory pool */
  976. rt_size_t block_size; /**< size of memory blocks */
  977. rt_uint8_t *block_list; /**< memory blocks list */
  978. rt_size_t block_total_count; /**< numbers of memory block */
  979. rt_size_t block_free_count; /**< numbers of free memory block */
  980. rt_list_t suspend_thread; /**< threads pended on this resource */
  981. struct rt_spinlock spinlock;
  982. };
  983. typedef struct rt_mempool *rt_mp_t;
  984. #endif /* RT_USING_MEMPOOL */
  985. /**@}*/
  986. #ifdef RT_USING_DEVICE
  987. /**
  988. * @addtogroup Device
  989. */
  990. /**@{*/
  991. /**
  992. * device (I/O) class type
  993. */
  994. enum rt_device_class_type
  995. {
  996. RT_Device_Class_Char = 0, /**< character device */
  997. RT_Device_Class_Block, /**< block device */
  998. RT_Device_Class_NetIf, /**< net interface */
  999. RT_Device_Class_MTD, /**< memory device */
  1000. RT_Device_Class_CAN, /**< CAN device */
  1001. RT_Device_Class_RTC, /**< RTC device */
  1002. RT_Device_Class_Sound, /**< Sound device */
  1003. RT_Device_Class_Graphic, /**< Graphic device */
  1004. RT_Device_Class_I2CBUS, /**< I2C bus device */
  1005. RT_Device_Class_USBDevice, /**< USB slave device */
  1006. RT_Device_Class_USBHost, /**< USB host bus */
  1007. RT_Device_Class_USBOTG, /**< USB OTG bus */
  1008. RT_Device_Class_SPIBUS, /**< SPI bus device */
  1009. RT_Device_Class_SPIDevice, /**< SPI device */
  1010. RT_Device_Class_SDIO, /**< SDIO bus device */
  1011. RT_Device_Class_PM, /**< PM pseudo device */
  1012. RT_Device_Class_Pipe, /**< Pipe device */
  1013. RT_Device_Class_Portal, /**< Portal device */
  1014. RT_Device_Class_Timer, /**< Timer device */
  1015. RT_Device_Class_Miscellaneous, /**< Miscellaneous device */
  1016. RT_Device_Class_Sensor, /**< Sensor device */
  1017. RT_Device_Class_Touch, /**< Touch device */
  1018. RT_Device_Class_PHY, /**< PHY device */
  1019. RT_Device_Class_Security, /**< Security device */
  1020. RT_Device_Class_WLAN, /**< WLAN device */
  1021. RT_Device_Class_Pin, /**< Pin device */
  1022. RT_Device_Class_ADC, /**< ADC device */
  1023. RT_Device_Class_DAC, /**< DAC device */
  1024. RT_Device_Class_WDT, /**< WDT device */
  1025. RT_Device_Class_PWM, /**< PWM device */
  1026. RT_Device_Class_Bus, /**< Bus device */
  1027. RT_Device_Class_Unknown /**< unknown device */
  1028. };
  1029. #define BLOCK_IO_MAGIC 0
  1030. #define NET_IO_MAGIC 1
  1031. #define MTD_IO_MAGIC 2
  1032. #define CAN_IO_MAGIC 3
  1033. #define RTC_IO_MAGIC 4
  1034. #define SOUND_IO_MAGIC 5
  1035. #define GRAPHIC_IO_MAGIC 6
  1036. #define I2CBUS_IO_MAGIC 7
  1037. #define USBDEVICE_IO_MAGIC 8
  1038. #define USBHOST_IO_MAGIC 9
  1039. #define USBOTG_IO_MAGIC 10
  1040. #define SPIBUS_IO_MAGIC 11
  1041. #define SPIDEVICE_IO_MAGIC 12
  1042. #define SDIO_IO_MAGIC 13
  1043. #define PM_IO_MAGIC 14
  1044. #define PIPE_IO_MAGIC 15
  1045. #define PORTAL_IO_MAGIC 16
  1046. #define TIMER_IO_MAGIC 17
  1047. #define MISCELLANEOUS_IO_MAGIC 18
  1048. #define SENSOR_IO_MAGIC 19
  1049. #define TOUCH_IO_MAGIC 20
  1050. #define PHY_IO_MAGIC 21
  1051. #define SECURITY_IO_MAGIC 22
  1052. #define WLAN_IO_MAGIC 23
  1053. #define PIN_IO_MAGIC 24
  1054. #define ADC_IO_MAGIC 25
  1055. #define DAC_IO_MAGIC 26
  1056. #define WDT_IO_MAGIC 27
  1057. #define PWM_IO_MAGIC 28
  1058. #define BUS_IO_MAGIC 29
  1059. #define UNKNOWN_IO_MAGIC 30
  1060. /**
  1061. * device flags definitions
  1062. */
  1063. #define RT_DEVICE_FLAG_DEACTIVATE 0x000 /**< device is not not initialized */
  1064. #define RT_DEVICE_FLAG_RDONLY 0x001 /**< read only */
  1065. #define RT_DEVICE_FLAG_WRONLY 0x002 /**< write only */
  1066. #define RT_DEVICE_FLAG_RDWR 0x003 /**< read and write */
  1067. #define RT_DEVICE_FLAG_REMOVABLE 0x004 /**< removable device */
  1068. #define RT_DEVICE_FLAG_STANDALONE 0x008 /**< standalone device */
  1069. #define RT_DEVICE_FLAG_ACTIVATED 0x010 /**< device is activated */
  1070. #define RT_DEVICE_FLAG_SUSPENDED 0x020 /**< device is suspended */
  1071. #define RT_DEVICE_FLAG_STREAM 0x040 /**< stream mode */
  1072. #define RT_DEVICE_FLAG_DYNAMIC 0x080 /**< device is determined when open() */
  1073. #define RT_DEVICE_FLAG_INT_RX 0x100 /**< INT mode on Rx */
  1074. #define RT_DEVICE_FLAG_DMA_RX 0x200 /**< DMA mode on Rx */
  1075. #define RT_DEVICE_FLAG_INT_TX 0x400 /**< INT mode on Tx */
  1076. #define RT_DEVICE_FLAG_DMA_TX 0x800 /**< DMA mode on Tx */
  1077. #define RT_DEVICE_OFLAG_CLOSE 0x000 /**< device is closed */
  1078. #define RT_DEVICE_OFLAG_RDONLY 0x001 /**< read only access */
  1079. #define RT_DEVICE_OFLAG_WRONLY 0x002 /**< write only access */
  1080. #define RT_DEVICE_OFLAG_RDWR 0x003 /**< read and write */
  1081. #define RT_DEVICE_OFLAG_OPEN 0x008 /**< device is opened */
  1082. #define RT_DEVICE_OFLAG_MASK 0xf0f /**< mask of open flag */
  1083. /**
  1084. * general device commands
  1085. * 0x01 - 0x1F general device control commands
  1086. * 0x20 - 0x3F udevice control commands
  1087. * 0x40 - special device control commands
  1088. */
  1089. #define RT_DEVICE_CTRL_RESUME 0x01 /**< resume device */
  1090. #define RT_DEVICE_CTRL_SUSPEND 0x02 /**< suspend device */
  1091. #define RT_DEVICE_CTRL_CONFIG 0x03 /**< configure device */
  1092. #define RT_DEVICE_CTRL_CLOSE 0x04 /**< close device */
  1093. #define RT_DEVICE_CTRL_NOTIFY_SET 0x05 /**< set notify func */
  1094. #define RT_DEVICE_CTRL_SET_INT 0x06 /**< set interrupt */
  1095. #define RT_DEVICE_CTRL_CLR_INT 0x07 /**< clear interrupt */
  1096. #define RT_DEVICE_CTRL_GET_INT 0x08 /**< get interrupt status */
  1097. #define RT_DEVICE_CTRL_CONSOLE_OFLAG 0x09 /**< get console open flag */
  1098. #define RT_DEVICE_CTRL_MASK 0x1f /**< mask for contrl commands */
  1099. /**
  1100. * device control
  1101. */
  1102. #define RT_DEVICE_CTRL_BASE(Type) ((RT_Device_Class_##Type + 1) * 0x100)
  1103. typedef struct rt_driver *rt_driver_t;
  1104. typedef struct rt_device *rt_device_t;
  1105. #ifdef RT_USING_DEVICE_OPS
  1106. /**
  1107. * operations set for device object
  1108. */
  1109. struct rt_device_ops
  1110. {
  1111. /* common device interface */
  1112. rt_err_t (*init) (rt_device_t dev);
  1113. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  1114. rt_err_t (*close) (rt_device_t dev);
  1115. rt_ssize_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  1116. rt_ssize_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  1117. rt_err_t (*control)(rt_device_t dev, int cmd, void *args);
  1118. };
  1119. #endif /* RT_USING_DEVICE_OPS */
  1120. /**
  1121. * WaitQueue structure
  1122. */
  1123. struct rt_wqueue
  1124. {
  1125. rt_uint32_t flag;
  1126. rt_list_t waiting_list;
  1127. struct rt_spinlock spinlock;
  1128. };
  1129. typedef struct rt_wqueue rt_wqueue_t;
  1130. #ifdef RT_USING_DM
  1131. struct rt_driver;
  1132. struct rt_bus;
  1133. #endif /* RT_USING_DM */
  1134. /**
  1135. * Device structure
  1136. */
  1137. struct rt_device
  1138. {
  1139. struct rt_object parent; /**< inherit from rt_object */
  1140. #ifdef RT_USING_DM
  1141. struct rt_bus *bus; /**< the bus mounting to */
  1142. rt_list_t node; /**< to mount on bus */
  1143. struct rt_driver *drv; /**< driver for powering the device */
  1144. #ifdef RT_USING_OFW
  1145. void *ofw_node; /**< ofw node get from device tree */
  1146. #endif /* RT_USING_OFW */
  1147. void *power_domain_unit;
  1148. #ifdef RT_USING_DMA
  1149. const void *dma_ops;
  1150. #endif
  1151. #endif /* RT_USING_DM */
  1152. enum rt_device_class_type type; /**< device type */
  1153. rt_uint16_t flag; /**< device flag */
  1154. rt_uint16_t open_flag; /**< device open flag */
  1155. rt_uint8_t ref_count; /**< reference count */
  1156. #ifdef RT_USING_DM
  1157. rt_uint8_t master_id; /**< 0 - 255 */
  1158. #endif
  1159. rt_uint8_t device_id; /**< 0 - 255 */
  1160. /* device call back */
  1161. rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);
  1162. rt_err_t (*tx_complete)(rt_device_t dev, void *buffer);
  1163. #ifdef RT_USING_DEVICE_OPS
  1164. const struct rt_device_ops *ops;
  1165. #else
  1166. /* common device interface */
  1167. rt_err_t (*init) (rt_device_t dev);
  1168. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  1169. rt_err_t (*close) (rt_device_t dev);
  1170. rt_ssize_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  1171. rt_ssize_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  1172. rt_err_t (*control)(rt_device_t dev, int cmd, void *args);
  1173. #endif /* RT_USING_DEVICE_OPS */
  1174. #ifdef RT_USING_POSIX_DEVIO
  1175. const struct dfs_file_ops *fops;
  1176. struct rt_wqueue wait_queue;
  1177. #endif /* RT_USING_POSIX_DEVIO */
  1178. rt_err_t (*readlink)
  1179. (rt_device_t dev, char *buf, int len); /**< for dynamic device */
  1180. void *user_data; /**< device private data */
  1181. };
  1182. /**
  1183. * Notify structure
  1184. */
  1185. struct rt_device_notify
  1186. {
  1187. void (*notify)(rt_device_t dev);
  1188. struct rt_device *dev;
  1189. };
  1190. #ifdef RT_USING_SMART
  1191. struct rt_channel
  1192. {
  1193. struct rt_ipc_object parent; /**< inherit from object */
  1194. struct rt_thread *reply; /**< the thread will be reply */
  1195. struct rt_spinlock slock; /**< spinlock of this channel */
  1196. rt_list_t wait_msg; /**< the wait queue of sender msg */
  1197. rt_list_t wait_thread; /**< the wait queue of sender thread */
  1198. rt_wqueue_t reader_queue; /**< channel poll queue */
  1199. rt_uint8_t stat; /**< the status of this channel */
  1200. rt_ubase_t ref;
  1201. };
  1202. typedef struct rt_channel *rt_channel_t;
  1203. #endif /* RT_USING_SMART */
  1204. /**@}*/
  1205. #endif /* RT_USING_DEVICE */
  1206. #ifdef __cplusplus
  1207. /* RT-Thread definitions for C++ */
  1208. namespace rtthread {
  1209. enum TICK_WAIT {
  1210. WAIT_NONE = 0,
  1211. WAIT_FOREVER = -1,
  1212. };
  1213. }
  1214. #endif /* __cplusplus */
  1215. #endif /* __RTDEF_H__ */