Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed by
SPI.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_SPI_H 00017 #define MBED_SPI_H 00018 00019 #include "platform.h" 00020 00021 #if DEVICE_SPI 00022 00023 #include "spi_api.h" 00024 00025 #if DEVICE_SPI_ASYNCH 00026 #include "CThunk.h" 00027 #include "dma_api.h" 00028 #include "CircularBuffer.h" 00029 #include "FunctionPointer.h" 00030 #include "Transaction.h" 00031 #endif 00032 00033 namespace mbed { 00034 00035 /** A SPI Master, used for communicating with SPI slave devices 00036 * 00037 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz 00038 * 00039 * Most SPI devices will also require Chip Select and Reset signals. These 00040 * can be controlled using <DigitalOut> pins 00041 * 00042 * Example: 00043 * @code 00044 * // Send a byte to a SPI slave, and record the response 00045 * 00046 * #include "mbed.h" 00047 * 00048 * SPI device(p5, p6, p7); // mosi, miso, sclk 00049 * 00050 * int main() { 00051 * int response = device.write(0xFF); 00052 * } 00053 * @endcode 00054 */ 00055 class SPI { 00056 00057 public: 00058 00059 /** Create a SPI master connected to the specified pins 00060 * 00061 * Pin Options: 00062 * (5, 6, 7) or (11, 12, 13) 00063 * 00064 * mosi or miso can be specfied as NC if not used 00065 * 00066 * @param mosi SPI Master Out, Slave In pin 00067 * @param miso SPI Master In, Slave Out pin 00068 * @param sclk SPI Clock pin 00069 */ 00070 SPI(PinName mosi, PinName miso, PinName sclk, PinName _unused=NC); 00071 00072 /** Configure the data transmission format 00073 * 00074 * @param bits Number of bits per SPI frame (4 - 16) 00075 * @param mode Clock polarity and phase mode (0 - 3) 00076 * 00077 * @code 00078 * mode | POL PHA 00079 * -----+-------- 00080 * 0 | 0 0 00081 * 1 | 0 1 00082 * 2 | 1 0 00083 * 3 | 1 1 00084 * @endcode 00085 */ 00086 void format(int bits, int mode = 0); 00087 00088 /** Set the spi bus clock frequency 00089 * 00090 * @param hz SCLK frequency in hz (default = 1MHz) 00091 */ 00092 void frequency(int hz = 1000000); 00093 00094 /** Write to the SPI Slave and return the response 00095 * 00096 * @param value Data to be sent to the SPI slave 00097 * 00098 * @returns 00099 * Response from the SPI slave 00100 */ 00101 virtual int write(int value); 00102 00103 #if DEVICE_SPI_ASYNCH 00104 00105 /** Start non-blocking SPI transfer using 8bit buffers. 00106 * 00107 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00108 * the default SPI value is sent 00109 * @param tx_length The length of TX buffer 00110 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00111 * received data are ignored 00112 * @param rx_length The length of RX buffer 00113 * @param callback The event callback function 00114 * @param event The logical OR of events to modify 00115 * @return Zero if the transfer has started, or -1 if SPI peripheral is busy 00116 */ 00117 virtual int transfer(uint8_t *tx_buffer, int tx_length, uint8_t *rx_buffer, int rx_length, const event_callback_t & callback, int event = SPI_EVENT_COMPLETE) { 00118 return transfer(tx_buffer, tx_length, rx_buffer, rx_length, 8, callback, event); 00119 } 00120 00121 /** Start non-blocking SPI transfer using 16bit buffers. 00122 * 00123 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00124 * the default SPI value is sent 00125 * @param tx_length The length of TX buffer 00126 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00127 * received data are ignored 00128 * @param rx_length The length of RX buffer 00129 * @param callback The event callback function 00130 * @param event The logical OR of events to modify 00131 * @return Zero if the transfer has started, or -1 if SPI peripheral is busy 00132 */ 00133 virtual int transfer(uint16_t *tx_buffer, int tx_length, uint16_t *rx_buffer, int rx_length, const event_callback_t & callback, int event = SPI_EVENT_COMPLETE) { 00134 return transfer(tx_buffer, tx_length, rx_buffer, rx_length, 16, callback, event); 00135 } 00136 00137 /** Start non-blocking SPI transfer using 32bit buffers. 00138 * 00139 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00140 * the default SPI value is sent 00141 * @param tx_length The length of TX buffer 00142 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00143 * received data are ignored 00144 * @param rx_length The length of RX buffer 00145 * @param callback The event callback function 00146 * @param event The logical OR of events to modify 00147 * @return Zero if the transfer has started, or -1 if SPI peripheral is busy 00148 */ 00149 virtual int transfer(uint32_t *tx_buffer, int tx_length, uint32_t *rx_buffer, int rx_length, const event_callback_t & callback, int event = SPI_EVENT_COMPLETE) { 00150 return transfer((void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, 32, callback, event); 00151 } 00152 00153 /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any. 00154 */ 00155 void abort_transfer(); 00156 00157 /** Clear the transaction buffer 00158 */ 00159 void clear_transfer_buffer(); 00160 00161 /** Clear the transaction buffer and abort on-going transfer. 00162 */ 00163 void abort_all_transfers(); 00164 00165 /** Configure DMA usage suggestion for non-blocking transfers 00166 * 00167 * @param usage The usage DMA hint for peripheral 00168 * @return Zero if the usage was set, -1 if a transaction is on-going 00169 */ 00170 int set_dma_usage(DMAUsage usage); 00171 00172 protected: 00173 /** SPI IRQ handler 00174 * 00175 */ 00176 void irq_handler_asynch(void); 00177 00178 /** Common transfer method 00179 * 00180 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00181 * the default SPI value is sent 00182 * @param tx_length The length of TX buffer 00183 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00184 * received data are ignored 00185 * @param rx_length The length of RX buffer 00186 * @param bit_width The buffers element width 00187 * @param callback The event callback function 00188 * @param event The logical OR of events to modify 00189 * @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full 00190 */ 00191 int transfer(void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t & callback, int event); 00192 00193 /** 00194 * 00195 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00196 * the default SPI value is sent 00197 * @param tx_length The length of TX buffer 00198 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00199 * received data are ignored 00200 * @param rx_length The length of RX buffer 00201 * @param bit_width The buffers element width 00202 * @param callback The event callback function 00203 * @param event The logical OR of events to modify 00204 * @return Zero if a transfer was added to the queue, or -1 if the queue is full 00205 */ 00206 int queue_transfer (void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t & callback, int event); 00207 00208 /** Configures a callback, spi peripheral and initiate a new transfer 00209 * 00210 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00211 * the default SPI value is sent 00212 * @param tx_length The length of TX buffer 00213 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00214 * received data are ignored 00215 * @param rx_length The length of RX buffer 00216 * @param bit_width The buffers element width 00217 * @param callback The event callback function 00218 * @param event The logical OR of events to modify 00219 */ 00220 void start_transfer(void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t & callback, int event); 00221 00222 #if TRANSACTION_QUEUE_SIZE_SPI 00223 00224 /** Start a new transaction 00225 * 00226 * @param data Transaction data 00227 */ 00228 void start_transaction(transaction_t *data); 00229 00230 /** Dequeue a transaction 00231 * 00232 */ 00233 void dequeue_transaction(); 00234 static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer; 00235 #endif 00236 00237 #endif 00238 00239 public: 00240 virtual ~SPI() { 00241 } 00242 00243 protected: 00244 spi_t _spi; 00245 00246 #if DEVICE_SPI_ASYNCH 00247 CThunk<SPI> _irq; 00248 event_callback_t _callback; 00249 DMAUsage _usage; 00250 #endif 00251 00252 void aquire(void); 00253 static SPI *_owner; 00254 int _bits; 00255 int _mode; 00256 int _hz; 00257 }; 00258 00259 } // namespace mbed 00260 00261 #endif 00262 00263 #endif
Generated on Tue Jul 12 2022 19:00:10 by
1.7.2
