Software SPI allows non-standard SPI pins to be used for interfacing with SPI devices

Fork of SWSPI by Dave Van Wagner

Committer:
lionello
Date:
Fri May 08 08:29:50 2015 +0000
Revision:
3:8c19911bdb58
Parent:
2:eafe3ec5fbf8
Use wait_us instead of wait(float)

Who changed what in which revision?

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