rtdef.h 53 KB

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