@@ -367,6 +367,9 @@ void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx,
367
367
368
368
SPI_ENABLE_SYNC (spi_base );
369
369
370
+ // Initialize total SPI transfer frames
371
+ obj -> spi .txrx_rmn = NU_MAX (tx_length , rx_length );
372
+
370
373
if (obj -> spi .dma_usage == DMA_USAGE_NEVER ) {
371
374
// Interrupt way
372
375
spi_master_write_asynch (obj , spi_fifo_depth (obj ) / 2 );
@@ -637,16 +640,12 @@ static uint32_t spi_event_check(spi_t *obj)
637
640
static uint32_t spi_master_write_asynch (spi_t * obj , uint32_t tx_limit )
638
641
{
639
642
uint32_t n_words = 0 ;
640
- uint32_t tx_rmn = obj -> tx_buff .length - obj -> tx_buff .pos ;
641
- uint32_t rx_rmn = obj -> rx_buff .length - obj -> rx_buff .pos ;
642
- uint32_t max_tx = NU_MAX (tx_rmn , rx_rmn );
643
- max_tx = NU_MIN (max_tx , tx_limit );
644
643
uint8_t data_width = spi_get_data_width (obj );
645
644
uint8_t bytes_per_word = (data_width + 7 ) / 8 ;
646
645
uint8_t * tx = (uint8_t * )(obj -> tx_buff .buffer ) + bytes_per_word * obj -> tx_buff .pos ;
647
646
SPI_T * spi_base = (SPI_T * ) NU_MODBASE (obj -> spi .spi );
648
647
649
- while (( n_words < max_tx ) && spi_writeable (obj )) {
648
+ while (obj -> spi . txrx_rmn && spi_writeable (obj )) {
650
649
if (spi_is_tx_complete (obj )) {
651
650
// Transmit dummy as transmit buffer is empty
652
651
SPI_WRITE_TX (spi_base , 0 );
@@ -669,6 +668,7 @@ static uint32_t spi_master_write_asynch(spi_t *obj, uint32_t tx_limit)
669
668
obj -> tx_buff .pos ++ ;
670
669
}
671
670
n_words ++ ;
671
+ obj -> spi .txrx_rmn -- ;
672
672
}
673
673
674
674
//Return the number of words that have been sent
@@ -689,15 +689,12 @@ static uint32_t spi_master_write_asynch(spi_t *obj, uint32_t tx_limit)
689
689
static uint32_t spi_master_read_asynch (spi_t * obj )
690
690
{
691
691
uint32_t n_words = 0 ;
692
- uint32_t tx_rmn = obj -> tx_buff .length - obj -> tx_buff .pos ;
693
- uint32_t rx_rmn = obj -> rx_buff .length - obj -> rx_buff .pos ;
694
- uint32_t max_rx = NU_MAX (tx_rmn , rx_rmn );
695
692
uint8_t data_width = spi_get_data_width (obj );
696
693
uint8_t bytes_per_word = (data_width + 7 ) / 8 ;
697
694
uint8_t * rx = (uint8_t * )(obj -> rx_buff .buffer ) + bytes_per_word * obj -> rx_buff .pos ;
698
695
SPI_T * spi_base = (SPI_T * ) NU_MODBASE (obj -> spi .spi );
699
696
700
- while (( n_words < max_rx ) && spi_readable (obj )) {
697
+ while (spi_readable (obj )) {
701
698
if (spi_is_rx_complete (obj )) {
702
699
// Disregard as receive buffer is full
703
700
SPI_READ_RX (spi_base );
0 commit comments