📄 spi.c
字号:
/************************************************************/
#include "config.h"
//#define FL1 0x00000080 //P0.7
#define SPI_SCK 0x00000010 //P0.4
#define SPI_SI 0x00000040 //P0.6
#define SPI_SO 0x00000020 //P0.5
#define PROTECT_PAGE_SIZE 200 // 保护页面大小
#define FLASH_PAGE_SIZE 512 // flash 物理页面大小
UINT8 FlashBusy;
uint16 PageSize;
uint8 PageProtect[FLASH_PAGE_SIZE];
//-------------------------------------------------------------------//
void delay(uint16 delay_time)
{
uint8 i,j;
for(i=0;i<delay_time;i++)
for(j=0;j<200;j++);
}
//------------------------------提取满足要求的字符串从提供字符串中------------------------//
void FindStrFromStr(uint8 str_object[],uint8 str_loading[],uint8 loading_point,uint8 start_point,uint8 number)
{
uint8 k;
for(k=0;k<number;k++)
str_loading[k+loading_point]=str_object[k+start_point];
}
//-----------------------------------------------------------------------------------//
void SPI_Ini(void)
{
PINSEL0 = (PINSEL0 & 0xffff00ff);
IODIR = (IODIR|SPI_SCK|SPI_SI|FL1|FL2);
}
//-----------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------//
//-----------------------------------------------------------------------------------//
void SPIwrite(uint8 x)
{
uint8 i,temp,x1,x2;
temp =x;
x1 =0x80;
for(i=0;i<8;i++)
{
IOCLR=SPI_SCK;
x2=temp & x1;
if (x2>0){IOSET=SPI_SI;}else{IOCLR=SPI_SI;}
DelayNS(1);
IOSET=SPI_SCK;
DelayNS(1);
x1=x1>>1;
}
}
//-----------------------------------------------------------------------------------//
uint8 SPIread(void)
{
uint8 i,x1;
x1=0;
for(i=0;i<8;i++){
IOCLR=SPI_SCK;
DelayNS(1);
IOSET=SPI_SCK;
x1=x1<<1;
if((IOPIN&SPI_SO)!=0){x1=x1|0x01;}
DelayNS(1);
}
return x1;
}
//-----------------------------------------------------------------------------------//
void SPI_write(uint8 temp)
{
while(write_at45db041b_busy()==0);
IOCLR=FL1;
SPIwrite(0x84);
SPIwrite(0);
SPIwrite(0);
SPIwrite(0);
DelayNS(1);
SPIwrite(temp); //向BUF写入0x66
DelayNS(1);
IOSET=FL1;
DelayNS(4);
}
//-----------------------------------------------------------------------------------//
uint8 SPI_read(void)
{
uint8 temp;
while(write_at45db041b_busy()==0);
IOCLR=FL1;
SPIwrite(0x54);
SPIwrite(0);
SPIwrite(0);
SPIwrite(0);
SPIwrite(0);
temp=SPIread();
IOSET=FL1;
DelayNS(4);
return temp;
}
/***********************************/
uint8 Read_Sta_AT45(void)
{
uint8 temp_re;
IOSET = SPI_SCK;
IOCLR = FL1;
IOCLR = SPI_SCK;
DelayNS(1);
SPIwrite(0x57);
temp_re=SPIread();
DelayNS(1);
IOSET = FL1;
return( temp_re);
}
/***********************************/
uint8 Read_Memory(uint16 page_count, uint16 page_offset,uint8 * pdata,uint16 lenth)
{
uint16 temp_i ;
uint16 length ;
if(FlashBusy) return(0);
FlashBusy =1;
while(lenth)
{
length =512 -page_offset ;
if(lenth <length) length =lenth ;
while(write_at45db041b_busy()==0);
IOCLR=FL1;
DelayNS(10);
SPIwrite(0x52);
DelayNS(5);
SPIwrite((uint8)(page_count >>6));
DelayNS(5);
SPIwrite((uint8)( (page_count <<2) | (page_offset>>8) ) );
DelayNS(5);
SPIwrite((uint8)(page_offset));
DelayNS(5);
SPIwrite(0x00);
SPIwrite(0x00);
SPIwrite(0x00);
SPIwrite(0x00);
for(temp_i =0 ;temp_i <length ;temp_i ++)
{
pdata[temp_i]=SPIread();
DelayNS(1);
}
DelayNS(10);
IOSET = FL1;
DelayNS(10);
page_count ++ ;
page_offset =0 ;
pdata +=length ;
lenth -=length ;
}
FlashBusy =0;
return(1);
}
/*********************************/
uint8 *ProtectCurrentPage(uint16 page_num)
{
uint16 temp_i;
while(write_at45db041b_busy()==0);
IOCLR=FL1;
DelayNS(10);
SPIwrite(0x52);
DelayNS(5);
SPIwrite((uint8)(page_num >>6));
DelayNS(5);
SPIwrite((uint8)( (page_num<<2) | (0>>8) ) );
DelayNS(5);
SPIwrite((uint8)(0));
DelayNS(5);
SPIwrite(0x00);
SPIwrite(0x00);
SPIwrite(0x00);
SPIwrite(0x00);
for(temp_i =0;temp_i <PageSize;temp_i++)
{
PageProtect[temp_i]=SPIread();
DelayNS(1);
}
DelayNS(10);
IOSET = FL1;
DelayNS(10);
return(PageProtect);
}
/***********************************/
uint8 Write_Memory(uint16 page_counter, uint16 page_offset,uint8 * pdata,uint16 lenth)
{
uint16 m;
uint8 * protect_page;
if(FlashBusy) return(0);
FlashBusy =1;
if(page_counter ==1 || page_counter ==0) PageSize =FLASH_PAGE_SIZE; // 页码为0或1时
else PageSize =PROTECT_PAGE_SIZE;
protect_page =ProtectCurrentPage(page_counter);
for(m =0 ;m <lenth ;m++) protect_page[page_offset +m] =pdata[m];
while(write_at45db041b_busy()==0);
IOCLR = FL1;
DelayNS(10);
SPIwrite(0x82);
DelayNS(5);
SPIwrite((uint8)(page_counter>>6));
DelayNS(5);
SPIwrite((uint8)(page_counter<<2));
DelayNS(5);
SPIwrite(0);
DelayNS(5);
for(m=0;m<PageSize;m++)
{
SPIwrite(protect_page[m]);
DelayNS(1);
}
DelayNS(10);
IOSET = FL1;
DelayNS(10);
FlashBusy =0;
return(1);
}
/***********************************/
uint8 WritePage2Flash(uint16 page, uint16 offset,uint8 * pdata) // 直接写1页(512B)至flash 与 Write_Memory的区别在与 Write_Memory 在写入之前对页面进行保护 而 WritePage2Flash 则不保护
{
uint16 count;
if(FlashBusy) return(0);
FlashBusy =1;
while(write_at45db041b_busy()==0);
IOCLR = FL1;
DelayNS(10);
SPIwrite(0x82);
DelayNS(5);
SPIwrite((uint8)(page>>6));
DelayNS(5);
SPIwrite((uint8)(page<<2));
DelayNS(5);
SPIwrite(0);
DelayNS(5);
for(count =0 ;count <512 ;count ++)
{
SPIwrite(pdata[count]);
DelayNS(1);
}
DelayNS(10);
IOSET = FL1;
DelayNS(10);
FlashBusy =0;
return(1);
}
/***********************************/
uint8 write_at45db041b_busy(void)
{
uint8 value1;
value1=Read_Sta_AT45();
if(value1<0x80)
{
return(0);//write busy
}
else
{
return(1);//write not busy
}
}
/***********************************/
/***********************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -