|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Erik Henriksson |
| 3 | + * Copyright (c) 2021, 2023, Niklas Hauser |
| 4 | + * |
| 5 | + * This file is part of the modm project. |
| 6 | + * |
| 7 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 8 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 9 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 10 | + */ |
| 11 | +// ---------------------------------------------------------------------------- |
| 12 | + |
| 13 | +#pragma once |
| 14 | + |
| 15 | +#include <stdint.h> |
| 16 | +#include <stddef.h> |
| 17 | +#include <stdbool.h> |
| 18 | +#include <modm/architecture/utils.hpp> |
| 19 | + |
| 20 | +#ifdef __cplusplus |
| 21 | +extern "C" |
| 22 | +{ |
| 23 | +#endif |
| 24 | + |
| 25 | +/** |
| 26 | + * @ingroup modm_processing_fiber |
| 27 | + * @defgroup modm_processing_fiber_context Fiber Context Functions |
| 28 | + * |
| 29 | + * These functions implement the underlying architecture specific operations to |
| 30 | + * initialize, start, yield, and end fibers. These can be used to implement an |
| 31 | + * alternative scheduling strategy or use fibers outside of modm. |
| 32 | + * @{ |
| 33 | + */ |
| 34 | + |
| 35 | +/// The fiber context stores information about the stack. |
| 36 | +struct modm_context_t |
| 37 | +{ |
| 38 | + uintptr_t *sp; ///< Stored stack pointer, only valid when *not* running! |
| 39 | + uintptr_t *bottom; ///< Bottom of stack |
| 40 | + uintptr_t *top; ///< Top of stack, one word beyond stack! |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * Initializes the context with the function pointer and argument stored at the |
| 45 | + * top. The `ctx->sp` is set to two `uintptr_t` below the top. |
| 46 | + * Must be called only once to initialize the stack. |
| 47 | + * |
| 48 | + * @param fn function pointer to a `void(*)(uintptr_t)` function. |
| 49 | + */ |
| 50 | +void |
| 51 | +modm_context_init(modm_context_t *ctx, |
| 52 | + uintptr_t *bottom, uintptr_t *top, |
| 53 | + uintptr_t fn, uintptr_t arg); |
| 54 | + |
| 55 | +/** |
| 56 | + * Resets the context stack pointer to the correct offset in preparation for |
| 57 | + * jumping into the fiber. Must be called after initialization, but every time |
| 58 | + * the function is started from the beginning. |
| 59 | + */ |
| 60 | +void |
| 61 | +modm_context_reset(modm_context_t *ctx); |
| 62 | + |
| 63 | +/** |
| 64 | + * Switches control from the main context to the fiber context. This initializes |
| 65 | + * the hardware and then jumps from the caller context into the `to` fiber. |
| 66 | + */ |
| 67 | +void |
| 68 | +modm_context_start(modm_context_t *to); |
| 69 | + |
| 70 | +/** |
| 71 | + * Pushes the context onto the `from->sp` and pops the context from the `to->sp` |
| 72 | + * to jump from one fiber to the next. |
| 73 | + */ |
| 74 | +void |
| 75 | +modm_context_jump(modm_context_t *from, modm_context_t *to); |
| 76 | + |
| 77 | +/** |
| 78 | + * Switches control from the fiber context back to the main context. |
| 79 | + * Control flow then continues in the main context by returning from the |
| 80 | + * `modm_context_start()` function. |
| 81 | + */ |
| 82 | +void modm_noreturn |
| 83 | +modm_context_end(); |
| 84 | + |
| 85 | +/** |
| 86 | + * Zeros the register file and watermarks the rest of the stack. |
| 87 | + * You may call this function before or after `modm_context_reset()`, however, |
| 88 | + * *NOT* while the fiber is running! |
| 89 | + */ |
| 90 | +void |
| 91 | +modm_context_watermark(modm_context_t *ctx); |
| 92 | + |
| 93 | +/** |
| 94 | + * Returns the stack usage by searching from the bottom of the stack for the |
| 95 | + * watermark level. You may call this function at any point after calling |
| 96 | + * `modm_context_watermark()`. |
| 97 | + */ |
| 98 | +size_t |
| 99 | +modm_context_stack_usage(const modm_context_t *ctx); |
| 100 | + |
| 101 | +/// A cheap way to check if the last word of the stack was written. |
| 102 | +bool |
| 103 | +modm_context_stack_overflow(const modm_context_t *ctx); |
| 104 | +/// @} |
| 105 | + |
| 106 | +#ifdef __cplusplus |
| 107 | +} |
| 108 | +#endif |
0 commit comments