您尚未登录。

楼主 #1 2019-11-16 23:04:37

xd717
会员
注册时间: 2019-10-23
已发帖子: 51
积分: 51

驱动学习

用的orangepi 学习编写gpio驱动
https://www.cnblogs.com/Sinlinx/p/10382203.html
照这个只配了PIO CONF 和PIO DAT
但似乎不行 想请问大佬有了解的吗?还需要配置别的寄存器吗?
另外驱动程序中的printk信息无法打印是什么原因?

救救孩子T.T

离线

楼主 #2 2019-11-16 23:51:11

xd717
会员
注册时间: 2019-10-23
已发帖子: 51
积分: 51

Re: 驱动学习

A64的

离线

楼主 #3 2019-11-18 14:23:44

xd717
会员
注册时间: 2019-10-23
已发帖子: 51
积分: 51

Re: 驱动学习

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define LED_MAJOR 200 / 主设备号 /
#define LED_NAME "led" / 设备名字 /

#define LEDOFF 0 / 关灯 /
#define LEDON 1 / 开灯 /
#define GPIO1_CONF_BASE (0X01C20824)
#define GPIO1_DAT_BASE (0X01C20834))

static void __iomem *GPIO1_CONF;
static void __iomem *GPIO1_DAT;
/*

  • @description : LED打开/关闭

  • @param - sta : LEDON(0) 打开LED,LEDOFF(1) 关闭LED

  • @return : 无
    */
    void led_switch(u8 sta)
    {
    u32 val = 0;
    if(sta == LEDON) {
    val = readl(GPIO1_DAT);
    val &= ~(1 << 9);
    writel(val, GPIO1_DAT);
    }else if(sta == LEDOFF) {
    val = readl(GPIO1_DAT);
    val|= (1 << 9);
    writel(val, GPIO1_DAT);
    }
    }

/*

  • @description : 打开设备

  • @param - inode : 传递给驱动的inode

  • @param - filp : 设备文件,file结构体有个叫做private_data的成员变量

  • 一般在open的时候将private_data指向设备结构体。

  • @return : 0 成功;其他 失败
    */
    static int led_open(struct inode inode, struct file filp)
    {
    return 0;
    }

/*

  • @description : 从设备读取数据

  • @param - filp : 要打开的设备文件(文件描述符)

  • @param - buf : 返回给用户空间的数据缓冲区

  • @param - cnt : 要读取的数据长度

  • @param - offt : 相对于文件首地址的偏移

  • @return : 读取的字节数,如果为负值,表示读取失败
    */
    static ssize_t led_read(struct file filp, char __user buf, size_t cnt, loff_t *offt)
    {
    return 0;
    }

/*

  • @description : 向设备写数据

  • @param - filp : 设备文件,表示打开的文件描述符

  • @param - buf : 要写给设备写入的数据

  • @param - cnt : 要写入的数据长度

  • @param - offt : 相对于文件首地址的偏移

  • @return : 写入的字节数,如果为负值,表示写入失败
    */
    static ssize_t led_write(struct file filp, const char __user buf, size_t cnt, loff_t *offt)
    {
    int retvalue;
    unsigned char databuf[1];
    unsigned char ledstat;

retvalue = copy_from_user(databuf, buf, cnt);
if(retvalue < 0) {
printk("kernel write failed!\r\n");
return -EFAULT;
}

ledstat = databuf[0]; / 获取状态值 /

if(ledstat == LEDON) {
led_switch(LEDON); / 打开LED灯 /
} else if(ledstat == LEDOFF) {
led_switch(LEDOFF); / 关闭LED灯 /
}
return 0;
}

/*

  • @description : 关闭/释放设备

  • @param - filp : 要关闭的设备文件(文件描述符)

  • @return : 0 成功;其他 失败
    */
    static int led_release(struct inode inode, struct file filp)
    {
    return 0;
    }

/ 设备操作函数 /
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.read = led_read,
.write = led_write,
.release = led_release,
};

/*

  • @description : 驱动出口函数

  • @param : 无

  • @return : 无
    */
    static int __init led_init(void)
    {
    int retvalue = 0;
    u32 val = 0;

GPIO1_CONF = ioremap(GPIO1_CONF_BASE, 4);
GPIO1_DAT = ioremap(GPIO1_DAT_BASE, 4);

/ 4、设置GPIO1_IO03为输出功能 /
val = readl(GPIO1_CONF);
val &= ~(7<< 28); / 清除以前的设置 /
val |= (1<< 28); / 设置为输出 /
writel(val, GPIO1_CONF);

/ 5、默认关闭LED /
val = readl(GPIO1_DAT);
val |= (1 << 9);
writel(val, GPIO1_DAT);

/ 6、注册字符设备驱动 /
retvalue = register_chrdev(LED_MAJOR, LED_NAME, &led_fops);
if(retvalue < 0){
printk("register chrdev failed!\r\n");
return -EIO;
}
return 0;
}

/*

  • @description : 驱动出口函数

  • @param : 无

  • @return : 无
    */
    static void __exit led_exit(void)
    {
    iounmap(GPIO1_CONF);
    iounmap(GPIO1_DAT);

/ 注销字符设备驱动 /
unregister_chrdev(LED_MAJOR, LED_NAME);
}

module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");

套用的这个模板可以吗 请大佬指点指点

离线

#4 2019-11-18 14:28:12

jimmy
会员
注册时间: 2017-10-29
已发帖子: 316
积分: 315

Re: 驱动学习

dmesg也不显示 printk 的log?

离线

楼主 #5 2019-11-18 14:40:48

xd717
会员
注册时间: 2019-10-23
已发帖子: 51
积分: 51

Re: 驱动学习

jimmy wrote:

dmesg也不显示 printk 的log?

是的。。什么原因呢

离线

#6 2019-11-18 14:43:11

jimmy
会员
注册时间: 2017-10-29
已发帖子: 316
积分: 315

Re: 驱动学习

printk输出error级别看你

离线

楼主 #7 2019-11-18 16:24:16

xd717
会员
注册时间: 2019-10-23
已发帖子: 51
积分: 51

Re: 驱动学习

jimmy wrote:

printk输出error级别看你

刚没注意,dmesg只有一条这个
[ 136.546775] led_gpio: loading out-of-tree module taints kernel.

printk级别
root@orangepiwin:/lib/modules# cat /proc/sys/kernel/printk
3 4 1 3

离线

楼主 #8 2019-11-18 16:27:11

xd717
会员
注册时间: 2019-10-23
已发帖子: 51
积分: 51

Re: 驱动学习

jimmy wrote:

printk输出error级别看你

顺便再请教下您 我A64的板子想用GPIO 输出 只要配置conf 和data就可以了吗 还有其它相关配置吗

离线

#9 2019-11-18 17:32:41

jimmy
会员
注册时间: 2017-10-29
已发帖子: 316
积分: 315

Re: 驱动学习

可以在应用层控制, 搜一下 /sys/class/gpio

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn


东莞哇酷科技有限公司开发