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