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

📄 new.txt

📁 spi驱动 已经通过测试 外加spi芯片
💻 TXT
字号:
#include <linux/module.h> 
#include <linux/version.h> 
#include <linux/kernel.h> 
#include <linux/iobuf.h> 
#include <linux/major.h> 
#include <linux/blkdev.h> 
#include <linux/capability.h> 
#include <linux/smp_lock.h> 
#include <linux/fs.h> 
#include <linux/mm.h> 
#include <asm/uaccess.h> 
#include <stdio.h> 
#include <linux/types.h> 
#include <linux/sched.h> 
#include <linux/pm.h> 
#include <linux/config.h> 
#include <linux/init.h> 
#include <asm/arch/cpu_s3c2410.h> 
#include <asm/io.h> 
#include <linux/vmalloc.h> 
#include <linux/miscdevice.h> 
#include <linux/sched.h> 
#include <linux/delay.h> 
#include <linux/poll.h> 
#include <linux/spinlock.h> 
#include <linux/irq.h> 
#include <linux/delay.h> 
#include <asm/hardware.h> 

#define SPI_MAJOR 180 
devfs_handle_t devfs_spi; 

#define SPI_BUFFER _NONCACHE_STARTADDRESS 

unsigned long SPI_GPECON;//GPE Part define 
unsigned long SPI_GPEDAT; 
unsigned long SPI_GPEUP; 
unsigned long SPI_GPGCON;//GPG Part define 
unsigned long SPI_GPGDAT; 
unsigned long SPI_GPGUP; 
unsigned long SPI_SPCON0;//SPI Part define 
unsigned long SPI_SPSTA0; 
unsigned long SPI_SPPIN0; 
unsigned long SPI_SPPRE0; 
unsigned long SPI_SPTDAT0; 
unsigned long SPI_SPRDAT0; 

#define spi_gpecon (*(volatile unsigned long *)SPI_GPECON) 
#define spi_gpedat (*(volatile unsigned long *)SPI_GPEDAT) 
#define spi_gpeup (*(volatile unsigned long *)SPI_GPEUP) 
#define spi_gpgcon (*(volatile unsigned long *)SPI_GPGCON) 
#define spi_gpgdat (*(volatile unsigned long *)SPI_GPTDAT) 
#define spi_gpgup (*(volatile unsigned long *)SPI_GPGUP) 
#define spi_con0 (*(volatile unsigned long *)SPI_SPCON0) 
#define spi_sta0 (*(volatile unsigned long *)SPI_SPSTA0) 
#define spi_pin0 (*(volatile unsigned long *)SPI_SPPRE0) 
#define spi_pre0 (*(volatile unsigned long *)SPI_SPPRE0) 
#define spi_tdat0 (*(volatile unsigned long *)SPI_SPTDAT0) 
#define spi_rdat0 (*(volatile unsigned long *)SPI_SPRDAT0) 


int spi_open(struct inode *,struct file *); 
int spi_close(struct inode *,struct file *); 
int spi_ioctl(struct inode *,struct file *,unsigned int,unsigned long); 
ssize_t spi_read(struct file *, char *,size_t); 
ssize_t spi_write(struct file *, char *,size_t); 

static struct file_operations spi_fops = { 
open:spi_open, 
read:NULL,//spi_read, 
write:spi_write, 
ioctl:NULL,//spi_ioctl, 
release:spi_close, 
}; 
 
 

int spi_open(struct inode *inode,struct file *filp) 
{ 
int value; 
printk("***************************"); 
printk("spi open program begin..\n"); 
printk("now,GPE AND GPG port init...\n"); 
spi_gpecon &=0xf03fffff;//GPECON-11,12,13=10 
spi_gpecon |=0x0a800000; 
value=(int)spi_gpecon; 
printk("spi_gpecon=0x%x\n",value); 
spi_gpeup &=(~0x3800);//GPEUP-11,12=0;GPEUP-13=1 
spi_gpeup |=0x2000; 
value=(int)spi_gpeup; 
printk("spi_gpeup=0x%x\n",value); 

spi_gpgcon &=0xffffffcf;//GPGCON-2=11(nSS) 
spi_gpgcon |=0x30; 
value=spi_gpgcon; 
printk("spi_gpgcon=0x%x\n",value); 

spi_gpgup |=0x4;//GPGUP-2=1 
value=spi_gpgup; 
printk("spi_gpeup=0x%x\n",value); 

printk("GPE AND GPG port init end!\n"); 
printk("****************************"); 

} 
/* 
ssize_t spi_read(struct file * fp, char *buf,size_t size) 
{ 
} 
*/ 
ssize_t spi_write(struct file *fp, char *buf,size_t size) 
{ 
int i=0; 
int config; 
char string; 
char str[20]; 
char *txStr,*rxStr; 
volatile char *spiTxStr,*spiRxStr; 
volatile int endSpiTx; 

printk("SPI polling TX/RX Test...\n"); 
printk("Connet SPIMOSI0 into SPIMISO\n"); 
endSpiTx=0; 
spiTxStr="ABCD0123"; 
spiRxStr=str; 
txStr=(char *)spiTxStr; 
rxStr=(char *)spiRxStr; 

spi_pre0=0x0;//SPI Baud Rate Prescaler Register,Baud Rate=PCLK/2/(Prescaler value+1) 
spi_con0=(0<<5)|(1<<4)|(1<<3)|(1<<2)|(0<<1)|(0<<0); 
config=(int)spi_con0; 
printk("spi_con0=0x%x\n",config); 
//polling,en-sck,master,low,format A,nomal 
spi_pin0=(0<<2)|(1<<1)|(0<<0); 
//Multi Master error detect disable,reserved,release 
config=(int)spi_pin0; 
printk("spi_pin0=0x%x\n",config); 


while(endSpiTx==0) 
{ 
//if(spi_sta0&0x1) 
if((spi_sta0&0x01)==1) //data Tx/Rx ready 
{ 
if(*spiTxStr !='\0') 
{ 
spi_tdat0=*spiTxStr++; 
string=spi_tdat0; 
printk("transmit char=%c\n",string); 
} 
else 
endSpiTx=1; 

//while(!(spi_sta0&0x1)) ; 
// while((spi_sta0&0x1)==0) 
// ;//check Rx ready state 

str[i]=spi_rdat0; 
printk("receive char=%c\n",str[i]); 
i++; 

} 
} 

spi_con0=(0<<5)|(0<<4)|(1<<2)|(0<<1)|(0<<0); 
//Polling,dis-sck,master,low,format A,nomal 
*(spiRxStr-1)='\0';//remove last dummy data & attach End of String(Null) 
printk("Tx string:%s\n",txStr); 
printk("Rx string:%s\n",rxStr); 
/* 
if(strcmp(rxStr,txStr)==0) 
printk("transmit and receive is ok!\n"); 
else 
printk("ERROR!\n"); 
*/ 
return 0; 

} 

 


/* 
int spi_ioctl(struct inode *inode,struct file *filp,unsigned int command,unsigned long arg) 
{ 

} 
*/ 

int spi_close(struct inode *inode,struct file *filp) 
{ 
printk("the program close!\n"); 
return 0; 
} 


static int __init spi_init(void) 
{ 

devfs_spi=devfs_register(NULL,"spi",DEVFS_FL_DEFAULT,SPI_MAJOR,0,S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,&spi_fops,NULL); 
/* 
if (devfs_spi<0) 
{ 
printk(KERN_CRIT"SPI:spi devfs_spi= %d\n",devfs_spi); 
return -EIO; 
} 
*/ 
/**************************************************************** 
* SMDK2400 SPI configuration * 
* GPG2=nSS0, GPE11=SPIMISO0, GPE12=SPIMOSI0, GPE13=SPICLK0* 
* SPI0 is tested by this code* 
****************************************************************/ 
SPI_GPECON = ioremap(0x56000040,4); 
SPI_GPEDAT = ioremap(0x56000044,4); 
SPI_GPEUP = ioremap(0x56000048,4); 

SPI_GPGCON = ioremap(0x56000060,4); 
SPI_GPGDAT = ioremap(0x56000064,4); 
SPI_GPGUP = ioremap(0x56000068,4); 

SPI_SPCON0 =ioremap(0x59000000,4); 
SPI_SPSTA0 =ioremap(0x59000004,4); 
SPI_SPPIN0 =ioremap(0x59000008,4); 
SPI_SPPRE0 =ioremap(0x5900000c,4); 
SPI_SPTDAT0 =ioremap(0x59000010,4); 
SPI_SPRDAT0 =ioremap(0x59000014,4); 

printk("Init spi success!\n"); 
return 0; 
} 

static int __exit spi_exit(void) 
{ 
devfs_unregister(devfs_spi); 
printk("spi device unregister!\n"); 
} 

module_init(spi_init); 
module_exit(spi_exit); 

 
 

⌨️ 快捷键说明

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