Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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