Stack use

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

Q: How much stack can my asm code use in a random C function? Are there ways to reserve stack space?

My code is written in C with some embedded assembly. I need to determine the frequency of an external clock relative to the MCU frequency.

I'm barely able to do this in asm today. But the problem is that the external clock is too fast for me to also increase and check a counter. The MCU's counter resources are spent. If the external clock stops, so does my program. I'd prefer not to use the watchdog for this at the moment since it has some other tasks.

Instead I wish to push PINB to the stack a finite number of time in fast "online" asm code, and then pop it "offline" to determine the frequency.

I wonder how much stack space I can use for something like this.

Børge

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

Most Mega AVRs have three timers. They all allow external clocks to dedicated input pins with or without prescaling.

Rearrange your design to free up one of those pins, and use the other two timers more effectively. Seriously, you can operate most applications on one master Timer.

If your external clock is relatively slow, you can use INTx or pin-change or Analog comparator interrupts.

I would never think of trying to crash the stack. You could increment a software counter with less ASM instructions.

David.

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

Quote:
I need to determine the frequency of an external clock relative to the MCU frequency.

AVRs can sense external clock frequency at up to F_CPU/4 (F_CPU/3 and 45-55% signal with low jitter works as well). If your external clock is faster than 5MHz, perhaps you should consider using faster uC? Like 70MHz ARM? Or external divider?

No RSTDISBL, no fun!

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

Quote:

I wonder how much stack space I can use for something like this.


As long as SP does not descend below __bss_end then you are fine. Doing it in Asm is no real different to doing it ni C in fact. On most AVR you might expect:

void a_function(void) {
  char buff[20];

to work but it'd be very unlikely for

void a_function(void) {
  char buff[20000];

to work. You the programmer just have to be sensible and know when the stack is being used and have a rough idea of what you can get away with.

Say you have a 1K AVR and a typical 70% usage for .data/.bss then you clearly have about 307 bytes to use on the stack.