Bibibou
Bibibou

Reputation: 21

How to Read GPIO from a CM108B Sound Card on Linux Using HID Interface

I'm using the CM108B sound card for a phone application. I need to use two GPIOs, one for writing and the other for reading. I've managed to write on my GPIOs with HID using Willy's GitHub project, but I haven't found any online resource about reading a GPIO.

Exploring the direwolf project, the parent repo, I tried to write my own read function :

static int cm108_read_gpio(char *name, int iomask) {
    int fd;
    unsigned char io[5] = {0};
    int n;

    // Open the device
    fd = open(name, O_RDWR);
    if (fd == -1) {
        printf("Could not open %s for read/write, errno=%d\n", name, errno);
        if (errno == EACCES) {
            printf("Ensure the device has proper permissions (e.g., group 'audio').\n");
        }
        return -1;
    }

    // Prepare the command to request GPIO state
    io[0] = 0x0;               // Report ID, always 0 for CM108
    io[1] = 0x1;               // Command to read GPIO (as per datasheet)
    io[2] = 0x0;               // Reserved
    io[3] = 0x0;               // Reserved
    io[4] = 0x0;               // Reserved

    // Send the command to the device
    n = write(fd, io, sizeof(io));
    if (n != sizeof(io)) {
        printf("Failed to send GPIO read command to %s, errno=%d\n", name, errno);
        close(fd);
        return -1;
    }

    // Read the response from the device
    n = read(fd, io, sizeof(io));
    if (n != sizeof(io)) {
        printf("Failed to read GPIO state from %s, errno=%d\n", name, errno);
        close(fd);
        return -1;
    }

    // Close the device
    close(fd);

    // Extract the GPIO state for the requested iomask
    int gpio_state = io[2] & iomask;  // Extract the relevant bit(s) for the requested GPIO(s)

    return gpio_state;
}

When I send this command, all my gpios goes back to 0 even if they were set at 1 and the read function is blocking. I'm assuming that I'm send the wrong command to my soundcard.

The datasheet explains well how to write GPIO using HID( even though it is not exactly the same command as the GitHub projects), but I find it really unclear about reading. I would appreciate any insight or project using this feature.

Upvotes: 0

Views: 31

Answers (0)

Related Questions