@@ -156,7 +156,13 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
156
156
handle -> Instance = SPI_INST (obj );
157
157
handle -> Init .Mode = SPI_MODE_MASTER ;
158
158
handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256 ;
159
- handle -> Init .Direction = SPI_DIRECTION_2LINES ;
159
+
160
+ if (miso != NC ) {
161
+ handle -> Init .Direction = SPI_DIRECTION_2LINES ;
162
+ } else {
163
+ handle -> Init .Direction = SPI_DIRECTION_1LINE ;
164
+ }
165
+
160
166
handle -> Init .CLKPhase = SPI_PHASE_1EDGE ;
161
167
handle -> Init .CLKPolarity = SPI_POLARITY_LOW ;
162
168
handle -> Init .CRCCalculation = SPI_CRCCALCULATION_DISABLE ;
@@ -353,6 +359,10 @@ int spi_master_write(spi_t *obj, int value)
353
359
struct spi_s * spiobj = SPI_S (obj );
354
360
SPI_HandleTypeDef * handle = & (spiobj -> handle );
355
361
362
+ if (handle -> Init .Direction == SPI_DIRECTION_1LINE ) {
363
+ return HAL_SPI_Transmit (handle , (uint8_t * )& value , 1 , 10 );
364
+ }
365
+
356
366
#if defined(LL_SPI_RX_FIFO_TH_HALF )
357
367
/* Configure the default data size */
358
368
if (handle -> Init .DataSize == SPI_DATASIZE_16BIT ) {
@@ -390,13 +400,31 @@ int spi_master_write(spi_t *obj, int value)
390
400
int spi_master_block_write (spi_t * obj , const char * tx_buffer , int tx_length ,
391
401
char * rx_buffer , int rx_length , char write_fill )
392
402
{
403
+ struct spi_s * spiobj = SPI_S (obj );
404
+ SPI_HandleTypeDef * handle = & (spiobj -> handle );
393
405
int total = (tx_length > rx_length ) ? tx_length : rx_length ;
394
-
395
- for (int i = 0 ; i < total ; i ++ ) {
396
- char out = (i < tx_length ) ? tx_buffer [i ] : write_fill ;
397
- char in = spi_master_write (obj , out );
398
- if (i < rx_length ) {
399
- rx_buffer [i ] = in ;
406
+ int i = 0 ;
407
+ if (handle -> Init .Direction == SPI_DIRECTION_2LINES ) {
408
+ for (i = 0 ; i < total ; i ++ ) {
409
+ char out = (i < tx_length ) ? tx_buffer [i ] : write_fill ;
410
+ char in = spi_master_write (obj , out );
411
+ if (i < rx_length ) {
412
+ rx_buffer [i ] = in ;
413
+ }
414
+ }
415
+ } else {
416
+ /* In case of 1 WIRE only, first handle TX, then Rx */
417
+ if (tx_length != 0 ) {
418
+ if (HAL_OK != HAL_SPI_Transmit (handle , (uint8_t * )tx_buffer , tx_length , tx_length * 10 )) {
419
+ /* report an error */
420
+ total = 0 ;
421
+ }
422
+ }
423
+ if (rx_length != 0 ) {
424
+ if (HAL_OK != HAL_SPI_Receive (handle , (uint8_t * )rx_buffer , rx_length , rx_length * 10 )) {
425
+ /* report an error */
426
+ total = 0 ;
427
+ }
400
428
}
401
429
}
402
430
0 commit comments