Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, Wind River Systems, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * @file |
| 19 | * |
| 20 | * @brief Public kernel APIs. |
| 21 | */ |
| 22 | |
| 23 | #ifndef _kernel__h_ |
| 24 | #define _kernel__h_ |
| 25 | |
| 26 | #include <stddef.h> |
| 27 | #include <stdint.h> |
| 28 | #include <toolchain.h> |
| 29 | #include <sections.h> |
| 30 | #include <atomic.h> |
| 31 | #include <errno.h> |
| 32 | #include <misc/__assert.h> |
| 33 | #include <misc/dlist.h> |
| 34 | #include <misc/slist.h> |
| 35 | |
| 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
| 40 | #ifdef CONFIG_KERNEL_V2_DEBUG |
| 41 | #define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__) |
| 42 | #else |
| 43 | #define K_DEBUG(fmt, ...) |
| 44 | #endif |
| 45 | |
| 46 | #define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x))) |
| 47 | #define K_PRIO_PREEMPT(x) (x) |
| 48 | |
| 49 | #define K_FOREVER (-1) |
| 50 | #define K_NO_WAIT 0 |
| 51 | |
| 52 | #define K_ANY NULL |
| 53 | #define K_END NULL |
| 54 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 55 | #if CONFIG_NUM_COOP_PRIORITIES > 0 |
| 56 | #define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES) |
| 57 | #else |
| 58 | #define K_HIGHEST_THREAD_PRIO 0 |
| 59 | #endif |
| 60 | |
| 61 | #if CONFIG_NUM_PREEMPT_PRIORITIES > 0 |
| 62 | #define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES |
| 63 | #else |
| 64 | #define K_LOWEST_THREAD_PRIO -1 |
| 65 | #endif |
| 66 | |
Benjamin Walsh | fab8d92 | 2016-11-08 15:36:36 -0500 | [diff] [blame] | 67 | #define K_IDLE_PRIO K_LOWEST_THREAD_PRIO |
| 68 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 69 | #define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO) |
| 70 | #define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1) |
| 71 | |
| 72 | typedef sys_dlist_t _wait_q_t; |
| 73 | |
| 74 | #ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS |
| 75 | #define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next |
| 76 | #define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL, |
| 77 | #else |
| 78 | #define _DEBUG_TRACING_KERNEL_OBJECTS_INIT |
| 79 | #define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) |
| 80 | #endif |
| 81 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 82 | #define tcs k_thread |
| 83 | struct k_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 84 | struct k_mutex; |
| 85 | struct k_sem; |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 86 | struct k_alert; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 87 | struct k_msgq; |
| 88 | struct k_mbox; |
| 89 | struct k_pipe; |
| 90 | struct k_fifo; |
| 91 | struct k_lifo; |
| 92 | struct k_stack; |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 93 | struct k_mem_slab; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 94 | struct k_mem_pool; |
| 95 | struct k_timer; |
| 96 | |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 97 | typedef struct k_thread *k_tid_t; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 98 | |
| 99 | /* threads/scheduler/execution contexts */ |
| 100 | |
| 101 | enum execution_context_types { |
| 102 | K_ISR = 0, |
| 103 | K_COOP_THREAD, |
| 104 | K_PREEMPT_THREAD, |
| 105 | }; |
| 106 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 107 | typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 108 | |
| 109 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 110 | * @brief Spawn a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 111 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 112 | * This routine initializes a thread, then schedules it for execution. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 113 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 114 | * The new thread may be scheduled for immediate execution or a delayed start. |
| 115 | * If the newly spawned thread does not have a delayed start the kernel |
| 116 | * scheduler may preempt the current thread to allow the new thread to |
| 117 | * execute. |
| 118 | * |
| 119 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 120 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 121 | * them using "|" (the logical OR operator). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 122 | * |
| 123 | * @param stack Pointer to the stack space. |
| 124 | * @param stack_size Stack size in bytes. |
| 125 | * @param entry Thread entry function. |
| 126 | * @param p1 1st entry point parameter. |
| 127 | * @param p2 2nd entry point parameter. |
| 128 | * @param p3 3rd entry point parameter. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 129 | * @param prio Thread priority. |
| 130 | * @param options Thread options. |
| 131 | * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 132 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 133 | * @return ID of new thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 134 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 135 | extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size, |
| 136 | void (*entry)(void *, void *, void*), |
| 137 | void *p1, void *p2, void *p3, |
| 138 | int32_t prio, uint32_t options, int32_t delay); |
| 139 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 140 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 141 | * @brief Put the current thread to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 142 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 143 | * This routine puts the currently thread to sleep for @a duration |
| 144 | * milliseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 145 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 146 | * @param duration Number of milliseconds to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 147 | * |
| 148 | * @return N/A |
| 149 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 150 | extern void k_sleep(int32_t duration); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 151 | |
| 152 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 153 | * @brief Cause the current thread to busy wait. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 154 | * |
| 155 | * This routine causes the current thread to execute a "do nothing" loop for |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 156 | * @a usec_to_wait microseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 157 | * |
| 158 | * @warning This routine utilizes the system clock, so it must not be invoked |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 159 | * until the system clock is operational or while interrupts are locked. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 160 | * |
| 161 | * @return N/A |
| 162 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 163 | extern void k_busy_wait(uint32_t usec_to_wait); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 164 | |
| 165 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 166 | * @brief Yield the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 167 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 168 | * This routine causes the current thread to yield execution to another |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 169 | * thread of the same or higher priority. If there are no other ready threads |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 170 | * of the same or higher priority, the routine returns immediately. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 171 | * |
| 172 | * @return N/A |
| 173 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 174 | extern void k_yield(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 175 | |
| 176 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 177 | * @brief Wake up a sleeping thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 178 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 179 | * This routine prematurely wakes up @a thread from sleeping. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 180 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 181 | * If @a thread is not currently sleeping, the routine has no effect. |
| 182 | * |
| 183 | * @param thread ID of thread to wake. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 184 | * |
| 185 | * @return N/A |
| 186 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 187 | extern void k_wakeup(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 188 | |
| 189 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 190 | * @brief Get thread ID of the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 191 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 192 | * @return ID of current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 193 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 194 | extern k_tid_t k_current_get(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 195 | |
| 196 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 197 | * @brief Cancel thread performing a delayed start. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 198 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 199 | * This routine prevents @a thread from executing if it has not yet started |
| 200 | * execution. The thread must be re-spawned before it will execute. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 201 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 202 | * @param thread ID of thread to cancel. |
| 203 | * |
| 204 | * @retval 0 if successful. |
| 205 | * @retval -EINVAL if the thread has already started executing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 206 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 207 | extern int k_thread_cancel(k_tid_t thread); |
| 208 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 209 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 210 | * @brief Abort thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 211 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 212 | * This routine permanently stops execution of @a thread. The thread is taken |
| 213 | * off all kernel queues it is part of (i.e. the ready queue, the timeout |
| 214 | * queue, or a kernel object wait queue). However, any kernel resources the |
| 215 | * thread might currently own (such as mutexes or memory blocks) are not |
| 216 | * released. It is the responsibility of the caller of this routine to ensure |
| 217 | * all necessary cleanup is performed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 218 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 219 | * @param thread ID of thread to abort. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 220 | * |
| 221 | * @return N/A |
| 222 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 223 | extern void k_thread_abort(k_tid_t thread); |
| 224 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 225 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 226 | #define _THREAD_TIMEOUT_INIT(obj) \ |
| 227 | (obj).nano_timeout = { \ |
| 228 | .node = { {0}, {0} }, \ |
Benjamin Walsh | 055262c | 2016-10-05 17:16:01 -0400 | [diff] [blame] | 229 | .thread = NULL, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 230 | .wait_q = NULL, \ |
| 231 | .delta_ticks_from_prev = -1, \ |
| 232 | }, |
| 233 | #else |
| 234 | #define _THREAD_TIMEOUT_INIT(obj) |
| 235 | #endif |
| 236 | |
| 237 | #ifdef CONFIG_ERRNO |
| 238 | #define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0, |
| 239 | #else |
| 240 | #define _THREAD_ERRNO_INIT(obj) |
| 241 | #endif |
| 242 | |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 243 | struct _static_thread_data { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 244 | union { |
| 245 | char *init_stack; |
| 246 | struct k_thread *thread; |
| 247 | }; |
| 248 | unsigned int init_stack_size; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 249 | void (*init_entry)(void *, void *, void *); |
| 250 | void *init_p1; |
| 251 | void *init_p2; |
| 252 | void *init_p3; |
| 253 | int init_prio; |
| 254 | uint32_t init_options; |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 255 | int32_t init_delay; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 256 | void (*init_abort)(void); |
| 257 | uint32_t init_groups; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 258 | }; |
| 259 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 260 | #define _THREAD_INITIALIZER(stack, stack_size, \ |
| 261 | entry, p1, p2, p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 262 | prio, options, delay, abort, groups) \ |
| 263 | { \ |
| 264 | .init_stack = (stack), \ |
| 265 | .init_stack_size = (stack_size), \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 266 | .init_entry = (void (*)(void *, void *, void *))entry, \ |
| 267 | .init_p1 = (void *)p1, \ |
| 268 | .init_p2 = (void *)p2, \ |
| 269 | .init_p3 = (void *)p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 270 | .init_prio = (prio), \ |
| 271 | .init_options = (options), \ |
| 272 | .init_delay = (delay), \ |
| 273 | .init_abort = (abort), \ |
| 274 | .init_groups = (groups), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 277 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 278 | * @brief Statically define and initialize a thread. |
| 279 | * |
| 280 | * The thread may be scheduled for immediate execution or a delayed start. |
| 281 | * |
| 282 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 283 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 284 | * them using "|" (the logical OR operator). |
| 285 | * |
| 286 | * The ID of the thread can be accessed using: |
| 287 | * |
| 288 | * extern const k_tid_t @a name; |
| 289 | * |
| 290 | * @param name Name of the thread. |
| 291 | * @param stack_size Stack size in bytes. |
| 292 | * @param entry Thread entry function. |
| 293 | * @param p1 1st entry point parameter. |
| 294 | * @param p2 2nd entry point parameter. |
| 295 | * @param p3 3rd entry point parameter. |
| 296 | * @param prio Thread priority. |
| 297 | * @param options Thread options. |
| 298 | * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay). |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 299 | * |
| 300 | * @internal It has been observed that the x86 compiler by default aligns |
| 301 | * these _static_thread_data structures to 32-byte boundaries, thereby |
| 302 | * wasting space. To work around this, force a 4-byte alignment. |
| 303 | */ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 304 | #define K_THREAD_DEFINE(name, stack_size, \ |
| 305 | entry, p1, p2, p3, \ |
| 306 | prio, options, delay) \ |
| 307 | char __noinit __stack _k_thread_obj_##name[stack_size]; \ |
| 308 | struct _static_thread_data _k_thread_data_##name __aligned(4) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 309 | __in_section(_static_thread_data, static, name) = \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 310 | _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \ |
| 311 | entry, p1, p2, p3, prio, options, delay, \ |
Allan Stephens | 8809502 | 2016-10-26 14:15:08 -0500 | [diff] [blame] | 312 | NULL, 0); \ |
| 313 | const k_tid_t name = (k_tid_t)_k_thread_obj_##name |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 314 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 315 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 316 | * @brief Get a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 317 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 318 | * This routine gets the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 319 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 320 | * @param thread ID of thread whose priority is needed. |
| 321 | * |
| 322 | * @return Priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 323 | */ |
Allan Stephens | 399d0ad | 2016-10-07 13:41:34 -0500 | [diff] [blame] | 324 | extern int k_thread_priority_get(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 325 | |
| 326 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 327 | * @brief Set a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 328 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 329 | * This routine immediately changes the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 330 | * |
| 331 | * Rescheduling can occur immediately depending on the priority @a thread is |
| 332 | * set to: |
| 333 | * |
| 334 | * - If its priority is raised above the priority of the caller of this |
| 335 | * function, and the caller is preemptible, @a thread will be scheduled in. |
| 336 | * |
| 337 | * - If the caller operates on itself, it lowers its priority below that of |
| 338 | * other threads in the system, and the caller is preemptible, the thread of |
| 339 | * highest priority will be scheduled in. |
| 340 | * |
| 341 | * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to |
| 342 | * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the |
| 343 | * highest priority. |
| 344 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 345 | * @param thread ID of thread whose priority is to be set. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 346 | * @param prio New priority. |
| 347 | * |
| 348 | * @warning Changing the priority of a thread currently involved in mutex |
| 349 | * priority inheritance may result in undefined behavior. |
| 350 | * |
| 351 | * @return N/A |
| 352 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 353 | extern void k_thread_priority_set(k_tid_t thread, int prio); |
| 354 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 355 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 356 | * @brief Suspend a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 357 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 358 | * This routine prevents the kernel scheduler from making @a thread the |
| 359 | * current thread. All other internal operations on @a thread are still |
| 360 | * performed; for example, any timeout it is waiting on keeps ticking, |
| 361 | * kernel objects it is waiting on are still handed to it, etc. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 362 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 363 | * If @a thread is already suspended, the routine has no effect. |
| 364 | * |
| 365 | * @param thread ID of thread to suspend. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 366 | * |
| 367 | * @return N/A |
| 368 | */ |
Benjamin Walsh | 71d5228 | 2016-09-29 10:49:48 -0400 | [diff] [blame] | 369 | extern void k_thread_suspend(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 370 | |
| 371 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 372 | * @brief Resume a suspended thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 373 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 374 | * This routine allows the kernel scheduler to make @a thread the current |
| 375 | * thread, when it is next eligible for that role. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 376 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 377 | * If @a thread is not currently suspended, the routine has no effect. |
| 378 | * |
| 379 | * @param thread ID of thread to resume. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 380 | * |
| 381 | * @return N/A |
| 382 | */ |
Benjamin Walsh | 71d5228 | 2016-09-29 10:49:48 -0400 | [diff] [blame] | 383 | extern void k_thread_resume(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 384 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 385 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 386 | * @brief Set time-slicing period and scope. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 387 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 388 | * This routine specifies how the scheduler will perform time slicing of |
| 389 | * preemptible threads. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 390 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 391 | * To enable time slicing, @a slice must be non-zero. The scheduler |
| 392 | * ensures that no thread runs for more than the specified time limit |
| 393 | * before other threads of that priority are given a chance to execute. |
| 394 | * Any thread whose priority is higher than @a prio is exempted, and may |
| 395 | * execute as long as desired without being pre-empted due to time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 396 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 397 | * Time slicing only limits the maximum amount of time a thread may continuously |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 398 | * execute. Once the scheduler selects a thread for execution, there is no |
| 399 | * minimum guaranteed time the thread will execute before threads of greater or |
| 400 | * equal priority are scheduled. |
| 401 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 402 | * When the current thread is the only one of that priority eligible |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 403 | * for execution, this routine has no effect; the thread is immediately |
| 404 | * rescheduled after the slice period expires. |
| 405 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 406 | * To disable timeslicing, set both @a slice and @a prio to zero. |
| 407 | * |
| 408 | * @param slice Maximum time slice length (in milliseconds). |
| 409 | * @param prio Highest thread priority level eligible for time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 410 | * |
| 411 | * @return N/A |
| 412 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 413 | extern void k_sched_time_slice_set(int32_t slice, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 414 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 415 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 416 | * @brief Determine if code is running at interrupt level. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 417 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 418 | * @return 0 if invoked by a thread. |
| 419 | * @return Non-zero if invoked by an ISR. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 420 | */ |
Benjamin Walsh | c7ba8b1 | 2016-11-08 16:12:59 -0500 | [diff] [blame] | 421 | extern int k_is_in_isr(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 422 | |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 423 | /** |
| 424 | * @brief Determine if code is running in a preemptible thread. |
| 425 | * |
| 426 | * Returns a 'true' value if these conditions are all met: |
| 427 | * |
| 428 | * - the code is not running in an ISR |
| 429 | * - the thread's priority is in the preemptible range |
| 430 | * - the thread has not locked the scheduler |
| 431 | * |
| 432 | * @return 0 if invoked by either an ISR or a cooperative thread. |
| 433 | * @return Non-zero if invoked by a preemptible thread. |
| 434 | */ |
| 435 | extern int k_is_preempt_thread(void); |
| 436 | |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 437 | /* |
| 438 | * @brief Lock the scheduler |
| 439 | * |
| 440 | * Prevent another thread from preempting the current thread. |
| 441 | * |
| 442 | * @note If the thread does an operation that causes it to pend, it will still |
| 443 | * be context switched out. |
| 444 | * |
| 445 | * @note Similar to irq_lock, the scheduler lock state is tracked per-thread. |
| 446 | * |
| 447 | * This should be chosen over irq_lock when possible, basically when the data |
| 448 | * protected by it is not accessible from ISRs. However, the associated |
| 449 | * k_sched_unlock() is heavier to use than irq_unlock, so if the amount of |
| 450 | * processing is really small, irq_lock might be a better choice. |
| 451 | * |
| 452 | * Can be called recursively. |
| 453 | * |
| 454 | * @return N/A |
| 455 | */ |
| 456 | extern void k_sched_lock(void); |
| 457 | |
| 458 | /* |
| 459 | * @brief Unlock the scheduler |
| 460 | * |
| 461 | * Re-enable scheduling previously disabled by k_sched_lock(). Must be called |
| 462 | * an equal amount of times k_sched_lock() was called. Threads are rescheduled |
| 463 | * upon exit. |
| 464 | * |
| 465 | * @return N/A |
| 466 | */ |
| 467 | extern void k_sched_unlock(void); |
| 468 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 469 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 470 | * @brief Set current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 471 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 472 | * This routine sets the custom data for the current thread to @ value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 473 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 474 | * Custom data is not used by the kernel itself, and is freely available |
| 475 | * for a thread to use as it sees fit. It can be used as a framework |
| 476 | * upon which to build thread-local storage. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 477 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 478 | * @param value New custom data value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 479 | * |
| 480 | * @return N/A |
| 481 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 482 | extern void k_thread_custom_data_set(void *value); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 483 | |
| 484 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 485 | * @brief Get current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 486 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 487 | * This routine returns the custom data for the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 488 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 489 | * @return Current custom data value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 490 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 491 | extern void *k_thread_custom_data_get(void); |
| 492 | |
| 493 | /** |
| 494 | * kernel timing |
| 495 | */ |
| 496 | |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 497 | #include <sys_clock.h> |
| 498 | |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame^] | 499 | /* Convenience helpers to convert durations into milliseconds */ |
| 500 | #define K_MSEC(ms) (ms) |
| 501 | #define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC) |
| 502 | #define K_MINUTES(m) K_SECONDS((m) * 60) |
| 503 | #define K_HOURS(h) K_MINUTES((h) * 60) |
| 504 | |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 505 | /* private internal time manipulation (users should never play with ticks) */ |
| 506 | |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 507 | /* added tick needed to account for tick in progress */ |
| 508 | #define _TICK_ALIGN 1 |
| 509 | |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 510 | static int64_t __ticks_to_ms(int64_t ticks) |
| 511 | { |
Benjamin Walsh | 57d55dc | 2016-10-04 16:58:08 -0400 | [diff] [blame] | 512 | #if CONFIG_SYS_CLOCK_EXISTS |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 513 | return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec; |
Benjamin Walsh | 57d55dc | 2016-10-04 16:58:08 -0400 | [diff] [blame] | 514 | #else |
| 515 | __ASSERT(ticks == 0, ""); |
| 516 | return 0; |
| 517 | #endif |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 521 | /* timeouts */ |
| 522 | |
| 523 | struct _timeout; |
| 524 | typedef void (*_timeout_func_t)(struct _timeout *t); |
| 525 | |
| 526 | struct _timeout { |
| 527 | sys_dlist_t node; |
Benjamin Walsh | 055262c | 2016-10-05 17:16:01 -0400 | [diff] [blame] | 528 | struct k_thread *thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 529 | sys_dlist_t *wait_q; |
| 530 | int32_t delta_ticks_from_prev; |
| 531 | _timeout_func_t func; |
| 532 | }; |
| 533 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 534 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 535 | /* timers */ |
| 536 | |
| 537 | struct k_timer { |
| 538 | /* |
| 539 | * _timeout structure must be first here if we want to use |
| 540 | * dynamic timer allocation. timeout.node is used in the double-linked |
| 541 | * list of free timers |
| 542 | */ |
| 543 | struct _timeout timeout; |
| 544 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 545 | /* wait queue for the (single) thread waiting on this timer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 546 | _wait_q_t wait_q; |
| 547 | |
| 548 | /* runs in ISR context */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 549 | void (*expiry_fn)(struct k_timer *); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 550 | |
| 551 | /* runs in the context of the thread that calls k_timer_stop() */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 552 | void (*stop_fn)(struct k_timer *); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 553 | |
| 554 | /* timer period */ |
| 555 | int32_t period; |
| 556 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 557 | /* timer status */ |
| 558 | uint32_t status; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 559 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 560 | /* used to support legacy timer APIs */ |
| 561 | void *_legacy_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 562 | |
| 563 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer); |
| 564 | }; |
| 565 | |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 566 | #define K_TIMER_INITIALIZER(obj, expiry, stop) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 567 | { \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 568 | .timeout.delta_ticks_from_prev = -1, \ |
| 569 | .timeout.wait_q = NULL, \ |
| 570 | .timeout.thread = NULL, \ |
| 571 | .timeout.func = _timer_expiration_handler, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 572 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 573 | .expiry_fn = expiry, \ |
| 574 | .stop_fn = stop, \ |
| 575 | .status = 0, \ |
| 576 | ._legacy_data = NULL, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 577 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 578 | } |
| 579 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 580 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 581 | * @brief Statically define and initialize a timer. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 582 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 583 | * The timer can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 584 | * |
| 585 | * extern struct k_timer @a name; |
| 586 | * |
| 587 | * @param name Name of the timer variable. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 588 | * @param expiry_fn Function to invoke each time the timer expires. |
| 589 | * @param stop_fn Function to invoke if the timer is stopped while running. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 590 | */ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 591 | #define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 592 | struct k_timer name \ |
| 593 | __in_section(_k_timer, static, name) = \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 594 | K_TIMER_INITIALIZER(name, expiry_fn, stop_fn) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 595 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 596 | /** |
| 597 | * @brief Initialize a timer. |
| 598 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 599 | * This routine initializes a timer, prior to its first use. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 600 | * |
| 601 | * @param timer Address of timer. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 602 | * @param expiry_fn Function to invoke each time the timer expires. |
| 603 | * @param stop_fn Function to invoke if the timer is stopped while running. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 604 | * |
| 605 | * @return N/A |
| 606 | */ |
| 607 | extern void k_timer_init(struct k_timer *timer, |
| 608 | void (*expiry_fn)(struct k_timer *), |
| 609 | void (*stop_fn)(struct k_timer *)); |
Andy Ross | 8d8b2ac | 2016-09-23 10:08:54 -0700 | [diff] [blame] | 610 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 611 | /** |
| 612 | * @brief Start a timer. |
| 613 | * |
| 614 | * This routine starts a timer, and resets its status to zero. The timer |
| 615 | * begins counting down using the specified duration and period values. |
| 616 | * |
| 617 | * Attempting to start a timer that is already running is permitted. |
| 618 | * The timer's status is reset to zero and the timer begins counting down |
| 619 | * using the new duration and period values. |
| 620 | * |
| 621 | * @param timer Address of timer. |
| 622 | * @param duration Initial timer duration (in milliseconds). |
| 623 | * @param period Timer period (in milliseconds). |
| 624 | * |
| 625 | * @return N/A |
| 626 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 627 | extern void k_timer_start(struct k_timer *timer, |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 628 | int32_t duration, int32_t period); |
| 629 | |
| 630 | /** |
| 631 | * @brief Stop a timer. |
| 632 | * |
| 633 | * This routine stops a running timer prematurely. The timer's stop function, |
| 634 | * if one exists, is invoked by the caller. |
| 635 | * |
| 636 | * Attempting to stop a timer that is not running is permitted, but has no |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 637 | * effect on the timer. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 638 | * |
| 639 | * @param timer Address of timer. |
| 640 | * |
| 641 | * @return N/A |
| 642 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 643 | extern void k_timer_stop(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 644 | |
| 645 | /** |
| 646 | * @brief Read timer status. |
| 647 | * |
| 648 | * This routine reads the timer's status, which indicates the number of times |
| 649 | * it has expired since its status was last read. |
| 650 | * |
| 651 | * Calling this routine resets the timer's status to zero. |
| 652 | * |
| 653 | * @param timer Address of timer. |
| 654 | * |
| 655 | * @return Timer status. |
| 656 | */ |
| 657 | extern uint32_t k_timer_status_get(struct k_timer *timer); |
| 658 | |
| 659 | /** |
| 660 | * @brief Synchronize thread to timer expiration. |
| 661 | * |
| 662 | * This routine blocks the calling thread until the timer's status is non-zero |
| 663 | * (indicating that it has expired at least once since it was last examined) |
| 664 | * or the timer is stopped. If the timer status is already non-zero, |
| 665 | * or the timer is already stopped, the caller continues without waiting. |
| 666 | * |
| 667 | * Calling this routine resets the timer's status to zero. |
| 668 | * |
| 669 | * This routine must not be used by interrupt handlers, since they are not |
| 670 | * allowed to block. |
| 671 | * |
| 672 | * @param timer Address of timer. |
| 673 | * |
| 674 | * @return Timer status. |
| 675 | */ |
| 676 | extern uint32_t k_timer_status_sync(struct k_timer *timer); |
| 677 | |
| 678 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 679 | * @brief Get time remaining before a timer next expires. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 680 | * |
| 681 | * This routine computes the (approximate) time remaining before a running |
| 682 | * timer next expires. If the timer is not running, it returns zero. |
| 683 | * |
| 684 | * @param timer Address of timer. |
| 685 | * |
| 686 | * @return Remaining time (in milliseconds). |
| 687 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 688 | extern int32_t k_timer_remaining_get(struct k_timer *timer); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 689 | |
| 690 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 691 | /* kernel clocks */ |
| 692 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 693 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 694 | * @brief Get system uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 695 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 696 | * This routine returns the elapsed time since the system booted, |
| 697 | * in milliseconds. |
| 698 | * |
| 699 | * @return Current uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 700 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 701 | extern int64_t k_uptime_get(void); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 702 | |
| 703 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 704 | * @brief Get system uptime (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 705 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 706 | * This routine returns the lower 32-bits of the elapsed time since the system |
| 707 | * booted, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 708 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 709 | * This routine can be more efficient than k_uptime_get(), as it reduces the |
| 710 | * need for interrupt locking and 64-bit math. However, the 32-bit result |
| 711 | * cannot hold a system uptime time larger than approximately 50 days, so the |
| 712 | * caller must handle possible rollovers. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 713 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 714 | * @return Current uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 715 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 716 | extern uint32_t k_uptime_get_32(void); |
| 717 | |
| 718 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 719 | * @brief Get elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 720 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 721 | * This routine computes the elapsed time between the current system uptime |
| 722 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 723 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 724 | * @param reftime Pointer to a reference time, which is updated to the current |
| 725 | * uptime upon return. |
| 726 | * |
| 727 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 728 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 729 | extern int64_t k_uptime_delta(int64_t *reftime); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 730 | |
| 731 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 732 | * @brief Get elapsed time (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 733 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 734 | * This routine computes the elapsed time between the current system uptime |
| 735 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 736 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 737 | * This routine can be more efficient than k_uptime_delta(), as it reduces the |
| 738 | * need for interrupt locking and 64-bit math. However, the 32-bit result |
| 739 | * cannot hold an elapsed time larger than approximately 50 days, so the |
| 740 | * caller must handle possible rollovers. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 741 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 742 | * @param reftime Pointer to a reference time, which is updated to the current |
| 743 | * uptime upon return. |
| 744 | * |
| 745 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 746 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 747 | extern uint32_t k_uptime_delta_32(int64_t *reftime); |
| 748 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 749 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 750 | * @brief Read the hardware clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 751 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 752 | * This routine returns the current time, as measured by the system's hardware |
| 753 | * clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 754 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 755 | * @return Current hardware clock up-counter (in cycles). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 756 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 757 | extern uint32_t k_cycle_get_32(void); |
| 758 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 759 | /** |
| 760 | * data transfers (basic) |
| 761 | */ |
| 762 | |
| 763 | /* fifos */ |
| 764 | |
| 765 | struct k_fifo { |
| 766 | _wait_q_t wait_q; |
| 767 | sys_slist_t data_q; |
| 768 | |
| 769 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo); |
| 770 | }; |
| 771 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 772 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 773 | * @brief Initialize a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 774 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 775 | * This routine initializes a fifo object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 776 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 777 | * @param fifo Address of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 778 | * |
| 779 | * @return N/A |
| 780 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 781 | extern void k_fifo_init(struct k_fifo *fifo); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 782 | |
| 783 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 784 | * @brief Add an element to a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 785 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 786 | * This routine adds a data item to @a fifo. A fifo data item must be |
| 787 | * aligned on a 4-byte boundary, and the first 32 bits of the item are |
| 788 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 789 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 790 | * @note Can be called by ISRs. |
| 791 | * |
| 792 | * @param fifo Address of the fifo. |
| 793 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 794 | * |
| 795 | * @return N/A |
| 796 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 797 | extern void k_fifo_put(struct k_fifo *fifo, void *data); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 798 | |
| 799 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 800 | * @brief Atomically add a list of elements to a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 801 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 802 | * This routine adds a list of data items to @a fifo in one operation. |
| 803 | * The data items must be in a singly-linked list, with the first 32 bits |
| 804 | * each data item pointing to the next data item; the list must be |
| 805 | * NULL-terminated. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 806 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 807 | * @note Can be called by ISRs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 808 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 809 | * @param fifo Address of the fifo. |
| 810 | * @param head Pointer to first node in singly-linked list. |
| 811 | * @param tail Pointer to last node in singly-linked list. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 812 | * |
| 813 | * @return N/A |
| 814 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 815 | extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 816 | |
| 817 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 818 | * @brief Atomically add a list of elements to a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 819 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 820 | * This routine adds a list of data items to @a fifo in one operation. |
| 821 | * The data items must be in a singly-linked list implemented using a |
| 822 | * sys_slist_t object. Upon completion, the sys_slist_t object is invalid |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 823 | * and must be re-initialized via sys_slist_init(). |
| 824 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 825 | * @note Can be called by ISRs. |
| 826 | * |
| 827 | * @param fifo Address of the fifo. |
| 828 | * @param list Pointer to sys_slist_t object. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 829 | * |
| 830 | * @return N/A |
| 831 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 832 | extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 833 | |
| 834 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 835 | * @brief Get an element from a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 836 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 837 | * This routine removes a data item from @a fifo in a "first in, first out" |
| 838 | * manner. The first 32 bits of the data item are reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 839 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 840 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 841 | * |
| 842 | * @param fifo Address of the fifo. |
| 843 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 844 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 845 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 846 | * @return Address of the data item if successful. |
| 847 | * @retval NULL if returned without waiting or waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 848 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 849 | extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout); |
| 850 | |
| 851 | #define K_FIFO_INITIALIZER(obj) \ |
| 852 | { \ |
| 853 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Benjamin Walsh | 9091e5d | 2016-09-30 10:42:47 -0400 | [diff] [blame] | 854 | .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 855 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 856 | } |
| 857 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 858 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 859 | * @brief Statically define and initialize a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 860 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 861 | * The fifo can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 862 | * |
| 863 | * extern struct k_fifo @a name; |
| 864 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 865 | * @param name Name of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 866 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 867 | #define K_FIFO_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 868 | struct k_fifo name \ |
| 869 | __in_section(_k_fifo, static, name) = \ |
| 870 | K_FIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 871 | |
| 872 | /* lifos */ |
| 873 | |
| 874 | struct k_lifo { |
| 875 | _wait_q_t wait_q; |
| 876 | void *list; |
| 877 | |
| 878 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo); |
| 879 | }; |
| 880 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 881 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 882 | * @brief Initialize a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 883 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 884 | * This routine initializes a lifo object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 885 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 886 | * @param lifo Address of the lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 887 | * |
| 888 | * @return N/A |
| 889 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 890 | extern void k_lifo_init(struct k_lifo *lifo); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 891 | |
| 892 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 893 | * @brief Add an element to a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 894 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 895 | * This routine adds a data item to @a lifo. A lifo data item must be |
| 896 | * aligned on a 4-byte boundary, and the first 32 bits of the item are |
| 897 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 898 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 899 | * @note Can be called by ISRs. |
| 900 | * |
| 901 | * @param lifo Address of the lifo. |
| 902 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 903 | * |
| 904 | * @return N/A |
| 905 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 906 | extern void k_lifo_put(struct k_lifo *lifo, void *data); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 907 | |
| 908 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 909 | * @brief Get an element from a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 910 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 911 | * This routine removes a data item from @a lifo in a "last in, first out" |
| 912 | * manner. The first 32 bits of the data item are reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 913 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 914 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 915 | * |
| 916 | * @param lifo Address of the lifo. |
| 917 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 918 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 919 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 920 | * @return Address of the data item if successful. |
| 921 | * @retval NULL if returned without waiting or waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 922 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 923 | extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout); |
| 924 | |
| 925 | #define K_LIFO_INITIALIZER(obj) \ |
| 926 | { \ |
| 927 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 928 | .list = NULL, \ |
| 929 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 930 | } |
| 931 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 932 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 933 | * @brief Statically define and initialize a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 934 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 935 | * The lifo can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 936 | * |
| 937 | * extern struct k_lifo @a name; |
| 938 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 939 | * @param name Name of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 940 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 941 | #define K_LIFO_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 942 | struct k_lifo name \ |
| 943 | __in_section(_k_lifo, static, name) = \ |
| 944 | K_LIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 945 | |
| 946 | /* stacks */ |
| 947 | |
| 948 | struct k_stack { |
| 949 | _wait_q_t wait_q; |
| 950 | uint32_t *base, *next, *top; |
| 951 | |
| 952 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack); |
| 953 | }; |
| 954 | |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 955 | /** |
| 956 | * @brief Initialize a stack. |
| 957 | * |
| 958 | * This routine initializes a stack object, prior to its first use. |
| 959 | * |
| 960 | * @param stack Address of the stack. |
| 961 | * @param buffer Address of array used to hold stacked values. |
| 962 | * @param num_entries Maximum number of values that can be stacked. |
| 963 | * |
| 964 | * @return N/A |
| 965 | */ |
Allan Stephens | 018cd9a | 2016-10-07 15:13:24 -0500 | [diff] [blame] | 966 | extern void k_stack_init(struct k_stack *stack, |
| 967 | uint32_t *buffer, int num_entries); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 968 | |
| 969 | /** |
| 970 | * @brief Push an element onto a stack. |
| 971 | * |
| 972 | * This routine adds a 32-bit value @a data to @a stack. |
| 973 | * |
| 974 | * @note Can be called by ISRs. |
| 975 | * |
| 976 | * @param stack Address of the stack. |
| 977 | * @param data Value to push onto the stack. |
| 978 | * |
| 979 | * @return N/A |
| 980 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 981 | extern void k_stack_push(struct k_stack *stack, uint32_t data); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 982 | |
| 983 | /** |
| 984 | * @brief Pop an element from a stack. |
| 985 | * |
| 986 | * This routine removes a 32-bit value from @a stack in a "last in, first out" |
| 987 | * manner and stores the value in @a data. |
| 988 | * |
| 989 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 990 | * |
| 991 | * @param stack Address of the stack. |
| 992 | * @param data Address of area to hold the value popped from the stack. |
| 993 | * @param timeout Waiting period to obtain a value (in milliseconds), |
| 994 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 995 | * |
| 996 | * @retval 0 if successful. |
| 997 | * @retval -EBUSY if returned without waiting. |
| 998 | * @retval -EAGAIN if waiting period timed out. |
| 999 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1000 | extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout); |
| 1001 | |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 1002 | #define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1003 | { \ |
| 1004 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1005 | .base = stack_buffer, \ |
| 1006 | .next = stack_buffer, \ |
| 1007 | .top = stack_buffer + stack_num_entries, \ |
| 1008 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1009 | } |
| 1010 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1011 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1012 | * @brief Statically define and initialize a stack |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1013 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1014 | * The stack can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1015 | * |
| 1016 | * extern struct k_stack @a name; |
| 1017 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1018 | * @param name Name of the stack. |
| 1019 | * @param stack_num_entries Maximum number of values that can be stacked. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1020 | */ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 1021 | #define K_STACK_DEFINE(name, stack_num_entries) \ |
| 1022 | uint32_t __noinit \ |
| 1023 | _k_stack_buf_##name[stack_num_entries]; \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1024 | struct k_stack name \ |
| 1025 | __in_section(_k_stack, static, name) = \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 1026 | K_STACK_INITIALIZER(name, _k_stack_buf_##name, \ |
| 1027 | stack_num_entries) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1028 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1029 | /** |
| 1030 | * workqueues |
| 1031 | */ |
| 1032 | |
| 1033 | struct k_work; |
| 1034 | |
| 1035 | typedef void (*k_work_handler_t)(struct k_work *); |
| 1036 | |
| 1037 | /** |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1038 | * A workqueue is a thread that executes @ref k_work items that are |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1039 | * queued to it. This is useful for drivers which need to schedule |
| 1040 | * execution of code which might sleep from ISR context. The actual |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1041 | * thread identifier is not stored in the structure in order to save |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1042 | * space. |
| 1043 | */ |
| 1044 | struct k_work_q { |
| 1045 | struct k_fifo fifo; |
| 1046 | }; |
| 1047 | |
| 1048 | /** |
| 1049 | * @brief Work flags. |
| 1050 | */ |
| 1051 | enum { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 1052 | K_WORK_STATE_PENDING, /* Work item pending state */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1053 | }; |
| 1054 | |
| 1055 | /** |
| 1056 | * @brief An item which can be scheduled on a @ref k_work_q. |
| 1057 | */ |
| 1058 | struct k_work { |
| 1059 | void *_reserved; /* Used by k_fifo implementation. */ |
| 1060 | k_work_handler_t handler; |
| 1061 | atomic_t flags[1]; |
| 1062 | }; |
| 1063 | |
| 1064 | /** |
| 1065 | * @brief Statically initialize work item |
| 1066 | */ |
| 1067 | #define K_WORK_INITIALIZER(work_handler) \ |
| 1068 | { \ |
| 1069 | ._reserved = NULL, \ |
| 1070 | .handler = work_handler, \ |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1071 | .flags = { 0 } \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | /** |
| 1075 | * @brief Dynamically initialize work item |
| 1076 | */ |
| 1077 | static inline void k_work_init(struct k_work *work, k_work_handler_t handler) |
| 1078 | { |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1079 | atomic_clear_bit(work->flags, K_WORK_STATE_PENDING); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1080 | work->handler = handler; |
| 1081 | } |
| 1082 | |
| 1083 | /** |
| 1084 | * @brief Submit a work item to a workqueue. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 1085 | * |
| 1086 | * This procedure schedules a work item to be processed. |
| 1087 | * In the case where the work item has already been submitted and is pending |
| 1088 | * execution, calling this function will result in a no-op. In this case, the |
| 1089 | * work item must not be modified externally (e.g. by the caller of this |
| 1090 | * function), since that could cause the work item to be processed in a |
| 1091 | * corrupted state. |
| 1092 | * |
| 1093 | * @param work_q to schedule the work item |
| 1094 | * @param work work item |
| 1095 | * |
| 1096 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1097 | */ |
| 1098 | static inline void k_work_submit_to_queue(struct k_work_q *work_q, |
| 1099 | struct k_work *work) |
| 1100 | { |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 1101 | if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1102 | k_fifo_put(&work_q->fifo, work); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /** |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1107 | * @brief Check if work item is pending. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1108 | * |
| 1109 | * @param work Work item to query |
| 1110 | * |
| 1111 | * @return K_WORK_STATE_PENDING if pending, 0 if not |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1112 | */ |
| 1113 | static inline int k_work_pending(struct k_work *work) |
| 1114 | { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 1115 | return atomic_test_bit(work->flags, K_WORK_STATE_PENDING); |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | /** |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1119 | * @brief Start a new workqueue. |
| 1120 | * |
| 1121 | * This routine must not be called from an ISR. |
| 1122 | * |
| 1123 | * @param work_q Pointer to Work queue |
| 1124 | * @param stack Pointer to work queue thread's stack |
| 1125 | * @param stack_size Size of the work queue thread's stack |
| 1126 | * @param prio Priority of the work queue's thread |
| 1127 | * |
| 1128 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1129 | */ |
Allan Stephens | 904cf97 | 2016-10-07 13:59:23 -0500 | [diff] [blame] | 1130 | extern void k_work_q_start(struct k_work_q *work_q, char *stack, |
| 1131 | unsigned stack_size, unsigned prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1132 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 1133 | #if defined(CONFIG_SYS_CLOCK_EXISTS) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1134 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1135 | /** |
| 1136 | * @brief An item which can be scheduled on a @ref k_work_q with a delay |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1137 | */ |
| 1138 | struct k_delayed_work { |
| 1139 | struct k_work work; |
| 1140 | struct _timeout timeout; |
| 1141 | struct k_work_q *work_q; |
| 1142 | }; |
| 1143 | |
| 1144 | /** |
| 1145 | * @brief Initialize delayed work |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1146 | * |
| 1147 | * Initialize a delayed work item. |
| 1148 | * |
| 1149 | * @param work Delayed work item |
| 1150 | * @param handler Routine invoked when processing delayed work item |
| 1151 | * |
| 1152 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1153 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1154 | extern void k_delayed_work_init(struct k_delayed_work *work, |
| 1155 | k_work_handler_t handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1156 | |
| 1157 | /** |
| 1158 | * @brief Submit a delayed work item to a workqueue. |
| 1159 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1160 | * This routine schedules a work item to be processed after a delay. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1161 | * Once the delay has passed, the work item is submitted to the work queue: |
| 1162 | * at this point, it is no longer possible to cancel it. Once the work item's |
| 1163 | * handler is about to be executed, the work is considered complete and can be |
| 1164 | * resubmitted. |
| 1165 | * |
| 1166 | * Care must be taken if the handler blocks or yield as there is no implicit |
| 1167 | * mutual exclusion mechanism. Such usage is not recommended and if necessary, |
| 1168 | * it should be explicitly done between the submitter and the handler. |
| 1169 | * |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1170 | * @param work_q Workqueue to schedule the work item |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1171 | * @param work Delayed work item |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1172 | * @param delay Delay before scheduling the work item (in milliseconds) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1173 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1174 | * @return 0 in case of success, or negative value in case of error. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1175 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1176 | extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q, |
| 1177 | struct k_delayed_work *work, |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1178 | int32_t delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1179 | |
| 1180 | /** |
| 1181 | * @brief Cancel a delayed work item |
| 1182 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1183 | * This routine cancels a scheduled work item. If the work has been completed |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1184 | * or is idle, this will do nothing. The only case where this can fail is when |
| 1185 | * the work has been submitted to the work queue, but the handler has not run |
| 1186 | * yet. |
| 1187 | * |
| 1188 | * @param work Delayed work item to be canceled |
| 1189 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1190 | * @return 0 in case of success, or negative value in case of error. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1191 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1192 | extern int k_delayed_work_cancel(struct k_delayed_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1193 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 1194 | #endif /* CONFIG_SYS_CLOCK_EXISTS */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1195 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1196 | extern struct k_work_q k_sys_work_q; |
| 1197 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1198 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1199 | * @brief Submit a work item to the system workqueue. |
| 1200 | * |
| 1201 | * @ref k_work_submit_to_queue |
| 1202 | * |
| 1203 | * When using the system workqueue it is not recommended to block or yield |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1204 | * on the handler since its thread is shared system wide it may cause |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1205 | * unexpected behavior. |
| 1206 | */ |
| 1207 | static inline void k_work_submit(struct k_work *work) |
| 1208 | { |
| 1209 | k_work_submit_to_queue(&k_sys_work_q, work); |
| 1210 | } |
| 1211 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 1212 | #if defined(CONFIG_SYS_CLOCK_EXISTS) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1213 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1214 | * @brief Submit a delayed work item to the system workqueue. |
| 1215 | * |
| 1216 | * @ref k_delayed_work_submit_to_queue |
| 1217 | * |
| 1218 | * When using the system workqueue it is not recommended to block or yield |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1219 | * on the handler since its thread is shared system wide it may cause |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1220 | * unexpected behavior. |
| 1221 | */ |
| 1222 | static inline int k_delayed_work_submit(struct k_delayed_work *work, |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1223 | int32_t delay) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1224 | { |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1225 | return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1226 | } |
| 1227 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 1228 | #endif /* CONFIG_SYS_CLOCK_EXISTS */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1229 | |
| 1230 | /** |
| 1231 | * synchronization |
| 1232 | */ |
| 1233 | |
| 1234 | /* mutexes */ |
| 1235 | |
| 1236 | struct k_mutex { |
| 1237 | _wait_q_t wait_q; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 1238 | struct k_thread *owner; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1239 | uint32_t lock_count; |
| 1240 | int owner_orig_prio; |
| 1241 | #ifdef CONFIG_OBJECT_MONITOR |
| 1242 | int num_lock_state_changes; |
| 1243 | int num_conflicts; |
| 1244 | #endif |
| 1245 | |
| 1246 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex); |
| 1247 | }; |
| 1248 | |
| 1249 | #ifdef CONFIG_OBJECT_MONITOR |
| 1250 | #define _MUTEX_INIT_OBJECT_MONITOR \ |
| 1251 | .num_lock_state_changes = 0, .num_conflicts = 0, |
| 1252 | #else |
| 1253 | #define _MUTEX_INIT_OBJECT_MONITOR |
| 1254 | #endif |
| 1255 | |
| 1256 | #define K_MUTEX_INITIALIZER(obj) \ |
| 1257 | { \ |
| 1258 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1259 | .owner = NULL, \ |
| 1260 | .lock_count = 0, \ |
| 1261 | .owner_orig_prio = K_LOWEST_THREAD_PRIO, \ |
| 1262 | _MUTEX_INIT_OBJECT_MONITOR \ |
| 1263 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1264 | } |
| 1265 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1266 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1267 | * @brief Statically define and initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1268 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1269 | * The mutex can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1270 | * |
| 1271 | * extern struct k_mutex @a name; |
| 1272 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1273 | * @param name Name of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1274 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1275 | #define K_MUTEX_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1276 | struct k_mutex name \ |
| 1277 | __in_section(_k_mutex, static, name) = \ |
| 1278 | K_MUTEX_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1279 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1280 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1281 | * @brief Initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1282 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1283 | * This routine initializes a mutex object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1284 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1285 | * Upon completion, the mutex is available and does not have an owner. |
| 1286 | * |
| 1287 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1288 | * |
| 1289 | * @return N/A |
| 1290 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1291 | extern void k_mutex_init(struct k_mutex *mutex); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1292 | |
| 1293 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1294 | * @brief Lock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1295 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1296 | * This routine locks @a mutex. If the mutex is locked by another thread, |
| 1297 | * the calling thread waits until the mutex becomes available or until |
| 1298 | * a timeout occurs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1299 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1300 | * A thread is permitted to lock a mutex it has already locked. The operation |
| 1301 | * completes immediately and the lock count is increased by 1. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1302 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1303 | * @param mutex Address of the mutex. |
| 1304 | * @param timeout Waiting period to lock the mutex (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1305 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1306 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1307 | * @retval 0 if successful. |
| 1308 | * @retval -EBUSY if returned without waiting. |
| 1309 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1310 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1311 | extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1312 | |
| 1313 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1314 | * @brief Unlock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1315 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1316 | * This routine unlocks @a mutex. The mutex must already be locked by the |
| 1317 | * calling thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1318 | * |
| 1319 | * The mutex cannot be claimed by another thread until it has been unlocked by |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1320 | * the calling thread as many times as it was previously locked by that |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1321 | * thread. |
| 1322 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1323 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1324 | * |
| 1325 | * @return N/A |
| 1326 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1327 | extern void k_mutex_unlock(struct k_mutex *mutex); |
| 1328 | |
| 1329 | /* semaphores */ |
| 1330 | |
| 1331 | struct k_sem { |
| 1332 | _wait_q_t wait_q; |
| 1333 | unsigned int count; |
| 1334 | unsigned int limit; |
| 1335 | |
| 1336 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem); |
| 1337 | }; |
| 1338 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1339 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1340 | * @brief Initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1341 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1342 | * This routine initializes a semaphore object, prior to its first use. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1343 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1344 | * @param sem Address of the semaphore. |
| 1345 | * @param initial_count Initial semaphore count. |
| 1346 | * @param limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1347 | * |
| 1348 | * @return N/A |
| 1349 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1350 | extern void k_sem_init(struct k_sem *sem, unsigned int initial_count, |
| 1351 | unsigned int limit); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1352 | |
| 1353 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1354 | * @brief Take a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1355 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1356 | * This routine takes @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1357 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1358 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1359 | * |
| 1360 | * @param sem Address of the semaphore. |
| 1361 | * @param timeout Waiting period to take the semaphore (in milliseconds), |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1362 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1363 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1364 | * @retval 0 if successful. |
| 1365 | * @retval -EBUSY if returned without waiting. |
| 1366 | * @retval -EAGAIN if waiting period timed out. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1367 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1368 | extern int k_sem_take(struct k_sem *sem, int32_t timeout); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1369 | |
| 1370 | /** |
| 1371 | * @brief Give a semaphore. |
| 1372 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1373 | * This routine gives @a sem, unless the semaphore is already at its maximum |
| 1374 | * permitted count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1375 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1376 | * @note Can be called by ISRs. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1377 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1378 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1379 | * |
| 1380 | * @return N/A |
| 1381 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1382 | extern void k_sem_give(struct k_sem *sem); |
| 1383 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1384 | /** |
| 1385 | * @brief Reset a semaphore's count to zero. |
| 1386 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1387 | * This routine sets the count of @a sem to zero. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1388 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1389 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1390 | * |
| 1391 | * @return N/A |
| 1392 | */ |
Benjamin Walsh | 70c68b9 | 2016-09-21 10:37:34 -0400 | [diff] [blame] | 1393 | static inline void k_sem_reset(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1394 | { |
| 1395 | sem->count = 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1396 | } |
| 1397 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1398 | /** |
| 1399 | * @brief Get a semaphore's count. |
| 1400 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1401 | * This routine returns the current count of @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1402 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1403 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1404 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1405 | * @return Current semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1406 | */ |
Tomasz Bursztyka | 276086d | 2016-09-21 16:03:21 +0200 | [diff] [blame] | 1407 | static inline unsigned int k_sem_count_get(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1408 | { |
| 1409 | return sem->count; |
| 1410 | } |
| 1411 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1412 | #define K_SEM_INITIALIZER(obj, initial_count, count_limit) \ |
| 1413 | { \ |
| 1414 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1415 | .count = initial_count, \ |
| 1416 | .limit = count_limit, \ |
| 1417 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1418 | } |
| 1419 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1420 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1421 | * @brief Statically define and initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1422 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1423 | * The semaphore can be accessed outside the module where it is defined using: |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1424 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1425 | * extern struct k_sem @a name; |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1426 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1427 | * @param name Name of the semaphore. |
| 1428 | * @param initial_count Initial semaphore count. |
| 1429 | * @param count_limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1430 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1431 | #define K_SEM_DEFINE(name, initial_count, count_limit) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1432 | struct k_sem name \ |
| 1433 | __in_section(_k_sem, static, name) = \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1434 | K_SEM_INITIALIZER(name, initial_count, count_limit) |
| 1435 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1436 | /* alerts */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1437 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1438 | #define K_ALERT_DEFAULT NULL |
| 1439 | #define K_ALERT_IGNORE ((void *)(-1)) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1440 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1441 | typedef int (*k_alert_handler_t)(struct k_alert *); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1442 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1443 | struct k_alert { |
| 1444 | k_alert_handler_t handler; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1445 | atomic_t send_count; |
| 1446 | struct k_work work_item; |
| 1447 | struct k_sem sem; |
| 1448 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1449 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1450 | }; |
| 1451 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1452 | extern void _alert_deliver(struct k_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1453 | |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1454 | #define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1455 | { \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1456 | .handler = (k_alert_handler_t)alert_handler, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1457 | .send_count = ATOMIC_INIT(0), \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1458 | .work_item = K_WORK_INITIALIZER(_alert_deliver), \ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1459 | .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1460 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1461 | } |
| 1462 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1463 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1464 | * @brief Statically define and initialize an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1465 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1466 | * The alert is to be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1467 | * |
| 1468 | * extern struct k_alert @a name; |
| 1469 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1470 | * @param name Name of the alert. |
| 1471 | * @param alert_handler Action to take when alert is sent. Specify either |
| 1472 | * the address of a function to be invoked by the system workqueue |
| 1473 | * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or |
| 1474 | * K_ALERT_DEFAULT (which causes the alert to pend). |
| 1475 | * @param max_num_pending_alerts Maximum number of pending alerts. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1476 | */ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1477 | #define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1478 | struct k_alert name \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1479 | __in_section(_k_alert, static, name) = \ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1480 | K_ALERT_INITIALIZER(name, alert_handler, \ |
| 1481 | max_num_pending_alerts) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1482 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1483 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1484 | * @brief Initialize an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1485 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1486 | * This routine initializes an alert object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1487 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1488 | * @param alert Address of the alert. |
| 1489 | * @param handler Action to take when alert is sent. Specify either the address |
| 1490 | * of a function to be invoked by the system workqueue thread, |
| 1491 | * K_ALERT_IGNORE (which causes the alert to be ignored), or |
| 1492 | * K_ALERT_DEFAULT (which causes the alert to pend). |
| 1493 | * @param max_num_pending_alerts Maximum number of pending alerts. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1494 | * |
| 1495 | * @return N/A |
| 1496 | */ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1497 | extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler, |
| 1498 | unsigned int max_num_pending_alerts); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1499 | |
| 1500 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1501 | * @brief Receive an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1502 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1503 | * This routine receives a pending alert for @a alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1504 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1505 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1506 | * |
| 1507 | * @param alert Address of the alert. |
| 1508 | * @param timeout Waiting period to receive the alert (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1509 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1510 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1511 | * @retval 0 if successful. |
| 1512 | * @retval -EBUSY if returned without waiting. |
| 1513 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1514 | */ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1515 | extern int k_alert_recv(struct k_alert *alert, int32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1516 | |
| 1517 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1518 | * @brief Signal an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1519 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1520 | * This routine signals @a alert. The action specified for @a alert will |
| 1521 | * be taken, which may trigger the execution of an alert handler function |
| 1522 | * and/or cause the alert to pend (assuming the alert has not reached its |
| 1523 | * maximum number of pending alerts). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1524 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1525 | * @note Can be called by ISRs. |
| 1526 | * |
| 1527 | * @param alert Address of the alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1528 | * |
| 1529 | * @return N/A |
| 1530 | */ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1531 | extern void k_alert_send(struct k_alert *alert); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1532 | |
| 1533 | /** |
| 1534 | * data transfers (complex) |
| 1535 | */ |
| 1536 | |
| 1537 | /* message queues */ |
| 1538 | |
| 1539 | struct k_msgq { |
| 1540 | _wait_q_t wait_q; |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1541 | size_t msg_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1542 | uint32_t max_msgs; |
| 1543 | char *buffer_start; |
| 1544 | char *buffer_end; |
| 1545 | char *read_ptr; |
| 1546 | char *write_ptr; |
| 1547 | uint32_t used_msgs; |
| 1548 | |
| 1549 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq); |
| 1550 | }; |
| 1551 | |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1552 | #define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1553 | { \ |
| 1554 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1555 | .max_msgs = q_max_msgs, \ |
| 1556 | .msg_size = q_msg_size, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1557 | .buffer_start = q_buffer, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1558 | .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1559 | .read_ptr = q_buffer, \ |
| 1560 | .write_ptr = q_buffer, \ |
| 1561 | .used_msgs = 0, \ |
| 1562 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1563 | } |
| 1564 | |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1565 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1566 | * @brief Statically define and initialize a message queue. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1567 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1568 | * The message queue's ring buffer contains space for @a q_max_msgs messages, |
| 1569 | * each of which is @a q_msg_size bytes long. The buffer is aligned to a |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 1570 | * @a q_align -byte boundary, which must be a power of 2. To ensure that each |
| 1571 | * message is similarly aligned to this boundary, @a q_msg_size must also be |
| 1572 | * a multiple of @a q_align. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1573 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1574 | * The message queue can be accessed outside the module where it is defined |
| 1575 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1576 | * |
| 1577 | * extern struct k_msgq @a name; |
| 1578 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1579 | * @param q_name Name of the message queue. |
| 1580 | * @param q_msg_size Message size (in bytes). |
| 1581 | * @param q_max_msgs Maximum number of messages that can be queued. |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 1582 | * @param q_align Alignment of the message queue's ring buffer. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1583 | */ |
| 1584 | #define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \ |
| 1585 | static char __noinit __aligned(q_align) \ |
| 1586 | _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1587 | struct k_msgq q_name \ |
| 1588 | __in_section(_k_msgq, static, q_name) = \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1589 | K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \ |
| 1590 | q_msg_size, q_max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1591 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1592 | /** |
| 1593 | * @brief Initialize a message queue. |
| 1594 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1595 | * This routine initializes a message queue object, prior to its first use. |
| 1596 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 1597 | * The message queue's ring buffer must contain space for @a max_msgs messages, |
| 1598 | * each of which is @a msg_size bytes long. The buffer must be aligned to an |
| 1599 | * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure |
| 1600 | * that each message is similarly aligned to this boundary, @a q_msg_size |
| 1601 | * must also be a multiple of N. |
| 1602 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1603 | * @param q Address of the message queue. |
| 1604 | * @param buffer Pointer to ring buffer that holds queued messages. |
| 1605 | * @param msg_size Message size (in bytes). |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1606 | * @param max_msgs Maximum number of messages that can be queued. |
| 1607 | * |
| 1608 | * @return N/A |
| 1609 | */ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1610 | extern void k_msgq_init(struct k_msgq *q, char *buffer, |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1611 | size_t msg_size, uint32_t max_msgs); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1612 | |
| 1613 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1614 | * @brief Send a message to a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1615 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1616 | * This routine sends a message to message queue @a q. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1617 | * |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 1618 | * @note Can be called by ISRs. |
| 1619 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1620 | * @param q Address of the message queue. |
| 1621 | * @param data Pointer to the message. |
| 1622 | * @param timeout Waiting period to add the message (in milliseconds), |
| 1623 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1624 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1625 | * @retval 0 if successful. |
| 1626 | * @retval -ENOMSG if returned without waiting or after a queue purge. |
| 1627 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1628 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1629 | extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1630 | |
| 1631 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1632 | * @brief Receive a message from a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1633 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1634 | * This routine receives a message from message queue @a q in a "first in, |
| 1635 | * first out" manner. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1636 | * |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 1637 | * @note Can be called by ISRs. |
| 1638 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1639 | * @param q Address of the message queue. |
| 1640 | * @param data Address of area to hold the received message. |
| 1641 | * @param timeout Waiting period to receive the message (in milliseconds), |
| 1642 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1643 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1644 | * @retval 0 if successful. |
| 1645 | * @retval -ENOMSG if returned without waiting. |
| 1646 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1647 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1648 | extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1649 | |
| 1650 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1651 | * @brief Purge a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1652 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1653 | * This routine discards all unreceived messages in a message queue's ring |
| 1654 | * buffer. Any threads that are blocked waiting to send a message to the |
| 1655 | * message queue are unblocked and see an -ENOMSG error code. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1656 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1657 | * @param q Address of the message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1658 | * |
| 1659 | * @return N/A |
| 1660 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1661 | extern void k_msgq_purge(struct k_msgq *q); |
| 1662 | |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1663 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1664 | * @brief Get the amount of free space in a message queue. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1665 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1666 | * This routine returns the number of unused entries in a message queue's |
| 1667 | * ring buffer. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1668 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1669 | * @param q Address of the message queue. |
| 1670 | * |
| 1671 | * @return Number of unused ring buffer entries. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1672 | */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1673 | static inline uint32_t k_msgq_num_free_get(struct k_msgq *q) |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1674 | { |
| 1675 | return q->max_msgs - q->used_msgs; |
| 1676 | } |
| 1677 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1678 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1679 | * @brief Get the number of messages in a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1680 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1681 | * This routine returns the number of messages in a message queue's ring buffer. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1682 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1683 | * @param q Address of the message queue. |
| 1684 | * |
| 1685 | * @return Number of messages. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1686 | */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1687 | static inline uint32_t k_msgq_num_used_get(struct k_msgq *q) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1688 | { |
| 1689 | return q->used_msgs; |
| 1690 | } |
| 1691 | |
| 1692 | struct k_mem_block { |
Peter Mitsis | 0cb65c3 | 2016-09-29 14:07:36 -0400 | [diff] [blame] | 1693 | struct k_mem_pool *pool_id; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1694 | void *addr_in_pool; |
| 1695 | void *data; |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 1696 | size_t req_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1697 | }; |
| 1698 | |
| 1699 | /* mailboxes */ |
| 1700 | |
| 1701 | struct k_mbox_msg { |
| 1702 | /** internal use only - needed for legacy API support */ |
| 1703 | uint32_t _mailbox; |
| 1704 | /** size of message (in bytes) */ |
Peter Mitsis | d93078c | 2016-10-14 12:59:37 -0400 | [diff] [blame] | 1705 | size_t size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1706 | /** application-defined information value */ |
| 1707 | uint32_t info; |
| 1708 | /** sender's message data buffer */ |
| 1709 | void *tx_data; |
| 1710 | /** internal use only - needed for legacy API support */ |
| 1711 | void *_rx_data; |
| 1712 | /** message data block descriptor */ |
| 1713 | struct k_mem_block tx_block; |
| 1714 | /** source thread id */ |
| 1715 | k_tid_t rx_source_thread; |
| 1716 | /** target thread id */ |
| 1717 | k_tid_t tx_target_thread; |
| 1718 | /** internal use only - thread waiting on send (may be a dummy) */ |
| 1719 | k_tid_t _syncing_thread; |
| 1720 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 1721 | /** internal use only - semaphore used during asynchronous send */ |
| 1722 | struct k_sem *_async_sem; |
| 1723 | #endif |
| 1724 | }; |
| 1725 | |
| 1726 | struct k_mbox { |
| 1727 | _wait_q_t tx_msg_queue; |
| 1728 | _wait_q_t rx_msg_queue; |
| 1729 | |
| 1730 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox); |
| 1731 | }; |
| 1732 | |
| 1733 | #define K_MBOX_INITIALIZER(obj) \ |
| 1734 | { \ |
| 1735 | .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \ |
| 1736 | .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \ |
| 1737 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1738 | } |
| 1739 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1740 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1741 | * @brief Statically define and initialize a mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1742 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1743 | * The mailbox is to be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1744 | * |
| 1745 | * extern struct k_mbox @a name; |
| 1746 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1747 | * @param name Name of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1748 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1749 | #define K_MBOX_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1750 | struct k_mbox name \ |
| 1751 | __in_section(_k_mbox, static, name) = \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1752 | K_MBOX_INITIALIZER(name) \ |
| 1753 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1754 | /** |
| 1755 | * @brief Initialize a mailbox. |
| 1756 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1757 | * This routine initializes a mailbox object, prior to its first use. |
| 1758 | * |
| 1759 | * @param mbox Address of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1760 | * |
| 1761 | * @return N/A |
| 1762 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1763 | extern void k_mbox_init(struct k_mbox *mbox); |
| 1764 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1765 | /** |
| 1766 | * @brief Send a mailbox message in a synchronous manner. |
| 1767 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1768 | * This routine sends a message to @a mbox and waits for a receiver to both |
| 1769 | * receive and process it. The message data may be in a buffer, in a memory |
| 1770 | * pool block, or non-existent (i.e. an empty message). |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1771 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1772 | * @param mbox Address of the mailbox. |
| 1773 | * @param tx_msg Address of the transmit message descriptor. |
| 1774 | * @param timeout Waiting period for the message to be received (in |
| 1775 | * milliseconds), or one of the special values K_NO_WAIT |
| 1776 | * and K_FOREVER. Once the message has been received, |
| 1777 | * this routine waits as long as necessary for the message |
| 1778 | * to be completely processed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1779 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1780 | * @retval 0 if successful. |
| 1781 | * @retval -ENOMSG if returned without waiting. |
| 1782 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1783 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1784 | extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1785 | int32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1786 | |
| 1787 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 1788 | /** |
| 1789 | * @brief Send a mailbox message in an asynchronous manner. |
| 1790 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1791 | * This routine sends a message to @a mbox without waiting for a receiver |
| 1792 | * to process it. The message data may be in a buffer, in a memory pool block, |
| 1793 | * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem |
| 1794 | * will be given when the message has been both received and completely |
| 1795 | * processed by the receiver. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1796 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1797 | * @param mbox Address of the mailbox. |
| 1798 | * @param tx_msg Address of the transmit message descriptor. |
| 1799 | * @param sem Address of a semaphore, or NULL if none is needed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1800 | * |
| 1801 | * @return N/A |
| 1802 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1803 | extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1804 | struct k_sem *sem); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1805 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1806 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1807 | /** |
| 1808 | * @brief Receive a mailbox message. |
| 1809 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1810 | * This routine receives a message from @a mbox, then optionally retrieves |
| 1811 | * its data and disposes of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1812 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1813 | * @param mbox Address of the mailbox. |
| 1814 | * @param rx_msg Address of the receive message descriptor. |
| 1815 | * @param buffer Address of the buffer to receive data, or NULL to defer data |
| 1816 | * retrieval and message disposal until later. |
| 1817 | * @param timeout Waiting period for a message to be received (in |
| 1818 | * milliseconds), or one of the special values K_NO_WAIT |
| 1819 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1820 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1821 | * @retval 0 if successful. |
| 1822 | * @retval -ENOMSG if returned without waiting. |
| 1823 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1824 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1825 | extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1826 | void *buffer, int32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1827 | |
| 1828 | /** |
| 1829 | * @brief Retrieve mailbox message data into a buffer. |
| 1830 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1831 | * This routine completes the processing of a received message by retrieving |
| 1832 | * its data into a buffer, then disposing of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1833 | * |
| 1834 | * Alternatively, this routine can be used to dispose of a received message |
| 1835 | * without retrieving its data. |
| 1836 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1837 | * @param rx_msg Address of the receive message descriptor. |
| 1838 | * @param buffer Address of the buffer to receive data, or NULL to discard |
| 1839 | * the data. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1840 | * |
| 1841 | * @return N/A |
| 1842 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1843 | extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1844 | |
| 1845 | /** |
| 1846 | * @brief Retrieve mailbox message data into a memory pool block. |
| 1847 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1848 | * This routine completes the processing of a received message by retrieving |
| 1849 | * its data into a memory pool block, then disposing of the message. |
| 1850 | * The memory pool block that results from successful retrieval must be |
| 1851 | * returned to the pool once the data has been processed, even in cases |
| 1852 | * where zero bytes of data are retrieved. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1853 | * |
| 1854 | * Alternatively, this routine can be used to dispose of a received message |
| 1855 | * without retrieving its data. In this case there is no need to return a |
| 1856 | * memory pool block to the pool. |
| 1857 | * |
| 1858 | * This routine allocates a new memory pool block for the data only if the |
| 1859 | * data is not already in one. If a new block cannot be allocated, the routine |
| 1860 | * returns a failure code and the received message is left unchanged. This |
| 1861 | * permits the caller to reattempt data retrieval at a later time or to dispose |
| 1862 | * of the received message without retrieving its data. |
| 1863 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1864 | * @param rx_msg Address of a receive message descriptor. |
| 1865 | * @param pool Address of memory pool, or NULL to discard data. |
| 1866 | * @param block Address of the area to hold memory pool block info. |
| 1867 | * @param timeout Waiting period to wait for a memory pool block (in |
| 1868 | * milliseconds), or one of the special values K_NO_WAIT |
| 1869 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1870 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1871 | * @retval 0 if successful. |
| 1872 | * @retval -ENOMEM if returned without waiting. |
| 1873 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1874 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1875 | extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg, |
Peter Mitsis | 0cb65c3 | 2016-09-29 14:07:36 -0400 | [diff] [blame] | 1876 | struct k_mem_pool *pool, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1877 | struct k_mem_block *block, int32_t timeout); |
| 1878 | |
| 1879 | /* pipes */ |
| 1880 | |
| 1881 | struct k_pipe { |
| 1882 | unsigned char *buffer; /* Pipe buffer: may be NULL */ |
| 1883 | size_t size; /* Buffer size */ |
| 1884 | size_t bytes_used; /* # bytes used in buffer */ |
| 1885 | size_t read_index; /* Where in buffer to read from */ |
| 1886 | size_t write_index; /* Where in buffer to write */ |
| 1887 | |
| 1888 | struct { |
| 1889 | _wait_q_t readers; /* Reader wait queue */ |
| 1890 | _wait_q_t writers; /* Writer wait queue */ |
| 1891 | } wait_q; |
| 1892 | |
| 1893 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe); |
| 1894 | }; |
| 1895 | |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1896 | #define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1897 | { \ |
| 1898 | .buffer = pipe_buffer, \ |
| 1899 | .size = pipe_buffer_size, \ |
| 1900 | .bytes_used = 0, \ |
| 1901 | .read_index = 0, \ |
| 1902 | .write_index = 0, \ |
| 1903 | .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \ |
| 1904 | .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \ |
| 1905 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1906 | } |
| 1907 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1908 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1909 | * @brief Statically define and initialize a pipe. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1910 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1911 | * The pipe can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1912 | * |
| 1913 | * extern struct k_pipe @a name; |
| 1914 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1915 | * @param name Name of the pipe. |
| 1916 | * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes), |
| 1917 | * or zero if no ring buffer is used. |
| 1918 | * @param pipe_align Alignment of the pipe's ring buffer (power of 2). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1919 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1920 | #define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \ |
| 1921 | static unsigned char __noinit __aligned(pipe_align) \ |
| 1922 | _k_pipe_buf_##name[pipe_buffer_size]; \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1923 | struct k_pipe name \ |
| 1924 | __in_section(_k_pipe, static, name) = \ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1925 | K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1926 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1927 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1928 | * @brief Initialize a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1929 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1930 | * This routine initializes a pipe object, prior to its first use. |
| 1931 | * |
| 1932 | * @param pipe Address of the pipe. |
| 1933 | * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer |
| 1934 | * is used. |
| 1935 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 1936 | * buffer is used. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1937 | * |
| 1938 | * @return N/A |
| 1939 | */ |
| 1940 | extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, |
| 1941 | size_t size); |
| 1942 | |
| 1943 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1944 | * @brief Write data to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1945 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1946 | * This routine writes up to @a bytes_to_write bytes of data to @a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1947 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1948 | * @param pipe Address of the pipe. |
| 1949 | * @param data Address of data to write. |
| 1950 | * @param bytes_to_write Size of data (in bytes). |
| 1951 | * @param bytes_written Address of area to hold the number of bytes written. |
| 1952 | * @param min_xfer Minimum number of bytes to write. |
| 1953 | * @param timeout Waiting period to wait for the data to be written (in |
| 1954 | * milliseconds), or one of the special values K_NO_WAIT |
| 1955 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1956 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1957 | * @retval 0 if at least @a min_xfer data bytes were written. |
| 1958 | * @retval -EIO if returned without waiting; zero data bytes were written. |
| 1959 | * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer |
| 1960 | * minus one data bytes were written. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1961 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1962 | extern int k_pipe_put(struct k_pipe *pipe, void *data, |
| 1963 | size_t bytes_to_write, size_t *bytes_written, |
| 1964 | size_t min_xfer, int32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1965 | |
| 1966 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1967 | * @brief Read data from a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1968 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1969 | * This routine reads up to @a bytes_to_read bytes of data from @a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1970 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1971 | * @param pipe Address of the pipe. |
| 1972 | * @param data Address to place the data read from pipe. |
| 1973 | * @param bytes_to_read Maximum number of data bytes to read. |
| 1974 | * @param bytes_read Address of area to hold the number of bytes read. |
| 1975 | * @param min_xfer Minimum number of data bytes to read. |
| 1976 | * @param timeout Waiting period to wait for the data to be read (in |
| 1977 | * milliseconds), or one of the special values K_NO_WAIT |
| 1978 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1979 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1980 | * @retval 0 if at least @a min_xfer data bytes were read. |
| 1981 | * @retval -EIO if returned without waiting; zero data bytes were read. |
| 1982 | * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer |
| 1983 | * minus one data bytes were read. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1984 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1985 | extern int k_pipe_get(struct k_pipe *pipe, void *data, |
| 1986 | size_t bytes_to_read, size_t *bytes_read, |
| 1987 | size_t min_xfer, int32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1988 | |
Peter Mitsis | 2fef023 | 2016-10-14 14:53:44 -0400 | [diff] [blame] | 1989 | #if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1990 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1991 | * @brief Write memory block to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1992 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1993 | * This routine writes the data contained in a memory block to @a pipe. |
| 1994 | * Once all of the data in the block has been written to the pipe, it will |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1995 | * free the memory block @a block and give the semaphore @a sem (if specified). |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1996 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1997 | * @param pipe Address of the pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1998 | * @param block Memory block containing data to send |
| 1999 | * @param size Number of data bytes in memory block to send |
| 2000 | * @param sem Semaphore to signal upon completion (else NULL) |
| 2001 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2002 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2003 | */ |
| 2004 | extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, |
| 2005 | size_t size, struct k_sem *sem); |
Peter Mitsis | 2fef023 | 2016-10-14 14:53:44 -0400 | [diff] [blame] | 2006 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2007 | |
| 2008 | /** |
| 2009 | * memory management |
| 2010 | */ |
| 2011 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2012 | /* memory slabs */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2013 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2014 | struct k_mem_slab { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2015 | _wait_q_t wait_q; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 2016 | uint32_t num_blocks; |
| 2017 | size_t block_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2018 | char *buffer; |
| 2019 | char *free_list; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 2020 | uint32_t num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2021 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2022 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2023 | }; |
| 2024 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2025 | #define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \ |
| 2026 | slab_num_blocks) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2027 | { \ |
| 2028 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2029 | .num_blocks = slab_num_blocks, \ |
| 2030 | .block_size = slab_block_size, \ |
| 2031 | .buffer = slab_buffer, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2032 | .free_list = NULL, \ |
| 2033 | .num_used = 0, \ |
| 2034 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 2035 | } |
| 2036 | |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2037 | /** |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2038 | * @brief Statically define and initialize a memory slab. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2039 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2040 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2041 | * that are @a slab_block_size bytes long. The buffer is aligned to a |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2042 | * @a slab_align -byte boundary. To ensure that each memory block is similarly |
| 2043 | * aligned to this boundary, @a slab_block_size must also be a multiple of |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2044 | * @a slab_align. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2045 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2046 | * The memory slab can be accessed outside the module where it is defined |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2047 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2048 | * |
| 2049 | * extern struct k_mem_slab @a name; |
| 2050 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2051 | * @param name Name of the memory slab. |
| 2052 | * @param slab_block_size Size of each memory block (in bytes). |
| 2053 | * @param slab_num_blocks Number memory blocks. |
| 2054 | * @param slab_align Alignment of the memory slab's buffer (power of 2). |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2055 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2056 | #define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \ |
| 2057 | char __noinit __aligned(slab_align) \ |
| 2058 | _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \ |
| 2059 | struct k_mem_slab name \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2060 | __in_section(_k_mem_slab, static, name) = \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2061 | K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \ |
| 2062 | slab_block_size, slab_num_blocks) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2063 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2064 | /** |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2065 | * @brief Initialize a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2066 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2067 | * Initializes a memory slab, prior to its first use. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2068 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2069 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
| 2070 | * that are @a slab_block_size bytes long. The buffer must be aligned to an |
| 2071 | * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...). |
| 2072 | * To ensure that each memory block is similarly aligned to this boundary, |
| 2073 | * @a slab_block_size must also be a multiple of N. |
| 2074 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2075 | * @param slab Address of the memory slab. |
| 2076 | * @param buffer Pointer to buffer used for the memory blocks. |
| 2077 | * @param block_size Size of each memory block (in bytes). |
| 2078 | * @param num_blocks Number of memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2079 | * |
| 2080 | * @return N/A |
| 2081 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2082 | extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer, |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 2083 | size_t block_size, uint32_t num_blocks); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2084 | |
| 2085 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2086 | * @brief Allocate memory from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2087 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2088 | * This routine allocates a memory block from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2089 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2090 | * @param slab Address of the memory slab. |
| 2091 | * @param mem Pointer to block address area. |
| 2092 | * @param timeout Maximum time to wait for operation to complete |
| 2093 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 2094 | * or K_FOREVER to wait as long as necessary. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2095 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2096 | * @retval 0 if successful. The block address area pointed at by @a mem |
| 2097 | * is set to the starting address of the memory block. |
| 2098 | * @retval -ENOMEM if failed immediately. |
| 2099 | * @retval -EAGAIN if timed out. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2100 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2101 | extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, |
| 2102 | int32_t timeout); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2103 | |
| 2104 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2105 | * @brief Free memory allocated from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2106 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2107 | * This routine releases a previously allocated memory block back to its |
| 2108 | * associated memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2109 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2110 | * @param slab Address of the memory slab. |
| 2111 | * @param mem Pointer to block address area (as set by k_mem_slab_alloc()). |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2112 | * |
| 2113 | * @return N/A |
| 2114 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2115 | extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2116 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2117 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2118 | * @brief Get the number of used blocks in a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2119 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2120 | * This routine gets the number of memory blocks that are currently |
| 2121 | * allocated in @a slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2122 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2123 | * @param slab Address of the memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2124 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2125 | * @return Number of allocated memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2126 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2127 | static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2128 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2129 | return slab->num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2130 | } |
| 2131 | |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2132 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2133 | * @brief Get the number of unused blocks in a memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2134 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2135 | * This routine gets the number of memory blocks that are currently |
| 2136 | * unallocated in @a slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2137 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2138 | * @param slab Address of the memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2139 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2140 | * @return Number of unallocated memory blocks. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2141 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2142 | static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab) |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2143 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2144 | return slab->num_blocks - slab->num_used; |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2145 | } |
| 2146 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2147 | /* memory pools */ |
| 2148 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2149 | /* |
| 2150 | * Memory pool requires a buffer and two arrays of structures for the |
| 2151 | * memory block accounting: |
| 2152 | * A set of arrays of k_mem_pool_quad_block structures where each keeps a |
| 2153 | * status of four blocks of memory. |
| 2154 | */ |
| 2155 | struct k_mem_pool_quad_block { |
| 2156 | char *mem_blocks; /* pointer to the first of four memory blocks */ |
| 2157 | uint32_t mem_status; /* four bits. If bit is set, memory block is |
| 2158 | allocated */ |
| 2159 | }; |
| 2160 | /* |
| 2161 | * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting |
| 2162 | * blocks of one size. Block sizes go from maximal to minimal. Next memory |
| 2163 | * block size is 4 times less than the previous one and thus requires 4 times |
| 2164 | * bigger array of k_mem_pool_quad_block structures to keep track of the |
| 2165 | * memory blocks. |
| 2166 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2167 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2168 | /* |
| 2169 | * The array of k_mem_pool_block_set keeps the information of each array of |
| 2170 | * k_mem_pool_quad_block structures |
| 2171 | */ |
| 2172 | struct k_mem_pool_block_set { |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2173 | size_t block_size; /* memory block size */ |
| 2174 | uint32_t nr_of_entries; /* nr of quad block structures in the array */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2175 | struct k_mem_pool_quad_block *quad_block; |
| 2176 | int count; |
| 2177 | }; |
| 2178 | |
| 2179 | /* Memory pool descriptor */ |
| 2180 | struct k_mem_pool { |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2181 | size_t max_block_size; |
| 2182 | size_t min_block_size; |
| 2183 | uint32_t nr_of_maxblocks; |
| 2184 | uint32_t nr_of_block_sets; |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2185 | struct k_mem_pool_block_set *block_set; |
| 2186 | char *bufblock; |
| 2187 | _wait_q_t wait_q; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2188 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool); |
| 2189 | }; |
| 2190 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2191 | #ifdef CONFIG_ARM |
| 2192 | #define _SECTION_TYPE_SIGN "%" |
| 2193 | #else |
| 2194 | #define _SECTION_TYPE_SIGN "@" |
| 2195 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2196 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2197 | /* |
| 2198 | * Static memory pool initialization |
| 2199 | */ |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 2200 | /** |
| 2201 | * @cond internal |
| 2202 | * Make Doxygen skip assembler macros |
| 2203 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2204 | /* |
| 2205 | * Use .altmacro to be able to recalculate values and pass them as string |
| 2206 | * arguments when calling assembler macros resursively |
| 2207 | */ |
| 2208 | __asm__(".altmacro\n\t"); |
| 2209 | |
| 2210 | /* |
| 2211 | * Recursively calls a macro |
| 2212 | * The followig global symbols need to be initialized: |
| 2213 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2214 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2215 | * Notes: |
| 2216 | * Global symbols are used due the fact that assembler macro allows only |
| 2217 | * one argument be passed with the % conversion |
| 2218 | * Some assemblers do not get division operation ("/"). To avoid it >> 2 |
| 2219 | * is used instead of / 4. |
| 2220 | * n_max argument needs to go first in the invoked macro, as some |
| 2221 | * assemblers concatenate \name and %(\n_max * 4) arguments |
| 2222 | * if \name goes first |
| 2223 | */ |
| 2224 | __asm__(".macro __do_recurse macro_name, name, n_max\n\t" |
| 2225 | ".ifge __memory_pool_max_block_size >> 2 -" |
| 2226 | " __memory_pool_min_block_size\n\t\t" |
| 2227 | "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t" |
| 2228 | "\\macro_name %(\\n_max * 4) \\name\n\t" |
| 2229 | ".endif\n\t" |
| 2230 | ".endm\n"); |
| 2231 | |
| 2232 | /* |
| 2233 | * Build quad blocks |
| 2234 | * Macro allocates space in memory for the array of k_mem_pool_quad_block |
| 2235 | * structures and recursively calls itself for the next array, 4 times |
| 2236 | * larger. |
| 2237 | * The followig global symbols need to be initialized: |
| 2238 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2239 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2240 | * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block) |
| 2241 | */ |
| 2242 | __asm__(".macro _build_quad_blocks n_max, name\n\t" |
Dmitriy Korovkin | 3c90651 | 2016-10-06 15:50:40 -0400 | [diff] [blame] | 2243 | ".balign 4\n\t" |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2244 | "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t" |
| 2245 | ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t" |
| 2246 | ".if \\n_max % 4\n\t\t" |
| 2247 | ".skip __memory_pool_quad_block_size\n\t" |
| 2248 | ".endif\n\t" |
| 2249 | "__do_recurse _build_quad_blocks \\name \\n_max\n\t" |
| 2250 | ".endm\n"); |
| 2251 | |
| 2252 | /* |
| 2253 | * Build block sets and initialize them |
| 2254 | * Macro initializes the k_mem_pool_block_set structure and |
| 2255 | * recursively calls itself for the next one. |
| 2256 | * The followig global symbols need to be initialized: |
| 2257 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2258 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2259 | * __memory_pool_block_set_count, the number of the elements in the |
| 2260 | * block set array must be set to 0. Macro calculates it's real |
| 2261 | * value. |
| 2262 | * Since the macro initializes pointers to an array of k_mem_pool_quad_block |
| 2263 | * structures, _build_quad_blocks must be called prior it. |
| 2264 | */ |
| 2265 | __asm__(".macro _build_block_set n_max, name\n\t" |
| 2266 | ".int __memory_pool_max_block_size\n\t" /* block_size */ |
| 2267 | ".if \\n_max % 4\n\t\t" |
| 2268 | ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */ |
| 2269 | ".else\n\t\t" |
| 2270 | ".int \\n_max >> 2\n\t" |
| 2271 | ".endif\n\t" |
| 2272 | ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */ |
| 2273 | ".int 0\n\t" /* count */ |
| 2274 | "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t" |
| 2275 | "__do_recurse _build_block_set \\name \\n_max\n\t" |
| 2276 | ".endm\n"); |
| 2277 | |
| 2278 | /* |
| 2279 | * Build a memory pool structure and initialize it |
| 2280 | * Macro uses __memory_pool_block_set_count global symbol, |
| 2281 | * block set addresses and buffer address, it may be called only after |
| 2282 | * _build_block_set |
| 2283 | */ |
| 2284 | __asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t" |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2285 | ".pushsection ._k_mem_pool.static.\\name,\"aw\"," |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2286 | _SECTION_TYPE_SIGN "progbits\n\t" |
| 2287 | ".globl \\name\n\t" |
| 2288 | "\\name:\n\t" |
| 2289 | ".int \\max_size\n\t" /* max_block_size */ |
| 2290 | ".int \\min_size\n\t" /* min_block_size */ |
| 2291 | ".int \\n_max\n\t" /* nr_of_maxblocks */ |
| 2292 | ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */ |
| 2293 | ".int _mem_pool_block_sets_\\name\n\t" /* block_set */ |
| 2294 | ".int _mem_pool_buffer_\\name\n\t" /* bufblock */ |
| 2295 | ".int 0\n\t" /* wait_q->head */ |
| 2296 | ".int 0\n\t" /* wait_q->next */ |
| 2297 | ".popsection\n\t" |
| 2298 | ".endm\n"); |
| 2299 | |
| 2300 | #define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \ |
| 2301 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 2302 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
| 2303 | __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \ |
| 2304 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 2305 | __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \ |
| 2306 | STRINGIFY(name) "\n\t"); \ |
| 2307 | __asm__(".popsection\n\t") |
| 2308 | |
| 2309 | #define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \ |
| 2310 | __asm__("__memory_pool_block_set_count = 0\n\t"); \ |
| 2311 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 2312 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 2313 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
Dmitriy Korovkin | 3c90651 | 2016-10-06 15:50:40 -0400 | [diff] [blame] | 2314 | __asm__(".balign 4\n\t"); \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2315 | __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \ |
| 2316 | __asm__("_build_block_set " STRINGIFY(n_max) " " \ |
| 2317 | STRINGIFY(name) "\n\t"); \ |
| 2318 | __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \ |
| 2319 | __asm__(".int __memory_pool_block_set_count\n\t"); \ |
| 2320 | __asm__(".popsection\n\t"); \ |
| 2321 | extern uint32_t _mem_pool_block_set_count_##name; \ |
| 2322 | extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[] |
| 2323 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2324 | #define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \ |
| 2325 | char __noinit __aligned(align) \ |
| 2326 | _mem_pool_buffer_##name[(max_size) * (n_max)] |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2327 | |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 2328 | /* |
| 2329 | * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block) |
| 2330 | * to __memory_pool_quad_block_size absolute symbol. |
| 2331 | * This function does not get called, but compiler calculates the value and |
| 2332 | * assigns it to the absolute symbol, that, in turn is used by assembler macros. |
| 2333 | */ |
| 2334 | static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void) |
| 2335 | { |
| 2336 | __asm__(".globl __memory_pool_quad_block_size\n\t" |
| 2337 | #ifdef CONFIG_NIOS2 |
| 2338 | "__memory_pool_quad_block_size = %0\n\t" |
| 2339 | #else |
| 2340 | "__memory_pool_quad_block_size = %c0\n\t" |
| 2341 | #endif |
| 2342 | : |
| 2343 | : "n"(sizeof(struct k_mem_pool_quad_block))); |
| 2344 | } |
| 2345 | |
| 2346 | /** |
| 2347 | * @endcond |
| 2348 | * End of assembler macros that Doxygen has to skip |
| 2349 | */ |
| 2350 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2351 | /** |
| 2352 | * @brief Define a memory pool |
| 2353 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2354 | * This defines and initializes a memory pool. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2355 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2356 | * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes |
| 2357 | * long. The memory pool allows blocks to be repeatedly partitioned into |
| 2358 | * quarters, down to blocks of @a min_size bytes long. The buffer is aligned |
| 2359 | * to a @a align -byte boundary. To ensure that the minimum sized blocks are |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2360 | * similarly aligned to this boundary, @a min_size must also be a multiple of |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2361 | * @a align. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2362 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2363 | * If the pool is to be accessed outside the module where it is defined, it |
| 2364 | * can be declared via |
| 2365 | * |
| 2366 | * extern struct k_mem_pool @a name; |
| 2367 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2368 | * @param name Name of the memory pool. |
| 2369 | * @param min_size Size of the smallest blocks in the pool (in bytes). |
| 2370 | * @param max_size Size of the largest blocks in the pool (in bytes). |
| 2371 | * @param n_max Number of maximum sized blocks in the pool. |
| 2372 | * @param align Alignment of the pool's buffer (power of 2). |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2373 | */ |
| 2374 | #define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2375 | _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \ |
| 2376 | _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \ |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2377 | _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2378 | __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \ |
| 2379 | STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \ |
| 2380 | extern struct k_mem_pool name |
| 2381 | |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2382 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2383 | * @brief Allocate memory from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2384 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2385 | * This routine allocates a memory block from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2386 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2387 | * @param pool Address of the memory pool. |
| 2388 | * @param block Pointer to block descriptor for the allocated memory. |
| 2389 | * @param size Amount of memory to allocate (in bytes). |
| 2390 | * @param timeout Maximum time to wait for operation to complete |
| 2391 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 2392 | * or K_FOREVER to wait as long as necessary. |
| 2393 | * |
| 2394 | * @retval 0 if successful. The @a data field of the block descriptor |
| 2395 | * is set to the starting address of the memory block. |
| 2396 | * @retval -ENOMEM if unable to allocate a memory block. |
| 2397 | * @retval -EAGAIN if timed out. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2398 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2399 | extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block, |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2400 | size_t size, int32_t timeout); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2401 | |
| 2402 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2403 | * @brief Free memory allocated from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2404 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2405 | * This routine releases a previously allocated memory block back to its |
| 2406 | * memory pool. |
| 2407 | * |
| 2408 | * @param block Pointer to block descriptor for the allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2409 | * |
| 2410 | * @return N/A |
| 2411 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2412 | extern void k_mem_pool_free(struct k_mem_block *block); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2413 | |
| 2414 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2415 | * @brief Defragment a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2416 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2417 | * This routine instructs a memory pool to concatenate unused memory blocks |
| 2418 | * into larger blocks wherever possible. Manually defragmenting the memory |
| 2419 | * pool may speed up future allocations of memory blocks by eliminating the |
| 2420 | * need for the memory pool to perform an automatic partial defragmentation. |
| 2421 | * |
| 2422 | * @param pool Address of the memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2423 | * |
| 2424 | * @return N/A |
| 2425 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2426 | extern void k_mem_pool_defrag(struct k_mem_pool *pool); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2427 | |
| 2428 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2429 | * @brief Allocate memory from heap. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2430 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2431 | * This routine provides traditional malloc() semantics. Memory is |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 2432 | * allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2433 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2434 | * @param size Amount of memory requested (in bytes). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2435 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2436 | * @return Address of the allocated memory if successful; otherwise NULL. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2437 | */ |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2438 | extern void *k_malloc(size_t size); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2439 | |
| 2440 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2441 | * @brief Free memory allocated from heap. |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 2442 | * |
| 2443 | * This routine provides traditional free() semantics. The memory being |
| 2444 | * returned must have been allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2445 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2446 | * @param ptr Pointer to previously allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2447 | * |
| 2448 | * @return N/A |
| 2449 | */ |
| 2450 | extern void k_free(void *ptr); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2451 | |
| 2452 | /* |
| 2453 | * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to |
| 2454 | * hook into the device subsystem, which itself uses nanokernel semaphores, |
| 2455 | * and thus currently requires the definition of nano_sem. |
| 2456 | */ |
| 2457 | #include <legacy.h> |
| 2458 | #include <arch/cpu.h> |
| 2459 | |
| 2460 | /* |
| 2461 | * private APIs that are utilized by one or more public APIs |
| 2462 | */ |
| 2463 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2464 | extern int _is_thread_essential(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2465 | extern void _init_static_threads(void); |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 2466 | extern void _timer_expiration_handler(struct _timeout *t); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2467 | |
| 2468 | #ifdef __cplusplus |
| 2469 | } |
| 2470 | #endif |
| 2471 | |
Andrew Boie | e004dec | 2016-11-07 09:01:19 -0800 | [diff] [blame] | 2472 | #if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) |
| 2473 | /* |
| 2474 | * Define new and delete operators. |
| 2475 | * At this moment, the operators do nothing since objects are supposed |
| 2476 | * to be statically allocated. |
| 2477 | */ |
| 2478 | inline void operator delete(void *ptr) |
| 2479 | { |
| 2480 | (void)ptr; |
| 2481 | } |
| 2482 | |
| 2483 | inline void operator delete[](void *ptr) |
| 2484 | { |
| 2485 | (void)ptr; |
| 2486 | } |
| 2487 | |
| 2488 | inline void *operator new(size_t size) |
| 2489 | { |
| 2490 | (void)size; |
| 2491 | return NULL; |
| 2492 | } |
| 2493 | |
| 2494 | inline void *operator new[](size_t size) |
| 2495 | { |
| 2496 | (void)size; |
| 2497 | return NULL; |
| 2498 | } |
| 2499 | |
| 2500 | /* Placement versions of operator new and delete */ |
| 2501 | inline void operator delete(void *ptr1, void *ptr2) |
| 2502 | { |
| 2503 | (void)ptr1; |
| 2504 | (void)ptr2; |
| 2505 | } |
| 2506 | |
| 2507 | inline void operator delete[](void *ptr1, void *ptr2) |
| 2508 | { |
| 2509 | (void)ptr1; |
| 2510 | (void)ptr2; |
| 2511 | } |
| 2512 | |
| 2513 | inline void *operator new(size_t size, void *ptr) |
| 2514 | { |
| 2515 | (void)size; |
| 2516 | return ptr; |
| 2517 | } |
| 2518 | |
| 2519 | inline void *operator new[](size_t size, void *ptr) |
| 2520 | { |
| 2521 | (void)size; |
| 2522 | return ptr; |
| 2523 | } |
| 2524 | |
| 2525 | #endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */ |
| 2526 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2527 | #endif /* _kernel__h_ */ |