×
单片机 > 单片机程序设计 > 详情

SST25VF016B串行flash驱动程序基于STM32F10x

发布时间:2020-05-18 发布时间:
|
#define Dummy_Byte                    0xff

#define Read_Data                     0x03       //读取存储器数据
#define HighSpeedReadData             0x0B       //快速读取存储器数据
#define SectorErace_4KB               0x20       //扇区擦除
#define BlockErace_32KB               0x52       //32KB块擦除
#define BlockErace_64KB               0xD8       //64KB块擦除
#define ChipErace                     0x60       //片擦除

#define Byte_Program                  0x02       //页面编程--写数据
#define AAI_WordProgram               0xAD
#define ReadStatusRegister            0x05       //读状态寄存器
#define EnableWriteStatusRegiste    0x50
#define WriteStatusRegister           0x01       //写状态寄存器

#define WriteEnable                   0x06       //写使能,设置状态寄存器
#define WriteDisable                  0x04       //写禁止
#define ReadDeviceID                  0xAB       //获取设备ID信息

#define ReadJedec_ID                  0x9F       //JEDEC的ID信息

#define EBSY                          0X70
#define DBSY                          0X80

 

 

 

//初始化SPI口
void SPI2init(void)
{
  SPI_InitTypeDef  SPI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;

  
  GPIO_InitStructure.GPIO_Pin = GPIO_SCK | GPIO_MISO | GPIO_MOSI;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  
  GPIO_InitStructure.GPIO_Pin = GPIO_CS;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIO_PORT_CS, &GPIO_InitStructure);

  
  SST25V_CS_HIGH();

  
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI2, &SPI_InitStructure);

  
  SPI_Cmd(SPI2, ENABLE);
}

//flash初始化
void SST25V_Init(void)
{
 {
   GPIO_InitTypeDef GPIO_InitStructure;
  
   
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_Init(GPIOD, &GPIO_InitStructure);
//    GPIO_ResetBits(GPIOD, GPIO_Pin_15);    //关闭Flash电源
//    Delay(10);  //延时10mS
   GPIO_SetBits(GPIOD, GPIO_Pin_15);    //打开Flash电源
   //GPIO_ResetBits(GPIOC, GPIO_Pin_8);
 }
 SPI2init();
  SST25V_CS_HIGH();
  SST25V_EnableWriteStatusRegister();      //使能写状态寄存器  
  SST25V_WriteStatusRegister(0);             //写状态寄存器  设置 BPL BP3 BP2 BP1 BP0  0 0 0 0 0 
  SST25V_DBSY();
}

//读字节,参数:地址
u8 SST25V_ByteRead(u32 ReadAddr)
{
  u8 Temp = 0;
  SST25V_CS_LOW();
  SPI_Flash_SendByte(Read_Data);
  SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);
  SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);
  SPI_Flash_SendByte(ReadAddr & 0xFF);
 
  Temp = SPI_Flash_ReceiveByte();
  SST25V_CS_HIGH();
  return Temp;
}

//读字节串,参数:起始地址、读入缓冲、读取长度
void SST25V_BufferRead(u32 ReadAddr, u8* pBuffer, u16 NumByteToRead)
{
  SST25V_CS_LOW();
  SPI_Flash_SendByte(Read_Data);
  SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);
  SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);
  SPI_Flash_SendByte(ReadAddr & 0xFF);

  while(NumByteToRead--)
  {
    *pBuffer = SPI_Flash_ReceiveByte();
    pBuffer++;
  }
  SST25V_CS_HIGH();
}

//高速读字节串,参数:起始地址、读入缓冲、读取长度
void SST25V_HighSpeedBufferRead( u32 ReadAddr,u8* pBuffer, u16 NumByteToRead)
{
  SST25V_CS_LOW();
  SPI_Flash_SendByte(HighSpeedReadData);
  SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);
  SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);
  SPI_Flash_SendByte(ReadAddr & 0xFF);
  SPI_Flash_SendByte(Dummy_Byte);

  while(NumByteToRead--)
  {
    *pBuffer = SPI_Flash_ReceiveByte();
    pBuffer++;
  }
  SST25V_CS_HIGH();
}

//读字节,参数:地址
u8 SST25V_HighSpeedRead(u32 ReadAddr)
{
  u32 Temp = 0;
  SST25V_CS_LOW();
  SPI_Flash_SendByte(HighSpeedReadData);
  SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);
  SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);
  SPI_Flash_SendByte(ReadAddr & 0xFF);
  SPI_Flash_SendByte(Dummy_Byte);
  Temp = SPI_Flash_ReceiveByte();
  SST25V_CS_HIGH();
  return Temp;
}


u8 SPI_Flash_SendByte(u8 byte)
{
  while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
  SPI_I2S_SendData(SPI2, byte);

  while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
  return SPI_I2S_ReceiveData(SPI2);
}

u8 SPI_Flash_ReceiveByte(void)
{
  return (SPI_Flash_SendByte(Dummy_Byte));
}

//写字节,参数:待写字节、写入地址
void SST25V_ByteWrite(u8 Byte, u32 WriteAddr)
{
  SST25V_WriteEnable();
  SST25V_CS_LOW();
  SPI_Flash_SendByte(Byte_Program);
  SPI_Flash_SendByte((WriteAddr & 0xFF0000) >> 16);
  SPI_Flash_SendByte((WriteAddr & 0xFF00) >> 8);
  SPI_Flash_SendByte(WriteAddr & 0xFF);
  SPI_Flash_SendByte(Byte);
  SST25V_CS_HIGH();
  SST25V_WaitForWriteEnd();
  SST25V_WriteDisable();
  SST25V_WaitForWriteEnd();
}

////写字节串,参数:起始地址、待写数据、写入长度
void SST25V_BufferWrite(u32 WriteAddr,u8* pBuffer, u16 NumByteToWrite)
{
  u16 i,length;
  length=NumByteToWrite;
  if(length%2!=0)length++;  //不足双数补1

SST25V_Init();//
 
  SST25V_WriteEnable();
  SST25V_CS_LOW();
  SPI_Flash_SendByte(AAI_WordProgram);
  SPI_Flash_SendByte((WriteAddr & 0xFF0000) >> 16);
  SPI_Flash_SendByte((WriteAddr & 0xFF00) >> 8);
  SPI_Flash_SendByte(WriteAddr & 0xFF);

  SPI_Flash_SendByte(pBuffer[0]);
  SPI_Flash_SendByte(pBuffer[1]);

  SST25V_CS_HIGH();
  SST25V_Wait_Busy_AAI();
 
  for(i=2;i
  {
      SST25V_CS_LOW();
   SPI_Flash_SendByte(AAI_WordProgram);
   SPI_Flash_SendByte(pBuffer[i]);
   SPI_Flash_SendByte(pBuffer[i+1]);
   SST25V_CS_HIGH();
   SST25V_Wait_Busy_AAI();
  }
  SST25V_WriteDisable();
  SST25V_Wait_Busy_AAI();
}


void SST25V_Wait_Busy_AAI(void)
{
  while (SST25V_ReadStatusRegister() == 0x43)
  SST25V_ReadStatusRegister();
}

// 扇区擦除4Kbit,参数:地址
void SST25V_SectorErase_4KByte(u32 Addr)
{
SST25V_Init();//
  SST25V_WriteEnable();
  SST25V_CS_LOW();
  SPI_Flash_SendByte(SectorErace_4KB);
  SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);
  SPI_Flash_SendByte((Addr & 0xFF00) >> 8);
  SPI_Flash_SendByte(Addr & 0xFF);
  SST25V_CS_HIGH();
  SST25V_WaitForWriteEnd();
  SST25V_WriteDisable();
  SST25V_WaitForWriteEnd();
}


void SST25V_BlockErase_32KByte(u32 Addr)
{
  SST25V_WriteEnable();
  SST25V_CS_LOW();
  SPI_Flash_SendByte(BlockErace_32KB);
  SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);
  SPI_Flash_SendByte((Addr & 0xFF00) >> 8);
  SPI_Flash_SendByte(Addr & 0xFF); 
  SST25V_CS_HIGH();
  SST25V_WaitForWriteEnd();
  SST25V_WriteDisable();
  SST25V_WaitForWriteEnd();
 
}

void SST25V_BlockErase_64KByte(u32 Addr)
{
  SST25V_WriteEnable();
  SST25V_CS_LOW();
  SPI_Flash_SendByte(BlockErace_64KB);
  SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);
  SPI_Flash_SendByte((Addr & 0xFF00) >> 8);
  SPI_Flash_SendByte(Addr & 0xFF); 
  SST25V_CS_HIGH();
  SST25V_WaitForWriteEnd();
  SST25V_WriteDisable();
  SST25V_WaitForWriteEnd();
}

void SST25V_ChipErase(void)
{
 SST25V_Init();
  SST25V_WriteEnable();
  SST25V_CS_LOW();
  SPI_Flash_SendByte(ChipErace);
  SST25V_CS_HIGH();
  SST25V_WaitForWriteEnd();
  SST25V_WriteDisable();
  SST25V_WaitForWriteEnd();
}

u8 SST25V_ReadStatusRegister(void)
{
  u8 StatusRegister = 0;
  SST25V_CS_LOW();
  SPI_Flash_SendByte(ReadStatusRegister);
  StatusRegister = SPI_Flash_ReceiveByte();
  SST25V_CS_HIGH();
  return StatusRegister;
}

void SST25V_WriteEnable(void)
{
  SST25V_CS_LOW();
  SPI_Flash_SendByte(WriteEnable);
  SST25V_CS_HIGH();
}

void SST25V_WriteDisable(void)
{
  SST25V_CS_LOW();
  SPI_Flash_SendByte(WriteDisable);
  SST25V_CS_HIGH();
}

void SST25V_EnableWriteStatusRegister(void)
{
  SST25V_CS_LOW();
  SPI_Flash_SendByte(EnableWriteStatusRegister);
  SST25V_CS_HIGH();
}

void SST25V_WriteStatusRegister(u8 Byte)
{
  SST25V_EnableWriteStatusRegister();
  SST25V_CS_LOW();
  SPI_Flash_SendByte(WriteStatusRegister);
  SPI_Flash_SendByte(Byte);
  SST25V_CS_HIGH();
}

void SST25V_WaitForWriteEnd(void)
{
  u8 FLASH_Status = 0;
  SST25V_CS_LOW();
  SPI_Flash_SendByte(ReadStatusRegister);
  do
  {
    FLASH_Status = SPI_Flash_SendByte(Dummy_Byte);

  } while((FLASH_Status & WriteStatusRegister)!=0);

  SST25V_CS_HIGH();
}

void SST25V_EBSY(void)
{
  SST25V_CS_LOW();
  SPI_Flash_SendByte(EBSY);
  SST25V_CS_HIGH();
}

void SST25V_DBSY(void)
{
  SST25V_CS_LOW();
  SPI_Flash_SendByte(DBSY);
  SST25V_CS_HIGH();
}



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
51单片机中断源的扩展方法