File tree Expand file tree Collapse file tree 2 files changed +17
-3
lines changed Expand file tree Collapse file tree 2 files changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -97,7 +97,11 @@ void SPIClass::setDataMode(uint8_t mode)
97
97
98
98
void SPIClass::setClockDivider (uint8_t div)
99
99
{
100
- _p_sercom->setBaudrateSPI (div);
100
+ if (div < SPI_MIN_CLOCK_DIVIDER) {
101
+ _p_sercom->setBaudrateSPI (SPI_MIN_CLOCK_DIVIDER);
102
+ } else {
103
+ _p_sercom->setBaudrateSPI (div);
104
+ }
101
105
}
102
106
103
107
byte SPIClass::transfer (uint8_t data)
Original file line number Diff line number Diff line change 18
18
#define SPI_MODE2 0x03
19
19
#define SPI_MODE3 0x01
20
20
21
+ #if defined(__SAMD21G18A__)
22
+ // Even if not specified on the datasheet, the SAMD21G18A MCU
23
+ // doesn't operate correctly with clock dividers lower than 4.
24
+ // This allows a theoretical maximum SPI clock speed of 12Mhz
25
+ #define SPI_MIN_CLOCK_DIVIDER 4
26
+ // Other SAMD21xxxxx MCU may be affected as well
27
+ #else
28
+ #define SPI_MIN_CLOCK_DIVIDER 2
29
+ #endif
30
+
21
31
class SPISettings {
22
32
public:
23
33
SPISettings (uint32_t clock, BitOrder bitOrder, uint8_t dataMode) {
@@ -36,8 +46,8 @@ class SPISettings {
36
46
uint8_t div;
37
47
if (clock < (F_CPU / 255 )) {
38
48
div = 255 ;
39
- } else if (clock >= (F_CPU / 2 )) {
40
- div = 2 ;
49
+ } else if (clock >= (F_CPU / SPI_MIN_CLOCK_DIVIDER )) {
50
+ div = SPI_MIN_CLOCK_DIVIDER ;
41
51
} else {
42
52
div = (F_CPU / (clock + 1 )) + 1 ;
43
53
}
You can’t perform that action at this time.
0 commit comments