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