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