📄 spi_ee.c
字号:
* - WriteAddr : EEPROM's internal address to write to.
* - NumByteToWrite : number of bytes to write to the EEPROM,
* must be equal or less than "SPI_PageSize" value.
* Output : None
* Return : None
*******************************************************************************/
void SPI_EE_PageWrite(u8* pBuffer, u16 WriteAddr, u8 NumByteToWrite)
{
/* enable the write access to the EEPROM */
BSPI_EE_WriteEnable();
/* select the EEPROM */
BSPI_EE_ChipSelect(Low);
/* send "Write to Memory " instruction */
Send_Byte(WRITE);
/* send the high byte of the EEPROM's internal address to write to */
Send_Byte(WriteAddr >> 8);
/* send the low byte of the EEPROM's internal address to write to */
Send_Byte(WriteAddr & 0xFF);
while(NumByteToWrite--) /* while there is data to be written */
{
/* send the current byte */
Send_Byte(*pBuffer);
/* point to the next byte to be written */
pBuffer++;
}
/* deselect the EEPROM */
BSPI_EE_ChipSelect(High);
/* waiting loop until write opertaion has completed */
BSPI_EE_WaitForLastTask();
}
/*******************************************************************************
* Function Name : SPI_EE_BufferRead
* Description : Reads a block of data from the EEPROM.
* Input : - pBuffer : pointer to the buffer that receives the data read
* from the EEPROM.
* - ReadAddr : EEPROM's internal address to read from.
* - NumByteToRead : number of bytes to read from the EEPROM.
* Output : None
* Return : None
*******************************************************************************/
void SPI_EE_BufferRead(u8* pBuffer, u16 ReadAddr, u16 NumByteToRead)
{
/* select the EEPROM */
BSPI_EE_ChipSelect(Low);
/* send "Read from Memory " instruction */
Send_Byte(READ);
/* send the high byte of the EEPROM's internal address to read from */
Send_Byte(ReadAddr >> 8);
/* send the low byte of the EEPROM's internal address to read from */
Send_Byte(ReadAddr & 0xFF);
while(NumByteToRead--) /* while there is data to be read */
{
/* read a byte from the EEPROM */
*pBuffer = Send_Byte(Dummy_Byte);
/* point to the next location where the byte read will be saved */
pBuffer++;
}
/* deselect the EEPROM */
BSPI_EE_ChipSelect(High);
}
/*******************************************************************************
* Function Name : SPI_EE_DMABufferRead
* Description : Reads a block of data from the EEPROM. This function use the
* DMA0 stream0 to store the data received by the BSPI2.
* Input : - pBuffer : pointer to the buffer that receives the data read
* from the EEPROM.
* - ReadAddr : EEPROM's internal address to read from.
* - NumByteToRead : number of bytes to read from the EEPROM.
* Output : None
* Return : None
*******************************************************************************/
void SPI_EE_DMABufferRead(u8* pBuffer, u16 ReadAddr, u16 NumByteToRead)
{
DMA_InitStructure.DMA_BufferSize = NumByteToRead;
DMA_InitStructure.DMA_DSTBaseAddr = (u32)pBuffer;
DMA_Init(DMA0, &DMA_InitStructure);
/* select the EEPROM */
BSPI_EE_ChipSelect(Low);
/* send "Read from Memory " instruction */
Send_Byte(READ);
/* send the high byte of the EEPROM's internal address to read from */
Send_Byte( ReadAddr >> 8);
/* send the low byte of the EEPROM's internal address to read from */
Send_Byte(ReadAddr & 0xFF);
/* purge the receive FIFO */
BSPI_RxFifoDisable(BSPI2);
/* enable DMA0 stream0 */
DMA_Cmd(DMA0, DMA_Stream0, ENABLE);
/* enable BSPI2 DMA interface */
BSPI_DMACmd(BSPI2, ENABLE);
while(NumByteToRead--) /* while there is data to be read */
{
/* send byte through the BSPI2 peripheral */
BSPI_WordSend(BSPI2, Dummy_Byte);
/* loop while Transmit FIFO is full */
while(BSPI_FlagStatus(BSPI2, BSPI_FLAG_TFF) == SET);
}
/* deselect the EEPROM */
BSPI_EE_ChipSelect(High);
/* disable BSPI2 DMA interface */
BSPI_DMACmd(BSPI2, DISABLE);
}
/*******************************************************************************
* Function Name : SPI_EE_Erase
* Description : Fills the whole memory with value 0xFF. This will set the
* memory to its initial delivery state.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SPI_EE_Erase()
{
u8 PageIndex = 0, Counter = 0;
u16 PageAddr = 0;
for(PageIndex = 32; PageIndex != 0; PageIndex--)
{
/* enable the write access to the EEPROM */
BSPI_EE_WriteEnable();
/* select the EEPROM */
BSPI_EE_ChipSelect(Low);
/* send "Write to Memory " instruction */
Send_Byte(WRITE);
/* send the high byte of the EEPROM's internal page address to write to */
Send_Byte(PageAddr >> 8);
/* send the low byte of the EEPROM's internal page address to write to */
Send_Byte(PageAddr &0xFF);
for(Counter = 32; Counter != 0; Counter--) /* Page write */
{
Send_Byte(0xFF);
}
/* deselect the EEPROM */
BSPI_EE_ChipSelect(High);
/* waiting loop until write opertaion has completed */
BSPI_EE_WaitForLastTask();
/* select the next page address to write to */
PageAddr += 32;
}
}
/*******************************************************************************
* Function Name : SPI_EE_ReadStatus
* Description : Returns the EEPROM's status register.
* Input : None
* Output : None
* Return : EEPROM's status register value .
*******************************************************************************/
static u8 SPI_EE_ReadStatus()
{
u8 EE_Status = 0;
/* select the EEPROM */
BSPI_EE_ChipSelect(Low);
/* send "Read Status Register" instruction */
Send_Byte(RDSR);
/* send a dummy byte to generate the clock needed by the EEPROM
and put the value of the status register in EE_Status variable */
EE_Status = Send_Byte(Dummy_Byte);
/* deselect the EEPROM */
BSPI_EE_ChipSelect(High);
/* return the status register value */
return (EE_Status);
}
/*******************************************************************************
* Function Name : BSPI_EE_ChipSelect
* Description : Selects and deselects the EEPROM.
* Input : State : level to be applied on the EEPROM's ChipSelect pin.
* Output : None
* Return : None
*******************************************************************************/
static void BSPI_EE_ChipSelect(u8 State)
{
GPIO_BitWrite(GPIO5, GPIO_PIN_3, (BitAction)State);
}
/*******************************************************************************
* Function Name : Send_Byte
* Description : Sends a byte through the BSPI interface and return the byte
* received from the SPI bus.
* Input : byte : byte to send.
* Output : None
* Return : The value of the received byte.
*******************************************************************************/
static u8 Send_Byte(u8 byte)
{
/* send byte through the BSPI2 peripheral */
BSPI_WordSend(BSPI2, byte);
/* loop while Transmit FIFO is full */
while(BSPI_FlagStatus(BSPI2, BSPI_FLAG_TFF) == SET);
/* loop while Receive FIFO is empty */
while(BSPI_FlagStatus(BSPI2, BSPI_FLAG_RFNE) == RESET);
/* return the byte read from the SPI bus */
return(BSPI_WordReceive(BSPI2));
}
/*******************************************************************************
* Function Name : BSPI_EE_WriteEnable
* Description : Enables the write access to the EEPROM.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void BSPI_EE_WriteEnable()
{
/* select the EEPROM */
BSPI_EE_ChipSelect(Low);
/* send "Write Enable" instruction */
Send_Byte(WREN);
/* deselect the EEPROM */
BSPI_EE_ChipSelect(High);
}
/*******************************************************************************
* Function Name : BSPI_EE_WaitForLastTask
* Description : Polls the status of the Write In Progress (WIP) flag in the
* EEPROM's status register and loop until write opertaion
* has completed.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void BSPI_EE_WaitForLastTask()
{
u8 EE_Status = 0;
/* loop as long as the memory is busy with a write cycle */
do
{
EE_Status = SPI_EE_ReadStatus();
} while((EE_Status & WIP_Flag) == Write_In_Progress);
}
/******************* (C) COPYRIGHT 2005 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -