Why is my Rx Buffer Not cleared?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DaMe_598781
Level 1
Level 1

PSOC5, SPI Master. I think I am supposed to be clearing the RX Buffer, but it's not empty. Why?

pastedImage_0.png

0 Likes
1 Solution
NoriTan
Employee
Employee
25 sign-ins 5 questions asked 10 sign-ins

I have created a project containing a SPI Master with 32Byte buffer.  Following is the content of the SPIM_1_ClearRxBuffer(void)

void SPIM_1_ClearRxBuffer(void)

{

    /* Clear Hardware RX FIFO */

    while(0u !=(SPIM_1_RX_STATUS_REG & SPIM_1_STS_RX_FIFO_NOT_EMPTY))

    {

        (void) CY_GET_REG8(SPIM_1_RXDATA_PTR);

    }

    #if(SPIM_1_RX_SOFTWARE_BUF_ENABLED)

        /* Disable RX interrupt to protect global veriables */

        SPIM_1_DisableRxInt();

        SPIM_1_rxBufferFull  = 0u;

        SPIM_1_rxBufferRead  = 0u;

        SPIM_1_rxBufferWrite = 0u;

        SPIM_1_EnableRxInt();

    #endif /* (SPIM_1_RX_SOFTWARE_BUF_ENABLED) */

}

In the function the number of characters in the buffer, SPIM_1_rxBufferRead and SPIM_1_rxBufferWrite are cleared but the content of the rxBuffer is not modified.

View solution in original post

6 Replies
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hello DaMe_598781

Please share your project, or atleast the main file so that we can debug the problem at out end. Please refer this KBAArchiving a PSoC Creator Design for details on archiving a project.

Thanks and regards
Harigovind

0 Likes
NoriTan
Employee
Employee
25 sign-ins 5 questions asked 10 sign-ins

I have created a project containing a SPI Master with 32Byte buffer.  Following is the content of the SPIM_1_ClearRxBuffer(void)

void SPIM_1_ClearRxBuffer(void)

{

    /* Clear Hardware RX FIFO */

    while(0u !=(SPIM_1_RX_STATUS_REG & SPIM_1_STS_RX_FIFO_NOT_EMPTY))

    {

        (void) CY_GET_REG8(SPIM_1_RXDATA_PTR);

    }

    #if(SPIM_1_RX_SOFTWARE_BUF_ENABLED)

        /* Disable RX interrupt to protect global veriables */

        SPIM_1_DisableRxInt();

        SPIM_1_rxBufferFull  = 0u;

        SPIM_1_rxBufferRead  = 0u;

        SPIM_1_rxBufferWrite = 0u;

        SPIM_1_EnableRxInt();

    #endif /* (SPIM_1_RX_SOFTWARE_BUF_ENABLED) */

}

In the function the number of characters in the buffer, SPIM_1_rxBufferRead and SPIM_1_rxBufferWrite are cleared but the content of the rxBuffer is not modified.

Thank you - but can you elaborate? Why does the API call ClearRxBuffer() NOT clear the RX buffer?

Also, using the global functions you suggest - seems this only clears one byte. For example the description of BufferRead is "Receives buffer location of the last data read from the buffer by the API.". How would BufferRead = 0u clear the entire buffer?

Thank you.

0 Likes

On ClearRxBuffer()API only the read/write indices into the circular buffer are set accordingly, so that the software sees an empty buffer. The contents of the buffer remain unchanged until new data gets in.

Bob

0 Likes

THanks - not at all clear from the data sheet... and why would SPIM_1_rxBufferRead  = 0u;    clear the buffer, not just one location?

0 Likes

When a circular buffer is implemented  two indices rxBufferRead and rxBufferWrite are declared.

rxBufferWrite indicates the location where the last byte was written to.

rxBufferRead indicates the location where the last byte was read from.

rxBufferWrite increased when a byte is received from the SPI, and rxBufferRead increased when a byte is gotten from the buffer.  So if the rxBufferRead is equal to the rxBufferWrite, it is assumed that there is no data in the buffer.

Please refer Circular buffer - Wikipedia for a generic information.

0 Likes