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