watchdog: Added a test module
[simavr] / tests / atmega48_watchdog_test.c
1 /*
2         attiny13_watchdog_test.c
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include <avr/io.h>
24 #include <stdio.h>
25 #include <avr/interrupt.h>
26 #include <avr/delay.h>
27 #include <avr/sleep.h>
28 #include <avr/wdt.h>
29
30 /*
31  * This demonstrate how to use the avr_mcu_section.h file
32  * The macro adds a section to the ELF file with useful
33  * information for the simulator
34  */
35 #include "avr_mcu_section.h"
36 AVR_MCU(F_CPU, "atmega48");
37
38 static int uart_putchar(char c, FILE *stream) {
39   if (c == '\n')
40     uart_putchar('\r', stream);
41   loop_until_bit_is_set(UCSR0A, UDRE0);
42   UDR0 = c;
43   return 0;
44 }
45
46 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
47                                          _FDEV_SETUP_WRITE);
48
49
50 ISR(WDT_vect)
51 {
52         // nothing to do here, we're just here to wake the CPU
53 }
54
55 int main()
56 {
57         stdout = &mystdout;
58
59         wdt_enable(WDTO_120MS);
60
61         // enable watchdog interupt
62         // NOTE: since the Change Enable bit is no longer on, it should not
63         // change the timer value that is already set!
64         WDTCSR = (1 << WDIE);
65
66         sei();
67
68         printf("Watchdog is active\n");
69         uint8_t count = 20;
70         while (count--) {
71                 _delay_ms(10);
72                 wdt_reset();
73         }
74         printf("Waiting for Watchdog to kick\n");
75         // now , stop calling the watchdog reset, and just sleep until it fires
76         sleep_cpu();
77         printf("Watchdog kicked us!\n");
78
79         // when arriving here, the watchdog timer interupt was called and woke up
80         // the core from sleep, so we can just quit properly
81         cli();
82         sleep_cpu();
83 }