-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathSPI_loop.ino
88 lines (76 loc) · 2.24 KB
/
SPI_loop.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
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"
#ifndef LED_BUILTIN
#define LED_BUILTIN PNUM_NOT_DEFINED
#warning "LED_BUILTIN is not defined."
#endif
/* 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)
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);
#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();
for (uint8_t received = 0; received < TEST_LOOP_NB; received++) {
#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 */
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 */
}