rtdef.h 47 KB

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