@@ -388,6 +388,9 @@ void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx,
388
388
389
389
SPI_ENABLE_SYNC (spi_base );
390
390
391
+ // Initialize total SPI transfer frames
392
+ obj -> spi .txrx_rmn = NU_MAX (tx_length , rx_length );
393
+
391
394
if (obj -> spi .dma_usage == DMA_USAGE_NEVER ) {
392
395
// Interrupt way
393
396
spi_master_write_asynch (obj , spi_fifo_depth (obj ) / 2 );
@@ -658,16 +661,12 @@ static uint32_t spi_event_check(spi_t *obj)
658
661
static uint32_t spi_master_write_asynch (spi_t * obj , uint32_t tx_limit )
659
662
{
660
663
uint32_t n_words = 0 ;
661
- uint32_t tx_rmn = obj -> tx_buff .length - obj -> tx_buff .pos ;
662
- uint32_t rx_rmn = obj -> rx_buff .length - obj -> rx_buff .pos ;
663
- uint32_t max_tx = NU_MAX (tx_rmn , rx_rmn );
664
- max_tx = NU_MIN (max_tx , tx_limit );
665
664
uint8_t data_width = spi_get_data_width (obj );
666
665
uint8_t bytes_per_word = (data_width + 7 ) / 8 ;
667
666
uint8_t * tx = (uint8_t * )(obj -> tx_buff .buffer ) + bytes_per_word * obj -> tx_buff .pos ;
668
667
SPI_T * spi_base = (SPI_T * ) NU_MODBASE (obj -> spi .spi );
669
668
670
- while (( n_words < max_tx ) && spi_writeable (obj )) {
669
+ while (obj -> spi . txrx_rmn && spi_writeable (obj )) {
671
670
if (spi_is_tx_complete (obj )) {
672
671
// Transmit dummy as transmit buffer is empty
673
672
SPI_WRITE_TX (spi_base , 0 );
@@ -690,6 +689,7 @@ static uint32_t spi_master_write_asynch(spi_t *obj, uint32_t tx_limit)
690
689
obj -> tx_buff .pos ++ ;
691
690
}
692
691
n_words ++ ;
692
+ obj -> spi .txrx_rmn -- ;
693
693
}
694
694
695
695
//Return the number of words that have been sent
@@ -710,15 +710,12 @@ static uint32_t spi_master_write_asynch(spi_t *obj, uint32_t tx_limit)
710
710
static uint32_t spi_master_read_asynch (spi_t * obj )
711
711
{
712
712
uint32_t n_words = 0 ;
713
- uint32_t tx_rmn = obj -> tx_buff .length - obj -> tx_buff .pos ;
714
- uint32_t rx_rmn = obj -> rx_buff .length - obj -> rx_buff .pos ;
715
- uint32_t max_rx = NU_MAX (tx_rmn , rx_rmn );
716
713
uint8_t data_width = spi_get_data_width (obj );
717
714
uint8_t bytes_per_word = (data_width + 7 ) / 8 ;
718
715
uint8_t * rx = (uint8_t * )(obj -> rx_buff .buffer ) + bytes_per_word * obj -> rx_buff .pos ;
719
716
SPI_T * spi_base = (SPI_T * ) NU_MODBASE (obj -> spi .spi );
720
717
721
- while (( n_words < max_rx ) && spi_readable (obj )) {
718
+ while (spi_readable (obj )) {
722
719
if (spi_is_rx_complete (obj )) {
723
720
// Disregard as receive buffer is full
724
721
SPI_READ_RX (spi_base );
0 commit comments