mingw: make simavr compilable with MinGW
[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 <util/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         DDRD = (1<<PD1); // configure TxD as output
59         
60         wdt_enable(WDTO_120MS);
61
62         // enable watchdog interupt
63         // NOTE: since the Change Enable bit is no longer on, it should not
64         // change the timer value that is already set!
65         WDTCSR = (1 << WDIE);
66
67         sei();
68
69         printf("Watchdog is active\n");
70         uint8_t count = 20;
71         while (count--) {
72                 _delay_ms(10);
73                 wdt_reset();
74         }
75         printf("Waiting for Watchdog to kick\n");
76         // now , stop calling the watchdog reset, and just sleep until it fires
77         sleep_cpu();
78         printf("Watchdog kicked us!\n");
79
80         // when arriving here, the watchdog timer interupt was called and woke up
81         // the core from sleep, so we can just quit properly
82         cli();
83         sleep_cpu();
84 }