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

📄 da_driver.c

📁 DA驱动
💻 C
字号:
#include <linux/module.h> // Needed by all modules#include <linux/kernel.h> // Needed for KERN_ALERT#include <linux/init.h>   // Needed for the macros#include <asm/io.h>#include <linux/types.h>#include <linux/fcntl.h>#include <sys/syscall.h>#include <linux/errno.h>#include <linux/fs.h>#include <linux/unistd.h>#include <linux/wrapper.h>#include <asm/uaccess.h>#define da_major 152typedef volatile unsigned int AT91_REG;//#include <linux/modversions.h>MODULE_LICENSE("GPL");static int da_open(struct inode *,struct file *);static ssize_t da_write(struct file *,const char *,size_t,loff_t *);static int da_close(struct inode *,struct file *); static int da_open(struct inode *inode,struct file *file){	//PIO A基地址ffff f400	//PIO C基地址ffff f800	//PMC基地址ffff fc00  AT91_REG *PIO_PDR=0;     AT91_REG *PIO_ASR=0;     AT91_REG *PIO_PER=0;     AT91_REG *PIO_OWER=0;    AT91_REG *PIO_OER=0;     AT91_REG *PIO_ODSR=0;    AT91_REG *PMC_PCER=0;           PIO_PDR = ioremap((unsigned long)0xfffff404,(unsigned long)1);      PIO_ASR = ioremap((unsigned long)0xfffff470,(unsigned long)1);      PIO_PER = ioremap((unsigned long)0xfffff800,(unsigned long)1);      PIO_OWER = ioremap((unsigned long)0xfffff8a0,(unsigned long)1);      PIO_ODSR = ioremap((unsigned long)0xfffff838,(unsigned long)1);      PIO_OER = ioremap((unsigned long)0xfffff810,(unsigned long)1);      *PIO_PDR=0X26;//禁止PA1,2,5I/O功能      *PIO_ASR=0X26;//PA1,2,5选择外围功能A      *PIO_PER=0x6000;//使能PC14I/O功能,DA的清零信号      *PIO_OER=0x6000;//PC14输出使能      *PIO_OWER=0x6000;//PC14输出写使能      *PIO_ODSR=0x0;//PC14输出0,将DA输出清零            PMC_PCER = ioremap((unsigned long)0xfffffc10,(unsigned long)1);      *PMC_PCER=0X2000;//使能SPI外围时钟      *PIO_ODSR=0x6000;//将清零信号置1   return 0;}static ssize_t da_write(struct file *file,const char *buffer,size_t count,loff_t *offset){ char value;//SPI define  AT91_REG *SPI_CR=0;  //SPI控制寄存器,使能SPI  AT91_REG *SPI_MR=0;  //SPI模式寄存器,设定主机模式,选择NPCS2  AT91_REG *SPI_TDR=0; //SPI发送数据寄存器  //AT91_REG *SPI_SR=0;  //SPI状态寄存器,用于检测TDRE  AT91_REG *SPI_CSR2=0; //SPI片选寄存器,设定数据发送时序      SPI_CSR2 = ioremap((unsigned long)0xfffe0038,(unsigned long)1);      SPI_MR = ioremap((unsigned long)0xfffe0004,(unsigned long)1);      SPI_CR = ioremap((unsigned long)0xfffe0000,(unsigned long)1);      //SPI_SR = ioremap((unsigned long)0xfffe0010,(unsigned long)1);      SPI_TDR = ioremap((unsigned long)0xfffe000c,(unsigned long)1);      *SPI_CSR2=0x30603; //Tcss约33ns,spck约7.4M      *SPI_MR=0x30001;  //选择NPCS2,主机模式      *SPI_CR=0x01;     //使能SPI      while(count>0)      { get_user(value,buffer++);        *SPI_TDR=value;        count--;      }              return (count);  	}static int da_close(struct inode *inode,struct file *file){	return 0;}struct file_operations da_fops ={	open:da_open,	write:da_write,	release:da_close,};int da_init(void){	int rc;	rc=register_chrdev(da_major,"spi2",&da_fops);	if (rc<0)	{		printk(KERN_WARNING "da:can't get major %d\n",da_major);		return rc;	}	printk(KERN_INFO "da:get major %d\n",da_major);	return 0;}	int init_module(void){	return da_init();}void cleanup_module(void){	unregister_chrdev(da_major,"spi2");	printk(KERN_ALERT "Goodbye\n");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -