Compare commits

...

5 Commits

5 changed files with 71 additions and 8 deletions

View File

@ -1,9 +1,9 @@
MCU = at90usb1287
MCU = atmega32u2
ARCH = AVR8
BOARD = USER
F_CPU = 8000000
F_USB = 8000000
OPTIMIZATION = 3
BOARD = MINIMUS
F_CPU = 16000000
F_USB = 16000000
OPTIMIZATION = s
TARGET = main
LUFA_PATH = lufa/LUFA
CC_FLAGS = -DUSE_LUFA_CONFIG_HEADER -Iconfig/ -Wall
@ -13,6 +13,7 @@ SRC = \
main.c \
ftdi.c \
reset.c \
stack.c \
$(LUFA_SRC_USB)
# Default target

4
ftdi.h
View File

@ -9,11 +9,11 @@
/* FTDI device-to-host data IN endpoint */
#define FTDI_TX_EPADDR (ENDPOINT_DIR_IN | 1)
#define FTDI_TX_QUEUE_LEN 256
#define FTDI_TX_QUEUE_LEN 32
/* Endpoint address of FTDI host-to-device data OUT endpoint */
#define FTDI_RX_EPADDR (ENDPOINT_DIR_OUT | 2)
#define FTDI_RX_QUEUE_LEN 256 /* Must be at least FTDI_TXRX_EPSIZE + 1 */
#define FTDI_RX_QUEUE_LEN 32 /* Must be at least FTDI_TXRX_EPSIZE + 1 */
/* Endpoint size */
#define FTDI_TXRX_EPSIZE 16 /* e.g. 16 or 64 */

6
main.c
View File

@ -9,6 +9,7 @@
#include <LUFA/Drivers/USB/USB.h>
#include "ftdi.h"
#include "stack.h"
static void setup(void)
{
@ -36,7 +37,10 @@ int main(void)
c = getchar();
/* If FTDI_NONBLOCKING was provided to ftdi_init,
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("Maximum stack space used: %d / %d bytes\n",
(stack_total() - stack_min()), stack_total());
}
}
}

46
stack.c Normal file
View 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;
}

12
stack.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef STACK_H
#define STACK_H
#include <stdint.h>
/* return low level mark of free stack space */
uint16_t stack_min(void);
/* return total amount of stack */
uint16_t stack_total(void);
#endif