mbed official / mbed

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

Committer:
Kojto
Date:
Fri Aug 12 13:04:35 2016 +0200
Revision:
123:b0220dba8be7
Parent:
122:f9eeca106725
Release 123 of the mbed library

Changes:
- new targets: nucleo_f207zg, beetle, nrf51_dk, hexiwear,
nuvoton nuc472, vk rz a1h
- ST - fix timer interrupt handler, sleep api fix
- NXP - lpc15xx us ticker fix
- Nordic - analogin fixes, LF clock init addition, enable i2c async

Who changed what in which revision?

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