|
| 1 | + |
| 2 | +[](https://github.com/marketplace/actions/arduino_ci) |
| 3 | +[](https://github.com/RobTillaart/SWSPI/actions/workflows/arduino-lint.yml) |
| 4 | +[](https://github.com/RobTillaart/SWSPI/actions/workflows/jsoncheck.yml) |
| 5 | +[](https://github.com/RobTillaart/SWSPI/issues) |
| 6 | + |
| 7 | +[](https://github.com/RobTillaart/SWSPI/blob/master/LICENSE) |
| 8 | +[](https://github.com/RobTillaart/SWSPI/releases) |
| 9 | +[](https://registry.platformio.org/libraries/robtillaart/SWSPI) |
| 10 | + |
| 11 | + |
| 12 | +# SWSPI |
| 13 | + |
| 14 | +Arduino library for SoftWare SPI. |
| 15 | + |
| 16 | + |
| 17 | +## Description |
| 18 | + |
| 19 | +**Experimental, use with care.** |
| 20 | + |
| 21 | +The SPI protocol is a "standard" for serial communication, however there exists many variants. |
| 22 | +This document only describe the four modi typical used in the Arduino context. |
| 23 | + |
| 24 | +The SWSPI library implements a software implementation of SPI with 4 different modi. |
| 25 | + |
| 26 | +The library is written for debugging purposes and might evolve to a real software SPI |
| 27 | +implementation. It is shared as it might be useful for others. |
| 28 | + |
| 29 | +- educational and debug purpose for now. |
| 30 | +- very limited tested |
| 31 | +- master only |
| 32 | +- no optimized performance |
| 33 | + - MSBFIRST is reversed LSBFIRST (reduces code size). |
| 34 | +- does not implement SPIClass (yet), |
| 35 | + - goal is to follow the SPI API so it can be done in some future. |
| 36 | + |
| 37 | +**Experimental, use with care.** |
| 38 | + |
| 39 | + |
| 40 | +### SPI modes |
| 41 | + |
| 42 | +There are 4 SPI MODES which encodes the clock polarity, phase and idle state. |
| 43 | +The mode also defines at which edge data out should be present and when |
| 44 | +data in can be read. |
| 45 | + |
| 46 | +The mode used is set in the **setTransaction()** call. |
| 47 | + |
| 48 | +| SPI MODE | polarity | phase | idle | data out | data in | |
| 49 | +|:----------:|:----------:|:-------:|:------:|:----------:|:----------:| |
| 50 | +| 0 | 0 | 0 | 0 | rising | falling | |
| 51 | +| 1 | 0 | 1 | 0 | falling | rising | |
| 52 | +| 2 | 1 | 0 | 1 | falling | rising | |
| 53 | +| 3 | 1 | 1 | 1 | rising | falling | |
| 54 | + |
| 55 | + |
| 56 | +### Basic SPI transaction |
| 57 | + |
| 58 | +```cpp |
| 59 | + // set pins in "start position" |
| 60 | + myspi.beginTransaction(bitOrder, spiMode); |
| 61 | + |
| 62 | + // select the device to communicate with |
| 63 | + // must be done after beginTransaction() to prevent "false clock edges". |
| 64 | + digitalWrite(selectPin, LOW); |
| 65 | + // do the IO |
| 66 | + data_in = myspi.transfer(data_out); |
| 67 | + // unslect the device when ready. |
| 68 | + digitalWrite(selectPin, HIGH); |
| 69 | + |
| 70 | + // close the transaction. |
| 71 | + myspi.endTransaction(); |
| 72 | +``` |
| 73 | +
|
| 74 | +
|
| 75 | +### Related |
| 76 | +
|
| 77 | +- https://github.com/RobTillaart/FastShiftIn |
| 78 | +- https://github.com/RobTillaart/FastShiftInOut |
| 79 | +- https://github.com/RobTillaart/FastShiftOut |
| 80 | +- https://github.com/RobTillaart/SWSPI |
| 81 | +- https://docs.arduino.cc/learn/communication/spi/ |
| 82 | +- https://en.wikipedia.org/wiki/Serial_Peripheral_Interface |
| 83 | +
|
| 84 | +
|
| 85 | +## Interface |
| 86 | +
|
| 87 | +```cpp |
| 88 | +#include SWSPI.h |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | +### Base |
| 93 | + |
| 94 | +- **SWSPI(uint8_t dataIn, uint8_t dataOut, uint8_t clock))** constructor. |
| 95 | +- **void begin()** |
| 96 | +- **void end()** |
| 97 | +- **void setBitOrder(uint8_t bitOrder)** bitOrder must be MSBFIRST or LSBFIRST |
| 98 | +- **void setDataMode(uint8_t dataMode)** dataMode must be in range 0..3 |
| 99 | + |
| 100 | +### Transfer |
| 101 | + |
| 102 | +- **void beginTransaction(uint8_t bitOrder, uint8_t dataMode)** |
| 103 | + - bitOrder must be MSBFIRST or LSBFIRST |
| 104 | + - dataMode must be in range 0..3 |
| 105 | +- **void endTransaction()** |
| 106 | +- **uint8_t transfer(uint8_t data)** |
| 107 | +- **uint16_t transfer16(uint16_t data)** |
| 108 | +- **void transfer(const void \*buf, size_t count)** |
| 109 | + |
| 110 | + |
| 111 | +## Future |
| 112 | + |
| 113 | +#### Must |
| 114 | + |
| 115 | +- elaborate documentation |
| 116 | + - links to SPI explainers. |
| 117 | +- test |
| 118 | +- implement SPIclass interface |
| 119 | + |
| 120 | +#### Should |
| 121 | + |
| 122 | +- derived classes, read or write only. |
| 123 | + |
| 124 | +#### Could |
| 125 | + |
| 126 | +- add **void suppressInteruupts(bool flag = false)** |
| 127 | +- code to .cpp file? |
| 128 | +- add transfer24() |
| 129 | +- unit test? |
| 130 | + |
| 131 | + |
| 132 | +#### Won't (unless requested) |
| 133 | + |
| 134 | + |
| 135 | +## Support |
| 136 | + |
| 137 | +If you appreciate my libraries, you can support the development and maintenance. |
| 138 | +Improve the quality of the libraries by providing issues and Pull Requests, or |
| 139 | +donate through PayPal or GitHub sponsors. |
| 140 | + |
| 141 | +Thank you, |
| 142 | + |
0 commit comments