SPI

SPI class Reference
SPI API Reference

SPI is a communication method for exchanging data between computers.

Differences from UART, which is commonly used are as follows:

  • Steady and high speed communication is possible because of a clock and signal lines.
  • SPI "exchanges" data, does not just send or receive it. If it sends 1 byte, it always receive 1 byte from the other party.
  • Communicate using master-slave architecture, and the slave device cannot send by itself. The communication starts when the master device want to.

It is used for communication with sensors rather than with computers. The microcomputer tell the sensor what to do and receive data instead, using SPI. It is attractive that the program is easy because the master can communicate at any time it like and that very high speed communication is possible.

Connect the other party with three lines; this is the only method obniz uses: CLK to send the time when the master device exchange data, MOSI (Master Out Slave In) to send data from the slave to the master, and MISO for the master to receive data from the slave. MISO may not be connected when the master may not need to receive data from the slave.

Initial settings

Available SPI depends on a device. For example, obniz Board has SPI0 and SPI1. The SPI is different from IO, and use it so that each CLK or MOSI of SPI can be used in any of the 12 IO.

You can get unused SPI by obniz.getFreeSpi(). spi.start() starts SPI. Set the IO and the frequency. For example, if you set IO1 to MOSI, IO1 to MISO, IO2 to CLK, and the frequency is 100kHz, the code is as follows.

// Javascript Example

var spi = obniz.getFreeSpi();
spi.start({mode:"master", mosi:0, miso:1, clk :2, frequency:100000}); 

If you know the SPI which is certainly unused, you can use it directly rather than use obniz.getFreeSpi().

// Javascript Example

obniz.spi0.start({mode:"master", mosi:0, miso:1, clk :2, frequency:100000}); 

As for "mode", set obniz to master so that it can send data by itself. Currently only master mode is supported.
"frequency" is the communication frequency, which sent from the master by CLK. Select the speed at which you can communicate with the other party. Available frequencies depend on the device and the drive method of IO. In many cases, push-pull3v enables higher-speed communication such as 2MHz.

The drive method and pull-up-down of MOSI, MISO and CLK can be changed as shown below. You do not have to use all of MOSI, MISO and CLK, though you need to specify at least one of them; it is possible to use only MOSI and CLK, without MISO.

// Javascript Example

var spi = obniz.getFreeSpi();
spi.start({mode:"master", mosi:0, miso:1, clk :2, frequency:100000, drive:"3v", pull:null}); 

''drive" can change the drive method, and "pull" can change pull-up-down. You can specify the same thing as the ones set by io.drive() and io.pull().

After the setting is completed, communication is possible.

End

The started SPI ends by spi.end().

// Javascript Example

var spi = obniz.getFreeSpi();
spi.start({mode:"master", clk :0, mosi:1, miso:2, clock:1000000});
spi.write([0x12, 0x98]);
spi.end();

Articles