• AVR Freaks

AnsweredHot!choose custom address for an object

Page: 12 > Showing page 1 of 2
Author
pajuhesh80
Super Member
  • Total Posts : 88
  • Reward points : 0
  • Joined: 2019/12/08 11:23:37
  • Location: Nowshahr, Mazandaran, Iran
  • Status: offline
2020/06/10 11:10:20 (permalink)
3.5 (2)

choose custom address for an object

How should I choose a custom address for an object? For example, in "HID Bootloader - PIC18F14K50" project in mla, you can see "void __at(0x30) BootMain(void)". Why "0x30" is chosen as the address of this function? Why not 0x20 or 0x40?

Slow and Steady!
#1
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11936
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: choose custom address for an object 2020/06/10 12:31:37 (permalink)
+1 (1)
It's not clear to me why they've done that.  Bootloaders typically start at the reset vector (address 0).  The comments make me think it might be C18 legacy.
#2
pajuhesh80
Super Member
  • Total Posts : 88
  • Reward points : 0
  • Joined: 2019/12/08 11:23:37
  • Location: Nowshahr, Mazandaran, Iran
  • Status: offline
Re: choose custom address for an object 2020/06/10 12:59:55 (permalink)
0
jtemples
Bootloaders typically start at the reset vector (address 0).

This bootloader starts from 0, too. "main()" and "BootMain()" are separate functions in this project; and as mentioned in comments, the project is optimized for XC8.
Anyway, I'm still waiting for an answer to my question.Smile

Slow and Steady!
#3
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11936
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: choose custom address for an object 2020/06/10 13:15:53 (permalink)
+2 (2)
The general answer to your question is that you will probably never need to place something at a specific address.  It's only required in uncommon circumstances, and if you have decided you need to place something at a fixed address, you will most likely know which address it needs to be placed at.
#4
du00000001
Just Some Member
  • Total Posts : 3852
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: online
Re: choose custom address for an object 2020/06/10 13:26:36 (permalink)
0
Might be 0x30 had been chosen as this is a "round" address - easy to memorize.
 
Flash addresses 0x00 - 0x27 could be considered "occupied", so 0x28 might have worked as well. But maybe (one more maybe) the initial ISR framework for the low priority interrupts was somewhat longer, having occupied some space between 0x27 and 0x2F. Hard to know.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#5
NorthGuy
Super Member
  • Total Posts : 6228
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: online
Re: choose custom address for an object 2020/06/10 14:46:14 (permalink)
+2 (2)
Impossible to tell unless you ask the person who wrote this.
#6
1and0
Access is Denied
  • Total Posts : 11000
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: choose custom address for an object 2020/06/10 16:28:30 (permalink)
0
It's probably an arbitrary address behind the low-priority interrupt and to keep the function in the boot block.
 
#7
pajuhesh80
Super Member
  • Total Posts : 88
  • Reward points : 0
  • Joined: 2019/12/08 11:23:37
  • Location: Nowshahr, Mazandaran, Iran
  • Status: offline
Re: choose custom address for an object 2020/06/10 22:23:53 (permalink)
0
I want to use USB in both my bootloader and main app. As USB uses a lot of memory, it's a really bad idea to implement it in both. So I decided to implement it just in bootloader and use pointers in main app to call USB related functions. So I have to fix functions at a specific addresses.
I have an idea: Complete bootloader code, compile it, start simulator (debugger), find current (compiler decided) addresses of each needed function and variable, and fix them there using "__at(0x....)".
Is it a good idea?

Slow and Steady!
#8
ric
Super Member
  • Total Posts : 28009
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: choose custom address for an object 2020/06/10 22:33:42 (permalink)
+3 (3)
You've forgotten, XC8 uses a compiled stack. You're going to have to find some way to avoid your bootloader and main app trying to use the same RAM.
Everyone gets this bright idea of sharing code between bootloader and app, then when they actually try to do it, the reality of how complicated it all is sinks in.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#9
pajuhesh80
Super Member
  • Total Posts : 88
  • Reward points : 0
  • Joined: 2019/12/08 11:23:37
  • Location: Nowshahr, Mazandaran, Iran
  • Status: offline
Re: choose custom address for an object 2020/06/10 22:57:06 (permalink)
0
1- What do you mean by "compiled stack"? Does it push some values into stack memory at application startup?
2- Same RAM or same ROM? Aren't functions stored in ROM?
3- Then what should I do? I need USB for both bootloader and main app. :-(
New idea: Write bootloader and main app in a single project and compile them into a single hex file (use fixed addresses for each object in bootloader and USB) and design bootloader in a way that prevents modifying memory addresses before main app. (I don't have XC8 pro and I need all USB related functions and variables for main app, so I can't reduce USB codes size and my USB bootloader will be larger than boot block of my MCU; so I can't use code protection, write protection, etc. and I have to protect bootloader and USB codes manually.)
This way, if I send hex file of this project to bootloader, it will ignore bootloader and USB sections and just programs main app.
post edited by pajuhesh80 - 2020/06/10 22:59:50

Slow and Steady!
#10
ric
Super Member
  • Total Posts : 28009
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: choose custom address for an object 2020/06/10 23:07:13 (permalink) ☼ Best Answerby pajuhesh80 2020/06/10 23:09:48
+3 (3)
pajuhesh80
1- What do you mean by "compiled stack"? Does it push some values into stack memory at application startup?

8 bit PIC's don't have stack memory. Just a very small stack ONLY used for return addresses.
Now would be a good time to read about "compiled stack" in the XC8 User Guide.
 

2- Same RAM or same ROM? Aren't functions stored in ROM?

I am talking about RAM.
 

3- Then what should I do? I need USB for both bootloader and main app. :-(

Simplest solution, get a bigger PIC.
 

New idea: Write bootloader and main app in a single project and compile them into a single hex file (use fixed addresses for each object in bootloader and USB) and design bootloader in a way that prevents modifying memory addresses before main app. (I don't have XC8 pro and I need all USB related functions and variables for main app, so I can't reduce USB codes size and my USB bootloader will be larger than boot block of my MCU; so I can't use code protection, write protection, etc. and I have to protect bootloader and USB codes manually.)
This way, if I send hex file of this project to bootloader, it will ignore bootloader and USB sections and just programs main app.

Once you read about how XC8 allocates RAM, you may see the difficulty of loading a new version of your main app, and getting it to work with the original bootloader.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#11
1and0
Access is Denied
  • Total Posts : 11000
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: choose custom address for an object 2020/06/11 00:38:18 (permalink)
0
ric
Everyone gets this bright idea of sharing code between bootloader and app, then when they actually try to do it, the reality of how complicated it all is sinks in.

 
This is so simple in absolute assembly code. ;)
 
#12
pajuhesh80
Super Member
  • Total Posts : 88
  • Reward points : 0
  • Joined: 2019/12/08 11:23:37
  • Location: Nowshahr, Mazandaran, Iran
  • Status: offline
Re: choose custom address for an object 2020/06/11 00:43:40 (permalink)
+1 (1)
1and0 
This is so simple in absolute assembly code. ;)

Right but not when you need USB!Smile
post edited by pajuhesh80 - 2020/06/11 00:58:24

Slow and Steady!
#13
1and0
Access is Denied
  • Total Posts : 11000
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: choose custom address for an object 2020/06/11 00:46:59 (permalink)
0
pajuhesh80
1and0 
This is so simple in absolute assembly code. ;)

Right but not when you need USB!



People have known to do USB in assembly too. ;)
 
#14
pajuhesh80
Super Member
  • Total Posts : 88
  • Reward points : 0
  • Joined: 2019/12/08 11:23:37
  • Location: Nowshahr, Mazandaran, Iran
  • Status: offline
Re: choose custom address for an object 2020/06/11 00:55:53 (permalink)
+1 (1)
1and0
People have known to do USB in assembly too. ;)

WHAT?!!! USB in assembly? I can't even implement USB in C by myself! I just use, edit and customize mla examples. I need a long time to completely learn and write USB codes in C. For assembly, perhaps I need "unsigned long long" time!mr green
post edited by pajuhesh80 - 2020/06/11 00:57:54

Slow and Steady!
#15
du00000001
Just Some Member
  • Total Posts : 3852
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: online
Re: choose custom address for an object 2020/06/11 02:58:46 (permalink)
+2 (2)
pajuhesh80
1and0
People have known to do USB in assembly too. ;)

WHAT?!!! USB in assembly? I can't even implement USB in C by myself! I just use, edit and customize mla examples. I need a long time to completely learn and write USB codes in C. For assembly, perhaps I need "unsigned long long" time!mr green



It will hardly work out in
pajuhesh80
Slow and Steady!

mode. You'd have to accelerate to "Fast and Competent!" in signed short time.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#16
pajuhesh80
Super Member
  • Total Posts : 88
  • Reward points : 0
  • Joined: 2019/12/08 11:23:37
  • Location: Nowshahr, Mazandaran, Iran
  • Status: offline
Re: choose custom address for an object 2020/06/11 05:46:53 (permalink)
+1 (1)
Currently I don't have enough time because I'm a student in last year of high school and I have to study hard for university entrance exam to be accepted in a good university. I'll start learning USB just after the exam.
Unfortunately, because of COVID-19, the exam is postponed to August 21! :(
 And remember:
Slow and steady wins the race! ;)
post edited by pajuhesh80 - 2020/06/11 05:51:59

Slow and Steady!
#17
crosland
Super Member
  • Total Posts : 2017
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Warks, UK
  • Status: offline
Re: choose custom address for an object 2020/06/11 11:53:42 (permalink)
+1 (1)
ric
You've forgotten, XC8 uses a compiled stack. You're going to have to find some way to avoid your bootloader and main app trying to use the same RAM.
Everyone gets this bright idea of sharing code between bootloader and app, then when they actually try to do it, the reality of how complicated it all is sinks in.

 
As I've said before, it was very easy in C18. I used function pointers at a known addresses. If the bootloader or the app were modified then everything still worked as the function pointers remained at their known addresses.
#18
visenri
Starting Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2017/10/25 15:30:29
  • Location: 0
  • Status: offline
Re: choose custom address for an object 2020/06/21 11:23:19 (permalink)
+1 (1)
ric
You've forgotten, XC8 uses a compiled stack. You're going to have to find some way to avoid your bootloader and main app trying to use the same RAM.
Everyone gets this bright idea of sharing code between bootloader and app, then when they actually try to do it, the reality of how complicated it all is sinks in.



XC8 normally uses compiled stack, but for Enhanced Mid-range and PIC18 you can specify the type of calling convention you want to use for specific functions.
 
Using software stack allows sharing a function without problems, because it uses the stack pointer for params and local variables.
 
You should be able to share functions between bootloader and main application if the following conditions are meet:
  • You have restricted the bootloader and applications program memory ranges to not overlap (this is a requirement to have a working bootloader, even if you don't share functions).
  • Shared functions must be placed at absolute addresses.
  • Shared functions must use the reentrant qualifier (software stack).
  • Any global/function static shared variables must also have absolute addresses.
 
Example:

 
__reentrant int sharedFunction(int param) __at(0x30)
{
...
}
 

 
 
 
post edited by visenri - 2020/06/21 11:25:53
#19
pajuhesh80
Super Member
  • Total Posts : 88
  • Reward points : 0
  • Joined: 2019/12/08 11:23:37
  • Location: Nowshahr, Mazandaran, Iran
  • Status: offline
Re: choose custom address for an object 2020/07/29 23:36:54 (permalink)
0
I wrote my bootloader and used __reentrant and fixed address for shared functions but I can't call them.
I used some strange pointers and even asm("goto 0xABC") and asm("call 0xABC"). Pointers are compiler but have no effect. Inline assemblies stop normal code execution but still not working.
I checked map file. All functions are placed where I set.

Slow and Steady!
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5