esp32_Adafruit_BusIO/examples/spi_readwrite/spi_readwrite.ino

40 lines
932 B
Arduino
Raw Normal View History

2019-08-29 20:00:58 +01:00
#include <Adafruit_SPIDevice.h>
2019-05-18 04:36:14 +01:00
2019-08-29 20:00:58 +01:00
#define SPIDEVICE_CS 10
Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS);
2019-05-18 04:36:14 +01:00
void setup() {
while (!Serial) { delay(10); }
Serial.begin(115200);
2019-08-29 20:00:58 +01:00
Serial.println("SPI device read and write test");
2019-05-18 04:36:14 +01:00
2019-08-29 20:00:58 +01:00
if (!spi_dev.begin()) {
Serial.println("Could not initialize SPI device");
2019-05-18 04:36:14 +01:00
while (1);
}
uint8_t buffer[32];
2019-08-29 20:00:58 +01:00
2019-05-18 04:36:14 +01:00
// Try to read 32 bytes
2019-08-29 20:00:58 +01:00
spi_dev.read(buffer, 32);
2019-05-18 04:36:14 +01:00
Serial.print("Read: ");
for (uint8_t i=0; i<32; i++) {
Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
}
Serial.println();
// read a register by writing first, then reading
2019-08-29 20:00:58 +01:00
buffer[0] = 0x8F; // we'll reuse the same buffer
spi_dev.write_then_read(buffer, 1, buffer, 2, false);
2019-05-18 04:36:14 +01:00
Serial.print("Write then Read: ");
for (uint8_t i=0; i<2; i++) {
Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
}
Serial.println();
}
void loop() {
}