Home > other >  There's some room for part written in STM32 AT45DB161 can't write in.
There's some room for part written in STM32 AT45DB161 can't write in.

Time:10-03

I need to write a project AT45DB161 and driver USES a red bull driver on board, found some space to write, read it out correctly, some space to write after the read out is 00, details are as follows:
Drive part:
/* Private typedef -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */
# define SPI_FLASH_PageSize 0 x210

/* Private define -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
# define READ 0 xd2/* READ from the Memory instruction */
# define the WRITE 0 x82/* WRITE to the Memory instruction */

# define RDID 0 x9f/* Read identification */
# define RDSR 0 xd7/* Read the Status Register instruction */

# define SE 0 x7c/* Sector Erase instruction */
# define 0 x81 PE/* Page Erase instruction */

# define RDY_Flag 0 x80/* Ready/busy (1/0) the status flag */

# define Dummy_Byte 0 xa5

Void SPI_FLASH_Init (void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;

/* the Enable SPI1 and GPIO clocks */
RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIO_CS, ENABLE);

/* Configure SPI1 pins: SCK, MISO and MOSI */
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, & amp; GPIO_InitStructure);

/* Configure I/O for Flash Chip select */
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_CS;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init (GPIO_CS, & amp; GPIO_InitStructure);

/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH ();

/* SPI1 configuration */
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_High;
SPI_InitStructure. SPI_CPHA=SPI_CPHA_2Edge;
SPI_InitStructure. SPI_NSS=SPI_NSS_Soft;
SPI_InitStructure. SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_4;
SPI_InitStructure. SPI_FirstBit=SPI_FirstBit_MSB;
SPI_InitStructure. SPI_CRCPolynomial=7;
SPI_Init (SPI1, & amp; SPI_InitStructure);

/* the Enable SPI1 */
SPI_Cmd (SPI1, ENABLE);
}

Void SPI_FLASH_PageWrite (uint8_t * pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW ();
/* Send "Write to the Memory" instruction */
SPI_FLASH_SendByte (WRITE);
/* Send WriteAddr high nibble the address byte to write to */
SPI_FLASH_SendByte ((WriteAddr & amp; 0 xff0000) & gt;> 16);
/* Send WriteAddr medium nibble the address byte to write to */
SPI_FLASH_SendByte ((WriteAddr & amp; 0 xff00) & gt;> 8);
/* Send WriteAddr low nibble the address byte to write to */
SPI_FLASH_SendByte (WriteAddr & amp; 0 XFF);

/* while there is data to be written on the FLASH */
While (NumByteToWrite -)
{
/* Send the current byte */
PBuffer SPI_FLASH_SendByte (*);
/* Point on the next byte to be written */
PBuffer++;
}

/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH ();

/* Wait the end of the Flash their */
SPI_FLASH_WaitForEnd ();
}

Void SPI_FLASH_BufferWrite (uint8_t * pBuffer, uint32_t WriteStartAddressOnOnePage, uint16_t NumByteToWrite)
{
//uint8_t ByteWritePages=0, ByteWriteleftOnOnepage=0, StartAddressOnOnePage=0, StartLeftBytesOnOnePage=0, temp=0; So the original program, it is estimated that there are wrong, overflow
Uint16_t ByteWritePages=0, ByteWriteleftOnOnepage=0, StartAddressOnOnePage=0, StartLeftBytesOnOnePage=0, temp=0;

StartAddressOnOnePage=WriteStartAddressOnOnePage % SPI_FLASH_PageSize;
StartLeftBytesOnOnePage=SPI_FLASH_PageSize - StartAddressOnOnePage;
ByteWritePages=NumByteToWrite/SPI_FLASH_PageSize;
ByteWriteleftOnOnepage=NumByteToWrite % SPI_FLASH_PageSize;

If (StartAddressOnOnePage==0)/*/* WriteStartAddressOnOnePage is SPI_FLASH_PageSize aligned
{
If (ByteWritePages==0)/* NumByteToWrite & lt; SPI_FLASH_PageSize */
{
SPI_FLASH_PageWrite (pBuffer, WriteStartAddressOnOnePage NumByteToWrite);
}
The else/* NumByteToWrite & gt; SPI_FLASH_PageSize */
{
While (ByteWritePages -)
{
SPI_FLASH_PageWrite (pBuffer, WriteStartAddressOnOnePage SPI_FLASH_PageSize);
WriteStartAddressOnOnePage +=SPI_FLASH_PageSize;
PBuffer +=SPI_FLASH_PageSize;
}

SPI_FLASH_PageWrite (pBuffer, WriteStartAddressOnOnePage ByteWriteleftOnOnepage);
}
}
The else/*/* WriteStartAddressOnOnePage is not SPI_FLASH_PageSize aligned
{
If (ByteWritePages==0)/* NumByteToWrite & lt; SPI_FLASH_PageSize */
{
If (ByteWriteleftOnOnepage & gt; StartLeftBytesOnOnePage)/* (NumByteToWrite + WriteStartAddressOnOnePage) & gt; SPI_FLASH_PageSize */
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related