Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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