Half Duplex Version of SWSPI

Fork of SWSPI by Dave Van Wagner

Committer:
martinsimpson
Date:
Tue Jun 19 11:19:34 2018 +0000
Revision:
3:d3be72ac889e
Parent:
1:17d758fe52f8
V3 changed Data output routine and sorted sclk for phase selection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martinsimpson 1:17d758fe52f8 1 /* SWSPI, Software SPI library
martinsimpson 1:17d758fe52f8 2 * Copyright (c) 2012-2014, David R. Van Wagner, http://techwithdave.blogspot.com
martinsimpson 1:17d758fe52f8 3 *
martinsimpson 1:17d758fe52f8 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
martinsimpson 1:17d758fe52f8 5 * of this software and associated documentation files (the "Software"), to deal
martinsimpson 1:17d758fe52f8 6 * in the Software without restriction, including without limitation the rights
martinsimpson 1:17d758fe52f8 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
martinsimpson 1:17d758fe52f8 8 * copies of the Software, and to permit persons to whom the Software is
martinsimpson 1:17d758fe52f8 9 * furnished to do so, subject to the following conditions:
martinsimpson 1:17d758fe52f8 10 *
martinsimpson 1:17d758fe52f8 11 * The above copyright notice and this permission notice shall be included in
martinsimpson 1:17d758fe52f8 12 * all copies or substantial portions of the Software.
martinsimpson 1:17d758fe52f8 13 *
martinsimpson 1:17d758fe52f8 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
martinsimpson 1:17d758fe52f8 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
martinsimpson 1:17d758fe52f8 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
martinsimpson 1:17d758fe52f8 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
martinsimpson 1:17d758fe52f8 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
martinsimpson 1:17d758fe52f8 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
martinsimpson 1:17d758fe52f8 20 * THE SOFTWARE.
martinsimpson 1:17d758fe52f8 21 */
martinsimpson 1:17d758fe52f8 22
martinsimpson 1:17d758fe52f8 23 /* Based on above SWSPI credit as above
martinsimpson 1:17d758fe52f8 24 * This is now SWSPI_HD class type to provide single line data input output (dio)
martinsimpson 1:17d758fe52f8 25 * M. R. Simpson 19/06/2018
martinsimpson 1:17d758fe52f8 26 */
martinsimpson 1:17d758fe52f8 27
martinsimpson 1:17d758fe52f8 28 #ifndef SWSPI_HD_H
martinsimpson 1:17d758fe52f8 29 #define SWSPI_HD_H
martinsimpson 1:17d758fe52f8 30
martinsimpson 1:17d758fe52f8 31 /** A software implemented SPI that can use any digital pins
martinsimpson 1:17d758fe52f8 32 *
martinsimpson 1:17d758fe52f8 33 * Useful when don't want to share a single SPI hardware among attached devices
martinsimpson 1:17d758fe52f8 34 * or when pinout doesn't match exactly to the target's SPI pins
martinsimpson 1:17d758fe52f8 35 *
martinsimpson 1:17d758fe52f8 36 * @code
martinsimpson 1:17d758fe52f8 37 * #include "mbed.h"
martinsimpson 1:17d758fe52f8 38 * #include "SWSPI_HD.h"
martinsimpson 1:17d758fe52f8 39 *
martinsimpson 3:d3be72ac889e 40 * SWSPI_HD spi(D8, D7, D6); // dio, sclk (n)cs (Using Arduino form factor Board (NUCLEO-32F401RE)
martinsimpson 1:17d758fe52f8 41 *
martinsimpson 1:17d758fe52f8 42 * int main()
martinsimpson 1:17d758fe52f8 43 * {
martinsimpson 1:17d758fe52f8 44 * spi.format(8, 0);
martinsimpson 1:17d758fe52f8 45 * spi.frequency(10000000);
martinsimpson 1:17d758fe52f8 46 * cs.write(0);
martinsimpson 1:17d758fe52f8 47 * spi.write(0x9f);
martinsimpson 1:17d758fe52f8 48 * int jedecid = (spi.write(0) << 16) | (spi.write(0) << 8) | spi.write(0);
martinsimpson 1:17d758fe52f8 49 * cs.write(1);
martinsimpson 1:17d758fe52f8 50 * }
martinsimpson 1:17d758fe52f8 51 * @endcode
martinsimpson 1:17d758fe52f8 52 */
martinsimpson 1:17d758fe52f8 53 class SWSPI_HD
martinsimpson 1:17d758fe52f8 54 {
martinsimpson 1:17d758fe52f8 55 private:
martinsimpson 1:17d758fe52f8 56 DigitalInOut* dio;
martinsimpson 1:17d758fe52f8 57 DigitalOut* sclk;
martinsimpson 3:d3be72ac889e 58 DigitalOut* cs;
martinsimpson 1:17d758fe52f8 59 int port;
martinsimpson 1:17d758fe52f8 60 int bits;
martinsimpson 1:17d758fe52f8 61 int mode;
martinsimpson 1:17d758fe52f8 62 int polarity; // idle clock value
martinsimpson 1:17d758fe52f8 63 int phase; // 0=sample on leading (first) clock edge, 1=trailing (second)
martinsimpson 1:17d758fe52f8 64 int freq;
martinsimpson 1:17d758fe52f8 65
martinsimpson 1:17d758fe52f8 66 public:
martinsimpson 1:17d758fe52f8 67 /** Create SWSPI_HD object
martinsimpson 1:17d758fe52f8 68 *
martinsimpson 1:17d758fe52f8 69 * @param dio
martinsimpson 1:17d758fe52f8 70 * @param sclk_pin
martinsimpson 1:17d758fe52f8 71 */
martinsimpson 3:d3be72ac889e 72 SWSPI_HD(PinName dio_pin, PinName sclk_pin, PinName cs_pin);
martinsimpson 1:17d758fe52f8 73
martinsimpson 1:17d758fe52f8 74 /** Destructor */
martinsimpson 1:17d758fe52f8 75 ~SWSPI_HD();
martinsimpson 1:17d758fe52f8 76
martinsimpson 1:17d758fe52f8 77 /** Specify SPI format
martinsimpson 1:17d758fe52f8 78 *
martinsimpson 1:17d758fe52f8 79 * @param bits 8 or 16 are typical values
martinsimpson 1:17d758fe52f8 80 * @param mode 0, 1, 2, or 3 phase (bit1) and idle clock (bit0)
martinsimpson 1:17d758fe52f8 81 */
martinsimpson 1:17d758fe52f8 82 void format(int bits, int mode = 0);
martinsimpson 1:17d758fe52f8 83
martinsimpson 1:17d758fe52f8 84 /** Specify SPI clock frequency
martinsimpson 1:17d758fe52f8 85 *
martinsimpson 1:17d758fe52f8 86 * @param hz frequency (optional, defaults to 10000000)
martinsimpson 1:17d758fe52f8 87 */
martinsimpson 1:17d758fe52f8 88 void frequency(int hz = 10000000);
martinsimpson 1:17d758fe52f8 89
martinsimpson 1:17d758fe52f8 90 /** Write data and read result
martinsimpson 1:17d758fe52f8 91 *
martinsimpson 1:17d758fe52f8 92 * @param value data to write (see format for bit size)
martinsimpson 1:17d758fe52f8 93 * returns value read from device
martinsimpson 1:17d758fe52f8 94 */
martinsimpson 1:17d758fe52f8 95 int write(int value);
martinsimpson 1:17d758fe52f8 96 };
martinsimpson 1:17d758fe52f8 97
martinsimpson 1:17d758fe52f8 98 #endif // SWSPI_HD_H