Compare commits
3 Commits
atmega32u2
...
stack-debu
| Author | SHA1 | Date | |
|---|---|---|---|
| a81c04e485 | |||
| bb82d588fb | |||
| bfaf3010e6 |
1
Makefile
1
Makefile
@@ -13,6 +13,7 @@ SRC = \
|
|||||||
main.c \
|
main.c \
|
||||||
ftdi.c \
|
ftdi.c \
|
||||||
reset.c \
|
reset.c \
|
||||||
|
stack.c \
|
||||||
$(LUFA_SRC_USB)
|
$(LUFA_SRC_USB)
|
||||||
|
|
||||||
# Default target
|
# Default target
|
||||||
|
|||||||
6
main.c
6
main.c
@@ -9,6 +9,7 @@
|
|||||||
#include <LUFA/Drivers/USB/USB.h>
|
#include <LUFA/Drivers/USB/USB.h>
|
||||||
|
|
||||||
#include "ftdi.h"
|
#include "ftdi.h"
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
static void setup(void)
|
static void setup(void)
|
||||||
{
|
{
|
||||||
@@ -36,7 +37,10 @@ int main(void)
|
|||||||
c = getchar();
|
c = getchar();
|
||||||
/* If FTDI_NONBLOCKING was provided to ftdi_init,
|
/* If FTDI_NONBLOCKING was provided to ftdi_init,
|
||||||
c will be -1 if no data was available. */
|
c will be -1 if no data was available. */
|
||||||
if (c >= 0)
|
if (c >= 0) {
|
||||||
printf("You sent %d (%c)\n", c, isprint(c) ? c : '?');
|
printf("You sent %d (%c)\n", c, isprint(c) ? c : '?');
|
||||||
|
printf("Maximum stack space used: %d / %d bytes\n",
|
||||||
|
(stack_total() - stack_min()), stack_total());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
46
stack.c
Normal file
46
stack.c
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#include "stack.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
|
extern uint8_t _end;
|
||||||
|
extern uint8_t __stack;
|
||||||
|
|
||||||
|
void __stack_canary_init(void)
|
||||||
|
__attribute__ ((naked))
|
||||||
|
__attribute__ ((section (".init1")));
|
||||||
|
void __stack_canary_init(void)
|
||||||
|
{
|
||||||
|
__asm volatile (" ldi r30,lo8(_end)\n"
|
||||||
|
" ldi r31,hi8(_end)\n"
|
||||||
|
" ldi r24,lo8(0xaa)\n" // canary
|
||||||
|
" ldi r25,hi8(__stack)\n"
|
||||||
|
" rjmp 2f\n"
|
||||||
|
"1:\n"
|
||||||
|
" wdr\n"
|
||||||
|
" st Z+,r24\n"
|
||||||
|
"2:\n"
|
||||||
|
" cpi r30,lo8(__stack)\n"
|
||||||
|
" cpc r31,r25\n"
|
||||||
|
" brlo 1b\n"
|
||||||
|
" breq 1b"
|
||||||
|
:
|
||||||
|
:
|
||||||
|
: "memory");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* return low level mark of free stack space */
|
||||||
|
uint16_t stack_min(void)
|
||||||
|
{
|
||||||
|
const uint8_t *p;
|
||||||
|
uint16_t c = 0;
|
||||||
|
|
||||||
|
for (p = &_end; *p == 0xaa && p <= &__stack; p++)
|
||||||
|
c++;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* return total amount of stack */
|
||||||
|
uint16_t stack_total(void)
|
||||||
|
{
|
||||||
|
return &__stack - &_end;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user