/* m25pe16 */
#define M25_WREN 0x06 /* 写使能 */
#define M25_WRDI 0x04
#define M25_RDID 0x9f
#define M25_RDSR 0x05 /* 读状态 */
#define M25_WRSR 0x01
#define M25_WRLR 0xe5
#define M25_RDLR 0xE8
#define M25_READ 0x03 /* 读 */
#define M25_FAST_READ 0x0b
#define M25_PW 0x0a /* 页写 */
#define M25_PP 0x02
#define M25_PE 0xdb
#define M25_SE 0xd8
#define M25_SSE 0x20
#define M25_BE 0xc7
#define M25_DP 0xb9
#define M25_RDA 0xab
#define M2P_WIP_RDSR 1 /* 1:忙,0:不忙 */
#define M2P_WEL_RDSR 2 /* 1:写保护,0:非写保护 */
#define M2P_SRWD_RDSR 0X80
#define M2P_PAGE_SIZE 256
#define DRV_FLASH_CS_HIGH() GPIO_SetBits(FLASH_CS_PORT, FLASH_CS_PIN)
#define DRV_FLASH_CS_LOW() GPIO_ResetBits(FLASH_CS_PORT, FLASH_CS_PIN)
void DRV_FlashInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = FLASH_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(FLASH_CS_PORT, &GPIO_InitStructure);
DRV_FLASH_CS_HIGH();
}
void DRV_FlashWritePage(uint32 Addr,uint8 *Buf,uint32 Len)
{
uint8 CmdBuf[4]={M25_RDSR};
uint8 RecBuf[1];
uint8 Timeout_i;
if ((Addr%M2P_PAGE_SIZE+Len > M2P_PAGE_SIZE)||(Len==0))
{
return;
}
for (Timeout_i=0; Timeout_i<M25_TIMEOUT_I; Timeout_i++)
{
DRV_FLASH_CS_LOW();
Drv_Spi_1Send(CmdBuf, 1);
Drv_Spi_1Rec(RecBuf, 1);
DRV_FLASH_CS_HIGH();
if (RecBuf[0]&M2P_WIP_RDSR)
{
OSTimeDlyHMSM(0, 0, 0, M25_TIMEOUT_WAIT);
}
else
{
break;
}
}
if (Timeout_i>=M25_TIMEOUT_I)
{
return;
}
DRV_FLASH_CS_LOW();
CmdBuf[0] = M25_WREN;
Drv_Spi_1Send(CmdBuf, 1);
DRV_FLASH_CS_HIGH();
DRV_FLASH_CS_LOW();
CmdBuf[0] = M25_PW;
CmdBuf[1] = (Addr>>16)&0xff;
CmdBuf[2] = (Addr>>8)&0xff;
CmdBuf[3] = Addr&0xff;
Drv_Spi_1Send(CmdBuf, 4);
Drv_Spi_1Send(Buf, Len);
DRV_FLASH_CS_HIGH();
}
void DRV_FlashWriteStr(uint32 Addr,uint8 *Buf,uint32 Len)
{
uint32 Len_ = Len;
uint32 Addr_ = Addr;
while(Len_ > 0)
{
if (Addr_%M2P_PAGE_SIZE+Len_ > M2P_PAGE_SIZE)
{
DRV_FlashWritePage (Addr_, Buf+Len-Len_, M2P_PAGE_SIZE-Addr_%M2P_PAGE_SIZE);
Len_ -= M2P_PAGE_SIZE-Addr_%M2P_PAGE_SIZE;
Addr_ += M2P_PAGE_SIZE-Addr_%M2P_PAGE_SIZE;
}
else
{
DRV_FlashWritePage (Addr_, Buf+Len-Len_, Len_);
break;
}
}
}
void DRV_FlashRead(uint32 Addr,uint8* Buf,uint32 Len)
{
uint8 CmdBuf[4]={M25_RDSR};
uint8 RecBuf[1];
uint8 Timeout_i;
if (Len==0)
{
return;
}
for (Timeout_i=0; Timeout_i<M25_TIMEOUT_I; Timeout_i++)
{
DRV_FLASH_CS_LOW();
Drv_Spi_1Send(CmdBuf, 1);
Drv_Spi_1Rec(RecBuf, 1);
DRV_FLASH_CS_HIGH();
if (RecBuf[0]&M2P_WIP_RDSR)
{
OSTimeDlyHMSM(0, 0, 0, M25_TIMEOUT_WAIT);
}
else
{
break;
}
}
if (Timeout_i>=M25_TIMEOUT_I)
{
return;
}
CmdBuf[0] = M25_READ;
CmdBuf[1] = (Addr>>16)&0xff;
CmdBuf[2] = (Addr>>8)&0xff;
CmdBuf[3] = Addr&0xff;
DRV_FLASH_CS_LOW();
Drv_Spi_1Send(CmdBuf, 4);
Drv_Spi_1Rec(Buf, Len);
DRV_FLASH_CS_HIGH();
}
void DRV_FlashDeepPDN(void)
{
#if 1
//uint8 err;
uint8 e2prom_data[1];
uint8 CmdBuf[4]={M25_RDSR};
uint8 RecBuf[1];
uint8 Timeout_i;
for (Timeout_i=0; Timeout_i<M25_TIMEOUT_I; Timeout_i++)
{
DRV_FLASH_CS_LOW();
Drv_Spi_1Send(CmdBuf, 1);
Drv_Spi_1Rec(RecBuf, 1);
DRV_FLASH_CS_HIGH();
if (RecBuf[0]&M2P_WIP_RDSR)
{
OSTimeDlyHMSM(0, 0, 0, M25_TIMEOUT_WAIT);
}
else
{
break;
}
}
if (Timeout_i>=M25_TIMEOUT_I)
{
return;
}
e2prom_data[0]=M25_DP;
DRV_FLASH_CS_LOW();
Drv_Spi_1Send(e2prom_data,1);
DRV_FLASH_CS_HIGH();
#endif
}
void DRV_FlashRlsPDN(void)
{
#if 1
uint8 e2prom_data[1];
e2prom_data[0]=M25_RDA;
DRV_FLASH_CS_LOW();
Drv_Spi_1Send(e2prom_data,1);
DRV_FLASH_CS_HIGH();
#endif
}
#define M25_WREN 0x06 /* 写使能 */
#define M25_WRDI 0x04
#define M25_RDID 0x9f
#define M25_RDSR 0x05 /* 读状态 */
#define M25_WRSR 0x01
#define M25_WRLR 0xe5
#define M25_RDLR 0xE8
#define M25_READ 0x03 /* 读 */
#define M25_FAST_READ 0x0b
#define M25_PW 0x0a /* 页写 */
#define M25_PP 0x02
#define M25_PE 0xdb
#define M25_SE 0xd8
#define M25_SSE 0x20
#define M25_BE 0xc7
#define M25_DP 0xb9
#define M25_RDA 0xab
#define M2P_WIP_RDSR 1 /* 1:忙,0:不忙 */
#define M2P_WEL_RDSR 2 /* 1:写保护,0:非写保护 */
#define M2P_SRWD_RDSR 0X80
#define M2P_PAGE_SIZE 256
#define DRV_FLASH_CS_HIGH() GPIO_SetBits(FLASH_CS_PORT, FLASH_CS_PIN)
#define DRV_FLASH_CS_LOW() GPIO_ResetBits(FLASH_CS_PORT, FLASH_CS_PIN)
#define M25_TIMEOUT_WAIT 3
#define M25_TIMEOUT_I 8
void DRV_FlashInit(void);
void DRV_FlashWritePage(uint32 Addr,uint8 *Buf,uint32 Len);
void DRV_FlashWriteStr(uint32 Addr,uint8 *Buf,uint32 Len);
void DRV_FlashRead(uint32 Addr,uint8* Buf,uint32 Len);
void DRV_FlashDeepPDN(void);
void DRV_FlashRlsPDN(void);
void DRV_FlashInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = FLASH_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(FLASH_CS_PORT, &GPIO_InitStructure);
DRV_FLASH_CS_HIGH();
}
void DRV_FlashWritePage(uint32 Addr,uint8 *Buf,uint32 Len)
{
uint8 CmdBuf[4]={M25_RDSR};
uint8 RecBuf[1];
uint8 Timeout_i;
if ((Addr%M2P_PAGE_SIZE+Len > M2P_PAGE_SIZE)||(Len==0))
{
return;
}
for (Timeout_i=0; Timeout_i<M25_TIMEOUT_I; Timeout_i++)
{
DRV_FLASH_CS_LOW();
Drv_Spi_1Send(CmdBuf, 1);
Drv_Spi_1Rec(RecBuf, 1);
DRV_FLASH_CS_HIGH();
if (RecBuf[0]&M2P_WIP_RDSR)
{
OSTimeDlyHMSM(0, 0, 0, M25_TIMEOUT_WAIT);
}
else
{
break;
}
}
if (Timeout_i>=M25_TIMEOUT_I)
{
return;
}
DRV_FLASH_CS_LOW();
CmdBuf[0] = M25_WREN;
Drv_Spi_1Send(CmdBuf, 1);
DRV_FLASH_CS_HIGH();
DRV_FLASH_CS_LOW();
CmdBuf[0] = M25_PW;
CmdBuf[1] = (Addr>>16)&0xff;
CmdBuf[2] = (Addr>>8)&0xff;
CmdBuf[3] = Addr&0xff;
Drv_Spi_1Send(CmdBuf, 4);
Drv_Spi_1Send(Buf, Len);
DRV_FLASH_CS_HIGH();
}
void DRV_FlashWriteStr(uint32 Addr,uint8 *Buf,uint32 Len)
{
uint32 Len_ = Len;
uint32 Addr_ = Addr;
while(Len_ > 0)
{
if (Addr_%M2P_PAGE_SIZE+Len_ > M2P_PAGE_SIZE)
{
DRV_FlashWritePage (Addr_, Buf+Len-Len_, M2P_PAGE_SIZE-Addr_%M2P_PAGE_SIZE);
Len_ -= M2P_PAGE_SIZE-Addr_%M2P_PAGE_SIZE;
Addr_ += M2P_PAGE_SIZE-Addr_%M2P_PAGE_SIZE;
}
else
{
DRV_FlashWritePage (Addr_, Buf+Len-Len_, Len_);
break;
}
}
}
void DRV_FlashRead(uint32 Addr,uint8* Buf,uint32 Len)
{
uint8 CmdBuf[4]={M25_RDSR};
uint8 RecBuf[1];
uint8 Timeout_i;
if (Len==0)
{
return;
}
for (Timeout_i=0; Timeout_i<M25_TIMEOUT_I; Timeout_i++)
{
DRV_FLASH_CS_LOW();
Drv_Spi_1Send(CmdBuf, 1);
Drv_Spi_1Rec(RecBuf, 1);
DRV_FLASH_CS_HIGH();
if (RecBuf[0]&M2P_WIP_RDSR)
{
OSTimeDlyHMSM(0, 0, 0, M25_TIMEOUT_WAIT);
}
else
{
break;
}
}
if (Timeout_i>=M25_TIMEOUT_I)
{
return;
}
CmdBuf[0] = M25_READ;
CmdBuf[1] = (Addr>>16)&0xff;
CmdBuf[2] = (Addr>>8)&0xff;
CmdBuf[3] = Addr&0xff;
DRV_FLASH_CS_LOW();
Drv_Spi_1Send(CmdBuf, 4);
Drv_Spi_1Rec(Buf, Len);
DRV_FLASH_CS_HIGH();
}
void DRV_FlashDeepPDN(void)
{
#if 1
//uint8 err;
uint8 e2prom_data[1];
uint8 CmdBuf[4]={M25_RDSR};
uint8 RecBuf[1];
uint8 Timeout_i;
for (Timeout_i=0; Timeout_i<M25_TIMEOUT_I; Timeout_i++)
{
DRV_FLASH_CS_LOW();
Drv_Spi_1Send(CmdBuf, 1);
Drv_Spi_1Rec(RecBuf, 1);
DRV_FLASH_CS_HIGH();
if (RecBuf[0]&M2P_WIP_RDSR)
{
OSTimeDlyHMSM(0, 0, 0, M25_TIMEOUT_WAIT);
}
else
{
break;
}
}
if (Timeout_i>=M25_TIMEOUT_I)
{
return;
}
e2prom_data[0]=M25_DP;
DRV_FLASH_CS_LOW();
Drv_Spi_1Send(e2prom_data,1);
DRV_FLASH_CS_HIGH();
#endif
}
void DRV_FlashRlsPDN(void)
{
#if 1
uint8 e2prom_data[1];
e2prom_data[0]=M25_RDA;
DRV_FLASH_CS_LOW();
Drv_Spi_1Send(e2prom_data,1);
DRV_FLASH_CS_HIGH();
#endif
}