The sensor I'm trying to communicate with uses 3-wire SPI (MOSI is tied to SDIO/MISO through a resistor):
| Sensor pin | uC pin |
|------------|--------|
| NCS | SS |
| SDIO | MISO |
| SCLK | SCLK |
It uses a 2-byte protocol, where byte0[7] = R/W bit and byte0[6:0] = address, and byte1 = data. Because there's only one data line, I'm unsure whether the built-in SPI library is compatible with my device.
This is my current function to execute a read operation; data=0x0000 (read at address = 0b0000000):
void initSPI() {
vspi = new SPIClass(VSPI);
vspi->begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_CS);
pinMode(vspi->pinSS(), OUTPUT); //VSPI SS
digitalWrite(vspi->pinSS(), HIGH);
}
uint8_t spiCommand(SPIClass *spi, uint16_t data) {
uint8_t addrByte = (uint8_t)(data >> 8);
uint8_t dataByte = (uint8_t)(data & 0xFF);
spi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE3));
digitalWrite(spi->pinSS(), LOW);
spi->transfer(addrByte);
pinMode(VSPI_MISO, INPUT);
uint8_t dataRet = spi->transfer(0);
digitalWrite(spi->pinSS(), HIGH);
spi->endTransaction();
pinMode(VSPI_MISO, OUTPUT);
return dataRet;
}
uint16_t compileSPIProtocol(uint8_t rw, uint8_t addr, uint8_t data) {
uint16_t protocol = 0x0;
protocol = (rw << 7) | addr;
protocol = protocol << 8;
protocol = protocol | data;
return protocol;
}
The datasheet indicates that the host controller should release control of the SDIO (MOSI) line to the sensor on the falling edge of the last address bit. That's what the line pinMode(VSPI_MISO, INPUT); is supposed to do, but I'm not actually certain whether this the correct method.
The expected value of the register at 0x00 (product ID) should be 0x31, but is currently outputting 0x00.
Any ideas?
