mbed library sources

Fork of mbed-src by mbed official

Committer:
Baycken
Date:
Fri Jul 03 17:02:48 2015 +0000
Revision:
582:ed68b5b3ef6d
Parent:
564:24a7119bd73a
Disable Hardware Flow Control

Who changed what in which revision?

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