📄 编辑2.c
字号:
#include "sst25.h"
#include <math.h>
#define MEMORY_TYPE_SST25VF080
#ifdef MEMORY_TYPE_SST25VF080
#define SPI_CS_MODEOUT PM7.6=0;
#define SPI_CS_MODEIN PM7.6=1;
#define SPI_CS_H P7.6=1;
#define SPI_CS_L P7.6=0;
#define SPI_DO_MODEOUT PM7.7=0;
#define SPI_DO_MODEIN PM7.7=1;
#define SPI_DO_H P7.7=1;
#define SPI_DO_PIN P7.7
#define SPI_DI_MODEOUT PM7.5=0;
#define SPI_DI_MODEIN PM7.5=1;
#define SPI_DI_H P7.5=1;
#define SPI_DI_L P7.5=0;
#define SPI_CK_MODEOUT PM7.4=0;
#define SPI_CK_MODEIN PM7.4=1;
#define SPI_CK_H P7.4=1;
#define SPI_CK_L P7.4=0;
#define SPIFLASH_CMD_AAIP 0xad
void Select_Serial_Memory()
{
SPI_CS_MODEOUT
SPI_CS_L
NOP();
}
void Deselect_Serial_Memory()
{
SPI_CS_MODEOUT
SPI_CS_H
NOP();
}
/*******************************************************************************/
/* PROCEDURE: EWSR */
/* */
/* This procedure enables the Write Status Register. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void EWSR(void )
{
Select_Serial_Memory();
SST_MasterIO(0x50); /* enuable writing to the stats register */
Deselect_Serial_Memory(); /* disable device */
}
/*******************************************************************************/
/* PROCEDURE: WRSR */
/* */
/* This procedure writes a byte to the Status Register. */
/* */
/* Input: data byte */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void WRSR(unsigned char byte)
{
Select_Serial_Memory();
SST_MasterIO(0x01); /* select write to status register */
SST_MasterIO(byte); /* data that will change the status of BPx or BPL
(only bits 2,3,7 can be written) */
Deselect_Serial_Memory(); /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: WREN */
/* */
/* This procedure enables the Write Enable Latch. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void WREN(void)
{
Select_Serial_Memory();
SST_MasterIO(0x06); /* send WREN command */
Deselect_Serial_Memory(); /* disable device */
}
/*******************************************************************************/
/* PROCEDURE: WRDI */
/* */
/* This procedure disables the Write Enable Latch. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void WRDI(void)
{
Select_Serial_Memory();
SST_MasterIO(0x04); /* send WRDI command */
Deselect_Serial_Memory(); /* disable device */
}
/*******************************************************************************/
/* PROCEDURE: Read_ID */
/* */
/* This procedure reads the manufacturer’s ID and device ID. */
/* It will use 90h as the command to read the ID. It is up to */
/* the user to give the last byte ID_addr to determine whether */
/* the device outputs manufacturer’s ID first, or device ID first. */
/* Review the data sheets for details. */
/* Returns ID in variable byte. */
/* */
/* Input: ID_addr */
/* */
/* Returns: byte: ID1 */
/* */
/*******************************************************************************/
unsigned char Read_ID(unsigned char ID_addr)
{
unsigned char byte;
Select_Serial_Memory();
SST_MasterIO(0x90); /* send read ID command */
SST_MasterIO(0x00); /* send address */
SST_MasterIO(0x00); /* send address */
SST_MasterIO(ID_addr); /* send address - either 00H or 01H */
byte = SST_MasterIO_Rece(); /* receive byte */
Deselect_Serial_Memory(); /* disable device */
return byte;
}
/*******************************************************************************/
/* PROCEDURE: Read */
/* */
/* This procedure reads one address of the device. */
/* It will return the byte read in variable byte. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* */
/* Returns: byte */
/* */
/*******************************************************************************/
unsigned char Read(unsigned long Dst)
{
unsigned char byte = 0;
Select_Serial_Memory();
SST_MasterIO(0x03); /* read command */
SST_MasterIO((unsigned char)(((Dst & 0xFFFFFF) >> 16))); /* send 3 address bytes */
SST_MasterIO((unsigned char)(((Dst & 0xFFFF) >> 8)));
SST_MasterIO((unsigned char)(Dst & 0xFF));
byte = SST_MasterIO_Rece();
Deselect_Serial_Memory(); /* disable device */
return byte; /* return one byte read */
}
/*******************************************************************************/
/* PROCEDURE: Read_Cont */
/* */
/* This procedure reads multiple consecutive addresses of */
/* the device and stores the data into DataArray. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* no_bytes Number of bytes to read */
/* 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 */
SST_MasterIO(0x03); /* read command */
SST_MasterIO((unsigned char)(((Dst) >> 16))); /* send 3 address bytes */
SST_MasterIO((unsigned char)(((Dst) >> 8)));
SST_MasterIO((unsigned char)(Dst&0xFF));
for (i = 0; i < no_bytes; i++) /* read until no_bytes is reached */
{
*(DataArray+i) = SST_MasterIO_Rece(); /* 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 */
SST_MasterIO(0x02); /* send Byte Program command */
SST_MasterIO((unsigned char)(((Dst & 0xFFFFFF) >> 16))); /* send 3 address bytes */
SST_MasterIO((unsigned char)(((Dst & 0xFFFF) >> 8)));
SST_MasterIO((unsigned char)(Dst & 0xFF));
SST_MasterIO(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 byte)
{
WREN();
Select_Serial_Memory(); /* enable device */
SST_MasterIO(0xAF); /* send AAI command */
SST_MasterIO((unsigned char)(((Dst & 0xFFFFFF) >> 16))); /* send 3 address bytes */
SST_MasterIO((unsigned char)(((Dst & 0xFFFF) >> 8)));
SST_MasterIO((unsigned char)(Dst & 0xFF));
SST_MasterIO(byte); /* 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 byte)
{
Select_Serial_Memory(); /* enable device */
SST_MasterIO(0xAF); /* send AAI command */
SST_MasterIO(byte); /* send byte to be programmed */
Deselect_Serial_Memory(); /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: Chip_Erase */
/* */
/* This procedure erases the entire Chip. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -