sw ADC SPI interface for the SOLID Slow control beta!!

Dependents:   SPItest sscm

Committer:
wbeaumont
Date:
Fri Oct 23 11:22:44 2015 +0000
Revision:
2:1fb81137d906
Parent:
1:01459a6ab296
production version archiving

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 0:9efb460e962b 1 /* SWSPI_BI, Software SPI library
wbeaumont 0:9efb460e962b 2 * Copyright (c) 2012-2014, David R. Van Wagner, http://techwithdave.blogspot.com
wbeaumont 0:9efb460e962b 3 *
wbeaumont 0:9efb460e962b 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
wbeaumont 0:9efb460e962b 5 * of this software and associated documentation files (the "Software"), to deal
wbeaumont 0:9efb460e962b 6 * in the Software without restriction, including without limitation the rights
wbeaumont 0:9efb460e962b 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wbeaumont 0:9efb460e962b 8 * copies of the Software, and to permit persons to whom the Software is
wbeaumont 0:9efb460e962b 9 * furnished to do so, subject to the following conditions:
wbeaumont 0:9efb460e962b 10 *
wbeaumont 0:9efb460e962b 11 * The above copyright notice and this permission notice shall be included in
wbeaumont 0:9efb460e962b 12 * all copies or substantial portions of the Software.
wbeaumont 0:9efb460e962b 13 *
wbeaumont 0:9efb460e962b 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wbeaumont 0:9efb460e962b 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wbeaumont 0:9efb460e962b 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wbeaumont 0:9efb460e962b 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wbeaumont 0:9efb460e962b 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wbeaumont 0:9efb460e962b 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wbeaumont 0:9efb460e962b 20 * THE SOFTWARE.
wbeaumont 2:1fb81137d906 21 *
wbeaumont 2:1fb81137d906 22 * Software adapted for the SM1 project of the SOLID colaboration
wbeaumont 2:1fb81137d906 23 * (C) Wim Beaumont Univeristeit Antwerpen 2014 , 2015
wbeaumont 2:1fb81137d906 24
wbeaumont 0:9efb460e962b 25 */
wbeaumont 0:9efb460e962b 26
wbeaumont 0:9efb460e962b 27 #ifndef SWSPI_BI_H
wbeaumont 0:9efb460e962b 28 #define SWSPI_BI_H
wbeaumont 0:9efb460e962b 29
wbeaumont 1:01459a6ab296 30 #include "getVersion.h"
wbeaumont 0:9efb460e962b 31 /** A software implemented SPI that can use any digital pins
wbeaumont 0:9efb460e962b 32 *
wbeaumont 0:9efb460e962b 33 * Useful when don't want to share a single SPI hardware among attached devices
wbeaumont 0:9efb460e962b 34 * or when pinout doesn't match exactly to the target's SPI pins
wbeaumont 0:9efb460e962b 35 *
wbeaumont 0:9efb460e962b 36 * @code
wbeaumont 0:9efb460e962b 37 * #include "mbed.h"
wbeaumont 0:9efb460e962b 38 * #include "SWSPI_BI.h"
wbeaumont 0:9efb460e962b 39 *
wbeaumont 0:9efb460e962b 40 * SWSPI_BI spi(p5, p6, p7); // mosio, dir , sclk
wbeaumont 0:9efb460e962b 41 *
wbeaumont 0:9efb460e962b 42 * int main()
wbeaumont 0:9efb460e962b 43 * {
wbeaumont 0:9efb460e962b 44 * DigitalOut cs(p8);
wbeaumont 0:9efb460e962b 45 * spi.format(8, 0);
wbeaumont 0:9efb460e962b 46 * spi.frequency(10000000);
wbeaumont 0:9efb460e962b 47 * cs.write(0);
wbeaumont 0:9efb460e962b 48 * spi.write(0x9f);
wbeaumont 0:9efb460e962b 49 * int jedecid = (spi.write(0) << 16) | (spi.write(0) << 8) | spi.write(0);
wbeaumont 0:9efb460e962b 50 * cs.write(1);
wbeaumont 0:9efb460e962b 51 * }
wbeaumont 0:9efb460e962b 52 * @endcode
wbeaumont 0:9efb460e962b 53 */
wbeaumont 0:9efb460e962b 54
wbeaumont 1:01459a6ab296 55 #define SWSPI_BI_HDR_VER "1.50"
wbeaumont 1:01459a6ab296 56
wbeaumont 0:9efb460e962b 57
wbeaumont 1:01459a6ab296 58 class SWSPI_BI: public getVersion
wbeaumont 0:9efb460e962b 59 {
wbeaumont 0:9efb460e962b 60 private:
wbeaumont 0:9efb460e962b 61 DigitalInOut* msio;
wbeaumont 0:9efb460e962b 62 DigitalOut* ldir;
wbeaumont 0:9efb460e962b 63 DigitalOut* rdir;
wbeaumont 0:9efb460e962b 64 DigitalOut* sclk;
wbeaumont 0:9efb460e962b 65 int port;
wbeaumont 0:9efb460e962b 66 int bits;
wbeaumont 0:9efb460e962b 67 int mode;
wbeaumont 0:9efb460e962b 68 int polarity; // idle clock value
wbeaumont 0:9efb460e962b 69 int phase; // 0=sample on leading (first) clock edge, 1=trailing (second)
wbeaumont 0:9efb460e962b 70 int freq;
wbeaumont 0:9efb460e962b 71
wbeaumont 0:9efb460e962b 72 public:
wbeaumont 0:9efb460e962b 73 /** Create SWSPI_BI object
wbeaumont 0:9efb460e962b 74 *
wbeaumont 0:9efb460e962b 75 * @param mosi_pin
wbeaumont 0:9efb460e962b 76 * @param miso_pin
wbeaumont 0:9efb460e962b 77 * @param sclk_pin
wbeaumont 0:9efb460e962b 78 */
wbeaumont 0:9efb460e962b 79 SWSPI_BI(DigitalInOut *msio_pin, DigitalOut *rdir_pin,DigitalOut *ldir_pin, DigitalOut *sclk_pin);
wbeaumont 0:9efb460e962b 80
wbeaumont 0:9efb460e962b 81 /** Destructor */
wbeaumont 0:9efb460e962b 82 ~SWSPI_BI();
wbeaumont 0:9efb460e962b 83
wbeaumont 0:9efb460e962b 84 /** Specify SPI format
wbeaumont 0:9efb460e962b 85 *
wbeaumont 0:9efb460e962b 86 * @param bits 8 or 16 are typical values
wbeaumont 0:9efb460e962b 87 * @param mode 0, 1, 2, or 3 phase (bit1) and idle clock (bit0)
wbeaumont 0:9efb460e962b 88 */
wbeaumont 0:9efb460e962b 89 void format(int bits, int mode = 0);
wbeaumont 0:9efb460e962b 90
wbeaumont 0:9efb460e962b 91 /** Specify SPI clock frequency
wbeaumont 0:9efb460e962b 92 *
wbeaumont 0:9efb460e962b 93 * @param hz frequency (optional, defaults to 10000000)
wbeaumont 0:9efb460e962b 94 */
wbeaumont 0:9efb460e962b 95 void frequency(int hz = 10000000);
wbeaumont 0:9efb460e962b 96
wbeaumont 0:9efb460e962b 97 /** Write data and read result
wbeaumont 0:9efb460e962b 98 *
wbeaumont 0:9efb460e962b 99 * @param value data to write (see format for bit size)
wbeaumont 0:9efb460e962b 100 * @param DigitalOut cs line
wbeaumont 0:9efb460e962b 101 * @param lastdata this is the last data of a sequence, sets the cs high at the end
wbeaumont 0:9efb460e962b 102 * @param nxtrd : next cycle will be a read, set the LVDS interface to master in ( ADC out)
wbeaumont 0:9efb460e962b 103 */
wbeaumont 0:9efb460e962b 104 void write(unsigned int value, DigitalOut * cs, bool lastdata, int cs_pol=0 ,bool nxtrd=false);
wbeaumont 0:9efb460e962b 105 unsigned int read( DigitalOut * cs, bool lastdata, int cs_pol=0 );
wbeaumont 0:9efb460e962b 106 };
wbeaumont 0:9efb460e962b 107
wbeaumont 0:9efb460e962b 108 #endif // SWSPI_BI_H