Skip to content

New test for SPI loop transfer #45

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 2 commits into from
Apr 20, 2021
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
71 changes: 71 additions & 0 deletions examples/NonReg/SPI_loop/SPI_loop.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
SPI_loopback

Test the loopback transfer on SPI.
A single SPI instance is used, the default one.
MISO pin must be externally connected to MOSI pin.
To test the transfer on different SPI instance, just redefine SPI pins
as follows:
#define MOSI_PIN XXX
#define SCK_PIN XXX
#define MISO_PIN XXX
#define CS_PIN XXX

Test behavior:
A test string is sent 9 times and compared to the Rx frame.
The LED remains ON when test ends successfully
The LED remains OFF in case of error.
This example code is in the public domain.
*/
#include "SPI.h"

#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)
uint8_t buffer_rx[BUF_SIZE] = {};

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

void setup() {
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);

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

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

/* compare what is received to what was sent */
if (!memcmp(buffer_tx, buffer_rx, BUF_SIZE)) {
/* this transfer passed */
tested_ok++;
}
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
if (tested_ok == TEST_LOOP_NB) {
/* success */
digitalWrite(LED_BUILTIN, HIGH);
} else {
/* error. Please verify MISO is externally connected to MOSI */
digitalWrite(LED_BUILTIN, LOW);
}
}

void loop() {
/* nothing to do */
}