rtdef.h 52 KB

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