m25pe16

/* 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)


#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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

缥缈孤鸿_jason

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值