wimbeaumont Project / SWSPI

Dependents:   sscm SPItest sscm

Fork of SWSPI by Dave Van Wagner

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SWSPI.h Source File

SWSPI.h

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