下载驱动实现led点亮

led.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include"led.h"
#define CNAME  "mycdev"

int major;//定义变量接收主设备号
char kbuf[128]={};//定义数组用于存放和用户之间拷贝的数据
unsigned int *vir_moder;
unsigned int *vir_moder2;
unsigned int *vir_moder3;

unsigned int *vir_odr;
unsigned int *vir_odr2;
unsigned int *vir_odr3;

unsigned int *vir_rcc;
unsigned int *vir_rcc2;
unsigned int *vir_rcc3;

//对应的是open()
int mycdev_open(struct inode *inode, struct file *file)
{
      printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
      return 0;
}
//read()
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    //size参数是用户期待读到的字节长度
    int ret;
    if(size>sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
     
       return size;
}
//write()
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if(size>sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
    if(kbuf[0]=='1')//开灯
    {
        (*vir_odr) |= (1<<10);
        (*vir_odr2) |= (1<<10);
        (*vir_odr3) |= (1<<8);
    }
    else//关灯
    {   (*vir_odr) &= ~(1<<10);
        (*vir_odr2) &= ~(1<<10);
        (*vir_odr3) &= ~(1<<8);
       
    }
     if(kbuf[0]=='2')//开灯
    {       
        (*vir_odr2) |= (1<<10);
    }
    else//关灯
    {   
        (*vir_odr2) &= ~(1<<10);
       
    }
     if(kbuf[0]=='3')//开灯
    {
        (*vir_odr3) |= (1<<8);
    }
    else//关灯
    {   
        (*vir_odr3) &= ~(1<<8);      
    }
    return size;
}
//close()
int mycdev_close(struct inode *inode, struct file *file)
{
      printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
       return 0;
}
//操作方法结构体的初始化
struct file_operations fops=
{
    .open=mycdev_open,
    .read=mycdev_read,
    .write=mycdev_write,
    .release=mycdev_close,
};

//入口函数,当驱动安装的时候执行
static int __init demo_init(void)
{
    
    //动态注册字符设备驱动
    major=register_chrdev(0,CNAME,&fops);
    if(major<0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n",major);
    //进行物理地址的映射
    vir_moder=ioremap(PHY_LED1_MODER,4);
    if(vir_moder==NULL)
    {
        printk("vir_moder 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_moder 映射成功\n");
    vir_moder2=ioremap(PHY_LED2_MODER,4);
    if(vir_moder2==NULL)
    {
        printk("vir_moder 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_moder 映射成功\n");
    vir_moder3=ioremap(PHY_LED3_MODER,4);
    if(vir_moder3==NULL)
    {
        printk("vir_moder 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_moder 映射成功\n");

    vir_odr=ioremap(PHY_LED1_ODR,4);
    if(vir_odr==NULL)
    {
        printk("vir_odr 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_odr 映射成功\n");
    vir_odr2=ioremap(PHY_LED2_ODR,4);
    if(vir_odr2==NULL)
    {
        printk("vir_odr 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_odr 映射成功\n");
    vir_odr3=ioremap(PHY_LED3_ODR,4);
    if(vir_odr3==NULL)
    {
        printk("vir_odr 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_odr 映射成功\n");

    vir_rcc=ioremap(PHY_LED1_RCC,4);
    if(vir_rcc==NULL)
    {
        printk("vir_rcc 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_rcc 映射成功\n");
    vir_rcc2=ioremap(PHY_LED2_RCC,4);
    if(vir_rcc2==NULL)
    {
        printk("vir_rcc 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_rcc 映射成功\n");
    vir_rcc3=ioremap(PHY_LED3_RCC,4);
    if(vir_rcc3==NULL)
    {
        printk("vir_rcc 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_rcc 映射成功\n");

    //寄存器的初始化
    (*vir_moder) &= ~(3<<20);
    (*vir_moder) |= (1<<20);
    (*vir_odr) &= ~(1<<10);
    (*vir_rcc) |= (1<<4);

    (*vir_moder2) &= ~(3<<20);
    (*vir_moder2) |= (1<<20);
    (*vir_odr2) &= ~(1<<10);
    (*vir_rcc2) |= (1<<5);

    (*vir_moder3) &= ~(3<<16);
    (*vir_moder3) |= (1<<16);
    (*vir_odr3) &= ~(1<<8);
    (*vir_rcc3) |= (1<<4);
    return 0;
}

//出口函数,卸载驱动的时候执行
static void __exit demo_exit(void)
{
    //注销字符设备驱动
    unregister_chrdev(major,CNAME);
   
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");

led.h

#ifndef __MYLED_H__
#define __MYLED_H__

#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR   0X50006014
#define PHY_LED1_RCC   0X50000A28 

#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR   0X50007014
#define PHY_LED2_RCC   0X50000A28 

#define PHY_LED3_MODER 0X50006000
#define PHY_LED3_ODR   0X50006014
#define PHY_LED3_RCC   0X50000A28 
#endif

test.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>

int main(int argc, char const *argv[])
{
    char buf[128]={};
    int fd=open("/dev/mychrdev",O_RDWR);
    if(fd<0)
    {

        printf("打开设备文件失败\n");
        exit(-1);
    }
    printf("设备文件打开成功\n");
    //在终端输入
    printf("请输入控制命令 1,2,3:开灯 0:关灯\n");
    fgets(buf,sizeof(buf),stdin);
    //吃掉换行符
    buf[strlen(buf)-1]='\0';
    write(fd,buf,sizeof(buf));
    //清空buf
    memset(buf,0,sizeof(buf));
    read(fd,buf,sizeof(buf));
    printf("读取到的数据为%s\n",buf);
    close(fd);
    return 0;
}
下面是一个简单的字符设备驱动程序的示例,它可以创建一个名为“mychrdev”的字符设备节点,并允许应用程序读取和写入数据: ``` #include <linux/module.h> #include <linux/fs.h> #include <linux/uaccess.h> #define DEVICE_NAME "mychrdev" #define BUF_LEN 80 static int Major; static char msg[BUF_LEN]; static char *msg_ptr; /* Function called when the device is opened */ static int device_open(struct inode *inode, struct file *file) { msg_ptr = msg; return 0; } /* Function called when the device is closed */ static int device_release(struct inode *inode, struct file *file) { return 0; } /* Function called when data is read from the device */ static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t *offset) { int bytes_read = 0; while (length && *msg_ptr) { put_user(*(msg_ptr++), buffer++); length--; bytes_read++; } return bytes_read; } /* Function called when data is written to the device */ static ssize_t device_write(struct file *filp, const char *buffer, size_t length, loff_t *offset) { int bytes_written = 0; while (length && bytes_written < BUF_LEN) { get_user(msg[bytes_written], buffer++); length--; bytes_written++; } return bytes_written; } /* Define file operations structure */ static struct file_operations fops = { .read = device_read, .write = device_write, .open = device_open, .release = device_release }; /* Function called when the module is loaded */ static int __init mychrdev_init(void) { Major = register_chrdev(0, DEVICE_NAME, &fops); if (Major < 0) { printk(KERN_ALERT "Registering char device failed with %d\n", Major); return Major; } printk(KERN_INFO "Registered char device with major number %d\n", Major); return 0; } /* Function called when the module is unloaded */ static void __exit mychrdev_exit(void) { unregister_chrdev(Major, DEVICE_NAME); printk(KERN_INFO "Unregistered char device\n"); } module_init(mychrdev_init); module_exit(mychrdev_exit); ``` 在此示例中,我们首先定义了一个设备名称和缓冲区长度。然后定义了设备打开、关闭、读取和写入时需要调用的函数。接下来定义了一个结构体,其中包含了文件操作指针,然后在模块加载时通过 `register_chrdev()` 函数注册该字符设备。 在 `device_read()` 函数中,我们从设备缓冲区中读取数据并将其写入用户空间缓冲区。在 `device_write()` 函数中,我们从用户空间缓冲区中读取数据并将其写入设备缓冲区。在 `device_open()` 函数中,我们初始化缓冲区指针。最后,在模块卸载时,我们通过 `unregister_chrdev()` 函数注销字符设备。 请注意,这只是一个简单的示例程序,实际的设备驱动程序可能需要更多的代码来处理设备的初始化、错误检查、数据校验等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值