Skip to content

Commit 5f1581b

Browse files
ananglnashif
authored andcommitted
drivers: spi_nrfx_spim: Fix limiting of SPIM frequency on nRF5340
According to the nRF5340 PS, SPIM4 only supports 32 Mbps when the application core is running at 128 MHz. This patch adds the corresponding check. Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent c58243d commit 5f1581b

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

drivers/spi/spi_nrfx_spim.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <drivers/spi.h>
88
#include <nrfx_spim.h>
9+
#include <hal/nrf_clock.h>
910
#include <string.h>
1011

1112
#define LOG_DOMAIN "spi_nrfx_spim"
@@ -104,9 +105,11 @@ static inline nrf_spim_bit_order_t get_nrf_spim_bit_order(uint16_t operation)
104105
static int configure(const struct device *dev,
105106
const struct spi_config *spi_cfg)
106107
{
108+
const struct spi_nrfx_config *dev_config = get_dev_config(dev);
107109
struct spi_context *ctx = &get_dev_data(dev)->ctx;
108-
const nrfx_spim_t *spim = &get_dev_config(dev)->spim;
109-
nrf_spim_frequency_t freq;
110+
const nrfx_spim_t *spim = &dev_config->spim;
111+
uint32_t max_freq = dev_config->max_freq;
112+
nrf_spim_frequency_t spim_frequency;
110113

111114
if (spi_context_configured(ctx, spi_cfg)) {
112115
/* Already configured. No need to do it again. */
@@ -143,14 +146,24 @@ static int configure(const struct device *dev,
143146
ctx->config = spi_cfg;
144147
spi_context_cs_configure(ctx);
145148

149+
#if defined(CONFIG_SOC_NRF5340_CPUAPP)
150+
/* On nRF5340, the 32 Mbps speed is supported by the application core
151+
* when it is running at 128 MHz (see the Timing specifications section
152+
* in the nRF5340 PS).
153+
*/
154+
if (max_freq > 16000000 &&
155+
nrf_clock_hfclk_div_get(NRF_CLOCK) != NRF_CLOCK_HFCLK_DIV_1) {
156+
max_freq = 16000000;
157+
}
158+
#endif
146159
/* Limit the frequency to that supported by the SPIM instance */
147-
freq = get_nrf_spim_frequency(MIN(spi_cfg->frequency,
148-
get_dev_config(dev)->max_freq));
160+
spim_frequency = get_nrf_spim_frequency(MIN(spi_cfg->frequency,
161+
max_freq));
149162

150163
nrf_spim_configure(spim->p_reg,
151164
get_nrf_spim_mode(spi_cfg->operation),
152165
get_nrf_spim_bit_order(spi_cfg->operation));
153-
nrf_spim_frequency_set(spim->p_reg, freq);
166+
nrf_spim_frequency_set(spim->p_reg, spim_frequency);
154167

155168
return 0;
156169
}

0 commit comments

Comments
 (0)