Creator 4.2: Read SPI message

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

cross mob
ZvVe_2582751
Level 1
Level 1
5 replies posted Welcome! 5 questions asked

Hello,

I have to receive 8 bytes message via SPI.

What is the right way doing it in polling, without any interrupts.

For writing, I used SPIS_PutArray . But there is no SPIS_GetArray.

Thank you,

Zvika

0 Likes
1 Solution
KyTr_1955226
Level 6
Level 6
250 sign-ins 10 likes given 50 solutions authored

There's no GetArray, but you have ReadRxStatus, GetRxBufferSize, and ReadRxData, so you've got what you need to read out bytes.

You would do something to this effect:

#define MAX_NUM_BYTES    64    /*Can be whatever arbitrary limit is decided*/

uint8_t SPIS_InBytes[MAX_NUM_BYTES] = {0};

void Process_SPIS_In(void){

    uint8_t num_bytes = SPIS_GetBufferSize();

    uint8_t byte_index = 0;

/*Read SPI bytes in until there are none left in the FIFO*/

    while ((SPIS_ReadRxStatus() & SPIS_STS_RX_FIFO_NOT_EMPTY) != 0){

        SPIS_InBytes[byte_index++] = SPIS_ReadRxData();

        /*For safety, make sure we are not exceeding MAX_NUM_BYTES*/

        /*Loop back to the first index - we could also decrement byte_index to only overwrite the last index */

        if (byte_index >= MAX_NUM_BYTES){

            byte_index = 0;

        }

    }

}

You would also need to reset byte_index to 0, as well as clear out SPIS_InBytes[] for the next transmission when you determine your message is "done", however you choose to decide that (number of bytes received?  End of Packet byte?) is up to you.

View solution in original post

2 Replies
KyTr_1955226
Level 6
Level 6
250 sign-ins 10 likes given 50 solutions authored

There's no GetArray, but you have ReadRxStatus, GetRxBufferSize, and ReadRxData, so you've got what you need to read out bytes.

You would do something to this effect:

#define MAX_NUM_BYTES    64    /*Can be whatever arbitrary limit is decided*/

uint8_t SPIS_InBytes[MAX_NUM_BYTES] = {0};

void Process_SPIS_In(void){

    uint8_t num_bytes = SPIS_GetBufferSize();

    uint8_t byte_index = 0;

/*Read SPI bytes in until there are none left in the FIFO*/

    while ((SPIS_ReadRxStatus() & SPIS_STS_RX_FIFO_NOT_EMPTY) != 0){

        SPIS_InBytes[byte_index++] = SPIS_ReadRxData();

        /*For safety, make sure we are not exceeding MAX_NUM_BYTES*/

        /*Loop back to the first index - we could also decrement byte_index to only overwrite the last index */

        if (byte_index >= MAX_NUM_BYTES){

            byte_index = 0;

        }

    }

}

You would also need to reset byte_index to 0, as well as clear out SPIS_InBytes[] for the next transmission when you determine your message is "done", however you choose to decide that (number of bytes received?  End of Packet byte?) is up to you.

Hello,

Thank you very much !

Best regards,

Zvika

0 Likes