rtdef.h 53 KB

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