rtdef.h 56 KB

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