Vim3 spidev1.1 CS pin problem

Which Khadas SBC do you use?

**Vim 3.**

Which system do you use? Android, Ubuntu, OOWOW or others?

** Ubuntu 20.04 **

Which version of system do you use? Khadas official images, self built images, or others?

**(https://dl.khadas.com/products/vim3/firmware/ubuntu/emmc/vim3-ubuntu-20.04-gnome-linux-4.9-fenix-1.4-221229-emmc.img.xz)**

Please describe your issue below:

**Our system needs CAN communication protocol over spi protocol, so we used mcp2515 ic to achive this. We modified /boot/env.txt file like this : "overlays=spi1". When we reboot the device and send message form khadas to mcp2515(external ic connected to 40 pin header) we notice that cs pin not working. We type the "gpio readall" bash command, the cs pin is still input mode. Khadas runs the spi as a master, so cs pin should be controlled by khadas. Should the cs pin become output to be successful in our situation? My Second question is that : "what we should do about cs pin while we need multi spi device connected?". Echo commands and cs pin usage from GPIO driver are useless because the time is cs high-low-high time is much more larger than the spi message transmit time(~10 us for message transmit time, gpio cs pin with echo commands approximately 15-80 ms). How can we use spi communication on khadas with custom cs pin while we using spidev1.1? Any advice or solution? **

Post a console log of your issue below:


**Delete this line and post your log here.**

Here is my spi cs pin control code,hope it may help you!

/*use fixed GPIO, the number of GPIO should be calculated by youself*/
void GPIO_Init(void)
{
    char buf[4] = "433";
    int fd;
    if ((fd = open("/sys/class/gpio/export", O_WRONLY)) == -1)

    {
        printf("export gpio433 fail\r\n");
    }
    write(fd, buf, 3);
    close(fd);
    if ((fd = open("/sys/class/gpio/gpio433/direction", O_WRONLY)) == -1)
    {
        printf("direction gpio433 fail\r\n");
    }
    sprintf(buf, "out");
    write(fd, buf, 3);
    close(fd);

    sprintf(buf, "464");
    if ((fd = open("/sys/class/gpio/export", O_WRONLY)) == -1)
    {
        printf("export gpio464 fail\r\n");
    }
    write(fd, buf, 3);
    close(fd);
    if ((fd = open("/sys/class/gpio/gpio464/direction", O_WRONLY)) == -1)
    {
        printf("direction gpio464 fail\r\n");
    }
    sprintf(buf, "out");
    write(fd, buf, 3);
    close(fd);
}
void CS_Control(int cs, int level)
{
    int fd;
    char buf[2];
    char fName[128];
    if (cs == 0)
    {
        sprintf(fName, "/sys/class/gpio/gpio433/value");
    }
    if (cs == 1)
    {
        sprintf(fName, "/sys/class/gpio/gpio464/value");
    }
    if (level == 0)
    {
        sprintf(buf, "0");
    }
    else
    {
        sprintf(buf, "1");
    }
    fd = open(fName, O_WRONLY);
    write(fd, buf, 1);
    close(fd);
}

usage:

...
        GPIO_Init();
...
        CS_Control(spi_1_fd , Low);
        // do spi communication
        int ret = ioctl(spi_1_fd , SPI_IOC_MESSAGE(1),
                        &spi_message);
        CS_Control(spi_1_fd , High);

I have tested its GPIO Speed via a logic analyzer, It works well under 12M(maybe higher? you can try it,12M is enough for me).

2 Likes

Thanks Dylan, can you share the logic analyzer photo please? I also find a solution more complex than your solution, i will share it and publish a guide for this issue. Thanks a lot…

The photo I have is only this one, it can’t show any info about cs pin time.I can’t offer more photos you need because I am on vacation, not in the school lab.

1 Like