void eeprom_GPIOInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //
/*!< SPI_EEPROM_SPI Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
/*!< Configure SPI_EEPROM_SPI pins: SCK */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*!< Configure SPI_EEPROM_SPI pins: MISO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*!< Configure SPI_EEPROM_SPI pins: MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*!< Configure SPI_EEPROM_SPI_CS_PIN pin: SPI_EEPROM Card CS pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
EEPROM_CS_H();
/* SPI2 configuration */
// AT25128: data input on the DIO pin is sampled on the rising edge of the CLK.
// Data on the DO and DIO pins are clocked out on the falling edge of CLK.
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_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE);
delay_us(20);
WRITE_EEPROM_STATUSREG(0x00);
}
void WRITE_EERPOM_ENABLE(void)
{
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0) ;// Tx buffer isnot empty ,wait
EEPROM_CS_L();
delay_us(20);
SPI_I2S_SendData(SPI2,WREN);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0) ;
delay_us(20);
EEPROM_CS_H();
delay_us(20);
}
void WRITE_EERPOM_DISABLE(void)
{
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0) ;
EEPROM_CS_L();
SPI_I2S_SendData(SPI2,WRDI);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0) ;
EEPROM_CS_H();
}
u8 READ_EEPROM_STATUSREG(void)
{
u16 eeprom_status_reg;
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0) ;
EEPROM_CS_L();
SPI_I2S_SendData(SPI2,RDSR);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0);
SPI_I2S_SendData(SPI2,0x0000); //send empty data,generate sclk
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE) == 0) ;
eeprom_status_reg = SPI_I2S_ReceiveData(SPI2);
EEPROM_CS_H();
return (u8)eeprom_status_reg;
}
//
void WRITE_EEPROM_STATUSREG(u8 status_data)
{
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0);
EEPROM_CS_L();
//WRITE_EERPOM_ENABLE();
delay_us(20);
SPI_I2S_SendData(SPI2,WRSR);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0);
delay_us(20);
SPI_I2S_SendData(SPI2,status_data);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0) ;
delay_us(20);
EEPROM_CS_H();
}
void WRITE_EEPROM_BYTE(u16 addr,u8 write_data)
{
// u16 temp;
//temp = (addr << 8);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0) ;
WRITE_EERPOM_ENABLE();
delay_us(20);
EEPROM_CS_L();
delay_us(20);
SPI_I2S_SendData(SPI2,WRITE);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0);
delay_us(20);
SPI_I2S_SendData(SPI2,addr>>8);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0);
delay_us(20);
SPI_I2S_SendData(SPI2,addr);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0);
delay_us(20);
SPI_I2S_SendData(SPI2,write_data);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0);
delay_us(20);
EEPROM_CS_H();
delay_us(20);
}
u8 READ_EEPROM_BYTE(u16 addr)
{
u16 temp, read_data;
//temp = addr << 8;
// WRITE_EERPOM_ENABLE();
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0);
EEPROM_CS_L();
delay_us(20);
SPI_I2S_SendData(SPI2,READ);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0);
delay_us(20);
SPI_I2S_SendData(SPI2,addr>>8);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0);
delay_us(20);
SPI_I2S_SendData(SPI2,addr);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == 0);
delay_us(20);
SPI_I2S_SendData(SPI2,0x00);//send empty data ,generate sclk
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE) == 0) ;
delay_us(20);
read_data = SPI_I2S_ReceiveData(SPI2);
delay_us(20);
EEPROM_CS_H();
delay_us(20);
return read_data;
}
|