rtdef.h 48 KB

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