Skip to content

Commit

Permalink
New test for SPI loop transfer (#45)
Browse files Browse the repository at this point in the history
* New test for SPI loop transfer

This sketch is testing the SPI Tx to Rx transfer
when the SPI MISO is connected to the SPI MOSI pin
of the hardware board.

Signed-off-by: Francois Ramu <[email protected]>

* Update examples/NonReg/SPI_loop/SPI_loop.ino

Co-authored-by: Francois Ramu <[email protected]>
Co-authored-by: Frederic Pillon <[email protected]>
  • Loading branch information
3 people authored Apr 20, 2021
1 parent 371bf9b commit 8ea5a31
Showing 1 changed file with 71 additions and 0 deletions.
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 */
}

0 comments on commit 8ea5a31

Please sign in to comment.