⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sst25lf080.c

📁 STM32 单片机例程
💻 C
📖 第 1 页 / 共 2 页
字号:
/********************************************************************/
#include "..\main\include.h"
//#include "include.h"
//本程序是SST25LF080A,SST25VF080A驱动,由于两者高速字节编程不兼容,所以
//采用普通字节编程,多字节编程效率将略微低些,实际并不明显
//sbit	CE_SPI          =P1^3;   //SPI控制
//#define LockSPI()    WP = 0  //写保护
//#define UnLockSPI()  WP = 1  //关闭写保护

//#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     //使能写状态寄存器命令
#define  RDSR_CMD           0x05     //读状态寄存器命令
#define  WRSR_CMD           0x01     //写状态寄存器命令
#define  WREN_CMD           0x06     //写使能命令
#define  WRDI_CMD           0x04     //写禁止命令
#define  READ_CMD           0x03     //读数据命令
#define  HIGH_SPEED_READ    0x0B     //高速数据读(地址自动增加)命令
#define  CHIP_ERASE_CMD     0x60     //芯片擦除命令
#define  BLOCK_ERASE_CMD    0x52     //块擦除命令
#define  SECTOR_ERASE_CMD   0x20     //扇区擦除命令
#define  AAI_PROGRAM_CMD    0xAF     //SST25LF080A字节编程(地址自动增加)命令
//#define  AAI_PROGRAM_CMD    0xAD     //SST25VF080A字节编程(地址自动增加)命令
#define  BYTE_PROGRAM_CMD   0x02     //字节编程命令  
#define  READ_ID            0x90     //读器件ID
#define  READ_ID2           0xAB     //读器件ID(功能完全同READ_ID)



//SPI发送低3字节地址
//#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存储器标志
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 状态寄存器     
* Input:                
* None
* Returns:                
状态              
************************************************************************/
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
使能写状态寄存器
************************************************************************/
static void EnableWRReg(void)
{
  EnableSPI();
  
  SPI_Send_Byte( EWSR_CMD ); //0x50 enable writing to the status register
  
  DisableSPI();
}

/************************************************************************
WriteSReg
写1byte到状态寄存器
dat--数据
************************************************************************/
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();
}

//开保护
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数据
 Input--地址(3byte)
   Dst:  Destination Address 000000H - 0FFFFFH
 Returns:--数据
************************************************************************/
/*
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
连续读数据
Input:
  Address--读地址 000000H - 0FFFFFH
  Buf--读出数据缓冲 
  n--读数量 (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数据
Address--地址
返回--数据
************************************************************************/
/*
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,发送任意数都可以
  dat = SPI_Send_Byte(0xff);
  
  DisableSPI();
  return dat;
}
*/

/************************************************************************
HighSpeed_Read_MultiByte
高速连续读数据
Address--地址
Buf--数据
n--数据量
************************************************************************/
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 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -