mbed.h library with any bug fixes AV finds.

Dependents:   micromouse4_encoder_testing PID_Test Lab1_Test WorkingPID ... more

Committer:
aravindsv
Date:
Mon Nov 02 03:07:12 2015 +0000
Revision:
1:ebce2ad32f95
Parent:
0:ba7650f404af
Changed the RCC timeout value to 500 ms, so total code startup time before program starts running is ~1s. Hopefully no side-effects from lower startup timeouts

Who changed what in which revision?

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