Skip to content

Commit 21d331c

Browse files
committed
round SPI freq down; check max freq
1 parent aa95526 commit 21d331c

File tree

2 files changed

+30
-26
lines changed
  • ports/nrf/common-hal/busio
  • shared-bindings/busio

2 files changed

+30
-26
lines changed

ports/nrf/common-hal/busio/SPI.c

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,27 @@ void spi_reset(void) {
6565
}
6666
}
6767

68-
// Convert frequency to clock-speed-dependent value. Choose the nearest value, lower or higher.
68+
// Convert frequency to clock-speed-dependent value. Choose the next lower baudrate if in between
69+
// available baudrates.
6970
static nrf_spim_frequency_t baudrate_to_spim_frequency(const uint32_t baudrate) {
7071

71-
// Round requested baudrate to nearest available baudrate.
7272
static const struct {
7373
const uint32_t boundary;
7474
nrf_spim_frequency_t spim_frequency;
7575
} baudrate_map[] = {
7676
#ifdef SPIM_FREQUENCY_FREQUENCY_M32
77-
{ (16000000 + 32000000) / 2, NRF_SPIM_FREQ_32M },
77+
{ 32000000, NRF_SPIM_FREQ_32M },
7878
#endif
7979
#ifdef SPIM_FREQUENCY_FREQUENCY_M16
80-
{ ( 8000000 + 16000000) / 2, NRF_SPIM_FREQ_16M },
80+
{ 16000000, NRF_SPIM_FREQ_16M },
8181
#endif
82-
{ ( 4000000 + 8000000) / 2, NRF_SPIM_FREQ_8M },
83-
{ ( 2000000 + 4000000) / 2, NRF_SPIM_FREQ_4M },
84-
{ ( 1000000 + 2000000) / 2, NRF_SPIM_FREQ_2M },
85-
{ ( 500000 + 1000000) / 2, NRF_SPIM_FREQ_1M },
86-
{ ( 250000 + 500000) / 2, NRF_SPIM_FREQ_500K },
87-
{ ( 125000 + 250000) / 2, NRF_SPIM_FREQ_250K },
88-
{ 0, NRF_SPIM_FREQ_125K },
82+
{ 8000000, NRF_SPIM_FREQ_8M },
83+
{ 4000000, NRF_SPIM_FREQ_4M },
84+
{ 2000000, NRF_SPIM_FREQ_2M },
85+
{ 1000000, NRF_SPIM_FREQ_1M },
86+
{ 500000, NRF_SPIM_FREQ_500K },
87+
{ 250000, NRF_SPIM_FREQ_250K },
88+
{ 0, NRF_SPIM_FREQ_125K },
8989
};
9090

9191
size_t i = 0;
@@ -97,7 +97,7 @@ static nrf_spim_frequency_t baudrate_to_spim_frequency(const uint32_t baudrate)
9797
}
9898
i++;
9999
} while (boundary != 0);
100-
// Will get here only if baudrate == 0.
100+
// Should not get here.
101101
return 0;
102102
}
103103

@@ -168,22 +168,26 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
168168
}
169169

170170
bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
171-
// nrf52 does not support 16 bit
172-
if (bits != 8)
171+
// nrf52 does not support 16 bit
172+
if (bits != 8) {
173173
return false;
174+
}
174175

175-
nrf_spim_frequency_set(self->spim_peripheral->spim.p_reg, baudrate_to_spim_frequency(baudrate));
176+
// Set desired frequency, rounding down, and don't go above available frequency for this SPIM.
177+
nrf_spim_frequency_set(self->spim_peripheral->spim.p_reg,
178+
baudrate_to_spim_frequency(MIN(baudrate,
179+
self->spim_peripheral->max_frequency_MHz * 1000000)));
176180

177-
nrf_spim_mode_t mode = NRF_SPIM_MODE_0;
178-
if (polarity) {
179-
mode = (phase) ? NRF_SPIM_MODE_3 : NRF_SPIM_MODE_2;
180-
} else {
181-
mode = (phase) ? NRF_SPIM_MODE_1 : NRF_SPIM_MODE_0;
182-
}
181+
nrf_spim_mode_t mode = NRF_SPIM_MODE_0;
182+
if (polarity) {
183+
mode = (phase) ? NRF_SPIM_MODE_3 : NRF_SPIM_MODE_2;
184+
} else {
185+
mode = (phase) ? NRF_SPIM_MODE_1 : NRF_SPIM_MODE_0;
186+
}
183187

184-
nrf_spim_configure(self->spim_peripheral->spim.p_reg, mode, NRF_SPIM_BIT_ORDER_MSB_FIRST);
188+
nrf_spim_configure(self->spim_peripheral->spim.p_reg, mode, NRF_SPIM_BIT_ORDER_MSB_FIRST);
185189

186-
return true;
190+
return true;
187191
}
188192

189193
bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {

shared-bindings/busio/SPI.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static void check_lock(busio_spi_obj_t *self) {
136136

137137
//| .. method:: SPI.configure(\*, baudrate=100000, polarity=0, phase=0, bits=8)
138138
//|
139-
//| Configures the SPI bus. Only valid when locked.
139+
//| Configures the SPI bus. The SPI object must be locked.
140140
//|
141141
//| :param int baudrate: the desired clock rate in Hertz. The actual clock rate may be higher or lower
142142
//| due to the granularity of available clock settings.
@@ -154,8 +154,8 @@ static void check_lock(busio_spi_obj_t *self) {
154154
//| and 8MHz. On the nRF52840, 16MHz and 32MHz are also available, but only on the first
155155
//| `busio.SPI` object you create. Two more ``busio.SPI`` objects can be created, but they are restricted
156156
//| to 8MHz maximum. This is a hardware restriction: there is only one high-speed SPI peripheral.
157-
//| If you pick a a baudrate other than one of these, the nearest available
158-
//| baudrate will be chosen.
157+
//| If you pick a a baudrate other than one of these, the nearest lower
158+
//| baudrate will be chosen, with a minimum of 125kHz.
159159
STATIC mp_obj_t busio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
160160
enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits };
161161
static const mp_arg_t allowed_args[] = {

0 commit comments

Comments
 (0)