| Author |
Message |
|
|
Posted: Jun 19, 2012 - 05:47 PM |
|

Joined: Mar 17, 2011
Posts: 97
|
|
Hello friends, I wanted to write a small manager program (similar to an operating system) for AVR ATmega8. I was able to write the bootloader program successfully for ATmega 8 and now I want to go even deep. But I have a few questions my first question is
1. How does the controller transfer a subroutine from program memory (FLASH) into the main memory (RAM) to start its execution. If it could be done then what are the instructions which we have to run to do this task in c.
2. How can I compile only a single function written in c without having main in the program. i.e. I only want the code of the function written in c to be compiled.
Please help me & tell me if it is possible. I am trying to program only a single function through the bootloader. The controller already has some program in it. |
_________________ Thanks and Regards
Mahesh Murty
Senior Fellow
ilabs The Robotics and Embedded Systems Research Lab
Nagpur
India
|
| |
|
|
|
|
|
Posted: Jun 19, 2012 - 05:54 PM |
|


Joined: Mar 27, 2002
Posts: 18752
Location: Lund, Sweden
|
|
|
Quote:
How does the controller transfer a subroutine from program memory (FLASH) into the main memory (RAM) to start its execution.
It does not.
The relations between flash memory and RAM memory in an AVR is not the same as for a hard disk and RAM on e.g. a desktop computer. In AVRs programs execute directly from flash. Instructions are fetched directly from flash into the processor (without passing RAM) and are "interpreted" there in hardware. It is the data that the programs act upon that are kept in RAM.
In one of the very first pages of every AVR data sheet there is a block diagram which clearly shows two separate busses going into the AVR CPU - one from flash memory and one from RAM memory. Such an arrangement is referred to as "Harvard architecture" (whereas your desktop computer has one system bus for RAM and all other peripherals, an arrangement called "Von Neumann" architecture).
If you want to know more about this, then Wikipedia has good articles on those two principal architectures.
I suppose the second question
Quote:
How can I compile only a single function written in c without having main in the program
is thereby moot. But anyway: Just place the function is a separate source file and compile it. The generated machine code is in the generated object file. There will be some other stuff in that object file also, e.g. information to the linker about (relative) addresses and sizes of "stuff" in the generated machine code. |
Last edited by JohanEkdahl on Jun 20, 2012 - 08:39 AM; edited 1 time in total
|
| |
|
|
|
|
|
Posted: Jun 20, 2012 - 08:28 AM |
|


Joined: Jul 18, 2005
Posts: 62938
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
How does the controller transfer a subroutine from program memory (FLASH) into the main memory (RAM) to start its execution.
That's the second thread in a couple of days with this mis-apprehension. Did you read this on a website somewhere? Clearly someone seems to be circulating an incorrect view of how AVRs work. |
_________________
|
| |
|
|
|
|
|
Posted: Jun 20, 2012 - 04:33 PM |
|


Joined: Nov 22, 2002
Posts: 12193
Location: Tangent, OR, USA
|
|
If you come to embedded microcontrollers with no "computer" technical knowledge other than desktop computing, you would naturally believe that this is how it worked.
Jim |
_________________ Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA
"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
|
| |
|
|
|
|
|
Posted: Jun 20, 2012 - 04:47 PM |
|


Joined: Jul 18, 2005
Posts: 62938
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
other than desktop computing
A PC does not load the BIOS from flash to RAM before it executes  |
_________________
|
| |
|
|
|
|
|
Posted: Jun 20, 2012 - 04:53 PM |
|


Joined: Nov 22, 2002
Posts: 12193
Location: Tangent, OR, USA
|
|
True, but that detail is probably lost on application programmers.
Jim |
_________________ Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA
"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
|
| |
|
|
|
|
|
Posted: Jun 20, 2012 - 05:38 PM |
|


Joined: Feb 19, 2001
Posts: 26104
Location: Wisconsin USA
|
|
| Perhaps Op is coming from doing FPGA work. I know that my son's university did indeed have some microcontroller work, but much emphasis was put on FPGA (often with an embedded micro core). |
|
|
| |
|
|
|
|
|
Posted: Jun 20, 2012 - 05:43 PM |
|


Joined: Jul 18, 2005
Posts: 62938
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
but much emphasis was put on FPGA (often with an embedded micro core).
Wish I had my time again - I'd love to have been taught more about this. I don't know enough about FPGA - is it not the case that some of it could be set aside to emulate ROM to hold the controller code then? Or is it always expected that it streams in from some external memory device? |
_________________
|
| |
|
|
|
|
|
Posted: Jun 21, 2012 - 08:44 AM |
|

Joined: Dec 30, 2004
Posts: 9003
Location: Melbourne,Australia
|
|
Most FPGAs are ram based - you need to load them up from an external serial flash or another device can load up the bitstream file. The logic is implemented as look up tables. You can also use the ram as ram, dual port memory and read only memory, so yes, you can have your code stored in the flash device.
Probably where this 'understanding' is coming from is the proliferation of little Linux devices that boot from a flash device and unpack the kernel etc from flash into ram then execute it. No XIP (execute in place) here. |
|
|
| |
|
|
|
|
|
Posted: Jun 21, 2012 - 08:45 AM |
|


Joined: Mar 27, 2002
Posts: 18752
Location: Lund, Sweden
|
|
|
Quote:
A PC does not load the BIOS from flash to RAM before it executes
I would not be so definitive. IIRC there is (was?) an optimization technique employed sometimes called "BIOS shadowing" or some such, and I think it was about copying the BIOS to RAM. Or some such..
Still, if so, it's a "trick" rather than a principle of a computer architecture. |
|
|
| |
|
|
|
|
|
Posted: Jun 21, 2012 - 09:09 AM |
|

Joined: Oct 18, 2001
Posts: 371
Location: Eastern England.
|
|
| [quote="JohanEkdahl"]
Quote:
...it was about copying the BIOS to RAM. Or some such..
IIRC is was done because at the time RAM was faster than EPROM. |
|
|
| |
|
|
|
|
|
Posted: Jun 21, 2012 - 01:02 PM |
|


Joined: Oct 30, 2002
Posts: 5727
Location: The Netherlands
|
|
That's still the case IIRC in ARM chips that execute directly from Flash they use ultrawide reads, 128 bits at the time, to get around this limitation. |
|
|
| |
|
|
|
|
|
Posted: Jun 21, 2012 - 01:23 PM |
|


Joined: Jul 18, 2005
Posts: 62938
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
| I guess it depends what we call a "PC" but if we focus on the IBM model #5150 there is no doubt at all that the BIOS was mapped into the 8088 memory map and it fetched instructions direct from the EPROM. |
_________________
|
| |
|
|
|
|
|
Posted: Jun 21, 2012 - 03:54 PM |
|

Joined: Oct 18, 2001
Posts: 371
Location: Eastern England.
|
|
| Well, I guess the 5150 was "The PC" with everything else that followed being "A PC". |
|
|
| |
|
|
|
|
|
Posted: Jun 21, 2012 - 07:25 PM |
|

Joined: Mar 17, 2011
Posts: 97
|
|
Ok. I was asking about transferring the function from flash to ram because we were being taught in the class (Probably about Desktop Computers)that on any thing before processing is to be transferred into the RAM first and then processed. By this discussing it is clear that in AVR we do not need to do so. It executes the code automatically from the flash. So if I have a pointer to function my pointer should hold the address from the flash to call the function.
So if I want to call a function whose starting address is 0x0C00 , then in C can I write
Code:
void *ptr(void);
ptr =(void*(void))0x0C00;
ptr();
,
so that my function will start its execution? |
_________________ Thanks and Regards
Mahesh Murty
Senior Fellow
ilabs The Robotics and Embedded Systems Research Lab
Nagpur
India
|
| |
|
|
|
|
|
Posted: Jun 21, 2012 - 08:23 PM |
|


Joined: Nov 11, 2003
Posts: 4040
Location: Chicago Illinois USA
|
|
Well...
They weren't teaching you about Harvard computers and slightly over simplifying Von Neumann computers.
In many computers some "ram" is actually rom and code is run directly from there. Often during boot. PC's are an example. |
_________________ Discursive design,
Torby
Some days, it's just not worth chewing through the restraints.
|
| |
|
|
|
|
|
Posted: Jun 21, 2012 - 08:28 PM |
|


Joined: Nov 22, 2002
Posts: 12193
Location: Tangent, OR, USA
|
|
I think what you say is true, but its rarely done that way in the embedded world, at least for 8 & 16 bit machines.
First, the physical address will change depending on the optimization used for the compile.
Second, it makes documentation and debugging totally miserable.
Third - probably something else, even worse.
So, functions are (almost) always referenced by name.
Jim |
_________________ Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA
"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
|
| |
|
|
|
|
|
Posted: Jun 21, 2012 - 10:13 PM |
|


Joined: Sep 04, 2002
Posts: 21393
Location: Orlando Florida
|
|
| Early 'PCs' like TRS80s with a z80 cpu had a little bit of rom to boot the os and hold some bios functions, and cpm or trsdos filled up 20k or so of the 64k ram, and executable programs would load from disk into the beginning of ram and start running. So the model of a computer with a microcontroller cpu as a general purpose computer with programs loaded from disk to ram is a valid convept, except 99% of embedded microcontrollers just have one program in rom instead of a disk and a bunch o ram. |
_________________ Imagecraft compiler user
|
| |
|
|
|
|
|
Posted: Jun 21, 2012 - 11:11 PM |
|


Joined: Mar 27, 2002
Posts: 18752
Location: Lund, Sweden
|
|
As Jim says, assigning a function pointer with an explicit address is a seldom used manouvre.
You can however do something like this:
Code:
void foo(void)
{
...
}
void (*fptr)(void);
fptr = foo;
fptr();
The compiler/linker will take care of resolving the actual address to the function.
This example is of-course quite meaningless, but demonstrates the syntax and technique. A more elaborate example could be constructed where using this technique makes more sense, e.g. for implementing a generic driver interface and where the actual instances of drivers are accessed through such function pointers.
In essence this means "polymorphism", and then you might consider using a language where such mechanism have an arguably nicer syntax. Think C++...
EDIT: Corrected bad function pointer definition. Twice.. Thanks, Jayjay! |
Last edited by JohanEkdahl on Jun 22, 2012 - 07:25 AM; edited 2 times in total
|
| |
|
|
|
|
|
Posted: Jun 22, 2012 - 12:44 AM |
|


Joined: Oct 30, 2002
Posts: 5727
Location: The Netherlands
|
|
I guess you meant to write:
Code:
void (*fptr)(void);
 |
|
|
| |
|
|
|
|
|