@@ -65,40 +65,40 @@ void spi_reset(void) {
6565 }
6666}
6767
68- // Convert frequency to clock-speed-dependent value
68+ // Convert frequency to clock-speed-dependent value. Choose the next lower baudrate if in between
69+ // available baudrates.
6970static nrf_spim_frequency_t baudrate_to_spim_frequency (const uint32_t baudrate ) {
70- if (baudrate <= 125000 ) {
71- return NRF_SPIM_FREQ_125K ;
72- }
73- if (baudrate <= 250000 ) {
74- return NRF_SPIM_FREQ_250K ;
75- }
76- if (baudrate <= 500000 ) {
77- return NRF_SPIM_FREQ_500K ;
78- }
79- if (baudrate <= 1000000 ) {
80- return NRF_SPIM_FREQ_1M ;
81- }
82- if (baudrate <= 2000000 ) {
83- return NRF_SPIM_FREQ_2M ;
84- }
85- if (baudrate <= 4000000 ) {
86- return NRF_SPIM_FREQ_4M ;
87- }
88- if (baudrate <= 8000000 ) {
89- return NRF_SPIM_FREQ_8M ;
90- }
91- #ifdef SPIM_FREQUENCY_FREQUENCY_M16
92- if (baudrate <= 16000000 ) {
93- return NRF_SPIM_FREQ_16M ;
94- }
95- #endif
9671
72+ static const struct {
73+ const uint32_t boundary ;
74+ nrf_spim_frequency_t spim_frequency ;
75+ } baudrate_map [] = {
9776#ifdef SPIM_FREQUENCY_FREQUENCY_M32
98- return NRF_SPIM_FREQ_32M ;
99- #else
100- return NRF_SPIM_FREQ_8M ;
77+ { 32000000 , NRF_SPIM_FREQ_32M },
78+ #endif
79+ #ifdef SPIM_FREQUENCY_FREQUENCY_M16
80+ { 16000000 , NRF_SPIM_FREQ_16M },
10181#endif
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 },
89+ };
90+
91+ size_t i = 0 ;
92+ uint32_t boundary ;
93+ do {
94+ boundary = baudrate_map [i ].boundary ;
95+ if (baudrate >= boundary ) {
96+ return baudrate_map [i ].spim_frequency ;
97+ }
98+ i ++ ;
99+ } while (boundary != 0 );
100+ // Should not get here.
101+ return 0 ;
102102}
103103
104104void common_hal_busio_spi_construct (busio_spi_obj_t * self , const mcu_pin_obj_t * clock , const mcu_pin_obj_t * mosi , const mcu_pin_obj_t * miso ) {
@@ -168,26 +168,26 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
168168}
169169
170170bool 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- if (baudrate > self -> spim_peripheral -> max_frequency_MHz * 1000000 ) {
176- mp_raise_ValueError (translate ("Baud rate too high for this SPI peripheral" ));
177- return false;
178- }
179- 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 )));
180180
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- }
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+ }
187187
188- 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 );
189189
190- return true;
190+ return true;
191191}
192192
193193bool common_hal_busio_spi_try_lock (busio_spi_obj_t * self ) {
0 commit comments