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 #include <mbed.h>
davervw 0:6a500a08c7fd 24 #include "SWSPI.h"
davervw 0:6a500a08c7fd 25
lionello 2:eafe3ec5fbf8 26 SWSPI::SWSPI(PinName mosi_pin, PinName miso_pin, PinName sclk_pin)
lionello 2:eafe3ec5fbf8 27 : mosi(mosi_pin),
lionello 2:eafe3ec5fbf8 28 miso(NULL),
lionello 2:eafe3ec5fbf8 29 sclk(sclk_pin)
davervw 0:6a500a08c7fd 30 {
lionello 1:9e0c0217646d 31 if (miso_pin!= NC)
lionello 1:9e0c0217646d 32 miso = new DigitalIn(miso_pin);
davervw 0:6a500a08c7fd 33 format(8);
davervw 0:6a500a08c7fd 34 frequency();
davervw 0:6a500a08c7fd 35 }
davervw 0:6a500a08c7fd 36
davervw 0:6a500a08c7fd 37 SWSPI::~SWSPI()
davervw 0:6a500a08c7fd 38 {
davervw 0:6a500a08c7fd 39 delete miso;
davervw 0:6a500a08c7fd 40 }
davervw 0:6a500a08c7fd 41
davervw 0:6a500a08c7fd 42 void SWSPI::format(int bits, int mode)
davervw 0:6a500a08c7fd 43 {
davervw 0:6a500a08c7fd 44 this->bits = bits;
davervw 0:6a500a08c7fd 45 polarity = (mode >> 1) & 1;
davervw 0:6a500a08c7fd 46 phase = mode & 1;
lionello 2:eafe3ec5fbf8 47 sclk.write(polarity);
davervw 0:6a500a08c7fd 48 }
davervw 0:6a500a08c7fd 49
davervw 0:6a500a08c7fd 50 void SWSPI::frequency(int hz)
davervw 0:6a500a08c7fd 51 {
lionello 2:eafe3ec5fbf8 52 // TODO: need wait_ns taking int
lionello 3:8c19911bdb58 53 this->delay_us = 500000/hz;
davervw 0:6a500a08c7fd 54 }
davervw 0:6a500a08c7fd 55
davervw 0:6a500a08c7fd 56 int SWSPI::write(int value)
davervw 0:6a500a08c7fd 57 {
davervw 0:6a500a08c7fd 58 int read = 0;
lionello 2:eafe3ec5fbf8 59 for (unsigned bit = 1 << (bits-1); bit; bit >>= 1)
davervw 0:6a500a08c7fd 60 {
lionello 2:eafe3ec5fbf8 61 mosi.write((value & bit) != 0);
davervw 0:6a500a08c7fd 62
davervw 0:6a500a08c7fd 63 if (phase == 0)
davervw 0:6a500a08c7fd 64 {
lionello 1:9e0c0217646d 65 if (miso && miso->read())
lionello 3:8c19911bdb58 66 read |= bit;
davervw 0:6a500a08c7fd 67 }
davervw 0:6a500a08c7fd 68
lionello 2:eafe3ec5fbf8 69 sclk.write(!polarity);
davervw 0:6a500a08c7fd 70
lionello 3:8c19911bdb58 71 if (delay_us) wait_us(delay_us);
davervw 0:6a500a08c7fd 72
davervw 0:6a500a08c7fd 73 if (phase == 1)
davervw 0:6a500a08c7fd 74 {
lionello 1:9e0c0217646d 75 if (miso && miso->read())
lionello 3:8c19911bdb58 76 read |= bit;
davervw 0:6a500a08c7fd 77 }
davervw 0:6a500a08c7fd 78
lionello 2:eafe3ec5fbf8 79 sclk.write(polarity);
davervw 0:6a500a08c7fd 80
lionello 3:8c19911bdb58 81 if (delay_us) wait_us(delay_us);
davervw 0:6a500a08c7fd 82 }
davervw 0:6a500a08c7fd 83
davervw 0:6a500a08c7fd 84 return read;
davervw 0:6a500a08c7fd 85 }