dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
cyberjoey
Date:
Sat Oct 22 01:31:58 2016 +0000
Revision:
9:6bb35cef007d
Parent:
1:55a6170b404f
WORKING

Who changed what in which revision?

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