📄 c8051f330_flash_spi_20071213.c
字号:
/* DataArray Array storing read data */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Read_Cont(unsigned long Dst, unsigned int no_bytes, unsigned char *DataArray)
{
unsigned int i = 0;
Select_Serial_Memory; /* enable device */
SendSPIByte(0x03); /* read command */
SendSPIByte(((Dst) >> 16)); /* send 3 address bytes */
SendSPIByte(((Dst) >> 8));
SendSPIByte(Dst&0xFF);
for (i = 0; i < no_bytes; i++) /* read until no_bytes is reached */
{
*(DataArray+i) = GetSPIByte(); /* receive byte and store in DataArray */
}
Deselect_Serial_Memory; /* disable device */
}
/*******************************************************************************/
/* PROCEDURE: Byte_Program */
/* */
/* 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 - 07FFFFH */
/* byte: byte to be programmed */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Byte_Program(unsigned long Dst, unsigned char byte)
{
WREN();
Select_Serial_Memory; /* enable device */
SendSPIByte(0x02); /* send Byte Program command */
SendSPIByte(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
SendSPIByte(((Dst & 0xFFFF) >> 8));
SendSPIByte(Dst & 0xFF);
SendSPIByte(byte); /* send byte to be programmed */
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: Auto_Add_IncA */
/* */
/* This procedure programs consecutive addresses of */
/* the device. This is used to start the AAI process. */
/* It should be followed by Auto_Add_IncB. */
/* Assumption: Address being programmed is already */
/* erased and is NOT block protected. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* byte: byte to be programmed */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Auto_Add_IncA(unsigned long Dst, unsigned char byte1, unsigned char byte2)
{
WREN();
Select_Serial_Memory; /* enable device */
SendSPIByte(0xAD); /* send AAI command */
SendSPIByte(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
SendSPIByte(((Dst & 0xFFFF) >> 8));
SendSPIByte(Dst & 0xFF);
SendSPIByte(byte1); /* send byte to be programmed */
SendSPIByte(byte2); /* send byte to be programmed */
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: Auto_Add_IncB */
/* */
/* This procedure programs consecutive addresses of */
/* the device. This is used after Auto_Address_IncA. */
/* Assumption: Address being programmed is already */
/* erased and is NOT block protected. */
/* */
/* Input: byte: byte to be programmed */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Auto_Add_IncB(unsigned char byte1,unsigned char byte2)
{
Select_Serial_Memory; /* enable device */
SendSPIByte(0xAD); /* send AAI command */
SendSPIByte(byte1); /* send byte to be programmed */
SendSPIByte(byte2); /* send byte to be programmed */
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: Chip_Erase */
/* */
/* This procedure erases the entire Chip. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Chip_Erase(void)
{
WREN();
Select_Serial_Memory; /* enable device */
SendSPIByte(0x60); /* send Chip Erase command */
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: Sector_Erase */
/* */
/* This procedure Sector Erases the Chip. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Sector_Erase(unsigned long Dst)
{
WREN();
Select_Serial_Memory; /* enable device */
SendSPIByte(0x20); /* send Sector Erase command */
SendSPIByte(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
SendSPIByte(((Dst & 0xFFFF) >> 8));
SendSPIByte(Dst & 0xFF);
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: Block_Erase */
/* */
/* This procedure Block Erases the Chip. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Block_Erase(unsigned long Dst)
{
WREN();
Select_Serial_Memory; /* enable device */
SendSPIByte(0x52); /* send Block Erase command */
SendSPIByte(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
SendSPIByte(((Dst & 0xFFFF) >> 8));
SendSPIByte(Dst & 0xFF);
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*********************************************************************************************
PUT CHAR ARRAY
*********************************************************************************************/
/*! \brief Write one or multiple bytes to the Serial SPI memory
*
* This function check the validity of the first byte address location (out of range or write protected area).
* A read access to the serial SPI memory is performed to get the status register value. During this access the interrupt is disabled.
* As from the first SPI write access is started, the function returns.
* The management of the next SPI accesses and the write access completion are performed by the SPI interrupt handler.
*
* \note No page roll-over control is performed.
*
* \param start_add : destination address of the first byte
* \param nb_of_byte : number of bytes to be written decremented by one (this is for compatibility reason for devices with 256 bytes page size)
* \param source : pointer to the write buffer location
* \return AccessStatus.
* \retval TRANSFER_STARTED : the write sequence is started without error.
* \retval OUT_OF_RANGE : the address is out of range of available memory.
* \retval BUSY : the SPI memory or the SPI interface is busy.
* \retval DATA_WR_PROTECTED : the address of the last byte to be written matches a write protected location.
*********************************************************************************************/
void PutCharArray(unsigned long StartAddr, unsigned int SendBytes, unsigned char* SourceAddr)
{
U16 byteread_cnt;
U8 * TempPtr ;
TempPtr= SourceAddr ;
WREN();
Auto_Add_IncA(StartAddr, (char)(*(TempPtr)), (char)(*(TempPtr+1)));
for(byteread_cnt=2; byteread_cnt < SendBytes; byteread_cnt+=2)
{
Auto_Add_IncB(*(TempPtr+byteread_cnt),*(TempPtr+byteread_cnt+1));
}
WRDI();
}
/*******************************************************************************/
/* PROCEDURE: HW_SPI_Init */
/* */
/* This procedure initializes the hardware SPI on the MCU. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void SST_SPI_Init(void )
{
Init_Device();
WREN();
EWSR();
WRSR(0x00);
}
//*******************************************************************************/
void main(void)
{
unsigned char data1[3];
unsigned char rdata[10];
unsigned char wdata[10]={0,1,2,3,4,5,6,7,8,9};
WP=1;
SST_SPI_Init();
//***************擦除整个芯片********************
Chip_Erase();
//***************读状态寄存器,读芯片ID**********
data1[0]=Read_Status_Register();
data1[1]= Read_ID(0x01);
//****************读写单个字节*******************
//Byte_Program(0x02,0x03);
//data1[2]=Read(0x02);
//****************写地址自动增加-****************
PutCharArray(0x00, 10, &wdata);
Read_Cont(0x00,10,&rdata);
//*****************连续读******************
Read_Cont(0x00,10,&rdata);
//***********************************
//Sector_Erase(0x00);
Block_Erase(0x00);
Read_Cont(0x00,10,&rdata);
//***********************************
data1[1]= Read_ID(0x01);
}
//***********************************End of Code*********************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -