1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef KPORT_H
6 #define KPORT_H
7 
8 #define _POLL_EVENT_OBJ_INIT(obj) \
9     .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
10 #define _POLL_EVENT sys_dlist_t poll_events
11 
12 
13 #define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
14     {}
15 
16 #define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
17 
18 #define K_SEM_DEFINE(name, initial_count, count_limit)     \
19     struct k_sem name __in_section(_k_sem, static, name) = \
20             _K_SEM_INITIALIZER(name, initial_count, count_limit)
21 
22 
23 #define _K_MUTEX_INITIALIZER(obj) \
24     {                             \
25         0                         \
26     }
27 
28 #define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
29 
30 #define K_MUTEX_DEFINE(name)                                   \
31     struct k_mutex name __in_section(_k_mutex, static, name) = \
32             _K_MUTEX_INITIALIZER(name)
33 
34 #ifndef UINT_MAX
35 #define UINT_MAX (~0U)
36 #endif
37 
38 #ifdef CONFIG_OBJECT_TRACING
39 #define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
40 #define _OBJECT_TRACING_INIT .__next = NULL,
41 #else
42 #define _OBJECT_TRACING_INIT
43 #define _OBJECT_TRACING_NEXT_PTR(type)
44 #endif
45 
46 typedef int ssize_t;
47 typedef sys_dlist_t _wait_q_t;
48 
49 /*attention: this is intialied as zero,the queue variable shoule use
50  * k_queue_init\k_lifo_init\k_fifo_init again*/
51 #define _K_QUEUE_INITIALIZER(obj) \
52     {                             \
53         {                         \
54             {                     \
55                 {                 \
56                     0             \
57                 }                 \
58             }                     \
59         }                         \
60     }
61 
62 #define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
63 
64 #define Z_WORK_INITIALIZER(work_handler) \
65 	{ \
66 	._reserved = NULL, \
67 	.handler = work_handler, \
68 	.flags = { 0 } \
69 	}
70 
71 #define _K_LIFO_INITIALIZER(obj)                   \
72     {                                              \
73         ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
74     }
75 
76 #define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
77 
78 #define K_LIFO_DEFINE(name)                                   \
79     struct k_lifo name __in_section(_k_queue, static, name) = \
80             _K_LIFO_INITIALIZER(name)
81 
82 #define _K_FIFO_INITIALIZER(obj)                   \
83     {                                              \
84         ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
85     }
86 #define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
87 
88 #define K_FIFO_DEFINE(name)                                   \
89     struct kfifo name __in_section(_k_queue, static, name) = \
90             _K_FIFO_INITIALIZER(name)
91 
92 struct k_thread {
93     _task_t task;
94 };
95 
96 typedef _stack_element_t k_thread_stack_t;
97 
98 #define K_THREAD_STACK_DEFINE(sym, size) _stack_element_t sym[size]
99 #define K_THREAD_STACK_SIZEOF(sym) (sizeof(sym) / sizeof(_stack_element_t))
100 
101 typedef void (*k_thread_entry_t)(void *arg);
102 /**
103  * @brief Spawn a thread.
104  *
105  * This routine initializes a thread, then schedules it for execution.
106  *
107  * @param thread Thread data
108  * @param name  Thread name
109  * @param stack Pointer to the stack space.
110  * @param stack_size Stack size in bytes.
111  * @param fn Thread entry function.
112  * @param arg entry point parameter.
113  * @param prio Thread priority.
114  *
115  * @return 0 success.
116  */
117 int k_thread_spawn(struct k_thread *thread, const char *name, uint32_t *stack, uint32_t stack_size, \
118                    k_thread_entry_t fn, void *arg, int prio);
119 
120 
121 /**
122  * @brief Yield the current thread.
123  *
124  * This routine causes the current thread to yield execution to another
125  * thread of the same or higher priority. If there are no other ready threads
126  * of the same or higher priority, the routine returns immediately.
127  *
128  * @return N/A
129  */
130 int k_yield();
131 
132 /**
133  * @brief Lock interrupts.
134  *
135  * This routine disables all interrupts on the CPU. It returns an unsigned
136  * integer "lock-out key", which is an architecture-dependent indicator of
137  * whether interrupts were locked prior to the call. The lock-out key must be
138  * passed to irq_unlock() to re-enable interrupts.
139  *
140  * @return Lock-out key.
141  */
142 unsigned int irq_lock();
143 
144 /**
145  * @brief Unlock interrupts.
146  *
147  * This routine reverses the effect of a previous call to irq_lock() using
148  * the associated lock-out key. The caller must call the routine once for
149  * each time it called irq_lock(), supplying the keys in the reverse order
150  * they were acquired, before interrupts are enabled.
151  *
152  * @param key Lock-out key generated by irq_lock().
153  *
154  * @return N/A
155  */
156 void irq_unlock(unsigned int key);
157 
158 #ifndef BIT
159 #define BIT(n) (1UL << (n))
160 #endif
161 
162 #ifndef CONFIG_NET_BUF_WARN_ALLOC_INTERVAL
163 #define CONFIG_NET_BUF_WARN_ALLOC_INTERVAL 1
164 #endif
165 #ifndef CONFIG_HEAP_MEM_POOL_SIZE
166 #define CONFIG_HEAP_MEM_POOL_SIZE 1
167 #endif
168 
169 #define SYS_TRACING_OBJ_INIT(name, obj) \
170     do {                                \
171     } while ((0))
172 
k_is_in_isr()173 static inline int k_is_in_isr()
174 {
175     //uint32_t vec = (__get_PSR() & PSR_VEC_Msk) >> PSR_VEC_Pos;
176     return 0;
177 }
178 #endif /* KPORT_H */
179