spi_readwrite.ino 963 B

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