1 |
2 |
olivier.gi |
//----------------------------------------------------------------------------
|
2 |
117 |
olivier.gi |
// Copyright (C) 2009 , Olivier Girard
|
3 |
2 |
olivier.gi |
//
|
4 |
117 |
olivier.gi |
// Redistribution and use in source and binary forms, with or without
|
5 |
|
|
// modification, are permitted provided that the following conditions
|
6 |
|
|
// are met:
|
7 |
|
|
// * Redistributions of source code must retain the above copyright
|
8 |
|
|
// notice, this list of conditions and the following disclaimer.
|
9 |
|
|
// * Redistributions in binary form must reproduce the above copyright
|
10 |
|
|
// notice, this list of conditions and the following disclaimer in the
|
11 |
|
|
// documentation and/or other materials provided with the distribution.
|
12 |
|
|
// * Neither the name of the authors nor the names of its contributors
|
13 |
|
|
// may be used to endorse or promote products derived from this software
|
14 |
|
|
// without specific prior written permission.
|
15 |
2 |
olivier.gi |
//
|
16 |
117 |
olivier.gi |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
19 |
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
20 |
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
21 |
|
|
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
22 |
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
23 |
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
24 |
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
25 |
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
26 |
|
|
// THE POSSIBILITY OF SUCH DAMAGE
|
27 |
2 |
olivier.gi |
//
|
28 |
|
|
//----------------------------------------------------------------------------
|
29 |
|
|
//
|
30 |
34 |
olivier.gi |
// *File Name: omsp_watchdog.v
|
31 |
2 |
olivier.gi |
//
|
32 |
|
|
// *Module Description:
|
33 |
|
|
// Watchdog Timer
|
34 |
|
|
//
|
35 |
|
|
// *Author(s):
|
36 |
|
|
// - Olivier Girard, olgirard@gmail.com
|
37 |
|
|
//
|
38 |
|
|
//----------------------------------------------------------------------------
|
39 |
17 |
olivier.gi |
// $Rev: 117 $
|
40 |
|
|
// $LastChangedBy: olivier.girard $
|
41 |
|
|
// $LastChangedDate: 2011-06-23 21:30:51 +0200 (Thu, 23 Jun 2011) $
|
42 |
|
|
//----------------------------------------------------------------------------
|
43 |
103 |
olivier.gi |
`ifdef OMSP_NO_INCLUDE
|
44 |
|
|
`else
|
45 |
23 |
olivier.gi |
`include "openMSP430_defines.v"
|
46 |
103 |
olivier.gi |
`endif
|
47 |
2 |
olivier.gi |
|
48 |
34 |
olivier.gi |
module omsp_watchdog (
|
49 |
2 |
olivier.gi |
|
50 |
|
|
// OUTPUTs
|
51 |
|
|
nmi_evt, // NMI Event
|
52 |
|
|
per_dout, // Peripheral data output
|
53 |
|
|
wdtifg_set, // Set Watchdog-timer interrupt flag
|
54 |
|
|
wdtpw_error, // Watchdog-timer password error
|
55 |
|
|
wdttmsel, // Watchdog-timer mode select
|
56 |
|
|
|
57 |
|
|
// INPUTs
|
58 |
|
|
aclk_en, // ACLK enable
|
59 |
|
|
dbg_freeze, // Freeze Watchdog counter
|
60 |
|
|
mclk, // Main system clock
|
61 |
|
|
nmi, // Non-maskable interrupt (asynchronous)
|
62 |
|
|
nmie, // Non-maskable interrupt enable
|
63 |
|
|
per_addr, // Peripheral address
|
64 |
|
|
per_din, // Peripheral data input
|
65 |
|
|
per_en, // Peripheral enable (high active)
|
66 |
106 |
olivier.gi |
per_we, // Peripheral write enable (high active)
|
67 |
111 |
olivier.gi |
puc_rst, // Main system reset
|
68 |
2 |
olivier.gi |
smclk_en, // SMCLK enable
|
69 |
|
|
wdtie // Watchdog timer interrupt enable
|
70 |
|
|
);
|
71 |
|
|
|
72 |
|
|
// OUTPUTs
|
73 |
|
|
//=========
|
74 |
|
|
output nmi_evt; // NMI Event
|
75 |
|
|
output [15:0] per_dout; // Peripheral data output
|
76 |
|
|
output wdtifg_set; // Set Watchdog-timer interrupt flag
|
77 |
|
|
output wdtpw_error; // Watchdog-timer password error
|
78 |
|
|
output wdttmsel; // Watchdog-timer mode select
|
79 |
|
|
|
80 |
|
|
// INPUTs
|
81 |
|
|
//=========
|
82 |
|
|
input aclk_en; // ACLK enable
|
83 |
|
|
input dbg_freeze; // Freeze Watchdog counter
|
84 |
|
|
input mclk; // Main system clock
|
85 |
|
|
input nmi; // Non-maskable interrupt (asynchronous)
|
86 |
|
|
input nmie; // Non-maskable interrupt enable
|
87 |
111 |
olivier.gi |
input [13:0] per_addr; // Peripheral address
|
88 |
2 |
olivier.gi |
input [15:0] per_din; // Peripheral data input
|
89 |
|
|
input per_en; // Peripheral enable (high active)
|
90 |
106 |
olivier.gi |
input [1:0] per_we; // Peripheral write enable (high active)
|
91 |
111 |
olivier.gi |
input puc_rst; // Main system reset
|
92 |
2 |
olivier.gi |
input smclk_en; // SMCLK enable
|
93 |
|
|
input wdtie; // Watchdog timer interrupt enable
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
//=============================================================================
|
97 |
|
|
// 1) PARAMETER DECLARATION
|
98 |
|
|
//=============================================================================
|
99 |
|
|
|
100 |
111 |
olivier.gi |
// Register base address (must be aligned to decoder bit width)
|
101 |
|
|
parameter [14:0] BASE_ADDR = 15'h0120;
|
102 |
2 |
olivier.gi |
|
103 |
111 |
olivier.gi |
// Decoder bit width (defines how many bits are considered for address decoding)
|
104 |
|
|
parameter DEC_WD = 2;
|
105 |
2 |
olivier.gi |
|
106 |
111 |
olivier.gi |
// Register addresses offset
|
107 |
|
|
parameter [DEC_WD-1:0] WDTCTL = 'h0;
|
108 |
|
|
|
109 |
|
|
// Register one-hot decoder utilities
|
110 |
|
|
parameter DEC_SZ = 2**DEC_WD;
|
111 |
|
|
parameter [DEC_SZ-1:0] BASE_REG = {{DEC_SZ-1{1'b0}}, 1'b1};
|
112 |
|
|
|
113 |
2 |
olivier.gi |
// Register one-hot decoder
|
114 |
111 |
olivier.gi |
parameter [DEC_SZ-1:0] WDTCTL_D = (BASE_REG << WDTCTL);
|
115 |
2 |
olivier.gi |
|
116 |
|
|
|
117 |
|
|
//============================================================================
|
118 |
|
|
// 2) REGISTER DECODER
|
119 |
|
|
//============================================================================
|
120 |
|
|
|
121 |
111 |
olivier.gi |
// Local register selection
|
122 |
|
|
wire reg_sel = per_en & (per_addr[13:DEC_WD-1]==BASE_ADDR[14:DEC_WD]);
|
123 |
|
|
|
124 |
|
|
// Register local address
|
125 |
|
|
wire [DEC_WD-1:0] reg_addr = {per_addr[DEC_WD-2:0], 1'b0};
|
126 |
|
|
|
127 |
2 |
olivier.gi |
// Register address decode
|
128 |
111 |
olivier.gi |
wire [DEC_SZ-1:0] reg_dec = (WDTCTL_D & {DEC_SZ{(reg_addr==WDTCTL)}});
|
129 |
2 |
olivier.gi |
|
130 |
|
|
// Read/Write probes
|
131 |
111 |
olivier.gi |
wire reg_write = |per_we & reg_sel;
|
132 |
|
|
wire reg_read = ~|per_we & reg_sel;
|
133 |
2 |
olivier.gi |
|
134 |
|
|
// Read/Write vectors
|
135 |
111 |
olivier.gi |
wire [DEC_SZ-1:0] reg_wr = reg_dec & {DEC_SZ{reg_write}};
|
136 |
|
|
wire [DEC_SZ-1:0] reg_rd = reg_dec & {DEC_SZ{reg_read}};
|
137 |
2 |
olivier.gi |
|
138 |
|
|
|
139 |
|
|
//============================================================================
|
140 |
|
|
// 3) REGISTERS
|
141 |
|
|
//============================================================================
|
142 |
|
|
|
143 |
|
|
// WDTCTL Register
|
144 |
|
|
//-----------------
|
145 |
|
|
// WDTNMI & WDTSSEL are not implemented and therefore masked
|
146 |
|
|
|
147 |
|
|
reg [7:0] wdtctl;
|
148 |
|
|
|
149 |
|
|
wire wdtctl_wr = reg_wr[WDTCTL];
|
150 |
|
|
|
151 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
152 |
|
|
if (puc_rst) wdtctl <= 8'h00;
|
153 |
2 |
olivier.gi |
else if (wdtctl_wr) wdtctl <= per_din[7:0] & 8'hd7;
|
154 |
|
|
|
155 |
|
|
wire wdtpw_error = wdtctl_wr & (per_din[15:8]!=8'h5a);
|
156 |
|
|
wire wdttmsel = wdtctl[4];
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
//============================================================================
|
160 |
|
|
// 3) REGISTERS
|
161 |
|
|
//============================================================================
|
162 |
|
|
|
163 |
|
|
// Data output mux
|
164 |
|
|
wire [15:0] wdtctl_rd = {8'h69, wdtctl} & {16{reg_rd[WDTCTL]}};
|
165 |
|
|
|
166 |
|
|
wire [15:0] per_dout = wdtctl_rd;
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
//=============================================================================
|
170 |
|
|
// 4) NMI GENERATION
|
171 |
|
|
//=============================================================================
|
172 |
|
|
|
173 |
111 |
olivier.gi |
// Synchronization
|
174 |
|
|
wire nmi_s;
|
175 |
|
|
`ifdef SYNC_NMI
|
176 |
|
|
omsp_sync_cell sync_cell_nmi (
|
177 |
|
|
.data_out (nmi_s),
|
178 |
|
|
.clk (mclk),
|
179 |
|
|
.data_in (nmi),
|
180 |
|
|
.rst (puc_rst)
|
181 |
|
|
);
|
182 |
|
|
`else
|
183 |
|
|
assign nmi_s = nmi;
|
184 |
|
|
`endif
|
185 |
|
|
|
186 |
|
|
// Delay
|
187 |
|
|
reg nmi_dly;
|
188 |
|
|
always @ (posedge mclk or posedge puc_rst)
|
189 |
|
|
if (puc_rst) nmi_dly <= 1'b0;
|
190 |
|
|
else nmi_dly <= nmi_s;
|
191 |
2 |
olivier.gi |
|
192 |
|
|
// Edge detection
|
193 |
111 |
olivier.gi |
wire nmi_re = ~nmi_dly & nmi_s & nmie;
|
194 |
|
|
wire nmi_fe = nmi_dly & ~nmi_s & nmie;
|
195 |
2 |
olivier.gi |
|
196 |
|
|
// NMI event
|
197 |
|
|
wire nmi_evt = wdtctl[6] ? nmi_fe : nmi_re;
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
//=============================================================================
|
201 |
|
|
// 5) WATCHDOG TIMER
|
202 |
|
|
//=============================================================================
|
203 |
|
|
|
204 |
|
|
// Watchdog clock source selection
|
205 |
|
|
//---------------------------------
|
206 |
|
|
wire clk_src_en = wdtctl[2] ? aclk_en : smclk_en;
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
// Watchdog 16 bit counter
|
210 |
|
|
//--------------------------
|
211 |
|
|
reg [15:0] wdtcnt;
|
212 |
|
|
|
213 |
|
|
wire wdtcnt_clr = (wdtctl_wr & per_din[3]) | wdtifg_set;
|
214 |
|
|
|
215 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
216 |
|
|
if (puc_rst) wdtcnt <= 16'h0000;
|
217 |
2 |
olivier.gi |
else if (wdtcnt_clr) wdtcnt <= 16'h0000;
|
218 |
|
|
else if (~wdtctl[7] & clk_src_en & ~dbg_freeze) wdtcnt <= wdtcnt+16'h0001;
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
// Interval selection mux
|
222 |
|
|
//--------------------------
|
223 |
|
|
reg wdtqn;
|
224 |
|
|
|
225 |
|
|
always @(wdtctl or wdtcnt)
|
226 |
|
|
case(wdtctl[1:0])
|
227 |
|
|
2'b00 : wdtqn = wdtcnt[15];
|
228 |
|
|
2'b01 : wdtqn = wdtcnt[13];
|
229 |
|
|
2'b10 : wdtqn = wdtcnt[9];
|
230 |
|
|
default: wdtqn = wdtcnt[6];
|
231 |
|
|
endcase
|
232 |
|
|
|
233 |
|
|
|
234 |
|
|
// Watchdog event detection
|
235 |
|
|
//-----------------------------
|
236 |
|
|
reg wdtqn_dly;
|
237 |
|
|
|
238 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
239 |
|
|
if (puc_rst) wdtqn_dly <= 1'b0;
|
240 |
|
|
else wdtqn_dly <= wdtqn;
|
241 |
2 |
olivier.gi |
|
242 |
|
|
wire wdtifg_set = (~wdtqn_dly & wdtqn) | wdtpw_error;
|
243 |
|
|
|
244 |
|
|
|
245 |
34 |
olivier.gi |
endmodule // omsp_watchdog
|
246 |
2 |
olivier.gi |
|
247 |
103 |
olivier.gi |
`ifdef OMSP_NO_INCLUDE
|
248 |
|
|
`else
|
249 |
33 |
olivier.gi |
`include "openMSP430_undefines.v"
|
250 |
103 |
olivier.gi |
`endif
|