驱动 led亮灭

led.c

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

int main(int argc,char const *argv)
{
    char buf[128] = {0};
    int fd = open("/dev/mychrdev",O_RDWR);
    if(fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }

    while(1)
    {
        printf("请输入选择哪一个灯泡 1 2 3\n");
        fgets(buf,sizeof(buf),stdin);
        printf("请输入亮灭 0 灭 1 亮\n");
        fgets(buf+1,sizeof(buf),stdin);
        buf[strlen(buf)-1]='\0';
        write(fd,buf,sizeof(buf));
    }
    close(fd);
    return 0;
}

mychrdev.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include "led.h"

unsigned int major;
char kbuf[128] = {0};
unsigned int *vir_led1_moder;
unsigned int *vir_led1_odr;
unsigned int *vir_rcc;

unsigned int *vir_led2_moder;
unsigned int *vir_led2_odr;

unsigned int *vir_led3_moder;
unsigned int *vir_led3_odr;

int mycdev_open(struct inode *inode,struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

ssize_t mycdev_read(struct file *file,char *ubuf,size_t size,loff_t *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    long ret;
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy_to_user failed\n");
        return -EIO;
    }
    return 0;
}


ssize_t mycdev_write(struct file *file,const char *ubuf,size_t size,loff_t *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    long ret;
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy_from_user failed\n");
        return -EIO;
    }
    switch(kbuf[0])
    {
        case '1':
            if(kbuf[1] == '0')
            {
                (*vir_led1_odr) &= (~(0x1 << 10));
            }
            else if(kbuf[1] == '1')
            {
                (*vir_led1_odr) |= (0x1 << 10);
            }
            break;
        case '2':
            if(kbuf[1] == '0')
            {
                (*vir_led2_odr) &= (~(0x1 << 10));
            }
            else if(kbuf[1] == '1')
            {
                (*vir_led2_odr) |= (0x1 << 10);
            }
            break;
        case '3':
            if(kbuf[1] == '0')
            {
                (*vir_led1_odr) &= (~(0x1 << 8));
            }
            else if(kbuf[1] == '1')
            {
                (*vir_led1_odr) |= (0x1 << 8);
            }
            break;
    }
    return 0;
}


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 mycdev_init(void)
{
    printk(KERN_ERR "入口函数\n");
    major=register_chrdev(0,"mychrdev",&fops);
    if(major<0)
    {
        printk("注册字符设备驱动失败\n");
        return major;
    }
    printk("字符设备注册成功\n");

    //地址映射
    vir_led1_moder=ioremap(PHY_LED1_MODER,4);
    if(vir_led1_moder==NULL)
    {
        printk("映射物理地址失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_led2_moder=ioremap(PHY_LED2_MODER,4);
    if(vir_led2_moder==NULL)
    {
        printk("映射物理地址失败%d\n",__LINE__);
        return -EFAULT;
    }

    vir_led1_odr=ioremap(PHY_LED1_ODR,4);
    if(vir_led1_odr==NULL)
    {
        printk("映射物理地址失败%d\n",__LINE__);
         return -EFAULT;
    }

    vir_led2_odr=ioremap(PHY_LED2_ODR,4);
    if(vir_led2_odr==NULL)
    {
        printk("映射物理地址失败%d\n",__LINE__);
         return -EFAULT;
    }

    vir_led3_moder = vir_led1_moder;
    vir_led3_odr = vir_led1_odr;

    vir_rcc=ioremap(PHY_RCC,4);
    if(vir_rcc==NULL)
    {
        printk("映射物理地址失败%d\n",__LINE__);
         return -EFAULT;
    }

    //硬件初始化
    (*vir_rcc)|=(0x3 << 4);
    (*vir_led1_moder) &= (~(0x3 << 20));
    (*vir_led1_moder) |= (0x1 << 20);

    (*vir_led3_moder) &= (~(0x3 << 16));
    (*vir_led3_moder) |= (0x1 << 16);

    (*vir_led2_moder) &= (~(0x3 << 20));
    (*vir_led2_moder) |= (0x1 << 20);

    (*vir_led1_odr) &= (~(0x1 << 10));
    (*vir_led3_odr) &= (~(0x1 << 8));
    (*vir_led2_odr) &= (~(0x1 << 10));
    printk("映射物理地址成功");
    return 0;//返回值一定要加,除非函数类型是void
}   

//出口函数,主要进行资源的释放和注销
static void __exit mycdev_exit(void)
{
    printk("出口函数\n");
    unregister_chrdev(major,"mychrdev");
    //__exit          __section(".exit.text")
    //用于声明mycdev_exit在.exit.text段
}
//用于向内核报备当前内核模块入口函数的地址
module_init(mycdev_init);
//用于向内核报备当前内核模块出口函数的地址            
module_exit(mycdev_exit);
//声明当前内核模块遵循GPL协议
MODULE_LICENSE("GPL");

led.h

#ifndef __HEAD_H__
#define __HEAD_H__

#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_RCC 0x50000A28

#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014

#define PHY_LED3_MODER 0x50006000
#define PHY_LED3_ODR 0x50006014

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值