Skip to content

Commit 8ea5a31

Browse files
ABOSTMFRASTMfpistm
authored
New test for SPI loop transfer (#45)
* 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 <francois.ramu@st.com> * Update examples/NonReg/SPI_loop/SPI_loop.ino Co-authored-by: Francois Ramu <francois.ramu@st.com> Co-authored-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 371bf9b commit 8ea5a31

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

examples/NonReg/SPI_loop/SPI_loop.ino

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
SPI_loopback
3+
4+
Test the loopback transfer on SPI.
5+
A single SPI instance is used, the default one.
6+
MISO pin must be externally connected to MOSI pin.
7+
To test the transfer on different SPI instance, just redefine SPI pins
8+
as follows:
9+
#define MOSI_PIN XXX
10+
#define SCK_PIN XXX
11+
#define MISO_PIN XXX
12+
#define CS_PIN XXX
13+
14+
Test behavior:
15+
A test string is sent 9 times and compared to the Rx frame.
16+
The LED remains ON when test ends successfully
17+
The LED remains OFF in case of error.
18+
This example code is in the public domain.
19+
*/
20+
#include "SPI.h"
21+
22+
#define MOSI_PIN PIN_SPI_MOSI
23+
#define MISO_PIN PIN_SPI_MISO
24+
#define SCK_PIN PIN_SPI_SCK
25+
#define CS_PIN PIN_SPI_SS
26+
27+
uint8_t buffer_tx[] = "Thequickbrownfoxjumpsoverthelazydog\0";
28+
#define BUF_SIZE sizeof(buffer_tx)
29+
uint8_t buffer_rx[BUF_SIZE] = {};
30+
31+
/* SPI transfer loop nb */
32+
#define TEST_LOOP_NB 9
33+
34+
void setup() {
35+
uint8_t tested_ok = 0; // test result
36+
37+
SPI.setMOSI(MOSI_PIN);
38+
SPI.setMISO(MISO_PIN);
39+
SPI.setSCLK(SCK_PIN);
40+
// Don't set CS_PIN to have Software ChipSelect management
41+
// Instead, CS_PIN is passed as argument to SPI.transfer(CS_PIN)
42+
// SPI.setSSEL(CS_PIN);
43+
44+
/* the SPI pin configuration is done by the SPI.begin */
45+
SPI.begin(CS_PIN);
46+
47+
for (uint8_t received = 0; received < TEST_LOOP_NB; received++) {
48+
SPI.transfer(CS_PIN, buffer_tx, buffer_rx, BUF_SIZE, SPI_LAST);
49+
50+
/* compare what is received to what was sent */
51+
if (!memcmp(buffer_tx, buffer_rx, BUF_SIZE)) {
52+
/* this transfer passed */
53+
tested_ok++;
54+
}
55+
memset(buffer_rx, 0, BUF_SIZE);
56+
}
57+
/* display test result */
58+
pinMode(LED_BUILTIN, OUTPUT); // Configure LED pin, for test result
59+
digitalWrite(LED_BUILTIN, LOW); // start with led off
60+
if (tested_ok == TEST_LOOP_NB) {
61+
/* success */
62+
digitalWrite(LED_BUILTIN, HIGH);
63+
} else {
64+
/* error. Please verify MISO is externally connected to MOSI */
65+
digitalWrite(LED_BUILTIN, LOW);
66+
}
67+
}
68+
69+
void loop() {
70+
/* nothing to do */
71+
}

0 commit comments

Comments
 (0)