Master Read/Write

After the SPI settings are completed, communication is possible. In Master mode, you are master and can send data at any time you like. You will not receive data without doing anything. With SPI, it receives as much data as it transmits. In other words, SPI is a communication method that "exchange data".

Use spi.writeWait() to send data. Set an array of data. In the example below, three bytes of [0x01, 0x02, 0x03] are transmitted. Then, the three bytes received from the other party will be the return value. The length of transmitted data and the one of received data are always same.

// Javascript Example

var spi = obniz.getFreeSpi();
spi.start({mode:"master", mosi:0, miso:1, clk :2, frequency:100000}); 
var ret = await spi.writeWait([0x01, 0x02, 0x03]);
console.log(ret.length); //=> 3

Like UART, SPI can communicate even if there is no actual partner. It will send data successfully and receive data that has the same length as the transmitted data, but the data received will be meaningless.

The amount of data sent at a time depends on the version of obnizOS or obniz.js. Normally, it is 1KByte.

Sending not need receiving data

spi.writeWait() sends data and waits until receiving data. In case received data is not needed, such as when the connection destination is a display, use spi.write().

// Javascript Example

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

The write() function sends data from MOSI like the writeWait() function, but ignores any data coming from MISO. A large amount of data or continuous data can be sent with the SPI of the device via the Internet, because it does not await.