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

📄 spiok.c

📁 本程序是s3c2410在linux2.4.18系统平台的spi接口驱动程序
💻 C
字号:
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#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 

int spi_open(struct inode *,struct file *); 
int spi_close(struct inode *,struct file *); 
static int spi_ioctl(struct inode *inode ,struct file* file,unsigned int cmd,unsigned long arg);
static ssize_t  spi_read(struct file * file,char * buffer, size_t count, loff_t *ppos);
static ssize_t  spi_write(struct file *flie,const char * buffer, size_t count, loff_t *ppos);

static struct file_operations spi_fops = { 
open:spi_open, 
read:spi_read, 
write:spi_write, 
ioctl:spi_ioctl, 
release:spi_close, 
}; 
void spi_port_init(void)
{//Master mode polling 
printk("***************************"); 
printk("spi open program begin..\n"); 
printk("now,GPE AND GPG port init...\n"); 
GPECON&=0xf03fffff;//GPECON-11,12,13=10 
GPECON|=0x0a800000; 
printk("SPI_GPECON=0x%x\n",GPECON); 

GPEUP&=(~0x3800);//GPEUP-11,12=0;GPEUP-13=1 
GPEUP|=0x2000;  
printk("SPI_GPEUP=0x%x\n",GPEUP); 

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

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

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

int spi_open(struct inode *inode,struct file *filp) 
{ 
spi_port_init();
MOD_INC_USE_COUNT;
return 0;
} 

static ssize_t  spi_read(struct file * file,char * buffer, size_t count, loff_t *ppos)
{ return 0;
} 

static ssize_t  spi_write(struct file *flie,const char * buffer, size_t count, loff_t *ppos)
{ 
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; 

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


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


*spiRxStr=SPRDAT0; 
printk("receive char=%c\n",*spiRxStr++); 
} 
} 

SPCON0=(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); 

return 0; 

} 




static int spi_ioctl(struct inode *inode ,struct file* file,unsigned int cmd,unsigned long arg)
{ 
return 0;
}  

int spi_close(struct inode *inode,struct file *filp) 
{ 
printk(KERN_CRIT"DEMO:spi device close\n");
MOD_DEC_USE_COUNT;
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; 
}  
printk("Init spi success!\n"); 
return 0; 
} 

static void __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 + -