rtdef.h 38 KB

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