How to use the SPI controller as a slave deivce ?

  • There is a detailed description in the wiki, please refer to the wiki
    https://wiki.luckfox.com/Luckfox-Pico/L ... munication
  • Thank you very much, I have add the spi slave driver successfully. and I can get the spi device here
    # ls /dev/spidev0.0
    /dev/spidev0.0
    I use the spi_mode_1, pico luckfox as a slave and arduino uno r4 as a master.
    The master send 0xaa, 0x55 and 0x5a three bytes to the slave.
    I found a issue, when I send the three bytes single. First time, the slave revice six bytes:0x75 0x64 0x69 0xAA 0x55 0x5A
    When i send the three bytes second, the slave can receive 0xAA 0x55 0x5A 0xAA 0x55 0x5A very werid issue.
    # ./spi_slave
    ret: 3
    receive data number: 3
    ---------Receive 3 bytes of data max speed:1000000 Hz---------
    75 62 69
    ret: 3
    receive data number: 3
    ---------Receive 3 bytes of data max speed:1000000 Hz---------
    AA 55 5A
    ret: 3
    receive data number: 3
    ---------Receive 3 bytes of data max speed:1000000 Hz---------
    AA 55 5A
    ret: 3
    receive data number: 3
    ---------Receive 3 bytes of data max speed:1000000 Hz---------
    AA 55 5A

    master code :
    SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE1));
    digitalWrite(7, LOW); // enable Slave Select
    delay(1);

    // send test string
    SPI.transfer(0xAA);
    SPI.transfer(0x55);
    SPI.transfer(0x5a);

    digitalWrite(7, HIGH); // disable Slave Select
    SPI.endTransaction();



    Slave code :
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <linux/spi/spidev.h>
    #include <sys/ioctl.h>
    #include <string.h>

    #define SPI_DEVICE_PATH "/dev/spidev0.0"

    int main() {
    int spi_file,ret;
    uint8_t tx_buffer[3];
    uint8_t rx_buffer[3];

    /* Open the SPI device */
    if ((spi_file = open(SPI_DEVICE_PATH, O_RDWR)) < 0) {
    perror("Failed to open SPI device");
    return -1;
    }

    /* Configure SPI mode and bits per word */
    uint8_t mode = SPI_MODE_1;
    uint8_t bits = 8;
    if (ioctl(spi_file, SPI_IOC_WR_MODE, &mode) < 0) {
    perror("Failed to set SPI mode");
    close(spi_file);
    return -1;
    }
    if (ioctl(spi_file, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0) {
    perror("Failed to set SPI bits per word");
    close(spi_file);
    return -1;
    }

    /* Perform SPI transfer */
    struct spi_ioc_transfer transfer = {
    .tx_buf = (unsigned long)tx_buffer,
    .rx_buf = (unsigned long)rx_buffer,
    .len = sizeof(rx_buffer),
    .delay_usecs = 0,
    .speed_hz = 1000000, // SPI speed in Hz
    .bits_per_word = 8,
    };

    while(1)
    {
    /* Clear buffer */
    memset(rx_buffer,0,sizeof(rx_buffer));

    /* Waiting for data */
    do {
    ret = ioctl(spi_file, SPI_IOC_MESSAGE(1), &transfer);
    printf("ret: %d\r\n",ret);
    } while (ret < 0);

    /* Print rx_buffer*/
    printf("receive data number: %d\r\n",ret);
    printf("---------Receive %d bytes of data max speed:%d Hz---------\n",ret,transfer.speed_hz);
    for (int i = 0; i < ret; i++) {
    printf(" %02X",rx_buffer );
    }
    printf("\n");
    }

    /* Close the SPI device */
    close(spi_file);

    return 0;
    }
  • 1. Try extending the delay time after "enable Slave Select".
    2. Attempt to reduce the communication frequency.