09_gpiochipLinux内核模块

这篇文章介绍了在Linux环境下如何编写一个基本的内核模块,实现了GPIO芯片的功能,包括请求、方向控制和数据获取/设置。模块初始化和退出函数展示了模块的生命周期管理。
摘要由CSDN通过智能技术生成

01_basicLinux内核模块-CSDN博客文章浏览阅读318次,点赞3次,收藏3次。环境ID=ubuntuMakefilemodules:clean:basic.creturn 0;运行效果。https://blog.csdn.net/m0_37132481/article/details/136157384my_gpiochip.c

#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/gpio/driver.h>

#define TAG "HELLO# "


static int my_gpiochip_gpio_request(struct gpio_chip *chip, unsigned int offset)
{
        printk(TAG "%s called\n", __func__);
        return 0;
}
static int my_gpiochip_gpio_direction_in(struct gpio_chip *chip, unsigned int offset)
{
        printk(TAG "%s called\n", __func__);
        return 0;
}
static int my_gpiochip_gpio_get(struct gpio_chip *chip, unsigned int offset)
{
        printk(TAG "%s called\n", __func__);
        return 0;
}
static int my_gpiochip_gpio_direction_out(struct gpio_chip *chip, unsigned offset, int value)
{
        printk(TAG "%s called\n", __func__);
        return 0;
}
static void my_gpiochip_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
{
        printk(TAG "%s called\n", __func__);
}

static struct gpio_chip my_gpiochip_gpio_chip = {
        .label                  = "my_gpiochip",
        .owner                  = THIS_MODULE,
        .request                = my_gpiochip_gpio_request,
        .direction_input        = my_gpiochip_gpio_direction_in,
        .get                    = my_gpiochip_gpio_get,
        .direction_output       = my_gpiochip_gpio_direction_out,
        .set                    = my_gpiochip_gpio_set,
        .can_sleep              = 1,
        .ngpio                  = 100,
        .base                   = -1,
};

static int my_gpiochip_init(void)
{
        printk(TAG "%s called\n", __func__);

        gpiochip_add(&my_gpiochip_gpio_chip);

        return 0;
}
static void my_gpiochip_exit(void)
{
        gpiochip_remove(&my_gpiochip_gpio_chip);

        printk(TAG "%s called\n", __func__);
}

module_init(my_gpiochip_init);
module_exit(my_gpiochip_exit);
MODULE_LICENSE("GPL");

效果

 gpio chip注册后,base由内核确定为512

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值