驱动程序一直原子哥,正常操作的读写没问题,因为项目需要读写json,所系写了一个读取json,并判断json数据是否正确的程序,如下:
这是正常读取数据的程序:
//读取SPI FLASH
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(24bit)
//NumByteToRead:要读取的字节数(最大65535)
void W25QXX_Read(uint8_t *pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead)
{
ESP_LOGI("read_date", "ReadAddr=%d,NumByteToRead=%d", ReadAddr, NumByteToRead);
uint32_t i;
uint8_t Temp = 0;
W25QXX_CS_L; //使能器件
VprocHALWrite(W25X_ReadData); //发送读取命令
VprocHALWrite((uint8_t)((ReadAddr) >> 16)); //发送24bit地址
VprocHALWrite((uint8_t)((ReadAddr) >> 8));
VprocHALWrite((uint8_t)ReadAddr);
for (i = 0; i < NumByteToRead; i++)
{
VprocHALRead(&Temp);
pBuffer[i] = Temp; //循环读数
}
W25QXX_CS_H;
}
这是我用来读取json的程序:
//读取数据缓存,返回一组数据占用flash的大小,可能包含错误的部分,失败返回0
//一组数据:{"created_at":"2020-02-20T08:52:08Z","field1":1.001,"field2":1.002,"field2":1.003}
uint16_t W25QXX_Read_Data(uint8_t *Temp_buff, uint32_t ReadAddr, uint16_t Size_Temp_buff)
{
W25QXX_PowerDown();
W25QXX_WAKEUP();
ESP_LOGI("read_date", "ReadAddr=%d,Size_Temp_buff=%d", ReadAddr, Size_Temp_buff);
uint16_t i = 0, j = 0;
uint8_t Temp = 0;
bool start_flag = false;
W25QXX_CS_L; //使能器件
VprocHALWrite(W25X_ReadData); //发送读取命令
VprocHALWrite((uint8_t)((ReadAddr) >> 16)); //发送24bit地址
VprocHALWrite((uint8_t)((ReadAddr) >> 8));
VprocHALWrite((uint8_t)ReadAddr);
for (i = 1; i < Size_Temp_buff; i++)
{
VprocHALRead(&Temp);
if (Temp == '{' && start_flag == false) //数据开头
{
Temp_buff[j] = Temp; //循环读数
start_flag = true;
j = 1;
}
else if (start_flag == true) //开始一组数据计数
{
if (Temp == '}') //一组数据结束
{
Temp_buff[j] = Temp; //循环读数
return i; //返回从缓存中读了多少字节,用来计算下次读取的开始地址
}
else if (Temp == '{') //数据缓存时出现中断,重新计数
{
memset(Temp_buff, 0, (j + 1));
j = 0;
start_flag = false;
ESP_LOGE("read_date", "data broke");
}
else
{
Temp_buff[j] = Temp; //循环读数
j++;
}
}
else
{
ESP_LOGE("read_date", "no data,i=%d,temp=%c", i, Temp);
// no_data_len++
}
}
W25QXX_CS_H;
ESP_LOGE("read_date", "read over ,no correct data");
return 0;
}
然后出现了如下问题,
这是执行程序:
uint8_t read_buff[104] = {0};
const char *test_json = "{\"created_at\":\"2020-02-28T14:54:03Z\",\"field6\":\"-51\"}{\"created_at\":\"2020-02-28T14:54:03Z\",\"field6\":\"-51\"}";
SPI_FLASH_Init();
W25QXX_Write((uint8_t *)test_json, 0, 104);
W25QXX_Read(read_buff, 0, 104);
printf("1\n%s\n", read_buff);
memset(read_buff, 0, 104);
W25QXX_Read_Data(read_buff, 0, 104);
printf("2\n%s\n", read_buff);
memset(read_buff, 0, 104);
// uint8_t read_buff_2[104] = {0};
W25QXX_Read(read_buff, 0, 104);
printf("3\n%s\n", read_buff);
memset(read_buff, 0, 104);
W25QXX_Read(read_buff, 0, 104);
printf("4\n%s\n", read_buff);
memset(read_buff, 0, 104);
输出结果:
1
{“created_at”:“2020-02-28T14:54:03Z”,“field6”:"-51"}{“created_at”:“2020-02-28T14:54:03Z”,“field6”:"-51"}2
{“created_at”:“2020-02-28T14:54:03Z”,“field6”:"-51"}3
eated_at":“2020-02-28T14:54:03Z”,“field6”:"-51"}{“created_at”:“2020-02-28T15:54:20Z”,“field6”:"-55"}{“cr
4
{“created_at”:“2020-02-28T14:54:03Z”,“field6”:”-51"}{“created_at”:“2020-02-28T14:54:03Z”,“field6”:"-51"}
可以看到,在执行了 W25QXX_Read_Data 函数后,再去用 W25QXX_Read 读同样的地址,数据已经乱了,但是再读一遍,又恢复了正常。
苦苦查找无解,最终解决办法:
每次读取数据前,重启W25Q128,即写 W25Q128 掉电和唤醒寄存器。
。。。。。。。
妈的,文本编辑到一般,发现问题了,W25QXX_Read_Data,中在return前没有 执行W25QXX_CS_H,太大意了!!!
同时上传 ESP32 的 w25q128 驱动程序。