uint32 SPIInit( void )
{
S0SPCR = 0x00;
PINSEL0 &= 0xFFFF00FF;
PINSEL0 |= 0x00001500; //引脚定义...
IOSET = AT45DB_CS;
/* Setting SPI0 clock, for Atmel SEEPROM, SPI clock should be no more
than 3Mhz on 4.5V~5.5V, no more than 2.1Mhz on 2.7V~5.5V */
S0SPCCR = 0x8; //速度...
//#if INTERRUPT_MODE
// if ( install_irq( SPI0_INT, (void *)SPI0Handler ) == FALSE )
// {
// return (FALSE);
// }
/* 8 bit, CPOL=CPHA=0, master mode, MSB first, interrupt enabled */
// S0SPCR = SPI0_SPIE | SPI0_MSTR;
//#else
S0SPCR = SPI0_MSTR; //用查询方式 ...
//#endif
return( TRUE );
}
//*************************************************************************************************************
//-------------------------------------------------------
//---------------Buffer1_To_Write_AT45 (uint16 u16_page);....---u16_page为主存的页地址.------
//---------------SPI,转缓存1写入主存页中---------------
//-------------------------------------------------------
//*************************************************************************************************************
void Buffer1_To_Write_AT45 (uint16 u16_page)
{
uint8 i;
IOSET=WP;
IOSET = AT45DB_CS;
while(!(IOPIN&Rdy_busy));
for(i=0;i<10;i++);
IOCLR = AT45DB_CS;
for(i=0;i<10;i++);
S0SPDR = 0x83;
while(0 == (S0SPSR&0x80));
S0SPDR = (uint8)(u16_page>>5);
while(0 == (S0SPSR&0x80));
S0SPDR = ( (uint8)(u16_page))<<3;
while(0 == (S0SPSR&0x80));
S0SPDR = 0x00;
while(0 == (S0SPSR&0x80));
IOSET = AT45DB_CS;
for(i=0;i<10;i++);
while(!(IOPIN&Rdy_busy));
IOCLR=WP;
}
//*************************************************************************************************************
//-------------------------------------------------------
//---Write_Buffer1_AT45();.---u16_address为缓存1的起始地址,u16_Counter为要写入的字节数.store_data写入数据的起始字节地址....
//---------------SPI,写缓存1---------------
//-------------------------------------------------------
//*************************************************************************************************************
void Write_Buffer1_AT45 (uint16 u16_address,uint16 u16_Counter,uint8 *store_data)
{
uint16 u16_cnt;
uint8 i;
IOSET = AT45DB_CS;
for(i=0;i<10;i++);
IOCLR = AT45DB_CS;
for(i=0;i<10;i++);
S0SPDR = 0x84;
while(0 == (S0SPSR&0x80));
S0SPDR = 0x00;
while(0 == (S0SPSR&0x80));
S0SPDR = (uint8)(u16_address>>8);
while(0 == (S0SPSR&0x80));
S0SPDR = (uint8)(u16_address);
while(0 == (S0SPSR&0x80));
for(u16_cnt=0;u16_cnt<u16_Counter;u16_cnt++)
{
S0SPDR = *(store_data+u16_cnt);
while(0 == (S0SPSR&0x80));
}
IOSET = AT45DB_CS;
IOSET = AT45DB_CS;
while(!(IOPIN&Rdy_busy));
}
//*************************************************************************************************************
//-------------------------------------------------------
//---Main_to_Buffer1_AT45();.---u16_page为主存的页地址....
//---------------SPI,主存页的内容转到缓存1中---------------
//-------------------------------------------------------
//*************************************************************************************************************
void Main_to_Buffer1_AT45(uint16 u16_page)
{
uint8 i;
IOSET = AT45DB_CS;
while(!(IOPIN&Rdy_busy));
for(i=0;i<10;i++);
IOCLR = AT45DB_CS;
for(i=0;i<10;i++);
S0SPDR = 0x53;
while(0 == (S0SPSR&0x80));
S0SPDR = (uint8)(u16_page>>5);
while(0 == (S0SPSR&0x80));
S0SPDR = ( (uint8)(u16_page))<<3;
while(0 == (S0SPSR&0x80));
S0SPDR = 0x00;
while(0 == (S0SPSR&0x80));
IOSET = AT45DB_CS;
IOSET = AT45DB_CS;
while(!(IOPIN&Rdy_busy));
}
//------------------------------------------------------
//---------------------------------------
//*************************************************************************************************************
//-------------------------------------------------------
//---Reda_AT45_Continue();.---u16_page为主存的页地址.u16_address要读的数据的起始地址.u16_cnt要读的字节数.Store_temp存放数据的起始位置.
//---------------SPI,直接读主存的任意地址数据..---------------Reda_AT45_Continue MUST BE 'Read_AT45_Continu'haha....
//-------------------------------------------------------
//*************************************************************************************************************
//****************************************************************************/
uint8 SpiSendData(uint8 data)
{
// OS_ENTER_CRITICAL();
S0SPDR = data ;
while(0 == (S0SPSR & 0x80));
// 等待SPIF置位,即等待数据发送完毕
return(S0SPDR);
// OS_EXIT_CRITICAL();
}
//---------------------------------------
//*************************************************************************************************************
//-------------------------------------------------------
//---Reda_AT45_Continue();.---u16_page为主存的页地址.u16_address要读的数据的起始地址.u16_cnt要读的字节数.Store_temp存放数据的起始位置.
//---------------SPI,直接读主存的任意地址数据..---------------Reda_AT45_Continue MUST BE 'Read_AT45_Continu'haha....
//-------------------------------------------------------
//*************************************************************************************************************
void Reda_AT45_Continue(uint16 u16_page,uint16 u16_address,uint16 u16_cnt,uint8 *Store_temp)
{
uint16 i;
IOSET = AT45DB_CS;
while(!(IOPIN&Rdy_busy));
for(i=0;i<9;i++);
IOCLR = AT45DB_CS;
for(i=0;i<9;i++);
/* S0SPDR = 0xe8;
while(0 == (S0SPSR&0x80));
S0SPDR = (uint8)(u16_page>>5);
while(0 == (S0SPSR&0x80));
S0SPDR = (( (uint8)(u16_page))<<3)|( (uint8)(u16_address>>8));
while(0 == (S0SPSR&0x80));
S0SPDR = (uint8)(u16_address);
while(0 == (S0SPSR&0x80));
S0SPDR = 0x00;
while(0 == (S0SPSR&0x80));;
S0SPDR = 0x00;
while(0 == (S0SPSR&0x80));
S0SPDR = 0x00;
while(0 == (S0SPSR&0x80));
S0SPDR = 0x00;
while(0 == (S0SPSR&0x80)); */
SpiSendData(0xe8);
SpiSendData((uint8)(u16_page>>5));
SpiSendData((( (uint8)(u16_page))<<3)|( (uint8)(u16_address>>8)));
SpiSendData((uint8)(u16_address));
SpiSendData(0x00);
SpiSendData(0x00);
SpiSendData(0x00);
SpiSendData(0x00);
for(i=0;i<u16_cnt;i++)
{
S0SPDR = 0xFF;
while(0 == (S0SPSR&0x80));
*(Store_temp+i) = S0SPDR;
}
IOSET = AT45DB_CS;
}
//*************************************************************************************************************
//-------------------------------------------------------
//---------------Buffer2_To_Write_AT45 (uint16 u16_page);....---u16_page为主存的页地址.------
//---------------SPI,转缓存2写入主存页中---------------
//-------------------------------------------------------
//*************************************************************************************************************
void Buffer2_To_Write_AT45 (uint16 u16_page)
{
uint8 i;
IOSET=WP;
IOSET = AT45DB_CS;
while(!(IOPIN&Rdy_busy));
for(i=0;i<10;i++);
IOCLR = AT45DB_CS;
for(i=0;i<10;i++);
S0SPDR = 0x86;
while(0 == (S0SPSR&0x80));
S0SPDR = (uint8)(u16_page>>5);
while(0 == (S0SPSR&0x80));
- 1
- 2
- 3
- 4
前往页