1232

Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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