Skip to content

Commit aebb27d

Browse files
committed
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>
1 parent a71796f commit aebb27d

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
SPI_loopback
3+
4+
Test the loopback transfer on SPI serial link.
5+
A test string is sent 9 times and compared to the Rx frame.
6+
when the MISO pin is physically connected to the MOSI pin of the SPI bus
7+
Default test the SPI_A link.
8+
Led remains ON when test ends successfully (off in case of error).
9+
To test the transfer on the SPI_B , just define SPI_B pins and connect,
10+
as follows:
11+
int MOSI_PIN = 22; // to be connected to the B_MISO
12+
int SCK_PIN = 23;
13+
int CS_PIN = 24;
14+
int MISO_PIN = 25; // to be connected to the B_MOSI
15+
and configure as follows:
16+
SPI.setMISO(MISO_PIN);
17+
SPI.setMOSI(MOSI_PIN);
18+
SPI.setSCLK(SCK_PIN);
19+
SPI.setSSEL(CS_PIN);
20+
21+
This example code is in the public domain.
22+
23+
*/
24+
#include "SPI.h"
25+
26+
#define MOSI_PIN PIN_SPI_MOSI
27+
#define SCK_PIN PIN_SPI_SCK
28+
#define CS_PIN PIN_SPI_SS
29+
#define MISO_PIN PIN_SPI_MOSI
30+
31+
#define BUF_SIZE 36
32+
uint8_t buffer_tx[] = "Thequickbrownfoxjumpsoverthelazydog\0";
33+
uint8_t buffer_rx[BUF_SIZE] = {};
34+
35+
/* SPI transfer loop nb */
36+
#define TEST_LOOP_NB 9
37+
38+
void setup() {
39+
uint8_t tested_ok = 0; // test result
40+
41+
/* the SPI pin configuration is done by the SPI.begin */
42+
SPI.begin(CS_PIN); // waking up SPI bus
43+
// SPI.setClockDivider(CS_PIN, 256); // setting a slower clock
44+
45+
pinMode(LED_BUILTIN, OUTPUT); // prepare led for status
46+
digitalWrite(LED_BUILTIN, LOW); // start with led off
47+
48+
for (uint8_t received = 0; received < TEST_LOOP_NB; received++) {
49+
SPI.transfer(CS_PIN, buffer_tx, buffer_rx, BUF_SIZE, SPI_LAST);
50+
delay(100);
51+
/* compare what is received to what was sent */
52+
if (!memcmp(buffer_tx, buffer_rx, BUF_SIZE)) {
53+
/* this transfer passed */
54+
tested_ok++;
55+
}
56+
memset(buffer_rx, 0, BUF_SIZE);
57+
}
58+
/* display test result */
59+
if (tested_ok == TEST_LOOP_NB) {
60+
/* success */
61+
digitalWrite(LED_BUILTIN, HIGH);
62+
} else {
63+
/* error */
64+
digitalWrite(LED_BUILTIN, LOW);
65+
}
66+
}
67+
68+
void loop() {
69+
/* nothing special */
70+
}
71+

0 commit comments

Comments
 (0)