the do / gr-peach-opencv-project

Fork of gr-peach-opencv-project by the do

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPI.h Source File

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 defined (DEVICE_SPI) || defined(DOXYGEN_ONLY)
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 /** A SPI Master, used for communicating with SPI slave devices
00039  *
00040  * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
00041  *
00042  * Most SPI devices will also require Chip Select and Reset signals. These
00043  * can be controlled using DigitalOut pins
00044  *
00045  * @note Synchronization level: Thread safe
00046  *
00047  * Example:
00048  * @code
00049  * // Send a byte to a SPI slave, and record the response
00050  *
00051  * #include "mbed.h"
00052  *
00053  * // hardware ssel (where applicable)
00054  * //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
00055  *
00056  * // software ssel
00057  * SPI device(p5, p6, p7); // mosi, miso, sclk
00058  * DigitalOut cs(p8); // ssel
00059  *
00060  * int main() {
00061  *     // hardware ssel (where applicable)
00062  *     //int response = device.write(0xFF);
00063  *
00064  *     device.lock();
00065  *     // software ssel
00066  *     cs = 0;
00067  *     int response = device.write(0xFF);
00068  *     cs = 1;
00069  *     device.unlock();
00070  *
00071  * }
00072  * @endcode
00073  * @ingroup drivers
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     /** Write to the SPI Slave and obtain the response
00122      *
00123      *  The total number of bytes sent and recieved will be the maximum of
00124      *  tx_length and rx_length. The bytes written will be padded with the
00125      *  value 0xff.
00126      *
00127      *  @param tx_buffer Pointer to the byte-array of data to write to the device
00128      *  @param tx_length Number of bytes to write, may be zero
00129      *  @param rx_buffer Pointer to the byte-array of data to read from the device
00130      *  @param rx_length Number of bytes to read, may be zero
00131      *  @returns
00132      *      The number of bytes written and read from the device. This is
00133      *      maximum of tx_length and rx_length.
00134      */
00135     virtual int write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length);
00136 
00137     /** Acquire exclusive access to this SPI bus
00138      */
00139     virtual void lock(void);
00140 
00141     /** Release exclusive access to this SPI bus
00142      */
00143     virtual void unlock(void);
00144 
00145 #if DEVICE_SPI_ASYNCH
00146 
00147     /** Start non-blocking SPI transfer using 8bit buffers.
00148      *
00149      * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
00150      *                  the default SPI value is sent
00151      * @param tx_length The length of TX buffer in bytes
00152      * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
00153      *                  received data are ignored
00154      * @param rx_length The length of RX buffer in bytes
00155      * @param callback  The event callback function
00156      * @param event     The logical OR of events to modify. Look at spi hal header file for SPI events.
00157      * @return Zero if the transfer has started, or -1 if SPI peripheral is busy
00158      */
00159     template<typename Type>
00160     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) {
00161         if (spi_active(&_spi)) {
00162             return queue_transfer (tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
00163         }
00164         start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
00165         return 0;
00166     }
00167 
00168     /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any.
00169      */
00170     void abort_transfer();
00171 
00172     /** Clear the transaction buffer
00173      */
00174     void clear_transfer_buffer();
00175 
00176     /** Clear the transaction buffer and abort on-going transfer.
00177      */
00178     void abort_all_transfers();
00179 
00180     /** Configure DMA usage suggestion for non-blocking transfers
00181      *
00182      *  @param usage The usage DMA hint for peripheral
00183      *  @return Zero if the usage was set, -1 if a transaction is on-going
00184     */
00185     int set_dma_usage(DMAUsage usage);
00186 
00187 protected:
00188     /** SPI IRQ handler
00189      *
00190     */
00191     void irq_handler_asynch(void);
00192 
00193     /** Common transfer method
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 in bytes
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 in bytes
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 the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full
00205     */
00206     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);
00207 
00208     /**
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 in bytes
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 in bytes
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      * @return Zero if a transfer was added to the queue, or -1 if the queue is full
00220     */
00221     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);
00222 
00223     /** Configures a callback, spi peripheral and initiate a new transfer
00224      *
00225      * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
00226      *                  the default SPI value is sent
00227      * @param tx_length The length of TX buffer in bytes
00228      * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
00229      *                  received data are ignored
00230      * @param rx_length The length of RX buffer in bytes
00231      * @param bit_width The buffers element width
00232      * @param callback  The event callback function
00233      * @param event     The logical OR of events to modify
00234     */
00235     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);
00236 
00237 #if TRANSACTION_QUEUE_SIZE_SPI
00238 
00239     /** Start a new transaction
00240      *
00241      *  @param data Transaction data
00242     */
00243     void start_transaction(transaction_t *data);
00244 
00245     /** Dequeue a transaction
00246      *
00247     */
00248     void dequeue_transaction();
00249     static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer;
00250 #endif
00251 
00252 #endif
00253 
00254 public:
00255     virtual ~SPI() {
00256     }
00257 
00258 protected:
00259     spi_t _spi;
00260 
00261 #if DEVICE_SPI_ASYNCH
00262     CThunk<SPI>  _irq;
00263     event_callback_t _callback;
00264     DMAUsage _usage;
00265 #endif
00266 
00267     void aquire(void);
00268     static SPI *_owner;
00269     static SingletonPtr<PlatformMutex>  _mutex;
00270     int _bits;
00271     int _mode;
00272     int _hz;
00273 };
00274 
00275 } // namespace mbed
00276 
00277 #endif
00278 
00279 #endif
00280