PostHeaderIcon ST72F325S6 Embedded Flash Binary File: When the Length Does Not Match

Sooner or later every ST72F325S6 embedded flash binary file delivery produces the same email. The customer has compared the file’s size against the number in the datasheet, found a discrepancy, and concluded that the recovery was incomplete. Sometimes they are right. More often the file is exactly correct and the comparison was made between two different quantities.

Four causes produce that email. Three of them are correct behaviour, and identifying which one is in play takes less time than writing the reply.

Why the file is a map, not an array

A binary file is a sequence of bytes with an implied starting address of zero. The device is a map where the interesting content sits at an offset that has nothing to do with zero.

There are two conventions for resolving that mismatch, and both are used in the trade. Either the file contains only the device’s content, so its first byte belongs at the device’s first flash address — in which case the file length equals the capacity. Or the file covers the address space from zero, so it is padded with something to reach the device’s first address — in which case the length equals 64 KB regardless of capacity.

convention file length first byte belongs at alignment work needed
content only equals capacity the device’s first flash address the recipient must know the base
address space from zero 64 KB address zero the recipient must skip the padding
programmed region only smaller than capacity the first programmed address the recipient must know both

Four reasons a length disagrees

what the customer sees actual cause is it a defect
file is larger than the stated capacity the file covers from zero, with padding up to the first flash address no
file is smaller than the stated capacity only the programmed region was included no, if documented
file is two to three times the expected size it is a text format, not a binary no
file is a little larger than expected a header or metadata block was prepended depends on the format
file length is not a power of two the read ended at a block boundary that is not the top yes, investigate

The first row is by far the most common. On this family the flash is top-aligned, so an 8 KB device begins at 0xE000 and a file that covers from zero to 0xFFFF is 65,536 bytes for an 8 KB part — eight times the capacity, and completely correct under its own convention.

The padding question

Padding is not filler. It is the representation of addresses that exist in the address space but are not part of the flash array.

padding byte what it usually represents how to verify
FF erased or unmapped, treated as blank consistent throughout the region below flash
00 an interface that returns zeros for unmapped reads consistent throughout
a repeated pattern a filler convention or a test pattern uniform and reproducible across reads
the contents of something else a read that started at the wrong address not uniform, decodes as something

The fourth row is the only one that indicates a problem. A region below the flash start that contains plausible-looking data is not padding; it is either a read from an unmapped area that mirrors something, or a defect in how the image was assembled.

Files longer than the device

The fix is arithmetic, not rework. Subtract the padding to find the offset where the device’s content begins, and confirm that the first byte at that offset is the first byte the device would return.

check expected result
the byte at the computed offset matches a read from the device’s first address the offset is correct
the region from the offset to the end is exactly the capacity the file is complete
the vector table sits thirty-two bytes before the end the top of the map is where it should be
the reset vector points into the application region the image is coherent

Those four checks take a minute and produce a defensible answer to the email. They also produce the three numbers the recipient needs, which is the subject of a later section.

Files shorter than the device

This is the case that sometimes is a defect, and the distinction is whether the missing bytes were blank or absent.

A file containing only the programmed region is a legitimate and often desirable delivery, because it is compact and it makes the unused space obvious. A file that stops at a block boundary in the middle of the application region is truncated, and the truncation may have a cause worth investigating: a read that ended early, a session that dropped, or an array that has a defective block.

where the file ends interpretation
exactly at the end of the last programmed block deliberate, only programmed content included
inside a region that should contain code truncated read, investigate
at a block boundary just before the vector table the top block was not read, which is a serious omission
shorter than any plausible program the read barely started

The third row deserves special emphasis. The top block contains the vector table and the configuration bytes, and a delivery without them is not an image of the device — it is an image of part of the device with the part that makes it interpretable removed.

The three numbers that end the argument

Every binary delivery should carry three numbers, and once they are present the length question stops being an argument.

The base address: where the file’s first byte belongs. The length: how many bytes from that base are covered. The capacity: how large the device’s array is.

With those three, any recipient can compute the answer themselves. File length equal to length field, base plus length equal to the top of the map, and capacity equal to the distance from the base to the top. A file that satisfies all three is correct by construction, and one that does not has a specific problem that can be named.

three numbers consistency test
base 0xE000, length 8192, capacity 8192 base plus length equals the top of the map, consistent
base 0x0000, length 65536, capacity 8192 the file covers the whole space, the capacity is a subset, consistent if documented
base 0xE000, length 4096, capacity 8192 half the array is missing, inconsistent
base 0x0000, length 65536, capacity 8192, text format on disk the on-disk size is larger than the image, consistent

Delivery convention

The convention that produces the fewest questions is to deliver both forms, each with a one-line description.

A content-only binary with a sidecar stating base, length and capacity. A text format with explicit addresses for anyone whose toolchain wants one. And a short note saying which regions were blank at the time of reading, because a blank region and a missing region look the same in a hex dump and are not the same thing.

That last note is what prevents the follow-up email about the file being “full of FF”. A blank region is information: it says the application does not use that part of the array, which is often a design decision worth knowing.

The format distinction that causes the third row of the length table is covered in ST72F324BK MCU heximal, and the checksum semantics of each text format are in ST72F321R9 locked memory file. The mapping that makes the base address a non-arbitrary number is the subject of ST72F32AJ1 flash memory binary.

Comments are closed.