<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: LPSPI read extra byte in S32K</title>
    <link>https://community.nxp.com/t5/S32K/LPSPI-read-extra-byte/m-p/2156154#M52054</link>
    <description>&lt;P&gt;Thanks for your help, Petr.&lt;/P&gt;</description>
    <pubDate>Thu, 21 Aug 2025 14:39:42 GMT</pubDate>
    <dc:creator>rmaier</dc:creator>
    <dc:date>2025-08-21T14:39:42Z</dc:date>
    <item>
      <title>LPSPI read extra byte</title>
      <link>https://community.nxp.com/t5/S32K/LPSPI-read-extra-byte/m-p/2152890#M51864</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;What am I missing to only have to send 4 bytes to write/read 4 bytes?&lt;BR /&gt;&lt;BR /&gt;Here's my function:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;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-&amp;gt;CR |= LPSPI_CR_RTF_MASK | LPSPI_CR_RRF_MASK;
    LPSPI2-&amp;gt;SR = 0xFFFFFFFFu;
    LPSPI2-&amp;gt;FCR = LPSPI_FCR_TXWATER(0) | LPSPI_FCR_RXWATER(0);

    while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_TDF_MASK))
        ;
    LPSPI2-&amp;gt;TCR = tcr_base | LPSPI_TCR_CONT(1);
    LPSPI2-&amp;gt;TDR = (uint8_t)txBuf[0];
    totalBytes++;

    for (uint8_t i = 1; i &amp;lt; txLen; i++)
    {
        if (totalBytes == total)
        {
            LPSPI2-&amp;gt;TCR = tcr_base | LPSPI_TCR_CONT(0);
        }

        while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_TDF_MASK))
            ;

        LPSPI2-&amp;gt;TDR = (uint8_t)txBuf[i];
        totalBytes++;

        while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_RDF_MASK))
            ;
        (void)LPSPI2-&amp;gt;RDR;
    }

    if (rxLen &amp;gt; 0)
    {
        while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_TDF_MASK))
            ;
        LPSPI2-&amp;gt;TDR = 0x00; // NOP
        while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_RDF_MASK))
            ;
        (void)LPSPI2-&amp;gt;RDR;
    }
    else
    {
        return;
    }

    for (uint8_t i = 0; i &amp;lt; rxLen; i++)
    {
        if (totalBytes == total)
        {
            LPSPI2-&amp;gt;TCR = tcr_base | LPSPI_TCR_CONT(0);
        }
        while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_TDF_MASK))
            ;

        LPSPI2-&amp;gt;TDR = 0x00; // NOP
        totalBytes++;

        while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_RDF_MASK))
            ;
        rxBuf[i] = (uint8_t)LPSPI2-&amp;gt;RDR;
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the initialization:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;    PCC-&amp;gt;PCCn[PCC_LPSPI2_INDEX] = 0;                  /* Disable clocks to modify PCS ( default) 	*/
    PCC-&amp;gt;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-&amp;gt;CR = 0x00000000;    
    LPSPI2-&amp;gt;IER = 0x00000000;   
    LPSPI2-&amp;gt;DER = 0x00000000;  
    LPSPI2-&amp;gt;CFGR0 = 0x00000000; 
    LPSPI2-&amp;gt;CFGR1 = LPSPI_CFGR1_MASTER_MASK | LPSPI_CFGR1_SAMPLE(1) | LPSPI_CFGR1_AUTOPCS(0) | LPSPI_CFGR1_PCSPOL(0b0000); 

    LPSPI2-&amp;gt;TCR = LPSPI_TCR_CPHA_MASK | LPSPI_TCR_PRESCALE(2) | LPSPI_TCR_PCS(0) | LPSPI_TCR_FRAMESZ(7) | LPSPI_TCR_CONT(1);

    LPSPI2-&amp;gt;CCR = LPSPI_CCR_SCKPCS(4) | LPSPI_CCR_PCSSCK(4) | LPSPI_CCR_DBT(8) | LPSPI_CCR_SCKDIV(8); 
    LPSPI2-&amp;gt;FCR = LPSPI_FCR_TXWATER(3); 
    LPSPI2-&amp;gt;CR = LPSPI_CR_MEN_MASK | LPSPI_CR_DBGEN_MASK; &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Aug 2025 13:31:15 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/LPSPI-read-extra-byte/m-p/2152890#M51864</guid>
      <dc:creator>rmaier</dc:creator>
      <dc:date>2025-08-15T13:31:15Z</dc:date>
    </item>
    <item>
      <title>Re: LPSPI read extra byte</title>
      <link>https://community.nxp.com/t5/S32K/LPSPI-read-extra-byte/m-p/2153659#M51904</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;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-&amp;gt;CR |= LPSPI_CR_RTF_MASK | LPSPI_CR_RRF_MASK;
    LPSPI2-&amp;gt;SR = 0xFFFFFFFFu;
    LPSPI2-&amp;gt;FCR = LPSPI_FCR_TXWATER(0) | LPSPI_FCR_RXWATER(0);

    if (txLen&amp;gt;0)
	{
    	LPSPI2-&amp;gt;TCR = tcr_base | LPSPI_TCR_CONT(1);
		while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_TDF_MASK));
		LPSPI2-&amp;gt;TDR = *txBuf++;
		totalBytes++;
		while(totalBytes &amp;lt; txLen)
		{
			while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_TDF_MASK));
			LPSPI2-&amp;gt;TDR = *txBuf++;
			totalBytes++;
			while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_RDF_MASK));
			(void)LPSPI2-&amp;gt;RDR;
		}
		if(rxLen==0) 
		{
			LPSPI2-&amp;gt;TCR = tcr_base | LPSPI_TCR_CONT(0);
			while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_RDF_MASK));
			(void)LPSPI2-&amp;gt;RDR;
		}
		
	}
    
    if (rxLen&amp;gt;0)
    {
    	if(txLen==0) LPSPI2-&amp;gt;TCR = tcr_base | LPSPI_TCR_CONT(1);
		while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_TDF_MASK));
		LPSPI2-&amp;gt;TDR = 0x00; // NOP
		totalBytes++;
		if(txLen&amp;gt;0)
		{
			while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_RDF_MASK));
			(void)LPSPI2-&amp;gt;RDR;
    	}
     	while(totalBytes &amp;lt; total)
		{
    		while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_TDF_MASK));
    		LPSPI2-&amp;gt;TDR = 0x00; // NOP
			totalBytes++;
			while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_RDF_MASK));
			*rxBuf++ = LPSPI2-&amp;gt;RDR;
		}
    	LPSPI2-&amp;gt;TCR = tcr_base | LPSPI_TCR_CONT(0);
		while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_RDF_MASK));
		*rxBuf++ = LPSPI2-&amp;gt;RDR;
    }
	
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;BR, Petr&lt;/P&gt;</description>
      <pubDate>Mon, 18 Aug 2025 12:47:47 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/LPSPI-read-extra-byte/m-p/2153659#M51904</guid>
      <dc:creator>PetrS</dc:creator>
      <dc:date>2025-08-18T12:47:47Z</dc:date>
    </item>
    <item>
      <title>Re: LPSPI read extra byte</title>
      <link>https://community.nxp.com/t5/S32K/LPSPI-read-extra-byte/m-p/2154465#M51956</link>
      <description>&lt;P&gt;Hello Petr,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your support. The first read attempt always stays forever in the while RDF mask loop. Even with your code. Any idea why?&lt;/P&gt;&lt;LI-CODE lang="c"&gt;if (rxLen == 0)
        {
            LPSPI2-&amp;gt;TCR = tcr_base | LPSPI_TCR_CONT(0);
            while (!(LPSPI2-&amp;gt;SR &amp;amp; LPSPI_SR_RDF_MASK)) // Stuck here on 1st read
                ;
            (void)LPSPI2-&amp;gt;RDR;
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Aug 2025 13:58:08 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/LPSPI-read-extra-byte/m-p/2154465#M51956</guid>
      <dc:creator>rmaier</dc:creator>
      <dc:date>2025-08-19T13:58:08Z</dc:date>
    </item>
    <item>
      <title>Re: LPSPI read extra byte</title>
      <link>https://community.nxp.com/t5/S32K/LPSPI-read-extra-byte/m-p/2155092#M51983</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;clear CONTC bit in TCR.&lt;BR /&gt;See attached main.c file I used in&amp;nbsp;S32K144_Project_LPSPI demo.&lt;BR /&gt;It gives right frames. I tested below sequences&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PetrS_0-1755679051074.png" style="width: 400px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/353245iD13120E3F257AAE8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PetrS_0-1755679051074.png" alt="PetrS_0-1755679051074.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PetrS_1-1755679060431.png" style="width: 400px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/353246iACAB54CAA732EA52/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PetrS_1-1755679060431.png" alt="PetrS_1-1755679060431.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;BR, Petr&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Aug 2025 08:38:56 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/LPSPI-read-extra-byte/m-p/2155092#M51983</guid>
      <dc:creator>PetrS</dc:creator>
      <dc:date>2025-08-20T08:38:56Z</dc:date>
    </item>
    <item>
      <title>Re: LPSPI read extra byte</title>
      <link>https://community.nxp.com/t5/S32K/LPSPI-read-extra-byte/m-p/2156154#M52054</link>
      <description>&lt;P&gt;Thanks for your help, Petr.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Aug 2025 14:39:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/LPSPI-read-extra-byte/m-p/2156154#M52054</guid>
      <dc:creator>rmaier</dc:creator>
      <dc:date>2025-08-21T14:39:42Z</dc:date>
    </item>
  </channel>
</rss>

