| 1 | #include <Adafruit_SPIDevice.h> |
| 2 | |
| 3 | #define SPIDEVICE_CS 10 |
| 4 | Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS); |
| 5 | |
| 6 | void setup() { |
| 7 | while (!Serial) { |
| 8 | delay(10); |
| 9 | } |
| 10 | Serial.begin(115200); |
| 11 | Serial.println("SPI device read and write test"); |
| 12 | |
| 13 | if (!spi_dev.begin()) { |
| 14 | Serial.println("Could not initialize SPI device"); |
| 15 | while (1) |
| 16 | ; |
| 17 | } |
| 18 | |
| 19 | uint8_t buffer[32]; |
| 20 | |
| 21 | // Try to read 32 bytes |
| 22 | spi_dev.read(buffer, 32); |
| 23 | Serial.print("Read: "); |
| 24 | for (uint8_t i = 0; i < 32; i++) { |
| 25 | Serial.print("0x"); |
| 26 | Serial.print(buffer[i], HEX); |
| 27 | Serial.print(", "); |
| 28 | } |
| 29 | Serial.println(); |
| 30 | |
| 31 | // read a register by writing first, then reading |
| 32 | buffer[0] = 0x8F; // we'll reuse the same buffer |
| 33 | spi_dev.write_then_read(buffer, 1, buffer, 2, false); |
| 34 | Serial.print("Write then Read: "); |
| 35 | for (uint8_t i = 0; i < 2; i++) { |
| 36 | Serial.print("0x"); |
| 37 | Serial.print(buffer[i], HEX); |
| 38 | Serial.print(", "); |
| 39 | } |
| 40 | Serial.println(); |
| 41 | } |
| 42 | |
| 43 | void loop() {} |