文档库 最新最全的文档下载
当前位置:文档库 › SPI_Flash_SST25V

SPI_Flash_SST25V

main.c:
#include "stm32f10x_lib.h"
#include "hw_conf.h"
#include"SST25V.h"

void delay(int d);

u8 Tx_Buffer[] = {0x72,0x62,0x02,0x78,0x60,0x96,0x86,0x79,0x85,0x24,0x36,0x48};
u8 Rx_Buffer[BufferSize];
u32 DataByte=0;

int main(void)
{
#ifdef DEBUG
debug();
#endif

Setup_System();

SST25V_Init();

SST25V_ChipErase();

//SST25V_ByteWrite(0x72, 0x01F000);

//SST25V_ByteWrite(0x62, 0x01F001);

//SST25V_ByteWrite(0x02, 0x01F002);

//SST25V_ByteWrite(0x78, 0x01F003);


AutoAddressIncrement_WordProgramA(0x72, 0x62, 0x01F000);
AutoAddressIncrement_WordProgramB(0,0x02, 0x78);
AutoAddressIncrement_WordProgramB(1,0x60, 0x96);

SST25V_HighSpeedBufferRead(Rx_Buffer,0x01F000,6);

//SST25V_SectorErase_4KByte(0x01F000);
//SST25V_BlockErase_32KByte(0x01F000);
//SST25V_BlockErase_64KByte(0x01F000);



//DataByte = SST25V_ReadStatusRegister();

//DataByte = SST25V_ReadJedecID();
DataByte = SST25V_ReadManuID_DeviceID(0);


//SST25V_HighSpeedBufferRead(Rx_Buffer,0x01F000,4);


while (1)
{
if(Rx_Buffer[0]==0x72)
{
GPIO_WriteBit(GPIOC,GPIO_Pin_6,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_6)));
delay(100);
}

if(Rx_Buffer[1]==0x62)
{
GPIO_WriteBit(GPIOC,GPIO_Pin_7,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_7)));
delay(100);
}

if(Rx_Buffer[2]==0x02)
{
GPIO_WriteBit(GPIOC,GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_4)));
delay(100);
}

if(Rx_Buffer[3]==0x78)
{
GPIO_WriteBit(GPIOC,GPIO_Pin_5,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_5)));
delay(100);
}

}
}


void delay(int d)
{
int i = 0;
for ( ;d;--d)
for (i = 0;i<10000;i++);
}

SST25V.C:
#include "stm32f10x_spi.h"
#include "SST25V.h"
#define SPI_FLASH_PageSize 256

void Delay(u8 d)
{
int i = 0;
for ( ;d;--d)
for (i = 0;i<1000;i++);
}

void SST25V_Init(void)
{
SST25V_CS_HIGH();
SST25V_WP_HIGH();
SST25V_HOLD_HIGH();
SST25V_WriteStatusRegister(0x02);
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(u8* pBuffer, u32 ReadAddr, 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(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
{
S

ST25V_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_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
SPI_SendData(SPI1, byte);

while(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);
return SPI_ReceiveData(SPI1);
}

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();
}

void AutoAddressIncrement_WordProgramA(u8 Byte1, u8 Byte2, u32 Addr)
{
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(AAI_WordProgram);
SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);
SPI_Flash_SendByte((Addr & 0xFF00) >> 8);
SPI_Flash_SendByte(Addr & 0xFF);

SPI_Flash_SendByte(Byte1);
SPI_Flash_SendByte(Byte2);

//Delay(10);

SST25V_CS_HIGH();
//SST25V_Wait_Busy_AAI();
SST25V_WaitForWriteEnd();
}


void AutoAddressIncrement_WordProgramB(u8 state,u8 Byte1, u8 Byte2)
{
//SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(AAI_WordProgram);

SPI_Flash_SendByte(Byte1);
SPI_Flash_SendByte(Byte2);

SST25V_CS_HIGH();
//SST25V_Wait_Busy_AAI();
SST25V_WaitForWriteEnd();

if(state==1)
{
SST25V_WriteDisable();
}
}


void SST25V_Wait_Busy_AAI(void)
{
while (SST25V_ReadStatusRegister() == 0x43) /* 等待空闲 */
SST25V_ReadStatusRegister();
}


void SST25V_SectorErase_4KByte(u32 Addr)
{
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();
}

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(

);
}

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();
}

void SST25V_ChipErase(void)
{
SST25V_WriteEnable();
SST25V_CS_LOW();
SPI_Flash_SendByte(ChipErace);
SST25V_CS_HIGH();
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) == SET);

SST25V_CS_HIGH();
}

u32 SST25V_ReadJedecID(void)
{
u32 JEDECID = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
SST25V_CS_LOW();
SPI_Flash_SendByte(ReadJedec_ID);
Temp0 = SPI_Flash_ReceiveByte();
Temp1 = SPI_Flash_ReceiveByte();
Temp2 = SPI_Flash_ReceiveByte();
SST25V_CS_HIGH();
JEDECID = (Temp0 << 16) | (Temp1 << 8) | Temp2;
return JEDECID;
}

u16 SST25V_ReadManuID_DeviceID(u32 ReadManu_DeviceID_Addr)
{
u16 ManuID_DeviceID = 0;
u8 ManufacturerID = 0, DeviceID = 0;
SST25V_CS_LOW();
SPI_Flash_SendByte(ReadDeviceID);

SPI_Flash_SendByte((ReadManu_DeviceID_Addr & 0xFF0000) >> 16);
SPI_Flash_SendByte((ReadManu_DeviceID_Addr & 0xFF00) >> 8);
SPI_Flash_SendByte(ReadManu_DeviceID_Addr & 0xFF);

if(ReadManu_DeviceID_Addr==1)
{
DeviceID = SPI_Flash_ReceiveByte();
ManufacturerID = SPI_Flash_ReceiveByte();
}
else
{
ManufacturerID = SPI_Flash_ReceiveByte();
DeviceID = SPI_Flash_ReceiveByte();
}

ManuID_DeviceID = ((ManufacturerID<<8) | DeviceID);
SST25V_CS_HIGH();

return ManuID_DeviceID;
}

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

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

SST25V.H:
#include "stm32f10x_lib.h"
#ifndef __SST25V_H
#define __SST25V_H

#define BufferSize (countof(Tx_Buffer)-1)
#d

efine countof(a) (sizeof(a) / sizeof(*(a)))
#define SST25V_PageSize 256
#define Dummy_Byte 0xA5

#define SST25V_CS_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
#define SST25V_CS_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_4)

#define SST25V_WP_LOW() GPIO_ResetBits(GPIOC, GPIO_Pin_0)
#define SST25V_WP_HIGH() GPIO_SetBits(GPIOC, GPIO_Pin_0)

#define SST25V_HOLD_LOW() GPIO_ResetBits(GPIOC, GPIO_Pin_1)
#define SST25V_HOLD_HIGH() GPIO_SetBits(GPIOC, GPIO_Pin_1)

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

#define Byte_Program 0x02 //页面编程--写数据
#define AAI_WordProgram 0xAD
#define ReadStatusRegister 0x05 //读状态寄存器
#define EnableWriteStatusRegister 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


void Delay(u8 d);
void SST25V_Init(void);
u8 SST25V_ByteRead(u32 ReadAddr);
void SST25V_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead);
void SST25V_HighSpeedBufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead);
u8 SST25V_HighSpeedRead(u32 ReadAddr);

u8 SPI_Flash_SendByte(u8 byte);
u8 SPI_Flash_ReceiveByte(void);
void SST25V_ByteWrite(u8 Byte, u32 WriteAddr);
void AutoAddressIncrement_WordProgramA(u8 Byte1, u8 Byte2, u32 Addr);
void AutoAddressIncrement_WordProgramB(u8 state,u8 Byte1, u8 Byte2) ;

void SST25V_Wait_Busy_AAI(void);
void SST25V_SectorErase_4KByte(u32 Addr);
void SST25V_BlockErase_32KByte(u32 Addr);
void SST25V_BlockErase_64KByte(u32 Addr);
void SST25V_ChipErase(void);

u8 SST25V_ReadStatusRegister(void);
void SST25V_WriteEnable(void);
void SST25V_WriteDisable(void);

void SST25V_EnableWriteStatusRegister(void);
void SST25V_WriteStatusRegister(u8 Byte);
void SST25V_WaitForWriteEnd(void);

u32 SST25V_ReadJedecID(void);

u16 SST25V_ReadManuID_DeviceID(u32 ReadManu_DeviceID_Addr);

void SST25V_EBSY();
void SST25V_DBSY();


#endif

hw_conf.c:
#include"stm32f10x_lib.h"
#include "hw_conf.h"
ErrorStatus HSEStartUpStatus;
void RCC_Configuration(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus

= RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_3);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
}

void GPIO_Configuration(void)
{
//PC4567分别接LED4,3,2,1,以便调试观察
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_5 |GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);

//PA5--SPI1_SCK PA6--SPI1--MISO PA7--SPI1--MOSI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; //Configure SPI1 pins: NSS, SCK, MISO and MOSI
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//PA4--SPI1_NSS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //Configure PA.4 as Output push-pull, used as Flash Chip select
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void SPI_configuration() //SPI1 configuration
{
SPI_InitTypeDef SPI_InitStructure;

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //SPI设置为双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置为主 SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //SPI发送接收 8 位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //时钟悬空高
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //数据捕获于第二个时钟沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //内部 NSS 信号有 SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //波特率预分频值为 4
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据传输从 MSB 位开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //定义了用于 CRC值计算的多项式 7
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE); //Enable SPI1
}

void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#en

dif
}

void Setup_System(void)
{
RCC_Configuration();
GPIO_Configuration();
SPI_configuration();
NVIC_Configuration();
}
hw_conf.h:
#ifndef _hw_conf_H_
#define _hw_conf_H_

extern void Setup_System(void);

#endif

相关文档