Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

Who changed what in which revision?

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