├── .gitignore ├── LICENSE.md ├── README.md ├── examples └── Loopback │ └── Loopback.ino ├── keywords.txt ├── library.properties ├── src ├── SoftSPI.cpp └── SoftSPI.h └── uecide.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | *.dylib 9 | 10 | # Compiled Static libraries 11 | *.lai 12 | *.la 13 | *.a 14 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018, Majenko Technologies 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, this 11 | list of conditions and the following disclaimer in the documentation and/or 12 | other materials provided with the distribution. 13 | 14 | * Neither the name of Majenko Technologies nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SoftSPI 2 | ======= 3 | 4 | Software SPI class for Arduino 5 | -------------------------------------------------------------------------------- /examples/Loopback/Loopback.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Create a new SPI port with: 5 | // Pin 2 = MOSI, 6 | // Pin 3 = MISO, 7 | // Pin 4 = SCK 8 | SoftSPI mySPI(2, 3, 4); 9 | 10 | void setup() { 11 | mySPI.begin(); 12 | Serial.begin(9600); 13 | } 14 | 15 | void loop() { 16 | static uint8_t v = 0; 17 | 18 | Serial.print("Sending value: "); 19 | Serial.print(v, HEX); 20 | uint8_t in = mySPI.transfer(v); 21 | Serial.print(" Got value: "); 22 | Serial.print(in, HEX); 23 | Serial.println(v == in ? " PASS" : " FAIL"); 24 | delay(1000); 25 | v++; 26 | } 27 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | SoftSPI KEYWORD1 2 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=SoftSPI 2 | version=1.0.6 3 | author=Matt Jenkins 4 | maintainer=Matt Jenkins 5 | sentence=Software SPI implementation 6 | paragraph=SoftSPI\n=======\n\nSoftware SPI class for Arduino\n 7 | url=https://github.com/MajenkoLibraries/SoftSPI 8 | category=Communication 9 | architectures=* 10 | includes=SoftSPI.h 11 | -------------------------------------------------------------------------------- /src/SoftSPI.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Majenko Technologies 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * 3. Neither the name of Majenko Technologies nor the names of its contributors may be used 16 | * to endorse or promote products derived from this software without 17 | * specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | 32 | #include 33 | 34 | SoftSPI::SoftSPI(uint8_t mosi, uint8_t miso, uint8_t sck) { 35 | _mosi = mosi; 36 | _miso = miso; 37 | _sck = sck; 38 | _delay = 2; 39 | _cke = 0; 40 | _ckp = 0; 41 | _order = MSBFIRST; 42 | } 43 | 44 | void SoftSPI::begin() { 45 | pinMode(_mosi, OUTPUT); 46 | pinMode(_miso, INPUT); 47 | pinMode(_sck, OUTPUT); 48 | } 49 | 50 | void SoftSPI::end() { 51 | pinMode(_mosi, INPUT); 52 | pinMode(_miso, INPUT); 53 | pinMode(_sck, INPUT); 54 | } 55 | 56 | void SoftSPI::setBitOrder(uint8_t order) { 57 | _order = order & 1; 58 | } 59 | 60 | void SoftSPI::setDataMode(uint8_t mode) { 61 | switch (mode) { 62 | case SPI_MODE0: 63 | _ckp = 0; 64 | _cke = 0; 65 | break; 66 | case SPI_MODE1: 67 | _ckp = 0; 68 | _cke = 1; 69 | break; 70 | case SPI_MODE2: 71 | _ckp = 1; 72 | _cke = 0; 73 | break; 74 | case SPI_MODE3: 75 | _ckp = 1; 76 | _cke = 1; 77 | break; 78 | } 79 | 80 | digitalWrite(_sck, _ckp ? HIGH : LOW); 81 | } 82 | 83 | void SoftSPI::setClockDivider(uint32_t div) { 84 | switch (div) { 85 | case SPI_CLOCK_DIV2: 86 | _delay = 2; 87 | break; 88 | case SPI_CLOCK_DIV4: 89 | _delay = 4; 90 | break; 91 | case SPI_CLOCK_DIV8: 92 | _delay = 8; 93 | break; 94 | case SPI_CLOCK_DIV16: 95 | _delay = 16; 96 | break; 97 | case SPI_CLOCK_DIV32: 98 | _delay = 32; 99 | break; 100 | case SPI_CLOCK_DIV64: 101 | _delay = 64; 102 | break; 103 | case SPI_CLOCK_DIV128: 104 | _delay = 128; 105 | break; 106 | default: 107 | _delay = 128; 108 | break; 109 | } 110 | } 111 | 112 | void SoftSPI::wait(uint_fast8_t del) { 113 | for (uint_fast8_t i = 0; i < del; i++) { 114 | asm volatile("nop"); 115 | } 116 | } 117 | 118 | uint8_t SoftSPI::transfer(uint8_t val) { 119 | uint8_t out = 0; 120 | if (_order == MSBFIRST) { 121 | uint8_t v2 = 122 | ((val & 0x01) << 7) | 123 | ((val & 0x02) << 5) | 124 | ((val & 0x04) << 3) | 125 | ((val & 0x08) << 1) | 126 | ((val & 0x10) >> 1) | 127 | ((val & 0x20) >> 3) | 128 | ((val & 0x40) >> 5) | 129 | ((val & 0x80) >> 7); 130 | val = v2; 131 | } 132 | 133 | uint8_t del = _delay >> 1; 134 | 135 | uint8_t bval = 0; 136 | /* 137 | * CPOL := 0, CPHA := 0 => INIT = 0, PRE = Z|0, MID = 1, POST = 0 138 | * CPOL := 1, CPHA := 0 => INIT = 1, PRE = Z|1, MID = 0, POST = 1 139 | * CPOL := 0, CPHA := 1 => INIT = 0, PRE = 1 , MID = 0, POST = Z|0 140 | * CPOL := 1, CPHA := 1 => INIT = 1, PRE = 0 , MID = 1, POST = Z|1 141 | */ 142 | 143 | int sck = (_ckp) ? HIGH : LOW; 144 | 145 | for (uint8_t bit = 0u; bit < 8u; bit++) 146 | { 147 | if (_cke) { 148 | sck ^= 1; 149 | digitalWrite(_sck, sck); 150 | wait(del); 151 | } 152 | 153 | /* ... Write bit */ 154 | digitalWrite(_mosi, ((val & (1<>= 1; 169 | out |= bval << 7; 170 | } 171 | } 172 | 173 | wait(del); 174 | 175 | if (!_cke) { 176 | sck ^= 1u; 177 | digitalWrite(_sck, sck); 178 | } 179 | } 180 | 181 | return out; 182 | } 183 | 184 | uint16_t SoftSPI::transfer16(uint16_t data) 185 | { 186 | union { 187 | uint16_t val; 188 | struct { 189 | uint8_t lsb; 190 | uint8_t msb; 191 | }; 192 | } in, out; 193 | 194 | in.val = data; 195 | 196 | if ( _order == MSBFIRST ) { 197 | out.msb = transfer(in.msb); 198 | out.lsb = transfer(in.lsb); 199 | } else { 200 | out.lsb = transfer(in.lsb); 201 | out.msb = transfer(in.msb); 202 | } 203 | 204 | return out.val; 205 | } 206 | -------------------------------------------------------------------------------- /src/SoftSPI.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Majenko Technologies 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * 3. Neither the name of Majenko Technologies nor the names of its contributors may be used 16 | * to endorse or promote products derived from this software without 17 | * specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | 32 | #ifndef _SOFTSPI_H 33 | #define _SOFTSPI_H 34 | 35 | #if (ARDUINO >= 100) 36 | # include 37 | #else 38 | # include 39 | #endif 40 | 41 | #include 42 | 43 | class SoftSPI : public SPIClass { 44 | private: 45 | void wait(uint_fast8_t del); 46 | 47 | private: 48 | uint8_t _cke; 49 | uint8_t _ckp; 50 | uint8_t _delay; 51 | uint8_t _miso; 52 | uint8_t _mosi; 53 | uint8_t _sck; 54 | uint8_t _order; 55 | 56 | public: 57 | SoftSPI(uint8_t mosi, uint8_t miso, uint8_t sck); 58 | void begin(); 59 | void end(); 60 | void setBitOrder(uint8_t); 61 | void setDataMode(uint8_t); 62 | void setClockDivider(uint32_t); 63 | uint8_t transfer(uint8_t); 64 | uint16_t transfer16(uint16_t data); 65 | 66 | }; 67 | #endif 68 | -------------------------------------------------------------------------------- /uecide.json: -------------------------------------------------------------------------------- 1 | {"name":"SoftSPI","provides":"SoftSPI.h","license":"LICENSE.md","readme":"README.md","description":"Software SPI implementation","category":"Communication","subcategory":"Serial","maintainer":"Matt Jenkins ","version":"1.0.6","family":"all","core":"all","install":{"examples":"\/","library.properties":"\/","src":"\/"}} 2 | --------------------------------------------------------------------------------