123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622 |
- #define THIS_FILE_ID 13
- #define W25Q32_JEDEC_ID 0xef4016
- #define W25Q16_JEDEC_ID 0xef4015
- #define AT25DF321_JEDEC_ID 0x1f4701
- #define MX25L1606_JEDEC_ID 0xc22015
- #define FM25Q32_JEDEC_ID 0xa14016
- static const unsigned long listFlashId[]={
- W25Q32_JEDEC_ID,
- W25Q16_JEDEC_ID,
- AT25DF321_JEDEC_ID,
- MX25L1606_JEDEC_ID,
- FM25Q32_JEDEC_ID,
- 0
- };
- static const char *listFlashName[]={
- "W25Q32",
- "W25Q16",
- "AT25DF321",
- "MX25L1606",
- "FM25Q32",
- ""
- };
- #include "includes.h"
- //4Kbytes为一个Sector
- //16个扇区为1个Block
- //W25X16
- //容量为2M字节,共有32个Block,512个Sector
- //AT25DF321 上电后 sector protect 默认为使能状态,即SR寄存器SWP(bit3:2)为11b
- //先要调用函数进行sector解保护,或者全局解保护后才能写入.
- //W25系列上电默认没有软件写保护,故可直接写入
- #define sFLASH_CS_PIN GPIO_Pin_4 /* PA.04 */
- #define sFLASH_CS_GPIO_PORT GPIOA /* 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 /* PA.05 */
- #define sFLASH_SPI_SCK_GPIO_PORT GPIOA /* GPIOA */
- #define sFLASH_SPI_SCK_GPIO_CLK RCC_APB2Periph_GPIOA
- #define sFLASH_SPI_MISO_PIN GPIO_Pin_6 /* PA.06 */
- #define sFLASH_SPI_MISO_GPIO_PORT GPIOA /* GPIOA */
- #define sFLASH_SPI_MISO_GPIO_CLK RCC_APB2Periph_GPIOA
- #define sFLASH_SPI_MOSI_PIN GPIO_Pin_7 /* PA.07 */
- #define sFLASH_SPI_MOSI_GPIO_PORT GPIOA /* GPIOA */
- #define sFLASH_SPI_MOSI_GPIO_CLK RCC_APB2Periph_GPIOA
- //指令表
- #define AT25DF_WriteEnable 0x06
- #define AT25DF_WriteDisable 0x04
- #define AT25DF_ReadStatusReg 0x05
- #define AT25DF_WriteStatusReg 0x01
- #define AT25DF_ReadData 0x03
- #define AT25DF_FastReadData 0x0B
- #define AT25DF_FastReadDual 0x3B //??
- #define AT25DF_PageProgram 0x02
- #define AT25DF_BlockErase 0x20 //4k-0x20 32k-0x52 64k-0xD8
- #define AT25DF_SectorErase 0x20
- #define AT25DF_ChipErase 0xC7 //0x60
- #define AT25DF_PowerDown 0xB9
- #define AT25DF_ReleasePowerDown 0xAB
- #define AT25DF_DeviceID 0x9F
- #define AT25DF_UnprotectSector 0x39
- #define AT25DF_ProtectSector 0x36
- uint32_t SPI_Flash_ReadID(void); //读取FLASH ID
- u8 SPI_Flash_ReadSR(void); //读取状态寄存器
- void SPI_FLASH_Write_SR(u8 sr); //写状态寄存器
- void SPI_FLASH_Write_Enable(void); //写使能
- void SPI_FLASH_Write_Disable(void); //写保护
- void SPI_Flash_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead); //读取flash
- void SPI_Flash_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);//写入flash
- void SPI_Flash_Erase_Chip(void); //整片擦除
- void SPI_Flash_Erase_Sector(u32 Dst_Addr);//扇区擦除
- void SPI_Flash_Wait_Busy(void); //等待空闲
- void SPI_Flash_PowerDown(void); //进入掉电模式
- void SPI_Flash_WAKEUP(void); //唤醒
- void SPI_Flash_Protect_Sector(u32 Dst_Addr);
- void SPI_Flash_Unprotect_Sector(u32 Dst_Addr);
- void SPI_Flash_Global_Protect(void);
- void SPI_Flash_Global_Unprotect(void);
- void SPI_Flash_Wait_Busy_ByDog(void);
- u8 SPIx_ReadWriteByte(u8 byte);
- /*
- 系统时钟 72Mhz 粗略延时us
- */
- void DelayUs(u16 myUs)
- {
- u16 i;
- while(myUs--)
- {
- i=6;
- while(i--);
- }
- }
- /*
- 系统时钟 72Mhz 粗略延时ms
- 带喂狗
- */
- void DelayMs(u16 myMs)
- {
- u16 i;
- while(myMs--)
- {
- DelayUs(1000);
- }
- }
- /***************************************************************
- AT25DF_PortInit
- ****************************************************************/
- void AT25DF_PortInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /*!< sFLASH_SPI_CS_GPIO, sFLASH_SPI_MOSI_GPIO, sFLASH_SPI_MISO_GPIO
- and sFLASH_SPI_SCK_GPIO Periph clock enable */
- RCC_APB2PeriphClockCmd(sFLASH_CS_GPIO_CLK | sFLASH_SPI_MOSI_GPIO_CLK | sFLASH_SPI_MISO_GPIO_CLK |
- sFLASH_SPI_SCK_GPIO_CLK, ENABLE);
- /*!< sFLASH_SPI Periph clock enable */
- RCC_APB2PeriphClockCmd(sFLASH_SPI_CLK, ENABLE);
-
- /*!< Configure sFLASH_SPI pins: SCK */
- 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);
- /*!< Configure sFLASH_SPI pins: MOSI */
- GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MOSI_PIN;
- GPIO_Init(sFLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
- /*!< Configure sFLASH_SPI pins: MISO */
- 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);
-
- /*!< Configure sFLASH_CS_PIN pin: sFLASH Card CS pin */
- GPIO_InitStructure.GPIO_Pin = sFLASH_CS_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(sFLASH_CS_GPIO_PORT, &GPIO_InitStructure);
- }
- uint32_t g_ulFlashId;
- /***************************************************************
- AT25DF_Init
- ****************************************************************/
- int AT25DF_Init(void)
- {
- char buf[30];
- int i;
- SPI_InitTypeDef SPI_InitStructure;
- SlwTrace(DEBUG,"sFlash Init...\r\n");
- AT25DF_PortInit();
- /*!< Deselect the FLASH: Chip Select high */
- AT25DF_CS_HIGH();
- /*!< SPI configuration */
- 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);
- /*!< Enable the sFLASH_SPI */
- SPI_Cmd(sFLASH_SPI, ENABLE);
- //
- DelayUs(1000);
- //
- g_ulFlashId=SPI_Flash_ReadID();
- sprintf(buf,"FxF Id=%04x\r\n",g_ulFlashId);
- SlwTrace(DEBUG,buf);
- for(i=0;;i++){
- if(g_ulFlashId==listFlashId[i]){
- sprintf(buf,"ExF=%s\r\n",listFlashName[i]);
- SlwTrace(DEBUG,buf);
- return 1;
- }
- }
- SlwTrace(DEBUG,"ExFlash init error!\r\n");
- return 0;
- }
- /**********************************************************************
- *
- ***********************************************************************/
- u8 SPIx_ReadWriteByte(u8 byte)
- {
- // < Loop while DR register in not emplty
- while (SPI_I2S_GetFlagStatus(sFLASH_SPI, SPI_I2S_FLAG_TXE) == RESET);
- // !< Send byte through the SPI1 peripheral
- SPI_I2S_SendData(sFLASH_SPI, byte);
- // !< Wait to receive a byte
- while (SPI_I2S_GetFlagStatus(sFLASH_SPI, SPI_I2S_FLAG_RXNE) == RESET);
- // !< Return the byte read from the SPI bus
- return SPI_I2S_ReceiveData(sFLASH_SPI);
- }
- /*
- //SPIx 读写一个字节
- //返回值:读取到的字节
- u8 SPIx_ReadWriteByte(u8 TxData)
- {
- u8 retry=0;
- while((SPI1->SR&1<<1)==0)//等待发送区空
- {
- retry++;
- if(retry>200)return 0;
- }
- SPI1->DR=TxData; //发送一个byte
- retry=0;
- while((SPI1->SR&1<<0)==0) //等待接收完一个byte
- {
- retry++;
- if(retry>200)return 0;
- }
- return SPI1->DR; //返回收到的数据
- }
- */
- //读取SPI_FLASH的状态寄存器
- //BIT7 6 5 4 3 2 1 0
- //SPR RV TB BP2 BP1 BP0 WEL BUSY
- //SPR:默认0,状态寄存器保护位,配合WP使用
- //TB,BP2,BP1,BP0:FLASH区域写保护设置
- //WEL:写使能锁定
- //BUSY:忙标记位(1,忙;0,空闲)
- //默认:0x00
- u8 SPI_Flash_ReadSR(void)
- {
- u8 byte=0;
- AT25DF_CS_LOW(); //使能器件
- SPIx_ReadWriteByte(AT25DF_ReadStatusReg); //发送读取状态寄存器命令
- byte=SPIx_ReadWriteByte(0Xff); //读取一个字节
- AT25DF_CS_HIGH(); //取消片选
- return byte;
- }
- //写SPI_FLASH状态寄存器
- //只有SPR,TB,BP2,BP1,BP0(bit 7,5,4,3,2)可以写!!!
- void SPI_FLASH_Write_SR(u8 sr)
- {
- AT25DF_CS_LOW(); //使能器件
- SPIx_ReadWriteByte(AT25DF_WriteStatusReg); //发送写取状态寄存器命令
- SPIx_ReadWriteByte(sr); //写入一个字节
- AT25DF_CS_HIGH(); //取消片选
- }
- //SPI_FLASH写使能
- //将WEL置位
- void SPI_FLASH_Write_Enable(void)
- {
- AT25DF_CS_LOW(); //使能器件
- SPIx_ReadWriteByte(AT25DF_WriteEnable); //发送写使能
- AT25DF_CS_HIGH(); //取消片选
- }
- //SPI_FLASH写禁止
- //将WEL清零
- void SPI_FLASH_Write_Disable(void)
- {
- AT25DF_CS_LOW(); //使能器件
- SPIx_ReadWriteByte(AT25DF_WriteDisable); //发送写禁止指令
- AT25DF_CS_HIGH(); //取消片选
- }
- //读取JEDEC ID
- uint32_t SPI_Flash_ReadID(void)
- {
- uint32_t Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
- AT25DF_CS_LOW();
- SPIx_ReadWriteByte(AT25DF_DeviceID);//发送读取ID命令
- Temp0=SPIx_ReadWriteByte(0xFF);
- Temp1=SPIx_ReadWriteByte(0xFF);
- Temp2=SPIx_ReadWriteByte(0xFF);
- AT25DF_CS_HIGH();
- Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
- return Temp;
- }
- //读取SPI FLASH
- //在指定地址开始读取指定长度的数据
- //pBuffer:数据存储区
- //ReadAddr:开始读取的地址(24bit)
- //NumByteToRead:要读取的字节数(最大65535)
- void SPI_Flash_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)
- {
- u16 i;
- AT25DF_CS_LOW(); //使能器件
- //SPIx_ReadWriteByte(AT25DF_ReadData); //发送读取命令
- SPIx_ReadWriteByte(AT25DF_FastReadData);//快速读命令
- SPIx_ReadWriteByte((u8)((ReadAddr)>>16)); //发送24bit地址
- 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); //循环读数
- }
- AT25DF_CS_HIGH(); //取消片选
- }
- //SPI在一页(0~65535)内写入少于256个字节的数据
- //在指定地址开始写入最大256字节的数据
- //pBuffer:数据存储区
- //WriteAddr:开始写入的地址(24bit)
- //NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!
- void SPI_Flash_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
- {
- u16 i;
- SPI_FLASH_Write_Enable(); //SET WEL
- AT25DF_CS_LOW(); //使能器件
- SPIx_ReadWriteByte(AT25DF_PageProgram); //发送写页命令
- SPIx_ReadWriteByte((u8)((WriteAddr)>>16)); //发送24bit地址
- SPIx_ReadWriteByte((u8)((WriteAddr)>>8));
- SPIx_ReadWriteByte((u8)WriteAddr);
- for(i=0;i<NumByteToWrite;i++) SPIx_ReadWriteByte(pBuffer[i]);//循环写数
- AT25DF_CS_HIGH(); //取消片选
- SPI_Flash_Wait_Busy(); //等待写入结束
- }
- //无检验写SPI FLASH
- //必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
- //具有自动换页功能
- //在指定地址开始写入指定长度的数据,但是要确保地址不越界!
- //pBuffer:数据存储区
- //WriteAddr:开始写入的地址(24bit)
- //NumByteToWrite:要写入的字节数(最大65535)
- //CHECK OK
- void SPI_Flash_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
- {
- u16 pageremain;
- pageremain=256-WriteAddr%256; //单页剩余的字节数
- if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//不大于256个字节
- while(1)
- {
- SPI_Flash_Write_Page(pBuffer,WriteAddr,pageremain);
- if(NumByteToWrite==pageremain)break;//写入结束了
- else //NumByteToWrite>pageremain
- {
- pBuffer+=pageremain;
- WriteAddr+=pageremain;
- NumByteToWrite-=pageremain; //减去已经写入了的字节数
- if(NumByteToWrite>256)pageremain=256; //一次可以写入256个字节
- else pageremain=NumByteToWrite; //不够256个字节了
- }
- };
- }
- //写SPI FLASH
- //在指定地址开始写入指定长度的数据
- //该函数带擦除操作!
- //pBuffer:数据存储区
- //WriteAddr:开始写入的地址(24bit)
- //NumByteToWrite:要写入的字节数(最大65535)
- u8 SPI_FLASH_BUF[4096];
- void SPI_Flash_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
- {
- u32 secpos;
- u16 secoff;
- u16 secremain;
- u16 i;
- secpos=WriteAddr/4096;//扇区地址 0~511 for w25x16
- secoff=WriteAddr%4096;//在扇区内的偏移
- secremain=4096-secoff;//扇区剩余空间大小
- if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//不大于4096个字节
- while(1)
- {
- SPI_Flash_Read(SPI_FLASH_BUF,secpos*4096,4096);//读出整个扇区的内容
- for(i=0;i<secremain;i++)//校验数据
- {
- if(SPI_FLASH_BUF[secoff+i]!=0XFF)break;//需要擦除
- }
- if(i<secremain)//需要擦除
- {
- SPI_Flash_Erase_Sector(secpos);//擦除这个扇区
- for(i=0;i<secremain;i++) //复制
- {
- SPI_FLASH_BUF[i+secoff]=pBuffer[i];
- }
- SPI_Flash_Write_NoCheck(SPI_FLASH_BUF,secpos*4096,4096);//写入整个扇区
- }else SPI_Flash_Write_NoCheck(pBuffer,WriteAddr,secremain);//写已经擦除了的,直接写入扇区剩余区间.
- if(NumByteToWrite==secremain)break;//写入结束了
- else//写入未结束
- {
- secpos++;//扇区地址增1
- secoff=0;//偏移位置为0
- pBuffer+=secremain; //指针偏移
- WriteAddr+=secremain;//写地址偏移
- NumByteToWrite-=secremain; //字节数递减
- if(NumByteToWrite>4096)secremain=4096; //下一个扇区还是写不完
- else secremain=NumByteToWrite; //下一个扇区可以写完了
- }
- }
- }
- //擦除整个芯片
- //整片擦除时间:
- //W25X16:25s
- //W25X32:40s
- //W25X64:40s
- //等待时间超长...
- void SPI_Flash_Erase_Chip(void)
- {
- SPI_Flash_Wait_Busy();
- SPI_FLASH_Write_Enable(); //SET WEL
- SPI_Flash_Wait_Busy();
- AT25DF_CS_LOW(); //使能器件
- SPIx_ReadWriteByte(AT25DF_ChipErase); //发送片擦除命令
- AT25DF_CS_HIGH(); //取消片选
- SPI_Flash_Wait_Busy_ByDog(); //等待芯片擦除结束
- }
- //擦除一个扇区
- //Dst_Addr:扇区地址 0~511 for w25x16
- //擦除一个扇区的最少时间:150ms
- void SPI_Flash_Erase_Sector(u32 Dst_Addr)
- {
- Dst_Addr*=4096;
- SPI_FLASH_Write_Enable(); //SET WEL
- SPI_Flash_Wait_Busy();
- AT25DF_CS_LOW(); //使能器件
- SPIx_ReadWriteByte(AT25DF_SectorErase); //发送扇区擦除指令
- SPIx_ReadWriteByte((u8)((Dst_Addr)>>16)); //发送24bit地址
- SPIx_ReadWriteByte((u8)((Dst_Addr)>>8));
- SPIx_ReadWriteByte((u8)Dst_Addr);
- AT25DF_CS_HIGH(); //取消片选
- SPI_Flash_Wait_Busy(); //等待擦除完成
- }
- //保护一个扇区
- //Dst_Addr:扇区地址 0~511 for w25x16
- //AT25DF_ProtectSector
- void SPI_Flash_Protect_Sector(u32 Dst_Addr)
- {
- if(AT25DF321_JEDEC_ID==g_ulFlashId){
- Dst_Addr*=4096;
- SPI_FLASH_Write_Enable(); //SET WEL
- SPI_Flash_Wait_Busy();
- AT25DF_CS_LOW(); //使能器件
- SPIx_ReadWriteByte(AT25DF_ProtectSector); //发送扇区解保护指令
- SPIx_ReadWriteByte((u8)((Dst_Addr)>>16)); //发送24bit地址
- SPIx_ReadWriteByte((u8)((Dst_Addr)>>8));
- SPIx_ReadWriteByte((u8)Dst_Addr);
- AT25DF_CS_HIGH(); //取消片选
- }else if(W25Q16_JEDEC_ID==g_ulFlashId){
-
- }else if(MX25L1606_JEDEC_ID==g_ulFlashId) {
-
- }
- }
- //解保护一个扇区
- //Dst_Addr:扇区地址 0~511 for w25x16
- //AT25DF_UnprotectSector
- void SPI_Flash_Unprotect_Sector(u32 Dst_Addr)
- {
- if(AT25DF321_JEDEC_ID==g_ulFlashId){
- Dst_Addr*=4096;
- SPI_FLASH_Write_Enable(); //SET WEL
- SPI_Flash_Wait_Busy();
- AT25DF_CS_LOW(); //使能器件
- SPIx_ReadWriteByte(AT25DF_UnprotectSector); //发送扇区解保护指令
- SPIx_ReadWriteByte((u8)((Dst_Addr)>>16)); //发送24bit地址
- SPIx_ReadWriteByte((u8)((Dst_Addr)>>8));
- SPIx_ReadWriteByte((u8)Dst_Addr);
- AT25DF_CS_HIGH(); //取消片选
- }else if(W25Q16_JEDEC_ID==g_ulFlashId){
- //SPI_FLASH_Write_Enable(); //SET WEL
- //SPI_Flash_Wait_Busy();
- //SPI_FLASH_Write_SR(0x00);
- }else if(MX25L1606_JEDEC_ID==g_ulFlashId){
- }
- }
- //保护全芯片
- //
- //
- void SPI_Flash_Global_Protect(void)
- {
-
- if(AT25DF321_JEDEC_ID==g_ulFlashId){
- SPI_FLASH_Write_Enable(); //SET WEL
- SPI_Flash_Wait_Busy();
- SPI_FLASH_Write_SR(0x3c);
- }else if(W25Q16_JEDEC_ID==g_ulFlashId){
- SPI_FLASH_Write_Enable(); //SET WEL
- SPI_Flash_Wait_Busy();
- SPI_FLASH_Write_SR(0x18);
- }else if(MX25L1606_JEDEC_ID==g_ulFlashId){
- SPI_FLASH_Write_Enable(); //SET WEL
- SPI_Flash_Wait_Busy();
- SPI_FLASH_Write_SR(0x18);
- }
- }
- //解保护全芯片
- //
- //
- void SPI_Flash_Global_Unprotect(void)
- {
- if(W25Q16_JEDEC_ID==g_ulFlashId){
- SPI_FLASH_Write_Enable(); //SET WEL
- SPI_Flash_Wait_Busy();
- SPI_FLASH_Write_SR(0x00);
- SPI_Flash_Wait_Busy();
- }else if(MX25L1606_JEDEC_ID==g_ulFlashId){
- SPI_FLASH_Write_Enable(); //SET WEL
- SPI_Flash_Wait_Busy();
- SPI_FLASH_Write_SR(0x00);
- SPI_Flash_Wait_Busy();
- }else if(FM25Q32_JEDEC_ID==g_ulFlashId){
- SPI_FLASH_Write_Enable(); //SET WEL
- SPI_Flash_Wait_Busy();
- SPI_FLASH_Write_SR(0x00);
- SPI_Flash_Wait_Busy();
- }
- }
- //等待空闲 不带喂狗
- void SPI_Flash_Wait_Busy(void)
- {
- while ((SPI_Flash_ReadSR()&0x01)==0x01); // 等待BUSY位清空
- }
- //等待空闲 带喂狗
- void SPI_Flash_Wait_Busy_ByDog(void)
- {
- int timeout=20000;//20s
- while ((SPI_Flash_ReadSR()&0x01)==0x01){ // 等待BUSY位清空
- if(timeout){
- IWDG_ReloadCounter();//喂狗
- FEED_EXTWATCHDOG();
- timeout--;
- DelayUs(1000);
- }
- }
- }
- //进入掉电模式
- void SPI_Flash_PowerDown(void)
- {
- AT25DF_CS_LOW(); //使能器件
- SPIx_ReadWriteByte(AT25DF_PowerDown); //发送掉电命令
- AT25DF_CS_HIGH(); //取消片选
- DelayUs(3); //等待TPD
- }
- //唤醒
- void SPI_Flash_WAKEUP(void)
- {
- AT25DF_CS_LOW(); //使能器件
- SPIx_ReadWriteByte(AT25DF_ReleasePowerDown); // send AT25DF_PowerDown command 0xAB
- AT25DF_CS_HIGH(); //取消片选
- DelayUs(3); //等待TRES1
- }
- /*********************************************
- *
- ***********************************************/
- void AT25DFTest(void)
- {
- uint32_t sFlashId;
- unsigned char sFlashSR;
- static int scStep;
- unsigned char t;
- char buf[200];
- //SPI_Flash_Global_Protect();
- //SPI_Flash_Global_Unprotect();
-
-
- switch(scStep){
- case 0:
- sFlashId=SPI_Flash_ReadID();
- sprintf(buf,"FID=%04x\r\n",sFlashId);
- SlwTrace(DEBUG,buf);
- break;
- case 1:
- strcpy(buf,"-------------good night--------\r\n");
- //SPI_Flash_Unprotect_Sector(0);//解扇区软件保护
- SPI_Flash_Write((unsigned char *)buf,256,strlen(buf));
- //SPI_Flash_Protect_Sector(0);
- strcpy(buf,"===TestWriteFail!===============\r\n");
-
- break;
- case 2:
- SPI_Flash_Read((unsigned char *)buf,256,sizeof(buf));
- buf[sizeof(buf)-1]=0;
- SlwTrace(DEBUG,buf);
- break;
- case 3:
- sFlashSR = SPI_Flash_ReadSR();
- sprintf(buf, "FSR=%04x\r\n", sFlashSR);
- SlwTrace(DEBUG,buf);
- break;
- }
- if(++scStep>3)scStep=0;
-
- }
|