rtdef.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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. * 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. */
  56. #ifndef __RT_DEF_H__
  57. #define __RT_DEF_H__
  58. #include <rtconfig.h>
  59. #ifdef RT_USING_LIBC
  60. #if !defined(RT_USING_LIBC_ISO_ONLY) && !defined(RT_VER_NUM)
  61. #define RT_USING_LIBC_ISO_ONLY (1)
  62. #else
  63. #define RT_USING_LIBC_ISO_ONLY (0)
  64. #endif /* !defined(RT_USING_LIBC_ISO_ONLY) && !defined(RT_VER_NUM) */
  65. #include <stdint.h>
  66. #include <stddef.h>
  67. #include <stdarg.h>
  68. #if !RT_USING_LIBC_ISO_ONLY
  69. #include <sys/types.h>
  70. #include <sys/errno.h>
  71. #if defined(RT_USING_SIGNALS) || defined(RT_USING_SMART)
  72. #include <sys/signal.h>
  73. #endif /* defined(RT_USING_SIGNALS) || defined(RT_USING_SMART) */
  74. #endif /* !RT_USING_LIBC_ISO_ONLY */
  75. #endif /* RT_USING_LIBC */
  76. #ifdef __cplusplus
  77. extern "C" {
  78. #endif
  79. /**
  80. * @addtogroup BasicDef
  81. */
  82. /**@{*/
  83. /* RT-Thread version information */
  84. #define RT_VERSION_MAJOR 5 /**< Major version number (X.x.x) */
  85. #define RT_VERSION_MINOR 1 /**< Minor version number (x.X.x) */
  86. #define RT_VERSION_PATCH 0 /**< Patch version number (x.x.X) */
  87. /* e.g. #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(4, 1, 0) */
  88. #define RT_VERSION_CHECK(major, minor, revise) ((major * 10000) + (minor * 100) + revise)
  89. /* RT-Thread version */
  90. #define RTTHREAD_VERSION RT_VERSION_CHECK(RT_VERSION_MAJOR, RT_VERSION_MINOR, RT_VERSION_PATCH)
  91. /* RT-Thread basic data type definitions */
  92. typedef int rt_bool_t; /**< boolean type */
  93. typedef signed long rt_base_t; /**< Nbit CPU related date type */
  94. typedef unsigned long rt_ubase_t; /**< Nbit unsigned CPU related data type */
  95. #ifndef RT_USING_ARCH_DATA_TYPE
  96. #ifdef RT_USING_LIBC
  97. typedef int8_t rt_int8_t; /**< 8bit integer type */
  98. typedef int16_t rt_int16_t; /**< 16bit integer type */
  99. typedef int32_t rt_int32_t; /**< 32bit integer type */
  100. typedef uint8_t rt_uint8_t; /**< 8bit unsigned integer type */
  101. typedef uint16_t rt_uint16_t; /**< 16bit unsigned integer type */
  102. typedef uint32_t rt_uint32_t; /**< 32bit unsigned integer type */
  103. typedef int64_t rt_int64_t; /**< 64bit integer type */
  104. typedef uint64_t rt_uint64_t; /**< 64bit unsigned integer type */
  105. #else
  106. typedef signed char rt_int8_t; /**< 8bit integer type */
  107. typedef signed short rt_int16_t; /**< 16bit integer type */
  108. typedef signed int rt_int32_t; /**< 32bit integer type */
  109. typedef unsigned char rt_uint8_t; /**< 8bit unsigned integer type */
  110. typedef unsigned short rt_uint16_t; /**< 16bit unsigned integer type */
  111. typedef unsigned int rt_uint32_t; /**< 32bit unsigned integer type */
  112. #ifdef ARCH_CPU_64BIT
  113. typedef signed long rt_int64_t; /**< 64bit integer type */
  114. typedef unsigned long rt_uint64_t; /**< 64bit unsigned integer type */
  115. #else
  116. typedef signed long long rt_int64_t; /**< 64bit integer type */
  117. typedef unsigned long long rt_uint64_t; /**< 64bit unsigned integer type */
  118. #endif /* ARCH_CPU_64BIT */
  119. #endif /* RT_USING_LIBC */
  120. #endif /* RT_USING_ARCH_DATA_TYPE */
  121. #if defined(RT_USING_LIBC) && !RT_USING_LIBC_ISO_ONLY
  122. typedef size_t rt_size_t; /**< Type for size number */
  123. typedef ssize_t rt_ssize_t; /**< Used for a count of bytes or an error indication */
  124. #else
  125. typedef rt_ubase_t rt_size_t; /**< Type for size number */
  126. typedef rt_base_t rt_ssize_t; /**< Used for a count of bytes or an error indication */
  127. #endif /* defined(RT_USING_LIBC) && !RT_USING_LIBC_ISO_ONLY */
  128. typedef rt_base_t rt_err_t; /**< Type for error number */
  129. typedef rt_uint32_t rt_time_t; /**< Type for time stamp */
  130. typedef rt_uint32_t rt_tick_t; /**< Type for tick count */
  131. typedef rt_base_t rt_flag_t; /**< Type for flags */
  132. typedef rt_ubase_t rt_dev_t; /**< Type for device */
  133. typedef rt_base_t rt_off_t; /**< Type for offset */
  134. #if !defined(__cplusplus)
  135. #if defined(RT_USING_HW_ATOMIC)
  136. typedef rt_base_t rt_atomic_t;
  137. #elif defined(RT_USING_STDC_ATOMIC)
  138. #include <stdatomic.h>
  139. typedef atomic_size_t rt_atomic_t;
  140. #else
  141. /* To detect std atomic */
  142. #if defined(RT_USING_LIBC) && defined(__GNUC__) && !defined(__STDC_NO_ATOMICS__)
  143. #include <stdatomic.h>
  144. typedef atomic_size_t rt_atomic_t;
  145. #else
  146. typedef rt_base_t rt_atomic_t;
  147. #endif /* __GNUC__ && !__STDC_NO_ATOMICS__ */
  148. #endif /* RT_USING_STDC_ATOMIC */
  149. #endif /* __cplusplus */
  150. /* boolean type definitions */
  151. #define RT_TRUE 1 /**< boolean true */
  152. #define RT_FALSE 0 /**< boolean fails */
  153. /* null pointer definition */
  154. #define RT_NULL 0
  155. /**@}*/
  156. /* maximum value of base type */
  157. #ifdef RT_USING_LIBC
  158. #define RT_UINT8_MAX UINT8_MAX /**< Maximum number of UINT8 */
  159. #define RT_UINT16_MAX UINT16_MAX /**< Maximum number of UINT16 */
  160. #define RT_UINT32_MAX UINT32_MAX /**< Maximum number of UINT32 */
  161. #else
  162. #define RT_UINT8_MAX 0xff /**< Maximum number of UINT8 */
  163. #define RT_UINT16_MAX 0xffff /**< Maximum number of UINT16 */
  164. #define RT_UINT32_MAX 0xffffffff /**< Maximum number of UINT32 */
  165. #endif /* RT_USING_LIBC */
  166. #define RT_TICK_MAX RT_UINT32_MAX /**< Maximum number of tick */
  167. /* maximum value of ipc type */
  168. #define RT_SEM_VALUE_MAX RT_UINT16_MAX /**< Maximum number of semaphore .value */
  169. #define RT_MUTEX_VALUE_MAX RT_UINT16_MAX /**< Maximum number of mutex .value */
  170. #define RT_MUTEX_HOLD_MAX RT_UINT8_MAX /**< Maximum number of mutex .hold */
  171. #define RT_MB_ENTRY_MAX RT_UINT16_MAX /**< Maximum number of mailbox .entry */
  172. #define RT_MQ_ENTRY_MAX RT_UINT16_MAX /**< Maximum number of message queue .entry */
  173. /* Common Utilities */
  174. #define RT_UNUSED(x) ((void)x)
  175. /* compile time assertion */
  176. #define RT_STATIC_ASSERT(name, expn) typedef char _static_assert_##name[(expn)?1:-1]
  177. /* Compiler Related Definitions */
  178. #if defined(__ARMCC_VERSION) /* ARM Compiler */
  179. #define rt_section(x) __attribute__((section(x)))
  180. #define rt_used __attribute__((used))
  181. #define rt_align(n) __attribute__((aligned(n)))
  182. #define rt_weak __attribute__((weak))
  183. #define rt_inline static __inline
  184. /* module compiling */
  185. #ifdef RT_USING_MODULE
  186. #define RTT_API __declspec(dllimport)
  187. #else
  188. #define RTT_API __declspec(dllexport)
  189. #endif /* RT_USING_MODULE */
  190. #elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
  191. #define rt_section(x) @ x
  192. #define rt_used __root
  193. #define PRAGMA(x) _Pragma(#x)
  194. #define rt_align(n) PRAGMA(data_alignment=n)
  195. #define rt_weak __weak
  196. #define rt_inline static inline
  197. #define RTT_API
  198. #elif defined (__GNUC__) /* GNU GCC Compiler */
  199. #ifndef RT_USING_LIBC
  200. /* the version of GNU GCC must be greater than 4.x */
  201. typedef __builtin_va_list __gnuc_va_list;
  202. typedef __gnuc_va_list va_list;
  203. #define va_start(v,l) __builtin_va_start(v,l)
  204. #define va_end(v) __builtin_va_end(v)
  205. #define va_arg(v,l) __builtin_va_arg(v,l)
  206. #endif /* RT_USING_LIBC */
  207. #define __RT_STRINGIFY(x...) #x
  208. #define RT_STRINGIFY(x...) __RT_STRINGIFY(x)
  209. #define rt_section(x) __attribute__((section(x)))
  210. #define rt_used __attribute__((used))
  211. #define rt_align(n) __attribute__((aligned(n)))
  212. #define rt_weak __attribute__((weak))
  213. #define rt_noreturn __attribute__ ((noreturn))
  214. #define rt_inline static __inline
  215. #define RTT_API
  216. #elif defined (__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
  217. #define rt_section(x) __attribute__((section(x)))
  218. #define rt_used __attribute__((used))
  219. #define rt_align(n) __attribute__((aligned(n)))
  220. #define rt_weak __attribute__((weak))
  221. #define rt_inline static inline
  222. #define RTT_API
  223. #elif defined (_MSC_VER)
  224. #define rt_section(x)
  225. #define rt_used
  226. #define rt_align(n) __declspec(align(n))
  227. #define rt_weak
  228. #define rt_inline static __inline
  229. #define RTT_API
  230. #elif defined (__TI_COMPILER_VERSION__)
  231. /* The way that TI compiler set section is different from other(at least
  232. * GCC and MDK) compilers. See ARM Optimizing C/C++ Compiler 5.9.3 for more
  233. * details. */
  234. #define rt_section(x) __attribute__((section(x)))
  235. #ifdef __TI_EABI__
  236. #define rt_used __attribute__((retain)) __attribute__((used))
  237. #else
  238. #define rt_used __attribute__((used))
  239. #endif
  240. #define PRAGMA(x) _Pragma(#x)
  241. #define rt_align(n) __attribute__((aligned(n)))
  242. #ifdef __TI_EABI__
  243. #define rt_weak __attribute__((weak))
  244. #else
  245. #define rt_weak
  246. #endif
  247. #define rt_inline static inline
  248. #define RTT_API
  249. #elif defined (__TASKING__)
  250. #define rt_section(x) __attribute__((section(x)))
  251. #define rt_used __attribute__((used, protect))
  252. #define PRAGMA(x) _Pragma(#x)
  253. #define rt_align(n) __attribute__((__align(n)))
  254. #define rt_weak __attribute__((weak))
  255. #define rt_inline static 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_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
  290. #else
  291. #ifdef RT_DEBUGING_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
  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 Code
  363. */
  364. /**@{*/
  365. /* RT-Thread error code definitions */
  366. #if defined(RT_USING_LIBC) && !RT_USING_LIBC_ISO_ONLY
  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) && !RT_USING_LIBC_ISO_ONLY */
  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. /**
  447. * @addtogroup KernelObject
  448. */
  449. /**@{*/
  450. /*
  451. * kernel object macros
  452. */
  453. #define RT_OBJECT_FLAG_MODULE 0x80 /**< is module object. */
  454. /**
  455. * Base structure of Kernel object
  456. */
  457. struct rt_object
  458. {
  459. #if RT_NAME_MAX > 0
  460. char name[RT_NAME_MAX]; /**< dynamic name of kernel object */
  461. #else
  462. const char *name; /**< static name of kernel object */
  463. #endif /* RT_NAME_MAX > 0 */
  464. rt_uint8_t type; /**< type of kernel object */
  465. rt_uint8_t flag; /**< flag of kernel object */
  466. #ifdef RT_USING_MODULE
  467. void * module_id; /**< id of application module */
  468. #endif /* RT_USING_MODULE */
  469. #ifdef RT_USING_SMART
  470. int lwp_ref_count; /**< ref count for lwp */
  471. #endif /* RT_USING_SMART */
  472. rt_list_t list; /**< list node of kernel object */
  473. };
  474. typedef struct rt_object *rt_object_t; /**< Type for kernel objects. */
  475. /**
  476. * The object type can be one of the follows with specific
  477. * macros enabled:
  478. * - Thread
  479. * - Semaphore
  480. * - Mutex
  481. * - Event
  482. * - MailBox
  483. * - MessageQueue
  484. * - MemHeap
  485. * - MemPool
  486. * - Device
  487. * - Timer
  488. * - Module
  489. * - Unknown
  490. * - Static
  491. */
  492. enum rt_object_class_type
  493. {
  494. RT_Object_Class_Null = 0x00, /**< The object is not used. */
  495. RT_Object_Class_Thread = 0x01, /**< The object is a thread. */
  496. RT_Object_Class_Semaphore = 0x02, /**< The object is a semaphore. */
  497. RT_Object_Class_Mutex = 0x03, /**< The object is a mutex. */
  498. RT_Object_Class_Event = 0x04, /**< The object is a event. */
  499. RT_Object_Class_MailBox = 0x05, /**< The object is a mail box. */
  500. RT_Object_Class_MessageQueue = 0x06, /**< The object is a message queue. */
  501. RT_Object_Class_MemHeap = 0x07, /**< The object is a memory heap. */
  502. RT_Object_Class_MemPool = 0x08, /**< The object is a memory pool. */
  503. RT_Object_Class_Device = 0x09, /**< The object is a device. */
  504. RT_Object_Class_Timer = 0x0a, /**< The object is a timer. */
  505. RT_Object_Class_Module = 0x0b, /**< The object is a module. */
  506. RT_Object_Class_Memory = 0x0c, /**< The object is a memory. */
  507. RT_Object_Class_Channel = 0x0d, /**< The object is a channel */
  508. RT_Object_Class_Custom = 0x0e, /**< The object is a custom object */
  509. RT_Object_Class_Unknown = 0x0f, /**< The object is unknown. */
  510. RT_Object_Class_Static = 0x80 /**< The object is a static object. */
  511. };
  512. /**
  513. * The information of the kernel object
  514. */
  515. struct rt_object_information
  516. {
  517. enum rt_object_class_type type; /**< object class type */
  518. rt_list_t object_list; /**< object list */
  519. rt_size_t object_size; /**< object size */
  520. };
  521. /**
  522. * The hook function call macro
  523. */
  524. #ifndef RT_USING_HOOK
  525. #define __ON_HOOK_ARGS(__hook, argv)
  526. #define RT_OBJECT_HOOK_CALL(func, argv)
  527. #else
  528. #define RT_OBJECT_HOOK_CALL(func, argv) __on_##func argv
  529. #ifdef RT_HOOK_USING_FUNC_PTR
  530. #define __ON_HOOK_ARGS(__hook, argv) do {if ((__hook) != RT_NULL) __hook argv; } while (0)
  531. #else
  532. #define __ON_HOOK_ARGS(__hook, argv)
  533. #endif /* RT_HOOK_USING_FUNC_PTR */
  534. #endif /* RT_USING_HOOK */
  535. #ifndef __on_rt_interrupt_switch_hook
  536. #define __on_rt_interrupt_switch_hook() __ON_HOOK_ARGS(rt_interrupt_switch_hook, ())
  537. #endif
  538. #ifndef __on_rt_malloc_hook
  539. #define __on_rt_malloc_hook(addr, size) __ON_HOOK_ARGS(rt_malloc_hook, (addr, size))
  540. #endif
  541. #ifndef __on_rt_free_hook
  542. #define __on_rt_free_hook(rmem) __ON_HOOK_ARGS(rt_free_hook, (rmem))
  543. #endif
  544. /**@}*/
  545. /**
  546. * @addtogroup Clock
  547. */
  548. /**@{*/
  549. /**
  550. * clock & timer macros
  551. */
  552. #define RT_TIMER_FLAG_DEACTIVATED 0x0 /**< timer is deactive */
  553. #define RT_TIMER_FLAG_ACTIVATED 0x1 /**< timer is active */
  554. #define RT_TIMER_FLAG_ONE_SHOT 0x0 /**< one shot timer */
  555. #define RT_TIMER_FLAG_PERIODIC 0x2 /**< periodic timer */
  556. #define RT_TIMER_FLAG_HARD_TIMER 0x0 /**< hard timer,the timer's callback function will be called in tick isr. */
  557. #define RT_TIMER_FLAG_SOFT_TIMER 0x4 /**< soft timer,the timer's callback function will be called in timer thread. */
  558. #define RT_TIMER_CTRL_SET_TIME 0x0 /**< set timer control command */
  559. #define RT_TIMER_CTRL_GET_TIME 0x1 /**< get timer control command */
  560. #define RT_TIMER_CTRL_SET_ONESHOT 0x2 /**< change timer to one shot */
  561. #define RT_TIMER_CTRL_SET_PERIODIC 0x3 /**< change timer to periodic */
  562. #define RT_TIMER_CTRL_GET_STATE 0x4 /**< get timer run state active or deactive*/
  563. #define RT_TIMER_CTRL_GET_REMAIN_TIME 0x5 /**< get the remaining hang time */
  564. #define RT_TIMER_CTRL_GET_FUNC 0x6 /**< get timer timeout func */
  565. #define RT_TIMER_CTRL_SET_FUNC 0x7 /**< set timer timeout func */
  566. #define RT_TIMER_CTRL_GET_PARM 0x8 /**< get timer parameter */
  567. #define RT_TIMER_CTRL_SET_PARM 0x9 /**< get timer parameter */
  568. #ifndef RT_TIMER_SKIP_LIST_LEVEL
  569. #define RT_TIMER_SKIP_LIST_LEVEL 1
  570. #endif
  571. /* 1 or 3 */
  572. #ifndef RT_TIMER_SKIP_LIST_MASK
  573. #define RT_TIMER_SKIP_LIST_MASK 0x3 /**< Timer skips the list mask */
  574. #endif
  575. /**
  576. * timer structure
  577. */
  578. struct rt_timer
  579. {
  580. struct rt_object parent; /**< inherit from rt_object */
  581. rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL];
  582. void (*timeout_func)(void *parameter); /**< timeout function */
  583. void *parameter; /**< timeout function's parameter */
  584. rt_tick_t init_tick; /**< timer timeout tick */
  585. rt_tick_t timeout_tick; /**< timeout tick */
  586. };
  587. typedef struct rt_timer *rt_timer_t;
  588. /**@}*/
  589. /**
  590. * @addtogroup Signal
  591. */
  592. /**@{*/
  593. #ifdef RT_USING_SIGNALS
  594. #define RT_SIG_MAX 32
  595. typedef unsigned long rt_sigset_t;
  596. typedef siginfo_t rt_siginfo_t;
  597. typedef void (*rt_sighandler_t)(int signo);
  598. #endif /* RT_USING_SIGNALS */
  599. /**@}*/
  600. /**
  601. * @addtogroup Thread
  602. */
  603. /**@{*/
  604. /*
  605. * Thread
  606. */
  607. /*
  608. * thread state definitions
  609. */
  610. #define RT_THREAD_INIT 0x00 /**< Initialized status */
  611. #define RT_THREAD_CLOSE 0x01 /**< Closed status */
  612. #define RT_THREAD_READY 0x02 /**< Ready status */
  613. #define RT_THREAD_RUNNING 0x03 /**< Running status */
  614. /*
  615. * for rt_thread_suspend_with_flag()
  616. */
  617. enum
  618. {
  619. RT_INTERRUPTIBLE = 0,
  620. RT_KILLABLE,
  621. RT_UNINTERRUPTIBLE,
  622. };
  623. #define RT_THREAD_SUSPEND_MASK 0x04
  624. #define RT_SIGNAL_COMMON_WAKEUP_MASK 0x02
  625. #define RT_SIGNAL_KILL_WAKEUP_MASK 0x01
  626. #define RT_THREAD_SUSPEND_INTERRUPTIBLE (RT_THREAD_SUSPEND_MASK) /**< Suspend interruptable 0x4 */
  627. #define RT_THREAD_SUSPEND RT_THREAD_SUSPEND_INTERRUPTIBLE
  628. #define RT_THREAD_SUSPEND_KILLABLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK) /**< Suspend with killable 0x6 */
  629. #define RT_THREAD_SUSPEND_UNINTERRUPTIBLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK | RT_SIGNAL_KILL_WAKEUP_MASK) /**< Suspend with uninterruptable 0x7 */
  630. #define RT_THREAD_STAT_MASK 0x07
  631. #define RT_THREAD_STAT_YIELD 0x08 /**< indicate whether remaining_tick has been reloaded since last schedule */
  632. #define RT_THREAD_STAT_YIELD_MASK RT_THREAD_STAT_YIELD
  633. #define RT_THREAD_STAT_SIGNAL 0x10 /**< task hold signals */
  634. #define RT_THREAD_STAT_SIGNAL_READY (RT_THREAD_STAT_SIGNAL | RT_THREAD_READY)
  635. #define RT_THREAD_STAT_SIGNAL_WAIT 0x20 /**< task is waiting for signals */
  636. #define RT_THREAD_STAT_SIGNAL_PENDING 0x40 /**< signals is held and it has not been procressed */
  637. #define RT_THREAD_STAT_SIGNAL_MASK 0xf0
  638. /**
  639. * thread control command definitions
  640. */
  641. #define RT_THREAD_CTRL_STARTUP 0x00 /**< Startup thread. */
  642. #define RT_THREAD_CTRL_CLOSE 0x01 /**< Close thread. */
  643. #define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */
  644. #define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */
  645. #define RT_THREAD_CTRL_BIND_CPU 0x04 /**< Set thread bind cpu. */
  646. #ifdef RT_USING_SMP
  647. #define RT_CPU_DETACHED RT_CPUS_NR /**< The thread not running on cpu. */
  648. #define RT_CPU_MASK ((1 << RT_CPUS_NR) - 1) /**< All CPUs mask bit. */
  649. #ifndef RT_SCHEDULE_IPI
  650. #define RT_SCHEDULE_IPI 0
  651. #endif /* RT_SCHEDULE_IPI */
  652. #ifndef RT_STOP_IPI
  653. #define RT_STOP_IPI 1
  654. #endif /* RT_STOP_IPI */
  655. struct rt_cpu_usage_stats
  656. {
  657. rt_uint64_t user;
  658. rt_uint64_t system;
  659. rt_uint64_t irq;
  660. rt_uint64_t idle;
  661. };
  662. typedef struct rt_cpu_usage_stats *rt_cpu_usage_stats_t;
  663. /**
  664. * CPUs definitions
  665. *
  666. */
  667. struct rt_cpu
  668. {
  669. struct rt_thread *current_thread;
  670. struct rt_thread *idle_thread;
  671. rt_uint16_t irq_nest;
  672. rt_uint8_t irq_switch_flag;
  673. rt_uint8_t current_priority;
  674. rt_list_t priority_table[RT_THREAD_PRIORITY_MAX];
  675. #if RT_THREAD_PRIORITY_MAX > 32
  676. rt_uint32_t priority_group;
  677. rt_uint8_t ready_table[32];
  678. #else
  679. rt_uint32_t priority_group;
  680. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  681. rt_tick_t tick;
  682. #ifdef RT_USING_SMART
  683. struct rt_cpu_usage_stats cpu_stat;
  684. #endif
  685. };
  686. typedef struct rt_cpu *rt_cpu_t;
  687. #endif /* RT_USING_SMP */
  688. struct rt_thread;
  689. #ifdef RT_USING_SMART
  690. typedef rt_err_t (*rt_wakeup_func_t)(void *object, struct rt_thread *thread);
  691. struct rt_wakeup
  692. {
  693. rt_wakeup_func_t func;
  694. void *user_data;
  695. };
  696. #define _LWP_NSIG 64
  697. #ifdef ARCH_CPU_64BIT
  698. #define _LWP_NSIG_BPW 64
  699. #else
  700. #define _LWP_NSIG_BPW 32
  701. #endif
  702. #define _LWP_NSIG_WORDS (RT_ALIGN(_LWP_NSIG, _LWP_NSIG_BPW) / _LWP_NSIG_BPW)
  703. typedef void (*lwp_sighandler_t)(int);
  704. typedef void (*lwp_sigaction_t)(int signo, siginfo_t *info, void *context);
  705. typedef struct {
  706. unsigned long sig[_LWP_NSIG_WORDS];
  707. } lwp_sigset_t;
  708. #if _LWP_NSIG <= 64
  709. #define lwp_sigmask(signo) ((lwp_sigset_t){.sig = {[0] = ((long)(1u << ((signo)-1)))}})
  710. #define lwp_sigset_init(mask) ((lwp_sigset_t){.sig = {[0] = (long)(mask)}})
  711. #endif
  712. struct lwp_sigaction {
  713. union {
  714. void (*_sa_handler)(int);
  715. void (*_sa_sigaction)(int, siginfo_t *, void *);
  716. } __sa_handler;
  717. lwp_sigset_t sa_mask;
  718. int sa_flags;
  719. void (*sa_restorer)(void);
  720. };
  721. typedef struct lwp_siginfo {
  722. rt_list_t node;
  723. struct {
  724. int signo;
  725. int code;
  726. long value;
  727. int from_tid;
  728. pid_t from_pid;
  729. } ksiginfo;
  730. } *lwp_siginfo_t;
  731. typedef struct lwp_sigqueue {
  732. rt_list_t siginfo_list;
  733. lwp_sigset_t sigset_pending;
  734. } *lwp_sigqueue_t;
  735. struct lwp_thread_signal {
  736. lwp_sigset_t sigset_mask;
  737. struct lwp_sigqueue sig_queue;
  738. };
  739. struct rt_user_context
  740. {
  741. void *sp;
  742. void *pc;
  743. void *flag;
  744. void *ctx;
  745. };
  746. #endif
  747. typedef void (*rt_thread_cleanup_t)(struct rt_thread *tid);
  748. /**
  749. * Thread structure
  750. */
  751. struct rt_thread
  752. {
  753. struct rt_object parent;
  754. rt_list_t tlist; /**< the thread list */
  755. /* stack point and entry */
  756. void *sp; /**< stack point */
  757. void *entry; /**< entry */
  758. void *parameter; /**< parameter */
  759. void *stack_addr; /**< stack address */
  760. rt_uint32_t stack_size; /**< stack size */
  761. /* error code */
  762. rt_err_t error; /**< error code */
  763. rt_uint8_t stat; /**< thread status */
  764. #ifdef RT_USING_SMP
  765. rt_uint8_t bind_cpu; /**< thread is bind to cpu */
  766. rt_uint8_t oncpu; /**< process on cpu */
  767. rt_uint16_t scheduler_lock_nest; /**< scheduler lock count */
  768. rt_int16_t cpus_lock_nest; /**< cpus lock count */
  769. rt_uint16_t critical_lock_nest; /**< critical lock count */
  770. #endif /*RT_USING_SMP*/
  771. /* priority */
  772. rt_uint8_t current_priority; /**< current priority */
  773. rt_uint8_t init_priority; /**< initialized priority */
  774. #if RT_THREAD_PRIORITY_MAX > 32
  775. rt_uint8_t number;
  776. rt_uint8_t high_mask;
  777. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  778. rt_uint32_t number_mask; /**< priority number mask */
  779. #ifdef RT_USING_MUTEX
  780. /* object for IPC */
  781. rt_list_t taken_object_list;
  782. rt_object_t pending_object;
  783. #endif
  784. #ifdef RT_USING_EVENT
  785. /* thread event */
  786. rt_uint32_t event_set;
  787. rt_uint8_t event_info;
  788. #endif /* RT_USING_EVENT */
  789. #ifdef RT_USING_SIGNALS
  790. rt_sigset_t sig_pending; /**< the pending signals */
  791. rt_sigset_t sig_mask; /**< the mask bits of signal */
  792. #ifndef RT_USING_SMP
  793. void *sig_ret; /**< the return stack pointer from signal */
  794. #endif /* RT_USING_SMP */
  795. rt_sighandler_t *sig_vectors; /**< vectors of signal handler */
  796. void *si_list; /**< the signal infor list */
  797. #endif /* RT_USING_SIGNALS */
  798. rt_ubase_t init_tick; /**< thread's initialized tick */
  799. rt_ubase_t remaining_tick; /**< remaining tick */
  800. #ifdef RT_USING_CPU_USAGE
  801. rt_uint64_t duration_tick; /**< cpu usage tick */
  802. #endif /* RT_USING_CPU_USAGE */
  803. #ifdef RT_USING_PTHREADS
  804. void *pthread_data; /**< the handle of pthread data, adapt 32/64bit */
  805. #endif /* RT_USING_PTHREADS */
  806. struct rt_timer thread_timer; /**< built-in thread timer */
  807. rt_thread_cleanup_t cleanup; /**< cleanup function when thread exit */
  808. /* light weight process if present */
  809. #ifdef RT_USING_SMART
  810. void *msg_ret; /**< the return msg */
  811. void *lwp; /**< the lwp reference */
  812. /* for user create */
  813. void *user_entry;
  814. void *user_stack;
  815. rt_uint32_t user_stack_size;
  816. rt_uint32_t *kernel_sp; /**< kernel stack point */
  817. rt_list_t sibling; /**< next thread of same process */
  818. struct lwp_thread_signal signal; /**< lwp signal for user-space thread */
  819. struct rt_user_context user_ctx; /**< user space context */
  820. struct rt_wakeup wakeup; /**< wakeup data */
  821. int exit_request;
  822. int tid;
  823. rt_uint64_t user_time;
  824. rt_uint64_t system_time;
  825. #ifndef ARCH_MM_MMU
  826. lwp_sighandler_t signal_handler[32];
  827. #else
  828. int step_exec;
  829. int debug_attach_req;
  830. int debug_ret_user;
  831. int debug_suspend;
  832. struct rt_hw_exp_stack *regs;
  833. void *thread_idr; /** lwp thread indicator */
  834. int *clear_child_tid;
  835. #endif /* ARCH_MM_MMU */
  836. #endif /* RT_USING_SMART */
  837. rt_ubase_t user_data; /**< private user data beyond this thread */
  838. };
  839. typedef struct rt_thread *rt_thread_t;
  840. #ifdef RT_USING_SMART
  841. #define IS_USER_MODE(t) ((t)->user_ctx.ctx == RT_NULL)
  842. #endif /* RT_USING_SMART */
  843. /**@}*/
  844. /**
  845. * @addtogroup IPC
  846. */
  847. /**@{*/
  848. /**
  849. * IPC flags and control command definitions
  850. */
  851. #define RT_IPC_FLAG_FIFO 0x00 /**< FIFOed IPC. @ref IPC. */
  852. #define RT_IPC_FLAG_PRIO 0x01 /**< PRIOed IPC. @ref IPC. */
  853. #define RT_IPC_CMD_UNKNOWN 0x00 /**< unknown IPC command */
  854. #define RT_IPC_CMD_RESET 0x01 /**< reset IPC object */
  855. #define RT_IPC_CMD_GET_STATE 0x02 /**< get the state of IPC object */
  856. #define RT_WAITING_FOREVER -1 /**< Block forever until get resource. */
  857. #define RT_WAITING_NO 0 /**< Non-block. */
  858. /**
  859. * Base structure of IPC object
  860. */
  861. struct rt_ipc_object
  862. {
  863. struct rt_object parent; /**< inherit from rt_object */
  864. rt_list_t suspend_thread; /**< threads pended on this resource */
  865. };
  866. #ifdef RT_USING_SEMAPHORE
  867. /**
  868. * Semaphore structure
  869. */
  870. struct rt_semaphore
  871. {
  872. struct rt_ipc_object parent; /**< inherit from ipc_object */
  873. rt_uint16_t value; /**< value of semaphore. */
  874. rt_uint16_t reserved; /**< reserved field */
  875. };
  876. typedef struct rt_semaphore *rt_sem_t;
  877. #endif /* RT_USING_SEMAPHORE */
  878. #ifdef RT_USING_MUTEX
  879. /**
  880. * Mutual exclusion (mutex) structure
  881. */
  882. struct rt_mutex
  883. {
  884. struct rt_ipc_object parent; /**< inherit from ipc_object */
  885. rt_uint8_t ceiling_priority; /**< the priority ceiling of mutexe */
  886. rt_uint8_t priority; /**< the maximal priority for pending thread */
  887. rt_uint8_t hold; /**< numbers of thread hold the mutex */
  888. rt_uint8_t reserved; /**< reserved field */
  889. struct rt_thread *owner; /**< current owner of mutex */
  890. rt_list_t taken_list; /**< the object list taken by thread */
  891. };
  892. typedef struct rt_mutex *rt_mutex_t;
  893. #endif /* RT_USING_MUTEX */
  894. #ifdef RT_USING_EVENT
  895. /**
  896. * flag definitions in event
  897. */
  898. #define RT_EVENT_FLAG_AND 0x01 /**< logic and */
  899. #define RT_EVENT_FLAG_OR 0x02 /**< logic or */
  900. #define RT_EVENT_FLAG_CLEAR 0x04 /**< clear flag */
  901. /*
  902. * event structure
  903. */
  904. struct rt_event
  905. {
  906. struct rt_ipc_object parent; /**< inherit from ipc_object */
  907. rt_uint32_t set; /**< event set */
  908. };
  909. typedef struct rt_event *rt_event_t;
  910. #endif /* RT_USING_EVENT */
  911. #ifdef RT_USING_MAILBOX
  912. /**
  913. * mailbox structure
  914. */
  915. struct rt_mailbox
  916. {
  917. struct rt_ipc_object parent; /**< inherit from ipc_object */
  918. rt_ubase_t *msg_pool; /**< start address of message buffer */
  919. rt_uint16_t size; /**< size of message pool */
  920. rt_uint16_t entry; /**< index of messages in msg_pool */
  921. rt_uint16_t in_offset; /**< input offset of the message buffer */
  922. rt_uint16_t out_offset; /**< output offset of the message buffer */
  923. rt_list_t suspend_sender_thread; /**< sender thread suspended on this mailbox */
  924. };
  925. typedef struct rt_mailbox *rt_mailbox_t;
  926. #endif /* RT_USING_MAILBOX */
  927. #ifdef RT_USING_MESSAGEQUEUE
  928. /**
  929. * message queue structure
  930. */
  931. struct rt_messagequeue
  932. {
  933. struct rt_ipc_object parent; /**< inherit from ipc_object */
  934. void *msg_pool; /**< start address of message queue */
  935. rt_uint16_t msg_size; /**< message size of each message */
  936. rt_uint16_t max_msgs; /**< max number of messages */
  937. rt_uint16_t entry; /**< index of messages in the queue */
  938. void *msg_queue_head; /**< list head */
  939. void *msg_queue_tail; /**< list tail */
  940. void *msg_queue_free; /**< pointer indicated the free node of queue */
  941. rt_list_t suspend_sender_thread; /**< sender thread suspended on this message queue */
  942. };
  943. typedef struct rt_messagequeue *rt_mq_t;
  944. #endif /* RT_USING_MESSAGEQUEUE */
  945. /**@}*/
  946. /**
  947. * @addtogroup MM
  948. */
  949. /**@{*/
  950. #ifdef RT_USING_HEAP
  951. /*
  952. * memory structure
  953. */
  954. struct rt_memory
  955. {
  956. struct rt_object parent; /**< inherit from rt_object */
  957. const char * algorithm; /**< Memory management algorithm name */
  958. rt_ubase_t address; /**< memory start address */
  959. rt_size_t total; /**< memory size */
  960. rt_size_t used; /**< size used */
  961. rt_size_t max; /**< maximum usage */
  962. };
  963. typedef struct rt_memory *rt_mem_t;
  964. #endif /* RT_USING_HEAP */
  965. /*
  966. * memory management
  967. * heap & partition
  968. */
  969. #ifdef RT_USING_SMALL_MEM
  970. typedef rt_mem_t rt_smem_t;
  971. #endif /* RT_USING_SMALL_MEM */
  972. #ifdef RT_USING_SLAB
  973. typedef rt_mem_t rt_slab_t;
  974. #endif /* RT_USING_SLAB */
  975. #ifdef RT_USING_MEMHEAP
  976. /**
  977. * memory item on the heap
  978. */
  979. struct rt_memheap_item
  980. {
  981. rt_uint32_t magic; /**< magic number for memheap */
  982. struct rt_memheap *pool_ptr; /**< point of pool */
  983. struct rt_memheap_item *next; /**< next memheap item */
  984. struct rt_memheap_item *prev; /**< prev memheap item */
  985. struct rt_memheap_item *next_free; /**< next free memheap item */
  986. struct rt_memheap_item *prev_free; /**< prev free memheap item */
  987. #ifdef RT_USING_MEMTRACE
  988. rt_uint8_t owner_thread_name[4]; /**< owner thread name */
  989. #endif /* RT_USING_MEMTRACE */
  990. };
  991. /**
  992. * Base structure of memory heap object
  993. */
  994. struct rt_memheap
  995. {
  996. struct rt_object parent; /**< inherit from rt_object */
  997. void *start_addr; /**< pool start address and size */
  998. rt_size_t pool_size; /**< pool size */
  999. rt_size_t available_size; /**< available size */
  1000. rt_size_t max_used_size; /**< maximum allocated size */
  1001. struct rt_memheap_item *block_list; /**< used block list */
  1002. struct rt_memheap_item *free_list; /**< free block list */
  1003. struct rt_memheap_item free_header; /**< free block list header */
  1004. struct rt_semaphore lock; /**< semaphore lock */
  1005. rt_bool_t locked; /**< External lock mark */
  1006. };
  1007. #endif /* RT_USING_MEMHEAP */
  1008. #ifdef RT_USING_MEMPOOL
  1009. /**
  1010. * Base structure of Memory pool object
  1011. */
  1012. struct rt_mempool
  1013. {
  1014. struct rt_object parent; /**< inherit from rt_object */
  1015. void *start_address; /**< memory pool start */
  1016. rt_size_t size; /**< size of memory pool */
  1017. rt_size_t block_size; /**< size of memory blocks */
  1018. rt_uint8_t *block_list; /**< memory blocks list */
  1019. rt_size_t block_total_count; /**< numbers of memory block */
  1020. rt_size_t block_free_count; /**< numbers of free memory block */
  1021. rt_list_t suspend_thread; /**< threads pended on this resource */
  1022. };
  1023. typedef struct rt_mempool *rt_mp_t;
  1024. #endif /* RT_USING_MEMPOOL */
  1025. /**@}*/
  1026. #ifdef RT_USING_DEVICE
  1027. /**
  1028. * @addtogroup Device
  1029. */
  1030. /**@{*/
  1031. /**
  1032. * device (I/O) class type
  1033. */
  1034. enum rt_device_class_type
  1035. {
  1036. RT_Device_Class_Char = 0, /**< character device */
  1037. RT_Device_Class_Block, /**< block device */
  1038. RT_Device_Class_NetIf, /**< net interface */
  1039. RT_Device_Class_MTD, /**< memory device */
  1040. RT_Device_Class_CAN, /**< CAN device */
  1041. RT_Device_Class_RTC, /**< RTC device */
  1042. RT_Device_Class_Sound, /**< Sound device */
  1043. RT_Device_Class_Graphic, /**< Graphic device */
  1044. RT_Device_Class_I2CBUS, /**< I2C bus device */
  1045. RT_Device_Class_USBDevice, /**< USB slave device */
  1046. RT_Device_Class_USBHost, /**< USB host bus */
  1047. RT_Device_Class_USBOTG, /**< USB OTG bus */
  1048. RT_Device_Class_SPIBUS, /**< SPI bus device */
  1049. RT_Device_Class_SPIDevice, /**< SPI device */
  1050. RT_Device_Class_SDIO, /**< SDIO bus device */
  1051. RT_Device_Class_PM, /**< PM pseudo device */
  1052. RT_Device_Class_Pipe, /**< Pipe device */
  1053. RT_Device_Class_Portal, /**< Portal device */
  1054. RT_Device_Class_Timer, /**< Timer device */
  1055. RT_Device_Class_Miscellaneous, /**< Miscellaneous device */
  1056. RT_Device_Class_Sensor, /**< Sensor device */
  1057. RT_Device_Class_Touch, /**< Touch device */
  1058. RT_Device_Class_PHY, /**< PHY device */
  1059. RT_Device_Class_Security, /**< Security device */
  1060. RT_Device_Class_WLAN, /**< WLAN device */
  1061. RT_Device_Class_Pin, /**< Pin device */
  1062. RT_Device_Class_ADC, /**< ADC device */
  1063. RT_Device_Class_DAC, /**< DAC device */
  1064. RT_Device_Class_WDT, /**< WDT device */
  1065. RT_Device_Class_PWM, /**< PWM device */
  1066. RT_Device_Class_Bus, /**< Bus device */
  1067. RT_Device_Class_Unknown /**< unknown device */
  1068. };
  1069. /**
  1070. * device flags definitions
  1071. */
  1072. #define RT_DEVICE_FLAG_DEACTIVATE 0x000 /**< device is not not initialized */
  1073. #define RT_DEVICE_FLAG_RDONLY 0x001 /**< read only */
  1074. #define RT_DEVICE_FLAG_WRONLY 0x002 /**< write only */
  1075. #define RT_DEVICE_FLAG_RDWR 0x003 /**< read and write */
  1076. #define RT_DEVICE_FLAG_REMOVABLE 0x004 /**< removable device */
  1077. #define RT_DEVICE_FLAG_STANDALONE 0x008 /**< standalone device */
  1078. #define RT_DEVICE_FLAG_ACTIVATED 0x010 /**< device is activated */
  1079. #define RT_DEVICE_FLAG_SUSPENDED 0x020 /**< device is suspended */
  1080. #define RT_DEVICE_FLAG_STREAM 0x040 /**< stream mode */
  1081. #define RT_DEVICE_FLAG_INT_RX 0x100 /**< INT mode on Rx */
  1082. #define RT_DEVICE_FLAG_DMA_RX 0x200 /**< DMA mode on Rx */
  1083. #define RT_DEVICE_FLAG_INT_TX 0x400 /**< INT mode on Tx */
  1084. #define RT_DEVICE_FLAG_DMA_TX 0x800 /**< DMA mode on Tx */
  1085. #define RT_DEVICE_OFLAG_CLOSE 0x000 /**< device is closed */
  1086. #define RT_DEVICE_OFLAG_RDONLY 0x001 /**< read only access */
  1087. #define RT_DEVICE_OFLAG_WRONLY 0x002 /**< write only access */
  1088. #define RT_DEVICE_OFLAG_RDWR 0x003 /**< read and write */
  1089. #define RT_DEVICE_OFLAG_OPEN 0x008 /**< device is opened */
  1090. #define RT_DEVICE_OFLAG_MASK 0xf0f /**< mask of open flag */
  1091. /**
  1092. * general device commands
  1093. * 0x01 - 0x1F general device control commands
  1094. * 0x20 - 0x3F udevice control commands
  1095. * 0x40 - special device control commands
  1096. */
  1097. #define RT_DEVICE_CTRL_RESUME 0x01 /**< resume device */
  1098. #define RT_DEVICE_CTRL_SUSPEND 0x02 /**< suspend device */
  1099. #define RT_DEVICE_CTRL_CONFIG 0x03 /**< configure device */
  1100. #define RT_DEVICE_CTRL_CLOSE 0x04 /**< close device */
  1101. #define RT_DEVICE_CTRL_NOTIFY_SET 0x05 /**< set notify func */
  1102. #define RT_DEVICE_CTRL_SET_INT 0x06 /**< set interrupt */
  1103. #define RT_DEVICE_CTRL_CLR_INT 0x07 /**< clear interrupt */
  1104. #define RT_DEVICE_CTRL_GET_INT 0x08 /**< get interrupt status */
  1105. #define RT_DEVICE_CTRL_CONSOLE_OFLAG 0x09 /**< get console open flag */
  1106. #define RT_DEVICE_CTRL_MASK 0x1f /**< mask for contrl commands */
  1107. /**
  1108. * device control
  1109. */
  1110. #define RT_DEVICE_CTRL_BASE(Type) ((RT_Device_Class_##Type + 1) * 0x100)
  1111. typedef struct rt_device *rt_device_t;
  1112. #ifdef RT_USING_DEVICE_OPS
  1113. /**
  1114. * operations set for device object
  1115. */
  1116. struct rt_device_ops
  1117. {
  1118. /* common device interface */
  1119. rt_err_t (*init) (rt_device_t dev);
  1120. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  1121. rt_err_t (*close) (rt_device_t dev);
  1122. rt_ssize_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  1123. rt_ssize_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  1124. rt_err_t (*control)(rt_device_t dev, int cmd, void *args);
  1125. };
  1126. #endif /* RT_USING_DEVICE_OPS */
  1127. /**
  1128. * WaitQueue structure
  1129. */
  1130. struct rt_wqueue
  1131. {
  1132. rt_uint32_t flag;
  1133. rt_list_t waiting_list;
  1134. };
  1135. typedef struct rt_wqueue rt_wqueue_t;
  1136. #ifdef RT_USING_DM
  1137. struct rt_driver;
  1138. struct rt_bus;
  1139. #endif
  1140. /**
  1141. * Device structure
  1142. */
  1143. struct rt_device
  1144. {
  1145. struct rt_object parent; /**< inherit from rt_object */
  1146. #ifdef RT_USING_DM
  1147. struct rt_bus *bus; /**< the bus mounting to */
  1148. rt_list_t node; /**< to mount on bus */
  1149. struct rt_driver *drv; /**< driver for powering the device */
  1150. #ifdef RT_USING_OFW
  1151. void *ofw_node; /**< ofw node get from device tree */
  1152. #endif
  1153. #endif
  1154. enum rt_device_class_type type; /**< device type */
  1155. rt_uint16_t flag; /**< device flag */
  1156. rt_uint16_t open_flag; /**< device open flag */
  1157. rt_uint8_t ref_count; /**< reference count */
  1158. rt_uint8_t device_id; /**< 0 - 255 */
  1159. /* device call back */
  1160. rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);
  1161. rt_err_t (*tx_complete)(rt_device_t dev, void *buffer);
  1162. #ifdef RT_USING_DEVICE_OPS
  1163. const struct rt_device_ops *ops;
  1164. #else
  1165. /* common device interface */
  1166. rt_err_t (*init) (rt_device_t dev);
  1167. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  1168. rt_err_t (*close) (rt_device_t dev);
  1169. rt_ssize_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  1170. rt_ssize_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  1171. rt_err_t (*control)(rt_device_t dev, int cmd, void *args);
  1172. #endif /* RT_USING_DEVICE_OPS */
  1173. #ifdef RT_USING_POSIX_DEVIO
  1174. const struct dfs_file_ops *fops;
  1175. struct rt_wqueue wait_queue;
  1176. #endif /* RT_USING_POSIX_DEVIO */
  1177. void *user_data; /**< device private data */
  1178. };
  1179. typedef struct rt_device *rt_device_t;
  1180. /**
  1181. * Notify structure
  1182. */
  1183. struct rt_device_notify
  1184. {
  1185. void (*notify)(rt_device_t dev);
  1186. struct rt_device *dev;
  1187. };
  1188. #ifdef RT_USING_SMART
  1189. struct rt_channel
  1190. {
  1191. struct rt_ipc_object parent; /**< inherit from object */
  1192. struct rt_thread *reply; /**< the thread will be reply */
  1193. rt_list_t wait_msg; /**< the wait queue of sender msg */
  1194. rt_list_t wait_thread; /**< the wait queue of sender thread */
  1195. rt_wqueue_t reader_queue; /**< channel poll queue */
  1196. rt_uint8_t stat; /**< the status of this channel */
  1197. rt_ubase_t ref;
  1198. };
  1199. typedef struct rt_channel *rt_channel_t;
  1200. #endif
  1201. /**@}*/
  1202. #endif /* RT_USING_DEVICE */
  1203. #ifdef __cplusplus
  1204. }
  1205. #endif
  1206. #ifdef __cplusplus
  1207. /* RT-Thread definitions for C++ */
  1208. namespace rtthread {
  1209. enum TICK_WAIT {
  1210. WAIT_NONE = 0,
  1211. WAIT_FOREVER = -1,
  1212. };
  1213. }
  1214. #endif /* __cplusplus */
  1215. #endif /* __RT_DEF_H__ */