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

📄 spi.c

📁 软件模拟SPI串行EEPROM控制时序的C程序
💻 C
字号:
#define pllclk  (int*)0x7018
#define wdcr    (int*)0x7029
#define iomux   (int*)0x7092
#define spiccr  (int*)0x7040
#define spictl  (int*)0x7041
#define spibrr  (int*)0x7044
#define spipri  (int*)0x704f
#define spidat  (int*)0x7049
#define spists  (int*)0x7042
#define spirxbuf (int*)0x7047 
 
#include <ioports.h> 

ioport unsigned port6;
int endspi();
int busy();

int endspi()
{    
 	 int i;      
  	 do {i=*spists;}
   	 while((i&0x40)==0);  
   	 return 0; 
}
int busy()
{  	
	int flag;
	*spiccr=0x8f;  /*16*/
	port6=4;  /*low eep cs*/
	*spidat=0x0500; 
    endspi();
    port6=0;  /*high eep cs*/
	flag=*spirxbuf; 
	flag&=0x0001;
	return flag;
}

void eep_write(int start_add,int * data,int num)	
{int i,page,j,a;
    page=start_add>>5;/*caculate current page*/ 
    
   while(busy());   
    *spiccr=0x8f;  /*16*/
	port6=4;       /*low cs*/
	*spidat=0x0100;  /* wrsr */
	endspi();      /*require send is over*/
	port6=0;        /*high cs*/
	while(busy());
	
	*spiccr=0x87;  /*8*/ 
	port6=4;
	*spidat=0x0600;  /*send wren command*/
	endspi();
	port6=0;
	while(busy());
	
	if(num<=16){
	*spiccr=0x87;  /*8*/ 	
   	port6=4;
	*spidat=0x0200; /*send write command*/
	endspi();
	*spiccr=0x8f; /*16*/
	*spidat=start_add; /*send address*/
	endspi();
	for (i=0;i<num;i++) {
		*spidat=data[i];
		endspi();
		}
    port6=0;
    }
 else {
    *spiccr=0x87;  /*8*/ 	
   	port6=4;
	*spidat=0x0200; /*send write command*/
	endspi();
	*spiccr=0x8f; /*16*/
	*spidat=start_add; /*send address*/
	endspi();
	for (i=0;i<16;i++) {
		*spidat=data[i];
		endspi();
		}
    port6=0;
    num=num-16;
    a=16;
while(num>0){
    start_add+=32;
    if(num>16) j=16;
    else j=num;
    *spiccr=0x87;  /*8*/ 
    port6=4;
     while(busy()); 
   	*spidat=0x0200; /*send write command*/
	endspi();
	*spiccr=0x8f; /*16*/
	*spidat=start_add; /*send address*/
	endspi();
	for (i=0;i<j;i++) {
		*spidat=data[i+a];
		endspi();
		}
    port6=0; 
    
    num=num-16;
    a=a+16;
            }   
 }   
    
}    
       
void eep_read(int start_add,int *data, int num)        
{   int i;
    while(busy());
    
    *spiccr=0x87; /*8bit send read command*/
    port6=4;  
    *spidat=0x0300;
    endspi();   
    *spiccr=0x8f;  /*16 bit send address*/
    *spidat=start_add; 
    endspi();
    data[0]=*spirxbuf;
   
    *spiccr=0x8f;  /*16*/  
    
    for(i=0;i<=num;i++)  {
    	*spidat=0x0000;
       	endspi();
       	data[i]=*spirxbuf;
    	}
    port6=0;	
}  

main()
{  
	int data[32],i,j,data1[32]; 
 	
 	for(i=0;i<32;i++)  {
 		data[i]=i+10;
 		}                          
 		
	*wdcr=0x006f;  /*Disable WD*/ 
	*iomux=0x01ff;
	*pllclk=0x007c; /*open spi set clk pll*/

	*spiccr=0x0f;   /*reset spi*/  
	
	*spibrr=20;   /*set baud=2M*/
	*spipri=0x10;   /*free run*/
	*spictl=0x0e;   /*set mode=2*/
	*spiccr=0x8f;
	
	eep_write(0,data,15);             
	eep_read(0,data1,15);
	i=0;
}	    
 

⌨️ 快捷键说明

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