?? da_driver.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 + -