Skip to content

fix(spi): update examples after SPI rework #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions examples/NonReg/SPI_loop/SPI_loop.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,46 @@
#warning "LED_BUILTIN is not defined."
#endif

#define MOSI_PIN PIN_SPI_MOSI
#define MISO_PIN PIN_SPI_MISO
#define SCK_PIN PIN_SPI_SCK
#define CS_PIN PIN_SPI_SS
/* Set to 1 if CS have to be managed by the hardware */
#define CS_HARDWARE_CONTROL 0

#define MOSI_PIN PIN_SPI_MOSI
#define MISO_PIN PIN_SPI_MISO
#define SCK_PIN PIN_SPI_SCK
#define CS_PIN PIN_SPI_SS

uint8_t buffer_tx[] = "Thequickbrownfoxjumpsoverthelazydog\0";
#define BUF_SIZE sizeof(buffer_tx)
#define BUF_SIZE sizeof(buffer_tx)
uint8_t buffer_rx[BUF_SIZE] = {};

/* SPI transfer loop nb */
#define TEST_LOOP_NB 9

void setup() {
uint8_t tested_ok = 0; // test result
uint8_t tested_ok = 0; // test result

SPI.setMOSI(MOSI_PIN);
SPI.setMISO(MISO_PIN);
SPI.setSCLK(SCK_PIN);
// Don't set CS_PIN to have Software ChipSelect management
// Instead, CS_PIN is passed as argument to SPI.transfer(CS_PIN)
// SPI.setSSEL(CS_PIN);
#if CS_HARDWARE_CONTROL == 1
SPI.setSSEL(CS_PIN);
#endif
#if CS_HARDWARE_CONTROL == 0
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
#endif

/* the SPI pin configuration is done by the SPI.begin */
SPI.begin(CS_PIN);
SPI.begin();

for (uint8_t received = 0; received < TEST_LOOP_NB; received++) {
SPI.transfer(CS_PIN, buffer_tx, buffer_rx, BUF_SIZE, SPI_LAST);

#if CS_HARDWARE_CONTROL == 0
digitalWrite(CS_PIN, LOW);
#endif
SPI.transfer(buffer_tx, buffer_rx, BUF_SIZE);
#if CS_HARDWARE_CONTROL == 0
digitalWrite(CS_PIN, HIGH);
#endif
/* compare what is received to what was sent */
if (!memcmp(buffer_tx, buffer_rx, BUF_SIZE)) {
/* this transfer passed */
Expand All @@ -60,14 +72,14 @@ void setup() {
memset(buffer_rx, 0, BUF_SIZE);
}
/* display test result */
pinMode(LED_BUILTIN, OUTPUT); // Configure LED pin, for test result
digitalWrite(LED_BUILTIN, LOW); // start with led off
pinMode(LED_BUILTIN, OUTPUT); // Configure LED pin, for test result
digitalWrite(LED_BUILTIN, LOW); // start with led off
if (tested_ok == TEST_LOOP_NB) {
/* success */
digitalWrite(LED_BUILTIN, HIGH);
/* success */
digitalWrite(LED_BUILTIN, HIGH);
} else {
/* error. Please verify MISO is externally connected to MOSI */
digitalWrite(LED_BUILTIN, LOW);
/* error. Please verify MISO is externally connected to MOSI */
digitalWrite(LED_BUILTIN, LOW);
}
}

Expand Down