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