AD9249 ADC

Fork of adc_ad9249 by wimbeaumont Project

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SWSPI_BI.h Source File

SWSPI_BI.h

00001 /* SWSPI_BI, 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_BI_H
00024 #define SWSPI_BI_H
00025 
00026 #include "getVersion.h"
00027 /** A software implemented SPI that can use any digital pins
00028  *
00029  * Useful when don't want to share a single SPI hardware among attached devices
00030  * or when pinout doesn't match exactly to the target's SPI pins
00031  *
00032  * @code
00033  * #include "mbed.h"
00034  * #include "SWSPI_BI.h"
00035  * 
00036  * SWSPI_BI spi(p5, p6, p7); // mosio, dir  , sclk
00037  * 
00038  * int main() 
00039  * {
00040  *     DigitalOut cs(p8);
00041  *     spi.format(8, 0);
00042  *     spi.frequency(10000000);
00043  *     cs.write(0);
00044  *     spi.write(0x9f);
00045  *     int jedecid = (spi.write(0) << 16) | (spi.write(0) << 8) | spi.write(0);
00046  *     cs.write(1);
00047  * }
00048  * @endcode
00049  */
00050 
00051 #define SWSPI_BI_HDR_VER  "1.50"
00052 
00053  
00054 class SWSPI_BI: public getVersion
00055 {
00056 private:
00057     DigitalInOut* msio;
00058     DigitalOut* ldir;
00059     DigitalOut* rdir;
00060     DigitalOut* sclk;
00061     int port;
00062     int bits;
00063     int mode;
00064     int polarity; // idle clock value
00065     int phase; // 0=sample on leading (first) clock edge, 1=trailing (second)
00066     int freq;
00067     
00068 public:
00069     /** Create SWSPI_BI object
00070      *
00071      *  @param mosi_pin
00072      *  @param miso_pin
00073      *  @param sclk_pin
00074      */
00075     SWSPI_BI(DigitalInOut *msio_pin, DigitalOut *rdir_pin,DigitalOut *ldir_pin, DigitalOut *sclk_pin);
00076     
00077     /** Destructor */
00078     ~SWSPI_BI();
00079     
00080     /** Specify SPI format
00081      *
00082      *  @param bits  8 or 16 are typical values
00083      *  @param mode  0, 1, 2, or 3 phase (bit1) and idle clock (bit0)
00084      */
00085     void format(int bits, int mode = 0);
00086     
00087     /** Specify SPI clock frequency
00088      *
00089      *  @param hz  frequency (optional, defaults to 10000000)
00090      */
00091     void frequency(int hz = 10000000);
00092     
00093     /** Write data and read result
00094      *
00095      *  @param value  data to write (see format for bit size) 
00096      *  @param  DigitalOut  cs line
00097      *  @param  lastdata this is the last data of a sequence, sets the cs high at the end 
00098      *  @param  nxtrd : next cycle will be a read, set the LVDS interface to master in ( ADC out)     
00099      */
00100     void  write(unsigned int value, DigitalOut * cs, bool lastdata, int cs_pol=0 ,bool nxtrd=false);
00101     unsigned int read( DigitalOut * cs, bool lastdata, int cs_pol=0 );
00102 };
00103 
00104 #endif // SWSPI_BI_H