Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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