rtdef.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. #elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
  104. #include <stdarg.h>
  105. #define SECTION(x) @ x
  106. #define RT_UNUSED
  107. #define RT_USED __root
  108. #define PRAGMA(x) _Pragma(#x)
  109. #define ALIGN(n) PRAGMA(data_alignment=n)
  110. #define RT_WEAK __weak
  111. #define rt_inline static inline
  112. #elif defined (__GNUC__) /* GNU GCC Compiler */
  113. #ifdef RT_USING_NEWLIB
  114. #include <stdarg.h>
  115. #else
  116. /* the version of GNU GCC must be greater than 4.x */
  117. typedef __builtin_va_list __gnuc_va_list;
  118. typedef __gnuc_va_list va_list;
  119. #define va_start(v,l) __builtin_va_start(v,l)
  120. #define va_end(v) __builtin_va_end(v)
  121. #define va_arg(v,l) __builtin_va_arg(v,l)
  122. #endif
  123. #define SECTION(x) __attribute__((section(x)))
  124. #define RT_UNUSED __attribute__((unused))
  125. #define RT_USED __attribute__((used))
  126. #define ALIGN(n) __attribute__((aligned(n)))
  127. #define RT_WEAK __attribute__((weak))
  128. #define rt_inline static __inline
  129. #elif defined (__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
  130. #include <stdarg.h>
  131. #define SECTION(x) __attribute__((section(x)))
  132. #define RT_UNUSED __attribute__((unused))
  133. #define RT_USED __attribute__((used))
  134. #define ALIGN(n) __attribute__((aligned(n)))
  135. #define RT_WEAK __attribute__((weak))
  136. #define rt_inline static inline
  137. #elif defined (_MSC_VER)
  138. #include <stdarg.h>
  139. #define SECTION(x)
  140. #define RT_UNUSED
  141. #define RT_USED
  142. #define ALIGN(n) __declspec(align(n))
  143. #define RT_WEAK
  144. #define rt_inline static __inline
  145. #elif defined (__TI_COMPILER_VERSION__)
  146. #include <stdarg.h>
  147. /* The way that TI compiler set section is different from other(at least
  148. * GCC and MDK) compilers. See ARM Optimizing C/C++ Compiler 5.9.3 for more
  149. * details. */
  150. #define SECTION(x)
  151. #define RT_UNUSED
  152. #define RT_USED
  153. #define PRAGMA(x) _Pragma(#x)
  154. #define ALIGN(n)
  155. #define RT_WEAK
  156. #define rt_inline static inline
  157. #else
  158. #error not supported tool chain
  159. #endif
  160. /* initialization export */
  161. #ifdef RT_USING_COMPONENTS_INIT
  162. typedef int (*init_fn_t)(void);
  163. #ifdef _MSC_VER /* we do not support MS VC++ compiler */
  164. #define INIT_EXPORT(fn, level)
  165. #else
  166. #if RT_DEBUG_INIT
  167. struct rt_init_desc
  168. {
  169. const char* fn_name;
  170. const init_fn_t fn;
  171. };
  172. #define INIT_EXPORT(fn, level) \
  173. const char __rti_##fn##_name[] = #fn; \
  174. RT_USED const struct rt_init_desc __rt_init_desc_##fn SECTION(".rti_fn." level) = \
  175. { __rti_##fn##_name, fn};
  176. #else
  177. #define INIT_EXPORT(fn, level) \
  178. RT_USED const init_fn_t __rt_init_##fn SECTION(".rti_fn." level) = fn
  179. #endif
  180. #endif
  181. #else
  182. #define INIT_EXPORT(fn, level)
  183. #endif
  184. /* board init routines will be called in board_init() function */
  185. #define INIT_BOARD_EXPORT(fn) INIT_EXPORT(fn, "1")
  186. /* pre/device/component/env/app init routines will be called in init_thread */
  187. /* components pre-initialization (pure software initilization) */
  188. #define INIT_PREV_EXPORT(fn) INIT_EXPORT(fn, "2")
  189. /* device initialization */
  190. #define INIT_DEVICE_EXPORT(fn) INIT_EXPORT(fn, "3")
  191. /* components initialization (dfs, lwip, ...) */
  192. #define INIT_COMPONENT_EXPORT(fn) INIT_EXPORT(fn, "4")
  193. /* environment initialization (mount disk, ...) */
  194. #define INIT_ENV_EXPORT(fn) INIT_EXPORT(fn, "5")
  195. /* appliation initialization (rtgui application etc ...) */
  196. #define INIT_APP_EXPORT(fn) INIT_EXPORT(fn, "6")
  197. #if !defined(RT_USING_FINSH)
  198. /* define these to empty, even if not include finsh.h file */
  199. #define FINSH_FUNCTION_EXPORT(name, desc)
  200. #define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc)
  201. #define FINSH_VAR_EXPORT(name, type, desc)
  202. #define MSH_CMD_EXPORT(command, desc)
  203. #define MSH_CMD_EXPORT_ALIAS(command, alias, desc)
  204. #elif !defined(FINSH_USING_SYMTAB)
  205. #define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc)
  206. #endif
  207. /* event length */
  208. #define RT_EVENT_LENGTH 32
  209. /* memory management option */
  210. #define RT_MM_PAGE_SIZE 4096
  211. #define RT_MM_PAGE_MASK (RT_MM_PAGE_SIZE - 1)
  212. #define RT_MM_PAGE_BITS 12
  213. /* kernel malloc definitions */
  214. #ifndef RT_KERNEL_MALLOC
  215. #define RT_KERNEL_MALLOC(sz) rt_malloc(sz)
  216. #endif
  217. #ifndef RT_KERNEL_FREE
  218. #define RT_KERNEL_FREE(ptr) rt_free(ptr)
  219. #endif
  220. #ifndef RT_KERNEL_REALLOC
  221. #define RT_KERNEL_REALLOC(ptr, size) rt_realloc(ptr, size)
  222. #endif
  223. /**
  224. * @addtogroup Error
  225. */
  226. /**@{*/
  227. /* RT-Thread error code definitions */
  228. #define RT_EOK 0 /**< There is no error */
  229. #define RT_ERROR 1 /**< A generic error happens */
  230. #define RT_ETIMEOUT 2 /**< Timed out */
  231. #define RT_EFULL 3 /**< The resource is full */
  232. #define RT_EEMPTY 4 /**< The resource is empty */
  233. #define RT_ENOMEM 5 /**< No memory */
  234. #define RT_ENOSYS 6 /**< No system */
  235. #define RT_EBUSY 7 /**< Busy */
  236. #define RT_EIO 8 /**< IO error */
  237. #define RT_EINTR 9 /**< Interrupted system call */
  238. #define RT_EINVAL 10 /**< Invalid argument */
  239. /**@}*/
  240. /**
  241. * @ingroup BasicDef
  242. *
  243. * @def RT_ALIGN(size, align)
  244. * Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
  245. * would return 16.
  246. */
  247. #define RT_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))
  248. /**
  249. * @ingroup BasicDef
  250. *
  251. * @def RT_ALIGN_DOWN(size, align)
  252. * Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4)
  253. * would return 12.
  254. */
  255. #define RT_ALIGN_DOWN(size, align) ((size) & ~((align) - 1))
  256. /**
  257. * @ingroup BasicDef
  258. *
  259. * @def RT_NULL
  260. * Similar as the \c NULL in C library.
  261. */
  262. #define RT_NULL (0)
  263. /**
  264. * Double List structure
  265. */
  266. struct rt_list_node
  267. {
  268. struct rt_list_node *next; /**< point to next node. */
  269. struct rt_list_node *prev; /**< point to prev node. */
  270. };
  271. typedef struct rt_list_node rt_list_t; /**< Type for lists. */
  272. /**
  273. * Single List structure
  274. */
  275. struct rt_slist_node
  276. {
  277. struct rt_slist_node *next; /**< point to next node. */
  278. };
  279. typedef struct rt_slist_node rt_slist_t; /**< Type for single list. */
  280. /**
  281. * @addtogroup KernelObject
  282. */
  283. /**@{*/
  284. /*
  285. * kernel object macros
  286. */
  287. /**
  288. * Base structure of Kernel object
  289. */
  290. struct rt_object
  291. {
  292. char name[RT_NAME_MAX]; /**< name of kernel object */
  293. rt_uint8_t type; /**< type of kernel object */
  294. rt_uint8_t flag; /**< flag of kernel object */
  295. rt_list_t list; /**< list node of kernel object */
  296. };
  297. typedef struct rt_object *rt_object_t; /**< Type for kernel objects. */
  298. /**
  299. * The object type can be one of the follows with specific
  300. * macros enabled:
  301. * - Thread
  302. * - Semaphore
  303. * - Mutex
  304. * - Event
  305. * - MailBox
  306. * - MessageQueue
  307. * - MemHeap
  308. * - MemPool
  309. * - Device
  310. * - Timer
  311. * - Unknown
  312. * - Static
  313. */
  314. enum rt_object_class_type
  315. {
  316. RT_Object_Class_Null = 0x00, /**< The object is not used. */
  317. RT_Object_Class_Thread = 0x01, /**< The object is a thread. */
  318. RT_Object_Class_Semaphore = 0x02, /**< The object is a semaphore. */
  319. RT_Object_Class_Mutex = 0x03, /**< The object is a mutex. */
  320. RT_Object_Class_Event = 0x04, /**< The object is a event. */
  321. RT_Object_Class_MailBox = 0x05, /**< The object is a mail box. */
  322. RT_Object_Class_MessageQueue = 0x06, /**< The object is a message queue. */
  323. RT_Object_Class_MemHeap = 0x07, /**< The object is a memory heap. */
  324. RT_Object_Class_MemPool = 0x08, /**< The object is a memory pool. */
  325. RT_Object_Class_Device = 0x09, /**< The object is a device. */
  326. RT_Object_Class_Timer = 0x0a, /**< The object is a timer. */
  327. RT_Object_Class_Unknown = 0x0c, /**< The object is unknown. */
  328. RT_Object_Class_Static = 0x80 /**< The object is a static object. */
  329. };
  330. /**
  331. * The information of the kernel object
  332. */
  333. struct rt_object_information
  334. {
  335. enum rt_object_class_type type; /**< object class type */
  336. rt_list_t object_list; /**< object list */
  337. rt_size_t object_size; /**< object size */
  338. };
  339. /**
  340. * The hook function call macro
  341. */
  342. #ifdef RT_USING_HOOK
  343. #define RT_OBJECT_HOOK_CALL(func, argv) \
  344. do { if ((func) != RT_NULL) func argv; } while (0)
  345. #else
  346. #define RT_OBJECT_HOOK_CALL(func, argv)
  347. #endif
  348. /**@}*/
  349. /**
  350. * @addtogroup Clock
  351. */
  352. /**@{*/
  353. /**
  354. * clock & timer macros
  355. */
  356. #define RT_TIMER_FLAG_DEACTIVATED 0x0 /**< timer is deactive */
  357. #define RT_TIMER_FLAG_ACTIVATED 0x1 /**< timer is active */
  358. #define RT_TIMER_FLAG_ONE_SHOT 0x0 /**< one shot timer */
  359. #define RT_TIMER_FLAG_PERIODIC 0x2 /**< periodic timer */
  360. #define RT_TIMER_FLAG_HARD_TIMER 0x0 /**< hard timer,the timer's callback function will be called in tick isr. */
  361. #define RT_TIMER_FLAG_SOFT_TIMER 0x4 /**< soft timer,the timer's callback function will be called in timer thread. */
  362. #define RT_TIMER_CTRL_SET_TIME 0x0 /**< set timer control command */
  363. #define RT_TIMER_CTRL_GET_TIME 0x1 /**< get timer control command */
  364. #define RT_TIMER_CTRL_SET_ONESHOT 0x2 /**< change timer to one shot */
  365. #define RT_TIMER_CTRL_SET_PERIODIC 0x3 /**< change timer to periodic */
  366. #define RT_TIMER_CTRL_GET_STATE 0x4 /**< get timer run state active or deactive*/
  367. #ifndef RT_TIMER_SKIP_LIST_LEVEL
  368. #define RT_TIMER_SKIP_LIST_LEVEL 1
  369. #endif
  370. /* 1 or 3 */
  371. #ifndef RT_TIMER_SKIP_LIST_MASK
  372. #define RT_TIMER_SKIP_LIST_MASK 0x3
  373. #endif
  374. /**
  375. * timer structure
  376. */
  377. struct rt_timer
  378. {
  379. struct rt_object parent; /**< inherit from rt_object */
  380. rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL];
  381. void (*timeout_func)(void *parameter); /**< timeout function */
  382. void *parameter; /**< timeout function's parameter */
  383. rt_tick_t init_tick; /**< timer timeout tick */
  384. rt_tick_t timeout_tick; /**< timeout tick */
  385. };
  386. typedef struct rt_timer *rt_timer_t;
  387. /**@}*/
  388. /**
  389. * @addtogroup Thread
  390. */
  391. /**@{*/
  392. /*
  393. * Thread
  394. */
  395. /*
  396. * thread state definitions
  397. */
  398. #define RT_THREAD_INIT 0x00 /**< Initialized status */
  399. #define RT_THREAD_READY 0x01 /**< Ready status */
  400. #define RT_THREAD_SUSPEND 0x02 /**< Suspend status */
  401. #define RT_THREAD_RUNNING 0x03 /**< Running status */
  402. #define RT_THREAD_BLOCK RT_THREAD_SUSPEND /**< Blocked status */
  403. #define RT_THREAD_CLOSE 0x04 /**< Closed status */
  404. #define RT_THREAD_STAT_MASK 0x0f
  405. /**
  406. * thread control command definitions
  407. */
  408. #define RT_THREAD_CTRL_STARTUP 0x00 /**< Startup thread. */
  409. #define RT_THREAD_CTRL_CLOSE 0x01 /**< Close thread. */
  410. #define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */
  411. #define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */
  412. /**
  413. * Thread structure
  414. */
  415. struct rt_thread
  416. {
  417. /* rt object */
  418. char name[RT_NAME_MAX]; /**< the name of thread */
  419. rt_uint8_t type; /**< type of object */
  420. rt_uint8_t flags; /**< thread's flags */
  421. rt_list_t list; /**< the object list */
  422. rt_list_t tlist; /**< the thread list */
  423. /* stack point and entry */
  424. void *sp; /**< stack point */
  425. void *entry; /**< entry */
  426. void *parameter; /**< parameter */
  427. void *stack_addr; /**< stack address */
  428. rt_uint32_t stack_size; /**< stack size */
  429. /* error code */
  430. rt_err_t error; /**< error code */
  431. rt_uint8_t stat; /**< thread status */
  432. /* priority */
  433. rt_uint8_t current_priority; /**< current priority */
  434. rt_uint8_t init_priority; /**< initialized priority */
  435. #if RT_THREAD_PRIORITY_MAX > 32
  436. rt_uint8_t number;
  437. rt_uint8_t high_mask;
  438. #endif
  439. rt_uint32_t number_mask;
  440. #if defined(RT_USING_EVENT)
  441. /* thread event */
  442. rt_uint32_t event_set;
  443. rt_uint8_t event_info;
  444. #endif
  445. rt_ubase_t init_tick; /**< thread's initialized tick */
  446. rt_ubase_t remaining_tick; /**< remaining tick */
  447. struct rt_timer thread_timer; /**< built-in thread timer */
  448. void (*cleanup)(struct rt_thread *tid); /**< cleanup function when thread exit */
  449. rt_uint32_t user_data; /**< private user data beyond this thread */
  450. };
  451. typedef struct rt_thread *rt_thread_t;
  452. /**@}*/
  453. /**
  454. * @addtogroup IPC
  455. */
  456. /**@{*/
  457. /**
  458. * IPC flags and control command definitions
  459. */
  460. #define RT_IPC_FLAG_FIFO 0x00 /**< FIFOed IPC. @ref IPC. */
  461. #define RT_IPC_FLAG_PRIO 0x01 /**< PRIOed IPC. @ref IPC. */
  462. #define RT_IPC_CMD_UNKNOWN 0x00 /**< unknown IPC command */
  463. #define RT_IPC_CMD_RESET 0x01 /**< reset IPC object */
  464. #define RT_WAITING_FOREVER -1 /**< Block forever until get resource. */
  465. #define RT_WAITING_NO 0 /**< Non-block. */
  466. /**
  467. * Base structure of IPC object
  468. */
  469. struct rt_ipc_object
  470. {
  471. struct rt_object parent; /**< inherit from rt_object */
  472. rt_list_t suspend_thread; /**< threads pended on this resource */
  473. };
  474. #ifdef RT_USING_SEMAPHORE
  475. /**
  476. * Semaphore structure
  477. */
  478. struct rt_semaphore
  479. {
  480. struct rt_ipc_object parent; /**< inherit from ipc_object */
  481. rt_uint16_t value; /**< value of semaphore. */
  482. rt_uint16_t reserved; /**< reserved field */
  483. };
  484. typedef struct rt_semaphore *rt_sem_t;
  485. #endif
  486. #ifdef RT_USING_MUTEX
  487. /**
  488. * Mutual exclusion (mutex) structure
  489. */
  490. struct rt_mutex
  491. {
  492. struct rt_ipc_object parent; /**< inherit from ipc_object */
  493. rt_uint16_t value; /**< value of mutex */
  494. rt_uint8_t original_priority; /**< priority of last thread hold the mutex */
  495. rt_uint8_t hold; /**< numbers of thread hold the mutex */
  496. struct rt_thread *owner; /**< current owner of mutex */
  497. };
  498. typedef struct rt_mutex *rt_mutex_t;
  499. #endif
  500. #ifdef RT_USING_EVENT
  501. /**
  502. * flag defintions in event
  503. */
  504. #define RT_EVENT_FLAG_AND 0x01 /**< logic and */
  505. #define RT_EVENT_FLAG_OR 0x02 /**< logic or */
  506. #define RT_EVENT_FLAG_CLEAR 0x04 /**< clear flag */
  507. /*
  508. * event structure
  509. */
  510. struct rt_event
  511. {
  512. struct rt_ipc_object parent; /**< inherit from ipc_object */
  513. rt_uint32_t set; /**< event set */
  514. };
  515. typedef struct rt_event *rt_event_t;
  516. #endif
  517. #ifdef RT_USING_MAILBOX
  518. /**
  519. * mailbox structure
  520. */
  521. struct rt_mailbox
  522. {
  523. struct rt_ipc_object parent; /**< inherit from ipc_object */
  524. rt_ubase_t *msg_pool; /**< start address of message buffer */
  525. rt_uint16_t size; /**< size of message pool */
  526. rt_uint16_t entry; /**< index of messages in msg_pool */
  527. rt_uint16_t in_offset; /**< input offset of the message buffer */
  528. rt_uint16_t out_offset; /**< output offset of the message buffer */
  529. rt_list_t suspend_sender_thread; /**< sender thread suspended on this mailbox */
  530. };
  531. typedef struct rt_mailbox *rt_mailbox_t;
  532. #endif
  533. #ifdef RT_USING_MESSAGEQUEUE
  534. /**
  535. * message queue structure
  536. */
  537. struct rt_messagequeue
  538. {
  539. struct rt_ipc_object parent; /**< inherit from ipc_object */
  540. void *msg_pool; /**< start address of message queue */
  541. rt_uint16_t msg_size; /**< message size of each message */
  542. rt_uint16_t max_msgs; /**< max number of messages */
  543. rt_uint16_t entry; /**< index of messages in the queue */
  544. void *msg_queue_head; /**< list head */
  545. void *msg_queue_tail; /**< list tail */
  546. void *msg_queue_free; /**< pointer indicated the free node of queue */
  547. rt_list_t suspend_sender_thread; /**< sender thread suspended on this message queue */
  548. };
  549. typedef struct rt_messagequeue *rt_mq_t;
  550. #endif
  551. /**@}*/
  552. /**
  553. * @addtogroup MM
  554. */
  555. /**@{*/
  556. /*
  557. * memory management
  558. * heap & partition
  559. */
  560. #ifdef RT_USING_MEMHEAP
  561. /**
  562. * memory item on the heap
  563. */
  564. struct rt_memheap_item
  565. {
  566. rt_uint32_t magic; /**< magic number for memheap */
  567. struct rt_memheap *pool_ptr; /**< point of pool */
  568. struct rt_memheap_item *next; /**< next memheap item */
  569. struct rt_memheap_item *prev; /**< prev memheap item */
  570. struct rt_memheap_item *next_free; /**< next free memheap item */
  571. struct rt_memheap_item *prev_free; /**< prev free memheap item */
  572. };
  573. /**
  574. * Base structure of memory heap object
  575. */
  576. struct rt_memheap
  577. {
  578. struct rt_object parent; /**< inherit from rt_object */
  579. void *start_addr; /**< pool start address and size */
  580. rt_uint32_t pool_size; /**< pool size */
  581. rt_uint32_t available_size; /**< available size */
  582. rt_uint32_t max_used_size; /**< maximum allocated size */
  583. struct rt_memheap_item *block_list; /**< used block list */
  584. struct rt_memheap_item *free_list; /**< free block list */
  585. struct rt_memheap_item free_header; /**< free block list header */
  586. struct rt_semaphore lock; /**< semaphore lock */
  587. };
  588. #endif
  589. #ifdef RT_USING_MEMPOOL
  590. /**
  591. * Base structure of Memory pool object
  592. */
  593. struct rt_mempool
  594. {
  595. struct rt_object parent; /**< inherit from rt_object */
  596. void *start_address; /**< memory pool start */
  597. rt_size_t size; /**< size of memory pool */
  598. rt_size_t block_size; /**< size of memory blocks */
  599. rt_uint8_t *block_list; /**< memory blocks list */
  600. rt_size_t block_total_count; /**< numbers of memory block */
  601. rt_size_t block_free_count; /**< numbers of free memory block */
  602. rt_list_t suspend_thread; /**< threads pended on this resource */
  603. };
  604. typedef struct rt_mempool *rt_mp_t;
  605. #endif
  606. /**@}*/
  607. #ifdef RT_USING_DEVICE
  608. /**
  609. * @addtogroup Device
  610. */
  611. /**@{*/
  612. /**
  613. * device (I/O) class type
  614. */
  615. enum rt_device_class_type
  616. {
  617. RT_Device_Class_Char = 0, /**< character device */
  618. RT_Device_Class_Block, /**< block device */
  619. RT_Device_Class_NetIf, /**< net interface */
  620. RT_Device_Class_MTD, /**< memory device */
  621. RT_Device_Class_CAN, /**< CAN device */
  622. RT_Device_Class_RTC, /**< RTC device */
  623. RT_Device_Class_Sound, /**< Sound device */
  624. RT_Device_Class_Graphic, /**< Graphic device */
  625. RT_Device_Class_I2CBUS, /**< I2C bus device */
  626. RT_Device_Class_USBDevice, /**< USB slave device */
  627. RT_Device_Class_USBHost, /**< USB host bus */
  628. RT_Device_Class_SPIBUS, /**< SPI bus device */
  629. RT_Device_Class_SPIDevice, /**< SPI device */
  630. RT_Device_Class_SDIO, /**< SDIO bus device */
  631. RT_Device_Class_PM, /**< PM pseudo device */
  632. RT_Device_Class_Pipe, /**< Pipe device */
  633. RT_Device_Class_Portal, /**< Portal device */
  634. RT_Device_Class_Timer, /**< Timer device */
  635. RT_Device_Class_Miscellaneous, /**< Miscellaneous device */
  636. RT_Device_Class_Sensor, /**< Sensor device */
  637. RT_Device_Class_Touch, /**< Touch device */
  638. RT_Device_Class_Unknown /**< unknown device */
  639. };
  640. /**
  641. * device flags defitions
  642. */
  643. #define RT_DEVICE_FLAG_DEACTIVATE 0x000 /**< device is not not initialized */
  644. #define RT_DEVICE_FLAG_RDONLY 0x001 /**< read only */
  645. #define RT_DEVICE_FLAG_WRONLY 0x002 /**< write only */
  646. #define RT_DEVICE_FLAG_RDWR 0x003 /**< read and write */
  647. #define RT_DEVICE_FLAG_REMOVABLE 0x004 /**< removable device */
  648. #define RT_DEVICE_FLAG_STANDALONE 0x008 /**< standalone device */
  649. #define RT_DEVICE_FLAG_ACTIVATED 0x010 /**< device is activated */
  650. #define RT_DEVICE_FLAG_SUSPENDED 0x020 /**< device is suspended */
  651. #define RT_DEVICE_FLAG_STREAM 0x040 /**< stream mode */
  652. #define RT_DEVICE_FLAG_INT_RX 0x100 /**< INT mode on Rx */
  653. #define RT_DEVICE_FLAG_DMA_RX 0x200 /**< DMA mode on Rx */
  654. #define RT_DEVICE_FLAG_INT_TX 0x400 /**< INT mode on Tx */
  655. #define RT_DEVICE_FLAG_DMA_TX 0x800 /**< DMA mode on Tx */
  656. #define RT_DEVICE_OFLAG_CLOSE 0x000 /**< device is closed */
  657. #define RT_DEVICE_OFLAG_RDONLY 0x001 /**< read only access */
  658. #define RT_DEVICE_OFLAG_WRONLY 0x002 /**< write only access */
  659. #define RT_DEVICE_OFLAG_RDWR 0x003 /**< read and write */
  660. #define RT_DEVICE_OFLAG_OPEN 0x008 /**< device is opened */
  661. #define RT_DEVICE_OFLAG_MASK 0xf0f /**< mask of open flag */
  662. /**
  663. * general device commands
  664. */
  665. #define RT_DEVICE_CTRL_RESUME 0x01 /**< resume device */
  666. #define RT_DEVICE_CTRL_SUSPEND 0x02 /**< suspend device */
  667. #define RT_DEVICE_CTRL_CONFIG 0x03 /**< configure device */
  668. #define RT_DEVICE_CTRL_CLOSE 0x04 /**< close device */
  669. #define RT_DEVICE_CTRL_SET_INT 0x10 /**< set interrupt */
  670. #define RT_DEVICE_CTRL_CLR_INT 0x11 /**< clear interrupt */
  671. #define RT_DEVICE_CTRL_GET_INT 0x12 /**< get interrupt status */
  672. typedef struct rt_device *rt_device_t;
  673. /**
  674. * operations set for device object
  675. */
  676. struct rt_device_ops
  677. {
  678. /* common device interface */
  679. rt_err_t (*init) (rt_device_t dev);
  680. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  681. rt_err_t (*close) (rt_device_t dev);
  682. rt_size_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  683. rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  684. rt_err_t (*control)(rt_device_t dev, int cmd, void *args);
  685. };
  686. /**
  687. * Device structure
  688. */
  689. struct rt_device
  690. {
  691. struct rt_object parent; /**< inherit from rt_object */
  692. enum rt_device_class_type type; /**< device type */
  693. rt_uint16_t flag; /**< device flag */
  694. rt_uint16_t open_flag; /**< device open flag */
  695. rt_uint8_t ref_count; /**< reference count */
  696. rt_uint8_t device_id; /**< 0 - 255 */
  697. /* device call back */
  698. rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);
  699. rt_err_t (*tx_complete)(rt_device_t dev, void *buffer);
  700. #ifdef RT_USING_DEVICE_OPS
  701. const struct rt_device_ops *ops;
  702. #else
  703. /* common device interface */
  704. rt_err_t (*init) (rt_device_t dev);
  705. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  706. rt_err_t (*close) (rt_device_t dev);
  707. rt_size_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  708. rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  709. rt_err_t (*control)(rt_device_t dev, int cmd, void *args);
  710. #endif
  711. void *user_data; /**< device private data */
  712. };
  713. /**@}*/
  714. #endif
  715. #ifdef __cplusplus
  716. }
  717. #endif
  718. #ifdef __cplusplus
  719. /* RT-Thread definitions for C++ */
  720. namespace rtthread {
  721. enum TICK_WAIT {
  722. WAIT_NONE = 0,
  723. WAIT_FOREVER = -1,
  724. };
  725. }
  726. #endif /* end of __cplusplus */
  727. #endif