-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathp18_wdt.c
107 lines (82 loc) · 2.91 KB
/
p18_wdt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* ########################################################################
PICsim - PIC simulator
########################################################################
Copyright (c) : 2019-2020 Luis Claudio Gamboa Lopes
This program 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 2, or (at your option)
any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
For e-mail suggestions : lcgamboa@yahoo.com
######################################################################## */
#include "../../include/periferic18.h"
#include "../../include/picsim.h"
#include <stdio.h>
extern const int fpw2[];
void p18_wdt_rst(_pic *pic) {
pic->twdt = 0;
pic->wdt = 0;
}
void p18_wdt(_pic *pic) {
if (pic->getconf(pic, CFG_WDT) || ((*pic->P18map.WDTCON) & 0x01)) {
// printf("WDT ON %f %i 0x%02X\n",pic->twdt
// ,pic->wdt,(*pic->P18map.RCON));
pic->twdt += 4.0 / pic->freq;
if (pic->twdt > (1e-3 * fpw2[pic->getconf(pic, CFG_WDT_DIV)])) {
pic->twdt = 0;
pic->wdt++;
if (pic->wdt == pic->WDT_MS) {
// reset
pic->wdt = 0;
if (pic->P18map.RCON)
(*pic->P18map.RCON) &= ~0x08; // clear TO
if (pic->sleep == 1) {
pic->sleep = 0;
} else {
pic_reset(pic, 0);
}
}
}
}
}
void p18_wdt_2(_pic *pic) {
// TODO WDT support window and clock select
/*
pic->config[2] & 0x0700 WDTCWS WDT Configuration Clock Select bits
pic->config[2] & 0x3800 WDTCCS WDT Configuration Window Select bits
pic->config[2] & 0x001F WDTCPS WDT Configuration Period Select bits
*/
if ((pic->getconf(pic, CFG_WDT) == 0x60) || // Enabled
((pic->getconf(pic, CFG_WDT) == 0x40) &&
pic->sleep) || // Enabled and disabled in sleep
((pic->getconf(pic, CFG_WDT) == 0x20) &&
((*pic->P18map.WDTCON0) & 0x01))) // Software Enable
{
// printf("WDT ON %f %i 0x%02X\n",pic->twdt
// ,pic->wdt,(*pic->P18map.STATUS));
pic->twdt += 4.0 / pic->freq;
int div = pic->getconf(pic, CFG_WDT_DIV);
if (div > 18)
div = 0;
if (pic->twdt > (1e-3 * fpw2[div])) {
pic->twdt = 0;
pic->wdt++;
if (pic->wdt == pic->WDT_MS) {
// reset
pic->wdt = 0;
(*pic->P18map.STATUS) &= ~0x40; // TO
if (pic->sleep == 1) {
pic->sleep = 0;
} else {
pic_reset(pic, 0);
}
}
}
}
}