From 738a0e9d1e7998c3b0c546ad432ac6b2047fa1ec Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Thu, 17 Dec 2020 11:11:34 +0100 Subject: [PATCH] Limit max SPI clock frequency to F_CPU/2 Fixes https://github.com/arduino/ArduinoCore-samd/issues/576 From https://ww1.microchip.com/downloads/en/DeviceDoc/SAM_D21_DA1_Family_DataSheet_DS40001882F.pdf , Table 25-2 Syncronous mode (the one we are using) condition is fBAUD <= fref/2 . Since fref can be as high as F_CPU (48MHz) let's limit the possible fBAUD to F_CPU/2 . --- libraries/SPI/SPI.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libraries/SPI/SPI.cpp b/libraries/SPI/SPI.cpp index 6b34e13de..f355e2378 100644 --- a/libraries/SPI/SPI.cpp +++ b/libraries/SPI/SPI.cpp @@ -93,8 +93,13 @@ void SPIClass::config(SPISettings settings) this->settings = settings; _p_sercom->disableSPI(); - _p_sercom->initSPI(_padTx, _padRx, SPI_CHAR_SIZE_8_BITS, getBitOrder(settings)); - _p_sercom->initSPIClock(getDataMode(settings), settings.getClockFreq()); + uint32_t clock_freq = settings.getClockFreq(); + if (clock_freq > F_CPU/2) { + clock_freq = F_CPU/2; + } + + _p_sercom->initSPI(_padTx, _padRx, SPI_CHAR_SIZE_8_BITS, getBitOrder(settings)); + _p_sercom->initSPIClock(getDataMode(settings), clock_freq); _p_sercom->enableSPI(); }