rtdef.h 48 KB

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