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