[SOLVED] Weird SPI flash problem (Winbond W25Q32JV) - Page 1
Forum section: electronics / projects

author: Unixon
author_rank: Frequent Contributor
timestamp: July 08, 2020, 01:42:16 pm
content: Hi, everyone.I have a couple of strange problems with W25Q32JV SPI flash chip on a piece of hardware with STM32F051 MCU. I'm running SPI at 12Mbps.1) After the first write operation something starts inserting 2 or 3 leading zero bytes at every following read transaction. First read gets 2 or 3 zero bytes, all next reads get 2 zero bytes before actual data. Data bytes are correct, but shifted, last 2 or 3 bytes of the actual data are missing (beyond transaction window). All read operations before the first write operation return correct data without leading zero bytes.Steps to reproduce:1.1 erase a sector, write one or more pages, reset uC, do not power off1.2 read one or more pages of that sector - all bytes are correct1.3 erase the sector again and write one or more pages1.4 read one or more pages of that sector again - every read result is now preceded with zero bytes2) Write operations to the first page of every sector, but the first sector, seem to be ignored, while the first page of the first sector always writes successfully. Under some conditions, I haven't figured this out yet, writing to the first page of a sector individually succeeds, but after next sector erase operation that page stays empty. Zero bytes also appear over empty pages.Steps to reproduce:1.1 erase 2 or more adjacent sectors1.2 write some data to these sectors1.3 read data back - all bytes are correct except for first pages of 2nd and following sectors, these pages are still filled with 0xFFI'm trying to figure out is it a hardware of a software problem. Since I'm not powering off the flash chip, 'first' operation I'm referring to isn't really the first for the flash chip, but they are truly first for MCU SPI hardware and firmware. After a long debugging I can say it is unlikely a memory access problem in the firmware or another bug like that and SPI flash driver looks up to documentation. Yet, something is very wrong.Any ideas?p.s. Would it help if I provide schematics and related pieces of firmware?

author: Unixon
author_rank: Frequent Contributor
timestamp: July 09, 2020, 10:06:26 am
content: Update:Hooked my scope to SPI bus, looks like flash IC still returns correct data when leading zeroes appear.Then this weird thing happens somewhere inside MCU. OK, at least it's not a faulty flash IC.Is there anything notorious about STM32 SPI hardware or HAL library that can do this stuff?Am I not reading out some internal SPI buffers after previous transactions?

author: Unixon
author_rank: Frequent Contributor
timestamp: July 09, 2020, 10:39:29 am
content: Update:Well, it appears that in fact leading zero bytes were a buffering problem, calling HAL_SPIEx_FlushRxFifo() before SPI transaction for data cleared one of issues.But why does the SPI buffers anything anyway? Shouldn't it always transmit and receive equal amount of data?Probably HAL splits Rx/Tx streams and puts an unused part of SPI exchange in Tx-only or Rx-only transfers into buffer instead of throwing it away immediately.

author: Unixon
author_rank: Frequent Contributor
timestamp: July 09, 2020, 12:52:04 pm
content: Update:OK, problem solved. Fixing one issue with SPI buffering automatically fixed the other one too, however, it is not clear how.