小小的led驱动和测试程序

使用的板子为gec210的板子


#include <linux/kernel.h>

#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <mach/gpio.h>
#include <mach/regs-gpio.h>
#include <plat/gpio-cfg.h>
#include <linux/delay.h>

#include <linux/io.h>


#define LEDCON 0xe0200280  //led控制寄存器的物理地址
#define LEDDAT 0xe0200284  //led数据寄存器的物理地址


#define LED_MAGIC 'm'
#define LED_ON _IO(LED_MAGIC,0)
#define LED_OFF _IO(LED_MAGIC,1)


unsigned int *led_config;
unsigned int *led_data;




struct cdev cdev;
dev_t devno;


#define DRIVER_NAME "my_led_driver"
struct class *my_class;


static int led_open(struct inode *inode,struct file *file)
{
led_config=ioremap(LEDCON,4);
writel(0x00001111,led_config);
led_data=ioremap(LEDDAT,4);


return 0;
}


static int led_close(struct inode *inode,struct file *file)
{
iounmap(led_config);
iounmap(led_data);
return 0;
}


long led_ioctl (struct file *file, unsigned int cmd, unsigned long arg)


{
switch(cmd)
{
case LED_ON:
writel(0x00,led_data);
break;
case LED_OFF:
writel(0x0f,led_data);
break;
default:
return -EINVAL;
}
return 0;
}


static struct file_operations led_fops =
{
.owner = THIS_MODULE,
.open = led_open,
.release = led_close,
.unlocked_ioctl =led_ioctl,
};


static int led_init(void)
{
cdev_init(&cdev,&led_fops);


alloc_chrdev_region(&devno, 0 , 1 , "myled");
cdev_add(&cdev, devno, 1);
my_class = class_create(THIS_MODULE, "led_class");
if(IS_ERR(my_class))
{
printk("Err: failed in creating class.\n");
return -1;
}
device_create(my_class, NULL, devno,NULL,"led_driver");
return 0;
}


static void led_exit(void)
{


device_destroy(my_class,devno);
class_destroy(my_class);


cdev_del(&cdev);
unregister_chrdev_region(devno,1);


}


module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");

MODULE_AUTHOR("EIGHT&FIVE");


测试程序如下

#include<stdio.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>


#define LED_MAGIC 'm'
#define LED_ON _IO(LED_MAGIC,0)
#define LED_OFF _IO(LED_MAGIC,1)


int main(int argv,char **argc)
{
int fd,i;
fd=open("/dev/led_driver",0x666);


while(1)
{
scanf("%d",&i);


switch(i)
{
case 1 :
ioctl(fd,LED_ON,NULL);
break;


case 2:
ioctl(fd,LED_OFF,NULL);
break;

default:
break;
}


}
close(fd);


return 0;


}


测试已成功。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值