@@ -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.
6970static 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
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- 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
189193bool common_hal_busio_spi_try_lock (busio_spi_obj_t * self ) {
0 commit comments