rtdef.h 54 KB

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