rtdef.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * File : rtdef.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://openlab.rt-thread.com/license/LICENSE.
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2007-01-10 Bernard the first version
  13. * 2008-07-12 Bernard remove all rt_int8, rt_uint32_t etc typedef
  14. */
  15. #ifndef __RT_DEF_H__
  16. #define __RT_DEF_H__
  17. #include <rtconfig.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /* RT-Thread version information */
  22. #define RT_VERSION 3
  23. #define RT_SUBVERSION 0
  24. /* date type defination */
  25. typedef signed char rt_int8_t;
  26. typedef signed short rt_int16_t;
  27. typedef signed long rt_int32_t;
  28. typedef unsigned char rt_uint8_t;
  29. typedef unsigned short rt_uint16_t;
  30. typedef unsigned long rt_uint32_t;
  31. typedef int rt_bool_t;
  32. /* 32bit CPU */
  33. typedef long rt_base_t;
  34. typedef unsigned long rt_ubase_t;
  35. /* RT-Thread definitions */
  36. typedef rt_base_t rt_err_t; /* Type for error number. */
  37. typedef rt_uint32_t rt_time_t; /* Type for time stamp. */
  38. typedef rt_uint32_t rt_tick_t; /* Type for tick count. */
  39. typedef rt_base_t rt_flag_t; /* Type for flags. */
  40. typedef rt_ubase_t rt_size_t; /* Type for size number. */
  41. typedef rt_ubase_t rt_dev_t; /* Type for device. */
  42. typedef rt_uint32_t rt_off_t; /* Type for offset. */
  43. /* RT-Thread bool type definitions */
  44. #define RT_TRUE 1
  45. #define RT_FALSE 0
  46. /* maximun value of base type */
  47. #define RT_UINT8_MAX 0xff /* Maxium number of UINT8. */
  48. #define RT_UINT16_MAX 0xffff /* Maxium number of UINT16. */
  49. #define RT_UINT32_MAX 0xffffffff /* Maxium number of UINT32. */
  50. #ifdef __CC_ARM /* ARM Compiler */
  51. #include <stdarg.h>
  52. #define SECTION(x) __attribute__((section(x)))
  53. #define UNUSED __attribute__((unused))
  54. #define ALIGN(n) __attribute__((aligned(n)))
  55. #define rt_inline static __inline
  56. #elif defined (__ICCARM__) /* for IAR Compiler */
  57. #include <stdarg.h>
  58. #define SECTION(x) @ x
  59. #define UNUSED
  60. #define ALIGN(n) #pragma pack(n)
  61. #define rt_inline inline
  62. #elif defined (__GNUC__) /* GNU GCC Compiler */
  63. #ifdef RT_USING_NEWLIB
  64. #include <stdarg.h>
  65. #else
  66. typedef void *__sys_va_list;
  67. typedef __sys_va_list va_list;
  68. #define __va_rounded_size(type) \
  69. (((sizeof (type) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
  70. #define va_start(ap, lastarg) \
  71. (ap = ((char *) &(lastarg) + __va_rounded_size(lastarg)))
  72. #define va_end(ap) ((void)0)
  73. /* little endian */
  74. #define va_arg(ap, type) \
  75. (ap = (__sys_va_list) ((char *) (ap) + __va_rounded_size (type)), \
  76. *((type *) (void *) ((char *) (ap) - __va_rounded_size (type))))
  77. #endif
  78. #define SECTION(x) __attribute__((section(x)))
  79. #define UNUSED __attribute__((unused))
  80. #define ALIGN(n) __attribute__((aligned(n)))
  81. #define rt_inline static __inline
  82. #endif
  83. /* event length */
  84. #define RT_EVENT_LENGTH 32
  85. /* memory management option */
  86. #define RT_MM_PAGE_SIZE 4096
  87. #define RT_MM_PAGE_MASK (RT_MM_PAGE_SIZE - 1)
  88. #define RT_MM_PAGE_BITS 12
  89. /**
  90. * @addtogroup Error
  91. */
  92. /*@{*/
  93. /* RT-Thread error code definitions */
  94. #define RT_EOK 0 /* There is no error */
  95. #define RT_ERROR 1 /* A generic error happens */
  96. #define RT_ETIMEOUT 2 /* Timed out */
  97. #define RT_EFULL 3 /* The resource is full */
  98. #define RT_EEMPTY 4 /* The resource is empty */
  99. #define RT_ENOMEM 5 /* No memory */
  100. #define RT_ENOSYS 6 /* No system */
  101. #define RT_EBUSY 7 /* Busy */
  102. /*@}*/
  103. #ifdef RT_DEBUG
  104. #define RT_ASSERT(EX) if (!(EX)) {volatile char dummy=0; rt_kprintf("(%s) assert failed at %s:%d \n", \
  105. #EX, __FUNCTION__, __LINE__); while (dummy==0);}
  106. #else
  107. #define RT_ASSERT(EX)
  108. #endif
  109. /**
  110. * @def RT_ALIGN(size, align)
  111. * Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
  112. * would equal to 16. It is needed in some critical contexts.
  113. */
  114. #define RT_ALIGN(size, align) (((size) + (align) - 1) & ~((align)-1))
  115. /**
  116. * @def RT_NULL
  117. * Similar as the \c NULL in C library.
  118. */
  119. #define RT_NULL ((void *)0)
  120. struct rt_list_node
  121. {
  122. struct rt_list_node *next; /* point to next node. */
  123. struct rt_list_node *prev; /* point to prev node. */
  124. };
  125. typedef struct rt_list_node rt_list_t; /* Type for lists. */
  126. /**
  127. * @addtogroup KernelObject
  128. */
  129. /*@{*/
  130. /*
  131. * Base structure of Kernel object
  132. */
  133. struct rt_object
  134. {
  135. /* name of kernel object */
  136. char name[RT_NAME_MAX];
  137. /* type of kernel object */
  138. rt_uint8_t type;
  139. /* flag of kernel object */
  140. rt_uint8_t flag;
  141. /* list pointer of kernel object */
  142. rt_list_t list;
  143. };
  144. typedef struct rt_object* rt_object_t; /* Type for kernel objects. */
  145. /**
  146. * The object type can be one of the follows with specific
  147. * macros enabled:
  148. * - Thread
  149. * - Process
  150. * - Semaphore
  151. * - Mutex
  152. * - Event
  153. * - MailBox
  154. * - MessageQueue
  155. * - MemPool
  156. * - Device
  157. * - Timer
  158. * - Unknown
  159. * - Static
  160. */
  161. enum rt_object_class_type
  162. {
  163. RT_Object_Class_Thread = 0, /* The object is a thread. */
  164. #ifdef RT_USING_SEMAPHORE
  165. RT_Object_Class_Semaphore, /* The object is a semaphore. */
  166. #endif
  167. #ifdef RT_USING_MUTEX
  168. RT_Object_Class_Mutex, /* The object is a mutex. */
  169. #endif
  170. #ifdef RT_USING_EVENT
  171. RT_Object_Class_Event, /* The object is a event. */
  172. #endif
  173. #ifdef RT_USING_MAILBOX
  174. RT_Object_Class_MailBox, /* The object is a mail box. */
  175. #endif
  176. #ifdef RT_USING_MESSAGEQUEUE
  177. RT_Object_Class_MessageQueue, /* The object is a message queue. */
  178. #endif
  179. #ifdef RT_USING_MEMPOOL
  180. RT_Object_Class_MemPool, /* The object is a memory pool. */
  181. #endif
  182. #ifdef RT_USING_DEVICE
  183. RT_Object_Class_Device, /* The object is a device */
  184. #endif
  185. RT_Object_Class_Timer, /* The object is a timer. */
  186. #ifdef RT_USING_MODULE
  187. RT_Object_Class_Module, /* The object is a module. */
  188. #endif
  189. RT_Object_Class_Unknown, /* The object is unknown. */
  190. RT_Object_Class_Static = 0x80 /* The object is a static object. */
  191. };
  192. /*
  193. * the information of the kernel object
  194. */
  195. struct rt_object_information
  196. {
  197. enum rt_object_class_type type; /* object class type. */
  198. rt_list_t object_list; /* object list. */
  199. rt_size_t object_size; /* object size. */
  200. };
  201. /*@}*/
  202. /**
  203. * @addtogroup Clock
  204. */
  205. /*@{*/
  206. /*
  207. * clock & timer macros
  208. */
  209. #define RT_TIMER_FLAG_DEACTIVATED 0x0 /* timer is deactive. */
  210. #define RT_TIMER_FLAG_ACTIVATED 0x1 /* timer is active. */
  211. #define RT_TIMER_FLAG_ONE_SHOT 0x0 /* one shot timer. */
  212. #define RT_TIMER_FLAG_PERIODIC 0x2 /* periodic timer. */
  213. #define RT_TIMER_FLAG_HARD_TIMER 0x0 /* hard timer,the timer's callback function will be called in tick isr. */
  214. #define RT_TIMER_FLAG_SOFT_TIMER 0x4 /* soft timer,the timer's callback function will be called in timer thread. */
  215. #define RT_TIMER_CTRL_SET_TIME 0x0 /* set timer. */
  216. #define RT_TIMER_CTRL_GET_TIME 0x1 /* get timer. */
  217. #define RT_TIMER_CTRL_SET_ONESHOT 0x2 /* change timer to one shot. */
  218. #define RT_TIMER_CTRL_SET_PERIODIC 0x3 /* change timer to periodic. */
  219. /*
  220. * timer structure
  221. *
  222. */
  223. struct rt_timer
  224. {
  225. struct rt_object parent;
  226. rt_list_t list; /* the node of timer list. */
  227. void (*timeout_func)(void* parameter); /* timeout function. */
  228. void *parameter; /* timeout function's parameter. */
  229. rt_tick_t init_tick; /* timer timeout tick. */
  230. rt_tick_t timeout_tick; /* timeout tick. */
  231. };
  232. typedef struct rt_timer* rt_timer_t;
  233. /*@}*/
  234. /**
  235. * @addtogroup Thread
  236. */
  237. /*@{*/
  238. /*
  239. * Thread
  240. */
  241. /* thread state definitions */
  242. #define RT_THREAD_RUNNING 0x0 /* Running. */
  243. #define RT_THREAD_READY 0x1 /* Ready. */
  244. #define RT_THREAD_SUSPEND 0x2 /* Suspend. */
  245. #define RT_THREAD_BLOCK RT_THREAD_SUSPEND /* Blocked. */
  246. #define RT_THREAD_CLOSE 0x3 /* Closed. */
  247. #define RT_THREAD_INIT RT_THREAD_CLOSE /* Inited. */
  248. #define RT_THREAD_FLAGS_TIMERSLICE 0x01
  249. #define RT_THREAD_CTRL_STARTUP 0x00 /* Starup thread. */
  250. #define RT_THREAD_CTRL_CLOSE 0x01 /* Close thread. */
  251. #define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /* Change thread priority. */
  252. #define RT_THREAD_CTRL_INFO 0x03 /* Get thread information. */
  253. typedef struct rt_thread* rt_thread_t;
  254. typedef struct rt_module* rt_module_t;
  255. /*
  256. * Thread related structure
  257. */
  258. struct rt_thread
  259. {
  260. /* rt object */
  261. char name[RT_NAME_MAX]; /* the name of thread */
  262. rt_uint8_t type; /* type of object */
  263. rt_uint8_t flags; /* thread's flags */
  264. rt_list_t list; /* the object list */
  265. rt_thread_t tid; /* the thread id */
  266. rt_list_t tlist; /* the thread list */
  267. /* stack point and entry */
  268. void* sp; /* stack point */
  269. void* entry; /* entry */
  270. void* parameter; /* parameter */
  271. void* stack_addr; /* stack address */
  272. rt_uint16_t stack_size; /* stack size */
  273. /* error code */
  274. rt_err_t error; /* error code */
  275. /* priority */
  276. rt_uint8_t current_priority; /* current priority */
  277. rt_uint8_t init_priority; /* initialized priority */
  278. #if RT_THREAD_PRIORITY_MAX > 32
  279. rt_uint8_t number;
  280. rt_uint8_t high_mask;
  281. #endif
  282. rt_uint32_t number_mask;
  283. #if defined(RT_USING_EVENT)
  284. /* thread event */
  285. rt_uint32_t event_set;
  286. rt_uint8_t event_info;
  287. #endif
  288. rt_uint8_t stat; /* thread stat */
  289. rt_ubase_t init_tick; /* thread's tick */
  290. rt_ubase_t remaining_tick; /* remaining tick */
  291. struct rt_timer thread_timer; /* thread timer */
  292. #ifdef RT_USING_MODULE
  293. rt_module_t module_parent; /* module parent */
  294. #endif
  295. rt_uint32_t user_data; /* user data */
  296. };
  297. /*@}*/
  298. #ifdef RT_USING_MODULE
  299. struct rt_module
  300. {
  301. /* inherit from object */
  302. struct rt_object parent;
  303. rt_uint32_t module_data;
  304. void* module_space;
  305. void* module_entry;
  306. rt_uint32_t stack_size;
  307. rt_uint32_t thread_priority;
  308. rt_thread_t module_thread;
  309. /* module memory pool */
  310. rt_uint32_t mempool_size;
  311. void* module_mempool;
  312. /* object in this module, module object is the last basic object type */
  313. struct rt_object_information module_object[RT_Object_Class_Module];
  314. };
  315. #endif
  316. /**
  317. * @addtogroup IPC
  318. */
  319. /*@{*/
  320. /*
  321. * ipc & sync
  322. */
  323. #define RT_IPC_FLAG_FIFO 0x00 /* FIFOed IPC. @ref IPC. */
  324. #define RT_IPC_FLAG_PRIO 0x01 /* PRIOed IPC. @ref IPC. */
  325. #define RT_WAITING_FOREVER -1 /* Block forever until get resource. */
  326. #define RT_WAITING_NO 0 /* Non-block. */
  327. /*
  328. * Base structure of IPC object
  329. */
  330. struct rt_ipc_object
  331. {
  332. struct rt_object parent;
  333. rt_list_t suspend_thread; /* threads pended on this resource. */
  334. };
  335. #ifdef RT_USING_SEMAPHORE
  336. /*
  337. * semaphore
  338. *
  339. * Binary and counter semaphore are both supported.
  340. */
  341. struct rt_semaphore
  342. {
  343. struct rt_ipc_object parent;
  344. rt_ubase_t value; /* value of semaphore. */
  345. };
  346. typedef struct rt_semaphore* rt_sem_t;
  347. #endif
  348. #ifdef RT_USING_MUTEX
  349. /*
  350. * mutex
  351. */
  352. struct rt_mutex
  353. {
  354. struct rt_ipc_object parent;
  355. rt_ubase_t value; /* value of mutex. */
  356. struct rt_thread *owner; /* current owner of mutex. */
  357. rt_uint8_t original_priority; /* priority of last thread hold the mutex. */
  358. rt_ubase_t hold; /* numbers of thread hold the mutex. */
  359. };
  360. typedef struct rt_mutex* rt_mutex_t;
  361. #endif
  362. #if defined(RT_USING_EVENT)
  363. #define RT_EVENT_FLAG_AND 0x01
  364. #define RT_EVENT_FLAG_OR 0x02
  365. #define RT_EVENT_FLAG_CLEAR 0x04
  366. #endif
  367. #ifdef RT_USING_EVENT
  368. /*
  369. * event
  370. */
  371. struct rt_event
  372. {
  373. struct rt_ipc_object parent;
  374. rt_uint32_t set; /* event set. */
  375. };
  376. typedef struct rt_event* rt_event_t;
  377. #endif
  378. #ifdef RT_USING_MAILBOX
  379. /*
  380. * mailbox
  381. *
  382. */
  383. struct rt_mailbox
  384. {
  385. struct rt_ipc_object parent;
  386. rt_uint32_t* msg_pool; /* start address of message buffer. */
  387. rt_size_t size; /* size of message pool. */
  388. rt_ubase_t entry; /* index of messages in msg_pool. */
  389. rt_ubase_t in_offset, out_offset; /* in/output offset of the message buffer. */
  390. };
  391. typedef struct rt_mailbox* rt_mailbox_t;
  392. #endif
  393. #ifdef RT_USING_MESSAGEQUEUE
  394. /*
  395. * messagequeue
  396. */
  397. struct rt_messagequeue
  398. {
  399. struct rt_ipc_object parent;
  400. void* msg_pool; /* start address of message queue. */
  401. rt_size_t msg_size; /* message size of each message. */
  402. rt_size_t max_msgs; /* max number of messages. */
  403. void* msg_queue_head; /* list head. */
  404. void* msg_queue_tail; /* list tail. */
  405. void* msg_queue_free; /* pointer indicated the free node of queue. */
  406. rt_ubase_t entry; /* index of messages in the queue. */
  407. };
  408. typedef struct rt_messagequeue* rt_mq_t;
  409. #endif
  410. /*@}*/
  411. /**
  412. * @addtogroup MM
  413. */
  414. /*@{*/
  415. /*
  416. * memory management
  417. * heap & partition
  418. */
  419. #ifdef RT_USING_MEMPOOL
  420. /*
  421. * Base structure of Memory pool object
  422. */
  423. struct rt_mempool
  424. {
  425. struct rt_object parent;
  426. void *start_address; /* memory pool start */
  427. rt_size_t size; /* size of memory pool */
  428. rt_size_t block_size; /* size of memory blocks */
  429. rt_uint8_t *block_list; /* memory blocks list */
  430. rt_size_t block_total_count; /* numbers of memory block */
  431. rt_size_t block_free_count; /* numbers of free memory block */
  432. rt_list_t suspend_thread; /* threads pended on this resource */
  433. rt_size_t suspend_thread_count; /* numbers of thread pended on this resource */
  434. };
  435. typedef struct rt_mempool* rt_mp_t;
  436. #endif
  437. /*@}*/
  438. #ifdef RT_USING_DEVICE
  439. /**
  440. * @addtogroup Device
  441. */
  442. /*@{*/
  443. /*
  444. * device (I/O) system
  445. */
  446. enum rt_device_class_type
  447. {
  448. RT_Device_Class_Char = 0, /* character device */
  449. RT_Device_Class_Block, /* block device */
  450. RT_Device_Class_NetIf, /* net interface */
  451. RT_Device_Class_MTD, /* memory device */
  452. RT_Device_Class_CAN, /* CAN device */
  453. RT_Device_Class_RTC, /* RTC device */
  454. RT_Device_Class_Sound, /* Sound device */
  455. RT_Device_Class_Unknown /* unknown device */
  456. };
  457. /* device flags */
  458. #define RT_DEVICE_FLAG_DEACTIVATE 0x000 /* not inited */
  459. #define RT_DEVICE_FLAG_RDONLY 0x001 /* read only */
  460. #define RT_DEVICE_FLAG_WRONLY 0x002 /* write only */
  461. #define RT_DEVICE_FLAG_RDWR 0x003 /* read and write */
  462. #define RT_DEVICE_FLAG_REMOVABLE 0x004 /* removable device */
  463. #define RT_DEVICE_FLAG_STANDALONE 0x008 /* standalone device */
  464. #define RT_DEVICE_FLAG_ACTIVATED 0x010 /* device is activated */
  465. #define RT_DEVICE_FLAG_SUSPENDED 0x020 /* device is suspended */
  466. #define RT_DEVICE_FLAG_STREAM 0x040 /* stream mode */
  467. #define RT_DEVICE_FLAG_INT_RX 0x100 /* INT mode on Rx */
  468. #define RT_DEVICE_FLAG_DMA_RX 0x200 /* DMA mode on Rx */
  469. #define RT_DEVICE_FLAG_INT_TX 0x400 /* INT mode on Tx */
  470. #define RT_DEVICE_FLAG_DMA_TX 0x800 /* DMA mode on Tx */
  471. #define RT_DEVICE_OFLAG_CLOSE 0x000 /* device is closed */
  472. #define RT_DEVICE_OFLAG_RDONLY 0x001 /* read only access */
  473. #define RT_DEVICE_OFLAG_WRONLY 0x002 /* write only access */
  474. #define RT_DEVICE_OFLAG_RDWR 0x003 /* read and write */
  475. #define RT_DEVICE_OFLAG_OPEN 0x008 /* device is opened */
  476. /* general device commands */
  477. #define RT_DEVICE_CTRL_RESUME 0x01 /* resume device */
  478. #define RT_DEVICE_CTRL_SUSPEND 0x02 /* suspend device */
  479. /* special device commands */
  480. #define RT_DEVICE_CTRL_CHAR_STREAM 0x10 /* stream mode on char device */
  481. #define RT_DEVICE_CTRL_BLK_GETGEOME 0x10 /* get geometry information */
  482. #define RT_DEVICE_CTRL_NETIF_GETMAC 0x10 /* get mac address */
  483. #define RT_DEVICE_CTRL_MTD_FORMAT 0x10 /* format a MTD device */
  484. #define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /* get time */
  485. #define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /* set time */
  486. typedef struct rt_device* rt_device_t;
  487. /*
  488. * Device related structure
  489. */
  490. struct rt_device
  491. {
  492. struct rt_object parent;
  493. /* device type */
  494. enum rt_device_class_type type;
  495. /* device flag and device open flag*/
  496. rt_uint16_t flag, open_flag;
  497. /* device call back */
  498. rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);
  499. rt_err_t (*tx_complete)(rt_device_t dev, void* buffer);
  500. /* common device interface */
  501. rt_err_t (*init) (rt_device_t dev);
  502. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  503. rt_err_t (*close) (rt_device_t dev);
  504. rt_size_t (*read) (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size);
  505. rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size);
  506. rt_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void *args);
  507. #ifdef RT_USING_DEVICE_SUSPEND
  508. rt_err_t (*suspend) (rt_device_t dev);
  509. rt_err_t (*resumed) (rt_device_t dev);
  510. #endif
  511. /* device private data */
  512. void* private;
  513. };
  514. /*@}*/
  515. #endif
  516. #ifdef __cplusplus
  517. }
  518. #endif
  519. #endif