mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
Kojto
Date:
Wed Jul 19 17:31:21 2017 +0100
Revision:
169:e3b6fe271b81
Parent:
168:9672193075cf
Child:
170:19eb464bc2be
This updates the lib to the mbed lib v 147

Who changed what in which revision?

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