mbed library sources for TON Board V1.2
Fork of mbed-dev by
drivers/SPI.h@168:1dc8c8bdd03d, 2017-06-29 (annotated)
- Committer:
- krebyy
- Date:
- Thu Jun 29 20:34:04 2017 +0000
- Revision:
- 168:1dc8c8bdd03d
- Parent:
- 167:e84263d55307
First commit - mbed for TON Board V1.2
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" |
<> | 149:156823d33999 | 26 | |
<> | 149:156823d33999 | 27 | #if DEVICE_SPI_ASYNCH |
<> | 149:156823d33999 | 28 | #include "platform/CThunk.h" |
<> | 149:156823d33999 | 29 | #include "hal/dma_api.h" |
<> | 149:156823d33999 | 30 | #include "platform/CircularBuffer.h" |
<> | 149:156823d33999 | 31 | #include "platform/FunctionPointer.h" |
<> | 149:156823d33999 | 32 | #include "platform/Transaction.h" |
<> | 149:156823d33999 | 33 | #endif |
<> | 149:156823d33999 | 34 | |
<> | 149:156823d33999 | 35 | namespace mbed { |
<> | 149:156823d33999 | 36 | /** \addtogroup drivers */ |
<> | 149:156823d33999 | 37 | |
<> | 149:156823d33999 | 38 | /** A SPI Master, used for communicating with SPI slave devices |
<> | 149:156823d33999 | 39 | * |
<> | 149:156823d33999 | 40 | * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz |
<> | 149:156823d33999 | 41 | * |
<> | 149:156823d33999 | 42 | * Most SPI devices will also require Chip Select and Reset signals. These |
AnnaBridge | 167:e84263d55307 | 43 | * can be controlled using DigitalOut pins |
<> | 149:156823d33999 | 44 | * |
AnnaBridge | 167:e84263d55307 | 45 | * @note Synchronization level: Thread safe |
<> | 149:156823d33999 | 46 | * |
<> | 149:156823d33999 | 47 | * Example: |
<> | 149:156823d33999 | 48 | * @code |
<> | 149:156823d33999 | 49 | * // Send a byte to a SPI slave, and record the response |
<> | 149:156823d33999 | 50 | * |
<> | 149:156823d33999 | 51 | * #include "mbed.h" |
<> | 149:156823d33999 | 52 | * |
<> | 149:156823d33999 | 53 | * // hardware ssel (where applicable) |
<> | 149:156823d33999 | 54 | * //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel |
<> | 149:156823d33999 | 55 | * |
<> | 149:156823d33999 | 56 | * // software ssel |
<> | 149:156823d33999 | 57 | * SPI device(p5, p6, p7); // mosi, miso, sclk |
<> | 149:156823d33999 | 58 | * DigitalOut cs(p8); // ssel |
<> | 149:156823d33999 | 59 | * |
<> | 149:156823d33999 | 60 | * int main() { |
<> | 149:156823d33999 | 61 | * // hardware ssel (where applicable) |
<> | 149:156823d33999 | 62 | * //int response = device.write(0xFF); |
<> | 149:156823d33999 | 63 | * |
<> | 149:156823d33999 | 64 | * device.lock(); |
<> | 149:156823d33999 | 65 | * // software ssel |
<> | 149:156823d33999 | 66 | * cs = 0; |
<> | 149:156823d33999 | 67 | * int response = device.write(0xFF); |
<> | 149:156823d33999 | 68 | * cs = 1; |
<> | 149:156823d33999 | 69 | * device.unlock(); |
<> | 149:156823d33999 | 70 | * |
<> | 149:156823d33999 | 71 | * } |
<> | 149:156823d33999 | 72 | * @endcode |
AnnaBridge | 167:e84263d55307 | 73 | * @ingroup drivers |
<> | 149:156823d33999 | 74 | */ |
<> | 149:156823d33999 | 75 | class SPI { |
<> | 149:156823d33999 | 76 | |
<> | 149:156823d33999 | 77 | public: |
<> | 149:156823d33999 | 78 | |
<> | 149:156823d33999 | 79 | /** Create a SPI master connected to the specified pins |
<> | 149:156823d33999 | 80 | * |
<> | 149:156823d33999 | 81 | * mosi or miso can be specfied as NC if not used |
<> | 149:156823d33999 | 82 | * |
<> | 149:156823d33999 | 83 | * @param mosi SPI Master Out, Slave In pin |
<> | 149:156823d33999 | 84 | * @param miso SPI Master In, Slave Out pin |
<> | 149:156823d33999 | 85 | * @param sclk SPI Clock pin |
<> | 149:156823d33999 | 86 | * @param ssel SPI chip select pin |
<> | 149:156823d33999 | 87 | */ |
<> | 149:156823d33999 | 88 | SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC); |
<> | 149:156823d33999 | 89 | |
<> | 149:156823d33999 | 90 | /** Configure the data transmission format |
<> | 149:156823d33999 | 91 | * |
<> | 149:156823d33999 | 92 | * @param bits Number of bits per SPI frame (4 - 16) |
<> | 149:156823d33999 | 93 | * @param mode Clock polarity and phase mode (0 - 3) |
<> | 149:156823d33999 | 94 | * |
<> | 149:156823d33999 | 95 | * @code |
<> | 149:156823d33999 | 96 | * mode | POL PHA |
<> | 149:156823d33999 | 97 | * -----+-------- |
<> | 149:156823d33999 | 98 | * 0 | 0 0 |
<> | 149:156823d33999 | 99 | * 1 | 0 1 |
<> | 149:156823d33999 | 100 | * 2 | 1 0 |
<> | 149:156823d33999 | 101 | * 3 | 1 1 |
<> | 149:156823d33999 | 102 | * @endcode |
<> | 149:156823d33999 | 103 | */ |
<> | 149:156823d33999 | 104 | void format(int bits, int mode = 0); |
<> | 149:156823d33999 | 105 | |
<> | 149:156823d33999 | 106 | /** Set the spi bus clock frequency |
<> | 149:156823d33999 | 107 | * |
<> | 149:156823d33999 | 108 | * @param hz SCLK frequency in hz (default = 1MHz) |
<> | 149:156823d33999 | 109 | */ |
<> | 149:156823d33999 | 110 | void frequency(int hz = 1000000); |
<> | 149:156823d33999 | 111 | |
<> | 149:156823d33999 | 112 | /** Write to the SPI Slave and return the response |
<> | 149:156823d33999 | 113 | * |
<> | 149:156823d33999 | 114 | * @param value Data to be sent to the SPI slave |
<> | 149:156823d33999 | 115 | * |
<> | 149:156823d33999 | 116 | * @returns |
<> | 149:156823d33999 | 117 | * Response from the SPI slave |
AnnaBridge | 167:e84263d55307 | 118 | */ |
<> | 149:156823d33999 | 119 | virtual int write(int value); |
<> | 149:156823d33999 | 120 | |
AnnaBridge | 167:e84263d55307 | 121 | /** Write to the SPI Slave and obtain the response |
AnnaBridge | 167:e84263d55307 | 122 | * |
AnnaBridge | 167:e84263d55307 | 123 | * The total number of bytes sent and recieved will be the maximum of |
AnnaBridge | 167:e84263d55307 | 124 | * tx_length and rx_length. The bytes written will be padded with the |
AnnaBridge | 167:e84263d55307 | 125 | * value 0xff. |
AnnaBridge | 167:e84263d55307 | 126 | * |
AnnaBridge | 167:e84263d55307 | 127 | * @param tx_buffer Pointer to the byte-array of data to write to the device |
AnnaBridge | 167:e84263d55307 | 128 | * @param tx_length Number of bytes to write, may be zero |
AnnaBridge | 167:e84263d55307 | 129 | * @param rx_buffer Pointer to the byte-array of data to read from the device |
AnnaBridge | 167:e84263d55307 | 130 | * @param rx_length Number of bytes to read, may be zero |
AnnaBridge | 167:e84263d55307 | 131 | * @returns |
AnnaBridge | 167:e84263d55307 | 132 | * The number of bytes written and read from the device. This is |
AnnaBridge | 167:e84263d55307 | 133 | * maximum of tx_length and rx_length. |
AnnaBridge | 167:e84263d55307 | 134 | */ |
AnnaBridge | 167:e84263d55307 | 135 | virtual int write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length); |
AnnaBridge | 167:e84263d55307 | 136 | |
<> | 149:156823d33999 | 137 | /** Acquire exclusive access to this SPI bus |
<> | 149:156823d33999 | 138 | */ |
<> | 149:156823d33999 | 139 | virtual void lock(void); |
<> | 149:156823d33999 | 140 | |
<> | 149:156823d33999 | 141 | /** Release exclusive access to this SPI bus |
<> | 149:156823d33999 | 142 | */ |
<> | 149:156823d33999 | 143 | virtual void unlock(void); |
<> | 149:156823d33999 | 144 | |
<> | 149:156823d33999 | 145 | #if DEVICE_SPI_ASYNCH |
<> | 149:156823d33999 | 146 | |
<> | 149:156823d33999 | 147 | /** Start non-blocking SPI transfer using 8bit buffers. |
<> | 149:156823d33999 | 148 | * |
<> | 149:156823d33999 | 149 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
<> | 149:156823d33999 | 150 | * the default SPI value is sent |
<> | 149:156823d33999 | 151 | * @param tx_length The length of TX buffer in bytes |
<> | 149:156823d33999 | 152 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
<> | 149:156823d33999 | 153 | * received data are ignored |
<> | 149:156823d33999 | 154 | * @param rx_length The length of RX buffer in bytes |
<> | 149:156823d33999 | 155 | * @param callback The event callback function |
<> | 149:156823d33999 | 156 | * @param event The logical OR of events to modify. Look at spi hal header file for SPI events. |
<> | 149:156823d33999 | 157 | * @return Zero if the transfer has started, or -1 if SPI peripheral is busy |
<> | 149:156823d33999 | 158 | */ |
<> | 149:156823d33999 | 159 | template<typename Type> |
<> | 149:156823d33999 | 160 | 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) { |
<> | 149:156823d33999 | 161 | if (spi_active(&_spi)) { |
<> | 149:156823d33999 | 162 | return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); |
<> | 149:156823d33999 | 163 | } |
<> | 149:156823d33999 | 164 | start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); |
<> | 149:156823d33999 | 165 | return 0; |
<> | 149:156823d33999 | 166 | } |
<> | 149:156823d33999 | 167 | |
<> | 149:156823d33999 | 168 | /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any. |
<> | 149:156823d33999 | 169 | */ |
<> | 149:156823d33999 | 170 | void abort_transfer(); |
<> | 149:156823d33999 | 171 | |
<> | 149:156823d33999 | 172 | /** Clear the transaction buffer |
<> | 149:156823d33999 | 173 | */ |
<> | 149:156823d33999 | 174 | void clear_transfer_buffer(); |
<> | 149:156823d33999 | 175 | |
<> | 149:156823d33999 | 176 | /** Clear the transaction buffer and abort on-going transfer. |
<> | 149:156823d33999 | 177 | */ |
<> | 149:156823d33999 | 178 | void abort_all_transfers(); |
<> | 149:156823d33999 | 179 | |
<> | 149:156823d33999 | 180 | /** Configure DMA usage suggestion for non-blocking transfers |
<> | 149:156823d33999 | 181 | * |
<> | 149:156823d33999 | 182 | * @param usage The usage DMA hint for peripheral |
<> | 149:156823d33999 | 183 | * @return Zero if the usage was set, -1 if a transaction is on-going |
<> | 149:156823d33999 | 184 | */ |
<> | 149:156823d33999 | 185 | int set_dma_usage(DMAUsage usage); |
<> | 149:156823d33999 | 186 | |
<> | 149:156823d33999 | 187 | protected: |
<> | 149:156823d33999 | 188 | /** SPI IRQ handler |
<> | 149:156823d33999 | 189 | * |
<> | 149:156823d33999 | 190 | */ |
<> | 149:156823d33999 | 191 | void irq_handler_asynch(void); |
<> | 149:156823d33999 | 192 | |
<> | 149:156823d33999 | 193 | /** Common transfer method |
<> | 149:156823d33999 | 194 | * |
<> | 149:156823d33999 | 195 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
<> | 149:156823d33999 | 196 | * the default SPI value is sent |
<> | 149:156823d33999 | 197 | * @param tx_length The length of TX buffer in bytes |
<> | 149:156823d33999 | 198 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
<> | 149:156823d33999 | 199 | * received data are ignored |
<> | 149:156823d33999 | 200 | * @param rx_length The length of RX buffer in bytes |
<> | 149:156823d33999 | 201 | * @param bit_width The buffers element width |
<> | 149:156823d33999 | 202 | * @param callback The event callback function |
<> | 149:156823d33999 | 203 | * @param event The logical OR of events to modify |
<> | 149:156823d33999 | 204 | * @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full |
<> | 149:156823d33999 | 205 | */ |
<> | 149:156823d33999 | 206 | 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 | 207 | |
<> | 149:156823d33999 | 208 | /** |
<> | 149:156823d33999 | 209 | * |
<> | 149:156823d33999 | 210 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
<> | 149:156823d33999 | 211 | * the default SPI value is sent |
<> | 149:156823d33999 | 212 | * @param tx_length The length of TX buffer in bytes |
<> | 149:156823d33999 | 213 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
<> | 149:156823d33999 | 214 | * received data are ignored |
<> | 149:156823d33999 | 215 | * @param rx_length The length of RX buffer in bytes |
<> | 149:156823d33999 | 216 | * @param bit_width The buffers element width |
<> | 149:156823d33999 | 217 | * @param callback The event callback function |
<> | 149:156823d33999 | 218 | * @param event The logical OR of events to modify |
<> | 149:156823d33999 | 219 | * @return Zero if a transfer was added to the queue, or -1 if the queue is full |
<> | 149:156823d33999 | 220 | */ |
<> | 149:156823d33999 | 221 | 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 | 222 | |
<> | 149:156823d33999 | 223 | /** Configures a callback, spi peripheral and initiate a new transfer |
<> | 149:156823d33999 | 224 | * |
<> | 149:156823d33999 | 225 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
<> | 149:156823d33999 | 226 | * the default SPI value is sent |
<> | 149:156823d33999 | 227 | * @param tx_length The length of TX buffer in bytes |
<> | 149:156823d33999 | 228 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
<> | 149:156823d33999 | 229 | * received data are ignored |
<> | 149:156823d33999 | 230 | * @param rx_length The length of RX buffer in bytes |
<> | 149:156823d33999 | 231 | * @param bit_width The buffers element width |
<> | 149:156823d33999 | 232 | * @param callback The event callback function |
<> | 149:156823d33999 | 233 | * @param event The logical OR of events to modify |
<> | 149:156823d33999 | 234 | */ |
<> | 149:156823d33999 | 235 | 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 | 236 | |
<> | 149:156823d33999 | 237 | #if TRANSACTION_QUEUE_SIZE_SPI |
<> | 149:156823d33999 | 238 | |
<> | 149:156823d33999 | 239 | /** Start a new transaction |
<> | 149:156823d33999 | 240 | * |
<> | 149:156823d33999 | 241 | * @param data Transaction data |
<> | 149:156823d33999 | 242 | */ |
<> | 149:156823d33999 | 243 | void start_transaction(transaction_t *data); |
<> | 149:156823d33999 | 244 | |
<> | 149:156823d33999 | 245 | /** Dequeue a transaction |
<> | 149:156823d33999 | 246 | * |
<> | 149:156823d33999 | 247 | */ |
<> | 149:156823d33999 | 248 | void dequeue_transaction(); |
<> | 149:156823d33999 | 249 | static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer; |
<> | 149:156823d33999 | 250 | #endif |
<> | 149:156823d33999 | 251 | |
<> | 149:156823d33999 | 252 | #endif |
<> | 149:156823d33999 | 253 | |
<> | 149:156823d33999 | 254 | public: |
<> | 149:156823d33999 | 255 | virtual ~SPI() { |
<> | 149:156823d33999 | 256 | } |
<> | 149:156823d33999 | 257 | |
<> | 149:156823d33999 | 258 | protected: |
<> | 149:156823d33999 | 259 | spi_t _spi; |
<> | 149:156823d33999 | 260 | |
<> | 149:156823d33999 | 261 | #if DEVICE_SPI_ASYNCH |
<> | 149:156823d33999 | 262 | CThunk<SPI> _irq; |
<> | 149:156823d33999 | 263 | event_callback_t _callback; |
<> | 149:156823d33999 | 264 | DMAUsage _usage; |
<> | 149:156823d33999 | 265 | #endif |
<> | 149:156823d33999 | 266 | |
<> | 149:156823d33999 | 267 | void aquire(void); |
<> | 149:156823d33999 | 268 | static SPI *_owner; |
<> | 149:156823d33999 | 269 | static SingletonPtr<PlatformMutex> _mutex; |
<> | 149:156823d33999 | 270 | int _bits; |
<> | 149:156823d33999 | 271 | int _mode; |
<> | 149:156823d33999 | 272 | int _hz; |
<> | 149:156823d33999 | 273 | }; |
<> | 149:156823d33999 | 274 | |
<> | 149:156823d33999 | 275 | } // namespace mbed |
<> | 149:156823d33999 | 276 | |
<> | 149:156823d33999 | 277 | #endif |
<> | 149:156823d33999 | 278 | |
<> | 149:156823d33999 | 279 | #endif |