rtdef.h 55 KB

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