rtdef.h 54 KB

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