LPSPI read extra byte

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPSPI read extra byte

Jump to solution
955 Views
rmaier
Contributor III

Hello,

 

I am struggling with the LPSPI module on the S32K144. I want to send some bytes and receive some bytes of varying length. This works with the exception that I have to send an additional dummy byte. Let's say I want to Tx 2 bytes and Rx 2 bytes, I have to Tx a total of 5 bytes. What am I missing to only have to send 4 bytes to write/read 4 bytes?

Here's my function:

void Spi_Transceive(uint8_t txLen, uint8_t *txBuf, uint8_t rxLen, uint8_t *rxBuf, uint8_t module, uint8_t pcs)
{

    uint16_t total = rxLen + txLen - 1;
    uint32_t totalBytes = 0;
    const uint32_t tcr_base =
        LPSPI_TCR_FRAMESZ(7) | // 8-bit frames
        LPSPI_TCR_PCS(pcs) |
        LPSPI_TCR_CPOL(0) | 
        LPSPI_TCR_CPHA(1) |
        LPSPI_TCR_PRESCALE(0) |
        LPSPI_TCR_WIDTH(0) |
        LPSPI_TCR_CONTC(1);

    LPSPI2->CR |= LPSPI_CR_RTF_MASK | LPSPI_CR_RRF_MASK;
    LPSPI2->SR = 0xFFFFFFFFu;
    LPSPI2->FCR = LPSPI_FCR_TXWATER(0) | LPSPI_FCR_RXWATER(0);

    while (!(LPSPI2->SR & LPSPI_SR_TDF_MASK))
        ;
    LPSPI2->TCR = tcr_base | LPSPI_TCR_CONT(1);
    LPSPI2->TDR = (uint8_t)txBuf[0];
    totalBytes++;

    for (uint8_t i = 1; i < txLen; i++)
    {
        if (totalBytes == total)
        {
            LPSPI2->TCR = tcr_base | LPSPI_TCR_CONT(0);
        }

        while (!(LPSPI2->SR & LPSPI_SR_TDF_MASK))
            ;

        LPSPI2->TDR = (uint8_t)txBuf[i];
        totalBytes++;

        while (!(LPSPI2->SR & LPSPI_SR_RDF_MASK))
            ;
        (void)LPSPI2->RDR;
    }

    if (rxLen > 0)
    {
        while (!(LPSPI2->SR & LPSPI_SR_TDF_MASK))
            ;
        LPSPI2->TDR = 0x00; // NOP
        while (!(LPSPI2->SR & LPSPI_SR_RDF_MASK))
            ;
        (void)LPSPI2->RDR;
    }
    else
    {
        return;
    }

    for (uint8_t i = 0; i < rxLen; i++)
    {
        if (totalBytes == total)
        {
            LPSPI2->TCR = tcr_base | LPSPI_TCR_CONT(0);
        }
        while (!(LPSPI2->SR & LPSPI_SR_TDF_MASK))
            ;

        LPSPI2->TDR = 0x00; // NOP
        totalBytes++;

        while (!(LPSPI2->SR & LPSPI_SR_RDF_MASK))
            ;
        rxBuf[i] = (uint8_t)LPSPI2->RDR;
    }
}

 

Here's the initialization:

    PCC->PCCn[PCC_LPSPI2_INDEX] = 0;                  /* Disable clocks to modify PCS ( default) 	*/
    PCC->PCCn[PCC_LPSPI2_INDEX] = PCC_PCCn_PR_MASK    /* (default) Peripheral is present.			*/
                                  | PCC_PCCn_CGC_MASK /* Enable PCS=SPLL_DIV2 (40 MHz func'l clock) 	*/
                                  | PCC_PCCn_PCS(6);

    LPSPI2->CR = 0x00000000;    
    LPSPI2->IER = 0x00000000;   
    LPSPI2->DER = 0x00000000;  
    LPSPI2->CFGR0 = 0x00000000; 
    LPSPI2->CFGR1 = LPSPI_CFGR1_MASTER_MASK | LPSPI_CFGR1_SAMPLE(1) | LPSPI_CFGR1_AUTOPCS(0) | LPSPI_CFGR1_PCSPOL(0b0000); 

    LPSPI2->TCR = LPSPI_TCR_CPHA_MASK | LPSPI_TCR_PRESCALE(2) | LPSPI_TCR_PCS(0) | LPSPI_TCR_FRAMESZ(7) | LPSPI_TCR_CONT(1);

    LPSPI2->CCR = LPSPI_CCR_SCKPCS(4) | LPSPI_CCR_PCSSCK(4) | LPSPI_CCR_DBT(8) | LPSPI_CCR_SCKDIV(8); 
    LPSPI2->FCR = LPSPI_FCR_TXWATER(3); 
    LPSPI2->CR = LPSPI_CR_MEN_MASK | LPSPI_CR_DBGEN_MASK; 

  

Tags (2)
0 Kudos
Reply
1 Solution
835 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi,

clear CONTC bit in TCR.
See attached main.c file I used in S32K144_Project_LPSPI demo.
It gives right frames. I tested below sequences

PetrS_0-1755679051074.pngPetrS_1-1755679060431.png

BR, Petr

 

 

 

View solution in original post

4 Replies
904 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi,

for each write to TDR you should increment your totalBytes. For each TDR write there should be RDR reading. After CONT is cleared you should read RDR finally. Try below one, but I did not tested that.

 

void Spi_Transceive(uint8_t txLen, uint8_t *txBuf, uint8_t rxLen, uint8_t *rxBuf, uint8_t module, uint8_t pcs)
{

    uint16_t total = rxLen + txLen;
    uint32_t totalBytes = 0;
    const uint32_t tcr_base =
        LPSPI_TCR_FRAMESZ(7) | // 8-bit frames
        LPSPI_TCR_PCS(pcs) |
        LPSPI_TCR_CPOL(0) | 
        LPSPI_TCR_CPHA(1) |
        LPSPI_TCR_PRESCALE(0) |
        LPSPI_TCR_WIDTH(0) |
        LPSPI_TCR_CONTC(1);

    LPSPI2->CR |= LPSPI_CR_RTF_MASK | LPSPI_CR_RRF_MASK;
    LPSPI2->SR = 0xFFFFFFFFu;
    LPSPI2->FCR = LPSPI_FCR_TXWATER(0) | LPSPI_FCR_RXWATER(0);

    if (txLen>0)
	{
    	LPSPI2->TCR = tcr_base | LPSPI_TCR_CONT(1);
		while (!(LPSPI2->SR & LPSPI_SR_TDF_MASK));
		LPSPI2->TDR = *txBuf++;
		totalBytes++;
		while(totalBytes < txLen)
		{
			while (!(LPSPI2->SR & LPSPI_SR_TDF_MASK));
			LPSPI2->TDR = *txBuf++;
			totalBytes++;
			while (!(LPSPI2->SR & LPSPI_SR_RDF_MASK));
			(void)LPSPI2->RDR;
		}
		if(rxLen==0) 
		{
			LPSPI2->TCR = tcr_base | LPSPI_TCR_CONT(0);
			while (!(LPSPI2->SR & LPSPI_SR_RDF_MASK));
			(void)LPSPI2->RDR;
		}
		
	}
    
    if (rxLen>0)
    {
    	if(txLen==0) LPSPI2->TCR = tcr_base | LPSPI_TCR_CONT(1);
		while (!(LPSPI2->SR & LPSPI_SR_TDF_MASK));
		LPSPI2->TDR = 0x00; // NOP
		totalBytes++;
		if(txLen>0)
		{
			while (!(LPSPI2->SR & LPSPI_SR_RDF_MASK));
			(void)LPSPI2->RDR;
    	}
     	while(totalBytes < total)
		{
    		while (!(LPSPI2->SR & LPSPI_SR_TDF_MASK));
    		LPSPI2->TDR = 0x00; // NOP
			totalBytes++;
			while (!(LPSPI2->SR & LPSPI_SR_RDF_MASK));
			*rxBuf++ = LPSPI2->RDR;
		}
    	LPSPI2->TCR = tcr_base | LPSPI_TCR_CONT(0);
		while (!(LPSPI2->SR & LPSPI_SR_RDF_MASK));
		*rxBuf++ = LPSPI2->RDR;
    }
	
}

 BR, Petr

0 Kudos
Reply
856 Views
rmaier
Contributor III

Hello Petr,

 

Thanks for your support. The first read attempt always stays forever in the while RDF mask loop. Even with your code. Any idea why?

if (rxLen == 0)
        {
            LPSPI2->TCR = tcr_base | LPSPI_TCR_CONT(0);
            while (!(LPSPI2->SR & LPSPI_SR_RDF_MASK)) // Stuck here on 1st read
                ;
            (void)LPSPI2->RDR;
        }

 

0 Kudos
Reply
836 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi,

clear CONTC bit in TCR.
See attached main.c file I used in S32K144_Project_LPSPI demo.
It gives right frames. I tested below sequences

PetrS_0-1755679051074.pngPetrS_1-1755679060431.png

BR, Petr

 

 

 

809 Views
rmaier
Contributor III

Thanks for your help, Petr.

Tags (2)
0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-2152890%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ELPSPI%20read%20extra%20byte%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2152890%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EI%20am%20struggling%20with%20the%20LPSPI%20module%20on%20the%20S32K144.%20I%20want%20to%20send%20some%20bytes%20and%20receive%20some%20bytes%20of%20varying%20length.%20This%20works%20with%20the%20exception%20that%20I%20have%20to%20send%20an%20additional%20dummy%20byte.%20Let's%20say%20I%20want%20to%20Tx%202%20bytes%20and%20Rx%202%20bytes%2C%20I%20have%20to%20Tx%20a%20total%20of%205%20bytes.%26nbsp%3BWhat%20am%20I%20missing%20to%20only%20have%20to%20send%204%20bytes%20to%20write%2Fread%204%20bytes%3F%3CBR%20%2F%3E%3CBR%20%2F%3EHere's%20my%20function%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%3Evoid%20Spi_Transceive(uint8_t%20txLen%2C%20uint8_t%20*txBuf%2C%20uint8_t%20rxLen%2C%20uint8_t%20*rxBuf%2C%20uint8_t%20module%2C%20uint8_t%20pcs)%0A%7B%0A%0A%20%20%20%20uint16_t%20total%20%3D%20rxLen%20%2B%20txLen%20-%201%3B%0A%20%20%20%20uint32_t%20totalBytes%20%3D%200%3B%0A%20%20%20%20const%20uint32_t%20tcr_base%20%3D%0A%20%20%20%20%20%20%20%20LPSPI_TCR_FRAMESZ(7)%20%7C%20%2F%2F%208-bit%20frames%0A%20%20%20%20%20%20%20%20LPSPI_TCR_PCS(pcs)%20%7C%0A%20%20%20%20%20%20%20%20LPSPI_TCR_CPOL(0)%20%7C%20%0A%20%20%20%20%20%20%20%20LPSPI_TCR_CPHA(1)%20%7C%0A%20%20%20%20%20%20%20%20LPSPI_TCR_PRESCALE(0)%20%7C%0A%20%20%20%20%20%20%20%20LPSPI_TCR_WIDTH(0)%20%7C%0A%20%20%20%20%20%20%20%20LPSPI_TCR_CONTC(1)%3B%0A%0A%20%20%20%20LPSPI2-%26gt%3BCR%20%7C%3D%20LPSPI_CR_RTF_MASK%20%7C%20LPSPI_CR_RRF_MASK%3B%0A%20%20%20%20LPSPI2-%26gt%3BSR%20%3D%200xFFFFFFFFu%3B%0A%20%20%20%20LPSPI2-%26gt%3BFCR%20%3D%20LPSPI_FCR_TXWATER(0)%20%7C%20LPSPI_FCR_RXWATER(0)%3B%0A%0A%20%20%20%20while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_TDF_MASK))%0A%20%20%20%20%20%20%20%20%3B%0A%20%20%20%20LPSPI2-%26gt%3BTCR%20%3D%20tcr_base%20%7C%20LPSPI_TCR_CONT(1)%3B%0A%20%20%20%20LPSPI2-%26gt%3BTDR%20%3D%20(uint8_t)txBuf%5B0%5D%3B%0A%20%20%20%20totalBytes%2B%2B%3B%0A%0A%20%20%20%20for%20(uint8_t%20i%20%3D%201%3B%20i%20%26lt%3B%20txLen%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(totalBytes%20%3D%3D%20total)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20LPSPI2-%26gt%3BTCR%20%3D%20tcr_base%20%7C%20LPSPI_TCR_CONT(0)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_TDF_MASK))%0A%20%20%20%20%20%20%20%20%20%20%20%20%3B%0A%0A%20%20%20%20%20%20%20%20LPSPI2-%26gt%3BTDR%20%3D%20(uint8_t)txBuf%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20totalBytes%2B%2B%3B%0A%0A%20%20%20%20%20%20%20%20while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_RDF_MASK))%0A%20%20%20%20%20%20%20%20%20%20%20%20%3B%0A%20%20%20%20%20%20%20%20(void)LPSPI2-%26gt%3BRDR%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20if%20(rxLen%20%26gt%3B%200)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_TDF_MASK))%0A%20%20%20%20%20%20%20%20%20%20%20%20%3B%0A%20%20%20%20%20%20%20%20LPSPI2-%26gt%3BTDR%20%3D%200x00%3B%20%2F%2F%20NOP%0A%20%20%20%20%20%20%20%20while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_RDF_MASK))%0A%20%20%20%20%20%20%20%20%20%20%20%20%3B%0A%20%20%20%20%20%20%20%20(void)LPSPI2-%26gt%3BRDR%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20for%20(uint8_t%20i%20%3D%200%3B%20i%20%26lt%3B%20rxLen%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(totalBytes%20%3D%3D%20total)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20LPSPI2-%26gt%3BTCR%20%3D%20tcr_base%20%7C%20LPSPI_TCR_CONT(0)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_TDF_MASK))%0A%20%20%20%20%20%20%20%20%20%20%20%20%3B%0A%0A%20%20%20%20%20%20%20%20LPSPI2-%26gt%3BTDR%20%3D%200x00%3B%20%2F%2F%20NOP%0A%20%20%20%20%20%20%20%20totalBytes%2B%2B%3B%0A%0A%20%20%20%20%20%20%20%20while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_RDF_MASK))%0A%20%20%20%20%20%20%20%20%20%20%20%20%3B%0A%20%20%20%20%20%20%20%20rxBuf%5Bi%5D%20%3D%20(uint8_t)LPSPI2-%26gt%3BRDR%3B%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CBR%20%2F%3E%3CP%3EHere's%20the%20initialization%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%3E%20%20%20%20PCC-%26gt%3BPCCn%5BPCC_LPSPI2_INDEX%5D%20%3D%200%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Disable%20clocks%20to%20modify%20PCS%20(%20default)%20%09*%2F%0A%20%20%20%20PCC-%26gt%3BPCCn%5BPCC_LPSPI2_INDEX%5D%20%3D%20PCC_PCCn_PR_MASK%20%20%20%20%2F*%20(default)%20Peripheral%20is%20present.%09%09%09*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%20PCC_PCCn_CGC_MASK%20%2F*%20Enable%20PCS%3DSPLL_DIV2%20(40%20MHz%20func'l%20clock)%20%09*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%20PCC_PCCn_PCS(6)%3B%0A%0A%20%20%20%20LPSPI2-%26gt%3BCR%20%3D%200x00000000%3B%20%20%20%20%0A%20%20%20%20LPSPI2-%26gt%3BIER%20%3D%200x00000000%3B%20%20%20%0A%20%20%20%20LPSPI2-%26gt%3BDER%20%3D%200x00000000%3B%20%20%0A%20%20%20%20LPSPI2-%26gt%3BCFGR0%20%3D%200x00000000%3B%20%0A%20%20%20%20LPSPI2-%26gt%3BCFGR1%20%3D%20LPSPI_CFGR1_MASTER_MASK%20%7C%20LPSPI_CFGR1_SAMPLE(1)%20%7C%20LPSPI_CFGR1_AUTOPCS(0)%20%7C%20LPSPI_CFGR1_PCSPOL(0b0000)%3B%20%0A%0A%20%20%20%20LPSPI2-%26gt%3BTCR%20%3D%20LPSPI_TCR_CPHA_MASK%20%7C%20LPSPI_TCR_PRESCALE(2)%20%7C%20LPSPI_TCR_PCS(0)%20%7C%20LPSPI_TCR_FRAMESZ(7)%20%7C%20LPSPI_TCR_CONT(1)%3B%0A%0A%20%20%20%20LPSPI2-%26gt%3BCCR%20%3D%20LPSPI_CCR_SCKPCS(4)%20%7C%20LPSPI_CCR_PCSSCK(4)%20%7C%20LPSPI_CCR_DBT(8)%20%7C%20LPSPI_CCR_SCKDIV(8)%3B%20%0A%20%20%20%20LPSPI2-%26gt%3BFCR%20%3D%20LPSPI_FCR_TXWATER(3)%3B%20%0A%20%20%20%20LPSPI2-%26gt%3BCR%20%3D%20LPSPI_CR_MEN_MASK%20%7C%20LPSPI_CR_DBGEN_MASK%3B%20%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2156154%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20LPSPI%20read%20extra%20byte%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2156154%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EThanks%20for%20your%20help%2C%20Petr.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2155092%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20LPSPI%20read%20extra%20byte%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2155092%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%2C%3C%2FP%3E%0A%3CP%3Eclear%20CONTC%20bit%20in%20TCR.%3CBR%20%2F%3ESee%20attached%20main.c%20file%20I%20used%20in%26nbsp%3BS32K144_Project_LPSPI%20demo.%3CBR%20%2F%3EIt%20gives%20right%20frames.%20I%20tested%20below%20sequences%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22PetrS_0-1755679051074.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22PetrS_0-1755679051074.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F353245iD13120E3F257AAE8%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22PetrS_0-1755679051074.png%22%20alt%3D%22PetrS_0-1755679051074.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22PetrS_1-1755679060431.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22PetrS_1-1755679060431.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F353246iACAB54CAA732EA52%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22PetrS_1-1755679060431.png%22%20alt%3D%22PetrS_1-1755679060431.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EBR%2C%20Petr%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CBR%20%2F%3E%0A%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2154465%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20LPSPI%20read%20extra%20byte%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2154465%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%20Petr%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThanks%20for%20your%20support.%20The%20first%20read%20attempt%20always%20stays%20forever%20in%20the%20while%20RDF%20mask%20loop.%20Even%20with%20your%20code.%20Any%20idea%20why%3F%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%3Eif%20(rxLen%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20LPSPI2-%26gt%3BTCR%20%3D%20tcr_base%20%7C%20LPSPI_TCR_CONT(0)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_RDF_MASK))%20%2F%2F%20Stuck%20here%20on%201st%20read%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20(void)LPSPI2-%26gt%3BRDR%3B%0A%20%20%20%20%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2153659%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20LPSPI%20read%20extra%20byte%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2153659%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%2C%3C%2FP%3E%0A%3CP%3Efor%20each%20write%20to%20TDR%20you%20should%20increment%20your%20totalBytes.%20For%20each%20TDR%20write%20there%20should%20be%20RDR%20reading.%20After%20CONT%20is%20cleared%20you%20should%20read%20RDR%20finally.%20Try%20below%20one%2C%20but%20I%20did%20not%20tested%20that.%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Evoid%20Spi_Transceive(uint8_t%20txLen%2C%20uint8_t%20*txBuf%2C%20uint8_t%20rxLen%2C%20uint8_t%20*rxBuf%2C%20uint8_t%20module%2C%20uint8_t%20pcs)%0A%7B%0A%0A%20%20%20%20uint16_t%20total%20%3D%20rxLen%20%2B%20txLen%3B%0A%20%20%20%20uint32_t%20totalBytes%20%3D%200%3B%0A%20%20%20%20const%20uint32_t%20tcr_base%20%3D%0A%20%20%20%20%20%20%20%20LPSPI_TCR_FRAMESZ(7)%20%7C%20%2F%2F%208-bit%20frames%0A%20%20%20%20%20%20%20%20LPSPI_TCR_PCS(pcs)%20%7C%0A%20%20%20%20%20%20%20%20LPSPI_TCR_CPOL(0)%20%7C%20%0A%20%20%20%20%20%20%20%20LPSPI_TCR_CPHA(1)%20%7C%0A%20%20%20%20%20%20%20%20LPSPI_TCR_PRESCALE(0)%20%7C%0A%20%20%20%20%20%20%20%20LPSPI_TCR_WIDTH(0)%20%7C%0A%20%20%20%20%20%20%20%20LPSPI_TCR_CONTC(1)%3B%0A%0A%20%20%20%20LPSPI2-%26gt%3BCR%20%7C%3D%20LPSPI_CR_RTF_MASK%20%7C%20LPSPI_CR_RRF_MASK%3B%0A%20%20%20%20LPSPI2-%26gt%3BSR%20%3D%200xFFFFFFFFu%3B%0A%20%20%20%20LPSPI2-%26gt%3BFCR%20%3D%20LPSPI_FCR_TXWATER(0)%20%7C%20LPSPI_FCR_RXWATER(0)%3B%0A%0A%20%20%20%20if%20(txLen%26gt%3B0)%0A%09%7B%0A%20%20%20%20%09LPSPI2-%26gt%3BTCR%20%3D%20tcr_base%20%7C%20LPSPI_TCR_CONT(1)%3B%0A%09%09while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_TDF_MASK))%3B%0A%09%09LPSPI2-%26gt%3BTDR%20%3D%20*txBuf%2B%2B%3B%0A%09%09totalBytes%2B%2B%3B%0A%09%09while(totalBytes%20%26lt%3B%20txLen)%0A%09%09%7B%0A%09%09%09while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_TDF_MASK))%3B%0A%09%09%09LPSPI2-%26gt%3BTDR%20%3D%20*txBuf%2B%2B%3B%0A%09%09%09totalBytes%2B%2B%3B%0A%09%09%09while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_RDF_MASK))%3B%0A%09%09%09(void)LPSPI2-%26gt%3BRDR%3B%0A%09%09%7D%0A%09%09if(rxLen%3D%3D0)%20%0A%09%09%7B%0A%09%09%09LPSPI2-%26gt%3BTCR%20%3D%20tcr_base%20%7C%20LPSPI_TCR_CONT(0)%3B%0A%09%09%09while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_RDF_MASK))%3B%0A%09%09%09(void)LPSPI2-%26gt%3BRDR%3B%0A%09%09%7D%0A%09%09%0A%09%7D%0A%20%20%20%20%0A%20%20%20%20if%20(rxLen%26gt%3B0)%0A%20%20%20%20%7B%0A%20%20%20%20%09if(txLen%3D%3D0)%20LPSPI2-%26gt%3BTCR%20%3D%20tcr_base%20%7C%20LPSPI_TCR_CONT(1)%3B%0A%09%09while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_TDF_MASK))%3B%0A%09%09LPSPI2-%26gt%3BTDR%20%3D%200x00%3B%20%2F%2F%20NOP%0A%09%09totalBytes%2B%2B%3B%0A%09%09if(txLen%26gt%3B0)%0A%09%09%7B%0A%09%09%09while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_RDF_MASK))%3B%0A%09%09%09(void)LPSPI2-%26gt%3BRDR%3B%0A%20%20%20%20%09%7D%0A%20%20%20%20%20%09while(totalBytes%20%26lt%3B%20total)%0A%09%09%7B%0A%20%20%20%20%09%09while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_TDF_MASK))%3B%0A%20%20%20%20%09%09LPSPI2-%26gt%3BTDR%20%3D%200x00%3B%20%2F%2F%20NOP%0A%09%09%09totalBytes%2B%2B%3B%0A%09%09%09while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_RDF_MASK))%3B%0A%09%09%09*rxBuf%2B%2B%20%3D%20LPSPI2-%26gt%3BRDR%3B%0A%09%09%7D%0A%20%20%20%20%09LPSPI2-%26gt%3BTCR%20%3D%20tcr_base%20%7C%20LPSPI_TCR_CONT(0)%3B%0A%09%09while%20(!(LPSPI2-%26gt%3BSR%20%26amp%3B%20LPSPI_SR_RDF_MASK))%3B%0A%09%09*rxBuf%2B%2B%20%3D%20LPSPI2-%26gt%3BRDR%3B%0A%20%20%20%20%7D%0A%09%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3BBR%2C%20Petr%3C%2FP%3E%3C%2FLINGO-BODY%3E