Dataloger

Committer:
jhon309
Date:
Thu Aug 20 00:37:14 2015 +0000
Revision:
0:666850d06c9f
ok

Who changed what in which revision?

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