Skip to content

Commit a2c0a5b

Browse files
SAMD21: Fix freq clipping in SPI.h, allow 24 MHz SPI
SPI clock freq previously clipped at MAX_SPI/2 (6 MHz) rather than MAX_SPI. Now works correctly. Additionally, MAX_SPI set at 24 MHz on SAMD21. This is only slightly beyond spec and so far seems reliable with SD and SdFat tests on M0 Adalogger, eyes on HalloWing M0.
1 parent 1ab7cb6 commit a2c0a5b

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

libraries/SPI/SPI.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@
5252
// The datasheet specifies a typical SPI SCK period (tSCK) of 42 ns,
5353
// see "Table 36-48. SPI Timing Characteristics and Requirements",
5454
// which translates into a maximum SPI clock of 23.8 MHz.
55-
// Conservatively, the divider is set for a 12 MHz maximum SPI clock.
55+
// We'll permit use of 24 MHz SPI even though this is slightly out
56+
// of spec. Given how clock dividers work, the next "sensible"
57+
// threshold would be a substantial drop down to 12 MHz.
5658
#if !defined(MAX_SPI)
57-
#define MAX_SPI 12000000
59+
#define MAX_SPI 24000000
5860
#endif
5961
#define SPI_MIN_CLOCK_DIVIDER (uint8_t)(1 + ((F_CPU - 1) / MAX_SPI))
6062
#endif
@@ -81,7 +83,7 @@ class SPISettings {
8183
#if defined(__SAMD51__)
8284
this->clockFreq = clock; // Clipping handled in SERCOM.cpp
8385
#else
84-
this->clockFreq = (clock >= (MAX_SPI * 2 / SPI_MIN_CLOCK_DIVIDER) ? MAX_SPI * 2 / SPI_MIN_CLOCK_DIVIDER : clock);
86+
this->clockFreq = clock >= MAX_SPI ? MAX_SPI : clock;
8587
#endif
8688

8789
this->bitOrder = (bitOrder == MSBFIRST ? MSB_FIRST : LSB_FIRST);

0 commit comments

Comments
 (0)