📄 spi.c
字号:
#include "f2407_c.h"
#define LED port40
volatile ioport unsigned int port40;
extern unsigned int test_x25650(void);
void write_enable(void);
unsigned int read_x25650(unsigned int addr);
void write_x25650(unsigned int addr,unsigned int data);
void reset_cs(void);
void set_cs(void);
unsigned int wr_busy(void);
void write_status(void);
void spi_init(void);
void spi_init(void)
{
unsigned int temp;
*SPICCR=0x0007;
*SPICTL=0x0006;
*SPIBRR=0x001f;
*SPICCR=*SPICCR|0x0080;
*SPICCR=0xc7;
*SPIPRI=0x0030;
*SPITXBUF=0x5500;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
write_enable();
write_status();
}
void write_status(void) /*wpen=1,BL0=BL1=0*/
{
int temp;
reset_cs();
*SPITXBUF=0x0100;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
*SPITXBUF=0x8000;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
set_cs();
while(wr_busy());
}
unsigned int wr_busy(void)
{
unsigned int temp;
/*for(temp=0;temp<60000;temp++);
temp=0;
return(temp);*/
reset_cs();
*SPITXBUF=0x0500;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
*SPITXBUF=0x0000;
while((*SPISTS&0x0040)==0);
set_cs();
temp=*SPIRXBUF;
temp=temp&0x0001;
return(temp);
}
void set_cs(void)
{
/**PCDATDIR=*PCDATDIR|0x0020;*/
asm(" setc XF");
}
void reset_cs(void)
{
/**PCDATDIR=*PCDATDIR&0xffdf;*/
asm(" clrc XF");
}
void write_x25650(unsigned int addr,unsigned int data)
{
unsigned temp;
write_enable();
reset_cs();
*SPITXBUF=0x0200;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
*SPITXBUF=addr;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
*SPITXBUF=(addr%0x100)*0x100;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
*SPITXBUF=data*0x100;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
set_cs();
while(wr_busy());
}
unsigned int read_x25650(unsigned int addr)
{
unsigned int temp;
reset_cs();
*SPITXBUF=0x0300;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
*SPITXBUF=addr;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
*SPITXBUF=(addr%0x100)*0x100;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
*SPITXBUF=0;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
set_cs();
return(temp&0x00ff);
}
/*make X25040 write enable function*/
void write_enable(void)
{
int temp;
reset_cs();
*SPITXBUF=0x0600;
while((*SPISTS&0x0040)==0);
temp=*SPIRXBUF;
set_cs();
while(wr_busy());
}
/**************************************************************/
/*Ranging from address 0x00 to 0xff,write data 0x55 to each address
/*and test reading from each address.
/*******************************************************************/
unsigned int test_x25650(void)
{
unsigned int data_read;
unsigned int address;
spi_init();
for(address=0;address<0x00ff;address++)
{
write_x25650(address,0x55);
data_read=read_x25650(address);
/*
if((address%256)<128)
{
LED=0xff77;
}
else
{
LED=0xfff7;
}
*/
if(data_read!=0x55) /*Spi test error*/
{
asm("nop");
return(0);
}
/*write_x25650(address,0xff);*/
}
return(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -