@@ -208,8 +208,13 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
208
208
209
209
self -> handle .Instance = SPIx ;
210
210
self -> handle .Init .Mode = SPI_MODE_MASTER ;
211
- // Direction change only required for RX-only, see RefMan RM0090:884
212
- self -> handle .Init .Direction = (self -> mosi == NULL ) ? SPI_DIRECTION_2LINES_RXONLY : SPI_DIRECTION_2LINES ;
211
+ if (self -> mosi == NULL && self -> miso != NULL ) {
212
+ self -> handle .Init .Direction = SPI_DIRECTION_2LINES_RXONLY ;
213
+ } else if (self -> mosi != NULL && self -> miso == NULL ) {
214
+ self -> handle .Init .Direction = SPI_DIRECTION_1LINE ;
215
+ } else if (self -> mosi != NULL && self -> miso != NULL ) {
216
+ self -> handle .Init .Direction = SPI_DIRECTION_2LINES ;
217
+ }
213
218
self -> handle .Init .DataSize = SPI_DATASIZE_8BIT ;
214
219
self -> handle .Init .CLKPolarity = SPI_POLARITY_LOW ;
215
220
self -> handle .Init .CLKPhase = SPI_PHASE_1EDGE ;
@@ -340,13 +345,11 @@ bool common_hal_busio_spi_write(busio_spi_obj_t *self,
340
345
341
346
bool common_hal_busio_spi_read (busio_spi_obj_t * self ,
342
347
uint8_t * data , size_t len , uint8_t write_value ) {
343
- if (self -> miso == NULL ) {
344
- mp_raise_ValueError (translate ("No MISO Pin" ));
345
- }
346
348
HAL_StatusTypeDef result = HAL_OK ;
347
- if (self -> mosi == NULL ) {
349
+ // If in modes SPI_DIRECTION_2LINES_RXONLY or SPI_DIRECTION_1LINE
350
+ if ((self -> mosi == NULL && self -> miso != NULL ) || (self -> mosi != NULL && self -> miso == NULL )) {
348
351
result = HAL_SPI_Receive (& self -> handle , data , (uint16_t )len , HAL_MAX_DELAY );
349
- } else {
352
+ } else { // Else SPI_DIRECTION_2LINES
350
353
memset (data , write_value , len );
351
354
result = HAL_SPI_TransmitReceive (& self -> handle , data , data , (uint16_t )len , HAL_MAX_DELAY );
352
355
}
0 commit comments