Forcing a specific memory location for a variable

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I'm using Win-AVR / GCC and I need to keep a counter check (/branch) down to minimum instruction count (it is in an inner loop).

With a simple 'volatile char my_variable' decalration can I instruct / direction / provide 'advice' to the compiler / linker to just store the byte in a register (rather than a memory location which has to be copied to a register before each test).

Thanks,

Carl

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the C register keyword is made just for that.

register char myvar;

But it's only a subtle hint to the compiler, no guarantees.

Have you turned on all optimizations?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

See the avr-libc FAQ about binding a variable to a specific register but note that the libc will not have been built with this understanding so if you call to anything in the libc the variable/register may be re-used.

http://www.nongnu.org/avr-libc/u...

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Excellent - thanks for all the pointers.

All optomisations are on, and I'm inlining where sensible, just trying to learn if I have any other options.

WRT the avr-libc - my reading of the docs is if a libc function (or the compiler generically needs) the register it will use it - my question - will it push / pop the contents to a memory locaiton automatically?

Carl

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No it won't and that's your problem. Short of modifying the source and rebuilding libc you have no way of isolating a register for your own usage.

(curiously this is one area where Codevision's code generating model scores - it effectively does rebuild the entire libc ever time you compile your own .c so if you tell the system to preserve a register for your own use it applies across everything)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ah.....

So are there ANY 'free' registers from libc?

Otherwise if I'm using libc (eg printf), this whole concept is a non starter....

Carl

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

zkvii wrote:
Otherwise if I'm using libc (eg printf), this whole concept is a non starter....

Correct.

Is something SO timing sensitive that you can't afford the fetch/store from/to SRAM?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok - thanks, at least I understand the risks / problems now....

The problem is I have 95+% utilisation on a 15200 Baud line and I'm trying to write the data off to an SD/MMC card in a FAT F/S. The ATMega324p w/ 18.432Mhz chip can cope, but means I need both 3.3 and 5V supplies. I was hoping to get it down to <10Mhz and run everything at 3.3V - looking at options at this point.

Thanks again,

Carl

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You mean you have a stream of data coming from an external source and you need to write it to the card as it arrives? I've done something very similar (actually using Dataflash rather than SD). In my case the sender was a PC so I just implemented Xmodem-CRC protocol because (a) you can tell it to stop while you get on with writing to memory, (b) it includes data validitiy checking, (c) it sends stuff in easily digestible 128 byte packets, (d) almost everyone who has a PC has a terminal program capable of sending data in Xmodem-CRC format

Maybe you could do something similar or maybe your sender can be asked to stop for a while with Xon/Xoff or possibly by you implementing CTS on a port pin that is visible to his end?

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

If only.... :)

It is a single line RS232 feed from avionics unit providing current attitude, altitude, gps location, engine permaters etc..... no way to 'pause', drink from the firehose.....

Probably just have a catch for any fifo wrap and reboot, restart the next recording, having valid data stored, is more important than just have 'data' stored....

Carl

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can't help feeling that a (naturally 3.3V) ARM whizzing along at 100MHz or something couldn't half make life a lot easier here ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've only just started / got the basics going with the Megas.....

I'll get by, only a single board needed at this point.

Cheers,

Carl

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

zkvii wrote:
I'm using Win-AVR / GCC and I need to keep a counter check (/branch) down to minimum instruction count (it is in an inner loop).
... register ....
If the inner loop is small,
it can probably be written in inline assembly without defeating optimization.
That is one reason the gnu syntax includes so much information.
If the inner loop is large, a few more cycles won't matter much.
If the inner loop includes a function call,
you can lop off a few cycles by inlining.
In some cases, you might have to cut and paste the source.
If you're using printf, you can save a lot of cycles by using putchar instead.

Iluvatar is the better part of Valar.