|
6 | 6 |
|
7 | 7 | #include <drivers/spi.h> |
8 | 8 | #include <nrfx_spim.h> |
| 9 | +#include <hal/nrf_clock.h> |
9 | 10 | #include <string.h> |
10 | 11 |
|
11 | 12 | #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) |
104 | 105 | static int configure(const struct device *dev, |
105 | 106 | const struct spi_config *spi_cfg) |
106 | 107 | { |
| 108 | + const struct spi_nrfx_config *dev_config = get_dev_config(dev); |
107 | 109 | 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; |
110 | 113 |
|
111 | 114 | if (spi_context_configured(ctx, spi_cfg)) { |
112 | 115 | /* Already configured. No need to do it again. */ |
@@ -143,14 +146,24 @@ static int configure(const struct device *dev, |
143 | 146 | ctx->config = spi_cfg; |
144 | 147 | spi_context_cs_configure(ctx); |
145 | 148 |
|
| 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 |
146 | 159 | /* 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)); |
149 | 162 |
|
150 | 163 | nrf_spim_configure(spim->p_reg, |
151 | 164 | get_nrf_spim_mode(spi_cfg->operation), |
152 | 165 | 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); |
154 | 167 |
|
155 | 168 | return 0; |
156 | 169 | } |
|
0 commit comments