rtdef.h 55 KB

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