rtdef.h 54 KB

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