Fork of the official mbed C/C SDK provides the software platform and libraries to build your applications for RenBED.

Dependents:   1-RenBuggyTimed RenBED_RGB RenBED_RGB_PWM RenBED_RGB

Fork of mbed by mbed official

Committer:
elijahorr
Date:
Thu Apr 14 07:28:54 2016 +0000
Revision:
121:672067c3ada4
Parent:
102:da0ca467f8b5
.

Who changed what in which revision?

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