Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: hello SerialTestv11 SerialTestv12 Sierpinski ... more
SPI.h@122:f9eeca106725, 2016-07-07 (annotated)
- Committer:
- Kojto
- Date:
- Thu Jul 07 14:34:11 2016 +0100
- Revision:
- 122:f9eeca106725
- Parent:
- 102:da0ca467f8b5
- Child:
- 123:b0220dba8be7
Release 122 of the mbed library
Changes:
- new targets - Nucleo L432KC, Beetle, Nucleo F446ZE, Nucleo L011K4
- Thread safety addition - mbed API should contain a statement about thread safety
- critical section API addition
- CAS API (core_util_atomic_incr/decr)
- DEVICE_ are generated from targets.json file, device.h deprecated
- Callback replaces FunctionPointer to provide std like interface
- mbed HAL API docs improvements
- toolchain - prexif attributes with MBED_
- add new attributes - packed, weak, forcedinline, align
- target.json - contains targets definitions
- ST - L1XX - Cube update to 1.5
- SPI clock selection fix (clock from APB domain)
- F7 - Cube update v1.4.0
- L0 - baudrate init fix
- L1 - Cube update v1.5
- F3 - baudrate init fix, 3 targets CAN support
- F4 - Cube update v1.12.0, 3 targets CAN support
- L4XX - Cube update v1.5.1
- F0 - update Cube to v1.5.0
- L4 - 2 targets (L476RG/VG) CAN support
- NXP - pwm clock fix for KSDK2 MCU
- LPC2368 - remove ARM toolchain support - due to regression
- KSDK2 - fix SPI , I2C address and repeat start
- Silabs - some fixes backported from mbed 3
- Renesas - RZ_A1H - SystemCoreClockUpdate addition
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 | * |
| Kojto | 122:f9eeca106725 | 42 | * @Note Synchronization level: Thread safe |
| Kojto | 122:f9eeca106725 | 43 | * |
| bogdanm | 65:5798e58a58b1 | 44 | * Example: |
| bogdanm | 65:5798e58a58b1 | 45 | * @code |
| bogdanm | 65:5798e58a58b1 | 46 | * // Send a byte to a SPI slave, and record the response |
| bogdanm | 65:5798e58a58b1 | 47 | * |
| bogdanm | 65:5798e58a58b1 | 48 | * #include "mbed.h" |
| bogdanm | 65:5798e58a58b1 | 49 | * |
| Kojto | 101:7cff1c4259d7 | 50 | * // hardware ssel (where applicable) |
| Kojto | 101:7cff1c4259d7 | 51 | * //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel |
| Kojto | 101:7cff1c4259d7 | 52 | * |
| Kojto | 101:7cff1c4259d7 | 53 | * // software ssel |
| bogdanm | 65:5798e58a58b1 | 54 | * SPI device(p5, p6, p7); // mosi, miso, sclk |
| Kojto | 101:7cff1c4259d7 | 55 | * DigitalOut cs(p8); // ssel |
| bogdanm | 65:5798e58a58b1 | 56 | * |
| bogdanm | 65:5798e58a58b1 | 57 | * int main() { |
| Kojto | 101:7cff1c4259d7 | 58 | * // hardware ssel (where applicable) |
| Kojto | 101:7cff1c4259d7 | 59 | * //int response = device.write(0xFF); |
| Kojto | 101:7cff1c4259d7 | 60 | * |
| Kojto | 122:f9eeca106725 | 61 | * device.lock(); |
| Kojto | 101:7cff1c4259d7 | 62 | * // software ssel |
| Kojto | 101:7cff1c4259d7 | 63 | * cs = 0; |
| bogdanm | 65:5798e58a58b1 | 64 | * int response = device.write(0xFF); |
| Kojto | 101:7cff1c4259d7 | 65 | * cs = 1; |
| Kojto | 122:f9eeca106725 | 66 | * device.unlock(); |
| Kojto | 122:f9eeca106725 | 67 | * |
| bogdanm | 65:5798e58a58b1 | 68 | * } |
| bogdanm | 65:5798e58a58b1 | 69 | * @endcode |
| bogdanm | 65:5798e58a58b1 | 70 | */ |
| bogdanm | 65:5798e58a58b1 | 71 | class SPI { |
| bogdanm | 65:5798e58a58b1 | 72 | |
| bogdanm | 65:5798e58a58b1 | 73 | public: |
| bogdanm | 65:5798e58a58b1 | 74 | |
| bogdanm | 65:5798e58a58b1 | 75 | /** Create a SPI master connected to the specified pins |
| bogdanm | 65:5798e58a58b1 | 76 | * |
| bogdanm | 65:5798e58a58b1 | 77 | * mosi or miso can be specfied as NC if not used |
| bogdanm | 65:5798e58a58b1 | 78 | * |
| bogdanm | 65:5798e58a58b1 | 79 | * @param mosi SPI Master Out, Slave In pin |
| bogdanm | 65:5798e58a58b1 | 80 | * @param miso SPI Master In, Slave Out pin |
| bogdanm | 65:5798e58a58b1 | 81 | * @param sclk SPI Clock pin |
| Kojto | 101:7cff1c4259d7 | 82 | * @param ssel SPI chip select pin |
| bogdanm | 65:5798e58a58b1 | 83 | */ |
| Kojto | 101:7cff1c4259d7 | 84 | SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC); |
| bogdanm | 65:5798e58a58b1 | 85 | |
| bogdanm | 65:5798e58a58b1 | 86 | /** Configure the data transmission format |
| bogdanm | 65:5798e58a58b1 | 87 | * |
| bogdanm | 65:5798e58a58b1 | 88 | * @param bits Number of bits per SPI frame (4 - 16) |
| bogdanm | 65:5798e58a58b1 | 89 | * @param mode Clock polarity and phase mode (0 - 3) |
| bogdanm | 65:5798e58a58b1 | 90 | * |
| bogdanm | 65:5798e58a58b1 | 91 | * @code |
| bogdanm | 65:5798e58a58b1 | 92 | * mode | POL PHA |
| bogdanm | 65:5798e58a58b1 | 93 | * -----+-------- |
| bogdanm | 65:5798e58a58b1 | 94 | * 0 | 0 0 |
| bogdanm | 65:5798e58a58b1 | 95 | * 1 | 0 1 |
| bogdanm | 65:5798e58a58b1 | 96 | * 2 | 1 0 |
| bogdanm | 65:5798e58a58b1 | 97 | * 3 | 1 1 |
| bogdanm | 65:5798e58a58b1 | 98 | * @endcode |
| bogdanm | 65:5798e58a58b1 | 99 | */ |
| bogdanm | 65:5798e58a58b1 | 100 | void format(int bits, int mode = 0); |
| bogdanm | 65:5798e58a58b1 | 101 | |
| bogdanm | 65:5798e58a58b1 | 102 | /** Set the spi bus clock frequency |
| bogdanm | 65:5798e58a58b1 | 103 | * |
| bogdanm | 65:5798e58a58b1 | 104 | * @param hz SCLK frequency in hz (default = 1MHz) |
| bogdanm | 65:5798e58a58b1 | 105 | */ |
| bogdanm | 65:5798e58a58b1 | 106 | void frequency(int hz = 1000000); |
| bogdanm | 65:5798e58a58b1 | 107 | |
| bogdanm | 65:5798e58a58b1 | 108 | /** Write to the SPI Slave and return the response |
| bogdanm | 65:5798e58a58b1 | 109 | * |
| bogdanm | 65:5798e58a58b1 | 110 | * @param value Data to be sent to the SPI slave |
| bogdanm | 65:5798e58a58b1 | 111 | * |
| bogdanm | 65:5798e58a58b1 | 112 | * @returns |
| bogdanm | 65:5798e58a58b1 | 113 | * Response from the SPI slave |
| bogdanm | 65:5798e58a58b1 | 114 | */ |
| bogdanm | 65:5798e58a58b1 | 115 | virtual int write(int value); |
| bogdanm | 65:5798e58a58b1 | 116 | |
| Kojto | 122:f9eeca106725 | 117 | /** Acquire exclusive access to this SPI bus |
| Kojto | 122:f9eeca106725 | 118 | */ |
| Kojto | 122:f9eeca106725 | 119 | virtual void lock(void); |
| Kojto | 122:f9eeca106725 | 120 | |
| Kojto | 122:f9eeca106725 | 121 | /** Release exclusive access to this SPI bus |
| Kojto | 122:f9eeca106725 | 122 | */ |
| Kojto | 122:f9eeca106725 | 123 | virtual void unlock(void); |
| Kojto | 122:f9eeca106725 | 124 | |
| Kojto | 98:8ab26030e058 | 125 | #if DEVICE_SPI_ASYNCH |
| Kojto | 98:8ab26030e058 | 126 | |
| Kojto | 98:8ab26030e058 | 127 | /** Start non-blocking SPI transfer using 8bit buffers. |
| Kojto | 98:8ab26030e058 | 128 | * |
| Kojto | 98:8ab26030e058 | 129 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
| Kojto | 98:8ab26030e058 | 130 | * the default SPI value is sent |
| Kojto | 102:da0ca467f8b5 | 131 | * @param tx_length The length of TX buffer in bytes |
| Kojto | 98:8ab26030e058 | 132 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
| Kojto | 98:8ab26030e058 | 133 | * received data are ignored |
| Kojto | 102:da0ca467f8b5 | 134 | * @param rx_length The length of RX buffer in bytes |
| Kojto | 98:8ab26030e058 | 135 | * @param callback The event callback function |
| Kojto | 102:da0ca467f8b5 | 136 | * @param event The logical OR of events to modify. Look at spi hal header file for SPI events. |
| Kojto | 98:8ab26030e058 | 137 | * @return Zero if the transfer has started, or -1 if SPI peripheral is busy |
| Kojto | 98:8ab26030e058 | 138 | */ |
| Kojto | 102:da0ca467f8b5 | 139 | template<typename Type> |
| Kojto | 102:da0ca467f8b5 | 140 | 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 | 141 | if (spi_active(&_spi)) { |
| Kojto | 102:da0ca467f8b5 | 142 | return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); |
| Kojto | 102:da0ca467f8b5 | 143 | } |
| Kojto | 102:da0ca467f8b5 | 144 | start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); |
| Kojto | 102:da0ca467f8b5 | 145 | return 0; |
| Kojto | 98:8ab26030e058 | 146 | } |
| Kojto | 98:8ab26030e058 | 147 | |
| Kojto | 98:8ab26030e058 | 148 | /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any. |
| Kojto | 98:8ab26030e058 | 149 | */ |
| Kojto | 98:8ab26030e058 | 150 | void abort_transfer(); |
| Kojto | 98:8ab26030e058 | 151 | |
| Kojto | 98:8ab26030e058 | 152 | /** Clear the transaction buffer |
| Kojto | 98:8ab26030e058 | 153 | */ |
| Kojto | 98:8ab26030e058 | 154 | void clear_transfer_buffer(); |
| Kojto | 98:8ab26030e058 | 155 | |
| Kojto | 98:8ab26030e058 | 156 | /** Clear the transaction buffer and abort on-going transfer. |
| Kojto | 98:8ab26030e058 | 157 | */ |
| Kojto | 98:8ab26030e058 | 158 | void abort_all_transfers(); |
| Kojto | 98:8ab26030e058 | 159 | |
| Kojto | 98:8ab26030e058 | 160 | /** Configure DMA usage suggestion for non-blocking transfers |
| Kojto | 98:8ab26030e058 | 161 | * |
| Kojto | 98:8ab26030e058 | 162 | * @param usage The usage DMA hint for peripheral |
| Kojto | 98:8ab26030e058 | 163 | * @return Zero if the usage was set, -1 if a transaction is on-going |
| Kojto | 98:8ab26030e058 | 164 | */ |
| Kojto | 98:8ab26030e058 | 165 | int set_dma_usage(DMAUsage usage); |
| Kojto | 98:8ab26030e058 | 166 | |
| Kojto | 98:8ab26030e058 | 167 | protected: |
| Kojto | 98:8ab26030e058 | 168 | /** SPI IRQ handler |
| Kojto | 98:8ab26030e058 | 169 | * |
| Kojto | 98:8ab26030e058 | 170 | */ |
| Kojto | 98:8ab26030e058 | 171 | void irq_handler_asynch(void); |
| Kojto | 98:8ab26030e058 | 172 | |
| Kojto | 98:8ab26030e058 | 173 | /** Common transfer method |
| Kojto | 98:8ab26030e058 | 174 | * |
| Kojto | 98:8ab26030e058 | 175 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
| Kojto | 98:8ab26030e058 | 176 | * the default SPI value is sent |
| Kojto | 102:da0ca467f8b5 | 177 | * @param tx_length The length of TX buffer in bytes |
| Kojto | 98:8ab26030e058 | 178 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
| Kojto | 98:8ab26030e058 | 179 | * received data are ignored |
| Kojto | 102:da0ca467f8b5 | 180 | * @param rx_length The length of RX buffer in bytes |
| Kojto | 98:8ab26030e058 | 181 | * @param bit_width The buffers element width |
| Kojto | 98:8ab26030e058 | 182 | * @param callback The event callback function |
| Kojto | 98:8ab26030e058 | 183 | * @param event The logical OR of events to modify |
| Kojto | 98:8ab26030e058 | 184 | * @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 | 185 | */ |
| Kojto | 102:da0ca467f8b5 | 186 | 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 | 187 | |
| Kojto | 98:8ab26030e058 | 188 | /** |
| Kojto | 98:8ab26030e058 | 189 | * |
| Kojto | 98:8ab26030e058 | 190 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
| Kojto | 98:8ab26030e058 | 191 | * the default SPI value is sent |
| Kojto | 102:da0ca467f8b5 | 192 | * @param tx_length The length of TX buffer in bytes |
| Kojto | 98:8ab26030e058 | 193 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
| Kojto | 98:8ab26030e058 | 194 | * received data are ignored |
| Kojto | 102:da0ca467f8b5 | 195 | * @param rx_length The length of RX buffer in bytes |
| Kojto | 98:8ab26030e058 | 196 | * @param bit_width The buffers element width |
| Kojto | 98:8ab26030e058 | 197 | * @param callback The event callback function |
| Kojto | 98:8ab26030e058 | 198 | * @param event The logical OR of events to modify |
| Kojto | 98:8ab26030e058 | 199 | * @return Zero if a transfer was added to the queue, or -1 if the queue is full |
| Kojto | 98:8ab26030e058 | 200 | */ |
| Kojto | 102:da0ca467f8b5 | 201 | 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 | 202 | |
| Kojto | 98:8ab26030e058 | 203 | /** Configures a callback, spi peripheral and initiate a new transfer |
| Kojto | 98:8ab26030e058 | 204 | * |
| Kojto | 98:8ab26030e058 | 205 | * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, |
| Kojto | 98:8ab26030e058 | 206 | * the default SPI value is sent |
| Kojto | 102:da0ca467f8b5 | 207 | * @param tx_length The length of TX buffer in bytes |
| Kojto | 98:8ab26030e058 | 208 | * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, |
| Kojto | 98:8ab26030e058 | 209 | * received data are ignored |
| Kojto | 102:da0ca467f8b5 | 210 | * @param rx_length The length of RX buffer in bytes |
| Kojto | 98:8ab26030e058 | 211 | * @param bit_width The buffers element width |
| Kojto | 98:8ab26030e058 | 212 | * @param callback The event callback function |
| Kojto | 98:8ab26030e058 | 213 | * @param event The logical OR of events to modify |
| Kojto | 98:8ab26030e058 | 214 | */ |
| Kojto | 102:da0ca467f8b5 | 215 | 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 | 216 | |
| Kojto | 98:8ab26030e058 | 217 | #if TRANSACTION_QUEUE_SIZE_SPI |
| Kojto | 98:8ab26030e058 | 218 | |
| Kojto | 98:8ab26030e058 | 219 | /** Start a new transaction |
| Kojto | 98:8ab26030e058 | 220 | * |
| Kojto | 98:8ab26030e058 | 221 | * @param data Transaction data |
| Kojto | 98:8ab26030e058 | 222 | */ |
| Kojto | 98:8ab26030e058 | 223 | void start_transaction(transaction_t *data); |
| Kojto | 98:8ab26030e058 | 224 | |
| Kojto | 98:8ab26030e058 | 225 | /** Dequeue a transaction |
| Kojto | 98:8ab26030e058 | 226 | * |
| Kojto | 98:8ab26030e058 | 227 | */ |
| Kojto | 98:8ab26030e058 | 228 | void dequeue_transaction(); |
| Kojto | 98:8ab26030e058 | 229 | static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer; |
| Kojto | 98:8ab26030e058 | 230 | #endif |
| Kojto | 98:8ab26030e058 | 231 | |
| Kojto | 98:8ab26030e058 | 232 | #endif |
| Kojto | 98:8ab26030e058 | 233 | |
| bogdanm | 85:024bf7f99721 | 234 | public: |
| bogdanm | 85:024bf7f99721 | 235 | virtual ~SPI() { |
| bogdanm | 85:024bf7f99721 | 236 | } |
| bogdanm | 85:024bf7f99721 | 237 | |
| bogdanm | 65:5798e58a58b1 | 238 | protected: |
| bogdanm | 65:5798e58a58b1 | 239 | spi_t _spi; |
| bogdanm | 65:5798e58a58b1 | 240 | |
| Kojto | 98:8ab26030e058 | 241 | #if DEVICE_SPI_ASYNCH |
| Kojto | 98:8ab26030e058 | 242 | CThunk<SPI> _irq; |
| Kojto | 98:8ab26030e058 | 243 | event_callback_t _callback; |
| Kojto | 98:8ab26030e058 | 244 | DMAUsage _usage; |
| Kojto | 98:8ab26030e058 | 245 | #endif |
| Kojto | 98:8ab26030e058 | 246 | |
| bogdanm | 65:5798e58a58b1 | 247 | void aquire(void); |
| bogdanm | 65:5798e58a58b1 | 248 | static SPI *_owner; |
| Kojto | 122:f9eeca106725 | 249 | PlatformMutex _mutex; |
| bogdanm | 65:5798e58a58b1 | 250 | int _bits; |
| bogdanm | 65:5798e58a58b1 | 251 | int _mode; |
| bogdanm | 65:5798e58a58b1 | 252 | int _hz; |
| bogdanm | 65:5798e58a58b1 | 253 | }; |
| bogdanm | 65:5798e58a58b1 | 254 | |
| bogdanm | 65:5798e58a58b1 | 255 | } // namespace mbed |
| bogdanm | 65:5798e58a58b1 | 256 | |
| bogdanm | 65:5798e58a58b1 | 257 | #endif |
| bogdanm | 65:5798e58a58b1 | 258 | |
| bogdanm | 65:5798e58a58b1 | 259 | #endif |


