📄 sst25lf080.c
字号:
字节编程
This procedure programs one address of the device.
Assumption: Address being programmed is already erased and is NOT
block protected.
Input:
Dst: Destination Address 000000H - 0FFFFFH
byte: byte to be programmed
************************************************************************/
//*
static void Program_Byte( uint32 Address, uint8 dat )
{
EnableWrite(); //写使能
EnableSPI();
SPI_Send_Byte( BYTE_PROGRAM_CMD ); //0x02 send Byte Program command
SPI_Send32Address(Address); // send 3 address bytes
SPI_Send_Byte( dat ); // send byte to be programmed
DisableSPI();
Wait_Busy();
}
//*/
/************************************************************************
编程多个字节,使用地址自动增加命令
Address--地址
Buf--数据
n--数据量
************************************************************************/
static void Program_MultiByte( uint32 Address, uint16 n, uint8 *Buf )
{uint16 i;
if(n==0) return;
for(i=0; i<n; i++)
{
Program_Byte( Address+i, Buf[i]);
}
DisableWrite();
/*
EnableWrite(); //写使能
EnableSPI();
SPI_Send_Byte( AAI_PROGRAM_CMD ); //0xAF send AAI command
SPI_Send32Address(Address); // send 3 address bytes
SPI_Send_Byte( Buf[0] ); // send byte to be programmed
DisableSPI();
Wait_Busy_AAI();
for(i=1; i<n; i++)
{
EnableSPI();
SPI_Send_Byte( AAI_PROGRAM_CMD ); //0xAF send AAI command
SPI_Send_Byte( Buf[i] ); // send byte to be programmed
DisableSPI();
Wait_Busy_AAI();
}
//DisableWrite(),Read_Status_Register()表示地址自动增加编程命令结束
DisableWrite();
Read_Status_Register();
*/
}
/************************************************************************
Chip_Erase
芯片擦除
************************************************************************/
/*
void Chip_Erase(void)
{
EnableWrite(); //写使能
EnableSPI();
SPI_Send_Byte( CHIP_ERASE_CMD ); //0x60 send Block Erase command
DisableSPI();
Wait_Busy();
}
*/
/************************************************************************
Sector_Erase
扇区擦除(1个扇区4Kbyte)
Input:
Dst: Destination Address 000000H - 0FFFFFH
************************************************************************/
uint8 Sector_Erase( uint32 Address )
{
EnableWrite(); //写使能
EnableSPI();
SPI_Send_Byte( SECTOR_ERASE_CMD ); //0x20 send Block Erase command
SPI_Send32Address(Address); // send 3 address bytes
DisableSPI();
Wait_Busy();
return(0);
}
/************************************************************************
Block_Erase
块擦除(1个块64Kbyte)
Input:
Dst: Destination Address 000000H - 0FFFFFH
************************************************************************/
/*
void Block_Erase( uint32 Address )
{
EnableWrite(); //写使能
EnableSPI();
SPI_Send_Byte( BLOCK_ERASE_CMD ); //0x52 send Block Erase command
SPI_Send32Address(Address); // send 3 address bytes
DisableSPI();
Wait_Busy();
}
*/
/************************************************************************
* 函数原型:Wait_Busy()
* 函数功能:判断忙状态
* 输入参数:
* 输出参数:0:空闲, 非0:忙,超时失败推出
* 函数说明:被以下功能使用:
(Byte-Program, Sector-Erase, Block-Erase, Chip-Erase)
************************************************************************/
static uint8 Wait_Busy(void)
//{uint32 counter;
//
// counter=gTimer_1ms;
// while ((Read_Status_Register() & 0x03) == 0x03)
// {
// if( gTimer_1ms-counter > (100) )//100000us=100ms
// break;
// }
// return(0);
//}
/*
stm32 72MHz下的时间常数
时间常数由Read_Status_Register()决定
100 = 410us
1000 = 4.09ms
10000 = 40.1ms
20000 = 80.2ms
*/
{uint32 counter;
counter=0;
while ((Read_Status_Register() & 0x03) == 0x03)
{
if( ++counter > (20000) )//约80ms
break;
}
return(0);
}
/************************************************************************
* 函数原型:Wait_Busy_AAI()
* 函数功能:判断忙状态
* 输入参数:
* 输出参数:0:空闲, 非0:忙,超时失败推出
* 函数说明:被以下功能使用:
(Byte-Program, Sector-Erase, Block-Erase, Chip-Erase)
************************************************************************/
/*
static uint8 Wait_Busy_AAI(void)
{uint8 time;
time=10;
while ((Read_Status_Register() & 0x43) == 0x43)
{
if( (time=Timer_Over_Ms(time))==0 )
return(1); //超时退出
}
return(0);
}
*/
/************************************************************************
Verify
校验
Address--地址
Buf--数据
n--数据量
返回:0:相同,1:不相同
************************************************************************/
static uint8 Verify( uint32 Address, uint16 n, uint8 *Buf )
{uint16 i;
uint8 temp;
EnableSPI();
SPI_Send_Byte( HIGH_SPEED_READ ); //0x0B read command
SPI_Send32Address(Address); // send 3 address bytes
SPI_Send_Byte( 0xFF ); //dummy byte
for ( i = 0; i < n; i++ ) // read until no_bytes is reached
{
temp = SPI_Send_Byte(0xff); // receive byte and store at address 80H - FFH
if(Buf[i] != temp)
return(1);
}
DisableSPI();
return(0);
}
/************************************************************************
ProgramVerify_MultiByteApp
编程后校验
Address--地址
Buf--数据
n--数据量
返回:0:成功,1:失败
************************************************************************/
uint8 ProgramVerify_MultiByte( uint32 Address, uint16 n, uint8 *Buf )
{uint16 i;
for(i=0; i<3; i++)
{
Program_MultiByte( Address, n, Buf );
if( Verify( Address, n, Buf )==0 )
return(0);
}
return(1);
}
//如果写数据是扇区头自动擦除该扇区
uint8 Write_Flash_Page_FirstErase( uint32 Address, uint16 n, uint8 *Buf )
{//uint8 status;
if((Address%FLASH_SECTOR_SIZE)==0)//当移动到扇区头,擦除该扇区
Erase_Flash_Sector( Address );
return( Write_Flash_Page( Address, n, Buf ) );
}
/*
写flash, 不能写就先擦除再写
注意:不能跨扇区写
最大不能超过扇区最大字节数
*/
uint8 Force_Write_Flash_Page( uint32 Address, uint16 n, uint8 *Buf )
{//uint8 temp[FLASH_SECTOR_SIZE];
uint8* temp=gBigBuf;
uint8 status;
status =Read_Flash_Page(Address, n, temp);
if(status==0)
{
status=testFlashWrite( Buf, temp, n);
if(status==0)//可以直接写入
{
status=Write_Flash_Page( Address, n, Buf );
}
else//必须擦除再写
{
Read_Flash_Page(Address&FLASH_SECTOR_NOTMASK, FLASH_SECTOR_SIZE, temp);//从扇区首地址读1扇区
memcpy(temp+(Address&FLASH_SECTOR_MASK), Buf, n);//复制数据到扇区地址
Erase_Flash_Sector( Address );
status=Write_Flash_Page( Address&FLASH_SECTOR_NOTMASK, FLASH_SECTOR_SIZE, temp );//写整个扇区
}
}
return(status);
}
/*
测试数据不用擦除都是否可以写入flash, flash中原来是1->0可以写入,原来是0->1不可以写入
输入:WriteInfo--写入信息,FlashInfo--Flash中得数据
1001_0000 1111_0100, 可以写入
返回:
0=可以写入
1=不可以写入
*/
uint8 testFlashWrite( uint8 *WriteInfo, uint8 *FlashInfo, uint8 cmplen)
{
while( (cmplen--)>0 )
{
if( ((*WriteInfo)&(*FlashInfo)) != (*WriteInfo) )
return(1);
WriteInfo++;FlashInfo++;
}
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -