@@ -52,10 +52,10 @@ STATIC void spi_clock_disable(uint8_t mask);
52
52
STATIC uint32_t get_busclock (SPI_TypeDef * instance ) {
53
53
//SPI2 and 3 are on PCLK1, if they exist.
54
54
#ifdef SPI2
55
- if (instance == SPI2 ) return HAL_RCC_GetPCLK1Freq ();
55
+ if (instance == SPI2 ) return HAL_RCC_GetPCLK1Freq ();
56
56
#endif
57
57
#ifdef SPI3
58
- if (instance == SPI3 ) return HAL_RCC_GetPCLK1Freq ();
58
+ if (instance == SPI3 ) return HAL_RCC_GetPCLK1Freq ();
59
59
#endif
60
60
return HAL_RCC_GetPCLK2Freq ();
61
61
}
@@ -85,20 +85,20 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
85
85
bool spi_taken = false;
86
86
87
87
//SCK is not optional. MOSI and MISO are
88
- for (uint i = 0 ; i < sck_len ;i ++ ) {
88
+ for (uint i = 0 ; i < sck_len ;i ++ ) {
89
89
if (mcu_spi_sck_list [i ].pin == sck ) {
90
90
//if both MOSI and MISO exist, loop search normally
91
91
if ((mosi != mp_const_none ) && (miso != mp_const_none )) {
92
92
//MOSI
93
- for (uint j = 0 ; j < mosi_len ;j ++ ) {
93
+ for (uint j = 0 ; j < mosi_len ;j ++ ) {
94
94
if (mcu_spi_mosi_list [j ].pin == mosi ) {
95
95
//MISO
96
- for (uint k = 0 ; k < miso_len ;k ++ ) {
96
+ for (uint k = 0 ; k < miso_len ;k ++ ) {
97
97
if ((mcu_spi_miso_list [k ].pin == miso ) //everything needs the same index
98
98
&& (mcu_spi_sck_list [i ].spi_index == mcu_spi_mosi_list [j ].spi_index )
99
99
&& (mcu_spi_sck_list [i ].spi_index == mcu_spi_miso_list [k ].spi_index )) {
100
100
//keep looking if the SPI is taken, edge case
101
- if (reserved_spi [mcu_spi_sck_list [i ].spi_index - 1 ]) {
101
+ if (reserved_spi [mcu_spi_sck_list [i ].spi_index - 1 ]) {
102
102
spi_taken = true;
103
103
continue ;
104
104
}
@@ -113,11 +113,11 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
113
113
}
114
114
// if just MISO, reduce search
115
115
} else if (miso != mp_const_none ) {
116
- for (uint j = 0 ; j < miso_len ;j ++ ) {
116
+ for (uint j = 0 ; j < miso_len ;j ++ ) {
117
117
if ((mcu_spi_miso_list [j ].pin == miso ) //only SCK and MISO need the same index
118
118
&& (mcu_spi_sck_list [i ].spi_index == mcu_spi_miso_list [j ].spi_index )) {
119
119
//keep looking if the SPI is taken, edge case
120
- if (reserved_spi [mcu_spi_sck_list [i ].spi_index - 1 ]) {
120
+ if (reserved_spi [mcu_spi_sck_list [i ].spi_index - 1 ]) {
121
121
spi_taken = true;
122
122
continue ;
123
123
}
@@ -130,11 +130,11 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
130
130
}
131
131
// if just MOSI, reduce search
132
132
} else if (mosi != mp_const_none ) {
133
- for (uint j = 0 ; j < mosi_len ;j ++ ) {
133
+ for (uint j = 0 ; j < mosi_len ;j ++ ) {
134
134
if ((mcu_spi_mosi_list [j ].pin == mosi ) //only SCK and MOSI need the same index
135
135
&& (mcu_spi_sck_list [i ].spi_index == mcu_spi_mosi_list [j ].spi_index )) {
136
136
//keep looking if the SPI is taken, edge case
137
- if (reserved_spi [mcu_spi_sck_list [i ].spi_index - 1 ]) {
137
+ if (reserved_spi [mcu_spi_sck_list [i ].spi_index - 1 ]) {
138
138
spi_taken = true;
139
139
continue ;
140
140
}
@@ -153,7 +153,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
153
153
}
154
154
155
155
//handle typedef selection, errors
156
- if ( (self -> sck != NULL && self -> mosi != NULL && self -> miso != NULL ) ||
156
+ if ( (self -> sck != NULL && self -> mosi != NULL && self -> miso != NULL ) ||
157
157
(self -> sck != NULL && self -> mosi != NULL && miso == mp_const_none ) ||
158
158
(self -> sck != NULL && self -> miso != NULL && mosi == mp_const_none )) {
159
159
SPIx = mcu_spi_banks [self -> sck -> spi_index - 1 ];
@@ -219,17 +219,25 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
219
219
self -> bits = 8 ;
220
220
221
221
claim_pin (sck );
222
- if (self -> mosi != NULL ) claim_pin (mosi );
223
- if (self -> miso != NULL ) claim_pin (miso );
222
+ if (self -> mosi != NULL ) {
223
+ claim_pin (mosi );
224
+ }
225
+ if (self -> miso != NULL ) {
226
+ claim_pin (miso );
227
+ }
224
228
}
225
229
226
230
void common_hal_busio_spi_never_reset (busio_spi_obj_t * self ) {
227
- for (size_t i = 0 ; i < MP_ARRAY_SIZE (mcu_spi_banks ); i ++ ) {
231
+ for (size_t i = 0 ; i < MP_ARRAY_SIZE (mcu_spi_banks ); i ++ ) {
228
232
if (mcu_spi_banks [i ] == self -> handle .Instance ) {
229
233
never_reset_spi [i ] = true;
230
234
never_reset_pin_number (self -> sck -> pin -> port , self -> sck -> pin -> number );
231
- if (self -> mosi != NULL ) never_reset_pin_number (self -> mosi -> pin -> port , self -> mosi -> pin -> number );
232
- if (self -> miso != NULL ) never_reset_pin_number (self -> miso -> pin -> port , self -> miso -> pin -> number );
235
+ if (self -> mosi != NULL ) {
236
+ never_reset_pin_number (self -> mosi -> pin -> port , self -> mosi -> pin -> number );
237
+ }
238
+ if (self -> miso != NULL ) {
239
+ never_reset_pin_number (self -> miso -> pin -> port , self -> miso -> pin -> number );
240
+ }
233
241
break ;
234
242
}
235
243
}
@@ -245,8 +253,12 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
245
253
never_reset_spi [self -> sck -> spi_index - 1 ] = false;
246
254
247
255
reset_pin_number (self -> sck -> pin -> port ,self -> sck -> pin -> number );
248
- if (self -> mosi != NULL ) reset_pin_number (self -> mosi -> pin -> port ,self -> mosi -> pin -> number );
249
- if (self -> miso != NULL ) reset_pin_number (self -> miso -> pin -> port ,self -> miso -> pin -> number );
256
+ if (self -> mosi != NULL ) {
257
+ reset_pin_number (self -> mosi -> pin -> port ,self -> mosi -> pin -> number );
258
+ }
259
+ if (self -> miso != NULL ) {
260
+ reset_pin_number (self -> miso -> pin -> port ,self -> miso -> pin -> number );
261
+ }
250
262
self -> sck = mp_const_none ;
251
263
self -> mosi = mp_const_none ;
252
264
self -> miso = mp_const_none ;
@@ -282,7 +294,9 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
282
294
uint32_t baudrate , uint8_t polarity , uint8_t phase , uint8_t bits ) {
283
295
//This resets the SPI, so check before updating it redundantly
284
296
if (baudrate == self -> baudrate && polarity == self -> polarity
285
- && phase == self -> phase && bits == self -> bits ) return true;
297
+ && phase == self -> phase && bits == self -> bits ) {
298
+ return true;
299
+ }
286
300
287
301
//Deinit SPI
288
302
HAL_SPI_DeInit (& self -> handle );
@@ -335,22 +349,27 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
335
349
336
350
bool common_hal_busio_spi_write (busio_spi_obj_t * self ,
337
351
const uint8_t * data , size_t len ) {
338
- if (self -> mosi == NULL ) mp_raise_ValueError (translate ("No MOSI Pin" ));
352
+ if (self -> mosi == NULL ) {
353
+ mp_raise_ValueError (translate ("No MOSI Pin" ));
354
+ }
339
355
HAL_StatusTypeDef result = HAL_SPI_Transmit (& self -> handle , (uint8_t * )data , (uint16_t )len , HAL_MAX_DELAY );
340
356
return result == HAL_OK ;
341
357
}
342
358
343
359
bool common_hal_busio_spi_read (busio_spi_obj_t * self ,
344
360
uint8_t * data , size_t len , uint8_t write_value ) {
345
- if (self -> miso == NULL ) mp_raise_ValueError (translate ("No MISO Pin" ));
361
+ if (self -> miso == NULL ) {
362
+ mp_raise_ValueError (translate ("No MISO Pin" ));
363
+ }
346
364
HAL_StatusTypeDef result = HAL_SPI_Receive (& self -> handle , data , (uint16_t )len , HAL_MAX_DELAY );
347
365
return result == HAL_OK ;
348
366
}
349
367
350
368
bool common_hal_busio_spi_transfer (busio_spi_obj_t * self ,
351
369
uint8_t * data_out , uint8_t * data_in , size_t len ) {
352
- if (self -> miso == NULL ) mp_raise_ValueError (translate ("No MISO Pin" ));
353
- if (self -> mosi == NULL ) mp_raise_ValueError (translate ("No MOSI Pin" ));
370
+ if (self -> miso == NULL || self -> mosi == NULL ) {
371
+ mp_raise_ValueError (translate ("Missing MISO or MOSI Pin" ));
372
+ }
354
373
HAL_StatusTypeDef result = HAL_SPI_TransmitReceive (& self -> handle ,
355
374
data_out , data_in , (uint16_t )len ,HAL_MAX_DELAY );
356
375
return result == HAL_OK ;
0 commit comments