⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s3c2410_spi_cc1100-driver-backup2.c

📁 s3c2410开发板中使用cc1100无线模块的linux驱动
💻 C
字号:
#include <linux/config.h>#include <linux/errno.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/serio.h>#include <linux/delay.h>#include <asm/io.h>#include <asm/irq.h>#include <linux/sched.h>#include <linux/poll.h>#include <linux/spinlock.h>#include <linux/coda.h>#include <linux/cdev.h>#include <linux/interrupt.h>#include <linux/devfs_fs_kernel.h>#include <asm/io.h>#include <asm/arch/regs-adc.h>#include <asm/arch/regs-gpio.h>#include <asm/irq.h>#include <linux/delay.h>#include <asm/hardware.h>#include <linux/fs.h>#include <linux/ioctl.h>#include <linux/device.h>#include <asm/arch/map.h>#include <asm/hardware/clock.h>#include <asm/arch/hardware.h>#include <asm/arch/regs-spi.h>#include "CC1100.h"MODULE_DESCRIPTION("My kernel module");MODULE_AUTHOR("root (root@Figo)");MODULE_LICENSE("GPL");int CC1100_MAJOR=0;int CC1100_MINOR=0;void __iomem *spi_addr_vir=NULL;struct fasync_struct *fasync; static int cc1100_open(struct inode * inode,struct file *filp);static int cc1100_release(struct inode * inode,struct file *filp);static int cc1100_ioctl(struct inode * inode,struct file *filp,unsigned int cmd,unsigned long arg);static ssize_t cc1100_read(struct file *filp,char *buf,size_t count,loff_t *ppos);static ssize_t cc1100_write(struct file *filp,const char *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 void cc1100_receive(int irq,void *dev_id,struct pt_regs *regs);static int cc1100_fasync(int fd,struct file *filp,int on);static struct file_operations cc1100_fops={	.owner=		THIS_MODULE,	.open=		cc1100_open,	.read=		cc1100_read,	.write=		cc1100_write,	.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 int cc1100_open(struct inode * inode,struct file *filp){			/*initial the cc1100*/	printk("Figo1!\n");	RESET_CC1100();	printk("Figo2!\n");	halRfWriteRfSettings();	halSpiWriteBurstReg(CCxxx0_PATABLE,PaTabel,8);	return 0;}static int cc1100_release(struct inode * inode,struct file *filp){	cc1100_fasync(-1,filp,0);	return 0;}# define SEND 0x00# define RECEIVE 0xff static int cc1100_ioctl(struct inode * inode,struct file *filp,unsigned int cmd,unsigned long arg){	int result;	if(cmd==RECEIVE)	{		halSpiStrobe(CCxxx0_SRX);		s3c2410_gpio_cfgpin(GDO0,S3C2410_GPG2_EINT10);		set_irq_type(IRQ_EINT10, IRQT_FALLING);		result=request_irq(IRQ_EINT10,cc1100_receive,SA_INTERRUPT,"s3c2410_spi_cc1100",NULL);		if(result)		{			printk(KERN_ERR"the EINT0 is not free\n");			return -1;		}		enable_irq(IRQ_EINT10);			printk("Figo8!\n");	}	else	{		s3c2410_gpio_cfgpin(GDO0,S3C2410_GPG2_INP);		disable_irq(IRQ_EINT10);			printk("Figo9!\n");	}	return 0;}static ssize_t cc1100_read(struct file *filp,char *buf,size_t size,loff_t *ppos){	char kbuf[8]={0};	unsigned char count=(unsigned char)size;	halRfReceivePacket(kbuf,&count);	copy_to_user(buf,kbuf,count);	return count;}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;	}dev_t cc1100_devt;static struct clk  * spi_clk;static struct cdev * cc1100_cdev;static int s3c2410_spi_cc1100_probe(struct device *dev){	int result;	char spicon0;	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");	/*request the memorary resource*/		if(!request_mem_region((unsigned long)S3C24XX_VA_SPI,SZ_1M,"s3c2410_spi_cc1100"))	{		printk(KERN_ERR"the spi0 region %X is not free\n",S3C24XX_VA_SPI);		return -EBUSY;	}		/*request the external interrupt 0 resource*///	disable_irq(IRQ_EINT10);//	set_irq_type(IRQ_EINT10, IRQT_FALLING);//	result=request_irq(IRQ_EINT10,cc1100_receive,SA_INTERRUPT,"s3c2410_spi_cc1100",NULL);//	if(result)//	{//		printk(KERN_ERR"the EINT0 is not free\n");//		return -1;//	}//	disable_irq(IRQ_EINT10);	/*request the clock resource*/	spi_clk=clk_get(NULL,"spi");	if(!spi_clk)	{		printk(KERN_ERR"Can`t get the spi clock\n");		return -1;	}	clk_use(spi_clk);	clk_enable(spi_clk);	/*confiure the GPE14 GPE13,GPE12,GPE11*/	s3c2410_gpio_cfgpin(S3C2410_GPE13,S3C2410_GPE13_SPICLK0);	s3c2410_gpio_cfgpin(S3C2410_GPE12,S3C2410_GPE12_SPIMOSI0);	s3c2410_gpio_cfgpin(S3C2410_GPE11,S3C2410_GPE11_SPIMISO0);	s3c2410_gpio_cfgpin(CSN,S3C2410_GPE14_OUTP);	s3c2410_gpio_cfgpin(GDO0,S3C2410_GPG2_INP);	/*map the phy address to vir address*/	spi_addr_vir=ioremap(S3C2410_PA_SPI,SZ_1M);	if(!spi_addr_vir)	{		printk(KERN_ERR"spi0 ioremap failed\n");			iounmap(S3C2410_PA_SPI);		return -EINVAL;		}	/*initial the spi*/		iowrite8(0x20,spi_addr_vir+0x0c);	iowrite8(0x19,spi_addr_vir+0x00);	return 0;}static int s3c2410_spi_cc1100_remove(struct device *dev){	//	cdev(s3c2410_spi_cc1100_driver);	unregister_chrdev_region(cc1100_devt,1);	free_irq(IRQ_EINT10,NULL);	clk_disable(spi_clk);	clk_unuse(spi_clk);	clk_put(spi_clk);	spi_clk=NULL;	iounmap(spi_addr_vir);	return 0;}static void cc1100_receive(int irq,void *dev_id,struct pt_regs *regs){	disable_irq(IRQ_EINT10);	if(fasync)		kill_fasync(&fasync,SIGIO,POLL_IN);	printk("EINT10!\n");	enable_irq(IRQ_EINT10);	return 0;}static int cc1100_fasync(int fd,struct file *filp,int on){	int ret;	ret=fasync_helper(fd,filp,on,&fasync);	if(ret<0)		return ret;	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 + -