@@ -65,27 +65,27 @@ void spi_reset(void) {
65
65
}
66
66
}
67
67
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.
69
70
static nrf_spim_frequency_t baudrate_to_spim_frequency (const uint32_t baudrate ) {
70
71
71
- // Round requested baudrate to nearest available baudrate.
72
72
static const struct {
73
73
const uint32_t boundary ;
74
74
nrf_spim_frequency_t spim_frequency ;
75
75
} baudrate_map [] = {
76
76
#ifdef SPIM_FREQUENCY_FREQUENCY_M32
77
- { ( 16000000 + 32000000 ) / 2 , NRF_SPIM_FREQ_32M },
77
+ { 32000000 , NRF_SPIM_FREQ_32M },
78
78
#endif
79
79
#ifdef SPIM_FREQUENCY_FREQUENCY_M16
80
- { ( 8000000 + 16000000 ) / 2 , NRF_SPIM_FREQ_16M },
80
+ { 16000000 , NRF_SPIM_FREQ_16M },
81
81
#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 },
89
89
};
90
90
91
91
size_t i = 0 ;
@@ -97,7 +97,7 @@ static nrf_spim_frequency_t baudrate_to_spim_frequency(const uint32_t baudrate)
97
97
}
98
98
i ++ ;
99
99
} while (boundary != 0 );
100
- // Will get here only if baudrate == 0 .
100
+ // Should not get here.
101
101
return 0 ;
102
102
}
103
103
@@ -168,22 +168,26 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
168
168
}
169
169
170
170
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 ) {
173
173
return false;
174
+ }
174
175
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 )));
176
180
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
+ }
183
187
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 );
185
189
186
- return true;
190
+ return true;
187
191
}
188
192
189
193
bool common_hal_busio_spi_try_lock (busio_spi_obj_t * self ) {
0 commit comments