#include "stm32f10x.h"
#include "NandFlash.h"
#include "delay.h"
//请调用使用前,一定要读一次ID,本人就因为未读ID 擦除不了而耽误一天时间
void NandFlash_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
GPIO_Pin_4 | GPIO_Pin_5 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_10 |GPIO_Pin_11 |
GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
uint8_t Maker_ID = 0;
uint8_t Device_ID= 0;
uint8_t Third_ID;
uint8_t Fourth_ID;
uint8_t Fifth_ID;
uint8_t RB_State(void)
{
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0) == 0) ;
return 1;
}
void Write_Byte(uint8_t byte)
{
uint16_t temp_d=0;
temp_d=byte;
GPIOB->ODR = GPIOB->ODR&0x000003FC|((temp_d&0xFC)<<8)|(temp_d&0x03);
CLR_WE();
while(RB_State() != 1);
SET_WE();
while(RB_State() != 1);
}
void Write_CMD(uint8_t byte)
{
SET_CLE();
Write_Byte(byte);
CLR_CLE();
}
uint8_t Read_Byte(void)
{
uint8_t byte = 0;
SET_RE();
while(RB_State() != 1);
CLR_RE();
GPIOB->CRL = (GPIOB->CRL & 0xffffff00) | 0x00000044;
GPIOB->CRH = (GPIOB->CRH & 0x000000FF) | 0x44444400;
byte = (uint8_t)(((GPIOB->IDR>>8)&0xfC)|((GPIOB->IDR)&0x03));
//byte = (uint8_t)(((GPIOB->IDR>>8)&0xfC)|((GPIOB->IDR)&0x03));
GPIOB->CRL = (GPIOB->CRL & 0xffffff00) | 0x00000033;
GPIOB->CRH = (GPIOB->CRH & 0x000000FF) | 0x33333300;
SET_RE();
while(RB_State() != 1);
return byte;
}
uint8_t Read_ID(void)
{
Maker_ID = 0;
CLR_CE();
SET_CLE();
Write_Byte(0x90);
CLR_CLE();
SET_ALE();
Write_Byte(0x00);
CLR_ALE();
Maker_ID = Read_Byte();
Device_ID= Read_Byte();
Third_ID = Read_Byte();
Fourth_ID = Read_Byte();
SET_CE();
return Device_ID;
}
void Flash_Reset(void)
{
SET_CLE();
Write_Byte(0xFF);
CLR_CLE();
while(RB_State() != 1);
}
void Read_Data_from_page(uint32_t address,uint8_t *data,uint16_t index)
{
uint8_t fisrt_address=0;
uint8_t second_address=0;
uint8_t third_address=0;
uint8_t fourth_address=0;
uint16_t i=0;
CLR_CE();
fisrt_address=0; //(A0-A7)byte 0~255
second_address=(uint8_t)(address);//(A9-A16)page A13:9 0~31
third_address=(uint8_t)(address>>8); //(A24-A17)block A25:14 0~4095
fourth_address=(uint8_t)(address>>16)&0x1; //A25
SET_CLE();
Write_Byte(0x00);
CLR_CLE();
SET_ALE();
Write_Byte(fisrt_address);
Write_Byte(second_address);
Write_Byte(third_address);
Write_Byte(fourth_address);
CLR_ALE();
while(RB_State() != 1);
for(i=0;i<index;i++)
{
data[i]=Read_Byte();
}
SET_CE();
}
void Read_Data_from_Block(uint32_t address,uint8_t *data,uint16_t index)
{
uint8_t fisrt_address=0;
uint8_t second_address=0;
uint8_t third_address=0;
uint8_t fourth_address=0;
uint16_t i=0,j=0;
CLR_CE();
fisrt_address=0; //(A0-A7)byte 0~255
second_address=(uint8_t)(address<<5);//(A9-A16)page A13:9 0~31
third_address=(uint8_t)(address>>3); //(A24-A17)block A25:14 0~4095
fourth_address=(uint8_t)(address>>11)&0x1; //A25
SET_CLE();
Write_Byte(0x00);
CLR_CLE();
SET_ALE();
Write_Byte(fisrt_address);
Write_Byte(second_address);
Write_Byte(third_address);
Write_Byte(fourth_address);
CLR_ALE();
while(RB_State() != 1);
GPIOB->CRL = (GPIOB->CRL & 0xffffff00) | 0x00000044;
GPIOB->CRH = (GPIOB->CRH & 0x000000FF) | 0x44444400;
SET_RE();
for(j=0;j<32;j++)
{
for(i=0;i<528;i++)
{
CLR_RE();
if(i<512)
data[512*j+i]=(uint8_t)(((GPIOB->IDR>>8)&0xfC)|((GPIOB->IDR)&0x03));
SET_RE();
}
while(RB_State() != 1);
}
GPIOB->CRL = (GPIOB->CRL & 0xffffff00) | 0x00000033;
GPIOB->CRH = (GPIOB->CRH & 0x000000FF) | 0x33333300;
SET_CE();
}
uint8_t Write_Data_to_page(uint32_t address,uint8_t *data,uint16_t index)
{
uint8_t fisrt_address=0;
uint8_t second_address=0;
uint8_t third_address=0;
uint8_t fourth_address=0;
uint16_t i=0;
uint8_t write_state=0;
CLR_CE();
fisrt_address=0; //(A0-A7)byte 0~255
second_address=(uint8_t)(address);//(A9-A16)page A13:9 0~31
third_address=(uint8_t)(address>>8); //(A24-A17)block A25:14 0~4095
fourth_address=(uint8_t)(address>>16)&0x01; //A25
// Flash_Reset();
SET_CLE();
Write_Byte(0x00);
CLR_CLE();
SET_CLE();
Write_Byte(0x80);
CLR_CLE();
SET_ALE();
Write_Byte(fisrt_address);
Write_Byte(second_address);
Write_Byte(third_address);
Write_Byte(fourth_address);
CLR_ALE();
while(RB_State() != 1);
for(i=0;i<528;i++)
{
if(i<index)
Write_Byte(data[i]);
else
Write_Byte(0xFF);
}
SET_CLE();
Write_Byte(0x10);
CLR_CLE();
while(RB_State() != 1);
SET_CLE();
Write_Byte(0x70);
CLR_CLE();
while(RB_State() != 1);
write_state=Read_Byte();
write_state =write_state & 1;
SET_CE();
return(write_state);
}
uint8_t ERASE_Data_from_addres(uint32_t address)
{
uint8_t second_address=0;
uint8_t third_address=0;
uint8_t fourth_address=0;
uint8_t ERASE_state=0Xff;
CLR_CE();
//fisrt_address=0; //(A0-A7)byte 0~255
second_address=(uint8_t)(address)&0xe0;//(A9-A16)page A13:9 0~31
third_address=(uint8_t)(address>>8); //(A24-A17)block A25:14 0~4095
fourth_address=(uint8_t)(address>>16)&0x1; //A25
SET_CLE();
Write_Byte(0x60);
CLR_CLE();
while(RB_State() != 1);
SET_ALE();
Write_Byte(second_address);
Write_Byte(third_address);
Write_Byte(fourth_address);
CLR_ALE();
while(RB_State() != 1);
SET_CLE();
Write_Byte(0xD0);
CLR_CLE();
delay_us(1);
delay_ms(3);
while(RB_State()!= 1);
SET_CLE();
Write_Byte(0x70);
CLR_CLE();
while(RB_State() != 1);
ERASE_state=Read_Byte();
ERASE_state =ERASE_state & 1;
SET_CE();
return(ERASE_state);
}
void K9F1208_WRITE_BLOCK(uint32_t Blockaddr, u8 *readbuff, uint16_t BlockSize)
{
unsigned int i=0,page=0;
unsigned char IDtemp=0;
uint32_t addr=0;
addr=Blockaddr<<5;
while(page<(uint16_t)(BlockSize/512))
{
if(Write_Data_to_page((addr+page),&readbuff[512*page],512)==0)
page++;
else
{
Alarmled_off();
Workled_off();
delay_ms(1000);
Alarmled_on();
Workled_on();
delay_ms(1000);
Alarmled_off();
Workled_off();
}
}
if(BlockSize>512*page)
Write_Data_to_page((addr+page),&readbuff[512*page],(BlockSize-512*page));
}
- 1
- 2
- 3
前往页