rtdef.h 53 KB

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