簡體   English   中英

如何在Linux中寫入CM108芯片的GPIO引腳?

[英]Howto Write to the GPIO Pin of the CM108 Chip in Linux?

C-Media的CM108具有4個GPIO引腳,您可以通過hid接口訪問該引腳。

使用Windows中的通用寫入功能,我能夠寫入gpio引腳。

但是我試圖在Linux中做同樣的事情而沒有成功。

linux內核將設備檢測為Hidraw設備。

注意:我能夠從設備讀取,但不能寫入。 (我已以root用戶身份運行該應用程序,只是為了確保它不是權限問題)。

我得到了這個工作,這是怎么做的。

我需要創建一個新的Linux隱藏內核mod。 (並不難)/ *

/*
 * Driver for the C-Media 108 chips
 *
 * Copyright (C) 2009 Steve Beaulac <steve@sagacity.ca>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2.
 */

/*
 * This driver is based on the cm109.c driver
 */

#include <linux/device.h>
#include <linux/hid.h>
#include <linux/module.h>

#define DRIVER_VERSION "20090526"
#define DRIVER_AUTHOR "Steve Beaulac"
#define DRIVER_DESC "C-Media 108 chip"

#define CM108_VENDOR_ID 0x0d8c
#define CM108_PRODUCT_ID 0x000c

#ifdef CONFIG_USB_DYNAMIC_MINORS
#define CM108_MINOR_BASE 0
#else
#define CM108_MINOR_BASE 96
#endif

/*
 * Linux interface and usb initialisation
 */

static int cm108_hid_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
    int ret;

    ret = hid_parse(hdev);
    if (ret) {
        dev_err(&hdev->dev, "parse failed\n");
        goto error;
    }

    ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
    if (ret) {
        dev_err(&hdev->dev, "hw start failed\n");
        goto error;
    }
    return 0;

error:
    return ret;
}

static struct hid_device_id cm108_device_table[] = {
    { HID_USB_DEVICE (CM108_VENDOR_ID, CM108_PRODUCT_ID) },
    /* you can add more devices here with product ID 0x0008 - 0x000f */
    { }
};
MODULE_DEVICE_TABLE (hid, cm108_device_table);

static struct hid_driver hid_cm108_driver = {
    .name = "cm108",
    .id_table = cm108_device_table, 
    .probe = cm108_hid_probe,
};

static int hid_cm108_init(void)
{
    return hid_register_driver(&hid_cm108_driver);
}

static void hid_cm108_exit(void)
{
    hid_unregister_driver(&hid_cm108_driver);   
}

module_init(hid_cm108_init);
module_exit(hid_cm108_exit);

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

用過的這個makefile

obj-m += cm108.o

並編譯模塊

make -C /lib/modules/`uname -r`/build/ M=`pwd` EXTRAVERSION="-generic" modules

sudo make -C /lib/modules/`uname -r`/build/ M=`pwd` EXTRAVERSION="-generic" modules_install

depmod -a

我必須修改modules.order文件,以便在通用hid linux模塊之前可以查詢我的模塊。

該模塊確保hidraw使用接口2。

然后,我可以使用fopen讀取和寫入CM108芯片的GPIO引腳。

順便說一句:寫入時需要寫入5個字節,第一個字節用於HID_OUTPUT_REPORT

Linux中的大多數硬件都可以作為文件訪問。 如果驅動程序在文件系統上為其創建了硬件節點,那么您很幸運。 您將能夠使用常規文件例程對其進行寫入。 否則,您可能需要做一些組裝魔術,這可能需要您編寫內核模塊才能完成。

這是如何在Linux上寫入CM108 / CM119 GPIO引腳的完整示例。

https://github.com/wb2osz/direwolf/blob/dev/cm108.c

您無需以root用戶身份運行或編寫自己的設備驅動程序。

我有相反的問題。 我試圖弄清楚如何在Windows上執行相同的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM