forked library from davervw/SWSPI
Dependents: 11u35_usbLocalFilesystem
Fork of SWSPI by
SWSPI.cpp@1:0f59aa4b839f, 2015-10-06 (annotated)
- Committer:
- k4zuki
- Date:
- Tue Oct 06 02:12:46 2015 +0000
- Revision:
- 1:0f59aa4b839f
- Parent:
- 0:6a500a08c7fd
- Child:
- 2:457a920a1223
fork from davervw/SWSPI
Who changed what in which revision?
User | Revision | Line number | New 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 | |
k4zuki | 1:0f59aa4b839f | 26 | SWSPI::SWSPI(PinName mosi_pin, PinName miso_pin, PinName sclk_pin):_fast(false) |
davervw | 0:6a500a08c7fd | 27 | { |
k4zuki | 1:0f59aa4b839f | 28 | mosi = new DigitalInOut(mosi_pin); |
k4zuki | 1:0f59aa4b839f | 29 | mosi->input(); |
k4zuki | 1:0f59aa4b839f | 30 | mosi->mode(PullNone); |
k4zuki | 1:0f59aa4b839f | 31 | miso = new DigitalInOut(miso_pin); |
k4zuki | 1:0f59aa4b839f | 32 | miso->input(); |
k4zuki | 1:0f59aa4b839f | 33 | miso->mode(PullNone); |
k4zuki | 1:0f59aa4b839f | 34 | sclk = new DigitalInOut(sclk_pin); |
k4zuki | 1:0f59aa4b839f | 35 | sclk->input(); |
k4zuki | 1:0f59aa4b839f | 36 | sclk->mode(PullNone); |
davervw | 0:6a500a08c7fd | 37 | format(8); |
davervw | 0:6a500a08c7fd | 38 | frequency(); |
davervw | 0:6a500a08c7fd | 39 | } |
davervw | 0:6a500a08c7fd | 40 | |
davervw | 0:6a500a08c7fd | 41 | SWSPI::~SWSPI() |
davervw | 0:6a500a08c7fd | 42 | { |
davervw | 0:6a500a08c7fd | 43 | delete mosi; |
davervw | 0:6a500a08c7fd | 44 | delete miso; |
davervw | 0:6a500a08c7fd | 45 | delete sclk; |
davervw | 0:6a500a08c7fd | 46 | } |
davervw | 0:6a500a08c7fd | 47 | |
davervw | 0:6a500a08c7fd | 48 | void SWSPI::format(int bits, int mode) |
davervw | 0:6a500a08c7fd | 49 | { |
davervw | 0:6a500a08c7fd | 50 | this->bits = bits; |
davervw | 0:6a500a08c7fd | 51 | this->mode = mode; |
davervw | 0:6a500a08c7fd | 52 | polarity = (mode >> 1) & 1; |
davervw | 0:6a500a08c7fd | 53 | phase = mode & 1; |
davervw | 0:6a500a08c7fd | 54 | sclk->write(polarity); |
davervw | 0:6a500a08c7fd | 55 | } |
davervw | 0:6a500a08c7fd | 56 | |
davervw | 0:6a500a08c7fd | 57 | void SWSPI::frequency(int hz) |
davervw | 0:6a500a08c7fd | 58 | { |
davervw | 0:6a500a08c7fd | 59 | this->freq = hz; |
k4zuki | 1:0f59aa4b839f | 60 | _fast = (hz >= 1000000) ? true : false; |
davervw | 0:6a500a08c7fd | 61 | } |
davervw | 0:6a500a08c7fd | 62 | |
k4zuki | 1:0f59aa4b839f | 63 | #pragma Otime |
k4zuki | 1:0f59aa4b839f | 64 | |
davervw | 0:6a500a08c7fd | 65 | int SWSPI::write(int value) |
davervw | 0:6a500a08c7fd | 66 | { |
k4zuki | 1:0f59aa4b839f | 67 | mosi->output(); |
k4zuki | 1:0f59aa4b839f | 68 | mosi->mode(PullNone); |
k4zuki | 1:0f59aa4b839f | 69 | miso->input(); |
k4zuki | 1:0f59aa4b839f | 70 | miso->mode(PullNone); |
k4zuki | 1:0f59aa4b839f | 71 | sclk->output(); |
k4zuki | 1:0f59aa4b839f | 72 | sclk->mode(PullNone); |
k4zuki | 1:0f59aa4b839f | 73 | |
davervw | 0:6a500a08c7fd | 74 | int read = 0; |
k4zuki | 1:0f59aa4b839f | 75 | if (_fast) { |
k4zuki | 1:0f59aa4b839f | 76 | read = fast_write(value); |
k4zuki | 1:0f59aa4b839f | 77 | } |
davervw | 0:6a500a08c7fd | 78 | for (int bit = bits-1; bit >= 0; --bit) |
davervw | 0:6a500a08c7fd | 79 | { |
davervw | 0:6a500a08c7fd | 80 | mosi->write(((value >> bit) & 0x01) != 0); |
davervw | 0:6a500a08c7fd | 81 | |
davervw | 0:6a500a08c7fd | 82 | if (phase == 0) |
davervw | 0:6a500a08c7fd | 83 | { |
davervw | 0:6a500a08c7fd | 84 | if (miso->read()) |
davervw | 0:6a500a08c7fd | 85 | read |= (1 << bit); |
davervw | 0:6a500a08c7fd | 86 | } |
davervw | 0:6a500a08c7fd | 87 | |
davervw | 0:6a500a08c7fd | 88 | sclk->write(!polarity); |
davervw | 0:6a500a08c7fd | 89 | |
k4zuki | 1:0f59aa4b839f | 90 | wait_us(1000000/freq/2); |
davervw | 0:6a500a08c7fd | 91 | |
davervw | 0:6a500a08c7fd | 92 | if (phase == 1) |
davervw | 0:6a500a08c7fd | 93 | { |
davervw | 0:6a500a08c7fd | 94 | if (miso->read()) |
davervw | 0:6a500a08c7fd | 95 | read |= (1 << bit); |
davervw | 0:6a500a08c7fd | 96 | } |
davervw | 0:6a500a08c7fd | 97 | |
davervw | 0:6a500a08c7fd | 98 | sclk->write(polarity); |
davervw | 0:6a500a08c7fd | 99 | |
k4zuki | 1:0f59aa4b839f | 100 | wait_us(1000000/freq/2); |
davervw | 0:6a500a08c7fd | 101 | } |
k4zuki | 1:0f59aa4b839f | 102 | |
k4zuki | 1:0f59aa4b839f | 103 | mosi->input(); |
k4zuki | 1:0f59aa4b839f | 104 | mosi->mode(PullNone); |
k4zuki | 1:0f59aa4b839f | 105 | miso->input(); |
k4zuki | 1:0f59aa4b839f | 106 | miso->mode(PullNone); |
k4zuki | 1:0f59aa4b839f | 107 | sclk->input(); |
k4zuki | 1:0f59aa4b839f | 108 | sclk->mode(PullNone); |
davervw | 0:6a500a08c7fd | 109 | |
davervw | 0:6a500a08c7fd | 110 | return read; |
davervw | 0:6a500a08c7fd | 111 | } |
davervw | 0:6a500a08c7fd | 112 | |
k4zuki | 1:0f59aa4b839f | 113 | uint8_t SWSPI::fast_write(uint8_t value) |
k4zuki | 1:0f59aa4b839f | 114 | { |
k4zuki | 1:0f59aa4b839f | 115 | uint8_t data = 0; |
k4zuki | 1:0f59aa4b839f | 116 | for(uint8_t mask = 0x80; mask; mask >>= 1) { |
k4zuki | 1:0f59aa4b839f | 117 | mosi->write((value & mask) ? 1 : 0); |
k4zuki | 1:0f59aa4b839f | 118 | if (miso->read()) { |
k4zuki | 1:0f59aa4b839f | 119 | data |= mask; |
k4zuki | 1:0f59aa4b839f | 120 | } |
k4zuki | 1:0f59aa4b839f | 121 | sclk->write(1); |
k4zuki | 1:0f59aa4b839f | 122 | sclk->write(0); |
k4zuki | 1:0f59aa4b839f | 123 | } |
k4zuki | 1:0f59aa4b839f | 124 | return data; |
k4zuki | 1:0f59aa4b839f | 125 | } |
k4zuki | 1:0f59aa4b839f | 126 |