既然被推荐了,那我就把昨天调了一晚上的核心代码部分传上来。已经测试通过了,非常好用。
SPI_FLASH_SectorErase(FLASH_OFFLINE_LENGTH);
SPI_FLASH_SectorErase(FLASH_OFFLINE_DATA);
offlineLengthInit();
char data[] = "0102030405040304938473625364738473645263748594837463547182736475847364";
int index = 60;
while(index > 0 )
{
writeOfflineData(data, strlen(data));
index--;
}
readOfflineData();
readOfflineData();
readOfflineData();
//初始化离线数据扇区
void offlineDataInit(int data_index)
{
SPI_FLASH_SectorErase(W25Q128FV_SUBSECTOR_SIZE * data_index);
uint8_t data_length[5] = {0};
sprintf((char*)data_length, "%04x", 0);
SPI_FLASH_BufferWrite(data_length, W25Q128FV_SUBSECTOR_SIZE * data_index, 4);
}
// 初始化离线数据标识扇区
void offlineLengthInit(void)
{
uint8_t data_index_length[5] = {0};
SPI_FLASH_SectorErase(FLASH_OFFLINE_LENGTH);
sprintf((char*)data_index_length, "%04x", 6);
SPI_FLASH_BufferWrite(data_index_length, FLASH_OFFLINE_LENGTH, 4);
offlineDataInit(6);
}
// 修改离线数据标识
u32 newOfflineLength(int d_index)
{
if(d_index < 6 ){
d_index = 6;
}
if(d_index > 256 ){
d_index = 256;
}
printf("\r\n newOfflineLength: %d \r\n", d_index);
uint8_t data_index[5] = {0};
SPI_FLASH_SectorErase(FLASH_OFFLINE_LENGTH);
sprintf((char*)data_index, "%04x", d_index);
SPI_FLASH_BufferWrite(data_index, FLASH_OFFLINE_LENGTH, 4);
return W25Q128FV_SUBSECTOR_SIZE * d_index;
}
void readOfflineData(void)
{
// 查询离线数据标识确定最后数据地址
uint8_t data_index[5] = {0};
SPI_FLASH_BufferRead(data_index, FLASH_OFFLINE_LENGTH, 4);
int d_index = charhex_to_dec((char*)data_index);
printf("\r\n readOfflineData: %d \r\n", d_index);
u32 data_address = W25Q128FV_SUBSECTOR_SIZE * d_index;
// 查询离线数据标识扇区内数据长度标识
uint8_t data_length[5] = {0};
SPI_FLASH_BufferRead(data_length, data_address, 4);
int d_length = charhex_to_dec((char*)data_length);
SPI_FLASH_BufferRead(read_data, data_address + 4, d_length);
SPI_FLASH_SectorErase(data_address);
if(d_index > 6){
newOfflineLength(d_index - 1);
}
printf("\r\n 读出长度为:%d \r\n", d_length);
printf("\r\n 读出的数据为:%s \r\n", read_data);
printf("\r\n 读出长度为:%d \r\n", strlen((char*)read_data));
memset(read_data, 0, 4069);
}
void writeOfflineData(char* data, int dataLength)
{
// 查询离线数据标识确定最后数据地址
uint8_t data_index[5] = {0};
SPI_FLASH_BufferRead(data_index, FLASH_OFFLINE_LENGTH, 4);
int d_index = charhex_to_dec((char*)data_index);
u32 data_address = W25Q128FV_SUBSECTOR_SIZE * d_index;
// 查询离线数据标识扇区内数据长度标识
uint8_t data_length[5] = {0};
SPI_FLASH_BufferRead(data_length, data_address, 4);
int d_length = charhex_to_dec((char*)data_length);
// 如果新数据+扇区数据长度>扇区长度
if((dataLength + d_length) > W25Q128FV_SUBSECTOR_SIZE){
// 开辟新扇区
data_address = newOfflineLength(d_index + 1);
SPI_FLASH_SectorErase(data_address);
sprintf((char*)write_data, "%04x%s", dataLength, data);
SPI_FLASH_BufferWrite(write_data, data_address, strlen((char*)write_data));
printf("\r\n 写入的数据为:%s \r\t", write_data);
printf("\r\n 长度:%d \r\t", strlen((char*)write_data));
} else if(d_length == 0)
{
SPI_FLASH_SectorErase(data_address);
sprintf((char*)write_data, "%04x%s", dataLength, data);
SPI_FLASH_BufferWrite(write_data, data_address, strlen((char*)write_data));
} else
{
// 将旧数据读出,修改扇区数据长度,末尾追加新数据
SPI_FLASH_BufferRead(read_data, data_address + 4, d_length);
SPI_FLASH_SectorErase(data_address);
sprintf((char*)write_data, "%04x%s%s%s", dataLength + d_length + 1, read_data, ",", data);
SPI_FLASH_BufferWrite(write_data, data_address, strlen((char*)write_data));
}
memset(write_data, 0, 4069);
}
只只是个DEMO,还没往产品里移植呢,如果需要完整代码的,可以找我要。 |