mbed library for NZ32-SC151

Committer:
modtronix
Date:
Fri Jul 24 21:01:44 2015 +1000
Revision:
1:71204b8406f2
Current mbed v103 (594)

Who changed what in which revision?

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