|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef _PICO_MULTICORE_H |
| 8 | +#define _PICO_MULTICORE_H |
| 9 | + |
| 10 | +#include "pico/types.h" |
| 11 | +#include "pico/sync.h" |
| 12 | + |
| 13 | +#ifdef __cplusplus |
| 14 | +extern "C" { |
| 15 | +#endif |
| 16 | + |
| 17 | +/** \file multicore.h |
| 18 | + * \defgroup pico_multicore pico_multicore |
| 19 | + * Adds support for running code on the second processor core (core1) |
| 20 | + * |
| 21 | + * \subsection multicore_example Example |
| 22 | + * \addtogroup pico_multicore |
| 23 | + * \include multicore.c |
| 24 | +*/ |
| 25 | + |
| 26 | +// PICO_CONFIG: PICO_CORE1_STACK_SIZE, Stack size for core 1, min=0x100, max=0x10000, default=PICO_STACK_SIZE/0x800, group=pico_multicore |
| 27 | +#ifndef PICO_CORE1_STACK_SIZE |
| 28 | +#ifdef PICO_STACK_SIZE |
| 29 | +#define PICO_CORE1_STACK_SIZE PICO_STACK_SIZE |
| 30 | +#else |
| 31 | +#define PICO_CORE1_STACK_SIZE 0x800 |
| 32 | +#endif |
| 33 | +#endif |
| 34 | + |
| 35 | +/*! \brief Reset Core 1 |
| 36 | + * \ingroup pico_multicore |
| 37 | + * |
| 38 | + */ |
| 39 | +void multicore_reset_core1(); |
| 40 | + |
| 41 | +/*! \brief Run code on core 1 |
| 42 | + * \ingroup pico_multicore |
| 43 | + * |
| 44 | + * Reset core1 and enter the given function on core 1 using the default core 1 stack (below core 0 stack) |
| 45 | + * |
| 46 | + * \param entry Function entry point, this function should not return. |
| 47 | + */ |
| 48 | +void multicore_launch_core1(void (*entry)(void)); |
| 49 | + |
| 50 | +/*! \brief Launch code on core 1 with stack |
| 51 | + * \ingroup pico_multicore |
| 52 | + * |
| 53 | + * Reset core1 and enter the given function on core 1 using the passed stack for core 1 |
| 54 | + */ |
| 55 | +void multicore_launch_core1_with_stack(void (*entry)(void), uint32_t *stack_bottom, size_t stack_size_bytes); |
| 56 | + |
| 57 | +/*! \brief Send core 1 to sleep. |
| 58 | + * \ingroup pico_multicore |
| 59 | + * |
| 60 | + */ |
| 61 | +void multicore_sleep_core1(); |
| 62 | + |
| 63 | +/*! \brief Launch code on core 1 with no stack protection |
| 64 | + * \ingroup pico_multicore |
| 65 | + * |
| 66 | + * Reset core1 and enter the given function using the passed sp as the initial stack pointer. |
| 67 | + * This is a bare bones functions that does not provide a stack guard even if USE_STACK_GUARDS is defined |
| 68 | + * |
| 69 | + */ |
| 70 | +void multicore_launch_core1_raw(void (*entry)(void), uint32_t *sp, uint32_t vector_table); |
| 71 | + |
| 72 | +/*! |
| 73 | + * \defgroup multicore_fifo fifo |
| 74 | + * \ingroup pico_multicore |
| 75 | + * \brief Functions for inter-core FIFO |
| 76 | + * |
| 77 | + * The RP2040 contains two FIFOs for passing data, messages or ordered events between the two cores. Each FIFO is 32 bits |
| 78 | + * wide, and 8 entries deep. One of the FIFOs can only be written by core 0, and read by core 1. The other can only be written |
| 79 | + * by core 1, and read by core 0. |
| 80 | + */ |
| 81 | + |
| 82 | + |
| 83 | +/*! \brief Check the read FIFO to see if there is data waiting |
| 84 | + * \ingroup multicore_fifo |
| 85 | + * |
| 86 | + * \return true if the FIFO has data in it, false otherwise |
| 87 | + */ |
| 88 | +static inline bool multicore_fifo_rvalid() { |
| 89 | + return !!(sio_hw->fifo_st & SIO_FIFO_ST_VLD_BITS); |
| 90 | +} |
| 91 | + |
| 92 | +/*! \brief Check the FIFO to see if the write FIFO is full |
| 93 | + * \ingroup multicore_fifo |
| 94 | + * |
| 95 | + * @return true if the FIFO is full, false otherwise |
| 96 | + */ |
| 97 | +static inline bool multicore_fifo_wready() { |
| 98 | + return !!(sio_hw->fifo_st & SIO_FIFO_ST_RDY_BITS); |
| 99 | +} |
| 100 | + |
| 101 | +/*! \brief Push data on to the FIFO. |
| 102 | + * \ingroup multicore_fifo |
| 103 | + * |
| 104 | + * This function will block until there is space for the data to be sent. |
| 105 | + * Use multicore_fifo_wready() to check if it is possible to write to the |
| 106 | + * FIFO if you don't want to block. |
| 107 | + * |
| 108 | + * \param data A 32 bit value to push on to the FIFO |
| 109 | + */ |
| 110 | +void multicore_fifo_push_blocking(uint32_t data); |
| 111 | + |
| 112 | +bool multicore_fifo_push_timeout_us(uint32_t data, uint64_t timeout_us); |
| 113 | + |
| 114 | +/*! \brief Pop data from the FIFO. |
| 115 | + * \ingroup multicore_fifo |
| 116 | + * |
| 117 | + * This function will block until there is data ready to be read |
| 118 | + * Use multicore_fifo_rvalid() to check if data is ready to be read if you don't |
| 119 | + * want to block. |
| 120 | + * |
| 121 | + * \return 32 bit unsigned data from the FIFO. |
| 122 | + */ |
| 123 | +uint32_t multicore_fifo_pop_blocking(); |
| 124 | + |
| 125 | +bool multicore_fifo_pop_timeout_us(uint64_t timeout_us, uint32_t *out); |
| 126 | + |
| 127 | +/*! \brief Flush any data in the outgoing FIFO |
| 128 | + * \ingroup multicore_fifo |
| 129 | + * |
| 130 | + */ |
| 131 | +static inline void multicore_fifo_drain() { |
| 132 | + while (multicore_fifo_rvalid()) |
| 133 | + (void) sio_hw->fifo_rd; |
| 134 | +} |
| 135 | + |
| 136 | +/*! \brief Clear FIFO interrupt |
| 137 | + * \ingroup multicore_fifo |
| 138 | +*/ |
| 139 | +static inline void multicore_fifo_clear_irq() { |
| 140 | + // Write any value to clear any interrupts |
| 141 | + sio_hw->fifo_st = 0xff; |
| 142 | +} |
| 143 | + |
| 144 | +/*! \brief Get FIFO status |
| 145 | + * \ingroup multicore_fifo |
| 146 | + * |
| 147 | + * \return The status as a bitfield |
| 148 | + * |
| 149 | + * Bit | Description |
| 150 | + * ----|------------ |
| 151 | + * 3 | Sticky flag indicating the RX FIFO was read when empty. This read was ignored by the FIFO. |
| 152 | + * 2 | Sticky flag indicating the TX FIFO was written when full. This write was ignored by the FIFO. |
| 153 | + * 1 | Value is 1 if this core’s TX FIFO is not full (i.e. if FIFO_WR is ready for more data) |
| 154 | + * 0 | Value is 1 if this core’s RX FIFO is not empty (i.e. if FIFO_RD is valid) |
| 155 | +*/ |
| 156 | +static inline int32_t multicore_fifo_get_status() { |
| 157 | + return sio_hw->fifo_st; |
| 158 | +} |
| 159 | + |
| 160 | +// call this from the lockout victim thread |
| 161 | +void multicore_lockout_victim_init(); |
| 162 | + |
| 163 | +// start locking out the other core (it will be |
| 164 | +bool multicore_lockout_start_timeout_us(uint64_t timeout_us); |
| 165 | +void multicore_lockout_start_blocking(); |
| 166 | + |
| 167 | +bool multicore_lockout_end_timeout_us(uint64_t timeout_us); |
| 168 | +void multicore_lockout_end_blocking(); |
| 169 | + |
| 170 | +#ifdef __cplusplus |
| 171 | +} |
| 172 | +#endif |
| 173 | +#endif |
0 commit comments