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