#include "eeprom_at25df021.h" #include "stm32f10x_gpio.h" #include "stm32f10x_spi.h" /* Private variables ---------------------------------------------------------*/ SPI_InitTypeDef SPI_InitStructure; /*----------------------------------------------------------------------------- ROUTINE NAME : SPI_Init INPUT/OUTPUT : None DESCRIPTION : Configure the SPI peripheral COMMENTS : -----------------------------------------------------------------------------*/ void SPI_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); /* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA, GPIO_Pin_4); /* SPI1 configuration */ SPI_I2S_DeInit(SPI1); 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(SPI1, &SPI_InitStructure); /* Enable SPI1 */ SPI_Cmd(SPI1, ENABLE); } /*----------------------------------------------------------------------------- ROUTINE NAME : Chk_WP_Bit INPUT/OUTPUT : None/Boolean DESCRIPTION : Return 'TRUE' if write is still in progress in the EEPROM (M95040) COMMENTS : -----------------------------------------------------------------------------*/ /*void Chk_WP_Bit(void) { u8 i; SPI_Cmd(SPI1, ENABLE);//SetBit(SPICR,6); // spi enable GPIO_ResetBits(GPIOA,GPIO_Pin_4); //SPI_SendByte(RDSR); //SPI_SendByte(RDSR); while((SPI1->SR & SPI_I2S_FLAG_TXE) == RESET); SPI1->DR=RDSR; while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET); i=SPI1->DR; while((SPI1->SR & SPI_I2S_FLAG_TXE) == RESET); SPI1->DR=RDSR; while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET); i=SPI1->DR; GPIO_SetBits(GPIOA,GPIO_Pin_4);//SetBit(PADR,6); SPI_Cmd(SPI1, DISABLE);//ClrBit(SPICR,6); // spi disable if(((SPI1->DR)|0xFE)==0xFE) return(TRUE);//(ValBit(SPIDR,0)) return(TRUE); else return(FALSE); }*/ /*----------------------------------------------------------------------------- ROUTINE NAME : Read_EEPROM INPUT/OUTPUT : u8 (adress) / u8 (data) DESCRIPTION : Return data located at the adress specified COMMENTS : -----------------------------------------------------------------------------*/ void Read_EEPROM(u32 add) { u32 data; SPI_Cmd(SPI1, ENABLE);//SetBit(SPICR,6); // spi enable GPIO_ResetBits(GPIOA,GPIO_Pin_4); SPI_SendByte(READ); SPI_SendByte(add); data=SPI_ReadByte(); GPIO_SetBits(GPIOA,GPIO_Pin_4); SPI_Cmd(SPI1, DISABLE);//ClrBit(SPICR,6); // spi disable return (data); } /*----------------------------------------------------------------------------- ROUTINE NAME : Send_EEPROM INPUT/OUTPUT : u8,u8 (adress,data) / None DESCRIPTION : Send data byte to the adress specified COMMENTS : -----------------------------------------------------------------------------*/ void Send_EEPROM(u32 add, u32 data) { SPI_Cmd(SPI1, ENABLE);//SetBit(SPICR,6); // spi enable GPIO_ResetBits(GPIOA,GPIO_Pin_4);//ClrBit(PADR,6); SPI_SendByte(WREN); GPIO_SetBits(GPIOA,GPIO_Pin_4);//SetBit(PADR,6); GPIO_ResetBits(GPIOA,GPIO_Pin_4);//ClrBit(PADR,6); SPI_SendByte(WRITE); SPI_SendByte(add); SPI_SendByte(data); GPIO_SetBits(GPIOA,GPIO_Pin_4);//SetBit(PADR,6); SPI_Cmd(SPI1, DISABLE);//ClrBit(SPICR,6); // spi disable } /******************************************************************************* * Function Name : SPI_SendByte * Description : Sends a byte through the SPI interface and return the byte * received from the SPI bus. * Input : byte : byte to send. * Output : None * Return : The value of the received byte. *******************************************************************************/ void SPI_SendByte(u8 byte) { /* Loop while DR register in not emplty */ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); /* Send byte through the SPI1 peripheral */ SPI_I2S_SendData(SPI1, byte); /* Wait to receive a byte */ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); /* Return the byte read from the SPI bus */ return SPI_I2S_ReceiveData(SPI1); } /******************************************************************************* * Function Name : SPI_ReadByte * Description : Reads a byte from the SPI Flash. * This function must be used only if the Start_Read_Sequence * function has been previously called. * Input : None * Output : None * Return : Byte Read from the SPI Flash. *******************************************************************************/ void SPI_ReadByte(void) { return (SPI_SendByte(0x55)); } /*-----------------------------------------------------------------------------*/ void Delay_us(u16 us) { while(us--); } /*----------------------------------------------------------------------------- ROUTINE NAME : Delay_ms INPUT/OUTPUT : ms DESCRIPTION : COMMENTS : -----------------------------------------------------------------------------*/ void Delay_ms(u16 ms) { u16 i,j; for(i=0;i