João Victor / stm32f4-discovery-CAN-Activation

Fork of mbed-dev by mbed official

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 #include "platform/NonCopyable.h"
00027 
00028 #if DEVICE_SPI_ASYNCH
00029 #include "platform/CThunk.h"
00030 #include "hal/dma_api.h"
00031 #include "platform/CircularBuffer.h"
00032 #include "platform/FunctionPointer.h"
00033 #include "platform/Transaction.h"
00034 #endif
00035 
00036 namespace mbed {
00037 /** \addtogroup drivers */
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  * @ingroup drivers
00075  */
00076 class SPI : private NonCopyable<SPI> {
00077 
00078 public:
00079 
00080     /** Create a SPI master connected to the specified pins
00081      *
00082      *  mosi or miso can be specfied as NC if not used
00083      *
00084      *  @param mosi SPI Master Out, Slave In pin
00085      *  @param miso SPI Master In, Slave Out pin
00086      *  @param sclk SPI Clock pin
00087      *  @param ssel SPI chip select pin
00088      */
00089     SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC);
00090 
00091     /** Configure the data transmission format
00092      *
00093      *  @param bits Number of bits per SPI frame (4 - 16)
00094      *  @param mode Clock polarity and phase mode (0 - 3)
00095      *
00096      * @code
00097      * mode | POL PHA
00098      * -----+--------
00099      *   0  |  0   0
00100      *   1  |  0   1
00101      *   2  |  1   0
00102      *   3  |  1   1
00103      * @endcode
00104      */
00105     void format(int bits, int mode = 0);
00106 
00107     /** Set the spi bus clock frequency
00108      *
00109      *  @param hz SCLK frequency in hz (default = 1MHz)
00110      */
00111     void frequency(int hz = 1000000);
00112 
00113     /** Write to the SPI Slave and return the response
00114      *
00115      *  @param value Data to be sent to the SPI slave
00116      *
00117      *  @returns
00118      *    Response from the SPI slave
00119      */
00120     virtual int write(int value);
00121 
00122     /** Write to the SPI Slave and obtain the response
00123      *
00124      *  The total number of bytes sent and recieved will be the maximum of
00125      *  tx_length and rx_length. The bytes written will be padded with the
00126      *  value 0xff.
00127      *
00128      *  @param tx_buffer Pointer to the byte-array of data to write to the device
00129      *  @param tx_length Number of bytes to write, may be zero
00130      *  @param rx_buffer Pointer to the byte-array of data to read from the device
00131      *  @param rx_length Number of bytes to read, may be zero
00132      *  @returns
00133      *      The number of bytes written and read from the device. This is
00134      *      maximum of tx_length and rx_length.
00135      */
00136     virtual int write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length);
00137 
00138     /** Acquire exclusive access to this SPI bus
00139      */
00140     virtual void lock(void);
00141 
00142     /** Release exclusive access to this SPI bus
00143      */
00144     virtual void unlock(void);
00145 
00146 #if DEVICE_SPI_ASYNCH
00147 
00148     /** Start non-blocking SPI transfer using 8bit buffers.
00149      *
00150      * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
00151      *                  the default SPI value is sent
00152      * @param tx_length The length of TX buffer in bytes
00153      * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
00154      *                  received data are ignored
00155      * @param rx_length The length of RX buffer in bytes
00156      * @param callback  The event callback function
00157      * @param event     The logical OR of events to modify. Look at spi hal header file for SPI events.
00158      * @return Zero if the transfer has started, or -1 if SPI peripheral is busy
00159      */
00160     template<typename Type>
00161     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) {
00162         if (spi_active(&_spi)) {
00163             return queue_transfer (tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
00164         }
00165         start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
00166         return 0;
00167     }
00168 
00169     /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any.
00170      */
00171     void abort_transfer();
00172 
00173     /** Clear the transaction buffer
00174      */
00175     void clear_transfer_buffer();
00176 
00177     /** Clear the transaction buffer and abort on-going transfer.
00178      */
00179     void abort_all_transfers();
00180 
00181     /** Configure DMA usage suggestion for non-blocking transfers
00182      *
00183      *  @param usage The usage DMA hint for peripheral
00184      *  @return Zero if the usage was set, -1 if a transaction is on-going
00185     */
00186     int set_dma_usage(DMAUsage usage);
00187 
00188 protected:
00189     /** SPI IRQ handler
00190      *
00191     */
00192     void irq_handler_asynch(void);
00193 
00194     /** Common transfer method
00195      *
00196      * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
00197      *                  the default SPI value is sent
00198      * @param tx_length The length of TX buffer in bytes
00199      * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
00200      *                  received data are ignored
00201      * @param rx_length The length of RX buffer in bytes
00202      * @param bit_width The buffers element width
00203      * @param callback  The event callback function
00204      * @param event     The logical OR of events to modify
00205      * @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full
00206     */
00207     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);
00208 
00209     /**
00210      *
00211      * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
00212      *                  the default SPI value is sent
00213      * @param tx_length The length of TX buffer in bytes
00214      * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
00215      *                  received data are ignored
00216      * @param rx_length The length of RX buffer in bytes
00217      * @param bit_width The buffers element width
00218      * @param callback  The event callback function
00219      * @param event     The logical OR of events to modify
00220      * @return Zero if a transfer was added to the queue, or -1 if the queue is full
00221     */
00222     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);
00223 
00224     /** Configures a callback, spi peripheral and initiate a new transfer
00225      *
00226      * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
00227      *                  the default SPI value is sent
00228      * @param tx_length The length of TX buffer in bytes
00229      * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
00230      *                  received data are ignored
00231      * @param rx_length The length of RX buffer in bytes
00232      * @param bit_width The buffers element width
00233      * @param callback  The event callback function
00234      * @param event     The logical OR of events to modify
00235     */
00236     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);
00237 
00238 #if TRANSACTION_QUEUE_SIZE_SPI
00239 
00240     /** Start a new transaction
00241      *
00242      *  @param data Transaction data
00243     */
00244     void start_transaction(transaction_t *data);
00245 
00246     /** Dequeue a transaction
00247      *
00248     */
00249     void dequeue_transaction();
00250     static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer;
00251 #endif
00252 
00253 #endif
00254 
00255 public:
00256     virtual ~SPI() {
00257     }
00258 
00259 protected:
00260     spi_t _spi;
00261 
00262 #if DEVICE_SPI_ASYNCH
00263     CThunk<SPI>  _irq;
00264     event_callback_t _callback;
00265     DMAUsage _usage;
00266 #endif
00267 
00268     void aquire(void);
00269     static SPI *_owner;
00270     static SingletonPtr<PlatformMutex>  _mutex;
00271     int _bits;
00272     int _mode;
00273     int _hz;
00274 
00275 private:
00276     /* Private acquire function without locking/unlocking
00277      * Implemented in order to avoid duplicate locking and boost performance
00278      */
00279     void _acquire(void);
00280 };
00281 
00282 } // namespace mbed
00283 
00284 #endif
00285 
00286 #endif