rtdef.h 56 KB

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