rtdef.h 57 KB

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