rtdef.h 55 KB

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