rtdef.h 54 KB

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