rtdef.h 56 KB

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