mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
cmsis/BUILD/mbed/drivers/SPI.h@189:f392fc9709a3, 2019-02-20 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Feb 20 22:31:08 2019 +0000
- Revision:
- 189:f392fc9709a3
mbed library release version 165
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 189:f392fc9709a3 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 189:f392fc9709a3 | 2 | * Copyright (c) 2006-2015 ARM Limited |
AnnaBridge | 189:f392fc9709a3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
AnnaBridge | 189:f392fc9709a3 | 4 | * |
AnnaBridge | 189:f392fc9709a3 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 189:f392fc9709a3 | 6 | * you may not use this file except in compliance with the License. |
AnnaBridge | 189:f392fc9709a3 | 7 | * You may obtain a copy of the License at |
AnnaBridge | 189:f392fc9709a3 | 8 | * |
AnnaBridge | 189:f392fc9709a3 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 189:f392fc9709a3 | 10 | * |
AnnaBridge | 189:f392fc9709a3 | 11 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 189:f392fc9709a3 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 189:f392fc9709a3 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 189:f392fc9709a3 | 14 | * See the License for the specific language governing permissions and |
AnnaBridge | 189:f392fc9709a3 | 15 | * limitations under the License. |
AnnaBridge | 189:f392fc9709a3 | 16 | */ |
AnnaBridge | 189:f392fc9709a3 | 17 | #ifndef MBED_SPI_H |
AnnaBridge | 189:f392fc9709a3 | 18 | #define MBED_SPI_H |
AnnaBridge | 189:f392fc9709a3 | 19 | |
AnnaBridge | 189:f392fc9709a3 | 20 | #include "platform/platform.h" |
AnnaBridge | 189:f392fc9709a3 | 21 | |
AnnaBridge | 189:f392fc9709a3 | 22 | #if DEVICE_SPI || defined(DOXYGEN_ONLY) |
AnnaBridge | 189:f392fc9709a3 | 23 | |
AnnaBridge | 189:f392fc9709a3 | 24 | #include "platform/PlatformMutex.h" |
AnnaBridge | 189:f392fc9709a3 | 25 | #include "hal/spi_api.h" |
AnnaBridge | 189:f392fc9709a3 | 26 | #include "platform/SingletonPtr.h" |
AnnaBridge | 189:f392fc9709a3 | 27 | #include "platform/NonCopyable.h" |
AnnaBridge | 189:f392fc9709a3 | 28 | |
AnnaBridge | 189:f392fc9709a3 | 29 | #if DEVICE_SPI_ASYNCH |
AnnaBridge | 189:f392fc9709a3 | 30 | #include "platform/CThunk.h" |
AnnaBridge | 189:f392fc9709a3 | 31 | #include "hal/dma_api.h" |
AnnaBridge | 189:f392fc9709a3 | 32 | #include "platform/CircularBuffer.h" |
AnnaBridge | 189:f392fc9709a3 | 33 | #include "platform/FunctionPointer.h" |
AnnaBridge | 189:f392fc9709a3 | 34 | #include "platform/Transaction.h" |
AnnaBridge | 189:f392fc9709a3 | 35 | #endif |
AnnaBridge | 189:f392fc9709a3 | 36 | |
AnnaBridge | 189:f392fc9709a3 | 37 | namespace mbed { |
AnnaBridge | 189:f392fc9709a3 | 38 | /** \addtogroup drivers */ |
AnnaBridge | 189:f392fc9709a3 | 39 | |
AnnaBridge | 189:f392fc9709a3 | 40 | /** A SPI Master, used for communicating with SPI slave devices. |
AnnaBridge | 189:f392fc9709a3 | 41 | * |
AnnaBridge | 189:f392fc9709a3 | 42 | * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz. |
AnnaBridge | 189:f392fc9709a3 | 43 | * |
AnnaBridge | 189:f392fc9709a3 | 44 | * Most SPI devices will also require Chip Select and Reset signals. These |
AnnaBridge | 189:f392fc9709a3 | 45 | * can be controlled using DigitalOut pins. |
AnnaBridge | 189:f392fc9709a3 | 46 | * |
AnnaBridge | 189:f392fc9709a3 | 47 | * @note Synchronization level: Thread safe |
AnnaBridge | 189:f392fc9709a3 | 48 | * |
AnnaBridge | 189:f392fc9709a3 | 49 | * Example of how to send a byte to a SPI slave and record the response: |
AnnaBridge | 189:f392fc9709a3 | 50 | * @code |
AnnaBridge | 189:f392fc9709a3 | 51 | * #include "mbed.h" |
AnnaBridge | 189:f392fc9709a3 | 52 | * |
AnnaBridge | 189:f392fc9709a3 | 53 | * SPI device(SPI_MOSI, SPI_MISO, SPI_SCLK) |
AnnaBridge | 189:f392fc9709a3 | 54 | * |
AnnaBridge | 189:f392fc9709a3 | 55 | * DigitalOut chip_select(SPI_CS); |
AnnaBridge | 189:f392fc9709a3 | 56 | * |
AnnaBridge | 189:f392fc9709a3 | 57 | * int main() { |
AnnaBridge | 189:f392fc9709a3 | 58 | * device.lock(); |
AnnaBridge | 189:f392fc9709a3 | 59 | * chip_select = 0; |
AnnaBridge | 189:f392fc9709a3 | 60 | * |
AnnaBridge | 189:f392fc9709a3 | 61 | * int response = device.write(0xFF); |
AnnaBridge | 189:f392fc9709a3 | 62 | * |
AnnaBridge | 189:f392fc9709a3 | 63 | * chip_select = 1; |
AnnaBridge | 189:f392fc9709a3 | 64 | * device.unlock(); |
AnnaBridge | 189:f392fc9709a3 | 65 | * } |
AnnaBridge | 189:f392fc9709a3 | 66 | * @endcode |
AnnaBridge | 189:f392fc9709a3 | 67 | * |
AnnaBridge | 189:f392fc9709a3 | 68 | * Example using hardware Chip Select line: |
AnnaBridge | 189:f392fc9709a3 | 69 | * @code |
AnnaBridge | 189:f392fc9709a3 | 70 | * #include "mbed.h" |
AnnaBridge | 189:f392fc9709a3 | 71 | * |
AnnaBridge | 189:f392fc9709a3 | 72 | * SPI device(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS) |
AnnaBridge | 189:f392fc9709a3 | 73 | * |
AnnaBridge | 189:f392fc9709a3 | 74 | * int main() { |
AnnaBridge | 189:f392fc9709a3 | 75 | * device.lock(); |
AnnaBridge | 189:f392fc9709a3 | 76 | * int response = device.write(0xFF); |
AnnaBridge | 189:f392fc9709a3 | 77 | * device.unlock(); |
AnnaBridge | 189:f392fc9709a3 | 78 | * } |
AnnaBridge | 189:f392fc9709a3 | 79 | * @endcode |
AnnaBridge | 189:f392fc9709a3 | 80 | * @ingroup drivers |
AnnaBridge | 189:f392fc9709a3 | 81 | */ |
AnnaBridge | 189:f392fc9709a3 | 82 | class SPI : private NonCopyable<SPI> { |
AnnaBridge | 189:f392fc9709a3 | 83 | |
AnnaBridge | 189:f392fc9709a3 | 84 | public: |
AnnaBridge | 189:f392fc9709a3 | 85 | |
AnnaBridge | 189:f392fc9709a3 | 86 | /** Create a SPI master connected to the specified pins. |
AnnaBridge | 189:f392fc9709a3 | 87 | * |
AnnaBridge | 189:f392fc9709a3 | 88 | * @note You can specify mosi or miso as NC if not used. |
AnnaBridge | 189:f392fc9709a3 | 89 | * |
AnnaBridge | 189:f392fc9709a3 | 90 | * @param mosi SPI Master Out, Slave In pin. |
AnnaBridge | 189:f392fc9709a3 | 91 | * @param miso SPI Master In, Slave Out pin. |
AnnaBridge | 189:f392fc9709a3 | 92 | * @param sclk SPI Clock pin. |
AnnaBridge | 189:f392fc9709a3 | 93 | * @param ssel SPI Chip Select pin. |
AnnaBridge | 189:f392fc9709a3 | 94 | */ |
AnnaBridge | 189:f392fc9709a3 | 95 | SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel = NC); |
AnnaBridge | 189:f392fc9709a3 | 96 | virtual ~SPI(); |
AnnaBridge | 189:f392fc9709a3 | 97 | |
AnnaBridge | 189:f392fc9709a3 | 98 | /** Configure the data transmission format. |
AnnaBridge | 189:f392fc9709a3 | 99 | * |
AnnaBridge | 189:f392fc9709a3 | 100 | * @param bits Number of bits per SPI frame (4 - 16). |
AnnaBridge | 189:f392fc9709a3 | 101 | * @param mode Clock polarity and phase mode (0 - 3). |
AnnaBridge | 189:f392fc9709a3 | 102 | * |
AnnaBridge | 189:f392fc9709a3 | 103 | * @code |
AnnaBridge | 189:f392fc9709a3 | 104 | * mode | POL PHA |
AnnaBridge | 189:f392fc9709a3 | 105 | * -----+-------- |
AnnaBridge | 189:f392fc9709a3 | 106 | * 0 | 0 0 |
AnnaBridge | 189:f392fc9709a3 | 107 | * 1 | 0 1 |
AnnaBridge | 189:f392fc9709a3 | 108 | * 2 | 1 0 |
AnnaBridge | 189:f392fc9709a3 | 109 | * 3 | 1 1 |
AnnaBridge | 189:f392fc9709a3 | 110 | * @endcode |
AnnaBridge | 189:f392fc9709a3 | 111 | */ |
AnnaBridge | 189:f392fc9709a3 | 112 | void format(int bits, int mode = 0); |
AnnaBridge | 189:f392fc9709a3 | 113 | |
AnnaBridge | 189:f392fc9709a3 | 114 | /** Set the SPI bus clock frequency. |
AnnaBridge | 189:f392fc9709a3 | 115 | * |
AnnaBridge | 189:f392fc9709a3 | 116 | * @param hz Clock frequency in Hz (default = 1MHz). |
AnnaBridge | 189:f392fc9709a3 | 117 | */ |
AnnaBridge | 189:f392fc9709a3 | 118 | void frequency(int hz = 1000000); |
AnnaBridge | 189:f392fc9709a3 | 119 | |
AnnaBridge | 189:f392fc9709a3 | 120 | /** Write to the SPI Slave and return the response. |
AnnaBridge | 189:f392fc9709a3 | 121 | * |
AnnaBridge | 189:f392fc9709a3 | 122 | * @param value Data to be sent to the SPI slave. |
AnnaBridge | 189:f392fc9709a3 | 123 | * |
AnnaBridge | 189:f392fc9709a3 | 124 | * @return Response from the SPI slave. |
AnnaBridge | 189:f392fc9709a3 | 125 | */ |
AnnaBridge | 189:f392fc9709a3 | 126 | virtual int write(int value); |
AnnaBridge | 189:f392fc9709a3 | 127 | |
AnnaBridge | 189:f392fc9709a3 | 128 | /** Write to the SPI Slave and obtain the response. |
AnnaBridge | 189:f392fc9709a3 | 129 | * |
AnnaBridge | 189:f392fc9709a3 | 130 | * The total number of bytes sent and received will be the maximum of |
AnnaBridge | 189:f392fc9709a3 | 131 | * tx_length and rx_length. The bytes written will be padded with the |
AnnaBridge | 189:f392fc9709a3 | 132 | * value 0xff. |
AnnaBridge | 189:f392fc9709a3 | 133 | * |
AnnaBridge | 189:f392fc9709a3 | 134 | * @param tx_buffer Pointer to the byte-array of data to write to the device. |
AnnaBridge | 189:f392fc9709a3 | 135 | * @param tx_length Number of bytes to write, may be zero. |
AnnaBridge | 189:f392fc9709a3 | 136 | * @param rx_buffer Pointer to the byte-array of data to read from the device. |
AnnaBridge | 189:f392fc9709a3 | 137 | * @param rx_length Number of bytes to read, may be zero. |
AnnaBridge | 189:f392fc9709a3 | 138 | * @return |
AnnaBridge | 189:f392fc9709a3 | 139 | * The number of bytes written and read from the device. This is |
AnnaBridge | 189:f392fc9709a3 | 140 | * maximum of tx_length and rx_length. |
AnnaBridge | 189:f392fc9709a3 | 141 | */ |
AnnaBridge | 189:f392fc9709a3 | 142 | virtual int write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length); |
AnnaBridge | 189:f392fc9709a3 | 143 | |
AnnaBridge | 189:f392fc9709a3 | 144 | /** Acquire exclusive access to this SPI bus. |
AnnaBridge | 189:f392fc9709a3 | 145 | */ |
AnnaBridge | 189:f392fc9709a3 | 146 | virtual void lock(void); |
AnnaBridge | 189:f392fc9709a3 | 147 | |
AnnaBridge | 189:f392fc9709a3 | 148 | /** Release exclusive access to this SPI bus. |
AnnaBridge | 189:f392fc9709a3 | 149 | */ |
AnnaBridge | 189:f392fc9709a3 | 150 | virtual void unlock(void); |
AnnaBridge | 189:f392fc9709a3 | 151 | |
AnnaBridge | 189:f392fc9709a3 | 152 | /** Set default write data. |
AnnaBridge | 189:f392fc9709a3 | 153 | * SPI requires the master to send some data during a read operation. |
AnnaBridge | 189:f392fc9709a3 | 154 | * Different devices may require different default byte values. |
AnnaBridge | 189:f392fc9709a3 | 155 | * For example: A SD Card requires default bytes to be 0xFF. |
AnnaBridge | 189:f392fc9709a3 | 156 | * |
AnnaBridge | 189:f392fc9709a3 | 157 | * @param data Default character to be transmitted during a read operation. |
AnnaBridge | 189:f392fc9709a3 | 158 | */ |
AnnaBridge | 189:f392fc9709a3 | 159 | void set_default_write_value(char data); |
AnnaBridge | 189:f392fc9709a3 | 160 | |
AnnaBridge | 189:f392fc9709a3 | 161 | #if DEVICE_SPI_ASYNCH |
AnnaBridge | 189:f392fc9709a3 | 162 | |
AnnaBridge | 189:f392fc9709a3 | 163 | /** Start non-blocking SPI transfer using 8bit buffers. |
AnnaBridge | 189:f392fc9709a3 | 164 | * |
AnnaBridge | 189:f392fc9709a3 | 165 | * This function locks the deep sleep until any event has occurred. |
AnnaBridge | 189:f392fc9709a3 | 166 | * |
AnnaBridge | 189:f392fc9709a3 | 167 | * @param tx_buffer The TX buffer with data to be transferred. If NULL is passed, |
AnnaBridge | 189:f392fc9709a3 | 168 | * the default SPI value is sent. |
AnnaBridge | 189:f392fc9709a3 | 169 | * @param tx_length The length of TX buffer in bytes. |
AnnaBridge | 189:f392fc9709a3 | 170 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
AnnaBridge | 189:f392fc9709a3 | 171 | * received data are ignored. |
AnnaBridge | 189:f392fc9709a3 | 172 | * @param rx_length The length of RX buffer in bytes. |
AnnaBridge | 189:f392fc9709a3 | 173 | * @param callback The event callback function. |
AnnaBridge | 189:f392fc9709a3 | 174 | * @param event The event mask of events to modify. @see spi_api.h for SPI events. |
AnnaBridge | 189:f392fc9709a3 | 175 | * |
AnnaBridge | 189:f392fc9709a3 | 176 | * @return Operation result. |
AnnaBridge | 189:f392fc9709a3 | 177 | * @retval 0 If the transfer has started. |
AnnaBridge | 189:f392fc9709a3 | 178 | * @retval -1 If SPI peripheral is busy. |
AnnaBridge | 189:f392fc9709a3 | 179 | */ |
AnnaBridge | 189:f392fc9709a3 | 180 | template<typename Type> |
AnnaBridge | 189:f392fc9709a3 | 181 | int transfer(const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t &callback, int event = SPI_EVENT_COMPLETE) |
AnnaBridge | 189:f392fc9709a3 | 182 | { |
AnnaBridge | 189:f392fc9709a3 | 183 | if (spi_active(&_spi)) { |
AnnaBridge | 189:f392fc9709a3 | 184 | return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type) * 8, callback, event); |
AnnaBridge | 189:f392fc9709a3 | 185 | } |
AnnaBridge | 189:f392fc9709a3 | 186 | start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type) * 8, callback, event); |
AnnaBridge | 189:f392fc9709a3 | 187 | return 0; |
AnnaBridge | 189:f392fc9709a3 | 188 | } |
AnnaBridge | 189:f392fc9709a3 | 189 | |
AnnaBridge | 189:f392fc9709a3 | 190 | /** Abort the on-going SPI transfer, and continue with transfers in the queue, if any. |
AnnaBridge | 189:f392fc9709a3 | 191 | */ |
AnnaBridge | 189:f392fc9709a3 | 192 | void abort_transfer(); |
AnnaBridge | 189:f392fc9709a3 | 193 | |
AnnaBridge | 189:f392fc9709a3 | 194 | /** Clear the queue of transfers. |
AnnaBridge | 189:f392fc9709a3 | 195 | */ |
AnnaBridge | 189:f392fc9709a3 | 196 | void clear_transfer_buffer(); |
AnnaBridge | 189:f392fc9709a3 | 197 | |
AnnaBridge | 189:f392fc9709a3 | 198 | /** Clear the queue of transfers and abort the on-going transfer. |
AnnaBridge | 189:f392fc9709a3 | 199 | */ |
AnnaBridge | 189:f392fc9709a3 | 200 | void abort_all_transfers(); |
AnnaBridge | 189:f392fc9709a3 | 201 | |
AnnaBridge | 189:f392fc9709a3 | 202 | /** Configure DMA usage suggestion for non-blocking transfers. |
AnnaBridge | 189:f392fc9709a3 | 203 | * |
AnnaBridge | 189:f392fc9709a3 | 204 | * @param usage The usage DMA hint for peripheral. |
AnnaBridge | 189:f392fc9709a3 | 205 | * |
AnnaBridge | 189:f392fc9709a3 | 206 | * @return Result of the operation. |
AnnaBridge | 189:f392fc9709a3 | 207 | * @retval 0 The usage was set. |
AnnaBridge | 189:f392fc9709a3 | 208 | * @retval -1 Usage cannot be set as there is an ongoing transaction. |
AnnaBridge | 189:f392fc9709a3 | 209 | */ |
AnnaBridge | 189:f392fc9709a3 | 210 | int set_dma_usage(DMAUsage usage); |
AnnaBridge | 189:f392fc9709a3 | 211 | |
AnnaBridge | 189:f392fc9709a3 | 212 | #if !defined(DOXYGEN_ONLY) |
AnnaBridge | 189:f392fc9709a3 | 213 | protected: |
AnnaBridge | 189:f392fc9709a3 | 214 | /** SPI interrupt handler. |
AnnaBridge | 189:f392fc9709a3 | 215 | */ |
AnnaBridge | 189:f392fc9709a3 | 216 | void irq_handler_asynch(void); |
AnnaBridge | 189:f392fc9709a3 | 217 | |
AnnaBridge | 189:f392fc9709a3 | 218 | /** Start the transfer or put it on the queue. |
AnnaBridge | 189:f392fc9709a3 | 219 | * |
AnnaBridge | 189:f392fc9709a3 | 220 | * @param tx_buffer The TX buffer with data to be transferred. If NULL is passed, |
AnnaBridge | 189:f392fc9709a3 | 221 | * the default SPI value is sent |
AnnaBridge | 189:f392fc9709a3 | 222 | * @param tx_length The length of TX buffer in bytes. |
AnnaBridge | 189:f392fc9709a3 | 223 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
AnnaBridge | 189:f392fc9709a3 | 224 | * received data are ignored. |
AnnaBridge | 189:f392fc9709a3 | 225 | * @param rx_length The length of RX buffer in bytes. |
AnnaBridge | 189:f392fc9709a3 | 226 | * @param bit_width The buffers element width in bits. |
AnnaBridge | 189:f392fc9709a3 | 227 | * @param callback The event callback function. |
AnnaBridge | 189:f392fc9709a3 | 228 | * @param event The event mask of events to modify. |
AnnaBridge | 189:f392fc9709a3 | 229 | * |
AnnaBridge | 189:f392fc9709a3 | 230 | * @return Operation success. |
AnnaBridge | 189:f392fc9709a3 | 231 | * @retval 0 A transfer was started or added to the queue. |
AnnaBridge | 189:f392fc9709a3 | 232 | * @retval -1 Transfer can't be added because queue is full. |
AnnaBridge | 189:f392fc9709a3 | 233 | */ |
AnnaBridge | 189:f392fc9709a3 | 234 | int transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event); |
AnnaBridge | 189:f392fc9709a3 | 235 | |
AnnaBridge | 189:f392fc9709a3 | 236 | /** Put a transfer on the transfer queue. |
AnnaBridge | 189:f392fc9709a3 | 237 | * |
AnnaBridge | 189:f392fc9709a3 | 238 | * @param tx_buffer The TX buffer with data to be transferred. If NULL is passed, |
AnnaBridge | 189:f392fc9709a3 | 239 | * the default SPI value is sent. |
AnnaBridge | 189:f392fc9709a3 | 240 | * @param tx_length The length of TX buffer in bytes. |
AnnaBridge | 189:f392fc9709a3 | 241 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
AnnaBridge | 189:f392fc9709a3 | 242 | * received data are ignored. |
AnnaBridge | 189:f392fc9709a3 | 243 | * @param rx_length The length of RX buffer in bytes. |
AnnaBridge | 189:f392fc9709a3 | 244 | * @param bit_width The buffers element width in bits. |
AnnaBridge | 189:f392fc9709a3 | 245 | * @param callback The event callback function. |
AnnaBridge | 189:f392fc9709a3 | 246 | * @param event The event mask of events to modify. |
AnnaBridge | 189:f392fc9709a3 | 247 | * |
AnnaBridge | 189:f392fc9709a3 | 248 | * @return Operation success. |
AnnaBridge | 189:f392fc9709a3 | 249 | * @retval 0 A transfer was added to the queue. |
AnnaBridge | 189:f392fc9709a3 | 250 | * @retval -1 Transfer can't be added because queue is full. |
AnnaBridge | 189:f392fc9709a3 | 251 | */ |
AnnaBridge | 189:f392fc9709a3 | 252 | int queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event); |
AnnaBridge | 189:f392fc9709a3 | 253 | |
AnnaBridge | 189:f392fc9709a3 | 254 | /** Configure a callback, SPI peripheral, and initiate a new transfer. |
AnnaBridge | 189:f392fc9709a3 | 255 | * |
AnnaBridge | 189:f392fc9709a3 | 256 | * @param tx_buffer The TX buffer with data to be transferred. If NULL is passed, |
AnnaBridge | 189:f392fc9709a3 | 257 | * the default SPI value is sent. |
AnnaBridge | 189:f392fc9709a3 | 258 | * @param tx_length The length of TX buffer in bytes. |
AnnaBridge | 189:f392fc9709a3 | 259 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
AnnaBridge | 189:f392fc9709a3 | 260 | * received data are ignored. |
AnnaBridge | 189:f392fc9709a3 | 261 | * @param rx_length The length of RX buffer in bytes. |
AnnaBridge | 189:f392fc9709a3 | 262 | * @param bit_width The buffers element width. |
AnnaBridge | 189:f392fc9709a3 | 263 | * @param callback The event callback function. |
AnnaBridge | 189:f392fc9709a3 | 264 | * @param event The event mask of events to modify. |
AnnaBridge | 189:f392fc9709a3 | 265 | */ |
AnnaBridge | 189:f392fc9709a3 | 266 | void start_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event); |
AnnaBridge | 189:f392fc9709a3 | 267 | |
AnnaBridge | 189:f392fc9709a3 | 268 | private: |
AnnaBridge | 189:f392fc9709a3 | 269 | /** Lock deep sleep only if it is not yet locked */ |
AnnaBridge | 189:f392fc9709a3 | 270 | void lock_deep_sleep(); |
AnnaBridge | 189:f392fc9709a3 | 271 | |
AnnaBridge | 189:f392fc9709a3 | 272 | /** Unlock deep sleep in case it is locked */ |
AnnaBridge | 189:f392fc9709a3 | 273 | void unlock_deep_sleep(); |
AnnaBridge | 189:f392fc9709a3 | 274 | |
AnnaBridge | 189:f392fc9709a3 | 275 | |
AnnaBridge | 189:f392fc9709a3 | 276 | #if TRANSACTION_QUEUE_SIZE_SPI |
AnnaBridge | 189:f392fc9709a3 | 277 | |
AnnaBridge | 189:f392fc9709a3 | 278 | /** Start a new transaction. |
AnnaBridge | 189:f392fc9709a3 | 279 | * |
AnnaBridge | 189:f392fc9709a3 | 280 | * @param data Transaction data. |
AnnaBridge | 189:f392fc9709a3 | 281 | */ |
AnnaBridge | 189:f392fc9709a3 | 282 | void start_transaction(transaction_t *data); |
AnnaBridge | 189:f392fc9709a3 | 283 | |
AnnaBridge | 189:f392fc9709a3 | 284 | /** Dequeue a transaction and start the transfer if there was one pending. |
AnnaBridge | 189:f392fc9709a3 | 285 | */ |
AnnaBridge | 189:f392fc9709a3 | 286 | void dequeue_transaction(); |
AnnaBridge | 189:f392fc9709a3 | 287 | |
AnnaBridge | 189:f392fc9709a3 | 288 | /* Queue of pending transfers */ |
AnnaBridge | 189:f392fc9709a3 | 289 | static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer; |
AnnaBridge | 189:f392fc9709a3 | 290 | #endif |
AnnaBridge | 189:f392fc9709a3 | 291 | |
AnnaBridge | 189:f392fc9709a3 | 292 | #endif //!defined(DOXYGEN_ONLY) |
AnnaBridge | 189:f392fc9709a3 | 293 | |
AnnaBridge | 189:f392fc9709a3 | 294 | #endif //DEVICE_SPI_ASYNCH |
AnnaBridge | 189:f392fc9709a3 | 295 | |
AnnaBridge | 189:f392fc9709a3 | 296 | #if !defined(DOXYGEN_ONLY) |
AnnaBridge | 189:f392fc9709a3 | 297 | protected: |
AnnaBridge | 189:f392fc9709a3 | 298 | /* Internal SPI object identifying the resources */ |
AnnaBridge | 189:f392fc9709a3 | 299 | spi_t _spi; |
AnnaBridge | 189:f392fc9709a3 | 300 | |
AnnaBridge | 189:f392fc9709a3 | 301 | #if DEVICE_SPI_ASYNCH |
AnnaBridge | 189:f392fc9709a3 | 302 | /* Interrupt */ |
AnnaBridge | 189:f392fc9709a3 | 303 | CThunk<SPI> _irq; |
AnnaBridge | 189:f392fc9709a3 | 304 | /* Interrupt handler callback */ |
AnnaBridge | 189:f392fc9709a3 | 305 | event_callback_t _callback; |
AnnaBridge | 189:f392fc9709a3 | 306 | /* Current preferred DMA mode @see dma_api.h */ |
AnnaBridge | 189:f392fc9709a3 | 307 | DMAUsage _usage; |
AnnaBridge | 189:f392fc9709a3 | 308 | /* Current sate of the sleep manager */ |
AnnaBridge | 189:f392fc9709a3 | 309 | bool _deep_sleep_locked; |
AnnaBridge | 189:f392fc9709a3 | 310 | #endif |
AnnaBridge | 189:f392fc9709a3 | 311 | |
AnnaBridge | 189:f392fc9709a3 | 312 | /* Take over the physical SPI and apply our settings (thread safe) */ |
AnnaBridge | 189:f392fc9709a3 | 313 | void aquire(void); |
AnnaBridge | 189:f392fc9709a3 | 314 | /* Current user of the SPI */ |
AnnaBridge | 189:f392fc9709a3 | 315 | static SPI *_owner; |
AnnaBridge | 189:f392fc9709a3 | 316 | /* Used by lock and unlock for thread safety */ |
AnnaBridge | 189:f392fc9709a3 | 317 | static SingletonPtr<PlatformMutex> _mutex; |
AnnaBridge | 189:f392fc9709a3 | 318 | /* Size of the SPI frame */ |
AnnaBridge | 189:f392fc9709a3 | 319 | int _bits; |
AnnaBridge | 189:f392fc9709a3 | 320 | /* Clock polairy and phase */ |
AnnaBridge | 189:f392fc9709a3 | 321 | int _mode; |
AnnaBridge | 189:f392fc9709a3 | 322 | /* Clock frequency */ |
AnnaBridge | 189:f392fc9709a3 | 323 | int _hz; |
AnnaBridge | 189:f392fc9709a3 | 324 | /* Default character used for NULL transfers */ |
AnnaBridge | 189:f392fc9709a3 | 325 | char _write_fill; |
AnnaBridge | 189:f392fc9709a3 | 326 | |
AnnaBridge | 189:f392fc9709a3 | 327 | private: |
AnnaBridge | 189:f392fc9709a3 | 328 | /** Private acquire function without locking/unlocking. |
AnnaBridge | 189:f392fc9709a3 | 329 | * Implemented in order to avoid duplicate locking and boost performance. |
AnnaBridge | 189:f392fc9709a3 | 330 | */ |
AnnaBridge | 189:f392fc9709a3 | 331 | void _acquire(void); |
AnnaBridge | 189:f392fc9709a3 | 332 | |
AnnaBridge | 189:f392fc9709a3 | 333 | #endif //!defined(DOXYGEN_ONLY) |
AnnaBridge | 189:f392fc9709a3 | 334 | }; |
AnnaBridge | 189:f392fc9709a3 | 335 | |
AnnaBridge | 189:f392fc9709a3 | 336 | } // namespace mbed |
AnnaBridge | 189:f392fc9709a3 | 337 | |
AnnaBridge | 189:f392fc9709a3 | 338 | #endif |
AnnaBridge | 189:f392fc9709a3 | 339 | |
AnnaBridge | 189:f392fc9709a3 | 340 | #endif |