mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
Embed:
(wiki syntax)
Show/hide line numbers
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/platform.h" 00020 00021 #if DEVICE_SPI 00022 00023 #include "platform/PlatformMutex.h" 00024 #include "hal/spi_api.h" 00025 #include "platform/SingletonPtr.h" 00026 00027 #if DEVICE_SPI_ASYNCH 00028 #include "platform/CThunk.h" 00029 #include "hal/dma_api.h" 00030 #include "platform/CircularBuffer.h" 00031 #include "platform/FunctionPointer.h" 00032 #include "platform/Transaction.h" 00033 #endif 00034 00035 namespace mbed { 00036 /** \addtogroup drivers */ 00037 /** @{*/ 00038 00039 /** A SPI Master, used for communicating with SPI slave devices 00040 * 00041 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz 00042 * 00043 * Most SPI devices will also require Chip Select and Reset signals. These 00044 * can be controlled using <DigitalOut> pins 00045 * 00046 * @Note Synchronization level: Thread safe 00047 * 00048 * Example: 00049 * @code 00050 * // Send a byte to a SPI slave, and record the response 00051 * 00052 * #include "mbed.h" 00053 * 00054 * // hardware ssel (where applicable) 00055 * //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel 00056 * 00057 * // software ssel 00058 * SPI device(p5, p6, p7); // mosi, miso, sclk 00059 * DigitalOut cs(p8); // ssel 00060 * 00061 * int main() { 00062 * // hardware ssel (where applicable) 00063 * //int response = device.write(0xFF); 00064 * 00065 * device.lock(); 00066 * // software ssel 00067 * cs = 0; 00068 * int response = device.write(0xFF); 00069 * cs = 1; 00070 * device.unlock(); 00071 * 00072 * } 00073 * @endcode 00074 */ 00075 class SPI { 00076 00077 public: 00078 00079 /** Create a SPI master connected to the specified pins 00080 * 00081 * mosi or miso can be specfied as NC if not used 00082 * 00083 * @param mosi SPI Master Out, Slave In pin 00084 * @param miso SPI Master In, Slave Out pin 00085 * @param sclk SPI Clock pin 00086 * @param ssel SPI chip select pin 00087 */ 00088 SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC); 00089 00090 /** Configure the data transmission format 00091 * 00092 * @param bits Number of bits per SPI frame (4 - 16) 00093 * @param mode Clock polarity and phase mode (0 - 3) 00094 * 00095 * @code 00096 * mode | POL PHA 00097 * -----+-------- 00098 * 0 | 0 0 00099 * 1 | 0 1 00100 * 2 | 1 0 00101 * 3 | 1 1 00102 * @endcode 00103 */ 00104 void format(int bits, int mode = 0); 00105 00106 /** Set the spi bus clock frequency 00107 * 00108 * @param hz SCLK frequency in hz (default = 1MHz) 00109 */ 00110 void frequency(int hz = 1000000); 00111 00112 /** Write to the SPI Slave and return the response 00113 * 00114 * @param value Data to be sent to the SPI slave 00115 * 00116 * @returns 00117 * Response from the SPI slave 00118 */ 00119 virtual int write(int value); 00120 00121 /** Acquire exclusive access to this SPI bus 00122 */ 00123 virtual void lock(void); 00124 00125 /** Release exclusive access to this SPI bus 00126 */ 00127 virtual void unlock(void); 00128 00129 #if DEVICE_SPI_ASYNCH 00130 00131 /** Start non-blocking SPI transfer using 8bit buffers. 00132 * 00133 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00134 * the default SPI value is sent 00135 * @param tx_length The length of TX buffer in bytes 00136 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00137 * received data are ignored 00138 * @param rx_length The length of RX buffer in bytes 00139 * @param callback The event callback function 00140 * @param event The logical OR of events to modify. Look at spi hal header file for SPI events. 00141 * @return Zero if the transfer has started, or -1 if SPI peripheral is busy 00142 */ 00143 template<typename Type> 00144 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) { 00145 if (spi_active(&_spi)) { 00146 return queue_transfer (tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); 00147 } 00148 start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); 00149 return 0; 00150 } 00151 00152 /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any. 00153 */ 00154 void abort_transfer(); 00155 00156 /** Clear the transaction buffer 00157 */ 00158 void clear_transfer_buffer(); 00159 00160 /** Clear the transaction buffer and abort on-going transfer. 00161 */ 00162 void abort_all_transfers(); 00163 00164 /** Configure DMA usage suggestion for non-blocking transfers 00165 * 00166 * @param usage The usage DMA hint for peripheral 00167 * @return Zero if the usage was set, -1 if a transaction is on-going 00168 */ 00169 int set_dma_usage(DMAUsage usage); 00170 00171 protected: 00172 /** SPI IRQ handler 00173 * 00174 */ 00175 void irq_handler_asynch(void); 00176 00177 /** Common transfer method 00178 * 00179 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00180 * the default SPI value is sent 00181 * @param tx_length The length of TX buffer in bytes 00182 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00183 * received data are ignored 00184 * @param rx_length The length of RX buffer in bytes 00185 * @param bit_width The buffers element width 00186 * @param callback The event callback function 00187 * @param event The logical OR of events to modify 00188 * @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full 00189 */ 00190 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); 00191 00192 /** 00193 * 00194 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00195 * the default SPI value is sent 00196 * @param tx_length The length of TX buffer in bytes 00197 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00198 * received data are ignored 00199 * @param rx_length The length of RX buffer in bytes 00200 * @param bit_width The buffers element width 00201 * @param callback The event callback function 00202 * @param event The logical OR of events to modify 00203 * @return Zero if a transfer was added to the queue, or -1 if the queue is full 00204 */ 00205 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); 00206 00207 /** Configures a callback, spi peripheral and initiate a new transfer 00208 * 00209 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, 00210 * the default SPI value is sent 00211 * @param tx_length The length of TX buffer in bytes 00212 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed, 00213 * received data are ignored 00214 * @param rx_length The length of RX buffer in bytes 00215 * @param bit_width The buffers element width 00216 * @param callback The event callback function 00217 * @param event The logical OR of events to modify 00218 */ 00219 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); 00220 00221 #if TRANSACTION_QUEUE_SIZE_SPI 00222 00223 /** Start a new transaction 00224 * 00225 * @param data Transaction data 00226 */ 00227 void start_transaction(transaction_t *data); 00228 00229 /** Dequeue a transaction 00230 * 00231 */ 00232 void dequeue_transaction(); 00233 static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer; 00234 #endif 00235 00236 #endif 00237 00238 public: 00239 virtual ~SPI() { 00240 } 00241 00242 protected: 00243 spi_t _spi; 00244 00245 #if DEVICE_SPI_ASYNCH 00246 CThunk<SPI> _irq; 00247 event_callback_t _callback; 00248 DMAUsage _usage; 00249 #endif 00250 00251 void aquire(void); 00252 static SPI *_owner; 00253 static SingletonPtr<PlatformMutex> _mutex; 00254 int _bits; 00255 int _mode; 00256 int _hz; 00257 }; 00258 00259 } // namespace mbed 00260 00261 #endif 00262 00263 #endif 00264 00265 /** @}*/
Generated on Tue Jul 12 2022 23:05:49 by 1.7.2