Dave Lu / FYDP_Final2

Dependencies:   Servo mbed

Fork of FYDP_Final2 by Mark Vandermeulen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SWSPI.h Source File

SWSPI.h

00001 /* SWSPI, Software SPI library
00002  * Copyright (c) 2012-2014, David R. Van Wagner, http://techwithdave.blogspot.com
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #ifndef SWSPI_H
00024 #define SWSPI_H
00025 
00026 /** A software implemented SPI that can use any digital pins
00027  *
00028  * Useful when don't want to share a single SPI hardware among attached devices
00029  * or when pinout doesn't match exactly to the target's SPI pins
00030  *
00031  * @code
00032  * #include "mbed.h"
00033  * #include "SWSPI.h"
00034  * 
00035  * SWSPI spi(p5, p6, p7); // mosi, miso, sclk
00036  * 
00037  * int main() 
00038  * {
00039  *     DigitalOut cs(p8);
00040  *     spi.format(8, 0);
00041  *     spi.frequency(10000000);
00042  *     cs.write(0);
00043  *     spi.write(0x9f);
00044  *     int jedecid = (spi.write(0) << 16) | (spi.write(0) << 8) | spi.write(0);
00045  *     cs.write(1);
00046  * }
00047  * @endcode
00048  */
00049 class SWSPI
00050 {
00051 private:
00052     DigitalOut* mosi;
00053     DigitalIn* miso;
00054     DigitalOut* sclk;
00055     int port;
00056     int bits;
00057     int mode;
00058     int polarity; // idle clock value
00059     int phase; // 0=sample on leading (first) clock edge, 1=trailing (second)
00060     int freq;
00061     
00062 public:
00063     /** Create SWSPI object
00064      *
00065      *  @param mosi_pin
00066      *  @param miso_pin
00067      *  @param sclk_pin
00068      */
00069     SWSPI(PinName mosi_pin, PinName miso_pin, PinName sclk_pin);
00070     
00071     /** Destructor */
00072     ~SWSPI();
00073     
00074     /** Specify SPI format
00075      *
00076      *  @param bits  8 or 16 are typical values
00077      *  @param mode  0, 1, 2, or 3 phase (bit1) and idle clock (bit0)
00078      */
00079     void format(int bits, int mode = 0);
00080     
00081     /** Specify SPI clock frequency
00082      *
00083      *  @param hz  frequency (optional, defaults to 10000000)
00084      */
00085     void frequency(int hz = 10000000);
00086     
00087     /** Write data and read result
00088      *
00089      *  @param value  data to write (see format for bit size)
00090      *  returns value read from device
00091      */
00092     int write(int value);
00093 };
00094 
00095 #endif // SWSPI_H