rtdef.h 48 KB

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