#include "SFBTypes.h"
#include "lpc/irq.h"
Go to the source code of this file.
Defines | |
#define | STACK_CANARY_VALUE 0x2c50904e |
A special code stored in stackCanary, used by API_ASSERT_STACK_OK to help detect stack overflows. | |
#define | API_ASSERT_STACK_OK() API_ASSERT(stackCanary==STACK_CANARY_VALUE,E_BUG_STACK_CORRUPT) |
Assert that the stackCanary variable still has its special value of STACK_CANARY_VALUE, which is unlikely to be the case if the stack has overflowed into the sketch RAM area. | |
Functions | |
void | facePrintTimestamp (u8 face) |
Print a timestamp to face. | |
u32 | getCurrentStackSpace () |
Estimate how much more space is currently available on the stack. | |
u32 | getMinimumStackSpace () |
Estimate the minimum amount of stack space that has been available at any point during the running of the current sketch so far. | |
Variables | |
volatile u32 | stackCanary |
A variable placed at the end of sketch RAM usage, initialized with STACK_CANARY_VALUE, and checked by API_ASSERT_STACK_OK to help detect stack overflows. |
#define STACK_CANARY_VALUE 0x2c50904e |
A special code stored in stackCanary, used by API_ASSERT_STACK_OK to help detect stack overflows.
void facePrintTimestamp | ( | u8 | face | ) |
Print a timestamp to face.
The output depending on the context: In host programs the date and time are printed; in boards the equivalent of seconds() is printed (along with three decimal places), so the printed timestamps will roll over after about 180 years of uptime.
u32 getCurrentStackSpace | ( | ) |
Estimate how much more space is currently available on the stack.
Provides an estimate of the current stack depth, as of the call on this function.
u32 getMinimumStackSpace | ( | ) |
Estimate the minimum amount of stack space that has been available at any point during the running of the current sketch so far.
If this number ever gets 'too close' to zero, the sketch is risking a potential stack overflow condition.
Note that if stack space does run out, all bets are off about the resulting sketch behavior. If you are lucky, the sketch will die blinking with a E_BUG_STACK_CORRUPT code, but that is by no means assured. It might instead die with E_HW_UNDEF, or E_HW_DABT, or E_HW_PABT, or some other blink code, or it may just hang rather than dying, etc. etc. etc.
How close to zero is 'too close'? Well, that's somewhat up to you, the nature of your sketch, and your tolerance for risk. Be aware that the invocations of interrupt handlers do tie up space on the same system stack that regular functions use, and that is true both for user-written interrupt handlers -- e.g., as used with hardware timers -- as well as the core software interrupt handlers that deal with serial I/O and the main timer.
If you feel you've managed to run your sketch through pretty much all the worst (meaning: busiest, most heavily loaded, most complex execution path of the most complicated functions) you can reasonably expect, and getMinimumStackSpace() is still returning a number in the 100's or more, you may probably be okay. With a number in the 1000's you might probably feel pretty comforable.
But, it all depends on the sketch! For example, this sketch:
void hugeFunction() { u32 buf[10000]; // buf is too big to fit in RAM.. for (u32 i = 0; i < 10000; ++i) buf[i] = 0; println(buf[1]); // keeps compiler from optimizing stuff away.. } void setup() { } void loop() { if (random(1000000)==0) hugeFunction(); }
will probably be fine for hours.. but as soon as hugeFunction
is actually called -- with its hopelessly oversized array -- bad things will ensue.
volatile u32 stackCanary |
A variable placed at the end of sketch RAM usage, initialized with STACK_CANARY_VALUE, and checked by API_ASSERT_STACK_OK to help detect stack overflows.