我一個專案上需要寫AT45DB161。驅動采用的是紅牛板上的驅動,發現有的空間寫進去,讀出來正確,有的空間寫過后讀出來是00,具體情況如下:
驅動部分:
/* Private typedef -----------------------------------------------------------*/
#define SPI_FLASH_PageSize 0x210
/* Private define ------------------------------------------------------------*/
#define READ 0xD2 /* Read from Memory instruction */
#define WRITE 0x82 /* Write to Memory instruction */
#define RDID 0x9F /* Read identification */
#define RDSR 0xD7 /* Read Status Register instruction */
#define SE 0x7C /* Sector Erase instruction */
#define PE 0x81 /* Page Erase instruction */
#define RDY_Flag 0x80 /* Ready/busy(1/0) status flag */
#define Dummy_Byte 0xA5
void SPI_FLASH_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* 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, &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, &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, &SPI_InitStructure);
/* 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 Memory " instruction */
SPI_FLASH_SendByte(WRITE);
/* Send WriteAddr high nibble address byte to write to */
SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
/* Send WriteAddr medium nibble address byte to write to */
SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
/* Send WriteAddr low nibble address byte to write to */
SPI_FLASH_SendByte(WriteAddr & 0xFF);
/* while there is data to be written on the FLASH */
while (NumByteToWrite--)
{
/* Send the current byte */
SPI_FLASH_SendByte(*pBuffer);
/* Point on the next byte to be written */
pBuffer++;
}
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
/* Wait the end of Flash writing */
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; 原程式如此,估計有錯,溢位
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 < SPI_FLASH_PageSize */
{
SPI_FLASH_PageWrite(pBuffer, WriteStartAddressOnOnePage, NumByteToWrite);
}
else /* NumByteToWrite > 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);
}
}
else /* WriteStartAddressOnOnePage is not SPI_FLASH_PageSize aligned */
{
if (ByteWritePages == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
{
if (ByteWriteleftOnOnepage > StartLeftBytesOnOnePage) /* (NumByteToWrite + WriteStartAddressOnOnePage) > SPI_FLASH_PageSize */
{
temp = ByteWriteleftOnOnepage - StartLeftBytesOnOnePage;
SPI_FLASH_PageWrite(pBuffer, WriteStartAddressOnOnePage, StartLeftBytesOnOnePage);
WriteStartAddressOnOnePage += StartLeftBytesOnOnePage;
pBuffer += StartLeftBytesOnOnePage;
SPI_FLASH_PageWrite(pBuffer, WriteStartAddressOnOnePage, temp);
}
else
{
SPI_FLASH_PageWrite(pBuffer, WriteStartAddressOnOnePage, NumByteToWrite); /////
}
}
else /* NumByteToWrite > SPI_FLASH_PageSize */
{
NumByteToWrite -= StartLeftBytesOnOnePage;
ByteWritePages = NumByteToWrite / SPI_FLASH_PageSize;
ByteWriteleftOnOnepage = NumByteToWrite % SPI_FLASH_PageSize;
SPI_FLASH_PageWrite(pBuffer, WriteStartAddressOnOnePage, StartLeftBytesOnOnePage);
WriteStartAddressOnOnePage += StartLeftBytesOnOnePage;
pBuffer += StartLeftBytesOnOnePage;
while (ByteWritePages--)
{
SPI_FLASH_PageWrite(pBuffer, WriteStartAddressOnOnePage, SPI_FLASH_PageSize);
WriteStartAddressOnOnePage += SPI_FLASH_PageSize;
pBuffer += SPI_FLASH_PageSize;
}
if (ByteWriteleftOnOnepage != 0)
{
SPI_FLASH_PageWrite(pBuffer, WriteStartAddressOnOnePage, ByteWriteleftOnOnepage);
}
}
}
}
void SPI_FLASH_BufferRead(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead)
{
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send "Read from Memory " instruction */
SPI_FLASH_SendByte(READ);
/* Send ReadAddr high nibble address byte to read from */
SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
/* Send ReadAddr medium nibble address byte to read from */
SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
/* Send ReadAddr low nibble address byte to read from */
SPI_FLASH_SendByte(ReadAddr & 0xFF);
/* Read a byte from the FLASH */
SPI_FLASH_SendByte(Dummy_Byte);
/* Read a byte from the FLASH */
SPI_FLASH_SendByte(Dummy_Byte);
/* Read a byte from the FLASH */
SPI_FLASH_SendByte(Dummy_Byte);
/* Read a byte from the FLASH */
SPI_FLASH_SendByte(Dummy_Byte);
while (NumByteToRead--) /* while there is data to be read */
{
/* Read a byte from the FLASH */
*pBuffer = SPI_FLASH_SendByte(Dummy_Byte);
/* Point to the next location where the byte read will be saved */
pBuffer++;
}
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
}
注:在SPI_FLASH_BufferWrite函式里,變數宣告原為uint8_t 我改為uint16_t
因為頁為0-4096,不應該宣告為unsigned char.估計有錯溢位。故改為int16_t
在主函式里,是這樣寫的
U16 addr;
U8 save_data8bit[8]={0x99,0x44,0x33,0x44,0x55,0x66,0x77,0x88};
addr = 200 + Store_Pointer*31*2;
SPI_FLASH_BufferWrite(save_data8bit,addr,2);
OSTimeDly(500);
memset(save_data8bit,0x00,100);
SPI_FLASH_BufferRead(save_data8bit,addr,2);
試驗中發現
當store_pointer有些空間寫入后讀出是正確的,有些空間寫入后讀出是0(不能肯定一定是寫入錯)
如下:
store_pointer=0->5,正確
store_pointer=6->13,錯誤
store_pointer=14->21,正確
store_pointer=22->28,錯誤
store_pointer=29->38,正確
store_pointer=39->45,錯誤
因為讀寫僅二個位元組,不存在跨頁問題。
芯片也換了幾個,故障依舊。
感覺到是偶數頁面寫得進去,奇數頁面寫不進去。
怎么也查不出是什么問題,切望各位大神指點。謝謝謝
uj5u.com熱心網友回復:
問題已解決,這個驅動程式有錯誤。改正后就正常了uj5u.com熱心網友回復:
可以講講具體是哪里問題嗎 我也遇到奇數頁讀寫錯誤的問題轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/106987.html
標籤:單片機/工控
