rtdef.h 48 KB

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