diff --git a/ispprog.c b/ispprog.c index 8eb8713..5433941 100644 --- a/ispprog.c +++ b/ispprog.c @@ -28,6 +28,12 @@ #include "target.h" #include "uart.h" +#define TIMER_IRQFREQ_MS 10 + +/* convert milliseconds to timer ticks */ +#define TIMER_MSEC2TICKS(x) ((x * F_CPU) / (TIMER_DIVISOR * 1000ULL)) +#define TIMER_MSEC2IRQCNT(x) (x / TIMER_IRQFREQ_MS) + #define EV_NONE 0x00 #define EV_STATE_ENTER 0x01 #define EV_BUTTON_PRESSED 0x02 @@ -95,7 +101,8 @@ static void reset_statemachine(uint8_t events) /* remove all events */ events = EV_NONE; - timer = 0; /* stop timer */ + /* stop timer */ + timer = TIMER_MSEC2IRQCNT(0); spi_init(0); @@ -122,7 +129,7 @@ static void reset_statemachine(uint8_t events) { events &= ~(EV_STATE_ENTER); - timer = 1; /* timeout 10ms */ + timer = TIMER_MSEC2IRQCNT(10); /* put device in ISP mode */ RESET_ACTIVE(); @@ -153,7 +160,7 @@ static void reset_statemachine(uint8_t events) { events &= ~(EV_STATE_ENTER); - timer = 5; /* timeout 50ms */ + timer = TIMER_MSEC2IRQCNT(50); /* put device in RUN mode */ RESET_INACTIVE(); @@ -598,7 +605,7 @@ static void cmdloop(void) ISR(TIMER0_OVF_vect) { /* restart timer */ - TCNT0 = TIMER_RELOAD; + TCNT0 = 0xFF - TIMER_MSEC2TICKS(TIMER_IRQFREQ_MS); static uint8_t prev_pressed; if (ISP_CHECK()) diff --git a/target.h b/target.h index 2012402..ee1269c 100644 --- a/target.h +++ b/target.h @@ -8,9 +8,8 @@ * Fuse H: 0xDA (512 words bootloader, jtag disabled) * Fuse L: 0xFF (ext. Crystal) */ -#define F_CPU 7372800 +#define F_CPU 7372800ULL #define BAUDRATE 115200 -#define TIMER_RELOAD (0xFF - 72) /* 10ms @7.3728MHz */ #define ISP_RESET PORTB1 /* to target */ #define ISP_LED PORTB3 /* low active */ @@ -36,6 +35,7 @@ #define USE_DISPLAY 0 +#define TIMER_DIVISOR 1024 #define TIMER_INIT() { /* timer0, FCPU/1024, overflow interrupt */ \ TCCR0 = (1<