0
\$\begingroup\$

My querry is, I am trying to write data from a file stored in by Raspberry PI to an external flash. The size of the file is greater than 256 bytes. As Flash allows writing a max of only 256 bytes in 1 command, My idea of implementing a multiple page write was by first reading the entire data into an array at first. Then run a for loop with each time the sequence being enabling the write, write command and then the data.

I feel I am making some error when I am writing the data from the file to the array.. Am i missing out on the logic somewhere here.

Edit: Adding a more specific question: When I run, the data doesn't get transferred to the Flash. When i use a Flash read command later, all the data read just appear as boxes in the terminal.

\$\endgroup\$
1
  • \$\begingroup\$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. \$\endgroup\$
    – Community Bot
    Commented Mar 29 at 13:20

2 Answers 2

0
\$\begingroup\$

Potential code issue with multiple pages to be written

The code structure uses a loop where each iteration:

  • Issues a Write Enable command.
  • Issues a Page Program command.

The problem with this is that Page Program command can take some time to complete, before the flash is ready for a further program or erase command. Before issuing a program or erase command, a Read Status Register-1 command should be used and the WRITE IN PROGRESS (BUSY) status bit checked to confirm the flash is idle before sending the next program or erase command. From the W25Q32JV datasheet:

Erase/Write In Progress (BUSY) – Status Only

BUSY is a read only bit in the status register (S0) that is set to a 1 state when the device is executing a Page Program, Quad Page Program, Sector Erase, Block Erase, Chip Erase, Write Status Register or Erase/Program Security Register instruction. During this time the device will ignore further instructions except for the Read Status Register and Erase/Program Suspend instruction (see tW, tPP, tSE, tBE, and tCE in AC Characteristics). When the program, erase or write status/security register instruction has completed, the BUSY bit will be cleared to a 0 state indicating the device is ready for further instructions.


Other debugging suggestions

When i use a Flash read command later, all the data read just appear as boxes in the terminal.

The other answer already notes the flash must be erased before it can be programmed - since erasing changes bits from 0 to 1 and programming can only changes bits from a 1 to 0.

Some other suggestions for debugging:

  1. Have any tests been performed to check basic communication between the Raspberry PI and W25Q32JV? E.g. send the following commands and check if the flash returns the expected byte values described in the datasheet:
    • Manufacturer/Device ID (90h)
    • JEDEC ID (9Fh)
  2. After sending the Write Enable command, issue a Read Status Register-1 command and check if the WRITE ENABLE LATCH (WEL) bit is set to confirm if the write enable command was accepted.
\$\endgroup\$
0
\$\begingroup\$

At least you are not enabling writes before trying to write it.

You should maybe try to do one thing at a time. Like write 1 byte, then 256 succesfully even without bothering to think from where the data comes from. When you have simple functions that are verified to work, then just call them for any data you want, from array or from file. Now that is very spaghetti code, everything from file reading to writing flash in a single function.

It might also be beneficial to erase the sectors or whole chip, writing data only works correctly if the flash is already erased. And polling for chip being ready after write may end up being faster, and allows you to verify it was written correctly.

Edit: OK you do enable writes.

But you are mixing malloc and delete. If you do something, do it consistently. Write plain C or C++, not a mixture. You can't allocate with malloc() and dispose it with delete[].

\$\endgroup\$
1
  • \$\begingroup\$ @AD97 Exactly, it does not work if you call enablewrite once before this function. It must be called in the for loop every time before each page write command. \$\endgroup\$
    – Justme
    Commented Mar 29 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.