mbed library for slider v2

Dependents:   kl46z_slider_v2

Committer:
mturner5
Date:
Wed Sep 14 07:04:27 2016 +0000
Revision:
0:b7116bd48af6
Tried to use the timer.

Who changed what in which revision?

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