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