|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2012, Fabian Greif |
| 3 | + * Copyright (c) 2010, Martin Rosekeit |
| 4 | + * Copyright (c) 2012-2017, Niklas Hauser |
| 5 | + * Copyright (c) 2013, Sascha Schade |
| 6 | + * Copyright (c) 2021, Marton Lednczki |
| 7 | + * |
| 8 | + * This file is part of the modm project. |
| 9 | + * |
| 10 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 11 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 12 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 13 | + */ |
| 14 | +// ---------------------------------------------------------------------------- |
| 15 | + |
| 16 | +#ifndef MODM_INTERFACE_I2S_MASTER_HPP |
| 17 | +#define MODM_INTERFACE_I2S_MASTER_HPP |
| 18 | + |
| 19 | +#include <modm/processing/resumable.hpp> |
| 20 | +#include "i2s.hpp" |
| 21 | + |
| 22 | +namespace modm |
| 23 | +{ |
| 24 | + |
| 25 | +/** |
| 26 | + * Interface for a I2S Master |
| 27 | + * |
| 28 | + * @author Marton Ledneczki |
| 29 | + * @ingroup modm_architecture_i2s |
| 30 | + */ |
| 31 | +class I2sMaster : public ::modm::PeripheralDriver, public I2s |
| 32 | +{ |
| 33 | +#ifdef __DOXYGEN__ |
| 34 | +public: |
| 35 | + /** |
| 36 | + * Connect GPIOs to the peripheral and configure them. |
| 37 | + * |
| 38 | + * This configures the CK (serial clock), MCK (master clock), |
| 39 | + * SD (serial data) and WS (word select) signals and connects them. |
| 40 | + * |
| 41 | + * @tparam Signals |
| 42 | + * One CK, SD and WS signal are required |
| 43 | + * and can be passed out-of-order. |
| 44 | + */ |
| 45 | + template< class... Signals > |
| 46 | + static void |
| 47 | + connect(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Initializes the hardware and sets the baudrate. |
| 51 | + * |
| 52 | + * @tparam SystemClock |
| 53 | + * the currently active system clock |
| 54 | + * @tparam baudrate |
| 55 | + * the desired baudrate in Hz |
| 56 | + * @tparam tolerance |
| 57 | + * the allowed relative tolerance for the resulting baudrate |
| 58 | + */ |
| 59 | + template< class SystemClock, baudrate_t baudrate, percent_t tolerance=5_pct > |
| 60 | + static void |
| 61 | + initialize(); |
| 62 | + |
| 63 | + /// Sets a new data mode. |
| 64 | + static void |
| 65 | + setDataMode(DataMode mode); |
| 66 | + |
| 67 | + /// Sets a new data order. |
| 68 | + static void |
| 69 | + setDataOrder(DataOrder order); |
| 70 | + |
| 71 | + /** |
| 72 | + * Request access to the spi master within a context. |
| 73 | + * You may acquire the spi master multiple times within the same context. |
| 74 | + * |
| 75 | + * The configuration handler will only be called when aquiring the spi |
| 76 | + * master for the first time (if it is not a `nullptr`). |
| 77 | + * |
| 78 | + * @warning Aquires must be balanced with releases of the **same** context! |
| 79 | + * @warning Aquires are persistent even after calling `initialize()`! |
| 80 | + * |
| 81 | + * @return `0` if another context is using the spi master, otherwise |
| 82 | + * `>0` as the number of times this context acquired the master. |
| 83 | + */ |
| 84 | + static uint8_t |
| 85 | + acquire(void *ctx, ConfigurationHandler handler = nullptr); |
| 86 | + |
| 87 | + /** |
| 88 | + * Release access to the spi master within a context. |
| 89 | + * |
| 90 | + * @warning Releases must be balanced with acquires of the **same** context! |
| 91 | + * @warning Releases are persistent even after calling `initialize()`! |
| 92 | + * |
| 93 | + * @return `0` if nothing can be released anymore (for any context) |
| 94 | + * `>0` as the number of times this context can still release the master. |
| 95 | + */ |
| 96 | + static uint8_t |
| 97 | + release(void *ctx); |
| 98 | + |
| 99 | + /** |
| 100 | + * Swap a single byte and wait for completion. |
| 101 | + * |
| 102 | + * @param data |
| 103 | + * data to be sent |
| 104 | + * @return received data |
| 105 | + */ |
| 106 | + static uint8_t |
| 107 | + transferBlocking(uint8_t data); |
| 108 | + |
| 109 | + /** |
| 110 | + * Set the data buffers and length with options and starts a transfer. |
| 111 | + * This may be hardware accelerated (DMA or Interrupt), but not guaranteed. |
| 112 | + * |
| 113 | + * @param[in] tx |
| 114 | + * pointer to transmit buffer, set to `nullptr` to send dummy bytes |
| 115 | + * @param[out] rx |
| 116 | + * pointer to receive buffer, set to `nullptr` to discard received bytes |
| 117 | + * @param length |
| 118 | + * number of bytes to be shifted out |
| 119 | + */ |
| 120 | + static void |
| 121 | + transferBlocking(const uint8_t *tx, uint8_t *rx, std::size_t length); |
| 122 | + |
| 123 | + /** |
| 124 | + * Swap a single byte and wait for completion non-blocking!. |
| 125 | + * |
| 126 | + * You must call this inside a Protothread or Resumable |
| 127 | + * using `PT_CALL` or `RF_CALL` respectively. |
| 128 | + * @warning These methods differ from Resumables by lacking context protection! |
| 129 | + * You must ensure that only one driver is accessing this resumable function |
| 130 | + * by using `acquire(ctx)` and `release(ctx)`. |
| 131 | + * |
| 132 | + * @param data |
| 133 | + * data to be sent |
| 134 | + * @return received data |
| 135 | + */ |
| 136 | + static modm::ResumableResult<uint8_t> |
| 137 | + transfer(uint8_t data); |
| 138 | + |
| 139 | + /** |
| 140 | + * Set the data buffers and length with options and |
| 141 | + * starts a non-blocking transfer. |
| 142 | + * This may be hardware accelerated (DMA or Interrupt), but not guaranteed. |
| 143 | + * |
| 144 | + * You must call this inside a Protothread or Resumable |
| 145 | + * using `PT_CALL` or `RF_CALL` respectively. |
| 146 | + * @warning These methods differ from Resumables by lacking context protection! |
| 147 | + * You must ensure that only one driver is accessing this resumable function |
| 148 | + * by using `acquire(ctx)` and `release(ctx)`. |
| 149 | + * |
| 150 | + * @param[in] tx |
| 151 | + * pointer to transmit buffer, set to `nullptr` to send dummy bytes |
| 152 | + * @param[out] rx |
| 153 | + * pointer to receive buffer, set to `nullptr` to discard received bytes |
| 154 | + * @param length |
| 155 | + * number of bytes to be shifted out |
| 156 | + */ |
| 157 | + static modm::ResumableResult<void> |
| 158 | + transfer(const uint8_t *tx, uint8_t *rx, std::size_t length); |
| 159 | +#endif |
| 160 | +}; |
| 161 | + |
| 162 | +} // namespace modm |
| 163 | + |
| 164 | +#endif // MODM_INTERFACE_SPI_MASTER_HPP |
0 commit comments