How to declare constant in flash on fixed address

General discussion on mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
arco
Posts: 312
Joined: 15 Apr 2008 13:54
Location: The Netherlands

How to declare constant in flash on fixed address

#1 Post by arco » 04 Sep 2018 14:31

How do I declare a constant at a fixed address? I now tried:

Code: Select all

Const BOOT_VERSION As Word[1] = (0x0400) Org 0x1FFE
It seems this is stripped by the compiler because it's never used.
But I need this constant in a bootloader application, so main application can see what version the bootloader is...
Constants declared on fixed locations should never be stripped... (they are placed there for a reason)
Regards,

Peter.

marcus
Posts: 101
Joined: 12 Sep 2006 08:58

Re: How to declare constant in flash on fixed address

#2 Post by marcus » 05 Sep 2018 07:48

Place it in program flash (eg, 0x9D000800). If it still doesn't compile make a reference to it in code

arco
Posts: 312
Joined: 15 Apr 2008 13:54
Location: The Netherlands

Re: How to declare constant in flash on fixed address

#3 Post by arco » 05 Sep 2018 09:32

Yes,

I know it works if it is referenced, but that's a bit of a kludge... :mrgreen:
I hate to embed useless code in my program just to let the compiler behave correctly...
Constants with a fixed address should always be left alone by the compiler. (It's placed there for a reason.)
Could for instance be a serialnumber that normally is never accessed or with indirect addressing.

BTW: ORG is always in program flash...
Regards,

Peter.

marcus
Posts: 101
Joined: 12 Sep 2006 08:58

Re: How to declare constant in flash on fixed address

#4 Post by marcus » 05 Sep 2018 12:11

Sorry, was looking at the PIC32 compiler.

All compilers exclude unused code and constants, otherwise the hex will be far larger. Personally, I'm ok with the way the compiler works in this regard. Adding a dummy reference is not a big deal for me, it's rare to have an unused fixed const in the program.

arco
Posts: 312
Joined: 15 Apr 2008 13:54
Location: The Netherlands

Re: How to declare constant in flash on fixed address

#5 Post by arco » 05 Sep 2018 15:58

Fixed address constants should *never* be omitted. The're placed there on purpose.
I disabled the compilers optimization, with no luck, still strips them out... :x
Regards,

Peter.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: How to declare constant in flash on fixed address

#6 Post by filip » 06 Sep 2018 09:55

Hi,

When I compile your line of code, I get the following ASM listing :

Code: Select all

//** Constants locations ** 
//ADDRESS    SIZE    CONSTANT
//----------------------------------------------
0x1FFE       [2]    _BOOT_VERSION
It seems that the compiler did not exclude this constant even though it is not used anywhere in the code.

Can you see your ASM listing ?

Regards,
Filip.

arco
Posts: 312
Joined: 15 Apr 2008 13:54
Location: The Netherlands

Re: How to declare constant in flash on fixed address

#7 Post by arco » 06 Sep 2018 10:32

The compiler says that there is a _BOOT_VERSION constant, but there isn't
You won't find any other reference to it in that listing file. (it's never initialized with the requested value)
If you want to set value to 0x0400 and you reference the constant in code by assigning it to a variable you get code in picture in your list file.
If you do not reference the constant, that code is omitted. (listing says there is a _BOOT_VERSION constant, but there isn't)

You can also see it in the hex file: location 0x1FFE (is 0x3FFC in bytes) is never set to 0x0400... (see 2nd picture with correct value if referenced)
Attachments
Bootver2.png
Bootver2.png (5.81 KiB) Viewed 3048 times
Bootver.png
Bootver.png (4.89 KiB) Viewed 3049 times
Regards,

Peter.

Post Reply

Return to “mikroBasic PRO for dsPIC30/33 and PIC24 General”