1.led驱动程序
led_drv.c程序:
//头文件---内核源码
#include
#include
#include
#include
#include
#include
#include
#include
#include
static unsigned int led_major = 200;
static unsigned int led_minor = 0;
static dev_t led_dev_num;
static struct class *led_class = NULL;
static struct device *gec6818_led_device = NULL;
static struct resource *gpioe_led_res = NULL;
static struct resource *gpioc_led_res = NULL;
//定义物理地址对应的虚拟地址的指针
static volatile unsigned int __iomem *GPIOE_BASE = NULL;
static volatile unsigned int __iomem *GPIOEOUT = NULL; //0x00
static volatile unsigned int __iomem *GPIOEOUTENB = NULL; //0x04
static volatile unsigned int __iomem *GPIOEALTFN0 = NULL; //0x20
static volatile unsigned int __iomem *GPIOC_BASE = NULL;
static volatile unsigned int __iomem *GPIOCOUT = NULL; //0x00
static volatile unsigned int __iomem *GPIOCOUTENB = NULL; //0x04
static volatile unsigned int __iomem *GPIOCALTFN0 = NULL; //0x20
static volatile unsigned int __iomem *GPIOCALTFN1 = NULL; //0x24
//2.创建一个cdev
static struct cdev gec6818_led_dev;
static int gec6818_led_open(struct inode *inode, struct file *filp)
{
printk(KERN_WARNING "led drv openning\n");
return 0;
}
//是给应用程序的write()做系统调用的接口,用来接收应用程序写下来的数据
//struct file *filp ---- 指向驱动文件的指针
//const char __user *buf---应用程序写下来的数据存放的位置
//size_t len ---- 应用程序写下来数据的字节数
//loff_t *off ---文件指针的偏移量
static ssize_t gec6818_led_write(struct file *filp, const char __user *buf,
size_t len, loff_t *off)
{
int ret;
char led_flag[2];
if(len != 2)
return -EINVAL;
ret = copy_from_user(led_flag, buf,len);
if(ret != 0)
return -EFAULT;
printk(KERN_WARNING "led_flag[1]=%d,led_flag[0]=%d\n",
led_flag[1],led_flag[0]);
//用led_flag[2]配置寄存器
switch(led_flag[1]){
case 7 : //D7
if(led_flag[0] == 1) //D7 亮
*GPIOEOUT &= ~(1 |