Latest Version: 0.1.0
This library provides software-based bit-bang SPI (Serial Peripheral Interface) that can be used as an alternative to the imp API’s hardware.spi object. This class contains the same read and write methods as the imp API.
Note The only supported SPI mode is currently 0 (CPOL 0, CPHA 0) with the most significant bit sent first. Clock speed cannot be configured when using this class.
You can view the library’s source code on GitHub. Click here to see information on other versions of this library.
To add this library to your project, add #require "SoftwareSPI.device.lib.nut:0.1.0"
to the top of your device code
Parameter | Type | Required? | Description |
---|---|---|---|
sclk | imp pin object | Yes | The serial clock signal |
mosi | imp pin object | Yes | The data output |
miso | imp pin object | Yes | The data input |
Each of the imp pin objects will be configured by the class.
Note This class does not configure or toggle a chip-select pin. Your application should take care of this functionality.
local sclk = hardware.pinA; // Clock
local mosi = hardware.pinB; // Master Output
local miso = hardware.pinC; // Master Input
local sspi = SoftwareSPI(sclk, mosi, miso);
This method writes the specified data to the software SPI and returns the number of bytes written.
Parameter | Type | Required? | Description |
---|---|---|---|
data | String or blob | Yes | The data to be sent via SPI |
Integer — the number of bytes written.
// Configure chip select
local cs = hardware.pinD;
cs.configure(DIGITAL_OUT, 1);
// Write data to a blob
local value = blob(4);
value.writen(0xDEADBEEF, 'i');
// Write data to SPI
cs.write(0);
sspi.write(value);
cs.write(1);
This method writes to, and concurrently reads data from, the software SPI. The size and type of the data returned matches the size and type of the data sent.
Parameter | Type | Required? | Description |
---|---|---|---|
data | String or blob | Yes | The data to be sent via SPI |
String or blob — the data read from SPI.
// Configure chip select
local cs = hardware.pinD;
cs.configure(DIGITAL_OUT, 1);
// Write and read data to/from SPI
cs.write(0);
local value = sspi.writeread("\xFF");
cs.write(1);
server.log(value);
This method reads the specified number of bytes from the software SPI and returns it as a string.
Parameter | Type | Required? | Description |
---|---|---|---|
numberOfBytes | Integer | Yes | The number of bytes to be read from SPI |
String — the data read from SPI.
// Configure chip select
local cs = hardware.pinD;
cs.configure(DIGITAL_OUT, 1);
// Read 8 bytes of data from SPI and log it
cs.write(0);
local value = sspi.readstring(8);
cs.write(1);
server.log(value);
This method reads the specified number of bytes from the software SPI and returns it as a Squirrel blob.
Parameter | Type | Required? | Description |
---|---|---|---|
numberOfBytes | Integer | Yes | The number of bytes to be read from SPI |
Blob — the data read from SPI.
// Configure chip select
local cs = hardware.pinD;
cs.configure(DIGITAL_OUT, 1);
// Read 2 bytes of data from SPI and log it
cs.write(0);
local value = spi.readblob(2);
cs.write(1);
server.log(value);
The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.
Version | Source Code | Notes |
---|---|---|
0.1.0 | GitHub | Initial release |
The SoftwareSPI library is licensed under the MIT License.