读取flash id:
u32 flash_read_id(void)//获取Flash JEDEC_ID
{
u8 i ,ret = 0 ;
u8 id[6] ;
spi_cs(flash_hdl, 0);
spi_send_byte(WINBOND_JEDEC_ID);
for(i = 0 ; i <3 ;i++)
{
id[i] = spi_get_byte();
printf("id[%d] = %x\n",i,id[i]);
printf("id[%d] = %lx\n",i,id[i]);
if(id[i]!=0xff)
{
ret = DEV_OFFLINE ;
}
}
put_buf(id, 3);//get_id
spi_cs(flash_hdl, 1);
// flash_wipe_protect(0);
printf("\n****%x %x %x****\n", id[0], id[1], id[2]);
printf("\n****%lx %lx %lx****\n", id[0], id[1], id[2]);
if(id[1] == 0x80)// if(id[1] == 0xff)//if(id[1] == 0x80)
{
printf("flash DEV_ONLINE\n");
switch(id[2])
{ //根据ID判断Flash大小
case 0x2f:capacity = 2*SPI_FAT_LEN; return DEV_ONLINE;//2M
case 0x16:capacity = 4*SPI_FAT_LEN; return DEV_ONLINE;//4M
case 0x17:capacity = 8*SPI_FAT_LEN; return DEV_ONLINE;//8M
default://其他型号ID自行添加
capacity = 2*SPI_FAT_LEN; //2M
return DEV_ONLINE;
}
}
else
{ //ID错误,Flash不在线
printf("flash DEV_OFFLINE\n");
return DEV_OFFLINE;
}
}
片选使能:
void flash_cs(u8 cs)
{
JL_PORTB->DIR &= ~BIT(1);
cs ? (JL_PORTB->OUT |= BIT(1)) : (JL_PORTB->OUT &= ~BIT(1));
///词板子片选为PB1
//JL_PORTB->DIR &= ~BIT(6);
// cs ? (JL_PORTB->OUT |= BIT(6)) : (JL_PORTB->OUT &= ~BIT(6));
}
初始化:
u32 flash_init(void)
{
u32 ret = 0;
u8 sd_busy;
sd_busy = 1;
dev_power_ctrl(sd1, DEV_POWER_OFF);
sd1_ioctl_api((void *)&sd_busy, SDMMC_GET_CTRLER_FLAG);
// while(sd1_unmount_api());
flash_hdl = spi_init(SPI1_A_CTRL, 0x05, 0, 0, (void *)flash_cs);
ASSERT(flash_hdl);
ret = flash_read_id();
flash_ctrl_close();
return ret;
}
IO口配置 3线或者四线
flash_hdl = spi_init(SPI1_A_CTRL, 0x05, 0, 0, (void *)flash_cs);