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