PSoC 6 - SPI with two devices

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

cross mob
user_349366
Level 1
Level 1

Good day. This is my first post. I have a problem with SPI communication. I am using PSoC 6 and would like to communicate it with two devices using SPI communication. Unfortunately, I do not know how to do it. I do not know how to give them ID numbers. My second question is how to send a structure using SPI? I want to communicate with PSoC 6 with Analog Devices AD converter model AD7177-2. I found sample libraries on GitHub but I can not use them. Please help.

0 Likes
1 Solution
BragadeeshV
Moderator
Moderator
Moderator
First question asked 1000 replies posted 750 replies posted

Hi user_336603693​,

Welcome to the community.

Did you get a chance to see the code examples we have in PSoC 6? If not please visit the following page:

https://www.cypress.com/documentation/code-examples/psoc-6-mcu-code-examples

PSoC 6 SPI Master Code example:

https://www.cypress.com/documentation/code-examples/ce221120-psoc-6-mcu-spi-master

You need to select the no of SS pins to two and connect each SS pin to each on of the slave devices.

Passing structure in SPI:

typedef struct

{

    uint8 data1;

    uint8 data2[10];

    uint16 data3;

    uint16 data4[5];

  

}TXBuffer;

uint32_t WriteSPI ()

{

  cy_en_scb_spi_status_t errorStatus;

  TXBuffer dataBuf = {

                        .data1 = 1,

                        .data2 = {1,2,3,4,5,6,7,8,9,0xA},

                        .data3 = 0xABCD,

                        .data4 = {0x12CD,0x3412,0x2334,0x5634,0x7324}

                       };

uint8_t statusRxBuf[sizeof(dataBuf)];

errorStatus = Cy_SCB_SPI_Transfer(mSPI_HW, &dataBuf, statusRxBuf, sizeof(dataBuf), &mSPI_context);

  /* If no error wait till master sends data in Tx FIFO */

    if(errorStatus == CY_SCB_SPI_SUCCESS)

    {

        uint32_t masterStatus;      

        /* Timeout 1 sec */

        uint32_t timeOut = 1000UL;

      

        /* Wait until master complete read transfer or time out has occured */

        do

        {

            masterStatus  = Cy_SCB_SPI_GetTransferStatus(mSPI_HW, &mSPI_context);

            Cy_SysLib_Delay(CY_SCB_WAIT_1_UNIT);

            timeOut--;

          

        } while ((0UL != (masterStatus & CY_SCB_SPI_TRANSFER_ACTIVE)) && (timeOut > 0UL));

      

      

        if ((0UL == (MASTER_ERROR_MASK & masterStatus)) &&

            (sizeof(dataBuf) == Cy_SCB_SPI_GetNumTransfered(mSPI_HW, &mSPI_context)))

        {

            status = TRANSFER_CMPLT;    

        }

        else

        {

            HandleError();

        }

    }

  

    return (status);

}

Regards,

Bragadeesh

Regards,
Bragadeesh

View solution in original post

0 Likes
1 Reply
BragadeeshV
Moderator
Moderator
Moderator
First question asked 1000 replies posted 750 replies posted

Hi user_336603693​,

Welcome to the community.

Did you get a chance to see the code examples we have in PSoC 6? If not please visit the following page:

https://www.cypress.com/documentation/code-examples/psoc-6-mcu-code-examples

PSoC 6 SPI Master Code example:

https://www.cypress.com/documentation/code-examples/ce221120-psoc-6-mcu-spi-master

You need to select the no of SS pins to two and connect each SS pin to each on of the slave devices.

Passing structure in SPI:

typedef struct

{

    uint8 data1;

    uint8 data2[10];

    uint16 data3;

    uint16 data4[5];

  

}TXBuffer;

uint32_t WriteSPI ()

{

  cy_en_scb_spi_status_t errorStatus;

  TXBuffer dataBuf = {

                        .data1 = 1,

                        .data2 = {1,2,3,4,5,6,7,8,9,0xA},

                        .data3 = 0xABCD,

                        .data4 = {0x12CD,0x3412,0x2334,0x5634,0x7324}

                       };

uint8_t statusRxBuf[sizeof(dataBuf)];

errorStatus = Cy_SCB_SPI_Transfer(mSPI_HW, &dataBuf, statusRxBuf, sizeof(dataBuf), &mSPI_context);

  /* If no error wait till master sends data in Tx FIFO */

    if(errorStatus == CY_SCB_SPI_SUCCESS)

    {

        uint32_t masterStatus;      

        /* Timeout 1 sec */

        uint32_t timeOut = 1000UL;

      

        /* Wait until master complete read transfer or time out has occured */

        do

        {

            masterStatus  = Cy_SCB_SPI_GetTransferStatus(mSPI_HW, &mSPI_context);

            Cy_SysLib_Delay(CY_SCB_WAIT_1_UNIT);

            timeOut--;

          

        } while ((0UL != (masterStatus & CY_SCB_SPI_TRANSFER_ACTIVE)) && (timeOut > 0UL));

      

      

        if ((0UL == (MASTER_ERROR_MASK & masterStatus)) &&

            (sizeof(dataBuf) == Cy_SCB_SPI_GetNumTransfered(mSPI_HW, &mSPI_context)))

        {

            status = TRANSFER_CMPLT;    

        }

        else

        {

            HandleError();

        }

    }

  

    return (status);

}

Regards,

Bragadeesh

Regards,
Bragadeesh
0 Likes