阻塞io读取内核驱动变量值

阻塞io读取内核驱动变量值_第1张图片

 应用程序:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"

int main(int argc, char const *argv[])
{
    int a, b;
    char buf[128] = {0};

    int fd_led1 = open("/dev/led0", O_RDWR); //对应key1按键
    if (fd_led1 < 0)
    {
        printf("打开LED1设备文件失败\n");
        exit(-1);
    }

    while (1)
    {
        // 从终端读取
        printf("请选择KEY1灯功能\n");
        printf("0(关) 1(开)>");
        scanf("%d", &a);
        printf("请输入要控制的按键\n");
        printf("1(KEY1) 2(KEY2) 3(KEY3)>");
        scanf("%d", &b);
        if (a == 1) // 开灯
        {
			switch (b)
			{
			case 1:
				while(1)
				{
           	    //ioctl(fd_led1, LED_ON, b);
           	    //ioctl(fd_led1, LED_ON, b);
				//memset(buf,0,sizeof(buf));//清空
       			read(fd_led1,buf,sizeof(buf));//读取数据
        		printf("number:%c\n",buf[0]);
				sleep(1);
				}
				break;
			}
        }
        else if (a == 0) // 关灯
        {			
			switch (b)
			{
			case 1:
           	    //ioctl(fd_led1, LED_OFF, b);
           	    //ioctl(fd_led1, LED_ON, b);
				break;
			case 2:
           	    //ioctl(fd_led2, LED_OFF, b);
				break;
			case 3:
           	    //ioctl(fd_led3, LED_OFF, b);
				break;
			}
        }
    }
    close(fd_led1);
//    close(fd_led2);
//    close(fd_led3);
    return 0;
}

驱动程序

#include 
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "head.h"

struct cdev *cdev; 		//字符设备空间首地址
unsigned int major=500; //静态申请设备号
unsigned int minor=0;//次设备号的起始值
dev_t devno; 		//动态申请设备号
struct class *cls;  //接收注册结构体的地址
struct device *dev; //设备号
int irqno;     //中断
struct gpio_desc *gpiono; 	//gpio
struct device_node *dnode;
char number = 0; 	  //定义number
char kbuf[128] = {0};  //驱动内的缓冲区

int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
// 中断处理函数
irqreturn_t myirq_handler(int irqno, void *dev_id)
{
	printk("key1 interrupt\n");
	//灯状态取反
	gpiod_set_value(gpiono, !gpiod_get_value(gpiono));
	//将number的数据发送给kbuf缓冲区
	number = !number;
    return IRQ_HANDLED;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    int ret;
    if(sizeof(kbuf)0)//静态申请设备号
    {
        ret=register_chrdev_region(MKDEV(major,minor),1,"led0"); //设备号需要是组合出来的,次设备数量,设备文件名
        if(ret)
        {
            printk("静态指定设备号失败\n");
            goto out2;
        }
    }
    else
    {
        ret=alloc_chrdev_region(&devno,minor,1,"led0");   //动态申请设备号,次设备号,设备数量,文件名
         if(ret)
        {
            printk("动态申请设备号失败\n");
            goto out2;
        }
        major=MAJOR(devno); 	//根据设备号得到主设备号
        minor=MINOR(devno); 	//根据设备号得到次设备号
    }
    printk("申请设备号成功\n");
    //4.注册字符设备驱动对象  cdev_add()
    ret=cdev_add(cdev,MKDEV(major,minor),1); //字符设备,设备号,设备数量
    if(ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto out3;
    }
    printk("注册字符设备驱动对象成功\n");
    //5.向上提交目录
    cls=class_create(THIS_MODULE,"led0"); //指向自身的指针,文件名
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret=-PTR_ERR(cls);
        goto out4;
    }
    printk("向上提交目录成功\n");
    //6.向上提交设备节点
    dev=device_create(cls,NULL,MKDEV(major,0),NULL,"led0"); //创建设备节点
    if(IS_ERR(dev))
    {
        printk("向上提交节点信息失败\n");
        ret=-PTR_ERR(dev);
        goto out5;
    }
    printk("向上提交设备节点信息成功\n");

	//gpio子系统
    // 1、解析设备树节点
   dnode=of_find_node_by_name(NULL,"myleds");
   if(dnode==NULL)
   {
    printk("解析设备树节点失败\n");
    return -ENOMEM;
   }
   printk("解析设备树节点成功\n");
   // 2、根据设备树节点解析出gpio编号并申请对应的led
    gpiono=gpiod_get_from_of_node(dnode,"led1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("解析设备号失败\n");
        return -PTR_ERR(gpiono);
    }
	printk("申请gpio编号成功\n");
	// 3、开灯
	gpiod_set_value(gpiono,1);
	
	//软中断
	//解析设备树节点
    dnode=of_find_node_by_name(NULL,"mykeys");
    if(dnode==NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    printk("设备树节点解析成功\n");

    // 获取软中断号
    irqno = irq_of_parse_and_map(dnode, 0);
	if (!irqno)
    {
        printk("软中断号获取失败\n");
		return -ENOMEM;
    }
    printk("软中断号获取成功irqno=%d\n", irqno);
    // 注册中断
    ret1 = request_irq(irqno, myirq_handler, IRQF_TRIGGER_FALLING, "key", NULL);
    if (ret1)
	{
        printk("注册驱动失败\n");
		return ret1;
    }
    printk("key1中断注册成功\n");  
    return 0;

out5:
    //销毁上面提交的设备信息
    device_destroy(cls,MKDEV(major,0));
    class_destroy(cls);
out4:
    cdev_del(cdev);
out3:
    unregister_chrdev_region(MKDEV(major,minor),1);
out2:
    kfree(cdev);
out1:
    return ret;

}
static void __exit mycdev_exit(void)
{
	//gpio子系统    
	// 4、灭灯
    gpiod_set_value(gpiono,0);
    // 5、释放gpio编号
    gpiod_put(gpiono);


    // 注销中断
    	free_irq(irqno,NULL);
    //1.销毁设备信息  device_destroy
    device_destroy(cls,MKDEV(major,0));
    //2.销毁目录  class_destroy
    class_destroy(cls);
    //3.注销对象  cdev_del()
    cdev_del(cdev);
    //4.释放设备号   unregister_chrdev_region()
    unregister_chrdev_region(MKDEV(major,minor),1);
    //5.释放对象空间  kfree()
    kfree(cdev);
	// 字符设备驱动的注销
    unregister_chrdev(major, "led0");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

头文件

#ifndef __HEAD_H__
#define __HEAD_H__ 
typedef struct{
    unsigned int MODER;
    unsigned int OTYPER;
    unsigned int OSPEEDR;
    unsigned int PUPDR;
    unsigned int IDR;
    unsigned int ODR;
}gpio_t;
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR  0X50000A28

#define LED_ON _IOW('l',1,int) //开灯
#define LED_OFF _IOW('l',0,int)//关灯

#endif 

你可能感兴趣的:(c语言,开发语言)