设为首页 加入收藏

TOP

ARM+Linux驱动----点亮开发板的LED(二)
2014-11-24 07:54:03 来源: 作者: 【 】 浏览:1
Tags:ARM Linux 驱动 ----点亮 开发 LED
cmd , unsigned long arg )
{
struct light_dev * dev = filp-> private_data;


switch(_cmd){
case 1:
dev->value = 1; //just a flag
light_on();
break;
case 0:
dev->value = 0; //just a flag
light_off();
break;
default: return -ENOTTY; //CMD CAN NOT SUPPORT
}
return 0;
}


static struct file_operations light_fops = {
//fill the file_operations struct
.owner = THIS_MODULE,
.read = light_read,
.write = light_write,
.ioctl = light_ioctl,
.open = light_open,
.release = light_release,


};


static void light_setup_cdev(struct light_dev * dev , int index)
{
dev_t devno = MKDEV(light_major , index); //make a device number and save it in a 32-intergred
int err = 0;
cdev_init(&dev->cdev , &light_fops ); //only fill the struct cdev
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &light_fops; // Re


if(( err = cdev_add(&dev->cdev , devno , 1) ) == 1){ // some key !!!!!!!!!!!!!!!!
printk(KERN_NOTICE " Error adding LED ");
}


}



int light_init(void){
int result = 0;
dev_t devno = 0;
devno = MKDEV(light_major,0);


if(light_major){


if(( result = register_chrdev_region(devno,1,"LED")) < 0){
printk(KERN_NOTICE "add a register item failed");
printk(DEVICE_NAME "Allocate the device nomber failed!~");
return result;
}


}else{


if( ( result = alloc_chrdev_region(&devno,0,1,"LED") ) < 0 ){
printk(KERN_NOTICE "add a register item failed");
printk(DEVICE_NAME "Allocate the device number failed!~");
return result;
}
light_major = MAJOR(devno); // fill the majot_device_number to var light_major


}


//allocate zhe spcae to device struct
if( ( light_devp = kmalloc(sizeof(struct light_dev),GFP_KERNEL)) <0 ){
result = -ENOMEM;
printk(KERN_NOTICE "kmalloc failed!~");
goto failmalloc;
}


//Init the memory
memset(light_devp,0,sizeof(struct light_dev));


light_setup_cdev(light_devp,0);
light_GPIO_init();



s3c2410_gpio_setpin(S3C2410_GPF4 , 0);
s3c2410_gpio_setpin(S3C2410_GPF5 , 1);
s3c2410_gpio_setpin(S3C2410_GPF6 , 0);
s3c2410_gpio_setpin(S3C2410_GPF7 , 1);



//all successful~
return 0;


failmalloc: unregister_chrdev_region(devno,1);
printk(DEVICE_NAME "unregister~");
return result;
}



void light_cleanup(void){
cdev_del(&light_devp->cdev); //add the &light_devp->cdev to system as well,~Remember add the device point and del the device point
kfree(light_devp);
unregister_chrdev_region(MKDEV(light_major,0),1);
}


module_init(light_init);
module_exit(light_cleanup);


MODULE_AUTHOR("Cz.");
MODULE_LICENSE("Dual BSD/GPL");



Makefile:


#################################################
obj-m := ToggleLed.o
KERNELDIR := /usr/local/arm/3.4.1/arm-linux/yle2440_2.6.12
default:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules
install:
insmod LED.ko
uninstall:
rmmod LED.ko
#################################################


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇一个让桌面下雪的ruby 小程序 snow 下一篇Linux Kernel 2.6.37.2内核编译实..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·在 Redis 中如何查看 (2025-12-26 03:19:03)
·Redis在实际应用中, (2025-12-26 03:19:01)
·Redis配置中`require (2025-12-26 03:18:58)
·Asus Armoury Crate (2025-12-26 02:52:33)
·WindowsFX (LinuxFX) (2025-12-26 02:52:30)