rtdef.h 55 KB

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