1 |
198 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEO430 - Watchdog Timer >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
|
|
-- # The internal counter is 16 bit wide and triggers a HW reset when overflowing. The clock is #
|
5 |
|
|
-- # selected via the clk_sel bits of the control register. The WDT can only operate when the #
|
6 |
|
|
-- # enable bit is set. A write access to the WDT can only be performed, if the higher byte of the #
|
7 |
|
|
-- # written data contains the specific WDT password (0x47). I a write access occurs with a wrong #
|
8 |
|
|
-- # password, a HW reset is triggered, but only if the WDT is enabled. #
|
9 |
|
|
-- # ********************************************************************************************* #
|
10 |
|
|
-- # BSD 3-Clause License #
|
11 |
|
|
-- # #
|
12 |
|
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
13 |
|
|
-- # #
|
14 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
15 |
|
|
-- # permitted provided that the following conditions are met: #
|
16 |
|
|
-- # #
|
17 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
18 |
|
|
-- # conditions and the following disclaimer. #
|
19 |
|
|
-- # #
|
20 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
21 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
22 |
|
|
-- # provided with the distribution. #
|
23 |
|
|
-- # #
|
24 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
25 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
26 |
|
|
-- # permission. #
|
27 |
|
|
-- # #
|
28 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
29 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
30 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
31 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
32 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
33 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
34 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
35 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
36 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
37 |
|
|
-- # ********************************************************************************************* #
|
38 |
|
|
-- # The NEO430 Processor - https://github.com/stnolting/neo430 #
|
39 |
|
|
-- #################################################################################################
|
40 |
|
|
|
41 |
|
|
library ieee;
|
42 |
|
|
use ieee.std_logic_1164.all;
|
43 |
|
|
use ieee.numeric_std.all;
|
44 |
|
|
|
45 |
|
|
library neo430;
|
46 |
|
|
use neo430.neo430_package.all;
|
47 |
|
|
|
48 |
|
|
entity neo430_wdt is
|
49 |
|
|
port (
|
50 |
|
|
-- host access --
|
51 |
|
|
clk_i : in std_ulogic; -- global clock line
|
52 |
|
|
rst_i : in std_ulogic; -- external reset, low-active, use as async
|
53 |
|
|
rden_i : in std_ulogic; -- read enable
|
54 |
|
|
wren_i : in std_ulogic; -- write enable
|
55 |
|
|
addr_i : in std_ulogic_vector(15 downto 0); -- address
|
56 |
|
|
data_i : in std_ulogic_vector(15 downto 0); -- data in
|
57 |
|
|
data_o : out std_ulogic_vector(15 downto 0); -- data out
|
58 |
|
|
-- clock generator --
|
59 |
|
|
clkgen_en_o : out std_ulogic; -- enable clock generator
|
60 |
|
|
clkgen_i : in std_ulogic_vector(07 downto 0);
|
61 |
|
|
-- system reset --
|
62 |
|
|
rst_o : out std_ulogic -- timeout reset, low_active, use it as async!
|
63 |
|
|
);
|
64 |
|
|
end neo430_wdt;
|
65 |
|
|
|
66 |
|
|
architecture neo430_wdt_rtl of neo430_wdt is
|
67 |
|
|
|
68 |
|
|
-- IO space: module base address --
|
69 |
|
|
constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
|
70 |
|
|
constant lo_abb_c : natural := index_size_f(wdt_size_c); -- low address boundary bit
|
71 |
|
|
|
72 |
|
|
-- Watchdog access password - do not change! --
|
73 |
|
|
constant wdt_password_c : std_ulogic_vector(07 downto 0) := x"47";
|
74 |
|
|
|
75 |
|
|
-- Control register bits --
|
76 |
|
|
constant ctrl_clksel0_c : natural := 0; -- r/w: prescaler select bit 0
|
77 |
|
|
constant ctrl_clksel1_c : natural := 1; -- r/w: prescaler select bit 1
|
78 |
|
|
constant ctrl_clksel2_c : natural := 2; -- r/w: prescaler select bit 2
|
79 |
|
|
constant ctrl_enable_c : natural := 3; -- r/w: WDT enable
|
80 |
|
|
constant ctrl_rcause_c : natural := 4; -- r/-: reset cause (0: external, 1: watchdog timeout)
|
81 |
|
|
constant ctrl_rpwfail_c : natural := 5; -- r/-: watchdog reset caused by wrong password access when '1'
|
82 |
|
|
|
83 |
|
|
-- access control --
|
84 |
|
|
signal acc_en : std_ulogic; -- module access enable
|
85 |
|
|
signal pwd_ok : std_ulogic; -- password correct
|
86 |
|
|
signal fail, fail_ff : std_ulogic; -- unauthorized access
|
87 |
|
|
signal wren : std_ulogic;
|
88 |
|
|
|
89 |
|
|
-- accessible regs --
|
90 |
|
|
signal rst_source : std_ulogic; -- source of the system reset: '0' = external, '1' = watchdog timeout
|
91 |
|
|
signal pw_fail : std_ulogic; -- watchdog reset caused by wrong password access
|
92 |
|
|
signal enable : std_ulogic;
|
93 |
|
|
signal clk_sel : std_ulogic_vector(02 downto 0);
|
94 |
|
|
|
95 |
|
|
-- reset counter --
|
96 |
|
|
signal cnt : std_ulogic_vector(16 downto 0);
|
97 |
|
|
signal rst_gen : std_ulogic_vector(03 downto 0);
|
98 |
|
|
signal rst_sync : std_ulogic_vector(01 downto 0);
|
99 |
|
|
|
100 |
|
|
-- prescaler clock generator --
|
101 |
|
|
signal prsc_tick : std_ulogic;
|
102 |
|
|
|
103 |
|
|
begin
|
104 |
|
|
|
105 |
|
|
-- Access Control -----------------------------------------------------------
|
106 |
|
|
-- -----------------------------------------------------------------------------
|
107 |
|
|
acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = wdt_base_c(hi_abb_c downto lo_abb_c)) else '0';
|
108 |
|
|
pwd_ok <= '1' when (data_i(15 downto 8) = wdt_password_c) else '0'; -- password check
|
109 |
|
|
wren <= '1' when ((acc_en = '1') and (wren_i = '1') and (pwd_ok = '1')) else '0'; -- write access ok
|
110 |
|
|
fail <= '1' when ((acc_en = '1') and (wren_i = '1') and (pwd_ok = '0')) else '0'; -- write access fail!
|
111 |
|
|
|
112 |
|
|
|
113 |
|
|
-- Write Access, Reset Generator --------------------------------------------
|
114 |
|
|
-- -----------------------------------------------------------------------------
|
115 |
|
|
wdt_core: process(clk_i)
|
116 |
|
|
begin
|
117 |
|
|
if rising_edge(clk_i) then
|
118 |
|
|
if (rst_i = '0') or (rst_sync(1) = '0') then -- external or internal reset
|
119 |
|
|
enable <= '0'; -- disable WDT
|
120 |
|
|
clk_sel <= (others => '1'); -- slowest clock rst_source
|
121 |
|
|
rst_gen <= (others => '1'); -- do NOT fire on reset!
|
122 |
|
|
else
|
123 |
|
|
-- control register write access --
|
124 |
|
|
if (wren = '1') then -- allow write if password is correct
|
125 |
|
|
enable <= data_i(ctrl_enable_c);
|
126 |
|
|
clk_sel <= data_i(ctrl_clksel2_c downto ctrl_clksel0_c);
|
127 |
|
|
end if;
|
128 |
|
|
-- reset generator - enabled and (overflow or unauthorized access)? --
|
129 |
|
|
if (enable = '1') and ((cnt(cnt'left) = '1') or (fail_ff = '1')) then
|
130 |
|
|
rst_gen <= (others => '0');
|
131 |
|
|
else
|
132 |
|
|
rst_gen <= rst_gen(rst_gen'left-1 downto 0) & '1';
|
133 |
|
|
end if;
|
134 |
|
|
end if;
|
135 |
|
|
end if;
|
136 |
|
|
end process wdt_core;
|
137 |
|
|
|
138 |
|
|
-- enable external clock generator --
|
139 |
|
|
clkgen_en_o <= enable;
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
-- Counter Update -----------------------------------------------------------
|
143 |
|
|
-- -----------------------------------------------------------------------------
|
144 |
|
|
cnt_sync: process(clk_i)
|
145 |
|
|
begin
|
146 |
|
|
if rising_edge(clk_i) then
|
147 |
|
|
-- clock_en buffer --
|
148 |
|
|
prsc_tick <= clkgen_i(to_integer(unsigned(clk_sel)));
|
149 |
|
|
-- unauthorized access buffer --
|
150 |
|
|
fail_ff <= fail;
|
151 |
|
|
-- reset synchronizer --
|
152 |
|
|
rst_sync <= rst_sync(0) & rst_gen(rst_gen'left);
|
153 |
|
|
-- counter update --
|
154 |
|
|
if (wren = '1') then -- clear counter on write access (manual watchdog reset)
|
155 |
|
|
cnt <= (others => '0');
|
156 |
|
|
elsif (enable = '1') and (prsc_tick = '1') then
|
157 |
|
|
cnt <= std_ulogic_vector(unsigned('0' & cnt(cnt'left-1 downto 0)) + 1);
|
158 |
|
|
end if;
|
159 |
|
|
end if;
|
160 |
|
|
end process cnt_sync;
|
161 |
|
|
|
162 |
|
|
-- system reset --
|
163 |
|
|
rst_o <= rst_sync(1);
|
164 |
|
|
|
165 |
|
|
|
166 |
|
|
-- Reset Cause Indicator ----------------------------------------------------
|
167 |
|
|
-- -----------------------------------------------------------------------------
|
168 |
|
|
rst_cause: process(rst_i, clk_i)
|
169 |
|
|
begin
|
170 |
|
|
if (rst_i = '0') then
|
171 |
|
|
rst_source <= '0';
|
172 |
|
|
pw_fail <= '0';
|
173 |
|
|
elsif rising_edge(clk_i) then
|
174 |
|
|
rst_source <= rst_source or (cnt(cnt'left) and enable) or (fail_ff and enable); -- set on WDT timeout or access error
|
175 |
|
|
pw_fail <= (pw_fail or (fail_ff and enable)) and (not (cnt(cnt'left) and enable)); -- set on failed access, clear on WDT timeout
|
176 |
|
|
--pw_fail <= (pw_fail and (not (cnt(cnt'left) and enable))) or (fail_ff and enable); -- clear on WDT timeout, set on failed access
|
177 |
|
|
end if;
|
178 |
|
|
end process rst_cause;
|
179 |
|
|
|
180 |
|
|
|
181 |
|
|
-- Read Access --------------------------------------------------------------
|
182 |
|
|
-- -----------------------------------------------------------------------------
|
183 |
|
|
read_access: process(clk_i)
|
184 |
|
|
begin
|
185 |
|
|
if rising_edge(clk_i) then
|
186 |
|
|
data_o <= (others => '0');
|
187 |
|
|
if (acc_en = '1') and (rden_i = '1') then
|
188 |
|
|
data_o(ctrl_clksel2_c downto ctrl_clksel0_c) <= clk_sel;
|
189 |
|
|
data_o(ctrl_enable_c) <= enable;
|
190 |
|
|
data_o(ctrl_rcause_c) <= rst_source;
|
191 |
|
|
data_o(ctrl_rpwfail_c) <= pw_fail;
|
192 |
|
|
end if;
|
193 |
|
|
end if;
|
194 |
|
|
end process read_access;
|
195 |
|
|
|
196 |
|
|
|
197 |
|
|
end neo430_wdt_rtl;
|