24
24
#warning "LED_BUILTIN is not defined."
25
25
#endif
26
26
27
- #define MOSI_PIN PIN_SPI_MOSI
28
- #define MISO_PIN PIN_SPI_MISO
29
- #define SCK_PIN PIN_SPI_SCK
30
- #define CS_PIN PIN_SPI_SS
27
+ /* Set to 1 if CS have to be managed by the hardware */
28
+ #define CS_HARDWARE_CONTROL 0
29
+
30
+ #define MOSI_PIN PIN_SPI_MOSI
31
+ #define MISO_PIN PIN_SPI_MISO
32
+ #define SCK_PIN PIN_SPI_SCK
33
+ #define CS_PIN PIN_SPI_SS
31
34
32
35
uint8_t buffer_tx[] = " Thequickbrownfoxjumpsoverthelazydog\0 " ;
33
- #define BUF_SIZE sizeof (buffer_tx)
36
+ #define BUF_SIZE sizeof (buffer_tx)
34
37
uint8_t buffer_rx[BUF_SIZE] = {};
35
38
36
39
/* SPI transfer loop nb */
37
40
#define TEST_LOOP_NB 9
38
41
39
42
void setup () {
40
- uint8_t tested_ok = 0 ; // test result
43
+ uint8_t tested_ok = 0 ; // test result
41
44
42
45
SPI.setMOSI (MOSI_PIN);
43
46
SPI.setMISO (MISO_PIN);
44
47
SPI.setSCLK (SCK_PIN);
45
- // Don't set CS_PIN to have Software ChipSelect management
46
- // Instead, CS_PIN is passed as argument to SPI.transfer(CS_PIN)
47
- // SPI.setSSEL(CS_PIN);
48
+ #if CS_HARDWARE_CONTROL == 1
49
+ SPI.setSSEL (CS_PIN);
50
+ #endif
51
+ #if CS_HARDWARE_CONTROL == 0
52
+ pinMode (CS_PIN, OUTPUT);
53
+ digitalWrite (CS_PIN, HIGH);
54
+ #endif
48
55
49
56
/* the SPI pin configuration is done by the SPI.begin */
50
- SPI.begin (CS_PIN );
57
+ SPI.begin ();
51
58
52
59
for (uint8_t received = 0 ; received < TEST_LOOP_NB; received++) {
53
- SPI.transfer (CS_PIN, buffer_tx, buffer_rx, BUF_SIZE, SPI_LAST);
54
-
60
+ #if CS_HARDWARE_CONTROL == 0
61
+ digitalWrite (CS_PIN, LOW);
62
+ #endif
63
+ SPI.transfer (buffer_tx, buffer_rx, BUF_SIZE);
64
+ #if CS_HARDWARE_CONTROL == 0
65
+ digitalWrite (CS_PIN, HIGH);
66
+ #endif
55
67
/* compare what is received to what was sent */
56
68
if (!memcmp (buffer_tx, buffer_rx, BUF_SIZE)) {
57
69
/* this transfer passed */
@@ -60,14 +72,14 @@ void setup() {
60
72
memset (buffer_rx, 0 , BUF_SIZE);
61
73
}
62
74
/* display test result */
63
- pinMode (LED_BUILTIN, OUTPUT); // Configure LED pin, for test result
64
- digitalWrite (LED_BUILTIN, LOW); // start with led off
75
+ pinMode (LED_BUILTIN, OUTPUT); // Configure LED pin, for test result
76
+ digitalWrite (LED_BUILTIN, LOW); // start with led off
65
77
if (tested_ok == TEST_LOOP_NB) {
66
- /* success */
67
- digitalWrite (LED_BUILTIN, HIGH);
78
+ /* success */
79
+ digitalWrite (LED_BUILTIN, HIGH);
68
80
} else {
69
- /* error. Please verify MISO is externally connected to MOSI */
70
- digitalWrite (LED_BUILTIN, LOW);
81
+ /* error. Please verify MISO is externally connected to MOSI */
82
+ digitalWrite (LED_BUILTIN, LOW);
71
83
}
72
84
}
73
85
0 commit comments