Skip to content

Commit 4294d36

Browse files
joseangeljimenezcmaglie
authored andcommitted
Fix calculation of SPI_MIN_CLOCK_DIVIDER in SPI.h
The previous version of SPI.h works only for the SAMD21G18A variant and only for a 48 MHz system clock. For the rest of variants, it sets SPI_MIN_CLOCK_DIVIDER to 2, which is incorrect for a 48 MHz system clock. The SAMD21 datasheet specifies a typical SPI SCK period (tSCK) of 42 ns, see "Table 36-48. SPI Timing Characteristics and Requirements", which translates into a maximum SPI clock of 23.8 MHz. The new code conservatively sets the divider for a 12 MHz maximum SPI clock, taking into account any value for the system clock (not only 48 MHz). It also executes for other variants of the SAMD, not only SAMD21G18A. Can you, please, review this patch?
1 parent e00bcb2 commit 4294d36

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

libraries/SPI/SPI.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,12 @@
3737
#define SPI_MODE2 0x03
3838
#define SPI_MODE3 0x01
3939

40-
#if defined(__SAMD21G18A__)
41-
// Even if not specified on the datasheet, the SAMD21G18A MCU
42-
// doesn't operate correctly with clock dividers lower than 4.
43-
// This allows a theoretical maximum SPI clock speed of 12Mhz
44-
#define SPI_MIN_CLOCK_DIVIDER 4
45-
// Other SAMD21xxxxx MCU may be affected as well
46-
#else
47-
#define SPI_MIN_CLOCK_DIVIDER 2
40+
#if defined(ARDUINO_ARCH_SAMD)
41+
// The datasheet specifies a typical SPI SCK period (tSCK) of 42 ns,
42+
// see "Table 36-48. SPI Timing Characteristics and Requirements",
43+
// which translates into a maximum SPI clock of 23.8 MHz.
44+
// Conservatively, the divider is set for a 12 MHz maximum SPI clock.
45+
#define SPI_MIN_CLOCK_DIVIDER (uint8_t)(1 + ((F_CPU - 1) / 12000000))
4846
#endif
4947

5048
class SPISettings {

0 commit comments

Comments
 (0)