rtdef.h 53 KB

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