initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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