Skip to content

Commit ed20c06

Browse files
Jordan Yatescarlescufi
authored andcommitted
spi: spi_nrfx_spim: limit clock frequency by inst
Limit the maximum SPI frequency to that supported by the instance hardware. This stops peripherals supporting >8MHz on slow instances from wrapping around on the clock frequency for undefined behaviour. Fixes #34402 Signed-off-by: Jordan Yates <[email protected]>
1 parent 653c987 commit ed20c06

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

drivers/spi/spi_nrfx_spim.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct spi_nrfx_data {
3131
struct spi_nrfx_config {
3232
nrfx_spim_t spim;
3333
size_t max_chunk_len;
34+
uint32_t max_freq;
3435
nrfx_spim_config_t config;
3536
};
3637

@@ -108,6 +109,7 @@ static int configure(const struct device *dev,
108109
{
109110
struct spi_context *ctx = &get_dev_data(dev)->ctx;
110111
const nrfx_spim_t *spim = &get_dev_config(dev)->spim;
112+
nrf_spim_frequency_t freq;
111113

112114
if (spi_context_configured(ctx, spi_cfg)) {
113115
/* Already configured. No need to do it again. */
@@ -144,11 +146,14 @@ static int configure(const struct device *dev,
144146
ctx->config = spi_cfg;
145147
spi_context_cs_configure(ctx);
146148

149+
/* Limit the frequency to that supported by the SPIM instance */
150+
freq = get_nrf_spim_frequency(MIN(spi_cfg->frequency,
151+
get_dev_config(dev)->max_freq));
152+
147153
nrf_spim_configure(spim->p_reg,
148154
get_nrf_spim_mode(spi_cfg->operation),
149155
get_nrf_spim_bit_order(spi_cfg->operation));
150-
nrf_spim_frequency_set(spim->p_reg,
151-
get_nrf_spim_frequency(spi_cfg->frequency));
156+
nrf_spim_frequency_set(spim->p_reg, freq);
152157

153158
return 0;
154159
}
@@ -424,6 +429,7 @@ static int spim_nrfx_pm_control(const struct device *dev,
424429
static const struct spi_nrfx_config spi_##idx##z_config = { \
425430
.spim = NRFX_SPIM_INSTANCE(idx), \
426431
.max_chunk_len = (1 << SPIM##idx##_EASYDMA_MAXCNT_SIZE) - 1, \
432+
.max_freq = SPIM##idx##_MAX_DATARATE * 1000000, \
427433
.config = { \
428434
.sck_pin = SPIM_PROP(idx, sck_pin), \
429435
.mosi_pin = SPIM_PROP(idx, mosi_pin), \

0 commit comments

Comments
 (0)