@@ -171,7 +171,7 @@ STATIC int check_pins(busio_spi_obj_t *self,
171
171
172
172
void common_hal_busio_spi_construct (busio_spi_obj_t * self ,
173
173
const mcu_pin_obj_t * sck , const mcu_pin_obj_t * mosi ,
174
- const mcu_pin_obj_t * miso ) {
174
+ const mcu_pin_obj_t * miso , bool half_duplex ) {
175
175
176
176
int periph_index = check_pins (self , sck , mosi , miso );
177
177
SPI_TypeDef * SPIx = mcu_spi_banks [periph_index - 1 ];
@@ -208,12 +208,11 @@ 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
- 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 ) {
211
+ // Direction change only required for RX-only, see RefMan RM0090:884
212
+ if (half_duplex == true) {
214
213
self -> handle .Init .Direction = SPI_DIRECTION_1LINE ;
215
- } else if ( self -> mosi != NULL && self -> miso != NULL ) {
216
- self -> handle .Init .Direction = SPI_DIRECTION_2LINES ;
214
+ } else {
215
+ self -> handle .Init .Direction = ( self -> mosi == NULL ) ? SPI_DIRECTION_2LINES_RXONLY : SPI_DIRECTION_2LINES ;
217
216
}
218
217
self -> handle .Init .DataSize = SPI_DATASIZE_8BIT ;
219
218
self -> handle .Init .CLKPolarity = SPI_POLARITY_LOW ;
@@ -229,6 +228,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
229
228
}
230
229
self -> baudrate = (get_busclock (SPIx ) / 16 );
231
230
self -> prescaler = 16 ;
231
+ self -> half_duplex = half_duplex ;
232
232
self -> polarity = 0 ;
233
233
self -> phase = 0 ;
234
234
self -> bits = 8 ;
@@ -345,11 +345,15 @@ bool common_hal_busio_spi_write(busio_spi_obj_t *self,
345
345
346
346
bool common_hal_busio_spi_read (busio_spi_obj_t * self ,
347
347
uint8_t * data , size_t len , uint8_t write_value ) {
348
+ if (self -> miso == NULL && self -> half_duplex == false) {
349
+ mp_raise_ValueError (translate ("No MISO Pin" ));
350
+ } else if (self -> half_duplex == true && self -> mosi == NULL ) {
351
+ mp_raise_ValueError (translate ("No MOSI Pin" ));
352
+ }
348
353
HAL_StatusTypeDef result = HAL_OK ;
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 )) {
354
+ if ((self -> half_duplex == false && self -> mosi == NULL ) || (self -> half_duplex == true && self -> mosi != NULL && self -> miso == NULL )) {
351
355
result = HAL_SPI_Receive (& self -> handle , data , (uint16_t )len , HAL_MAX_DELAY );
352
- } else { // Else SPI_DIRECTION_2LINES
356
+ } else {
353
357
memset (data , write_value , len );
354
358
result = HAL_SPI_TransmitReceive (& self -> handle , data , data , (uint16_t )len , HAL_MAX_DELAY );
355
359
}
0 commit comments