?? sst25lf080.c
字號:
/********************************************************************/
#include "..\main\include.h"
//#include "include.h"
//本程序是SST25LF080A,SST25VF080A驅(qū)動,由于兩者高速字節(jié)編程不兼容,所以
//采用普通字節(jié)編程,多字節(jié)編程效率將略微低些,實(shí)際并不明顯
//sbit CE_SPI =P1^3; //SPI控制
//#define LockSPI() WP = 0 //寫保護(hù)
//#define UnLockSPI() WP = 1 //關(guān)閉寫保護(hù)
//#define EnableSPI() CE_SPI=0 //操作允許
//#define DisableSPI() CE_SPI=1 //操作禁止
//#define EnableSPI() SPI_FLASH_CS_LOW()
//#define DisableSPI() SPI_FLASH_CS_HIGH()
#define EnableSPI() SPI_FLASH_CS_LOW() //操作允許
#define DisableSPI() SPI_FLASH_CS_HIGH() //操作禁止
#define EWSR_CMD 0x50 //使能寫狀態(tài)寄存器命令
#define RDSR_CMD 0x05 //讀狀態(tài)寄存器命令
#define WRSR_CMD 0x01 //寫狀態(tài)寄存器命令
#define WREN_CMD 0x06 //寫使能命令
#define WRDI_CMD 0x04 //寫禁止命令
#define READ_CMD 0x03 //讀數(shù)據(jù)命令
#define HIGH_SPEED_READ 0x0B //高速數(shù)據(jù)讀(地址自動增加)命令
#define CHIP_ERASE_CMD 0x60 //芯片擦除命令
#define BLOCK_ERASE_CMD 0x52 //塊擦除命令
#define SECTOR_ERASE_CMD 0x20 //扇區(qū)擦除命令
#define AAI_PROGRAM_CMD 0xAF //SST25LF080A字節(jié)編程(地址自動增加)命令
//#define AAI_PROGRAM_CMD 0xAD //SST25VF080A字節(jié)編程(地址自動增加)命令
#define BYTE_PROGRAM_CMD 0x02 //字節(jié)編程命令
#define READ_ID 0x90 //讀器件ID
#define READ_ID2 0xAB //讀器件ID(功能完全同READ_ID)
//SPI發(fā)送低3字節(jié)地址
//#define SPI_Send32Address(Address) { \
//SPI_Send_Byte(HL_ByteOfLong(Address)); \
//SPI_Send_Byte(LH_ByteOfLong(Address)); \
//SPI_Send_Byte(LL_ByteOfLong(Address)); \
//}
#define SPI_Send32Address(Address) { \
SPI_Send_Byte((uint8)(Address>>16)); \
SPI_Send_Byte((uint8)((Address>>8)&0xff)); \
SPI_Send_Byte((uint8)(Address&0xff)); \
}
//定義FLASH存儲器標(biāo)志
uint8 FLASH_FLAG;
uint8 HighSpeed_Read_Byte( uint32 Address );
void Chip_Erase(void);
void Block_Erase( uint32 Address );
static uint8 Wait_Busy(void);
//static uint8 Wait_Busy_AAI(void);
/*
void SPI_Init()
{
//P1=0xff;
SPSR = SPSR & 0x7F;
SPCR = 0x50;
}
uint8 SPI_Send_Byte(uint8 dat)
{//uint8 temp;
SPDR = dat;
//do
//{
// temp = SPSR & 0x80;
//}while (temp != 0x80);
while((SPSR&0x80)^0x80);
SPSR = SPSR & 0x7F;
//BUZZ_ON();
return SPDR;
}
*/
/************************************************************************
* Read_Status_Register
* 讀SST25LF080 狀態(tài)寄存器
* Input:
* None
* Returns:
狀態(tài)
************************************************************************/
static uint8 Read_Status_Register(void)
{uint8 dat;
EnableSPI();
SPI_Send_Byte( RDSR_CMD ); // 0x05 send RDSR command
dat = SPI_Send_Byte(0xff); // receive byte
DisableSPI();
return dat;
}
/************************************************************************
EnableWRSReg
使能寫狀態(tài)寄存器
************************************************************************/
static void EnableWRReg(void)
{
EnableSPI();
SPI_Send_Byte( EWSR_CMD ); //0x50 enable writing to the status register
DisableSPI();
}
/************************************************************************
WriteSReg
寫1byte到狀態(tài)寄存器
dat--數(shù)據(jù)
************************************************************************/
static void WriteSReg( uint8 dat )
{
EnableSPI();
SPI_Send_Byte( WRSR_CMD ); //0x01 select write to status register
SPI_Send_Byte( dat ); //data that will change the status of BPx
//or BPL (only bits 2,3,7 can be written)
DisableSPI();
Wait_Busy();
}
/************************************************************************
EnableWrite
寫使能
************************************************************************/
static void EnableWrite()
{
EnableSPI();
SPI_Send_Byte( WREN_CMD ); //0x06 send WREN command
DisableSPI();
}
/************************************************************************
*DisableWrite()
寫禁止
************************************************************************/
static void DisableWrite()
{
EnableSPI();
SPI_Send_Byte( WRDI_CMD ); //0x04 send WRDI command
DisableSPI();
}
//開保護(hù)
void UnProtectSST25VF080(void)
{
EnableWRReg();
WriteSReg(0);
}
/************************************************************************
Read_ID
讀廠商/器件ID
Input--ID_addr(A7~A1 =0)
:Manufacturer’s ID is read with A0=0
:Device ID is read with A0=1.
Returns--dat
:ID1(Manufacture's ID = BFh
:Device ID = 80h(SST25LF080))
************************************************************************/
uint8 ReadDeviceID(void)//( uint8 ID_addr )
{uint8 dat;
EnableSPI();
SPI_Send_Byte( READ_ID ); //0x90 send read ID command (90h or ABh)
SPI_Send_Byte( 0x00 ); // send address
SPI_Send_Byte( 0x00 ); // send address
SPI_Send_Byte( 1 ); // send address - either 00H or 01H
dat = SPI_Send_Byte(0xff); // receive byte
DisableSPI();
return dat;
}
/************************************************************************
PROCEDURE: Read
讀1byte數(shù)據(jù)
Input--地址(3byte)
Dst: Destination Address 000000H - 0FFFFFH
Returns:--數(shù)據(jù)
************************************************************************/
/*
static uint8 Read_Byte( uint32 Address )
{uint8 dat;
EnableSPI();
SPI_Send_Byte( READ_CMD ); //0x03 read command
SPI_Send32Address(Address); // send 3 address bytes
dat = SPI_Send_Byte(0xff);
DisableSPI();
return dat; // return one byte read
}
*/
/************************************************************************
Read_Cont
連續(xù)讀數(shù)據(jù)
Input:
Address--讀地址 000000H - 0FFFFFH
Buf--讀出數(shù)據(jù)緩沖
n--讀數(shù)量 (max = 255)
************************************************************************/
/*
static uint8 Read_MultiByte( uint32 Address, uint8 n, uint8 *Buf )
{uint8 i;
EnableSPI();
SPI_Send_Byte( READ_CMD ); //0x03 read command
SPI_Send32Address(Address); // send 3 address bytes
for ( i = 0; i < n; i++ ) // read until no_bytes is reached
{
Buf[i] = SPI_Send_Byte(0xff); // receive byte and store at address 80H - FFH
}
DisableSPI();
return(0);
}
*/
/************************************************************************
HighSpeed_Read
高速讀1byte數(shù)據(jù)
Address--地址
返回--數(shù)據(jù)
************************************************************************/
/*
uint8 HighSpeed_Read_Byte( uint32 Address )
{uint8 dat = 0;
EnableSPI();
SPI_Send_Byte( HIGH_SPEED_READ ); //0x0B read command
SPI_Send32Address(Address); // send 3 address bytes
SPI_Send_Byte( 0xFF ); //dummy byte,發(fā)送任意數(shù)都可以
dat = SPI_Send_Byte(0xff);
DisableSPI();
return dat;
}
*/
/************************************************************************
HighSpeed_Read_MultiByte
高速連續(xù)讀數(shù)據(jù)
Address--地址
Buf--數(shù)據(jù)
n--數(shù)據(jù)量
************************************************************************/
uint8 HighSpeed_Read_MultiByte( uint32 Address, uint16 n, uint8 *Buf )
{uint16 i;
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
{
Buf[i] = SPI_Send_Byte(0xff); // receive byte and store at address 80H - FFH
}
DisableSPI();
return(0);
}
/************************************************************************
Byte_Program
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -