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