mbed-os
Fork of mbed-os by
drivers/SPI.h@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2006-2015 ARM Limited |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 6 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 13 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 14 | * limitations under the License. |
elessair | 0:f269e3021894 | 15 | */ |
elessair | 0:f269e3021894 | 16 | #ifndef MBED_SPI_H |
elessair | 0:f269e3021894 | 17 | #define MBED_SPI_H |
elessair | 0:f269e3021894 | 18 | |
elessair | 0:f269e3021894 | 19 | #include "platform/platform.h" |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | #if DEVICE_SPI |
elessair | 0:f269e3021894 | 22 | |
elessair | 0:f269e3021894 | 23 | #include "platform/PlatformMutex.h" |
elessair | 0:f269e3021894 | 24 | #include "hal/spi_api.h" |
elessair | 0:f269e3021894 | 25 | #include "platform/SingletonPtr.h" |
elessair | 0:f269e3021894 | 26 | |
elessair | 0:f269e3021894 | 27 | #if DEVICE_SPI_ASYNCH |
elessair | 0:f269e3021894 | 28 | #include "platform/CThunk.h" |
elessair | 0:f269e3021894 | 29 | #include "hal/dma_api.h" |
elessair | 0:f269e3021894 | 30 | #include "platform/CircularBuffer.h" |
elessair | 0:f269e3021894 | 31 | #include "platform/FunctionPointer.h" |
elessair | 0:f269e3021894 | 32 | #include "platform/Transaction.h" |
elessair | 0:f269e3021894 | 33 | #endif |
elessair | 0:f269e3021894 | 34 | |
elessair | 0:f269e3021894 | 35 | namespace mbed { |
elessair | 0:f269e3021894 | 36 | /** \addtogroup drivers */ |
elessair | 0:f269e3021894 | 37 | /** @{*/ |
elessair | 0:f269e3021894 | 38 | |
elessair | 0:f269e3021894 | 39 | /** A SPI Master, used for communicating with SPI slave devices |
elessair | 0:f269e3021894 | 40 | * |
elessair | 0:f269e3021894 | 41 | * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz |
elessair | 0:f269e3021894 | 42 | * |
elessair | 0:f269e3021894 | 43 | * Most SPI devices will also require Chip Select and Reset signals. These |
elessair | 0:f269e3021894 | 44 | * can be controlled using <DigitalOut> pins |
elessair | 0:f269e3021894 | 45 | * |
elessair | 0:f269e3021894 | 46 | * @Note Synchronization level: Thread safe |
elessair | 0:f269e3021894 | 47 | * |
elessair | 0:f269e3021894 | 48 | * Example: |
elessair | 0:f269e3021894 | 49 | * @code |
elessair | 0:f269e3021894 | 50 | * // Send a byte to a SPI slave, and record the response |
elessair | 0:f269e3021894 | 51 | * |
elessair | 0:f269e3021894 | 52 | * #include "mbed.h" |
elessair | 0:f269e3021894 | 53 | * |
elessair | 0:f269e3021894 | 54 | * // hardware ssel (where applicable) |
elessair | 0:f269e3021894 | 55 | * //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel |
elessair | 0:f269e3021894 | 56 | * |
elessair | 0:f269e3021894 | 57 | * // software ssel |
elessair | 0:f269e3021894 | 58 | * SPI device(p5, p6, p7); // mosi, miso, sclk |
elessair | 0:f269e3021894 | 59 | * DigitalOut cs(p8); // ssel |
elessair | 0:f269e3021894 | 60 | * |
elessair | 0:f269e3021894 | 61 | * int main() { |
elessair | 0:f269e3021894 | 62 | * // hardware ssel (where applicable) |
elessair | 0:f269e3021894 | 63 | * //int response = device.write(0xFF); |
elessair | 0:f269e3021894 | 64 | * |
elessair | 0:f269e3021894 | 65 | * device.lock(); |
elessair | 0:f269e3021894 | 66 | * // software ssel |
elessair | 0:f269e3021894 | 67 | * cs = 0; |
elessair | 0:f269e3021894 | 68 | * int response = device.write(0xFF); |
elessair | 0:f269e3021894 | 69 | * cs = 1; |
elessair | 0:f269e3021894 | 70 | * device.unlock(); |
elessair | 0:f269e3021894 | 71 | * |
elessair | 0:f269e3021894 | 72 | * } |
elessair | 0:f269e3021894 | 73 | * @endcode |
elessair | 0:f269e3021894 | 74 | */ |
elessair | 0:f269e3021894 | 75 | class SPI { |
elessair | 0:f269e3021894 | 76 | |
elessair | 0:f269e3021894 | 77 | public: |
elessair | 0:f269e3021894 | 78 | |
elessair | 0:f269e3021894 | 79 | /** Create a SPI master connected to the specified pins |
elessair | 0:f269e3021894 | 80 | * |
elessair | 0:f269e3021894 | 81 | * mosi or miso can be specfied as NC if not used |
elessair | 0:f269e3021894 | 82 | * |
elessair | 0:f269e3021894 | 83 | * @param mosi SPI Master Out, Slave In pin |
elessair | 0:f269e3021894 | 84 | * @param miso SPI Master In, Slave Out pin |
elessair | 0:f269e3021894 | 85 | * @param sclk SPI Clock pin |
elessair | 0:f269e3021894 | 86 | * @param ssel SPI chip select pin |
elessair | 0:f269e3021894 | 87 | */ |
elessair | 0:f269e3021894 | 88 | SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC); |
elessair | 0:f269e3021894 | 89 | |
elessair | 0:f269e3021894 | 90 | /** Configure the data transmission format |
elessair | 0:f269e3021894 | 91 | * |
elessair | 0:f269e3021894 | 92 | * @param bits Number of bits per SPI frame (4 - 16) |
elessair | 0:f269e3021894 | 93 | * @param mode Clock polarity and phase mode (0 - 3) |
elessair | 0:f269e3021894 | 94 | * |
elessair | 0:f269e3021894 | 95 | * @code |
elessair | 0:f269e3021894 | 96 | * mode | POL PHA |
elessair | 0:f269e3021894 | 97 | * -----+-------- |
elessair | 0:f269e3021894 | 98 | * 0 | 0 0 |
elessair | 0:f269e3021894 | 99 | * 1 | 0 1 |
elessair | 0:f269e3021894 | 100 | * 2 | 1 0 |
elessair | 0:f269e3021894 | 101 | * 3 | 1 1 |
elessair | 0:f269e3021894 | 102 | * @endcode |
elessair | 0:f269e3021894 | 103 | */ |
elessair | 0:f269e3021894 | 104 | void format(int bits, int mode = 0); |
elessair | 0:f269e3021894 | 105 | |
elessair | 0:f269e3021894 | 106 | /** Set the spi bus clock frequency |
elessair | 0:f269e3021894 | 107 | * |
elessair | 0:f269e3021894 | 108 | * @param hz SCLK frequency in hz (default = 1MHz) |
elessair | 0:f269e3021894 | 109 | */ |
elessair | 0:f269e3021894 | 110 | void frequency(int hz = 1000000); |
elessair | 0:f269e3021894 | 111 | |
elessair | 0:f269e3021894 | 112 | /** Write to the SPI Slave and return the response |
elessair | 0:f269e3021894 | 113 | * |
elessair | 0:f269e3021894 | 114 | * @param value Data to be sent to the SPI slave |
elessair | 0:f269e3021894 | 115 | * |
elessair | 0:f269e3021894 | 116 | * @returns |
elessair | 0:f269e3021894 | 117 | * Response from the SPI slave |
elessair | 0:f269e3021894 | 118 | */ |
elessair | 0:f269e3021894 | 119 | virtual int write(int value); |
elessair | 0:f269e3021894 | 120 | |
elessair | 0:f269e3021894 | 121 | /** Acquire exclusive access to this SPI bus |
elessair | 0:f269e3021894 | 122 | */ |
elessair | 0:f269e3021894 | 123 | virtual void lock(void); |
elessair | 0:f269e3021894 | 124 | |
elessair | 0:f269e3021894 | 125 | /** Release exclusive access to this SPI bus |
elessair | 0:f269e3021894 | 126 | */ |
elessair | 0:f269e3021894 | 127 | virtual void unlock(void); |
elessair | 0:f269e3021894 | 128 | |
elessair | 0:f269e3021894 | 129 | #if DEVICE_SPI_ASYNCH |
elessair | 0:f269e3021894 | 130 | |
elessair | 0:f269e3021894 | 131 | /** Start non-blocking SPI transfer using 8bit buffers. |
elessair | 0:f269e3021894 | 132 | * |
elessair | 0:f269e3021894 | 133 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
elessair | 0:f269e3021894 | 134 | * the default SPI value is sent |
elessair | 0:f269e3021894 | 135 | * @param tx_length The length of TX buffer in bytes |
elessair | 0:f269e3021894 | 136 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
elessair | 0:f269e3021894 | 137 | * received data are ignored |
elessair | 0:f269e3021894 | 138 | * @param rx_length The length of RX buffer in bytes |
elessair | 0:f269e3021894 | 139 | * @param callback The event callback function |
elessair | 0:f269e3021894 | 140 | * @param event The logical OR of events to modify. Look at spi hal header file for SPI events. |
elessair | 0:f269e3021894 | 141 | * @return Zero if the transfer has started, or -1 if SPI peripheral is busy |
elessair | 0:f269e3021894 | 142 | */ |
elessair | 0:f269e3021894 | 143 | template<typename Type> |
elessair | 0:f269e3021894 | 144 | 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) { |
elessair | 0:f269e3021894 | 145 | if (spi_active(&_spi)) { |
elessair | 0:f269e3021894 | 146 | return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); |
elessair | 0:f269e3021894 | 147 | } |
elessair | 0:f269e3021894 | 148 | start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); |
elessair | 0:f269e3021894 | 149 | return 0; |
elessair | 0:f269e3021894 | 150 | } |
elessair | 0:f269e3021894 | 151 | |
elessair | 0:f269e3021894 | 152 | /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any. |
elessair | 0:f269e3021894 | 153 | */ |
elessair | 0:f269e3021894 | 154 | void abort_transfer(); |
elessair | 0:f269e3021894 | 155 | |
elessair | 0:f269e3021894 | 156 | /** Clear the transaction buffer |
elessair | 0:f269e3021894 | 157 | */ |
elessair | 0:f269e3021894 | 158 | void clear_transfer_buffer(); |
elessair | 0:f269e3021894 | 159 | |
elessair | 0:f269e3021894 | 160 | /** Clear the transaction buffer and abort on-going transfer. |
elessair | 0:f269e3021894 | 161 | */ |
elessair | 0:f269e3021894 | 162 | void abort_all_transfers(); |
elessair | 0:f269e3021894 | 163 | |
elessair | 0:f269e3021894 | 164 | /** Configure DMA usage suggestion for non-blocking transfers |
elessair | 0:f269e3021894 | 165 | * |
elessair | 0:f269e3021894 | 166 | * @param usage The usage DMA hint for peripheral |
elessair | 0:f269e3021894 | 167 | * @return Zero if the usage was set, -1 if a transaction is on-going |
elessair | 0:f269e3021894 | 168 | */ |
elessair | 0:f269e3021894 | 169 | int set_dma_usage(DMAUsage usage); |
elessair | 0:f269e3021894 | 170 | |
elessair | 0:f269e3021894 | 171 | protected: |
elessair | 0:f269e3021894 | 172 | /** SPI IRQ handler |
elessair | 0:f269e3021894 | 173 | * |
elessair | 0:f269e3021894 | 174 | */ |
elessair | 0:f269e3021894 | 175 | void irq_handler_asynch(void); |
elessair | 0:f269e3021894 | 176 | |
elessair | 0:f269e3021894 | 177 | /** Common transfer method |
elessair | 0:f269e3021894 | 178 | * |
elessair | 0:f269e3021894 | 179 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
elessair | 0:f269e3021894 | 180 | * the default SPI value is sent |
elessair | 0:f269e3021894 | 181 | * @param tx_length The length of TX buffer in bytes |
elessair | 0:f269e3021894 | 182 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
elessair | 0:f269e3021894 | 183 | * received data are ignored |
elessair | 0:f269e3021894 | 184 | * @param rx_length The length of RX buffer in bytes |
elessair | 0:f269e3021894 | 185 | * @param bit_width The buffers element width |
elessair | 0:f269e3021894 | 186 | * @param callback The event callback function |
elessair | 0:f269e3021894 | 187 | * @param event The logical OR of events to modify |
elessair | 0:f269e3021894 | 188 | * @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full |
elessair | 0:f269e3021894 | 189 | */ |
elessair | 0:f269e3021894 | 190 | 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); |
elessair | 0:f269e3021894 | 191 | |
elessair | 0:f269e3021894 | 192 | /** |
elessair | 0:f269e3021894 | 193 | * |
elessair | 0:f269e3021894 | 194 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
elessair | 0:f269e3021894 | 195 | * the default SPI value is sent |
elessair | 0:f269e3021894 | 196 | * @param tx_length The length of TX buffer in bytes |
elessair | 0:f269e3021894 | 197 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
elessair | 0:f269e3021894 | 198 | * received data are ignored |
elessair | 0:f269e3021894 | 199 | * @param rx_length The length of RX buffer in bytes |
elessair | 0:f269e3021894 | 200 | * @param bit_width The buffers element width |
elessair | 0:f269e3021894 | 201 | * @param callback The event callback function |
elessair | 0:f269e3021894 | 202 | * @param event The logical OR of events to modify |
elessair | 0:f269e3021894 | 203 | * @return Zero if a transfer was added to the queue, or -1 if the queue is full |
elessair | 0:f269e3021894 | 204 | */ |
elessair | 0:f269e3021894 | 205 | 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); |
elessair | 0:f269e3021894 | 206 | |
elessair | 0:f269e3021894 | 207 | /** Configures a callback, spi peripheral and initiate a new transfer |
elessair | 0:f269e3021894 | 208 | * |
elessair | 0:f269e3021894 | 209 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
elessair | 0:f269e3021894 | 210 | * the default SPI value is sent |
elessair | 0:f269e3021894 | 211 | * @param tx_length The length of TX buffer in bytes |
elessair | 0:f269e3021894 | 212 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
elessair | 0:f269e3021894 | 213 | * received data are ignored |
elessair | 0:f269e3021894 | 214 | * @param rx_length The length of RX buffer in bytes |
elessair | 0:f269e3021894 | 215 | * @param bit_width The buffers element width |
elessair | 0:f269e3021894 | 216 | * @param callback The event callback function |
elessair | 0:f269e3021894 | 217 | * @param event The logical OR of events to modify |
elessair | 0:f269e3021894 | 218 | */ |
elessair | 0:f269e3021894 | 219 | 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); |
elessair | 0:f269e3021894 | 220 | |
elessair | 0:f269e3021894 | 221 | #if TRANSACTION_QUEUE_SIZE_SPI |
elessair | 0:f269e3021894 | 222 | |
elessair | 0:f269e3021894 | 223 | /** Start a new transaction |
elessair | 0:f269e3021894 | 224 | * |
elessair | 0:f269e3021894 | 225 | * @param data Transaction data |
elessair | 0:f269e3021894 | 226 | */ |
elessair | 0:f269e3021894 | 227 | void start_transaction(transaction_t *data); |
elessair | 0:f269e3021894 | 228 | |
elessair | 0:f269e3021894 | 229 | /** Dequeue a transaction |
elessair | 0:f269e3021894 | 230 | * |
elessair | 0:f269e3021894 | 231 | */ |
elessair | 0:f269e3021894 | 232 | void dequeue_transaction(); |
elessair | 0:f269e3021894 | 233 | static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer; |
elessair | 0:f269e3021894 | 234 | #endif |
elessair | 0:f269e3021894 | 235 | |
elessair | 0:f269e3021894 | 236 | #endif |
elessair | 0:f269e3021894 | 237 | |
elessair | 0:f269e3021894 | 238 | public: |
elessair | 0:f269e3021894 | 239 | virtual ~SPI() { |
elessair | 0:f269e3021894 | 240 | } |
elessair | 0:f269e3021894 | 241 | |
elessair | 0:f269e3021894 | 242 | protected: |
elessair | 0:f269e3021894 | 243 | spi_t _spi; |
elessair | 0:f269e3021894 | 244 | |
elessair | 0:f269e3021894 | 245 | #if DEVICE_SPI_ASYNCH |
elessair | 0:f269e3021894 | 246 | CThunk<SPI> _irq; |
elessair | 0:f269e3021894 | 247 | event_callback_t _callback; |
elessair | 0:f269e3021894 | 248 | DMAUsage _usage; |
elessair | 0:f269e3021894 | 249 | #endif |
elessair | 0:f269e3021894 | 250 | |
elessair | 0:f269e3021894 | 251 | void aquire(void); |
elessair | 0:f269e3021894 | 252 | static SPI *_owner; |
elessair | 0:f269e3021894 | 253 | static SingletonPtr<PlatformMutex> _mutex; |
elessair | 0:f269e3021894 | 254 | int _bits; |
elessair | 0:f269e3021894 | 255 | int _mode; |
elessair | 0:f269e3021894 | 256 | int _hz; |
elessair | 0:f269e3021894 | 257 | }; |
elessair | 0:f269e3021894 | 258 | |
elessair | 0:f269e3021894 | 259 | } // namespace mbed |
elessair | 0:f269e3021894 | 260 | |
elessair | 0:f269e3021894 | 261 | #endif |
elessair | 0:f269e3021894 | 262 | |
elessair | 0:f269e3021894 | 263 | #endif |
elessair | 0:f269e3021894 | 264 | |
elessair | 0:f269e3021894 | 265 | /** @}*/ |