private void write(byte data) throws IOException{ channelSelect.low(); spi.write(data); channelSelect.high();}
private void write(byte data) throws IOException{ channelSelect.low();
// MSB first manual data transmission for (int i = 0; i < 8; i++) { mosi.setState((data & 0x80) != 0); data = (byte) ((data << 1) & 0xFF); clock.low(); // NOP clock.high(); } channelSelect.high();}
So you don't have to manage the CS pin state in your code at all.
private void write(byte data) throws IOException{
spi.write(data); }
import com.pi4j.io.spi.SpiChannel;
import com.pi4j.io.spi.SpiDevice;
import com.pi4j.io.spi.SpiFactory;
import java.io.IOException;
public class SpiExample {
// SPI device
public static SpiDevice spi = null;
public static void main(String args[]) throws InterruptedException, IOException {
System.out.println("<--Pi4J--> SPI test program");
spi = SpiFactory.getInstance(SpiChannel.CS0,
SpiDevice.DEFAULT_SPI_SPEED, // default spi speed 1 MHz
SpiDevice.DEFAULT_SPI_MODE); // default spi mode 0
// create SPI payload
byte[] buffer = "Hello World".getBytes("US-ASCII");
// send one byte at a time
for(byte b : buffer){
spi.write(b);
}
}
}
This produces the following result (screenshot):
If this works, but is still not fast enough, then I could create more optimized SPI method to handle splitting up the byte buffer/array in the C native code in Pi4J.
Thanks, Robert
# Set DC low for command, high for data self._gpio.output(self._dc, is_data) |
spi.set_mode(0)
spi.write((byte) data);
I was also looking at an Adafruit example implementation in Python for the ILI9341:
// Init Pins and SPI
dataCommandSelect = GpioFactory.getInstance().provisionDigitalOutputPin(RaspiPin.GPIO_05); // 5) D/C
reset = GpioFactory.getInstance().provisionDigitalOutputPin(RaspiPin.GPIO_06, PinState.HIGH); // 6) RESET
//channelSelect = GpioFactory.getInstance().provisionDigitalOutputPin(RaspiPin.GPIO_10, PinState.HIGH); // 7) CS
spi = SpiFactory.getInstance(SpiChannel.CS0);
Thanks, Robert
This is because the GPIO pins used by SPI were provisioned as regular GPIO pins in MWN and they were left is some state that was not working with the SDI device driver.