mbed library sources

Dependents:   Freedman_v2 Nucleo_i2c_OLED_BME280_copy

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed May 27 13:30:08 2015 +0100
Revision:
552:a1b9575155a3
Parent:
525:c320967f86b9
Child:
563:536c9fb088a0
Synchronized with git revision a74a8f14fd8c4bf3dc09980c4bf9498ebcf4c207

Full URL: https://github.com/mbedmicro/mbed/commit/a74a8f14fd8c4bf3dc09980c4bf9498ebcf4c207/

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 525:c320967f86b9 118 * @param tx_length The length of TX buffer
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 525:c320967f86b9 121 * @param rx_length The length of RX buffer
mbed_official 525:c320967f86b9 122 * @param callback The event callback function
mbed_official 525:c320967f86b9 123 * @param event The logical OR of events to modify
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 525:c320967f86b9 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) {
mbed_official 525:c320967f86b9 127 return transfer(tx_buffer, tx_length, rx_buffer, rx_length, 8, callback, event);
mbed_official 525:c320967f86b9 128 }
mbed_official 525:c320967f86b9 129
mbed_official 525:c320967f86b9 130 /** Start non-blocking SPI transfer using 16bit buffers.
mbed_official 525:c320967f86b9 131 *
mbed_official 525:c320967f86b9 132 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
mbed_official 525:c320967f86b9 133 * the default SPI value is sent
mbed_official 525:c320967f86b9 134 * @param tx_length The length of TX buffer
mbed_official 525:c320967f86b9 135 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
mbed_official 525:c320967f86b9 136 * received data are ignored
mbed_official 525:c320967f86b9 137 * @param rx_length The length of RX buffer
mbed_official 525:c320967f86b9 138 * @param callback The event callback function
mbed_official 525:c320967f86b9 139 * @param event The logical OR of events to modify
mbed_official 525:c320967f86b9 140 * @return Zero if the transfer has started, or -1 if SPI peripheral is busy
mbed_official 525:c320967f86b9 141 */
mbed_official 525:c320967f86b9 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) {
mbed_official 525:c320967f86b9 143 return transfer(tx_buffer, tx_length, rx_buffer, rx_length, 16, callback, event);
mbed_official 525:c320967f86b9 144 }
mbed_official 525:c320967f86b9 145
mbed_official 525:c320967f86b9 146 /** Start non-blocking SPI transfer using 32bit buffers.
mbed_official 525:c320967f86b9 147 *
mbed_official 525:c320967f86b9 148 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
mbed_official 525:c320967f86b9 149 * the default SPI value is sent
mbed_official 525:c320967f86b9 150 * @param tx_length The length of TX buffer
mbed_official 525:c320967f86b9 151 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
mbed_official 525:c320967f86b9 152 * received data are ignored
mbed_official 525:c320967f86b9 153 * @param rx_length The length of RX buffer
mbed_official 525:c320967f86b9 154 * @param callback The event callback function
mbed_official 525:c320967f86b9 155 * @param event The logical OR of events to modify
mbed_official 525:c320967f86b9 156 * @return Zero if the transfer has started, or -1 if SPI peripheral is busy
mbed_official 525:c320967f86b9 157 */
mbed_official 525:c320967f86b9 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) {
mbed_official 525:c320967f86b9 159 return transfer((void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, 32, callback, event);
mbed_official 525:c320967f86b9 160 }
mbed_official 525:c320967f86b9 161
mbed_official 525:c320967f86b9 162 /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any.
mbed_official 525:c320967f86b9 163 */
mbed_official 525:c320967f86b9 164 void abort_transfer();
mbed_official 525:c320967f86b9 165
mbed_official 525:c320967f86b9 166 /** Clear the transaction buffer
mbed_official 525:c320967f86b9 167 */
mbed_official 525:c320967f86b9 168 void clear_transfer_buffer();
mbed_official 525:c320967f86b9 169
mbed_official 525:c320967f86b9 170 /** Clear the transaction buffer and abort on-going transfer.
mbed_official 525:c320967f86b9 171 */
mbed_official 525:c320967f86b9 172 void abort_all_transfers();
mbed_official 525:c320967f86b9 173
mbed_official 525:c320967f86b9 174 /** Configure DMA usage suggestion for non-blocking transfers
mbed_official 525:c320967f86b9 175 *
mbed_official 525:c320967f86b9 176 * @param usage The usage DMA hint for peripheral
mbed_official 525:c320967f86b9 177 * @return Zero if the usage was set, -1 if a transaction is on-going
mbed_official 525:c320967f86b9 178 */
mbed_official 525:c320967f86b9 179 int set_dma_usage(DMAUsage usage);
mbed_official 525:c320967f86b9 180
mbed_official 525:c320967f86b9 181 protected:
mbed_official 525:c320967f86b9 182 /** SPI IRQ handler
mbed_official 525:c320967f86b9 183 *
mbed_official 525:c320967f86b9 184 */
mbed_official 525:c320967f86b9 185 void irq_handler_asynch(void);
mbed_official 525:c320967f86b9 186
mbed_official 525:c320967f86b9 187 /** Common transfer method
mbed_official 525:c320967f86b9 188 *
mbed_official 525:c320967f86b9 189 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
mbed_official 525:c320967f86b9 190 * the default SPI value is sent
mbed_official 525:c320967f86b9 191 * @param tx_length The length of TX buffer
mbed_official 525:c320967f86b9 192 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
mbed_official 525:c320967f86b9 193 * received data are ignored
mbed_official 525:c320967f86b9 194 * @param rx_length The length of RX buffer
mbed_official 525:c320967f86b9 195 * @param bit_width The buffers element width
mbed_official 525:c320967f86b9 196 * @param callback The event callback function
mbed_official 525:c320967f86b9 197 * @param event The logical OR of events to modify
mbed_official 525:c320967f86b9 198 * @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 199 */
mbed_official 525:c320967f86b9 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);
mbed_official 525:c320967f86b9 201
mbed_official 525:c320967f86b9 202 /**
mbed_official 525:c320967f86b9 203 *
mbed_official 525:c320967f86b9 204 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
mbed_official 525:c320967f86b9 205 * the default SPI value is sent
mbed_official 525:c320967f86b9 206 * @param tx_length The length of TX buffer
mbed_official 525:c320967f86b9 207 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
mbed_official 525:c320967f86b9 208 * received data are ignored
mbed_official 525:c320967f86b9 209 * @param rx_length The length of RX buffer
mbed_official 525:c320967f86b9 210 * @param bit_width The buffers element width
mbed_official 525:c320967f86b9 211 * @param callback The event callback function
mbed_official 525:c320967f86b9 212 * @param event The logical OR of events to modify
mbed_official 525:c320967f86b9 213 * @return Zero if a transfer was added to the queue, or -1 if the queue is full
mbed_official 525:c320967f86b9 214 */
mbed_official 525:c320967f86b9 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);
mbed_official 525:c320967f86b9 216
mbed_official 525:c320967f86b9 217 /** Configures a callback, spi peripheral and initiate a new transfer
mbed_official 525:c320967f86b9 218 *
mbed_official 525:c320967f86b9 219 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
mbed_official 525:c320967f86b9 220 * the default SPI value is sent
mbed_official 525:c320967f86b9 221 * @param tx_length The length of TX buffer
mbed_official 525:c320967f86b9 222 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
mbed_official 525:c320967f86b9 223 * received data are ignored
mbed_official 525:c320967f86b9 224 * @param rx_length The length of RX buffer
mbed_official 525:c320967f86b9 225 * @param bit_width The buffers element width
mbed_official 525:c320967f86b9 226 * @param callback The event callback function
mbed_official 525:c320967f86b9 227 * @param event The logical OR of events to modify
mbed_official 525:c320967f86b9 228 */
mbed_official 525:c320967f86b9 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);
mbed_official 525:c320967f86b9 230
mbed_official 525:c320967f86b9 231 #if TRANSACTION_QUEUE_SIZE_SPI
mbed_official 525:c320967f86b9 232
mbed_official 525:c320967f86b9 233 /** Start a new transaction
mbed_official 525:c320967f86b9 234 *
mbed_official 525:c320967f86b9 235 * @param data Transaction data
mbed_official 525:c320967f86b9 236 */
mbed_official 525:c320967f86b9 237 void start_transaction(transaction_t *data);
mbed_official 525:c320967f86b9 238
mbed_official 525:c320967f86b9 239 /** Dequeue a transaction
mbed_official 525:c320967f86b9 240 *
mbed_official 525:c320967f86b9 241 */
mbed_official 525:c320967f86b9 242 void dequeue_transaction();
mbed_official 525:c320967f86b9 243 static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer;
mbed_official 525:c320967f86b9 244 #endif
mbed_official 525:c320967f86b9 245
mbed_official 525:c320967f86b9 246 #endif
mbed_official 525:c320967f86b9 247
mbed_official 212:34d62c0b2af6 248 public:
mbed_official 212:34d62c0b2af6 249 virtual ~SPI() {
mbed_official 212:34d62c0b2af6 250 }
mbed_official 212:34d62c0b2af6 251
bogdanm 13:0645d8841f51 252 protected:
bogdanm 13:0645d8841f51 253 spi_t _spi;
bogdanm 13:0645d8841f51 254
mbed_official 525:c320967f86b9 255 #if DEVICE_SPI_ASYNCH
mbed_official 525:c320967f86b9 256 CThunk<SPI> _irq;
mbed_official 525:c320967f86b9 257 event_callback_t _callback;
mbed_official 525:c320967f86b9 258 DMAUsage _usage;
mbed_official 525:c320967f86b9 259 #endif
mbed_official 525:c320967f86b9 260
bogdanm 13:0645d8841f51 261 void aquire(void);
bogdanm 13:0645d8841f51 262 static SPI *_owner;
bogdanm 13:0645d8841f51 263 int _bits;
bogdanm 13:0645d8841f51 264 int _mode;
bogdanm 13:0645d8841f51 265 int _hz;
bogdanm 13:0645d8841f51 266 };
bogdanm 13:0645d8841f51 267
bogdanm 13:0645d8841f51 268 } // namespace mbed
bogdanm 13:0645d8841f51 269
bogdanm 13:0645d8841f51 270 #endif
bogdanm 13:0645d8841f51 271
bogdanm 13:0645d8841f51 272 #endif