内核驱动开发-通过在设备树文件中添加自己的设备树节点来驱动LED工作

本文详细介绍了如何在Linux内核驱动开发中,通过在设备树文件中添加自定义的设备节点,实现对LED硬件的工作控制。内容涵盖了所需的头文件引用、应用层文件的修改以及关键的内核层代码实现。
摘要由CSDN通过智能技术生成

头文件

#ifndef __HEAD__H__ 
#define __HEAD__H__ 

//宏定义功能码
#define LED1_ON _IO('l',1)
#define LED1_OFF _IO('l',0)
#define LED2_ON _IO('l',3)
#define LED2_OFF _IO('l',2)
#define LED3_ON _IO('l',5)
#define LED3_OFF _IO('l',4)

#endif

应用层文件

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"


int main(int argc, char const *argv[])
{
   
    char buf[128]={
   0};
    int op;
    int fd = open("/dev/myled1",O_RDWR);
    if(fd <0 )
    {
   
        perror("文件打开失败\n");
        return -1;
    }
    while (1)
    {
   
        printf("请输入: 0 (led1关灯) 1(led1开灯) 2 (led2关灯) 3 (led2开灯) 4 (led3关灯) 5 (led3开灯) ->");
        scanf("%d",&op);
        while(getchar()!='\n');
        switch (op)
        {
   
        case 0:
            ioctl(fd,LED1_OFF);
            break;
        case 1:
            ioctl(fd,LED1_ON);
            break;
        case 2:
            ioctl(fd,LED2_OFF);
            break;
        case 3:
            ioctl(fd,LED2_ON);
            break;
        case 4:
            ioctl(fd,LED3_OFF);
            break;
        case 5:
            ioctl(fd,LED3_ON);
            break;
        default:
            break;
        }
    }
    close(fd);
    return 0;
}

内核层代码

#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include<linux/device.h>
#include<linux/poll.h>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值