sw SPI interface specific for the SOLID Slow control beta!!
Fork of SWSPI by
SWSPI.h@5:75730aba1ce7, 2015-10-23 (annotated)
- Committer:
- wbeaumont
- Date:
- Fri Oct 23 11:23:00 2015 +0000
- Revision:
- 5:75730aba1ce7
- Parent:
- 4:2a23c789bd1c
production version archiving
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wbeaumont | 1:02327a96a5e2 | 1 | |
wbeaumont | 1:02327a96a5e2 | 2 | #ifndef SWSPI_H |
wbeaumont | 1:02327a96a5e2 | 3 | #define SWSPI_H |
wbeaumont | 1:02327a96a5e2 | 4 | |
davervw | 0:6a500a08c7fd | 5 | /* SWSPI, Software SPI library |
davervw | 0:6a500a08c7fd | 6 | * Copyright (c) 2012-2014, David R. Van Wagner, http://techwithdave.blogspot.com |
davervw | 0:6a500a08c7fd | 7 | * |
davervw | 0:6a500a08c7fd | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
davervw | 0:6a500a08c7fd | 9 | * of this software and associated documentation files (the "Software"), to deal |
davervw | 0:6a500a08c7fd | 10 | * in the Software without restriction, including without limitation the rights |
davervw | 0:6a500a08c7fd | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
davervw | 0:6a500a08c7fd | 12 | * copies of the Software, and to permit persons to whom the Software is |
davervw | 0:6a500a08c7fd | 13 | * furnished to do so, subject to the following conditions: |
davervw | 0:6a500a08c7fd | 14 | * |
davervw | 0:6a500a08c7fd | 15 | * The above copyright notice and this permission notice shall be included in |
davervw | 0:6a500a08c7fd | 16 | * all copies or substantial portions of the Software. |
davervw | 0:6a500a08c7fd | 17 | * |
davervw | 0:6a500a08c7fd | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
davervw | 0:6a500a08c7fd | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
davervw | 0:6a500a08c7fd | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
davervw | 0:6a500a08c7fd | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
davervw | 0:6a500a08c7fd | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
davervw | 0:6a500a08c7fd | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
davervw | 0:6a500a08c7fd | 24 | * THE SOFTWARE. |
wbeaumont | 5:75730aba1ce7 | 25 | * |
wbeaumont | 5:75730aba1ce7 | 26 | * Software adapted for the SM1 project of the SOLID colaboration |
wbeaumont | 5:75730aba1ce7 | 27 | * (C) Wim Beaumont Univeristeit Antwerpen 2014 , 2015 |
davervw | 0:6a500a08c7fd | 28 | */ |
davervw | 0:6a500a08c7fd | 29 | |
davervw | 0:6a500a08c7fd | 30 | |
davervw | 0:6a500a08c7fd | 31 | /** A software implemented SPI that can use any digital pins |
davervw | 0:6a500a08c7fd | 32 | * |
davervw | 0:6a500a08c7fd | 33 | * Useful when don't want to share a single SPI hardware among attached devices |
davervw | 0:6a500a08c7fd | 34 | * or when pinout doesn't match exactly to the target's SPI pins |
davervw | 0:6a500a08c7fd | 35 | * |
davervw | 0:6a500a08c7fd | 36 | * @code |
davervw | 0:6a500a08c7fd | 37 | * #include "mbed.h" |
davervw | 0:6a500a08c7fd | 38 | * #include "SWSPI.h" |
davervw | 0:6a500a08c7fd | 39 | * |
davervw | 0:6a500a08c7fd | 40 | * SWSPI spi(p5, p6, p7); // mosi, miso, sclk |
davervw | 0:6a500a08c7fd | 41 | * |
davervw | 0:6a500a08c7fd | 42 | * int main() |
davervw | 0:6a500a08c7fd | 43 | * { |
davervw | 0:6a500a08c7fd | 44 | * DigitalOut cs(p8); |
davervw | 0:6a500a08c7fd | 45 | * spi.format(8, 0); |
davervw | 0:6a500a08c7fd | 46 | * spi.frequency(10000000); |
davervw | 0:6a500a08c7fd | 47 | * cs.write(0); |
davervw | 0:6a500a08c7fd | 48 | * spi.write(0x9f); |
davervw | 0:6a500a08c7fd | 49 | * int jedecid = (spi.write(0) << 16) | (spi.write(0) << 8) | spi.write(0); |
davervw | 0:6a500a08c7fd | 50 | * cs.write(1); |
davervw | 0:6a500a08c7fd | 51 | * } |
davervw | 0:6a500a08c7fd | 52 | * @endcode |
davervw | 0:6a500a08c7fd | 53 | */ |
wbeaumont | 1:02327a96a5e2 | 54 | #include "mbed.h" |
wbeaumont | 4:2a23c789bd1c | 55 | #include "getVersion.h" |
wbeaumont | 1:02327a96a5e2 | 56 | |
wbeaumont | 4:2a23c789bd1c | 57 | #define SWSPI_HDR_VER "1.22" |
wbeaumont | 4:2a23c789bd1c | 58 | |
wbeaumont | 1:02327a96a5e2 | 59 | |
wbeaumont | 4:2a23c789bd1c | 60 | class SWSPI : public getVersion |
davervw | 0:6a500a08c7fd | 61 | { |
davervw | 0:6a500a08c7fd | 62 | private: |
davervw | 0:6a500a08c7fd | 63 | DigitalOut* mosi; |
davervw | 0:6a500a08c7fd | 64 | DigitalIn* miso; |
davervw | 0:6a500a08c7fd | 65 | DigitalOut* sclk; |
davervw | 0:6a500a08c7fd | 66 | int port; |
davervw | 0:6a500a08c7fd | 67 | int bits; |
davervw | 0:6a500a08c7fd | 68 | int mode; |
davervw | 0:6a500a08c7fd | 69 | int polarity; // idle clock value |
davervw | 0:6a500a08c7fd | 70 | int phase; // 0=sample on leading (first) clock edge, 1=trailing (second) |
davervw | 0:6a500a08c7fd | 71 | int freq; |
davervw | 0:6a500a08c7fd | 72 | |
davervw | 0:6a500a08c7fd | 73 | public: |
davervw | 0:6a500a08c7fd | 74 | /** Create SWSPI object |
davervw | 0:6a500a08c7fd | 75 | * |
davervw | 0:6a500a08c7fd | 76 | * @param mosi_pin |
davervw | 0:6a500a08c7fd | 77 | * @param miso_pin |
davervw | 0:6a500a08c7fd | 78 | * @param sclk_pin |
davervw | 0:6a500a08c7fd | 79 | */ |
wbeaumont | 1:02327a96a5e2 | 80 | SWSPI(DigitalOut *mosi_pin, DigitalIn *miso_pin, DigitalOut *sclk_pin); |
davervw | 0:6a500a08c7fd | 81 | |
davervw | 0:6a500a08c7fd | 82 | /** Destructor */ |
davervw | 0:6a500a08c7fd | 83 | ~SWSPI(); |
davervw | 0:6a500a08c7fd | 84 | |
davervw | 0:6a500a08c7fd | 85 | /** Specify SPI format |
davervw | 0:6a500a08c7fd | 86 | * |
davervw | 0:6a500a08c7fd | 87 | * @param bits 8 or 16 are typical values |
davervw | 0:6a500a08c7fd | 88 | * @param mode 0, 1, 2, or 3 phase (bit1) and idle clock (bit0) |
davervw | 0:6a500a08c7fd | 89 | */ |
davervw | 0:6a500a08c7fd | 90 | void format(int bits, int mode = 0); |
davervw | 0:6a500a08c7fd | 91 | |
davervw | 0:6a500a08c7fd | 92 | /** Specify SPI clock frequency |
davervw | 0:6a500a08c7fd | 93 | * |
davervw | 0:6a500a08c7fd | 94 | * @param hz frequency (optional, defaults to 10000000) |
davervw | 0:6a500a08c7fd | 95 | */ |
davervw | 0:6a500a08c7fd | 96 | void frequency(int hz = 10000000); |
davervw | 0:6a500a08c7fd | 97 | |
davervw | 0:6a500a08c7fd | 98 | /** Write data and read result |
davervw | 0:6a500a08c7fd | 99 | * |
davervw | 0:6a500a08c7fd | 100 | * @param value data to write (see format for bit size) |
davervw | 0:6a500a08c7fd | 101 | * returns value read from device |
davervw | 0:6a500a08c7fd | 102 | */ |
wbeaumont | 2:598d0028d5d2 | 103 | unsigned int write(unsigned int value); |
wbeaumont | 3:9c5ae1507a81 | 104 | |
wbeaumont | 3:9c5ae1507a81 | 105 | static unsigned short get_hdr_version_nr(); |
wbeaumont | 3:9c5ae1507a81 | 106 | static unsigned short get_src_version_nr(); |
davervw | 0:6a500a08c7fd | 107 | }; |
davervw | 0:6a500a08c7fd | 108 | |
davervw | 0:6a500a08c7fd | 109 | #endif // SWSPI_H |