2424#warning "LED_BUILTIN is not defined."
2525#endif
2626
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
3134
3235uint8_t buffer_tx[] = " Thequickbrownfoxjumpsoverthelazydog\0 " ;
33- #define BUF_SIZE sizeof (buffer_tx)
36+ #define BUF_SIZE sizeof (buffer_tx)
3437uint8_t buffer_rx[BUF_SIZE] = {};
3538
3639/* SPI transfer loop nb */
3740#define TEST_LOOP_NB 9
3841
3942void setup () {
40- uint8_t tested_ok = 0 ; // test result
43+ uint8_t tested_ok = 0 ; // test result
4144
4245 SPI.setMOSI (MOSI_PIN);
4346 SPI.setMISO (MISO_PIN);
4447 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
4855
4956 /* the SPI pin configuration is done by the SPI.begin */
50- SPI.begin (CS_PIN );
57+ SPI.begin ();
5158
5259 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
5567 /* compare what is received to what was sent */
5668 if (!memcmp (buffer_tx, buffer_rx, BUF_SIZE)) {
5769 /* this transfer passed */
@@ -60,14 +72,14 @@ void setup() {
6072 memset (buffer_rx, 0 , BUF_SIZE);
6173 }
6274 /* 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
6577 if (tested_ok == TEST_LOOP_NB) {
66- /* success */
67- digitalWrite (LED_BUILTIN, HIGH);
78+ /* success */
79+ digitalWrite (LED_BUILTIN, HIGH);
6880 } 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);
7183 }
7284}
7385
0 commit comments