The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Fri Sep 15 14:46:57 2017 +0100
Revision:
151:675da3299148
Parent:
148:fd96258d940d
Child:
152:235179ab3f27
Release 151 of the mbed library.

Who changed what in which revision?

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