gpio_desc()的分析

Linux下GPIO驱动(三) ----gpio_desc()的分析

 

上篇最后提出的疑问是结构体gpio_chip中的成员函数set等是怎么实现的,在回答之前先介绍下gpio_desc这个结构体。 

 

 

 

     如上图所示,右上方部分为GPIO驱动对其它驱动提供的GPIO操作接口,其对应的右下方部分为GPIO硬件操作接口,也就是说对外提供的接口最终会一一对应的对硬件GPIO进行操作。

     再来看左边部分,左上方部分为一全局数组,记录各个GPIO的描述符,即对应左下方的gpio_desc结构体,其中gpio_chip指向硬件层的GPIO,flags为一标志位,用来指示当前GPIO是否已经占用,当用gpio_request申请GPIO资源时,flags位就会置位,当调用gpio_free释放GPIO资源时,flags就会清零。label是一个字符串指针,用来作说明。

     在软件上,我们首先通过函数gpiochip_add注册一个gpio_chip对应的gpio_desc到全局数组gpio描述符中。其中,一个描述符对应一个GPIO,所以如果我们要使用多个GPIO,那么就在gpio_chip结构体的ngpio指定个数,base为起始的GPIO号。

 

//每个引脚分配一个gpio_desc数据结构
struct gpio_desc {
    struct gpio_chip    *chip;
    unsigned long        flags;
};

 

复制代码

/**
 * struct gpio_chip - abstract a GPIO controller
 * @label: for diagnostics
 * @dev: optional device providing the GPIOs
 * @owner: helps prevent removal of modules exporting active GPIOs
 * @request: optional hook for chip-specific activation, such as
 *    enabling module power and clock; may sleep
 * @free: optional hook for chip-specific deactivation, such as
 *    disabling module power and clock; may sleep
 * @direction_input: configures signal "offset" as input, or returns error
 * @get: returns value for signal "offset"; for output signals this
 *    returns either the value actually sensed, or zero
 * @direction_output: configures signal "offset" as output, or returns error
 * @set: assigns output value for signal "offset"
 * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
 *    implementation may not sleep
 * @dbg_show: optional routine to show contents in debugfs; default code
 *    will be used when this is omitted, but custom code can show extra
 *    state (such as pullup/pulldown configuration).
 * @base: identifies the first GPIO number handled by this chip; or, if
 *    negative during registration, requests dynamic ID allocation.
 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
 *    handled is (base + ngpio - 1).
 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
 *    must while accessing GPIO expander chips over I2C or SPI
 * @names: if set, must be an array of strings to use as alternative
 *      names for the GPIOs in this chip. Any entry in the array
 *      may be NULL if there is no alias for the GPIO, however the
 *      array must be @ngpio entries long.  A name can include a single printk
 *      format specifier for an unsigned int.  It is substituted by the actual
 *      number of the gpio.
 *
 * A gpio_chip can help platforms abstract various sources of GPIOs so
 * they can all be accessed through a common programing interface.
 * Example sources would be SOC controllers, FPGAs, multifunction
 * chips, dedicated GPIO expanders, and so on.
 *
 * Each chip controls a number of signals, identified in method calls
 * by "offset" values in the range 0..(@ngpio - 1).  When those signals
 * are referenced through calls like gpio_get_value(gpio), the offset
 * is calculated by subtracting @base from the gpio number.
 */
struct gpio_chip {
//这些函数实现在arch\arm\mach-s5pv210\gpiolib.c    
    const char        *label;
    struct device        *dev;
    struct module        *owner;

    int            (*request)(struct gpio_chip *chip,
                        unsigned offset);
    void            (*free)(struct gpio_chip *chip,
                        unsigned offset);

    int            (*direction_input)(struct gpio_chip *chip,
                        unsigned offset);
    int            (*get)(struct gpio_chip *chip,
                        unsigned offset);
    int            (*direction_output)(struct gpio_chip *chip,
                        unsigned offset, int value);
    int            (*set_debounce)(struct gpio_chip *chip,
                        unsigned offset, unsigned debounce);

    void            (*set)(struct gpio_chip *chip,
                        unsigned offset, int value);

    int            (*to_irq)(struct gpio_chip *chip,
                        unsigned offset);

    void            (*dbg_show)(struct seq_file *s,
                        struct gpio_chip *chip);
    int            base;
    u16            ngpio;
    const char        *const *names;
    unsigned        can_sleep:1;
    unsigned        exported:1;
};

复制代码

下面分析gpio_desc中成员chip的成员函数的实现:

复制代码

__init int s5pv210_gpiolib_init(void)//在Linux初始化期间,此函数就执行了
{
    struct s3c_gpio_chip *chip = s5pv210_gpio_4bit;
    int nr_chips = ARRAY_SIZE(s5pv210_gpio_4bit);
    int i = 0;

    for (i = 0; i < nr_chips; i++, chip++) {
        if (chip->config == NULL)
            chip->config = &gpio_cfg;
        if (chip->base == NULL)
            chip->base = S5PV210_BANK_BASE(i);
    }

    samsung_gpiolib_add_4bit_chips(s5pv210_gpio_4bit, nr_chips);

    return 0;
}

复制代码

复制代码

void __init samsung_gpiolib_add_4bit_chips(struct s3c_gpio_chip *chip,
                       int nr_chips)
{
    for (; nr_chips > 0; nr_chips--, chip++) {
        samsung_gpiolib_add_4bit(chip);
        s3c_gpiolib_add(chip);

    }
}

复制代码

复制代码

void __init samsung_gpiolib_add_4bit(struct s3c_gpio_chip *chip)
{
    chip->chip.direction_input = samsung_gpiolib_4bit_input;
    chip->chip.direction_output = samsung_gpiolib_4bit_output;
    chip->pm = __gpio_pm(&s3c_gpio_pm_4bit);
}

复制代码

复制代码

__init void s3c_gpiolib_add(struct s3c_gpio_chip *chip)
{
    struct gpio_chip *gc = &chip->chip;
    int ret;

    BUG_ON(!chip->base);
    BUG_ON(!gc->label);
    BUG_ON(!gc->ngpio);

    spin_lock_init(&chip->lock);// 初始化s3c_gpio_chip的自旋锁
if (!gc->direction_input)
        gc->direction_input = s3c_gpiolib_input;//chip->direction_input
    if (!gc->direction_output)
        gc->direction_output = s3c_gpiolib_output;//chip->direction_output
    if (!gc->set)
        gc->set = s3c_gpiolib_set;//chip->set此处就回答了上篇的疑问
    if (!gc->get)
        gc->get = s3c_gpiolib_get;//chip->get

#ifdef CONFIG_PM
    if (chip->pm != NULL) {
        if (!chip->pm->save || !chip->pm->resume)
            printk(KERN_ERR "gpio: %s has missing PM functions\n",
                   gc->label);
    } else
        printk(KERN_ERR "gpio: %s has no PM function\n", gc->label);
#endif

    /* gpiochip_add() prints own failure message on error. */
    ret = gpiochip_add(gc);
    if (ret >= 0)
        s3c_gpiolib_track(chip);
}

复制代码

复制代码

/**
 * gpiochip_add() - register a gpio_chip
 * @chip: the chip to register, with chip->base initialized
 * Context: potentially before irqs or kmalloc will work
 *
 * Returns a negative errno if the chip can't be registered, such as
 * because the chip->base is invalid or already associated with a
 * different chip.  Otherwise it returns zero as a success code.
 *
 * When gpiochip_add() is called very early during boot, so that GPIOs
 * can be freely used, the chip->dev device must be registered before
 * the gpio framework's arch_initcall().  Otherwise sysfs initialization
 * for GPIOs will fail rudely.
 *
 * If chip->base is negative, this requests dynamic assignment of
 * a range of valid GPIOs.
 */
int gpiochip_add(struct gpio_chip *chip) // 在gpio_desc[]中分配空间,并链接chip结构;注册一个gpio_chip对应的gpio_desc到全局数组gpio描述符中
{
    unsigned long    flags;
    int        status = 0;
    unsigned    id;
    int        base = chip->base;

    if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
            && base >= 0) {
        status = -EINVAL;
        goto fail;
    }

    spin_lock_irqsave(&gpio_lock, flags);

    if (base < 0) {
        base = gpiochip_find_base(chip->ngpio);// 这个函数在gpiolib.c中,在gpio_desc[]中分配chip->ngpio个空间(从最后往前分配),返回第一个index
       if (base < 0) {
            status = base;
            goto unlock;
        }
        chip->base = base;
    }

    /* these GPIO numbers must not be managed by another gpio_chip */
    for (id = base; id < base + chip->ngpio; id++) {
        if (gpio_desc[id].chip != NULL) {
            status = -EBUSY;
            break;
        }
    }
    if (status == 0) {// 分配到空间,正常情况下
      for (id = base; id < base + chip->ngpio; id++) {
            gpio_desc[id].chip = chip;// 这里将gpio_desc与s3c_gpio_chip联系起来,他们的chip成员指向的是同一个数据结构 
/* REVISIT:  most hardware initializes GPIOs as
             * inputs (often with pullups enabled) so power
             * usage is minimized.  Linux code should set the
             * gpio direction first thing; but until it does,
             * we may expose the wrong direction in sysfs.
             */
            gpio_desc[id].flags = !chip->direction_input
                ? (1 << FLAG_IS_OUT)
                : 0;
        }
    }

unlock:
    spin_unlock_irqrestore(&gpio_lock, flags);
    if (status == 0)
        status = gpiochip_export(chip);
fail:
    /* failures here can mean systems won't boot... */
    if (status)
        pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
            chip->base, chip->base + chip->ngpio - 1,
            chip->label ? : "generic");
    return status;
}
EXPORT_SYMBOL_GPL(gpiochip_add);
void SPI_Master_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; __HAL_RCC_GPIOF_CLK_ENABLE(); /* SCK配置(推挽输出) */ GPIO_InitStruct.Pin = SPI_SCK_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(SPI_SCK_PORT, &GPIO_InitStruct); /* MOSI配置(推挽输出) */ GPIO_InitStruct.Pin = SPI_MOSI_PIN; HAL_GPIO_Init(SPI_MOSI_PORT, &GPIO_InitStruct); /* CS配置(推挽输出,默认高电平) */ GPIO_InitStruct.Pin = SPI_CS_PIN; HAL_GPIO_Init(SPI_CS_PORT, &GPIO_InitStruct); HAL_GPIO_WritePin(SPI_CS_PORT, SPI_CS_PIN, GPIO_PIN_SET); /* MISO配置(浮空输入) */ GPIO_InitStruct.Pin = SPI_MISO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(SPI_MISO_PORT, &GPIO_InitStruct); } //----------------------------------------------------------------------------- // Function: Soft_SPI_Transfer // Desc: 软件SPI全双工传输(主机模式) // 参数:tx_data - 待发送数据指针(NULL表示只接收) // rx_data - 接收缓冲区指针(NULL表示只发送) // size - 数据长度(字节数) // 注意:需根据SPI模式调整时钟边沿操作! //----------------------------------------------------------------------------- void Soft_SPI_Transfer(uint8_t *tx_data, uint8_t *rx_data, uint16_t size) { if (size == 0) return; /* 断言检查 */ assert_param(IS_GPIO_PIN(SPI_SCK_PIN)); assert_param(IS_GPIO_PIN(SPI_MOSI_PIN)); SPI_ENTER_CRITICAL(); // 进入临界区(防止中断干扰时序) /* 拉低CS启动传输 */ HAL_GPIO_WritePin(SPI_CS_PORT, SPI_CS_PIN, GPIO_PIN_RESET); for (uint16_t byte_cnt = 0; byte_cnt < size; byte_cnt++) { uint8_t tx_byte = tx_data ? tx_data[byte_cnt] : 0xFF; uint8_t rx_byte = 0; /* 按指定位序处理每个bit */ for (uint8_t bit_cnt = 0; bit_cnt < 8; bit_cnt++) { /* 设置MOSI输出 */ #if (SPI_BIT_ORDER == 0) // MSB First uint8_t bit_mask = 0x80 >> bit_cnt; #else // LSB First uint8_t bit_mask = 0x01 << bit_cnt; #endif HAL_GPIO_WritePin(SPI_MOSI_PORT, SPI_MOSI_PIN, (tx_byte & bit_mask) ? GPIO_PIN_SET : GPIO_PIN_RESET); /* 根据SPI模式生成时钟 */ #if (SPI_MODE == 0) // Mode 0: CPOL=0, CPHA=0 (采样在上升沿) HAL_GPIO_WritePin(SPI_SCK_PORT, SPI_SCK_PIN, GPIO_PIN_SET); HAL_delay_us(SPI_CLK_DELAY_US); // 读取MISO rx_byte <<= 1; if (HAL_GPIO_ReadPin(SPI_MISO_PORT, SPI_MISO_PIN)) { rx_byte |= 0x01; } HAL_GPIO_WritePin(SPI_SCK_PORT, SPI_SCK_PIN, GPIO_PIN_RESET); delay_us(SPI_CLK_DELAY_US); #else // 其他模式需补充对应时序 #error "Unsupported SPI Mode!" #endif } /* 存储接收数据 */ if (rx_data) rx_data[byte_cnt] = rx_byte; } /* 释放CS结束传输 */ HAL_GPIO_WritePin(SPI_CS_PORT, SPI_CS_PIN, GPIO_PIN_SET); SPI_EXIT_CRITICAL(); // 退出临界区 } 怎么改善这个代码传输效率
03-17
<think>好的,用户想了解Linux内核中gpio_export的用法和实现。首先,我需要回忆一下GPIO在内核中的基本概念。GPIO(通用输入输出)是嵌入式系统中常用的接口,用于控制硬件引脚。内核中的GPIO子系统提供了一系列API来管理这些引脚,包括申请、配置、导出等操作。 用户提到的gpio_export函数,应该是在内核中用于将GPIO引脚导出到用户空间的函数。根据引用[3],GPIO芯片被抽象为gpio_chip结构,每个Bank管理一组GPIO。导出GPIO后,用户空间可以通过sysfs接口访问和控制这些引脚。 接下来,我需要确认gpio_export的具体用法。通常在内核驱动中,开发者会先使用gpio_request申请GPIO,然后调用gpio_export将其导出。可能还需要设置方向和初始值。用户空间的访问路径一般是/sys/class/gpio/gpioX。需要检查是否正确处理了错误情况,比如GPIO已被占用或编号无效。 关于实现部分,gpio_export函数应该在drivers/gpio/gpiolib.c中定义。它主要创建sysfs条目,并设置相应的属性文件,比如direction和value。这里可能会用到内核的sysfs接口,比如device_create和sysfs_create_group。同时,需要确保导出的GPIO没有被其他驱动占用,这涉及到gpio_desc结构的状态管理。 还需要注意权限问题,导出后的GPIO在用户空间的访问可能需要root权限,或者通过udev规则设置合适的权限。另外,GPIO编号的管理,包括动态分配和静态声明,也是需要考虑的。例如,使用gpio_is_valid来检查编号有效性。 可能用户在使用时遇到问题,比如无法导出GPIO,这时候需要检查是否已经申请了该GPIO,或者是否有冲突。此外,导出后的方向设置和读写操作是否正确,也是常见的问题点。 最后,结合引用中的内容,特别是引用[3]提到的gpio_chip结构,可以说明GPIO子系统的层次结构,帮助用户理解gpio_export在整个框架中的位置和作用。需要确保解释清晰,并给出示例代码和注意事项,帮助用户正确使用该函数。</think>### Linux内核中`gpio_export`的用法与实现 #### 1. `gpio_export`的作用 `gpio_export`是Linux内核GPIO子系统提供的接口,用于将GPIO引脚导出到用户空间,允许通过sysfs文件系统(路径`/sys/class/gpio`)进行访问和控制[^3]。导出后,用户可通过读写文件(如`direction`、`value`)操作GPIO的输入/输出方向和电平。 --- #### 2. 使用方法 ##### 内核驱动中的调用流程: 1. **申请GPIO**:使用`gpio_request`分配GPIO资源。 2. **配置方向**:通过`gpio_direction_input`或`gpio_direction_output`设置方向。 3. **导出到用户空间**:调用`gpio_export`导出GPIO。 4. **可选标签**:使用`gpio_export_link`在sysfs中创建符号链接。 示例代码: ```c int gpio_num = 16; // GPIO编号 if (gpio_is_valid(gpio_num)) { gpio_request(gpio_num, "my_gpio"); gpio_direction_output(gpio_num, 0); // 初始输出低电平 gpio_export(gpio_num, false); // 第二个参数控制是否允许方向修改 } ``` --- #### 3. 实现分析 ##### 核心代码位置: - 函数定义:`drivers/gpio/gpiolib.c` - 关键步骤: 1. **有效性检查**:验证GPIO编号是否合法。 2. **分配`gpio_desc`结构**:管理GPIO状态和属性。 3. **创建sysfs接口**:通过`device_create`和`sysfs_create_group`生成`value`、`direction`等文件。 4. **权限设置**:默认权限为0644,用户可读写。 ##### 代码片段: ```c int gpio_export(unsigned gpio, bool direction_may_change) { struct gpio_desc *desc; if (!gpio_is_valid(gpio)) return -EINVAL; desc = gpio_to_desc(gpio); return gpiod_export(desc, direction_may_change); } ``` --- #### 4. 注意事项 - **GPIO编号有效性**:需通过`gpio_is_valid`检查。 - **资源冲突**:避免重复申请已占用的GPIO。 - **用户空间权限**:可能需要root权限或配置udev规则。 - **动态导出**:可通过`echo 16 > /sys/class/gpio/export`手动导出。 --- #### 5. 相关结构体 - `gpio_chip`:抽象GPIO控制器,包含操作函数(如`direction_input`)[^3]。 - `gpio_desc`:描述单个GPIO的状态(申请/释放、方向等)。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值