rtdef.h 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. /*
  2. * Copyright (c) 2006-2022, 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-17 Meco Man add RT_USING_LIBC_ISO_ONLY macro
  53. */
  54. #ifndef __RT_DEF_H__
  55. #define __RT_DEF_H__
  56. #include <rtconfig.h>
  57. #ifdef RT_USING_LIBC
  58. #if !defined(RT_USING_LIBC_ISO_ONLY) && !defined(RT_VER_NUM)
  59. #define RT_USING_LIBC_ISO_ONLY (1)
  60. #else
  61. #define RT_USING_LIBC_ISO_ONLY (0)
  62. #endif /* !defined(RT_USING_LIBC_ISO_ONLY) && !defined(RT_VER_NUM) */
  63. #include <stdint.h>
  64. #include <stddef.h>
  65. #include <stdarg.h>
  66. #if !RT_USING_LIBC_ISO_ONLY
  67. #include <sys/types.h>
  68. #include <sys/errno.h>
  69. #if defined(RT_USING_SIGNALS) || defined(RT_USING_SMART)
  70. #include <sys/signal.h>
  71. #endif /* defined(RT_USING_SIGNALS) || defined(RT_USING_SMART) */
  72. #endif /* !RT_USING_LIBC_ISO_ONLY */
  73. #endif /* RT_USING_LIBC */
  74. #ifdef __cplusplus
  75. extern "C" {
  76. #endif
  77. /**
  78. * @addtogroup BasicDef
  79. */
  80. /**@{*/
  81. /* RT-Thread version information */
  82. #define RT_VERSION_MAJOR 5 /**< Major version number (X.x.x) */
  83. #define RT_VERSION_MINOR 0 /**< Minor version number (x.X.x) */
  84. #define RT_VERSION_PATCH 1 /**< Patch version number (x.x.X) */
  85. /* e.g. #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(4, 1, 0) */
  86. #define RT_VERSION_CHECK(major, minor, revise) ((major * 10000) + (minor * 100) + revise)
  87. /* RT-Thread version */
  88. #define RTTHREAD_VERSION RT_VERSION_CHECK(RT_VERSION_MAJOR, RT_VERSION_MINOR, RT_VERSION_PATCH)
  89. /* RT-Thread basic data type definitions */
  90. typedef int rt_bool_t; /**< boolean type */
  91. typedef signed long rt_base_t; /**< Nbit CPU related date type */
  92. typedef unsigned long rt_ubase_t; /**< Nbit unsigned CPU related data type */
  93. #ifndef RT_USING_ARCH_DATA_TYPE
  94. #ifdef RT_USING_LIBC
  95. typedef int8_t rt_int8_t; /**< 8bit integer type */
  96. typedef int16_t rt_int16_t; /**< 16bit integer type */
  97. typedef int32_t rt_int32_t; /**< 32bit integer type */
  98. typedef uint8_t rt_uint8_t; /**< 8bit unsigned integer type */
  99. typedef uint16_t rt_uint16_t; /**< 16bit unsigned integer type */
  100. typedef uint32_t rt_uint32_t; /**< 32bit unsigned integer type */
  101. typedef int64_t rt_int64_t; /**< 64bit integer type */
  102. typedef uint64_t rt_uint64_t; /**< 64bit unsigned integer type */
  103. #else
  104. typedef signed char rt_int8_t; /**< 8bit integer type */
  105. typedef signed short rt_int16_t; /**< 16bit integer type */
  106. typedef signed int rt_int32_t; /**< 32bit integer type */
  107. typedef unsigned char rt_uint8_t; /**< 8bit unsigned integer type */
  108. typedef unsigned short rt_uint16_t; /**< 16bit unsigned integer type */
  109. typedef unsigned int rt_uint32_t; /**< 32bit unsigned integer type */
  110. #ifdef ARCH_CPU_64BIT
  111. typedef signed long rt_int64_t; /**< 64bit integer type */
  112. typedef unsigned long rt_uint64_t; /**< 64bit unsigned integer type */
  113. #else
  114. typedef signed long long rt_int64_t; /**< 64bit integer type */
  115. typedef unsigned long long rt_uint64_t; /**< 64bit unsigned integer type */
  116. #endif /* ARCH_CPU_64BIT */
  117. #endif /* RT_USING_LIBC */
  118. #endif /* RT_USING_ARCH_DATA_TYPE */
  119. #if defined(RT_USING_LIBC) && !RT_USING_LIBC_ISO_ONLY
  120. typedef size_t rt_size_t; /**< Type for size number */
  121. typedef ssize_t rt_ssize_t; /**< Used for a count of bytes or an error indication */
  122. #else
  123. typedef rt_ubase_t rt_size_t; /**< Type for size number */
  124. typedef rt_base_t rt_ssize_t; /**< Used for a count of bytes or an error indication */
  125. #endif /* defined(RT_USING_LIBC) && !RT_USING_LIBC_ISO_ONLY */
  126. typedef rt_base_t rt_err_t; /**< Type for error number */
  127. typedef rt_uint32_t rt_time_t; /**< Type for time stamp */
  128. typedef rt_uint32_t rt_tick_t; /**< Type for tick count */
  129. typedef rt_base_t rt_flag_t; /**< Type for flags */
  130. typedef rt_ubase_t rt_dev_t; /**< Type for device */
  131. typedef rt_base_t rt_off_t; /**< Type for offset */
  132. #if !defined(__cplusplus)
  133. #if defined(RT_USING_HW_ATOMIC)
  134. typedef rt_base_t rt_atomic_t;
  135. #elif defined(RT_USING_STDC_ATOMIC)
  136. #include <stdatomic.h>
  137. typedef atomic_size_t rt_atomic_t;
  138. #else
  139. /* To detect std atomic */
  140. #if defined(RT_USING_LIBC) && defined(__GNUC__) && !defined(__STDC_NO_ATOMICS__)
  141. #include <stdatomic.h>
  142. typedef atomic_size_t rt_atomic_t;
  143. #else
  144. typedef rt_base_t rt_atomic_t;
  145. #endif /* __GNUC__ && !__STDC_NO_ATOMICS__ */
  146. #endif /* RT_USING_STDC_ATOMIC */
  147. #endif /* __cplusplus */
  148. /* boolean type definitions */
  149. #define RT_TRUE 1 /**< boolean true */
  150. #define RT_FALSE 0 /**< boolean fails */
  151. /* null pointer definition */
  152. #define RT_NULL 0
  153. /**@}*/
  154. /* maximum value of base type */
  155. #ifdef RT_USING_LIBC
  156. #define RT_UINT8_MAX UINT8_MAX /**< Maximum number of UINT8 */
  157. #define RT_UINT16_MAX UINT16_MAX /**< Maximum number of UINT16 */
  158. #define RT_UINT32_MAX UINT32_MAX /**< Maximum number of UINT32 */
  159. #else
  160. #define RT_UINT8_MAX 0xff /**< Maximum number of UINT8 */
  161. #define RT_UINT16_MAX 0xffff /**< Maximum number of UINT16 */
  162. #define RT_UINT32_MAX 0xffffffff /**< Maximum number of UINT32 */
  163. #endif /* RT_USING_LIBC */
  164. #define RT_TICK_MAX RT_UINT32_MAX /**< Maximum number of tick */
  165. /* maximum value of ipc type */
  166. #define RT_SEM_VALUE_MAX RT_UINT16_MAX /**< Maximum number of semaphore .value */
  167. #define RT_MUTEX_VALUE_MAX RT_UINT16_MAX /**< Maximum number of mutex .value */
  168. #define RT_MUTEX_HOLD_MAX RT_UINT8_MAX /**< Maximum number of mutex .hold */
  169. #define RT_MB_ENTRY_MAX RT_UINT16_MAX /**< Maximum number of mailbox .entry */
  170. #define RT_MQ_ENTRY_MAX RT_UINT16_MAX /**< Maximum number of message queue .entry */
  171. /* Common Utilities */
  172. #define RT_UNUSED(x) ((void)x)
  173. /* compile time assertion */
  174. #define RT_STATIC_ASSERT(name, expn) typedef char _static_assert_##name[(expn)?1:-1]
  175. /* Compiler Related Definitions */
  176. #if defined(__ARMCC_VERSION) /* ARM Compiler */
  177. #define rt_section(x) __attribute__((section(x)))
  178. #define rt_used __attribute__((used))
  179. #define rt_align(n) __attribute__((aligned(n)))
  180. #define rt_weak __attribute__((weak))
  181. #define rt_inline static __inline
  182. /* module compiling */
  183. #ifdef RT_USING_MODULE
  184. #define RTT_API __declspec(dllimport)
  185. #else
  186. #define RTT_API __declspec(dllexport)
  187. #endif /* RT_USING_MODULE */
  188. #elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
  189. #define rt_section(x) @ x
  190. #define rt_used __root
  191. #define PRAGMA(x) _Pragma(#x)
  192. #define rt_align(n) PRAGMA(data_alignment=n)
  193. #define rt_weak __weak
  194. #define rt_inline static inline
  195. #define RTT_API
  196. #elif defined (__GNUC__) /* GNU GCC Compiler */
  197. #ifndef RT_USING_LIBC
  198. /* the version of GNU GCC must be greater than 4.x */
  199. typedef __builtin_va_list __gnuc_va_list;
  200. typedef __gnuc_va_list va_list;
  201. #define va_start(v,l) __builtin_va_start(v,l)
  202. #define va_end(v) __builtin_va_end(v)
  203. #define va_arg(v,l) __builtin_va_arg(v,l)
  204. #endif /* RT_USING_LIBC */
  205. #define __RT_STRINGIFY(x...) #x
  206. #define RT_STRINGIFY(x...) __RT_STRINGIFY(x)
  207. #define rt_section(x) __attribute__((section(x)))
  208. #define rt_used __attribute__((used))
  209. #define rt_align(n) __attribute__((aligned(n)))
  210. #define rt_weak __attribute__((weak))
  211. #define rt_noreturn __attribute__ ((noreturn))
  212. #define rt_inline static __inline
  213. #define RTT_API
  214. #elif defined (__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
  215. #define rt_section(x) __attribute__((section(x)))
  216. #define rt_used __attribute__((used))
  217. #define rt_align(n) __attribute__((aligned(n)))
  218. #define rt_weak __attribute__((weak))
  219. #define rt_inline static inline
  220. #define RTT_API
  221. #elif defined (_MSC_VER)
  222. #define rt_section(x)
  223. #define rt_used
  224. #define rt_align(n) __declspec(align(n))
  225. #define rt_weak
  226. #define rt_inline static __inline
  227. #define RTT_API
  228. #elif defined (__TI_COMPILER_VERSION__)
  229. /* The way that TI compiler set section is different from other(at least
  230. * GCC and MDK) compilers. See ARM Optimizing C/C++ Compiler 5.9.3 for more
  231. * details. */
  232. #define rt_section(x) __attribute__((section(x)))
  233. #ifdef __TI_EABI__
  234. #define rt_used __attribute__((retain)) __attribute__((used))
  235. #else
  236. #define rt_used __attribute__((used))
  237. #endif
  238. #define PRAGMA(x) _Pragma(#x)
  239. #define rt_align(n) __attribute__((aligned(n)))
  240. #ifdef __TI_EABI__
  241. #define rt_weak __attribute__((weak))
  242. #else
  243. #define rt_weak
  244. #endif
  245. #define rt_inline static inline
  246. #define RTT_API
  247. #elif defined (__TASKING__)
  248. #define rt_section(x) __attribute__((section(x)))
  249. #define rt_used __attribute__((used, protect))
  250. #define PRAGMA(x) _Pragma(#x)
  251. #define rt_align(n) __attribute__((__align(n)))
  252. #define rt_weak __attribute__((weak))
  253. #define rt_inline static inline
  254. #define RTT_API
  255. #else
  256. #error not supported tool chain
  257. #endif /* __ARMCC_VERSION */
  258. /* initialization export */
  259. #ifdef RT_USING_COMPONENTS_INIT
  260. typedef int (*init_fn_t)(void);
  261. #ifdef _MSC_VER
  262. #pragma section("rti_fn$f",read)
  263. #ifdef RT_DEBUGING_INIT
  264. struct rt_init_desc
  265. {
  266. const char* level;
  267. const init_fn_t fn;
  268. const char* fn_name;
  269. };
  270. #define INIT_EXPORT(fn, level) \
  271. const char __rti_level_##fn[] = ".rti_fn." level; \
  272. const char __rti_##fn##_name[] = #fn; \
  273. __declspec(allocate("rti_fn$f")) \
  274. rt_used const struct rt_init_desc __rt_init_msc_##fn = \
  275. {__rti_level_##fn, fn, __rti_##fn##_name};
  276. #else
  277. struct rt_init_desc
  278. {
  279. const char* level;
  280. const init_fn_t fn;
  281. };
  282. #define INIT_EXPORT(fn, level) \
  283. const char __rti_level_##fn[] = ".rti_fn." level; \
  284. __declspec(allocate("rti_fn$f")) \
  285. rt_used const struct rt_init_desc __rt_init_msc_##fn = \
  286. {__rti_level_##fn, fn };
  287. #endif
  288. #else
  289. #ifdef RT_DEBUGING_INIT
  290. struct rt_init_desc
  291. {
  292. const char* fn_name;
  293. const init_fn_t fn;
  294. };
  295. #define INIT_EXPORT(fn, level) \
  296. const char __rti_##fn##_name[] = #fn; \
  297. rt_used const struct rt_init_desc __rt_init_desc_##fn rt_section(".rti_fn." level) = \
  298. { __rti_##fn##_name, fn};
  299. #else
  300. #define INIT_EXPORT(fn, level) \
  301. rt_used const init_fn_t __rt_init_##fn rt_section(".rti_fn." level) = fn
  302. #endif
  303. #endif
  304. #else
  305. #define INIT_EXPORT(fn, level)
  306. #endif
  307. /* board init routines will be called in board_init() function */
  308. #define INIT_BOARD_EXPORT(fn) INIT_EXPORT(fn, "1")
  309. /* init cpu, memory, interrupt-controller, bus... */
  310. #define INIT_CORE_EXPORT(fn) INIT_EXPORT(fn, "1.0")
  311. /* init pci/pcie, usb platform driver... */
  312. #define INIT_FRAMEWORK_EXPORT(fn) INIT_EXPORT(fn, "1.1")
  313. /* init platform, user code... */
  314. #define INIT_PLATFORM_EXPORT(fn) INIT_EXPORT(fn, "1.2")
  315. /* init sys-timer, clk, pinctrl... */
  316. #define INIT_SUBSYS_EXPORT(fn) INIT_EXPORT(fn, "1.3")
  317. /* init early drivers */
  318. #define INIT_DRIVER_EARLY_EXPORT(fn) INIT_EXPORT(fn, "1.4")
  319. /* pre/device/component/env/app init routines will be called in init_thread */
  320. /* components pre-initialization (pure software initialization) */
  321. #define INIT_PREV_EXPORT(fn) INIT_EXPORT(fn, "2")
  322. /* device initialization */
  323. #define INIT_DEVICE_EXPORT(fn) INIT_EXPORT(fn, "3")
  324. /* components initialization (dfs, lwip, ...) */
  325. #define INIT_COMPONENT_EXPORT(fn) INIT_EXPORT(fn, "4")
  326. /* environment initialization (mount disk, ...) */
  327. #define INIT_ENV_EXPORT(fn) INIT_EXPORT(fn, "5")
  328. /* application initialization (rtgui application etc ...) */
  329. #define INIT_APP_EXPORT(fn) INIT_EXPORT(fn, "6")
  330. /* init after mount fs */
  331. #define INIT_FS_EXPORT(fn) INIT_EXPORT(fn, "6.0")
  332. /* init in secondary_cpu_c_start */
  333. #define INIT_SECONDARY_CPU_EXPORT(fn) INIT_EXPORT(fn, "7")
  334. #if !defined(RT_USING_FINSH)
  335. /* define these to empty, even if not include finsh.h file */
  336. #define FINSH_FUNCTION_EXPORT(name, desc)
  337. #define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc)
  338. #define MSH_CMD_EXPORT(command, desc)
  339. #define MSH_CMD_EXPORT_ALIAS(command, alias, desc)
  340. #elif !defined(FINSH_USING_SYMTAB)
  341. #define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc)
  342. #endif
  343. /* event length */
  344. #define RT_EVENT_LENGTH 32
  345. /* memory management option */
  346. #define RT_MM_PAGE_SIZE 4096
  347. #define RT_MM_PAGE_MASK (RT_MM_PAGE_SIZE - 1)
  348. #define RT_MM_PAGE_BITS 12
  349. /* kernel malloc definitions */
  350. #ifndef RT_KERNEL_MALLOC
  351. #define RT_KERNEL_MALLOC(sz) rt_malloc(sz)
  352. #endif
  353. #ifndef RT_KERNEL_FREE
  354. #define RT_KERNEL_FREE(ptr) rt_free(ptr)
  355. #endif
  356. #ifndef RT_KERNEL_REALLOC
  357. #define RT_KERNEL_REALLOC(ptr, size) rt_realloc(ptr, size)
  358. #endif
  359. /**
  360. * @addtogroup Error Code
  361. */
  362. /**@{*/
  363. /* RT-Thread error code definitions */
  364. #if defined(RT_USING_LIBC) && !RT_USING_LIBC_ISO_ONLY
  365. /* POSIX error code compatible */
  366. #define RT_EOK 0 /**< There is no error */
  367. #define RT_ERROR 255 /**< A generic/unknown error happens */
  368. #define RT_ETIMEOUT ETIMEDOUT /**< Timed out */
  369. #define RT_EFULL ENOSPC /**< The resource is full */
  370. #define RT_EEMPTY ENODATA /**< The resource is empty */
  371. #define RT_ENOMEM ENOMEM /**< No memory */
  372. #define RT_ENOSYS ENOSYS /**< Function not implemented */
  373. #define RT_EBUSY EBUSY /**< Busy */
  374. #define RT_EIO EIO /**< IO error */
  375. #define RT_EINTR EINTR /**< Interrupted system call */
  376. #define RT_EINVAL EINVAL /**< Invalid argument */
  377. #define RT_ENOENT ENOENT /**< No entry */
  378. #define RT_ENOSPC ENOSPC /**< No space left */
  379. #define RT_EPERM EPERM /**< Operation not permitted */
  380. #define RT_ETRAP 254 /**< Trap event */
  381. #else
  382. #define RT_EOK 0 /**< There is no error */
  383. #define RT_ERROR 1 /**< A generic/unknown error happens */
  384. #define RT_ETIMEOUT 2 /**< Timed out */
  385. #define RT_EFULL 3 /**< The resource is full */
  386. #define RT_EEMPTY 4 /**< The resource is empty */
  387. #define RT_ENOMEM 5 /**< No memory */
  388. #define RT_ENOSYS 6 /**< Function not implemented */
  389. #define RT_EBUSY 7 /**< Busy */
  390. #define RT_EIO 8 /**< IO error */
  391. #define RT_EINTR 9 /**< Interrupted system call */
  392. #define RT_EINVAL 10 /**< Invalid argument */
  393. #define RT_ENOENT 11 /**< No entry */
  394. #define RT_ENOSPC 12 /**< No space left */
  395. #define RT_EPERM 13 /**< Operation not permitted */
  396. #define RT_ETRAP 14 /**< Trap event */
  397. #endif /* defined(RT_USING_LIBC) && !RT_USING_LIBC_ISO_ONLY */
  398. /**@}*/
  399. /**
  400. * @ingroup BasicDef
  401. *
  402. * @def RT_IS_ALIGN(addr, align)
  403. * Return true(1) or false(0).
  404. * RT_IS_ALIGN(128, 4) is judging whether 128 aligns with 4.
  405. * The result is 1, which means 128 aligns with 4.
  406. * @note If the address is NULL, false(0) will be returned
  407. */
  408. #define RT_IS_ALIGN(addr, align) ((!(addr & (align - 1))) && (addr != RT_NULL))
  409. /**
  410. * @ingroup BasicDef
  411. *
  412. * @def RT_ALIGN(size, align)
  413. * Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
  414. * would return 16.
  415. */
  416. #define RT_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))
  417. /**
  418. * @ingroup BasicDef
  419. *
  420. * @def RT_ALIGN_DOWN(size, align)
  421. * Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4)
  422. * would return 12.
  423. */
  424. #define RT_ALIGN_DOWN(size, align) ((size) & ~((align) - 1))
  425. /**
  426. * Double List structure
  427. */
  428. struct rt_list_node
  429. {
  430. struct rt_list_node *next; /**< point to next node. */
  431. struct rt_list_node *prev; /**< point to prev node. */
  432. };
  433. typedef struct rt_list_node rt_list_t; /**< Type for lists. */
  434. /**
  435. * Single List structure
  436. */
  437. struct rt_slist_node
  438. {
  439. struct rt_slist_node *next; /**< point to next node. */
  440. };
  441. typedef struct rt_slist_node rt_slist_t; /**< Type for single list. */
  442. /**
  443. * @addtogroup KernelObject
  444. */
  445. /**@{*/
  446. /*
  447. * kernel object macros
  448. */
  449. #define RT_OBJECT_FLAG_MODULE 0x80 /**< is module object. */
  450. /**
  451. * Base structure of Kernel object
  452. */
  453. struct rt_object
  454. {
  455. #if RT_NAME_MAX > 0
  456. char name[RT_NAME_MAX]; /**< dynamic name of kernel object */
  457. #else
  458. const char *name; /**< static name of kernel object */
  459. #endif /* RT_NAME_MAX > 0 */
  460. rt_uint8_t type; /**< type of kernel object */
  461. rt_uint8_t flag; /**< flag of kernel object */
  462. #ifdef RT_USING_MODULE
  463. void * module_id; /**< id of application module */
  464. #endif /* RT_USING_MODULE */
  465. #ifdef RT_USING_SMART
  466. int lwp_ref_count; /**< ref count for lwp */
  467. #endif /* RT_USING_SMART */
  468. rt_list_t list; /**< list node of kernel object */
  469. };
  470. typedef struct rt_object *rt_object_t; /**< Type for kernel objects. */
  471. /**
  472. * The object type can be one of the follows with specific
  473. * macros enabled:
  474. * - Thread
  475. * - Semaphore
  476. * - Mutex
  477. * - Event
  478. * - MailBox
  479. * - MessageQueue
  480. * - MemHeap
  481. * - MemPool
  482. * - Device
  483. * - Timer
  484. * - Module
  485. * - Unknown
  486. * - Static
  487. */
  488. enum rt_object_class_type
  489. {
  490. RT_Object_Class_Null = 0x00, /**< The object is not used. */
  491. RT_Object_Class_Thread = 0x01, /**< The object is a thread. */
  492. RT_Object_Class_Semaphore = 0x02, /**< The object is a semaphore. */
  493. RT_Object_Class_Mutex = 0x03, /**< The object is a mutex. */
  494. RT_Object_Class_Event = 0x04, /**< The object is a event. */
  495. RT_Object_Class_MailBox = 0x05, /**< The object is a mail box. */
  496. RT_Object_Class_MessageQueue = 0x06, /**< The object is a message queue. */
  497. RT_Object_Class_MemHeap = 0x07, /**< The object is a memory heap. */
  498. RT_Object_Class_MemPool = 0x08, /**< The object is a memory pool. */
  499. RT_Object_Class_Device = 0x09, /**< The object is a device. */
  500. RT_Object_Class_Timer = 0x0a, /**< The object is a timer. */
  501. RT_Object_Class_Module = 0x0b, /**< The object is a module. */
  502. RT_Object_Class_Memory = 0x0c, /**< The object is a memory. */
  503. RT_Object_Class_Channel = 0x0d, /**< The object is a channel */
  504. RT_Object_Class_Custom = 0x0e, /**< The object is a custom object */
  505. RT_Object_Class_Unknown = 0x0f, /**< The object is unknown. */
  506. RT_Object_Class_Static = 0x80 /**< The object is a static object. */
  507. };
  508. /**
  509. * The information of the kernel object
  510. */
  511. struct rt_object_information
  512. {
  513. enum rt_object_class_type type; /**< object class type */
  514. rt_list_t object_list; /**< object list */
  515. rt_size_t object_size; /**< object size */
  516. };
  517. /**
  518. * The hook function call macro
  519. */
  520. #ifndef RT_USING_HOOK
  521. #define __ON_HOOK_ARGS(__hook, argv)
  522. #define RT_OBJECT_HOOK_CALL(func, argv)
  523. #else
  524. #define RT_OBJECT_HOOK_CALL(func, argv) __on_##func argv
  525. #ifdef RT_HOOK_USING_FUNC_PTR
  526. #define __ON_HOOK_ARGS(__hook, argv) do {if ((__hook) != RT_NULL) __hook argv; } while (0)
  527. #else
  528. #define __ON_HOOK_ARGS(__hook, argv)
  529. #endif /* RT_HOOK_USING_FUNC_PTR */
  530. #endif /* RT_USING_HOOK */
  531. #ifndef __on_rt_interrupt_switch_hook
  532. #define __on_rt_interrupt_switch_hook() __ON_HOOK_ARGS(rt_interrupt_switch_hook, ())
  533. #endif
  534. #ifndef __on_rt_malloc_hook
  535. #define __on_rt_malloc_hook(addr, size) __ON_HOOK_ARGS(rt_malloc_hook, (addr, size))
  536. #endif
  537. #ifndef __on_rt_free_hook
  538. #define __on_rt_free_hook(rmem) __ON_HOOK_ARGS(rt_free_hook, (rmem))
  539. #endif
  540. /**@}*/
  541. /**
  542. * @addtogroup Clock
  543. */
  544. /**@{*/
  545. /**
  546. * clock & timer macros
  547. */
  548. #define RT_TIMER_FLAG_DEACTIVATED 0x0 /**< timer is deactive */
  549. #define RT_TIMER_FLAG_ACTIVATED 0x1 /**< timer is active */
  550. #define RT_TIMER_FLAG_ONE_SHOT 0x0 /**< one shot timer */
  551. #define RT_TIMER_FLAG_PERIODIC 0x2 /**< periodic timer */
  552. #define RT_TIMER_FLAG_HARD_TIMER 0x0 /**< hard timer,the timer's callback function will be called in tick isr. */
  553. #define RT_TIMER_FLAG_SOFT_TIMER 0x4 /**< soft timer,the timer's callback function will be called in timer thread. */
  554. #define RT_TIMER_CTRL_SET_TIME 0x0 /**< set timer control command */
  555. #define RT_TIMER_CTRL_GET_TIME 0x1 /**< get timer control command */
  556. #define RT_TIMER_CTRL_SET_ONESHOT 0x2 /**< change timer to one shot */
  557. #define RT_TIMER_CTRL_SET_PERIODIC 0x3 /**< change timer to periodic */
  558. #define RT_TIMER_CTRL_GET_STATE 0x4 /**< get timer run state active or deactive*/
  559. #define RT_TIMER_CTRL_GET_REMAIN_TIME 0x5 /**< get the remaining hang time */
  560. #define RT_TIMER_CTRL_GET_FUNC 0x6 /**< get timer timeout func */
  561. #define RT_TIMER_CTRL_SET_FUNC 0x7 /**< set timer timeout func */
  562. #define RT_TIMER_CTRL_GET_PARM 0x8 /**< get timer parameter */
  563. #define RT_TIMER_CTRL_SET_PARM 0x9 /**< get timer parameter */
  564. #ifndef RT_TIMER_SKIP_LIST_LEVEL
  565. #define RT_TIMER_SKIP_LIST_LEVEL 1
  566. #endif
  567. /* 1 or 3 */
  568. #ifndef RT_TIMER_SKIP_LIST_MASK
  569. #define RT_TIMER_SKIP_LIST_MASK 0x3 /**< Timer skips the list mask */
  570. #endif
  571. /**
  572. * timer structure
  573. */
  574. struct rt_timer
  575. {
  576. struct rt_object parent; /**< inherit from rt_object */
  577. rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL];
  578. void (*timeout_func)(void *parameter); /**< timeout function */
  579. void *parameter; /**< timeout function's parameter */
  580. rt_tick_t init_tick; /**< timer timeout tick */
  581. rt_tick_t timeout_tick; /**< timeout tick */
  582. };
  583. typedef struct rt_timer *rt_timer_t;
  584. /**@}*/
  585. /**
  586. * @addtogroup Signal
  587. */
  588. /**@{*/
  589. #ifdef RT_USING_SIGNALS
  590. #define RT_SIG_MAX 32
  591. typedef unsigned long rt_sigset_t;
  592. typedef siginfo_t rt_siginfo_t;
  593. typedef void (*rt_sighandler_t)(int signo);
  594. #endif /* RT_USING_SIGNALS */
  595. /**@}*/
  596. /**
  597. * @addtogroup Thread
  598. */
  599. /**@{*/
  600. /*
  601. * Thread
  602. */
  603. /*
  604. * thread state definitions
  605. */
  606. #define RT_THREAD_INIT 0x00 /**< Initialized status */
  607. #define RT_THREAD_CLOSE 0x01 /**< Closed status */
  608. #define RT_THREAD_READY 0x02 /**< Ready status */
  609. #define RT_THREAD_RUNNING 0x03 /**< Running status */
  610. /*
  611. * for rt_thread_suspend_with_flag()
  612. */
  613. enum
  614. {
  615. RT_INTERRUPTIBLE = 0,
  616. RT_KILLABLE,
  617. RT_UNINTERRUPTIBLE,
  618. };
  619. #define RT_THREAD_SUSPEND_MASK 0x04
  620. #define RT_SIGNAL_COMMON_WAKEUP_MASK 0x02
  621. #define RT_SIGNAL_KILL_WAKEUP_MASK 0x01
  622. #define RT_THREAD_SUSPEND_INTERRUPTIBLE (RT_THREAD_SUSPEND_MASK) /**< Suspend interruptable 0x4 */
  623. #define RT_THREAD_SUSPEND RT_THREAD_SUSPEND_INTERRUPTIBLE
  624. #define RT_THREAD_SUSPEND_KILLABLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK) /**< Suspend with killable 0x6 */
  625. #define RT_THREAD_SUSPEND_UNINTERRUPTIBLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK | RT_SIGNAL_KILL_WAKEUP_MASK) /**< Suspend with uninterruptable 0x7 */
  626. #define RT_THREAD_STAT_MASK 0x07
  627. #define RT_THREAD_STAT_YIELD 0x08 /**< indicate whether remaining_tick has been reloaded since last schedule */
  628. #define RT_THREAD_STAT_YIELD_MASK RT_THREAD_STAT_YIELD
  629. #define RT_THREAD_STAT_SIGNAL 0x10 /**< task hold signals */
  630. #define RT_THREAD_STAT_SIGNAL_READY (RT_THREAD_STAT_SIGNAL | RT_THREAD_READY)
  631. #define RT_THREAD_STAT_SIGNAL_WAIT 0x20 /**< task is waiting for signals */
  632. #define RT_THREAD_STAT_SIGNAL_PENDING 0x40 /**< signals is held and it has not been procressed */
  633. #define RT_THREAD_STAT_SIGNAL_MASK 0xf0
  634. /**
  635. * thread control command definitions
  636. */
  637. #define RT_THREAD_CTRL_STARTUP 0x00 /**< Startup thread. */
  638. #define RT_THREAD_CTRL_CLOSE 0x01 /**< Close thread. */
  639. #define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */
  640. #define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */
  641. #define RT_THREAD_CTRL_BIND_CPU 0x04 /**< Set thread bind cpu. */
  642. #ifdef RT_USING_SMP
  643. #define RT_CPU_DETACHED RT_CPUS_NR /**< The thread not running on cpu. */
  644. #define RT_CPU_MASK ((1 << RT_CPUS_NR) - 1) /**< All CPUs mask bit. */
  645. #ifndef RT_SCHEDULE_IPI
  646. #define RT_SCHEDULE_IPI 0
  647. #endif /* RT_SCHEDULE_IPI */
  648. #ifndef RT_STOP_IPI
  649. #define RT_STOP_IPI 1
  650. #endif /* RT_STOP_IPI */
  651. /**
  652. * CPUs definitions
  653. *
  654. */
  655. struct rt_cpu
  656. {
  657. struct rt_thread *current_thread;
  658. rt_uint16_t irq_nest;
  659. rt_uint8_t irq_switch_flag;
  660. rt_uint8_t current_priority;
  661. rt_list_t priority_table[RT_THREAD_PRIORITY_MAX];
  662. #if RT_THREAD_PRIORITY_MAX > 32
  663. rt_uint32_t priority_group;
  664. rt_uint8_t ready_table[32];
  665. #else
  666. rt_uint32_t priority_group;
  667. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  668. rt_tick_t tick;
  669. };
  670. #endif /* RT_USING_SMP */
  671. struct rt_thread;
  672. #ifdef RT_USING_SMART
  673. typedef rt_err_t (*rt_wakeup_func_t)(void *object, struct rt_thread *thread);
  674. struct rt_wakeup
  675. {
  676. rt_wakeup_func_t func;
  677. void *user_data;
  678. };
  679. #define _LWP_NSIG 64
  680. #ifdef ARCH_CPU_64BIT
  681. #define _LWP_NSIG_BPW 64
  682. #else
  683. #define _LWP_NSIG_BPW 32
  684. #endif
  685. #define _LWP_NSIG_WORDS (RT_ALIGN(_LWP_NSIG, _LWP_NSIG_BPW) / _LWP_NSIG_BPW)
  686. typedef void (*lwp_sighandler_t)(int);
  687. typedef void (*lwp_sigaction_t)(int signo, siginfo_t *info, void *context);
  688. typedef struct {
  689. unsigned long sig[_LWP_NSIG_WORDS];
  690. } lwp_sigset_t;
  691. #if _LWP_NSIG <= 64
  692. #define lwp_sigmask(signo) ((lwp_sigset_t){.sig = {[0] = ((long)(1u << ((signo)-1)))}})
  693. #define lwp_sigset_init(mask) ((lwp_sigset_t){.sig = {[0] = (long)(mask)}})
  694. #endif
  695. struct lwp_sigaction {
  696. union {
  697. void (*_sa_handler)(int);
  698. void (*_sa_sigaction)(int, siginfo_t *, void *);
  699. } __sa_handler;
  700. lwp_sigset_t sa_mask;
  701. int sa_flags;
  702. void (*sa_restorer)(void);
  703. };
  704. typedef struct lwp_siginfo {
  705. rt_list_t node;
  706. struct {
  707. int signo;
  708. int code;
  709. long value;
  710. int from_tid;
  711. pid_t from_pid;
  712. } ksiginfo;
  713. } *lwp_siginfo_t;
  714. typedef struct lwp_sigqueue {
  715. rt_list_t siginfo_list;
  716. lwp_sigset_t sigset_pending;
  717. } *lwp_sigqueue_t;
  718. struct lwp_thread_signal {
  719. lwp_sigset_t sigset_mask;
  720. struct lwp_sigqueue sig_queue;
  721. };
  722. struct rt_user_context
  723. {
  724. void *sp;
  725. void *pc;
  726. void *flag;
  727. void *ctx;
  728. };
  729. #endif
  730. typedef void (*rt_thread_cleanup_t)(struct rt_thread *tid);
  731. /**
  732. * Thread structure
  733. */
  734. struct rt_thread
  735. {
  736. struct rt_object parent;
  737. rt_list_t tlist; /**< the thread list */
  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. rt_uint8_t stat; /**< thread status */
  747. #ifdef RT_USING_SMP
  748. rt_uint8_t bind_cpu; /**< thread is bind to cpu */
  749. rt_uint8_t oncpu; /**< process on cpu */
  750. rt_uint16_t scheduler_lock_nest; /**< scheduler lock count */
  751. rt_int16_t cpus_lock_nest; /**< cpus lock count */
  752. rt_uint16_t critical_lock_nest; /**< critical lock count */
  753. #endif /*RT_USING_SMP*/
  754. /* priority */
  755. rt_uint8_t current_priority; /**< current priority */
  756. rt_uint8_t init_priority; /**< initialized priority */
  757. #if RT_THREAD_PRIORITY_MAX > 32
  758. rt_uint8_t number;
  759. rt_uint8_t high_mask;
  760. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  761. rt_uint32_t number_mask; /**< priority number mask */
  762. #ifdef RT_USING_MUTEX
  763. /* object for IPC */
  764. rt_list_t taken_object_list;
  765. rt_object_t pending_object;
  766. #endif
  767. #ifdef RT_USING_EVENT
  768. /* thread event */
  769. rt_uint32_t event_set;
  770. rt_uint8_t event_info;
  771. #endif /* RT_USING_EVENT */
  772. #ifdef RT_USING_SIGNALS
  773. rt_sigset_t sig_pending; /**< the pending signals */
  774. rt_sigset_t sig_mask; /**< the mask bits of signal */
  775. #ifndef RT_USING_SMP
  776. void *sig_ret; /**< the return stack pointer from signal */
  777. #endif /* RT_USING_SMP */
  778. rt_sighandler_t *sig_vectors; /**< vectors of signal handler */
  779. void *si_list; /**< the signal infor list */
  780. #endif /* RT_USING_SIGNALS */
  781. rt_ubase_t init_tick; /**< thread's initialized tick */
  782. rt_ubase_t remaining_tick; /**< remaining tick */
  783. #ifdef RT_USING_CPU_USAGE
  784. rt_uint64_t duration_tick; /**< cpu usage tick */
  785. #endif /* RT_USING_CPU_USAGE */
  786. #ifdef RT_USING_PTHREADS
  787. void *pthread_data; /**< the handle of pthread data, adapt 32/64bit */
  788. #endif /* RT_USING_PTHREADS */
  789. struct rt_timer thread_timer; /**< built-in thread timer */
  790. rt_thread_cleanup_t cleanup; /**< cleanup function when thread exit */
  791. /* light weight process if present */
  792. #ifdef RT_USING_SMART
  793. void *msg_ret; /**< the return msg */
  794. void *lwp; /**< the lwp reference */
  795. /* for user create */
  796. void *user_entry;
  797. void *user_stack;
  798. rt_uint32_t user_stack_size;
  799. rt_uint32_t *kernel_sp; /**< kernel stack point */
  800. rt_list_t sibling; /**< next thread of same process */
  801. struct lwp_thread_signal signal; /**< lwp signal for user-space thread */
  802. struct rt_user_context user_ctx; /**< user space context */
  803. struct rt_wakeup wakeup; /**< wakeup data */
  804. int exit_request;
  805. int tid;
  806. #ifndef ARCH_MM_MMU
  807. lwp_sighandler_t signal_handler[32];
  808. #else
  809. int step_exec;
  810. int debug_attach_req;
  811. int debug_ret_user;
  812. int debug_suspend;
  813. struct rt_hw_exp_stack *regs;
  814. void *thread_idr; /** lwp thread indicator */
  815. int *clear_child_tid;
  816. #endif /* ARCH_MM_MMU */
  817. #endif /* RT_USING_SMART */
  818. rt_ubase_t user_data; /**< private user data beyond this thread */
  819. };
  820. typedef struct rt_thread *rt_thread_t;
  821. /**@}*/
  822. /**
  823. * @addtogroup IPC
  824. */
  825. /**@{*/
  826. /**
  827. * IPC flags and control command definitions
  828. */
  829. #define RT_IPC_FLAG_FIFO 0x00 /**< FIFOed IPC. @ref IPC. */
  830. #define RT_IPC_FLAG_PRIO 0x01 /**< PRIOed IPC. @ref IPC. */
  831. #define RT_IPC_CMD_UNKNOWN 0x00 /**< unknown IPC command */
  832. #define RT_IPC_CMD_RESET 0x01 /**< reset IPC object */
  833. #define RT_IPC_CMD_GET_STATE 0x02 /**< get the state of IPC object */
  834. #define RT_WAITING_FOREVER -1 /**< Block forever until get resource. */
  835. #define RT_WAITING_NO 0 /**< Non-block. */
  836. /**
  837. * Base structure of IPC object
  838. */
  839. struct rt_ipc_object
  840. {
  841. struct rt_object parent; /**< inherit from rt_object */
  842. rt_list_t suspend_thread; /**< threads pended on this resource */
  843. };
  844. #ifdef RT_USING_SEMAPHORE
  845. /**
  846. * Semaphore structure
  847. */
  848. struct rt_semaphore
  849. {
  850. struct rt_ipc_object parent; /**< inherit from ipc_object */
  851. rt_uint16_t value; /**< value of semaphore. */
  852. rt_uint16_t reserved; /**< reserved field */
  853. };
  854. typedef struct rt_semaphore *rt_sem_t;
  855. #endif /* RT_USING_SEMAPHORE */
  856. #ifdef RT_USING_MUTEX
  857. /**
  858. * Mutual exclusion (mutex) structure
  859. */
  860. struct rt_mutex
  861. {
  862. struct rt_ipc_object parent; /**< inherit from ipc_object */
  863. rt_uint8_t ceiling_priority; /**< the priority ceiling of mutexe */
  864. rt_uint8_t priority; /**< the maximal priority for pending thread */
  865. rt_uint8_t hold; /**< numbers of thread hold the mutex */
  866. rt_uint8_t reserved; /**< reserved field */
  867. struct rt_thread *owner; /**< current owner of mutex */
  868. rt_list_t taken_list; /**< the object list taken by thread */
  869. };
  870. typedef struct rt_mutex *rt_mutex_t;
  871. #endif /* RT_USING_MUTEX */
  872. #ifdef RT_USING_EVENT
  873. /**
  874. * flag definitions in event
  875. */
  876. #define RT_EVENT_FLAG_AND 0x01 /**< logic and */
  877. #define RT_EVENT_FLAG_OR 0x02 /**< logic or */
  878. #define RT_EVENT_FLAG_CLEAR 0x04 /**< clear flag */
  879. /*
  880. * event structure
  881. */
  882. struct rt_event
  883. {
  884. struct rt_ipc_object parent; /**< inherit from ipc_object */
  885. rt_uint32_t set; /**< event set */
  886. };
  887. typedef struct rt_event *rt_event_t;
  888. #endif /* RT_USING_EVENT */
  889. #ifdef RT_USING_MAILBOX
  890. /**
  891. * mailbox structure
  892. */
  893. struct rt_mailbox
  894. {
  895. struct rt_ipc_object parent; /**< inherit from ipc_object */
  896. rt_ubase_t *msg_pool; /**< start address of message buffer */
  897. rt_uint16_t size; /**< size of message pool */
  898. rt_uint16_t entry; /**< index of messages in msg_pool */
  899. rt_uint16_t in_offset; /**< input offset of the message buffer */
  900. rt_uint16_t out_offset; /**< output offset of the message buffer */
  901. rt_list_t suspend_sender_thread; /**< sender thread suspended on this mailbox */
  902. };
  903. typedef struct rt_mailbox *rt_mailbox_t;
  904. #endif /* RT_USING_MAILBOX */
  905. #ifdef RT_USING_MESSAGEQUEUE
  906. /**
  907. * message queue structure
  908. */
  909. struct rt_messagequeue
  910. {
  911. struct rt_ipc_object parent; /**< inherit from ipc_object */
  912. void *msg_pool; /**< start address of message queue */
  913. rt_uint16_t msg_size; /**< message size of each message */
  914. rt_uint16_t max_msgs; /**< max number of messages */
  915. rt_uint16_t entry; /**< index of messages in the queue */
  916. void *msg_queue_head; /**< list head */
  917. void *msg_queue_tail; /**< list tail */
  918. void *msg_queue_free; /**< pointer indicated the free node of queue */
  919. rt_list_t suspend_sender_thread; /**< sender thread suspended on this message queue */
  920. };
  921. typedef struct rt_messagequeue *rt_mq_t;
  922. #endif /* RT_USING_MESSAGEQUEUE */
  923. /**@}*/
  924. /**
  925. * @addtogroup MM
  926. */
  927. /**@{*/
  928. #ifdef RT_USING_HEAP
  929. /*
  930. * memory structure
  931. */
  932. struct rt_memory
  933. {
  934. struct rt_object parent; /**< inherit from rt_object */
  935. const char * algorithm; /**< Memory management algorithm name */
  936. rt_ubase_t address; /**< memory start address */
  937. rt_size_t total; /**< memory size */
  938. rt_size_t used; /**< size used */
  939. rt_size_t max; /**< maximum usage */
  940. };
  941. typedef struct rt_memory *rt_mem_t;
  942. #endif /* RT_USING_HEAP */
  943. /*
  944. * memory management
  945. * heap & partition
  946. */
  947. #ifdef RT_USING_SMALL_MEM
  948. typedef rt_mem_t rt_smem_t;
  949. #endif /* RT_USING_SMALL_MEM */
  950. #ifdef RT_USING_SLAB
  951. typedef rt_mem_t rt_slab_t;
  952. #endif /* RT_USING_SLAB */
  953. #ifdef RT_USING_MEMHEAP
  954. /**
  955. * memory item on the heap
  956. */
  957. struct rt_memheap_item
  958. {
  959. rt_uint32_t magic; /**< magic number for memheap */
  960. struct rt_memheap *pool_ptr; /**< point of pool */
  961. struct rt_memheap_item *next; /**< next memheap item */
  962. struct rt_memheap_item *prev; /**< prev memheap item */
  963. struct rt_memheap_item *next_free; /**< next free memheap item */
  964. struct rt_memheap_item *prev_free; /**< prev free memheap item */
  965. #ifdef RT_USING_MEMTRACE
  966. rt_uint8_t owner_thread_name[4]; /**< owner thread name */
  967. #endif /* RT_USING_MEMTRACE */
  968. };
  969. /**
  970. * Base structure of memory heap object
  971. */
  972. struct rt_memheap
  973. {
  974. struct rt_object parent; /**< inherit from rt_object */
  975. void *start_addr; /**< pool start address and size */
  976. rt_size_t pool_size; /**< pool size */
  977. rt_size_t available_size; /**< available size */
  978. rt_size_t max_used_size; /**< maximum allocated size */
  979. struct rt_memheap_item *block_list; /**< used block list */
  980. struct rt_memheap_item *free_list; /**< free block list */
  981. struct rt_memheap_item free_header; /**< free block list header */
  982. struct rt_semaphore lock; /**< semaphore lock */
  983. rt_bool_t locked; /**< External lock mark */
  984. };
  985. #endif /* RT_USING_MEMHEAP */
  986. #ifdef RT_USING_MEMPOOL
  987. /**
  988. * Base structure of Memory pool object
  989. */
  990. struct rt_mempool
  991. {
  992. struct rt_object parent; /**< inherit from rt_object */
  993. void *start_address; /**< memory pool start */
  994. rt_size_t size; /**< size of memory pool */
  995. rt_size_t block_size; /**< size of memory blocks */
  996. rt_uint8_t *block_list; /**< memory blocks list */
  997. rt_size_t block_total_count; /**< numbers of memory block */
  998. rt_size_t block_free_count; /**< numbers of free memory block */
  999. rt_list_t suspend_thread; /**< threads pended on this resource */
  1000. };
  1001. typedef struct rt_mempool *rt_mp_t;
  1002. #endif /* RT_USING_MEMPOOL */
  1003. /**@}*/
  1004. #ifdef RT_USING_DEVICE
  1005. /**
  1006. * @addtogroup Device
  1007. */
  1008. /**@{*/
  1009. /**
  1010. * device (I/O) class type
  1011. */
  1012. enum rt_device_class_type
  1013. {
  1014. RT_Device_Class_Char = 0, /**< character device */
  1015. RT_Device_Class_Block, /**< block device */
  1016. RT_Device_Class_NetIf, /**< net interface */
  1017. RT_Device_Class_MTD, /**< memory device */
  1018. RT_Device_Class_CAN, /**< CAN device */
  1019. RT_Device_Class_RTC, /**< RTC device */
  1020. RT_Device_Class_Sound, /**< Sound device */
  1021. RT_Device_Class_Graphic, /**< Graphic device */
  1022. RT_Device_Class_I2CBUS, /**< I2C bus device */
  1023. RT_Device_Class_USBDevice, /**< USB slave device */
  1024. RT_Device_Class_USBHost, /**< USB host bus */
  1025. RT_Device_Class_USBOTG, /**< USB OTG bus */
  1026. RT_Device_Class_SPIBUS, /**< SPI bus device */
  1027. RT_Device_Class_SPIDevice, /**< SPI device */
  1028. RT_Device_Class_SDIO, /**< SDIO bus device */
  1029. RT_Device_Class_PM, /**< PM pseudo device */
  1030. RT_Device_Class_Pipe, /**< Pipe device */
  1031. RT_Device_Class_Portal, /**< Portal device */
  1032. RT_Device_Class_Timer, /**< Timer device */
  1033. RT_Device_Class_Miscellaneous, /**< Miscellaneous device */
  1034. RT_Device_Class_Sensor, /**< Sensor device */
  1035. RT_Device_Class_Touch, /**< Touch device */
  1036. RT_Device_Class_PHY, /**< PHY device */
  1037. RT_Device_Class_Security, /**< Security device */
  1038. RT_Device_Class_WLAN, /**< WLAN device */
  1039. RT_Device_Class_Pin, /**< Pin device */
  1040. RT_Device_Class_ADC, /**< ADC device */
  1041. RT_Device_Class_DAC, /**< DAC device */
  1042. RT_Device_Class_WDT, /**< WDT device */
  1043. RT_Device_Class_PWM, /**< PWM device */
  1044. RT_Device_Class_Bus, /**< Bus device */
  1045. RT_Device_Class_Unknown /**< unknown device */
  1046. };
  1047. /**
  1048. * device flags definitions
  1049. */
  1050. #define RT_DEVICE_FLAG_DEACTIVATE 0x000 /**< device is not not initialized */
  1051. #define RT_DEVICE_FLAG_RDONLY 0x001 /**< read only */
  1052. #define RT_DEVICE_FLAG_WRONLY 0x002 /**< write only */
  1053. #define RT_DEVICE_FLAG_RDWR 0x003 /**< read and write */
  1054. #define RT_DEVICE_FLAG_REMOVABLE 0x004 /**< removable device */
  1055. #define RT_DEVICE_FLAG_STANDALONE 0x008 /**< standalone device */
  1056. #define RT_DEVICE_FLAG_ACTIVATED 0x010 /**< device is activated */
  1057. #define RT_DEVICE_FLAG_SUSPENDED 0x020 /**< device is suspended */
  1058. #define RT_DEVICE_FLAG_STREAM 0x040 /**< stream mode */
  1059. #define RT_DEVICE_FLAG_INT_RX 0x100 /**< INT mode on Rx */
  1060. #define RT_DEVICE_FLAG_DMA_RX 0x200 /**< DMA mode on Rx */
  1061. #define RT_DEVICE_FLAG_INT_TX 0x400 /**< INT mode on Tx */
  1062. #define RT_DEVICE_FLAG_DMA_TX 0x800 /**< DMA mode on Tx */
  1063. #define RT_DEVICE_OFLAG_CLOSE 0x000 /**< device is closed */
  1064. #define RT_DEVICE_OFLAG_RDONLY 0x001 /**< read only access */
  1065. #define RT_DEVICE_OFLAG_WRONLY 0x002 /**< write only access */
  1066. #define RT_DEVICE_OFLAG_RDWR 0x003 /**< read and write */
  1067. #define RT_DEVICE_OFLAG_OPEN 0x008 /**< device is opened */
  1068. #define RT_DEVICE_OFLAG_MASK 0xf0f /**< mask of open flag */
  1069. /**
  1070. * general device commands
  1071. * 0x01 - 0x1F general device control commands
  1072. * 0x20 - 0x3F udevice control commands
  1073. * 0x40 - special device control commands
  1074. */
  1075. #define RT_DEVICE_CTRL_RESUME 0x01 /**< resume device */
  1076. #define RT_DEVICE_CTRL_SUSPEND 0x02 /**< suspend device */
  1077. #define RT_DEVICE_CTRL_CONFIG 0x03 /**< configure device */
  1078. #define RT_DEVICE_CTRL_CLOSE 0x04 /**< close device */
  1079. #define RT_DEVICE_CTRL_NOTIFY_SET 0x05 /**< set notify func */
  1080. #define RT_DEVICE_CTRL_SET_INT 0x06 /**< set interrupt */
  1081. #define RT_DEVICE_CTRL_CLR_INT 0x07 /**< clear interrupt */
  1082. #define RT_DEVICE_CTRL_GET_INT 0x08 /**< get interrupt status */
  1083. #define RT_DEVICE_CTRL_CONSOLE_OFLAG 0x09 /**< get console open flag */
  1084. #define RT_DEVICE_CTRL_MASK 0x1f /**< mask for contrl commands */
  1085. /**
  1086. * device control
  1087. */
  1088. #define RT_DEVICE_CTRL_BASE(Type) ((RT_Device_Class_##Type + 1) * 0x100)
  1089. /**
  1090. * special device commands
  1091. */
  1092. /* character device */
  1093. #define RT_DEVICE_CTRL_CHAR_STREAM (RT_DEVICE_CTRL_BASE(Char) + 1) /**< stream mode on char device */
  1094. /* block device */
  1095. #define RT_DEVICE_CTRL_BLK_GETGEOME (RT_DEVICE_CTRL_BASE(Block) + 1) /**< get geometry information */
  1096. #define RT_DEVICE_CTRL_BLK_SYNC (RT_DEVICE_CTRL_BASE(Block) + 2) /**< flush data to block device */
  1097. #define RT_DEVICE_CTRL_BLK_ERASE (RT_DEVICE_CTRL_BASE(Block) + 3) /**< erase block on block device */
  1098. #define RT_DEVICE_CTRL_BLK_AUTOREFRESH (RT_DEVICE_CTRL_BASE(Block) + 4) /**< block device : enter/exit auto refresh mode */
  1099. #define RT_DEVICE_CTRL_BLK_PARTITION (RT_DEVICE_CTRL_BASE(Block) + 5) /**< get block device partition */
  1100. /* net interface device*/
  1101. #define RT_DEVICE_CTRL_NETIF_GETMAC (RT_DEVICE_CTRL_BASE(NetIf) + 1) /**< get mac address */
  1102. /* mtd interface device*/
  1103. #define RT_DEVICE_CTRL_MTD_FORMAT (RT_DEVICE_CTRL_BASE(MTD) + 1) /**< format a MTD device */
  1104. typedef struct rt_device *rt_device_t;
  1105. #ifdef RT_USING_DEVICE_OPS
  1106. /**
  1107. * operations set for device object
  1108. */
  1109. struct rt_device_ops
  1110. {
  1111. /* common device interface */
  1112. rt_err_t (*init) (rt_device_t dev);
  1113. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  1114. rt_err_t (*close) (rt_device_t dev);
  1115. rt_ssize_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  1116. rt_ssize_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  1117. rt_err_t (*control)(rt_device_t dev, int cmd, void *args);
  1118. };
  1119. #endif /* RT_USING_DEVICE_OPS */
  1120. /**
  1121. * WaitQueue structure
  1122. */
  1123. struct rt_wqueue
  1124. {
  1125. rt_uint32_t flag;
  1126. rt_list_t waiting_list;
  1127. };
  1128. typedef struct rt_wqueue rt_wqueue_t;
  1129. /**
  1130. * Device structure
  1131. */
  1132. struct rt_device
  1133. {
  1134. struct rt_object parent; /**< inherit from rt_object */
  1135. #ifdef RT_USING_DM
  1136. struct rt_driver *drv;
  1137. void *dtb_node;
  1138. #endif
  1139. enum rt_device_class_type type; /**< device type */
  1140. rt_uint16_t flag; /**< device flag */
  1141. rt_uint16_t open_flag; /**< device open flag */
  1142. rt_uint8_t ref_count; /**< reference count */
  1143. rt_uint8_t device_id; /**< 0 - 255 */
  1144. /* device call back */
  1145. rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);
  1146. rt_err_t (*tx_complete)(rt_device_t dev, void *buffer);
  1147. #ifdef RT_USING_DEVICE_OPS
  1148. const struct rt_device_ops *ops;
  1149. #else
  1150. /* common device interface */
  1151. rt_err_t (*init) (rt_device_t dev);
  1152. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  1153. rt_err_t (*close) (rt_device_t dev);
  1154. rt_ssize_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  1155. rt_ssize_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  1156. rt_err_t (*control)(rt_device_t dev, int cmd, void *args);
  1157. #endif /* RT_USING_DEVICE_OPS */
  1158. #ifdef RT_USING_POSIX_DEVIO
  1159. const struct dfs_file_ops *fops;
  1160. struct rt_wqueue wait_queue;
  1161. #endif /* RT_USING_POSIX_DEVIO */
  1162. void *user_data; /**< device private data */
  1163. };
  1164. #define RT_DRIVER_MATCH_DTS (1<<0)
  1165. struct rt_device_id
  1166. {
  1167. const char *compatible;
  1168. void *data;
  1169. };
  1170. struct rt_driver
  1171. {
  1172. #ifdef RT_USING_DEVICE_OPS
  1173. const struct rt_device_ops *dev_ops;
  1174. #else
  1175. /* common device interface */
  1176. rt_err_t (*init) (rt_device_t dev);
  1177. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  1178. rt_err_t (*close) (rt_device_t dev);
  1179. rt_ssize_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  1180. rt_ssize_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  1181. rt_err_t (*control)(rt_device_t dev, int cmd, void *args);
  1182. #endif
  1183. const struct filesystem_ops *fops;
  1184. const char *name;
  1185. enum rt_device_class_type dev_type;
  1186. int device_size;
  1187. int flag;
  1188. const struct rt_device_id *dev_match;
  1189. int (*probe)(struct rt_device *dev);
  1190. int (*probe_init)(struct rt_device *dev);
  1191. int (*remove)(struct rt_device *dev);
  1192. const void *ops; /* driver-specific operations */
  1193. void *drv_priv_data;
  1194. };
  1195. typedef struct rt_driver *rt_driver_t;
  1196. /**
  1197. * Notify structure
  1198. */
  1199. struct rt_device_notify
  1200. {
  1201. void (*notify)(rt_device_t dev);
  1202. struct rt_device *dev;
  1203. };
  1204. #ifdef RT_USING_SMART
  1205. struct rt_channel
  1206. {
  1207. struct rt_ipc_object parent; /**< inherit from object */
  1208. struct rt_thread *reply; /**< the thread will be reply */
  1209. rt_list_t wait_msg; /**< the wait queue of sender msg */
  1210. rt_list_t wait_thread; /**< the wait queue of sender thread */
  1211. rt_wqueue_t reader_queue; /**< channel poll queue */
  1212. rt_uint8_t stat; /**< the status of this channel */
  1213. rt_ubase_t ref;
  1214. };
  1215. typedef struct rt_channel *rt_channel_t;
  1216. #endif
  1217. /**
  1218. * block device geometry structure
  1219. */
  1220. struct rt_device_blk_geometry
  1221. {
  1222. rt_uint64_t sector_count; /**< count of sectors */
  1223. rt_uint32_t bytes_per_sector; /**< number of bytes per sector */
  1224. rt_uint32_t block_size; /**< number of bytes to erase one block */
  1225. };
  1226. /**
  1227. * sector arrange struct on block device
  1228. */
  1229. struct rt_device_blk_sectors
  1230. {
  1231. rt_uint64_t sector_begin; /**< begin sector */
  1232. rt_uint64_t sector_end; /**< end sector */
  1233. };
  1234. /**
  1235. * cursor control command
  1236. */
  1237. #define RT_DEVICE_CTRL_CURSOR_SET_POSITION 0x10
  1238. #define RT_DEVICE_CTRL_CURSOR_SET_TYPE 0x11
  1239. /**
  1240. * graphic device control command
  1241. */
  1242. #define RTGRAPHIC_CTRL_RECT_UPDATE (RT_DEVICE_CTRL_BASE(Graphic) + 0)
  1243. #define RTGRAPHIC_CTRL_POWERON (RT_DEVICE_CTRL_BASE(Graphic) + 1)
  1244. #define RTGRAPHIC_CTRL_POWEROFF (RT_DEVICE_CTRL_BASE(Graphic) + 2)
  1245. #define RTGRAPHIC_CTRL_GET_INFO (RT_DEVICE_CTRL_BASE(Graphic) + 3)
  1246. #define RTGRAPHIC_CTRL_SET_MODE (RT_DEVICE_CTRL_BASE(Graphic) + 4)
  1247. #define RTGRAPHIC_CTRL_GET_EXT (RT_DEVICE_CTRL_BASE(Graphic) + 5)
  1248. #define RTGRAPHIC_CTRL_SET_BRIGHTNESS (RT_DEVICE_CTRL_BASE(Graphic) + 6)
  1249. #define RTGRAPHIC_CTRL_GET_BRIGHTNESS (RT_DEVICE_CTRL_BASE(Graphic) + 7)
  1250. #define RTGRAPHIC_CTRL_GET_MODE (RT_DEVICE_CTRL_BASE(Graphic) + 8)
  1251. #define RTGRAPHIC_CTRL_GET_STATUS (RT_DEVICE_CTRL_BASE(Graphic) + 9)
  1252. #define RTGRAPHIC_CTRL_PAN_DISPLAY (RT_DEVICE_CTRL_BASE(Graphic) + 10)
  1253. #define RTGRAPHIC_CTRL_WAIT_VSYNC (RT_DEVICE_CTRL_BASE(Graphic) + 11)
  1254. /* graphic device */
  1255. enum
  1256. {
  1257. RTGRAPHIC_PIXEL_FORMAT_MONO = 0,
  1258. RTGRAPHIC_PIXEL_FORMAT_GRAY4,
  1259. RTGRAPHIC_PIXEL_FORMAT_GRAY16,
  1260. RTGRAPHIC_PIXEL_FORMAT_RGB332,
  1261. RTGRAPHIC_PIXEL_FORMAT_RGB444,
  1262. RTGRAPHIC_PIXEL_FORMAT_RGB565,
  1263. RTGRAPHIC_PIXEL_FORMAT_RGB565P,
  1264. RTGRAPHIC_PIXEL_FORMAT_BGR565 = RTGRAPHIC_PIXEL_FORMAT_RGB565P,
  1265. RTGRAPHIC_PIXEL_FORMAT_RGB666,
  1266. RTGRAPHIC_PIXEL_FORMAT_RGB888,
  1267. RTGRAPHIC_PIXEL_FORMAT_BGR888,
  1268. RTGRAPHIC_PIXEL_FORMAT_ARGB888,
  1269. RTGRAPHIC_PIXEL_FORMAT_ABGR888,
  1270. RTGRAPHIC_PIXEL_FORMAT_RESERVED,
  1271. };
  1272. /**
  1273. * build a pixel position according to (x, y) coordinates.
  1274. */
  1275. #define RTGRAPHIC_PIXEL_POSITION(x, y) ((x << 16) | y)
  1276. /**
  1277. * graphic device information structure
  1278. */
  1279. struct rt_device_graphic_info
  1280. {
  1281. rt_uint8_t pixel_format; /**< graphic format */
  1282. rt_uint8_t bits_per_pixel; /**< bits per pixel */
  1283. rt_uint16_t pitch; /**< bytes per line */
  1284. rt_uint16_t width; /**< width of graphic device */
  1285. rt_uint16_t height; /**< height of graphic device */
  1286. rt_uint8_t *framebuffer; /**< frame buffer */
  1287. rt_uint32_t smem_len; /**< allocated frame buffer size */
  1288. };
  1289. /**
  1290. * rectangle information structure
  1291. */
  1292. struct rt_device_rect_info
  1293. {
  1294. rt_uint16_t x; /**< x coordinate */
  1295. rt_uint16_t y; /**< y coordinate */
  1296. rt_uint16_t width; /**< width */
  1297. rt_uint16_t height; /**< height */
  1298. };
  1299. /**
  1300. * graphic operations
  1301. */
  1302. struct rt_device_graphic_ops
  1303. {
  1304. void (*set_pixel) (const char *pixel, int x, int y);
  1305. void (*get_pixel) (char *pixel, int x, int y);
  1306. void (*draw_hline)(const char *pixel, int x1, int x2, int y);
  1307. void (*draw_vline)(const char *pixel, int x, int y1, int y2);
  1308. void (*blit_line) (const char *pixel, int x, int y, rt_size_t size);
  1309. };
  1310. #define rt_graphix_ops(device) ((struct rt_device_graphic_ops *)(device->user_data))
  1311. /**@}*/
  1312. #endif /* RT_USING_DEVICE */
  1313. #ifdef __cplusplus
  1314. }
  1315. #endif
  1316. #ifdef __cplusplus
  1317. /* RT-Thread definitions for C++ */
  1318. namespace rtthread {
  1319. enum TICK_WAIT {
  1320. WAIT_NONE = 0,
  1321. WAIT_FOREVER = -1,
  1322. };
  1323. }
  1324. #endif /* __cplusplus */
  1325. #endif /* __RT_DEF_H__ */