Skip to content

Commit fdb631c

Browse files
jfischer-nocarlescufi
authored andcommitted
drivers: spi_nrfx_spim: bring back get_nrf_spim_frequency
Commit 246393e ("drivers: spi: spi_nrfx_spim: Remove nrf_frequency_t handling")' introduced two changes, one of them is removing the function get_nrf_spim_frequency with a strange justification. This change breaks support for peripherals written in a common way, where the maximum frequency is set to the maximum supported by the peripheral, not the controller, see shields for example. On the occasion of bringing it back, the original function was refactored to be easier to read and understand. Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no> Signed-off-by: Andrzej Głąbek <andrzej.glabek@nordicsemi.no>
1 parent 0972cb9 commit fdb631c

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

drivers/spi/spi_nrfx_spim.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,31 @@ struct spi_nrfx_config {
5757

5858
static void event_handler(const nrfx_spim_evt_t *p_event, void *p_context);
5959

60+
static inline uint32_t get_nrf_spim_frequency(uint32_t frequency)
61+
{
62+
/* Get the highest supported frequency not exceeding the requested one.
63+
*/
64+
if (frequency >= MHZ(32) && NRF_SPIM_HAS_32_MHZ_FREQ) {
65+
return MHZ(32);
66+
} else if (frequency >= MHZ(16) && NRF_SPIM_HAS_16_MHZ_FREQ) {
67+
return MHZ(16);
68+
} else if (frequency >= MHZ(8)) {
69+
return MHZ(8);
70+
} else if (frequency >= MHZ(4)) {
71+
return MHZ(4);
72+
} else if (frequency >= MHZ(2)) {
73+
return MHZ(2);
74+
} else if (frequency >= MHZ(1)) {
75+
return MHZ(1);
76+
} else if (frequency >= KHZ(500)) {
77+
return KHZ(500);
78+
} else if (frequency >= KHZ(250)) {
79+
return KHZ(250);
80+
} else {
81+
return KHZ(125);
82+
}
83+
}
84+
6085
static inline nrf_spim_mode_t get_nrf_spim_mode(uint16_t operation)
6186
{
6287
if (SPI_MODE_GET(operation) & SPI_MODE_CPOL) {
@@ -143,7 +168,8 @@ static int configure(const struct device *dev,
143168
config = dev_config->def_config;
144169

145170
/* Limit the frequency to that supported by the SPIM instance. */
146-
config.frequency = MIN(spi_cfg->frequency, max_freq);
171+
config.frequency = get_nrf_spim_frequency(MIN(spi_cfg->frequency,
172+
max_freq));
147173
config.mode = get_nrf_spim_mode(spi_cfg->operation);
148174
config.bit_order = get_nrf_spim_bit_order(spi_cfg->operation);
149175

0 commit comments

Comments
 (0)