inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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