📄 spi.c
字号:
/***************************************************************************
Project : Car two ting dvd System
Compiler : ST7 HiCross C (HiWARE)
Module : autos.c
Version : V 1.0
Created : 24 11, 2005
Author : tomi / SA
Description
- Main Program of the car two ting dvd System.
Modified
- tomi
***************************************************************************/
#include "SPI.h"
#include "Libr.h"
#include "St72321.h"
#include "IOport.h"
#include "Sys_config.h"
void SPI_HostWriteByte(unsigned char wByte);
unsigned char SPI_HostReadByte(void);
unsigned char AT45DB041B_StatusRegisterRead(void);
void AT45DB041B_ContinuousArrayRead(unsigned int PA,unsigned int BFA,unsigned char *pHeader,unsigned int len);
void AT45DB041B_BufferWrite(unsigned char buffer,unsigned int BFA,unsigned char *pHeader,unsigned int len);
void AT45DB041B_BufferToMainMemoryPageProgramWithBuilt_inErase(unsigned char buffer,unsigned int PA,unsigned int BFA,unsigned char *pHeader,unsigned int len);
void AT45DB041B_BufferToMainMemoryPageProgramWithoutBuilt_inErase(unsigned char buffer,unsigned int PA,unsigned int BFA,unsigned char *pHeader,unsigned int len);
void SPI_HostWriteByte(unsigned char wByte)
{
unsigned char i,j;
for(i=0;i<8;i++)
{
if((wByte<<i)&0x80)
Set_SPI_SI_high;
else
Set_SPI_SI_low;
Set_SPI_CLK_low;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
Set_SPI_CLK_high;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
}
}
unsigned char SPI_HostReadByte(void)
{
unsigned char i,j,rByte=0;
for(i=0;i<8;i++)
{
Set_SPI_CLK_low;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
Set_SPI_CLK_high;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
rByte<<=1;
if(IS_SPI_SO_high)
rByte|=1;
}
return rByte;
}
unsigned char AT45DB041B_StatusRegisterRead(void)
{
unsigned char R_data;
Set_SPI_CS_low;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
SPI_HostWriteByte(0x57);
R_data=SPI_HostReadByte();
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
Set_SPI_CS_high;
return R_data;
}
/******************************************************************************/
/*描述: */
/* When the last bit in the main memory array has been read,the device will*/
/* continue reading back at the beginning of the first page of memory.As w-*/
/* ith crossing over page boundaries,no delays will be incurred when wrapp-*/
/* ing around from the end of the array to the beginning of the array. */
/*参数: */
/* PA - 页地址,0~2047 */
/* BFA - 指定BUFFER中的起始写入地址 */
/* pHeader - 指定数据的首地址 */
/* len - 指定数据的长度 */
/******************************************************************************/
void AT45DB041B_ContinuousArrayRead(unsigned int PA,unsigned int BFA,unsigned char *pHeader,unsigned int len)
{
unsigned int i=0;
while(i++<255)
{if(AT45DB041B_StatusRegisterRead()&0x80)
{break;}
}
Set_SPI_CS_low;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
SPI_HostWriteByte(0xe8);
SPI_HostWriteByte((unsigned char)(PA>>7));
SPI_HostWriteByte((unsigned char)((PA<<1)|(BFA>>8)));
SPI_HostWriteByte((unsigned char)BFA);
for(i=0;i<4;i++){SPI_HostWriteByte(0x00);}
for(i=0;i<len;i++)
pHeader[i]=SPI_HostReadByte();
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
Set_SPI_CS_high;
}
/******************************************************************************/
/*描述: */
/* 将指定数据写入从某个地址(0~263)开始的BUFFER中。 */
/*参数: */
/* buffer - 选择BUFFER,01H选择BUFFER 1,02H选择BUFFER 2 */
/* 在该指令序列中,操作码84H选择BUFFER 1,87H选择BUFFER 2 */
/* BFA - BUFFER中的起始地址,0~263 */
/* pHeader - 待存数据的头指针 */
/* len - 待存数据的长度1~264 */
/******************************************************************************/
void AT45DB041B_BufferWrite(unsigned char buffer,unsigned int BFA,unsigned char *pHeader,unsigned int len)
{
unsigned int i=0;
while(i++<255)
{if(AT45DB041B_StatusRegisterRead()&0x80)
{break;}
}
Set_SPI_CS_low;
switch(buffer)
{
case 1:SPI_HostWriteByte(0x84);break;
case 2:SPI_HostWriteByte(0x87);break;
}
SPI_HostWriteByte(0x00);
SPI_HostWriteByte((unsigned char)(BFA>>8));
SPI_HostWriteByte((unsigned char)BFA);
for(i=0;i<len;i++)
SPI_HostWriteByte(pHeader[i]);
Set_SPI_CS_high;
}
/******************************************************************************/
/*描述: */
/* 将指定数据写入从某个地址(0~263)开始的页中:包含2个动作,首先将指定数据*/
/* 写入到BUFFER 1或者BUFFER 2中,其中可以指定BUFFER中的起始写入地址,此写入*/
/* 动作不影响BUFFER中其它地址中的数据,然后再将BUFFER中的整个数据写入到某指*/
/* 定页中(带预擦除)。 */
/*参数: */
/* buffer - 选择BUFFER,01H选择BUFFER 1,02H选择BUFFER 2 */
/* PA - 页地址,0~2047 */
/* BFA - 指定BUFFER中的起始写入地址 */
/* pHeader - 指定数据的首地址 */
/* len - 指定数据的长度 */
/******************************************************************************/
void AT45DB041B_BufferToMainMemoryPageProgramWithBuilt_inErase(unsigned char buffer,unsigned int PA,unsigned int BFA,unsigned char *pHeader,unsigned int len)
{
unsigned int i;
AT45DB041B_BufferWrite(buffer,BFA,pHeader,len);
while(i++<1000)
{if(AT45DB041B_StatusRegisterRead()&0x80)
{break;}
}
Set_SPI_CS_low;
switch(buffer)
{
case 1:SPI_HostWriteByte(0x83);break;
case 2:SPI_HostWriteByte(0x86);break;
}
SPI_HostWriteByte((unsigned char)(PA>>7));
SPI_HostWriteByte((unsigned char)(PA<<1));
SPI_HostWriteByte(0x00);
Set_SPI_CS_high;
}
/******************************************************************************/
/*描述: */
/* 与上一个函数的唯一区别是不带预擦除。 */
/******************************************************************************/
void AT45DB041B_BufferToMainMemoryPageProgramWithoutBuilt_inErase(unsigned char buffer,unsigned int PA,unsigned int BFA,unsigned char *pHeader,unsigned int len)
{
unsigned int i=0;
AT45DB041B_BufferWrite(buffer,BFA,pHeader,len);
while(i++<1000)
{if(AT45DB041B_StatusRegisterRead()&0x80)
{break;}
}
Set_SPI_CS_low;
SPI_HostWriteByte(0x88+buffer);
SPI_HostWriteByte((unsigned char)(PA>>7));
SPI_HostWriteByte((unsigned char)(PA<<1));
SPI_HostWriteByte(0x00);
for(i=0;i<len;i++)
SPI_HostWriteByte(pHeader[i]);
Set_SPI_CS_high;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -