SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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