0
\$\begingroup\$

I have a PIC18F67K22 controller. I am a making data logging system using an M25PE80 external flash memory for storing data. I have interfaced this flash to the controller using SPI.

My question is when we write or read from flash, are the addresses automatically incremented or must we do it manually?

(If anyone has some demo code, it will be helpful.)

\$\endgroup\$
  • \$\begingroup\$ Do you understand the concepet of pages implemented by this memory? Have you read the differences between PAGE WRITE and PAGE PROGRAM? Both do autoincrement of the address up to the 256 byte page size. The datasheet recommends writing in large contiguous blocks rather than small byte based writes. \$\endgroup\$ – David Oct 21 '13 at 8:07
  • \$\begingroup\$ @David:Yes I understand pages implemented concept in memory.But I did not read about difference between PP and PW command in data sheet.My requirement is to write 13 bytes at time not a 256 bytes.Can you explain what is the difference between PP and PW?? \$\endgroup\$ – shail korat Oct 21 '13 at 9:05
3
\$\begingroup\$

Throughout this post I will refer to this datasheet found on micron.com.

You have asked two questions above which I will cover here:

  1. Is the address pointer incremented automatically during read/write
  2. What is the difference between PAGE WRITE and PAGE PROGRAM

Memory organisation

First it helps to understand how the memory is arranged internally. The device has 8Mb total flash memory, from page 5 of the datasheet we see:

The memory is organized as 16 sectors, divided into 16 subsectors each (256 subsectors total). Each sector contains 256 pages and each subsector contains 16 pages. Each page is 256 bytes wide. The entire memory can be viewed as consisting of 4096 pages, or 1,048,576 bytes.

(emphasis added by me)

It is important to note that a page, which totals 256 bytes, is the smallest unit we can operate on at once. There are 4096 of these which give our total memory area. We see on the same page that:

The memory can be written or programmed 1 to 256 bytes at a time

You specified in a comment that you want to write 13 bytes at a time. So how would this work?

Addressing

Firstly let's take a look at how we address different parts of the flash memory. This is relevant for both reading and writing.

Reading data bytes http://img5.imageshack.us/img5/6811/rzz0.png

From page 27 we can see that:

The device is first selected by driving chip select (S#) LOW. The command code for READ DATA BYTES is followed by a 3-byte address (A23-A0), each bit being latched-in during the rising edge of serial clock (C).

Why are three bytes required to address the memory? Remember that we have a total of 256 bytes * 4096 pages = 1048576 bytes total. This is equivalent to 2^20, so we would need 20 bits in order to address every byte individually.

There are two good reasons that three bytes are used for the address:

  • it aligns the SPI data on a byte boundary, which greatly simplifies the protocol
  • it allows larger memory size parts (e.g. 16Mb or 64Mb) to be dropped in as a replacement with minimal changes to your code

Writing data

Here's the equivalent timing diagram for a write:

Page write timing diagram http://img6.imageshack.us/img6/6973/wwea.png

We can see the 24 bit (3 byte) address followed by a number of data bytes, up to 256 total. Both types of write (PAGE PROGRAM and PAGE WRITE) share these three common characteristics, which are important to address question 1.

Firstly:

If the eight least-significant address bits (A7-A0) are not all zero, all transmitted data that goes beyond the end of the current page are programmed from the start address of the same page; that is, from the address whose eight least significant bits (A7-A0) are all zero.

So this suggests that you can address any byte individually in flash memory. You are not constrained to starting at the beginning of a page. If you wish to address byte 913409 you could.

Secondly:

If more than 256 bytes are sent to the device, previously latched data are discarded and the last 256 data bytes are guaranteed to be programmed correctly within the same page.

This constraint comes from our page size of 256 bytes. If we write bytes 0..256 (257 total) then the first byte we wrote will be overwritten.

Thirdly:

If less than 256 data bytes are sent to device, they are correctly programmed at the requested addresses without any effects on the other bytes of the same page.

This tells us that we can write less than 256 bytes successfully. We do not need to read 256 bytes, modify them then write back to the device. You can write your 13 bytes, come out of write mode and the other 243 bytes will be unaffected.

However notice from the above that the address only needs to be sent once. It is a starting address, which is then auto-incremented inside the flash part as you write more data. This is our answer to question 1.

PAGE PROGRAM vs. PAGE WRITE

Finally an answer to question 2, which you asked in the comments. Two quotes from the datasheet that explain the difference. For PAGE WRITE:

The PAGE WRITE command allows bytes in the memory to be programmed. [.. snip ..] The PAGE WRITE command performs a page erase cycle even if only one byte is updated.

And for PAGE PROGRAM:

The PAGE PROGRAM command allows bytes in the memory to be programmed, which means the bits are changed from 1 to 0.

The PAGE PROGRAM command is interesting, because it only allows us to change from 1 to 0. When a page is erased the bytes are all set to the default value 0xFF (all binary 1). We can then change some bits back to 0 without incurring the PAGE ERASE, which will eventually wear out the flash (this part is rated to >100k cycles).

Why would this be useful? Well from the timing diagram on page 51 we see the below:

PAGE WRITE vs. PAGE PROGRAM timings http://img689.imageshack.us/img689/1106/9kaj.png

So we can see that PAGE PROGRAM is significantly faster by ~8 times in both typical (11/1.35ms) and max (25/3ms). (Note: this is a very crude comparison as it doesn't include the implicit PAGE ERASE that a PAGE WRITE will always run)

So the answer to question 2 is that PAGE PROGRAM is faster but less flexible.

A fuller discussion can be found in the datasheet under the headings "An easy way to modify data" and "A fast way to modify data".

\$\endgroup\$
  • \$\begingroup\$ :Thanks.I understand but one more question.I am writing 13 Bytes in Flash its base on RTC interrupt.So RTC give me 1 sec interrupt and I select chip writing 13 Bytes and de-select chip \$\endgroup\$ – shail korat Oct 22 '13 at 4:41
  • \$\begingroup\$ :- Thanks.I understand but one more question.I am writing 13 Bytes in Flash its base on RTC interrupt.RTC give me 1 sec interrupt and I select chip,writing 13 Bytes and de-select chip.So My question is like memory Page is 256 Bytes and I am writing 13 Bytes at time of select and de-select chip.so remaining Bytes(243 bytes) are skip or writing continue with 13 Bytes???? \$\endgroup\$ – shail korat Oct 22 '13 at 4:50
  • \$\begingroup\$ Which are better for writing data in flash PAGE PROGRAM or PAGE WRITE instruction??? \$\endgroup\$ – shail korat Oct 22 '13 at 4:53
  • \$\begingroup\$ Every time you write to the chip you need to specify the starting address. If you only write 13 bytes that is fine, the other 243 bytes in that page will have the same value as before and will not be altered. \$\endgroup\$ – David Oct 22 '13 at 5:03
  • \$\begingroup\$ PAGE PROGRAM and PAGE WRITE are fundamentally different. One is fast but can only change bits from 1 to 0, it cannot change a 0 to 1 in memory. The other is slower and will eventually wear out the part, but allows you to change bits from 0 to 1 or 1 to 0. \$\endgroup\$ – David Oct 22 '13 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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