rtdef.h 46 KB

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