驱动:
#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/io.h>
#include"head.h"
int major;
char kbuf[128]={0};
gpio_t *vaddr;
gpio_t *vaddr2;
gpio_t *vaddr3;
gpio_t *vbee;
gpio_t *vfan;
unsigned int *vrcc;
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__);
unsigned long ret;
//向用户空间读取拷贝
if(size>sizeof(kbuf))//用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
size=sizeof(kbuf);
ret=copy_to_user(ubuf,kbuf,size);
if(ret)//拷贝失败
{
printk("copy_to_user filed\n");
return ret;
}
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
unsigned long ret;
//从用户空间读取数据
if(size>sizeof(kbuf))//用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
if(ret)//拷贝失败
{
printk("copy_to_user filed\n");
return ret;
}
switch(kbuf[0])
{
case '1':
if(kbuf[1]=='0')//关灯
{
vaddr->odr &= (~(1<<10));
}
else
{
vaddr->odr |= 1<<10;
}
break;
case '2':
if(kbuf[1]=='0')//关灯
{
vaddr2->odr &= (~(1<<10));
}
else
{
vaddr2->odr |= 1<<10;
}
break;
case '3':
if(kbuf[1]=='0')//关灯
{
vaddr3->odr &= (~(1<<8));
}
else
{
vaddr3->odr |= 1<<8;
}
break;
case '4':
if(kbuf[1]=='0')//关灯
{
vfan->odr &= (~(1<<9));
}
else
{
vfan->odr |= 1<<9;
}
break;
case '5':
if(kbuf[1]=='0')//关灯
{
vbee->odr &= (~(1<<6));
}
else
{
vbee->odr |= 1<<6;
}
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)
{
//字符设备驱动注册
major=register_chrdev(0,"mychrdev",&fops);
if(major<0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功:major=%d\n",major);
//寄存器地址的映射
//led1
vaddr=ioremap(GPIO_E,sizeof(gpio_t));
if(vaddr==NULL)
{
printk("ioremap filed:%d\n",__LINE__);
return -ENOMEM;
}
//led2
vaddr2=ioremap(GPIO_F,sizeof(gpio_t));
if(vaddr2==NULL)
{
printk("ioremap filed:%d\n",__LINE__);
return -ENOMEM;
}
//led3
vaddr3=ioremap(GPIO_E,sizeof(gpio_t));
if(vaddr3==NULL)
{
printk("ioremap filed:%d\n",__LINE__);
return -ENOMEM;
}
//风扇地址
vfan=ioremap(GPIO_E,sizeof(gpio_t));
if(vfan==NULL)
{
printk("ioremap filed:%d\n",__LINE__);
return -ENOMEM;
}
//蜂鸣器地址
vbee=ioremap(GPIO_B,sizeof(gpio_t));
if(vbee==NULL)
{
printk("ioremap filed:%d\n",__LINE__);
return -ENOMEM;
}
//RCC地址
vrcc=ioremap(PHY_RCC,4);
if(vaddr==NULL)
{
printk("ioremap filed:%d\n",__LINE__);
return -ENOMEM;
}
printk("物理地址映射成功\n");
//寄存器的初始化
//rcc
(*vrcc) |= (1<<4);
(*vrcc) |= (1<<5);
(*vrcc) |= (1<<1);
//moder
//led1
vaddr->moder &= (~(3<<20));
vaddr->moder |= (1<<20);
//odr输出低电平
vaddr->odr &= (~(1<<10));
//led2
vaddr2->moder &= (~(3<<20));
vaddr2->moder |= (1<<20);
//odr输出低电平
vaddr2->odr &= (~(1<<10));
//led3
vaddr3->moder &= (~(3<<16));
vaddr3->moder |= (1<<16);
//odr输出低电平
vaddr3->odr &= (~(1<<18));
//风扇
vfan->moder &= (~(3<<18));
vfan->moder |= (1<<18);
//odr输出低电平
vfan->odr &= (~(1<<9));
//蜂鸣器
vbee->moder &= (~(3<<12));
vbee->moder |= (1<<12);
//odr输出低电平
vbee->odr &= (~(1<<6));
return 0;
}
static void __exit mycdev_exit(void)
{
//取消地址映射
iounmap(vaddr);
iounmap(vaddr2);
iounmap(vaddr3);
iounmap(vfan);
iounmap(vbee);
iounmap(vrcc);
//注销字符设备驱动
unregister_chrdev(major,"mychrdev");
}
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 GPIO_B 0x50003000
#define GPIO_E 0x50006000
#define GPIO_F 0x50007000
#define PHY_RCC 0x50000A28
#define PHY_BEE ((gpio_t *)GPIO_B)
#define PHY_FAN ((gpio_t *)GPIO_E)
#define PHY_LED1 ((gpio_t *)GPIO_E)
#define PHY_LED2 ((gpio_t *)GPIO_F)
#define PHY_LED3 ((gpio_t *)GPIO_E)
#endif