0

The setup is: RPI: Raspberry Pi Zero W PSoC: CY8CKIT-059

I have created the following code below and I'm able to write from the SPI Master(RPI) to the SPI slave(PSoC) and toggle(on/off) the LED, but how do I read from the slave?

I want to send data back from the slave and print it out on the master.

Code:

#define SPI_DEVICE "/dev/spidev0.0"
#define BUF_SIZE 1
#define SPI_SPEED 10
#define DELAY 5

int main(void)
{

    int status;

    int tx_buffer2[BUF_SIZE] = {2};
    int rx_buffer2[BUF_SIZE];

    int tx_buffer3[BUF_SIZE] = {3};
    int rx_buffer3[BUF_SIZE];

    struct spi_ioc_transfer transfer2 = {
    .tx_buf = (unsigned long)tx_buffer2,
    .rx_buf = (unsigned long)rx_buffer2,
    .len = BUF_SIZE,
    .speed_hz = 2500,
    .delay_usecs = DELAY,
    .bits_per_word = 8,
    .cs_change = 0,
    };

    struct spi_ioc_transfer transfer3 = {
    .tx_buf = (unsigned long)tx_buffer3,
    .rx_buf = (unsigned long)rx_buffer3,
    .len = BUF_SIZE,
    .speed_hz = 2500,
    .delay_usecs = DELAY,
    .bits_per_word = 8,
    .cs_change = 0,
    };


    int fd;
    int err;

    while (1)
    {
        fd = open(SPI_DEVICE,O_RDWR);

        status = ioctl(fd, SPI_IOC_MESSAGE(1), &transfer2);
        if (status < 0)
        {
            perror("Fejl i IOCTL");
        }
        printf("Turning on LED!\n");
        close(fd);


        sleep(5); // Sleeps

        fd = open(SPI_DEVICE,O_RDWR);
        status = ioctl(fd, SPI_IOC_MESSAGE(1), &transfer3);
        if (status < 0)
        {
            perror("Fejl i IOCTL");
        }
        printf("Turning off LED!\n");
        close(fd);
        

        sleep(5); // Sleeps

    }
    


    return 0;
}

0 Answers0