📄 s3c2410_spi_cc1100-driver.c
字号:
#include "CC1100.h"MODULE_DESCRIPTION("My kernel module");MODULE_AUTHOR("root (root@Figo)");MODULE_LICENSE("GPL");int CC1100_MAJOR=0;int CC1100_MINOR=0;//static struct tasklet_struct tasklet;struct fasync_struct *fasync1; static int cc1100_fasync(int fd,struct file *filp,int on);static int cc1100_open(struct inode * inode,struct file *filp);static int cc1100_release(struct inode * inode,struct file *filp);static ssize_t cc1100_write(struct file *filp,const char *buf,size_t count,loff_t *ppos);static ssize_t cc1100_read(struct file *filp,char __user *buf,size_t count,loff_t *ppos);static int s3c2410_spi_cc1100_probe(struct device *dev);static int s3c2410_spi_cc1100_remove(struct device *dev);static int cc1100_ioctl(struct inode * inode,struct file *filp,unsigned int cmd,unsigned long arg);static struct file_operations cc1100_fops={ .owner= THIS_MODULE, .open= cc1100_open, .write= cc1100_write, .read= cc1100_read, .release= cc1100_release, .ioctl= cc1100_ioctl, .fasync= cc1100_fasync,};static struct device_driver s3c2410_spi_cc1100_driver={ .name= "s3c2410_spi_cc1100", .bus= &platform_bus_type, .probe= s3c2410_spi_cc1100_probe, .remove= s3c2410_spi_cc1100_remove,};/*static void tasklet_callback(unsigned long data){ while(1) { printk(KERN_ALERT "tasklet_callback running.\n"); mdelay(500); }}*/static int cc1100_open(struct inode * inode,struct file *filp){ /*initial the cc1100*/// tasklet_init(&tasklet,tasklet_callback, 0); CpuInit(); POWER_UP_RESET_CC1100(); halRfWriteRfSettings(); halSpiWriteBurstReg(CCxxx0_PATABLE, PaTabel, 8);// tasklet_schedule(&tasklet); return 0;}static int cc1100_release(struct inode * inode,struct file *filp){ cc1100_fasync(-1,filp,0);// tasklet_kill(&tasklet); return 0;}# define SEND 0x00# define RECEIVE 0xff static void wq_func(void *data){ while(1) { printk(KERN_ALERT "work_queue running.\n"); mdelay(500); }}static struct work_struct *my_work=NULL;static int mydata=1;static struct workqueue_struct *my_wq=NULL;static int cc1100_ioctl(struct inode * inode,struct file *filp,unsigned int cmd,unsigned long arg){ if(cmd==RECEIVE) {// tasklet_enable(&tasklet); printk("Figo 1\n"); my_wq=create_workqueue("cc1100_wq"); if(my_wq==NULL) { printk("create workqueue fialed !\n"); return -1; } printk("Figo 1\n"); INIT_WORK(my_work,wq_func,(void*)&mydata); printk("Figo 1\n"); queue_delayed_work(my_wq,my_work,1); printk("Figo 1\n"); } else {// tasklet_disable(&tasklet); destroy_workqueue(my_work); } return 0;}static ssize_t cc1100_write(struct file *filp,const char *buf,size_t count,loff_t *ppos){ char *kbuf[8]={0}; copy_from_user(kbuf,buf,count); halRfSendPacket(kbuf,count); return count; }static ssize_t cc1100_read(struct file *filp,char __user *buf,size_t count,loff_t *ppos){ char kbuf[4]={0};// copy_to_user(&buf[size],&kbuf[size],1); return count; }static int cc1100_fasync(int fd,struct file *filp,int on){ int ret; ret=fasync_helper(fd,filp,on,&fasync1); if(ret<0) return ret; return 0;}dev_t cc1100_devt;static struct cdev * cc1100_cdev;static int s3c2410_spi_cc1100_probe(struct device *dev){ int result; printk("s3c2410_spi_cc1100_probe!\n"); /*register the driver to system*/ result=alloc_chrdev_region(&cc1100_devt,CC1100_MINOR,1,"s3c2410_spi_cc1100"); CC1100_MAJOR=MAJOR(cc1100_devt); if(result<0) { printk(KERN_ERR"Can`t get the major number %d for CC1100\n",CC1100_MAJOR); return -1; } cc1100_cdev=cdev_alloc(); if(cc1100_cdev!=NULL) { cdev_init(cc1100_cdev,&cc1100_fops); cc1100_cdev->ops=&cc1100_fops; cc1100_cdev->owner=THIS_MODULE; if(cdev_add(cc1100_cdev,cc1100_devt,1)) printk(KERN_NOTICE"Can`t add this driver to system!\n"); else printk(KERN_NOTICE" add this driver to system succefully!\n"); } else { printk(KERN_ERR"cdev_alloc error!\n"); return -1; } devfs_mk_cdev(MKDEV(CC1100_MAJOR,CC1100_MINOR),S_IFCHR|S_IRUSR|S_IWUSR,"cc1100"); s3c2410_gpio_cfgpin(SCK,S3C2410_GPE13_OUTP); s3c2410_gpio_cfgpin(CSN,S3C2410_GPE14_OUTP); s3c2410_gpio_cfgpin(MOSI,S3C2410_GPE12_OUTP); s3c2410_gpio_cfgpin(MISO,S3C2410_GPE11_INP); s3c2410_gpio_cfgpin(GDO0,S3C2410_GPE15_INP); return 0;}static int s3c2410_spi_cc1100_remove(struct device *dev){ // cdev(s3c2410_spi_cc1100_driver); unregister_chrdev_region(cc1100_devt,1); return 0;}static int __init s3c2410_spi_cc1100_init_module(void){ printk( KERN_DEBUG "Module s3c2410_spi_cc1100 init\n" ); driver_register(&s3c2410_spi_cc1100_driver); return 0;}static void __exit s3c2410_spi_cc1100_exit_module(void){ printk( KERN_DEBUG "Module s3c2410_spi_cc1100 exit\n" ); driver_unregister(&s3c2410_spi_cc1100_driver); cdev_del(cc1100_cdev); devfs_remove("cc1100");}module_init(s3c2410_spi_cc1100_init_module);module_exit(s3c2410_spi_cc1100_exit_module);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -