From 47dc76fc3426b542096d8811c846a575178f9d89 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 18 Oct 2020 19:52:00 +0200 Subject: [PATCH] nrf: give more flexibility in picking SPI speeds Instead of only allowing a limited number of speeds, use the provided speed as an upper bound on the allowed speed. The reasoning is that picking a higher speed than requrested will likely result in malfunction while picking a lower speed will usually only result in slower operation. This behavior matches the ESP32 at least. --- src/machine/machine_nrf.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/machine/machine_nrf.go b/src/machine/machine_nrf.go index 39743bf5..e6109e99 100644 --- a/src/machine/machine_nrf.go +++ b/src/machine/machine_nrf.go @@ -357,23 +357,25 @@ func (spi SPI) Configure(config SPIConfig) { // set frequency var freq uint32 - switch config.Frequency { - case 125000: - freq = nrf.SPI_FREQUENCY_FREQUENCY_K125 - case 250000: - freq = nrf.SPI_FREQUENCY_FREQUENCY_K250 - case 500000: - freq = nrf.SPI_FREQUENCY_FREQUENCY_K500 - case 1000000: - freq = nrf.SPI_FREQUENCY_FREQUENCY_M1 - case 2000000: - freq = nrf.SPI_FREQUENCY_FREQUENCY_M2 - case 4000000: - freq = nrf.SPI_FREQUENCY_FREQUENCY_M4 - case 8000000: + if config.Frequency == 0 { + config.Frequency = 4000000 // 4MHz + } + + switch { + case config.Frequency >= 8000000: freq = nrf.SPI_FREQUENCY_FREQUENCY_M8 - default: + case config.Frequency >= 4000000: + freq = nrf.SPI_FREQUENCY_FREQUENCY_M4 + case config.Frequency >= 2000000: + freq = nrf.SPI_FREQUENCY_FREQUENCY_M2 + case config.Frequency >= 1000000: + freq = nrf.SPI_FREQUENCY_FREQUENCY_M1 + case config.Frequency >= 500000: freq = nrf.SPI_FREQUENCY_FREQUENCY_K500 + case config.Frequency >= 250000: + freq = nrf.SPI_FREQUENCY_FREQUENCY_K250 + default: // below 250kHz, default to the lowest speed available + freq = nrf.SPI_FREQUENCY_FREQUENCY_K125 } spi.Bus.FREQUENCY.Set(freq)