4545

Dependents:   LSS_Rev_1

Fork of mbed-dev by Umar Naeem

Committer:
iftaziz
Date:
Wed Aug 23 10:32:38 2017 +0000
Revision:
166:33361e55dd8c
Parent:
149:156823d33999
r1

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
<> 149:156823d33999 21 #if DEVICE_SPI
<> 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"
<> 149:156823d33999 26
<> 149:156823d33999 27 #if DEVICE_SPI_ASYNCH
<> 149:156823d33999 28 #include "platform/CThunk.h"
<> 149:156823d33999 29 #include "hal/dma_api.h"
<> 149:156823d33999 30 #include "platform/CircularBuffer.h"
<> 149:156823d33999 31 #include "platform/FunctionPointer.h"
<> 149:156823d33999 32 #include "platform/Transaction.h"
<> 149:156823d33999 33 #endif
<> 149:156823d33999 34
<> 149:156823d33999 35 namespace mbed {
<> 149:156823d33999 36 /** \addtogroup drivers */
<> 149:156823d33999 37 /** @{*/
<> 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
<> 149:156823d33999 44 * can be controlled using <DigitalOut> pins
<> 149:156823d33999 45 *
<> 149:156823d33999 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
<> 149:156823d33999 74 */
<> 149:156823d33999 75 class SPI {
<> 149:156823d33999 76
<> 149:156823d33999 77 public:
<> 149:156823d33999 78
<> 149:156823d33999 79 /** Create a SPI master connected to the specified pins
<> 149:156823d33999 80 *
<> 149:156823d33999 81 * mosi or miso can be specfied as NC if not used
<> 149:156823d33999 82 *
<> 149:156823d33999 83 * @param mosi SPI Master Out, Slave In pin
<> 149:156823d33999 84 * @param miso SPI Master In, Slave Out pin
<> 149:156823d33999 85 * @param sclk SPI Clock pin
<> 149:156823d33999 86 * @param ssel SPI chip select pin
<> 149:156823d33999 87 */
<> 149:156823d33999 88 SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC);
<> 149:156823d33999 89
<> 149:156823d33999 90 /** Configure the data transmission format
<> 149:156823d33999 91 *
<> 149:156823d33999 92 * @param bits Number of bits per SPI frame (4 - 16)
<> 149:156823d33999 93 * @param mode Clock polarity and phase mode (0 - 3)
<> 149:156823d33999 94 *
<> 149:156823d33999 95 * @code
<> 149:156823d33999 96 * mode | POL PHA
<> 149:156823d33999 97 * -----+--------
<> 149:156823d33999 98 * 0 | 0 0
<> 149:156823d33999 99 * 1 | 0 1
<> 149:156823d33999 100 * 2 | 1 0
<> 149:156823d33999 101 * 3 | 1 1
<> 149:156823d33999 102 * @endcode
<> 149:156823d33999 103 */
<> 149:156823d33999 104 void format(int bits, int mode = 0);
<> 149:156823d33999 105
<> 149:156823d33999 106 /** Set the spi bus clock frequency
<> 149:156823d33999 107 *
<> 149:156823d33999 108 * @param hz SCLK frequency in hz (default = 1MHz)
<> 149:156823d33999 109 */
<> 149:156823d33999 110 void frequency(int hz = 1000000);
<> 149:156823d33999 111
<> 149:156823d33999 112 /** Write to the SPI Slave and return the response
<> 149:156823d33999 113 *
<> 149:156823d33999 114 * @param value Data to be sent to the SPI slave
<> 149:156823d33999 115 *
<> 149:156823d33999 116 * @returns
<> 149:156823d33999 117 * Response from the SPI slave
<> 149:156823d33999 118 */
<> 149:156823d33999 119 virtual int write(int value);
<> 149:156823d33999 120
<> 149:156823d33999 121 /** Acquire exclusive access to this SPI bus
<> 149:156823d33999 122 */
<> 149:156823d33999 123 virtual void lock(void);
<> 149:156823d33999 124
<> 149:156823d33999 125 /** Release exclusive access to this SPI bus
<> 149:156823d33999 126 */
<> 149:156823d33999 127 virtual void unlock(void);
<> 149:156823d33999 128
<> 149:156823d33999 129 #if DEVICE_SPI_ASYNCH
<> 149:156823d33999 130
<> 149:156823d33999 131 /** Start non-blocking SPI transfer using 8bit buffers.
<> 149:156823d33999 132 *
<> 149:156823d33999 133 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
<> 149:156823d33999 134 * the default SPI value is sent
<> 149:156823d33999 135 * @param tx_length The length of TX buffer in bytes
<> 149:156823d33999 136 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
<> 149:156823d33999 137 * received data are ignored
<> 149:156823d33999 138 * @param rx_length The length of RX buffer in bytes
<> 149:156823d33999 139 * @param callback The event callback function
<> 149:156823d33999 140 * @param event The logical OR of events to modify. Look at spi hal header file for SPI events.
<> 149:156823d33999 141 * @return Zero if the transfer has started, or -1 if SPI peripheral is busy
<> 149:156823d33999 142 */
<> 149:156823d33999 143 template<typename Type>
<> 149:156823d33999 144 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 145 if (spi_active(&_spi)) {
<> 149:156823d33999 146 return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
<> 149:156823d33999 147 }
<> 149:156823d33999 148 start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
<> 149:156823d33999 149 return 0;
<> 149:156823d33999 150 }
<> 149:156823d33999 151
<> 149:156823d33999 152 /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any.
<> 149:156823d33999 153 */
<> 149:156823d33999 154 void abort_transfer();
<> 149:156823d33999 155
<> 149:156823d33999 156 /** Clear the transaction buffer
<> 149:156823d33999 157 */
<> 149:156823d33999 158 void clear_transfer_buffer();
<> 149:156823d33999 159
<> 149:156823d33999 160 /** Clear the transaction buffer and abort on-going transfer.
<> 149:156823d33999 161 */
<> 149:156823d33999 162 void abort_all_transfers();
<> 149:156823d33999 163
<> 149:156823d33999 164 /** Configure DMA usage suggestion for non-blocking transfers
<> 149:156823d33999 165 *
<> 149:156823d33999 166 * @param usage The usage DMA hint for peripheral
<> 149:156823d33999 167 * @return Zero if the usage was set, -1 if a transaction is on-going
<> 149:156823d33999 168 */
<> 149:156823d33999 169 int set_dma_usage(DMAUsage usage);
<> 149:156823d33999 170
<> 149:156823d33999 171 protected:
<> 149:156823d33999 172 /** SPI IRQ handler
<> 149:156823d33999 173 *
<> 149:156823d33999 174 */
<> 149:156823d33999 175 void irq_handler_asynch(void);
<> 149:156823d33999 176
<> 149:156823d33999 177 /** Common transfer method
<> 149:156823d33999 178 *
<> 149:156823d33999 179 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
<> 149:156823d33999 180 * the default SPI value is sent
<> 149:156823d33999 181 * @param tx_length The length of TX buffer in bytes
<> 149:156823d33999 182 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
<> 149:156823d33999 183 * received data are ignored
<> 149:156823d33999 184 * @param rx_length The length of RX buffer in bytes
<> 149:156823d33999 185 * @param bit_width The buffers element width
<> 149:156823d33999 186 * @param callback The event callback function
<> 149:156823d33999 187 * @param event The logical OR of events to modify
<> 149:156823d33999 188 * @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 189 */
<> 149:156823d33999 190 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 191
<> 149:156823d33999 192 /**
<> 149:156823d33999 193 *
<> 149:156823d33999 194 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
<> 149:156823d33999 195 * the default SPI value is sent
<> 149:156823d33999 196 * @param tx_length The length of TX buffer in bytes
<> 149:156823d33999 197 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
<> 149:156823d33999 198 * received data are ignored
<> 149:156823d33999 199 * @param rx_length The length of RX buffer in bytes
<> 149:156823d33999 200 * @param bit_width The buffers element width
<> 149:156823d33999 201 * @param callback The event callback function
<> 149:156823d33999 202 * @param event The logical OR of events to modify
<> 149:156823d33999 203 * @return Zero if a transfer was added to the queue, or -1 if the queue is full
<> 149:156823d33999 204 */
<> 149:156823d33999 205 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 206
<> 149:156823d33999 207 /** Configures a callback, spi peripheral and initiate a new transfer
<> 149:156823d33999 208 *
<> 149:156823d33999 209 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
<> 149:156823d33999 210 * the default SPI value is sent
<> 149:156823d33999 211 * @param tx_length The length of TX buffer in bytes
<> 149:156823d33999 212 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
<> 149:156823d33999 213 * received data are ignored
<> 149:156823d33999 214 * @param rx_length The length of RX buffer in bytes
<> 149:156823d33999 215 * @param bit_width The buffers element width
<> 149:156823d33999 216 * @param callback The event callback function
<> 149:156823d33999 217 * @param event The logical OR of events to modify
<> 149:156823d33999 218 */
<> 149:156823d33999 219 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 220
<> 149:156823d33999 221 #if TRANSACTION_QUEUE_SIZE_SPI
<> 149:156823d33999 222
<> 149:156823d33999 223 /** Start a new transaction
<> 149:156823d33999 224 *
<> 149:156823d33999 225 * @param data Transaction data
<> 149:156823d33999 226 */
<> 149:156823d33999 227 void start_transaction(transaction_t *data);
<> 149:156823d33999 228
<> 149:156823d33999 229 /** Dequeue a transaction
<> 149:156823d33999 230 *
<> 149:156823d33999 231 */
<> 149:156823d33999 232 void dequeue_transaction();
<> 149:156823d33999 233 static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer;
<> 149:156823d33999 234 #endif
<> 149:156823d33999 235
<> 149:156823d33999 236 #endif
<> 149:156823d33999 237
<> 149:156823d33999 238 public:
<> 149:156823d33999 239 virtual ~SPI() {
<> 149:156823d33999 240 }
<> 149:156823d33999 241
<> 149:156823d33999 242 protected:
<> 149:156823d33999 243 spi_t _spi;
<> 149:156823d33999 244
<> 149:156823d33999 245 #if DEVICE_SPI_ASYNCH
<> 149:156823d33999 246 CThunk<SPI> _irq;
<> 149:156823d33999 247 event_callback_t _callback;
<> 149:156823d33999 248 DMAUsage _usage;
<> 149:156823d33999 249 #endif
<> 149:156823d33999 250
<> 149:156823d33999 251 void aquire(void);
<> 149:156823d33999 252 static SPI *_owner;
<> 149:156823d33999 253 static SingletonPtr<PlatformMutex> _mutex;
<> 149:156823d33999 254 int _bits;
<> 149:156823d33999 255 int _mode;
<> 149:156823d33999 256 int _hz;
<> 149:156823d33999 257 };
<> 149:156823d33999 258
<> 149:156823d33999 259 } // namespace mbed
<> 149:156823d33999 260
<> 149:156823d33999 261 #endif
<> 149:156823d33999 262
<> 149:156823d33999 263 #endif
<> 149:156823d33999 264
<> 149:156823d33999 265 /** @}*/