123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- #define THIS_FILE_ID 13
- #include "includes.h"
- #define sFLASH_CS_PIN GPIO_Pin_4
- #define sFLASH_CS_GPIO_PORT GPIOA
- #define sFLASH_CS_GPIO_CLK RCC_APB2Periph_GPIOA
- #define sFLASH_SPI SPI1
- #define sFLASH_SPI_CLK RCC_APB2Periph_SPI1
- #define sFLASH_SPI_SCK_PIN GPIO_Pin_5
- #define sFLASH_SPI_SCK_GPIO_PORT GPIOA
- #define sFLASH_SPI_SCK_GPIO_CLK RCC_APB2Periph_GPIOA
- #define sFLASH_SPI_MISO_PIN GPIO_Pin_6
- #define sFLASH_SPI_MISO_GPIO_PORT GPIOA
- #define sFLASH_SPI_MISO_GPIO_CLK RCC_APB2Periph_GPIOA
- #define sFLASH_SPI_MOSI_PIN GPIO_Pin_7
- #define sFLASH_SPI_MOSI_GPIO_PORT GPIOA
- #define sFLASH_SPI_MOSI_GPIO_CLK RCC_APB2Periph_GPIOA
- #define sFLASH_CS_LOW() GPIO_ResetBits(sFLASH_CS_GPIO_PORT, sFLASH_CS_PIN)
- #define sFLASH_CS_HIGH() GPIO_SetBits(sFLASH_CS_GPIO_PORT, sFLASH_CS_PIN)
- #define W25Q64_WriteEnable 0x06
- #define W25Q64_WriteDisable 0x04
- #define W25Q64_ReadStatusReg 0x05
- #define W25Q64_WriteStatusReg 0x01
- #define W25Q64_ReadData 0x03
- #define W25Q64_FastReadData 0x0B
- #define W25Q64_FastReadDual 0x3B
- #define W25Q64_PageProgram 0x02
- #define W25Q64_SectorErase 0x20
- #define W25Q64_BlockErase 0x52
- #define W25Q64_BigBlockErase 0xd8
- #define W25Q64_ChipErase 0xC7
- #define W25Q64_PowerDown 0xB9
- #define W25Q64_ReleasePowerDown 0xAB
- #define W25Q64_DeviceID 0x9F
- uint32_t sFlash_ReadID(void);
- u8 sFlash_ReadSR(void);
- void sFlash_Write_SR(u8 sr);
- void sFlash_Write_Enable(void);
- void sFlash_Write_Disable(void);
- void sFlash_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead);
- void sFlash_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);
- void sFlash_Erase_Chip(void);
- void sFlash_Erase_Sector(u32 Dst_Addr);
- void sFlash_Wait_Busy(void);
- void sFlash_PowerDown(void);
- void sFlash_WAKEUP(void);
- void sFlash_SetProtectMode(unsigned char mode);
- void sFlash_Global_Protect(void);
- void sFlash_Global_Unprotect(void);
- static unsigned char sFlashProtectMode=0;
- void DelayMs(unsigned short ms)
- {
- unsigned short i;
- unsigned short t=ms;
-
- while(t--){
- for(i=0;i<6000;i++);
-
- }
- }
- void DelayUs(unsigned short us)
- {
- unsigned short i;
- unsigned short t=us;
- while(t--){
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- }
- }
- void W25Q64_PortInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(sFLASH_CS_GPIO_CLK | sFLASH_SPI_MOSI_GPIO_CLK | sFLASH_SPI_MISO_GPIO_CLK |
-
- sFLASH_SPI_SCK_GPIO_CLK, ENABLE);
-
- RCC_APB2PeriphClockCmd(sFLASH_SPI_CLK, ENABLE);
-
-
- GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_SCK_PIN;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(sFLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MOSI_PIN;
- GPIO_Init(sFLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MISO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(sFLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Pin = sFLASH_CS_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(sFLASH_CS_GPIO_PORT, &GPIO_InitStructure);
- }
- int W25Q64_Init(void)
- {
- uint32_t sFlashId;
- SPI_InitTypeDef SPI_InitStructure;
-
- W25Q64_PortInit();
-
- sFLASH_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_4;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_CRCPolynomial = 7;
- SPI_Init(sFLASH_SPI, &SPI_InitStructure);
-
-
-
- SPI_Cmd(sFLASH_SPI, ENABLE);
-
- DelayUs(1000);
-
- sFlashId=sFlash_ReadID();
-
-
-
-
-
-
- return 1;
-
-
- }
- u8 SPIx_ReadWriteByte(u8 byte)
- {
-
- while (SPI_I2S_GetFlagStatus(sFLASH_SPI, SPI_I2S_FLAG_TXE) == RESET);
-
- SPI_I2S_SendData(sFLASH_SPI, byte);
-
- while (SPI_I2S_GetFlagStatus(sFLASH_SPI, SPI_I2S_FLAG_RXNE) == RESET);
-
- return SPI_I2S_ReceiveData(sFLASH_SPI);
- }
- u8 sFlash_ReadSR(void)
- {
- u8 byte=0;
- sFLASH_CS_LOW();
- SPIx_ReadWriteByte(W25Q64_ReadStatusReg);
- byte=SPIx_ReadWriteByte(0Xff);
- sFLASH_CS_HIGH();
- return byte;
- }
- void sFlash_Write_SR(u8 sr)
- {
- sFLASH_CS_LOW();
- SPIx_ReadWriteByte(W25Q64_WriteStatusReg);
- SPIx_ReadWriteByte(sr);
- sFLASH_CS_HIGH();
- }
- void sFlash_Write_Enable(void)
- {
- sFLASH_CS_LOW();
- SPIx_ReadWriteByte(W25Q64_WriteEnable);
- sFLASH_CS_HIGH();
- }
- void sFlash_Write_Disable(void)
- {
- sFLASH_CS_LOW();
- SPIx_ReadWriteByte(W25Q64_WriteDisable);
- sFLASH_CS_HIGH();
- }
- uint32_t sFlash_ReadID(void)
- {
- uint32_t Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
- sFLASH_CS_LOW();
- SPIx_ReadWriteByte(W25Q64_DeviceID);
- Temp0=SPIx_ReadWriteByte(0xFF);
- Temp1=SPIx_ReadWriteByte(0xFF);
- Temp2=SPIx_ReadWriteByte(0xFF);
- sFLASH_CS_HIGH();
- Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
- return Temp;
- }
- void sFlash_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)
- {
- u16 i;
- sFLASH_CS_LOW();
-
- SPIx_ReadWriteByte(W25Q64_FastReadData);
- SPIx_ReadWriteByte((u8)((ReadAddr)>>16));
- SPIx_ReadWriteByte((u8)((ReadAddr)>>8));
- SPIx_ReadWriteByte((u8)ReadAddr);
- pBuffer[0]=SPIx_ReadWriteByte(0XFF);
- for(i=0;i<NumByteToRead;i++)
- {
- pBuffer[i]=SPIx_ReadWriteByte(0XFF);
- }
- sFLASH_CS_HIGH();
- }
- void sFlash_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
- {
- u16 i;
- sFlash_Write_Enable();
- sFLASH_CS_LOW();
- SPIx_ReadWriteByte(W25Q64_PageProgram);
- SPIx_ReadWriteByte((u8)((WriteAddr)>>16));
- SPIx_ReadWriteByte((u8)((WriteAddr)>>8));
- SPIx_ReadWriteByte((u8)WriteAddr);
- for(i=0;i<NumByteToWrite;i++) SPIx_ReadWriteByte(pBuffer[i]);
- sFLASH_CS_HIGH();
- sFlash_Wait_Busy();
- }
- void sFlash_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
- {
- u16 pageremain;
- pageremain=256-WriteAddr%256;
- if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;
- while(1)
- {
- sFlash_Write_Page(pBuffer,WriteAddr,pageremain);
- if(NumByteToWrite==pageremain)break;
- else
- {
- pBuffer+=pageremain;
- WriteAddr+=pageremain;
- NumByteToWrite-=pageremain;
- if(NumByteToWrite>256)pageremain=256;
- else pageremain=NumByteToWrite;
- }
- };
- }
- u8 sFlash_BUF[4096];
- void sFlash_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
- {
- u32 secpos;
- u16 secoff;
- u16 secremain;
- u16 i;
- secpos=WriteAddr/4096;
- secoff=WriteAddr%4096;
- secremain=4096-secoff;
- if(NumByteToWrite<=secremain)secremain=NumByteToWrite;
- while(1)
- {
- sFlash_Read(sFlash_BUF,secpos*4096,4096);
- for(i=0;i<secremain;i++)
- {
- if(sFlash_BUF[secoff+i]!=0XFF)break;
- }
- if(i<secremain)
- {
- sFlash_Erase_Sector(secpos);
- for(i=0;i<secremain;i++)
- {
- sFlash_BUF[i+secoff]=pBuffer[i];
- }
- sFlash_Write_NoCheck(sFlash_BUF,secpos*4096,4096);
- }else sFlash_Write_NoCheck(pBuffer,WriteAddr,secremain);
- if(NumByteToWrite==secremain)break;
- else
- {
- secpos++;
- secoff=0;
- pBuffer+=secremain;
- WriteAddr+=secremain;
- NumByteToWrite-=secremain;
- if(NumByteToWrite>4096)secremain=4096;
- else secremain=NumByteToWrite;
- }
- }
- }
- void sFlash_Erase_Chip(void)
- {
- sFlash_Write_Enable();
- sFlash_Wait_Busy();
- sFLASH_CS_LOW();
- SPIx_ReadWriteByte(W25Q64_ChipErase);
- sFLASH_CS_HIGH();
- sFlash_Wait_Busy();
- }
- void sFlash_Erase_Sector(u32 Dst_Addr)
- {
- Dst_Addr*=4096;
- sFlash_Write_Enable();
- sFlash_Wait_Busy();
- sFLASH_CS_LOW();
- SPIx_ReadWriteByte(W25Q64_SectorErase);
- SPIx_ReadWriteByte((u8)((Dst_Addr)>>16));
- SPIx_ReadWriteByte((u8)((Dst_Addr)>>8));
- SPIx_ReadWriteByte((u8)Dst_Addr);
- sFLASH_CS_HIGH();
- sFlash_Wait_Busy();
- }
- void sFlash_Global_Protect(void)
- {
-
- sFlash_Write_Enable();
- sFlash_Wait_Busy();
- sFlash_Write_SR(0x3c);
-
- }
- void sFlash_Global_Unprotect(void)
- {
-
- sFlash_Write_Enable();
- sFlash_Wait_Busy();
- sFlash_Write_SR(0x80);
-
- }
- void sFlash_Wait_Busy(void)
- {
- while ((sFlash_ReadSR()&0x01)==0x01){
-
- }
- }
- void sFlash_PowerDown(void)
- {
- sFLASH_CS_LOW();
- SPIx_ReadWriteByte(W25Q64_PowerDown);
- sFLASH_CS_HIGH();
- DelayUs(3);
- }
- void sFlash_WAKEUP(void)
- {
- sFLASH_CS_LOW();
- SPIx_ReadWriteByte(W25Q64_ReleasePowerDown);
- sFLASH_CS_HIGH();
- DelayUs(3);
- }
- void W25Q64Test(void)
- {
- uint32_t sFlashId;
- unsigned char sFlashSR;
- static int scStep=0;
- unsigned char t;
- char buf[200];
- int i;
- switch(scStep){
- case 0:
- sFlashId=sFlash_ReadID();
- printf("sFlashID=%04x\r\n",sFlashId);
- break;
- case 1:
- memset(buf,0,sizeof(buf));
- strcpy(buf,"333333333444444444455555555555\r\n");
- printf("sFlash Write=%s",buf);
- sFlash_Write((unsigned char *)buf,0,sizeof(buf));
- break;
- case 2:
- memset(buf,0,sizeof(buf));
- sFlash_Read((unsigned char *)buf,0,sizeof(buf));
- printf("sFlash Read=%s",buf);
- printf("sFlash write 8MB...\r\n");
- for(i=0;i<200;i++){
- buf[i]=i;
- }
- for(i=0;i<1000;i++){
- sFlash_Write_Page((unsigned char *)buf,i*4096,200);
- }
- printf("sFlash write 8MB OK!\r\n");
- break;
- case 3:
-
- break;
- }
- if(scStep<3)scStep++;
- }
|