From 6f0bab3fb511dcedf40530a4b7aed5b32db4c9c9 Mon Sep 17 00:00:00 2001 From: Simon Zimmermann Date: Sun, 24 Oct 2010 20:20:40 +0200 Subject: [PATCH 1/2] watchdog headers for ATtinyX5 with interrupt support --- ATtinyX5/wdt.h | 127 +++++++++++++++++++++++++++++++++++++++++++++++++ wdt.h | 31 ++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 ATtinyX5/wdt.h create mode 100644 wdt.h diff --git a/ATtinyX5/wdt.h b/ATtinyX5/wdt.h new file mode 100644 index 0000000..91d6016 --- /dev/null +++ b/ATtinyX5/wdt.h @@ -0,0 +1,127 @@ +/* + * This file is part of arduino-headers. + * + * arduino-headers is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or(at your option) any later version. + * + * arduino-headers is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with arduino-headers. If not, see . + */ + +#ifndef _ARDUINO_WDT_H +#define _ARDUINO_WDT_H + +#include +#include +#include + +/* watchdog change routines */ +#define wdt_disable()\ + cli();\ + wdt_clear_flag();\ + wdt_reset_disable();\ + +#define wdt_enable(clock)\ + cli();\ + wdt_reset();\ + wdt_clear_flag();\ + wdt_reset_enable();\ + wdt_change_d##clock();\ + +#define wdt_change_clock(clock)\ + cli();\ + wdt_clear_flag();\ + wdt_clock_d##clock();\ + +#define wdt_reset() __asm__ __volatile__ ("wdr") + +static inline void +wdt_reset_enable() { WDTCR |= _BV(WDE); } + +static inline void +wdt_reset_disable() +{ + WDTCR |= (_BV(WDCE) | _BV(WDE)); + WDTCR &= ~_BV(WDE); +} + +static inline void +wdt_clear_flag() { MCUSR &= ~_BV(WDRF); } + +/* watchdog interrupts */ +#define wdt_interrupt() ISR(WDT_vect) + +static inline void +wdt_interrupt_enable() { WDTCR |= _BV(WDIE); } +static inline void +wdt_interrupt_disable() { WDTCR &= ~(_BV(WDIE)); } + +/* prescaler settings */ +static inline void +wdt_clock_d2() +{ + WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP2) | _BV(WDP1) | _BV(WDP0))); +} + +static inline void +wdt_clock_d4() +{ + WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP2) | _BV(WDP1))) | _BV(WDP0); +} + +static inline void +wdt_clock_d8() +{ + WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP2) | _BV(WDP0))) | _BV(WDP1); +} + +static inline void +wdt_clock_d16() +{ + WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP2))) | _BV(WDP1) | _BV(WDP0); +} + +static inline void +wdt_clock_d32() +{ + WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP1) | _BV(WDP0))) | _BV(WDP2); +} + +static inline void +wdt_clock_d64() +{ + WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP1))) | _BV(WDP2) | _BV(WDP0); +} + +static inline void +wdt_clock_d128() +{ + WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP0))) | _BV(WDP2) | _BV(WDP1); +} + +static inline void +wdt_clock_d256() +{ + WDTCR = (WDTCR & ~(_BV(WDP3))) | _BV(WDP2) | _BV(WDP1) | _BV(WDP0); +} + +static inline void +wdt_clock_d512() +{ + WDTCR = (WDTCR & ~(_BV(WDP2) | _BV(WDP1) | _BV(WDP0))) | _BV(WDP3); +} + +static inline void +wdt_clock_d1024() { + WDTCR = (WDTCR & ~(_BV(WDP2) | _BV(WDP1))) | _BV(WDP3) | _BV(WDP0); +} + +#endif + diff --git a/wdt.h b/wdt.h new file mode 100644 index 0000000..5e1c314 --- /dev/null +++ b/wdt.h @@ -0,0 +1,31 @@ +/* + * This file is part of arduino-headers. + * + * arduino-headers is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or(at your option) any later version. + * + * arduino-headers is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with arduino-headers. If not, see . + */ + +#if defined(__AVR_ATmega8__) +# error "arduino/wdt.h: Not implemented for ATmega8 chips yet" +#elif defined(__AVR_ATmega168__) \ + || defined(__AVR_ATmega328P__) +# error "arduino/wdt.h: Not implemented for ATmega328P chips yet" +#elif defined(__AVR_ATmega1280__) +# error "arduino/wdt.h: Not implemented for ATmega1280 chips yet" +#elif defined(__AVR_ATtiny25__) \ + || defined(__AVR_ATtiny45__) \ + || defined(__AVR_ATtiny85__) +# include +#else +# error "arduino/wdt.h: Unknown chip type" +#endif From 3084805c3acc0eb18b80d1ebfef8c69890eb0058 Mon Sep 17 00:00:00 2001 From: Simon Zimmermann Date: Tue, 26 Oct 2010 22:17:27 +0200 Subject: [PATCH 2/2] much simpler and more robust solution to watchdog added for ATtinyX5/ATmegaX8 --- ATtinyX5/wdt.h | 127 ------------------------------------------------- common/wdt.h | 35 ++++++++++++++ wdt.h | 4 +- 3 files changed, 37 insertions(+), 129 deletions(-) delete mode 100644 ATtinyX5/wdt.h create mode 100644 common/wdt.h diff --git a/ATtinyX5/wdt.h b/ATtinyX5/wdt.h deleted file mode 100644 index 91d6016..0000000 --- a/ATtinyX5/wdt.h +++ /dev/null @@ -1,127 +0,0 @@ -/* - * This file is part of arduino-headers. - * - * arduino-headers is free software: you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of - * the License, or(at your option) any later version. - * - * arduino-headers is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with arduino-headers. If not, see . - */ - -#ifndef _ARDUINO_WDT_H -#define _ARDUINO_WDT_H - -#include -#include -#include - -/* watchdog change routines */ -#define wdt_disable()\ - cli();\ - wdt_clear_flag();\ - wdt_reset_disable();\ - -#define wdt_enable(clock)\ - cli();\ - wdt_reset();\ - wdt_clear_flag();\ - wdt_reset_enable();\ - wdt_change_d##clock();\ - -#define wdt_change_clock(clock)\ - cli();\ - wdt_clear_flag();\ - wdt_clock_d##clock();\ - -#define wdt_reset() __asm__ __volatile__ ("wdr") - -static inline void -wdt_reset_enable() { WDTCR |= _BV(WDE); } - -static inline void -wdt_reset_disable() -{ - WDTCR |= (_BV(WDCE) | _BV(WDE)); - WDTCR &= ~_BV(WDE); -} - -static inline void -wdt_clear_flag() { MCUSR &= ~_BV(WDRF); } - -/* watchdog interrupts */ -#define wdt_interrupt() ISR(WDT_vect) - -static inline void -wdt_interrupt_enable() { WDTCR |= _BV(WDIE); } -static inline void -wdt_interrupt_disable() { WDTCR &= ~(_BV(WDIE)); } - -/* prescaler settings */ -static inline void -wdt_clock_d2() -{ - WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP2) | _BV(WDP1) | _BV(WDP0))); -} - -static inline void -wdt_clock_d4() -{ - WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP2) | _BV(WDP1))) | _BV(WDP0); -} - -static inline void -wdt_clock_d8() -{ - WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP2) | _BV(WDP0))) | _BV(WDP1); -} - -static inline void -wdt_clock_d16() -{ - WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP2))) | _BV(WDP1) | _BV(WDP0); -} - -static inline void -wdt_clock_d32() -{ - WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP1) | _BV(WDP0))) | _BV(WDP2); -} - -static inline void -wdt_clock_d64() -{ - WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP1))) | _BV(WDP2) | _BV(WDP0); -} - -static inline void -wdt_clock_d128() -{ - WDTCR = (WDTCR & ~(_BV(WDP3) | _BV(WDP0))) | _BV(WDP2) | _BV(WDP1); -} - -static inline void -wdt_clock_d256() -{ - WDTCR = (WDTCR & ~(_BV(WDP3))) | _BV(WDP2) | _BV(WDP1) | _BV(WDP0); -} - -static inline void -wdt_clock_d512() -{ - WDTCR = (WDTCR & ~(_BV(WDP2) | _BV(WDP1) | _BV(WDP0))) | _BV(WDP3); -} - -static inline void -wdt_clock_d1024() { - WDTCR = (WDTCR & ~(_BV(WDP2) | _BV(WDP1))) | _BV(WDP3) | _BV(WDP0); -} - -#endif - diff --git a/common/wdt.h b/common/wdt.h new file mode 100644 index 0000000..2f6843e --- /dev/null +++ b/common/wdt.h @@ -0,0 +1,35 @@ +/* + * This file is part of arduino-headers. + * + * arduino-headers is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or(at your option) any later version. + * + * arduino-headers is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with arduino-headers. If not, see . + */ + +#ifndef _ARDUINO_WDT_H +#define _ARDUINO_WDT_H + +#include +#include + +#if defined(WDTCSR) +# define _WDT_REG WDTCSR +#else +# define _WDT_REG WDTCR +#endif + +static inline void wdt_interrupt_enable() { _WDT_REG |= _BV(WDIE); } +static inline void wdt_interrupt_disable() { _WDT_REG &= ~_BV(WDIE); } + +#define wdt_interrupt() ISR(WDT_vect) + +#endif diff --git a/wdt.h b/wdt.h index 5e1c314..8704f6f 100644 --- a/wdt.h +++ b/wdt.h @@ -19,13 +19,13 @@ # error "arduino/wdt.h: Not implemented for ATmega8 chips yet" #elif defined(__AVR_ATmega168__) \ || defined(__AVR_ATmega328P__) -# error "arduino/wdt.h: Not implemented for ATmega328P chips yet" +# include #elif defined(__AVR_ATmega1280__) # error "arduino/wdt.h: Not implemented for ATmega1280 chips yet" #elif defined(__AVR_ATtiny25__) \ || defined(__AVR_ATtiny45__) \ || defined(__AVR_ATtiny85__) -# include +# include #else # error "arduino/wdt.h: Unknown chip type" #endif