二、使用示例
1.W25qxx数据写入管理
//将Flash看做一个FIFO,读写指针进行控制,每次先从读写指针地址中读出相应值
void ReceiveInterMess(void)
{
uint8_t ucData;
uint8_t ucaInterBuf[256];
uint8_t usInterPos = 0;
uint8_t usRxLength = 0;
if(InterFifo.RxLength > 0) //判断FIFO中是否有数据
{
HAL_Delay(1000);
__HAL_UART_DISABLE_IT(&huart2, UART_IT_RXNE); //延迟1S确保接收完全
usRxLength = InterFifo.RxLength;
for ( ; usRxLength > 0; usRxLength--) //接收FIFO所以数据
{
if (InterRxBufferRead(&ucData))
{
ucaInterBuf[usInterPos++] = ucData;
}
}
InterRxBufferClear();
__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
}
if (usInterPos > 0)
{
//读取读写指针地址
W25QXX_Read(ucaWriteAddrValue, uiWriteSaveAddr, 3);
uiUserWriteAddr = ucaWriteAddrValue[2] + (ucaWriteAddrValue[1] << 8) + (ucaWriteAddrValue[0] << 16);
W25QXX_Read(ucaReadAddrValue, uiReadSaveAddr, 3);
uiUserReadAddr = ucaReadAddrValue[2] + (ucaReadAddrValue[1] << 8) + (ucaReadAddrValue[0] << 16);
//写入数据到Flash中
W25QXX_Write_Page(&usInterPos, uiUserWriteAddr, 1);
W25QXX_Write_Page(ucaInterBuf, uiUserWriteAddr+W25X_INTER_PAGE_ADDR, usInterPos);
W25QXX_Read(&ucDataCounter, uiDataCounterAddr, 1);
//队列满了,全部擦除
if ((uiUserWriteAddr + W25X_INTER_SECTOR_ADDR) % W25X_SECTOR_ADDR_MAX == uiUserReadAddr)
{
#if TEST_FALG
printf("队列满,全部擦除\r\n");
#endif
W25QXX_ExceptionHandling();
return ;
}
//刷新写地址
uiUserWriteAddr = (uiUserWriteAddr + W25X_INTER_SECTOR_ADDR) % W25X_SECTOR_ADDR_MAX;
ucaWriteAddrValue[0] = (uiUserWriteAddr >> 16);
ucaWriteAddrValue[1] = (uiUserWriteAddr >> 8);
ucaWriteAddrValue[2] = uiUserWriteAddr;
W25QXX_Erase_Sector(uiWriteSaveAddr);
W25QXX_Write_Page(ucaWriteAddrValue, uiWriteSaveAddr, 3);
ucDataCounter++;
W25QXX_Erase_Sector(uiDataCounterAddr);
W25QXX_Write_Page(&ucDataCounter, uiDataCounterAddr, 1);
#if TEST_FALG
printf("捕获一条数据:");
for (int i = 0; i < usInterPos; i++)
printf( " %02X", ucaInterBuf[i]);
printf("\r\n待发数据条数:%d\r\n", ucDataCounter);
printf("\r\nFlash读地址:%02X\r\n", uiUserReadAddr);
printf("Flash写地址:%02X\r\n", uiUserWriteAddr);
#endif
}
}
|