CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
0:aa13e1c335cd
debug

Who changed what in which revision?

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