The official mbed C/C SDK provides the software platform and libraries to build your applications.
Fork of mbed by
SPI.h@98:8ab26030e058, 2015-04-29 (annotated)
- Committer:
- Kojto
- Date:
- Wed Apr 29 10:16:23 2015 +0100
- Revision:
- 98:8ab26030e058
- Parent:
- 85:024bf7f99721
Release 98 of the mbed library
Changes:
- Silabs new targets (Giant, Zero, Happy, Leopard, Wonder Geckos)
- Asynchronous SPI, I2C, Serial
- LowPower classes
- Nordic - nordic SDK v8.0 update
- Teensy - gcc arm fix for startup
- Nucleo F411 - usb freq fix
Who changed what in which revision?
User | Revision | Line number | New 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 | |
bogdanm | 65:5798e58a58b1 | 23 | #include "spi_api.h" |
bogdanm | 65:5798e58a58b1 | 24 | |
Kojto | 98:8ab26030e058 | 25 | #if DEVICE_SPI_ASYNCH |
Kojto | 98:8ab26030e058 | 26 | #include "CThunk.h" |
Kojto | 98:8ab26030e058 | 27 | #include "dma_api.h" |
Kojto | 98:8ab26030e058 | 28 | #include "CircularBuffer.h" |
Kojto | 98:8ab26030e058 | 29 | #include "FunctionPointer.h" |
Kojto | 98:8ab26030e058 | 30 | #include "Transaction.h" |
Kojto | 98:8ab26030e058 | 31 | #endif |
Kojto | 98:8ab26030e058 | 32 | |
bogdanm | 65:5798e58a58b1 | 33 | namespace mbed { |
bogdanm | 65:5798e58a58b1 | 34 | |
bogdanm | 65:5798e58a58b1 | 35 | /** A SPI Master, used for communicating with SPI slave devices |
bogdanm | 65:5798e58a58b1 | 36 | * |
bogdanm | 65:5798e58a58b1 | 37 | * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz |
bogdanm | 65:5798e58a58b1 | 38 | * |
bogdanm | 65:5798e58a58b1 | 39 | * Most SPI devices will also require Chip Select and Reset signals. These |
bogdanm | 65:5798e58a58b1 | 40 | * can be controlled using <DigitalOut> pins |
bogdanm | 65:5798e58a58b1 | 41 | * |
bogdanm | 65:5798e58a58b1 | 42 | * Example: |
bogdanm | 65:5798e58a58b1 | 43 | * @code |
bogdanm | 65:5798e58a58b1 | 44 | * // Send a byte to a SPI slave, and record the response |
bogdanm | 65:5798e58a58b1 | 45 | * |
bogdanm | 65:5798e58a58b1 | 46 | * #include "mbed.h" |
bogdanm | 65:5798e58a58b1 | 47 | * |
bogdanm | 65:5798e58a58b1 | 48 | * SPI device(p5, p6, p7); // mosi, miso, sclk |
bogdanm | 65:5798e58a58b1 | 49 | * |
bogdanm | 65:5798e58a58b1 | 50 | * int main() { |
bogdanm | 65:5798e58a58b1 | 51 | * int response = device.write(0xFF); |
bogdanm | 65:5798e58a58b1 | 52 | * } |
bogdanm | 65:5798e58a58b1 | 53 | * @endcode |
bogdanm | 65:5798e58a58b1 | 54 | */ |
bogdanm | 65:5798e58a58b1 | 55 | class SPI { |
bogdanm | 65:5798e58a58b1 | 56 | |
bogdanm | 65:5798e58a58b1 | 57 | public: |
bogdanm | 65:5798e58a58b1 | 58 | |
bogdanm | 65:5798e58a58b1 | 59 | /** Create a SPI master connected to the specified pins |
bogdanm | 65:5798e58a58b1 | 60 | * |
bogdanm | 65:5798e58a58b1 | 61 | * Pin Options: |
bogdanm | 65:5798e58a58b1 | 62 | * (5, 6, 7) or (11, 12, 13) |
bogdanm | 65:5798e58a58b1 | 63 | * |
bogdanm | 65:5798e58a58b1 | 64 | * mosi or miso can be specfied as NC if not used |
bogdanm | 65:5798e58a58b1 | 65 | * |
bogdanm | 65:5798e58a58b1 | 66 | * @param mosi SPI Master Out, Slave In pin |
bogdanm | 65:5798e58a58b1 | 67 | * @param miso SPI Master In, Slave Out pin |
bogdanm | 65:5798e58a58b1 | 68 | * @param sclk SPI Clock pin |
bogdanm | 65:5798e58a58b1 | 69 | */ |
bogdanm | 71:8fabd470bb6e | 70 | SPI(PinName mosi, PinName miso, PinName sclk, PinName _unused=NC); |
bogdanm | 65:5798e58a58b1 | 71 | |
bogdanm | 65:5798e58a58b1 | 72 | /** Configure the data transmission format |
bogdanm | 65:5798e58a58b1 | 73 | * |
bogdanm | 65:5798e58a58b1 | 74 | * @param bits Number of bits per SPI frame (4 - 16) |
bogdanm | 65:5798e58a58b1 | 75 | * @param mode Clock polarity and phase mode (0 - 3) |
bogdanm | 65:5798e58a58b1 | 76 | * |
bogdanm | 65:5798e58a58b1 | 77 | * @code |
bogdanm | 65:5798e58a58b1 | 78 | * mode | POL PHA |
bogdanm | 65:5798e58a58b1 | 79 | * -----+-------- |
bogdanm | 65:5798e58a58b1 | 80 | * 0 | 0 0 |
bogdanm | 65:5798e58a58b1 | 81 | * 1 | 0 1 |
bogdanm | 65:5798e58a58b1 | 82 | * 2 | 1 0 |
bogdanm | 65:5798e58a58b1 | 83 | * 3 | 1 1 |
bogdanm | 65:5798e58a58b1 | 84 | * @endcode |
bogdanm | 65:5798e58a58b1 | 85 | */ |
bogdanm | 65:5798e58a58b1 | 86 | void format(int bits, int mode = 0); |
bogdanm | 65:5798e58a58b1 | 87 | |
bogdanm | 65:5798e58a58b1 | 88 | /** Set the spi bus clock frequency |
bogdanm | 65:5798e58a58b1 | 89 | * |
bogdanm | 65:5798e58a58b1 | 90 | * @param hz SCLK frequency in hz (default = 1MHz) |
bogdanm | 65:5798e58a58b1 | 91 | */ |
bogdanm | 65:5798e58a58b1 | 92 | void frequency(int hz = 1000000); |
bogdanm | 65:5798e58a58b1 | 93 | |
bogdanm | 65:5798e58a58b1 | 94 | /** Write to the SPI Slave and return the response |
bogdanm | 65:5798e58a58b1 | 95 | * |
bogdanm | 65:5798e58a58b1 | 96 | * @param value Data to be sent to the SPI slave |
bogdanm | 65:5798e58a58b1 | 97 | * |
bogdanm | 65:5798e58a58b1 | 98 | * @returns |
bogdanm | 65:5798e58a58b1 | 99 | * Response from the SPI slave |
bogdanm | 65:5798e58a58b1 | 100 | */ |
bogdanm | 65:5798e58a58b1 | 101 | virtual int write(int value); |
bogdanm | 65:5798e58a58b1 | 102 | |
Kojto | 98:8ab26030e058 | 103 | #if DEVICE_SPI_ASYNCH |
Kojto | 98:8ab26030e058 | 104 | |
Kojto | 98:8ab26030e058 | 105 | /** Start non-blocking SPI transfer using 8bit buffers. |
Kojto | 98:8ab26030e058 | 106 | * |
Kojto | 98:8ab26030e058 | 107 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
Kojto | 98:8ab26030e058 | 108 | * the default SPI value is sent |
Kojto | 98:8ab26030e058 | 109 | * @param tx_length The length of TX buffer |
Kojto | 98:8ab26030e058 | 110 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
Kojto | 98:8ab26030e058 | 111 | * received data are ignored |
Kojto | 98:8ab26030e058 | 112 | * @param rx_length The length of RX buffer |
Kojto | 98:8ab26030e058 | 113 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 114 | * @param event The logical OR of events to modify |
Kojto | 98:8ab26030e058 | 115 | * @return Zero if the transfer has started, or -1 if SPI peripheral is busy |
Kojto | 98:8ab26030e058 | 116 | */ |
Kojto | 98:8ab26030e058 | 117 | virtual int transfer(uint8_t *tx_buffer, int tx_length, uint8_t *rx_buffer, int rx_length, const event_callback_t& callback, int event = SPI_EVENT_COMPLETE) { |
Kojto | 98:8ab26030e058 | 118 | return transfer(tx_buffer, tx_length, rx_buffer, rx_length, 8, callback, event); |
Kojto | 98:8ab26030e058 | 119 | } |
Kojto | 98:8ab26030e058 | 120 | |
Kojto | 98:8ab26030e058 | 121 | /** Start non-blocking SPI transfer using 16bit buffers. |
Kojto | 98:8ab26030e058 | 122 | * |
Kojto | 98:8ab26030e058 | 123 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
Kojto | 98:8ab26030e058 | 124 | * the default SPI value is sent |
Kojto | 98:8ab26030e058 | 125 | * @param tx_length The length of TX buffer |
Kojto | 98:8ab26030e058 | 126 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
Kojto | 98:8ab26030e058 | 127 | * received data are ignored |
Kojto | 98:8ab26030e058 | 128 | * @param rx_length The length of RX buffer |
Kojto | 98:8ab26030e058 | 129 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 130 | * @param event The logical OR of events to modify |
Kojto | 98:8ab26030e058 | 131 | * @return Zero if the transfer has started, or -1 if SPI peripheral is busy |
Kojto | 98:8ab26030e058 | 132 | */ |
Kojto | 98:8ab26030e058 | 133 | virtual int transfer(uint16_t *tx_buffer, int tx_length, uint16_t *rx_buffer, int rx_length, const event_callback_t& callback, int event = SPI_EVENT_COMPLETE) { |
Kojto | 98:8ab26030e058 | 134 | return transfer(tx_buffer, tx_length, rx_buffer, rx_length, 16, callback, event); |
Kojto | 98:8ab26030e058 | 135 | } |
Kojto | 98:8ab26030e058 | 136 | |
Kojto | 98:8ab26030e058 | 137 | /** Start non-blocking SPI transfer using 32bit buffers. |
Kojto | 98:8ab26030e058 | 138 | * |
Kojto | 98:8ab26030e058 | 139 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
Kojto | 98:8ab26030e058 | 140 | * the default SPI value is sent |
Kojto | 98:8ab26030e058 | 141 | * @param tx_length The length of TX buffer |
Kojto | 98:8ab26030e058 | 142 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
Kojto | 98:8ab26030e058 | 143 | * received data are ignored |
Kojto | 98:8ab26030e058 | 144 | * @param rx_length The length of RX buffer |
Kojto | 98:8ab26030e058 | 145 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 146 | * @param event The logical OR of events to modify |
Kojto | 98:8ab26030e058 | 147 | * @return Zero if the transfer has started, or -1 if SPI peripheral is busy |
Kojto | 98:8ab26030e058 | 148 | */ |
Kojto | 98:8ab26030e058 | 149 | virtual int transfer(uint32_t *tx_buffer, int tx_length, uint32_t *rx_buffer, int rx_length, const event_callback_t& callback, int event = SPI_EVENT_COMPLETE) { |
Kojto | 98:8ab26030e058 | 150 | return transfer((void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, 32, callback, event); |
Kojto | 98:8ab26030e058 | 151 | } |
Kojto | 98:8ab26030e058 | 152 | |
Kojto | 98:8ab26030e058 | 153 | /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any. |
Kojto | 98:8ab26030e058 | 154 | */ |
Kojto | 98:8ab26030e058 | 155 | void abort_transfer(); |
Kojto | 98:8ab26030e058 | 156 | |
Kojto | 98:8ab26030e058 | 157 | /** Clear the transaction buffer |
Kojto | 98:8ab26030e058 | 158 | */ |
Kojto | 98:8ab26030e058 | 159 | void clear_transfer_buffer(); |
Kojto | 98:8ab26030e058 | 160 | |
Kojto | 98:8ab26030e058 | 161 | /** Clear the transaction buffer and abort on-going transfer. |
Kojto | 98:8ab26030e058 | 162 | */ |
Kojto | 98:8ab26030e058 | 163 | void abort_all_transfers(); |
Kojto | 98:8ab26030e058 | 164 | |
Kojto | 98:8ab26030e058 | 165 | /** Configure DMA usage suggestion for non-blocking transfers |
Kojto | 98:8ab26030e058 | 166 | * |
Kojto | 98:8ab26030e058 | 167 | * @param usage The usage DMA hint for peripheral |
Kojto | 98:8ab26030e058 | 168 | * @return Zero if the usage was set, -1 if a transaction is on-going |
Kojto | 98:8ab26030e058 | 169 | */ |
Kojto | 98:8ab26030e058 | 170 | int set_dma_usage(DMAUsage usage); |
Kojto | 98:8ab26030e058 | 171 | |
Kojto | 98:8ab26030e058 | 172 | protected: |
Kojto | 98:8ab26030e058 | 173 | /** SPI IRQ handler |
Kojto | 98:8ab26030e058 | 174 | * |
Kojto | 98:8ab26030e058 | 175 | */ |
Kojto | 98:8ab26030e058 | 176 | void irq_handler_asynch(void); |
Kojto | 98:8ab26030e058 | 177 | |
Kojto | 98:8ab26030e058 | 178 | /** Common transfer method |
Kojto | 98:8ab26030e058 | 179 | * |
Kojto | 98:8ab26030e058 | 180 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
Kojto | 98:8ab26030e058 | 181 | * the default SPI value is sent |
Kojto | 98:8ab26030e058 | 182 | * @param tx_length The length of TX buffer |
Kojto | 98:8ab26030e058 | 183 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
Kojto | 98:8ab26030e058 | 184 | * received data are ignored |
Kojto | 98:8ab26030e058 | 185 | * @param rx_length The length of RX buffer |
Kojto | 98:8ab26030e058 | 186 | * @param bit_width The buffers element width |
Kojto | 98:8ab26030e058 | 187 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 188 | * @param event The logical OR of events to modify |
Kojto | 98:8ab26030e058 | 189 | * @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 | 190 | */ |
Kojto | 98:8ab26030e058 | 191 | int transfer(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 | 192 | |
Kojto | 98:8ab26030e058 | 193 | /** |
Kojto | 98:8ab26030e058 | 194 | * |
Kojto | 98:8ab26030e058 | 195 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
Kojto | 98:8ab26030e058 | 196 | * the default SPI value is sent |
Kojto | 98:8ab26030e058 | 197 | * @param tx_length The length of TX buffer |
Kojto | 98:8ab26030e058 | 198 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
Kojto | 98:8ab26030e058 | 199 | * received data are ignored |
Kojto | 98:8ab26030e058 | 200 | * @param rx_length The length of RX buffer |
Kojto | 98:8ab26030e058 | 201 | * @param bit_width The buffers element width |
Kojto | 98:8ab26030e058 | 202 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 203 | * @param event The logical OR of events to modify |
Kojto | 98:8ab26030e058 | 204 | * @return Zero if a transfer was added to the queue, or -1 if the queue is full |
Kojto | 98:8ab26030e058 | 205 | */ |
Kojto | 98:8ab26030e058 | 206 | int queue_transfer(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 | 207 | |
Kojto | 98:8ab26030e058 | 208 | /** Configures a callback, spi peripheral and initiate a new transfer |
Kojto | 98:8ab26030e058 | 209 | * |
Kojto | 98:8ab26030e058 | 210 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
Kojto | 98:8ab26030e058 | 211 | * the default SPI value is sent |
Kojto | 98:8ab26030e058 | 212 | * @param tx_length The length of TX buffer |
Kojto | 98:8ab26030e058 | 213 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
Kojto | 98:8ab26030e058 | 214 | * received data are ignored |
Kojto | 98:8ab26030e058 | 215 | * @param rx_length The length of RX buffer |
Kojto | 98:8ab26030e058 | 216 | * @param bit_width The buffers element width |
Kojto | 98:8ab26030e058 | 217 | * @param callback The event callback function |
Kojto | 98:8ab26030e058 | 218 | * @param event The logical OR of events to modify |
Kojto | 98:8ab26030e058 | 219 | */ |
Kojto | 98:8ab26030e058 | 220 | void start_transfer(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 | 221 | |
Kojto | 98:8ab26030e058 | 222 | #if TRANSACTION_QUEUE_SIZE_SPI |
Kojto | 98:8ab26030e058 | 223 | |
Kojto | 98:8ab26030e058 | 224 | /** Start a new transaction |
Kojto | 98:8ab26030e058 | 225 | * |
Kojto | 98:8ab26030e058 | 226 | * @param data Transaction data |
Kojto | 98:8ab26030e058 | 227 | */ |
Kojto | 98:8ab26030e058 | 228 | void start_transaction(transaction_t *data); |
Kojto | 98:8ab26030e058 | 229 | |
Kojto | 98:8ab26030e058 | 230 | /** Dequeue a transaction |
Kojto | 98:8ab26030e058 | 231 | * |
Kojto | 98:8ab26030e058 | 232 | */ |
Kojto | 98:8ab26030e058 | 233 | void dequeue_transaction(); |
Kojto | 98:8ab26030e058 | 234 | static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer; |
Kojto | 98:8ab26030e058 | 235 | #endif |
Kojto | 98:8ab26030e058 | 236 | |
Kojto | 98:8ab26030e058 | 237 | #endif |
Kojto | 98:8ab26030e058 | 238 | |
bogdanm | 85:024bf7f99721 | 239 | public: |
bogdanm | 85:024bf7f99721 | 240 | virtual ~SPI() { |
bogdanm | 85:024bf7f99721 | 241 | } |
bogdanm | 85:024bf7f99721 | 242 | |
bogdanm | 65:5798e58a58b1 | 243 | protected: |
bogdanm | 65:5798e58a58b1 | 244 | spi_t _spi; |
bogdanm | 65:5798e58a58b1 | 245 | |
Kojto | 98:8ab26030e058 | 246 | #if DEVICE_SPI_ASYNCH |
Kojto | 98:8ab26030e058 | 247 | CThunk<SPI> _irq; |
Kojto | 98:8ab26030e058 | 248 | event_callback_t _callback; |
Kojto | 98:8ab26030e058 | 249 | DMAUsage _usage; |
Kojto | 98:8ab26030e058 | 250 | #endif |
Kojto | 98:8ab26030e058 | 251 | |
bogdanm | 65:5798e58a58b1 | 252 | void aquire(void); |
bogdanm | 65:5798e58a58b1 | 253 | static SPI *_owner; |
bogdanm | 65:5798e58a58b1 | 254 | int _bits; |
bogdanm | 65:5798e58a58b1 | 255 | int _mode; |
bogdanm | 65:5798e58a58b1 | 256 | int _hz; |
bogdanm | 65:5798e58a58b1 | 257 | }; |
bogdanm | 65:5798e58a58b1 | 258 | |
bogdanm | 65:5798e58a58b1 | 259 | } // namespace mbed |
bogdanm | 65:5798e58a58b1 | 260 | |
bogdanm | 65:5798e58a58b1 | 261 | #endif |
bogdanm | 65:5798e58a58b1 | 262 | |
bogdanm | 65:5798e58a58b1 | 263 | #endif |