Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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