2
\$\begingroup\$

My STM32F407 microcontroller datasheet states that my flash region is between the following addresses:

0x08000000 to 0x080FFFFF

I want to calculate the amount of flash, which I know is 1024KB, but when I try to prove this, I always get to 1023. Here's my math:

0x080FFFFF - 0x08000000 = 0x000FFFFF
                        = 1048575
                        = (1048575 / 1024)KB
                        = 1023KB

Inversely...

1024 * 1024 = 1048576

Why am I confused with 1 byte? Dumb question, and I know this is high school stuff...

\$\endgroup\$
2
  • 4
    \$\begingroup\$ You meant 0x080FFFFF - 0x08000000 + 1. To see this, write out the equivalent 1 byte address calculation... 0x08000000 - 0x08000000 ... \$\endgroup\$ Oct 30 '15 at 16:42
  • \$\begingroup\$ betterexplained.com/articles/… \$\endgroup\$
    – DJohnM
    Oct 30 '15 at 22:39
5
\$\begingroup\$

First of all, you are talking about the FLASH region, but then about RAM. These are two different things. Second: How many numbers can be numbered by 0 to 9? 10, right? So it is given by 9 - 0 + 1. The same with your space calculation. The total size is
0x080FFFFF - 0x08000000 + 1 = 0x100000 = 1048576. Then 1048576 / 1024 = 1024.

\$\endgroup\$
1
  • \$\begingroup\$ Edited the question, I meant to say flash. Thank's for pointing that out. \$\endgroup\$ Oct 30 '15 at 16:44
2
\$\begingroup\$

You are taking the difference of the last address minus the first. This gives you the offset from the first to the last, not the total. This will always be off by 1.

Think of counting people standing in a line by assigning each a sequential number starting at 1. In the obvious limiting case where you have two people, they are numbers 1 and 2. 2-1 tells you how many people distant #2 is from #1, which is always one less than the total number of people in the line if you apply this from first to last.

\$\endgroup\$

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.