rtdef.h 55 KB

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