OK

Fork of mbed-dev by mbed official

Committer:
screamer
Date:
Tue Aug 02 14:07:36 2016 +0000
Revision:
144:423e1876dc07
Parent:
0:9b334a45a8ff
Added targets.json file for the supported targets in the release

Who changed what in which revision?

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