.

Dependents:   RTC

Committer:
jhon309
Date:
Thu Aug 13 00:20:09 2015 +0000
Revision:
0:88e313c910d0
RTC Example

Who changed what in which revision?

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