e31d3bb7ee352d2bc9cc0b63ddf0b33578311b40
[simavr] / simavr / sim / avr_watchdog.h
1 /*
2         avr_watchdog.h
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 #ifndef __AVR_WATCHDOG_H___
24 #define __AVR_WATCHDOG_H___
25
26
27 #include "sim_avr.h"
28
29 typedef struct avr_watchdog_t {
30         avr_io_t        io;
31
32         avr_regbit_t    wdrf;           // watchdog reset flag (in MCU Status Register)
33
34         avr_regbit_t    wdce;           // watchdog change enable
35         avr_regbit_t    wde;            // watchdog enabled
36         avr_regbit_t    wdp[4];         // watchdog Timer Prescaler
37
38         avr_int_vector_t watchdog;      // watchdog interrupt
39
40         avr_cycle_count_t       cycle_count;
41 } avr_watchdog_t;
42
43 /* takes no parameter */
44 #define AVR_IOCTL_WATCHDOG_RESET        AVR_IOCTL_DEF('w','d','t','r')
45
46 void avr_watchdog_init(avr_t * avr, avr_watchdog_t * p);
47
48
49 /*
50  * This helps declare a watchdog block into a core.
51  * No guarantee it will work with all, but it works
52  * with the one we have right now
53  */
54 #define AVR_WATCHDOG_DECLARE(_WDSR, _vec) \
55         .watchdog = {\
56                 .wdrf = AVR_IO_REGBIT(MCUSR, WDRF),\
57                 .wdce = AVR_IO_REGBIT(_WDSR, WDCE),\
58                 .wde = AVR_IO_REGBIT(_WDSR, WDE),\
59                 .wdp = { AVR_IO_REGBIT(_WDSR, WDP0),AVR_IO_REGBIT(_WDSR, WDP1),\
60                                 AVR_IO_REGBIT(_WDSR, WDP2),AVR_IO_REGBIT(_WDSR, WDP3) },\
61                 .watchdog = {\
62                         .enable = AVR_IO_REGBIT(_WDSR, WDIE),\
63                         .raised = AVR_IO_REGBIT(_WDSR, WDIF),\
64                         .vector = _vec,\
65                 },\
66         }
67
68 /* no WDP3, WDIE, WDIF in atmega128 */
69 #define AVR_WATCHDOG_DECLARE_128(_WDSR, _vec) \
70         .watchdog = {\
71                 .wdrf = AVR_IO_REGBIT(MCUSR, WDRF),\
72                 .wdce = AVR_IO_REGBIT(_WDSR, WDCE),\
73                 .wde = AVR_IO_REGBIT(_WDSR, WDE),\
74                 .wdp = { AVR_IO_REGBIT(_WDSR, WDP0),AVR_IO_REGBIT(_WDSR, WDP1),\
75                                 AVR_IO_REGBIT(_WDSR, WDP2) },\
76                 .watchdog = {\
77                         .enable = AVR_IO_REGBIT(_WDSR, 6),\
78                         .raised = AVR_IO_REGBIT(_WDSR, 7),\
79                         .vector = _vec,\
80                 },\
81         }
82
83 #endif /* __AVR_WATCHDOG_H___ */