rtdef.h 56 KB

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