OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_wdt.vhd] - Blame information for rev 69

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - Watch Dog Timer (WDT) >>                                                         #
3
-- # ********************************************************************************************* #
4 47 zero_gravi
-- # Watchdog counter to trigger an action if the CPU gets stuck.                                  #
5
-- # The internal counter is 20-bit wide. If this counter overflows one of two possible actions is #
6
-- # triggered: Generate an IRQ or force a hardware reset of the system.                           #
7
-- # A WDT action can also be triggered manually at any time by setting the FORCE bit.             #
8
-- #                                                                                               #
9
-- # Access to the control register can be permanently locked by setting the lock bit. This bit    #
10
-- # can only be cleared by a hardware reset (external or caused by the watchdog itself).          #
11 2 zero_gravi
-- # ********************************************************************************************* #
12
-- # BSD 3-Clause License                                                                          #
13
-- #                                                                                               #
14 59 zero_gravi
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
15 2 zero_gravi
-- #                                                                                               #
16
-- # Redistribution and use in source and binary forms, with or without modification, are          #
17
-- # permitted provided that the following conditions are met:                                     #
18
-- #                                                                                               #
19
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
20
-- #    conditions and the following disclaimer.                                                   #
21
-- #                                                                                               #
22
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
23
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
24
-- #    provided with the distribution.                                                            #
25
-- #                                                                                               #
26
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
27
-- #    endorse or promote products derived from this software without specific prior written      #
28
-- #    permission.                                                                                #
29
-- #                                                                                               #
30
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
31
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
32
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
33
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
34
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
35
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
36
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
37
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
38
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
39
-- # ********************************************************************************************* #
40
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
41
-- #################################################################################################
42
 
43
library ieee;
44
use ieee.std_logic_1164.all;
45
use ieee.numeric_std.all;
46
 
47
library neorv32;
48
use neorv32.neorv32_package.all;
49
 
50
entity neorv32_wdt is
51 69 zero_gravi
  generic (
52
    DEBUG_EN : boolean -- CPU debug mode implemented?
53
  );
54 2 zero_gravi
  port (
55
    -- host access --
56
    clk_i       : in  std_ulogic; -- global clock line
57
    rstn_i      : in  std_ulogic; -- global reset line, low-active
58
    addr_i      : in  std_ulogic_vector(31 downto 0); -- address
59
    rden_i      : in  std_ulogic; -- read enable
60
    wren_i      : in  std_ulogic; -- write enable
61
    data_i      : in  std_ulogic_vector(31 downto 0); -- data in
62
    data_o      : out std_ulogic_vector(31 downto 0); -- data out
63
    ack_o       : out std_ulogic; -- transfer acknowledge
64 69 zero_gravi
    -- CPU in debug mode? --
65
    cpu_debug_i : in  std_ulogic;
66 2 zero_gravi
    -- clock generator --
67
    clkgen_en_o : out std_ulogic; -- enable clock generator
68
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
69
    -- timeout event --
70
    irq_o       : out std_ulogic; -- timeout IRQ
71
    rstn_o      : out std_ulogic  -- timeout reset, low_active, use as async
72
  );
73
end neorv32_wdt;
74
 
75
architecture neorv32_wdt_rtl of neorv32_wdt is
76
 
77
  -- IO space: module base address --
78
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
79
  constant lo_abb_c : natural := index_size_f(wdt_size_c); -- low address boundary bit
80
 
81
  -- Control register bits --
82 69 zero_gravi
  constant ctrl_enable_c  : natural :=  0; -- r/w: WDT enable
83
  constant ctrl_clksel0_c : natural :=  1; -- r/w: prescaler select bit 0
84
  constant ctrl_clksel1_c : natural :=  2; -- r/w: prescaler select bit 1
85
  constant ctrl_clksel2_c : natural :=  3; -- r/w: prescaler select bit 2
86
  constant ctrl_mode_c    : natural :=  4; -- r/w: 0: WDT timeout triggers interrupt, 1: WDT timeout triggers hard reset
87
  constant ctrl_rcause_c  : natural :=  5; -- r/-: cause of last action (reset/IRQ): 0=external reset, 1=watchdog overflow
88
  constant ctrl_reset_c   : natural :=  6; -- -/w: reset WDT if set
89
  constant ctrl_force_c   : natural :=  7; -- -/w: force WDT action
90
  constant ctrl_lock_c    : natural :=  8; -- r/w: lock access to control register when set
91
  constant ctrl_dben_c    : natural :=  9; -- r/w: allow WDT to continue operation even when in debug mode
92
  constant ctrl_half_c    : natural := 10; -- r/-: set if at least half of the max. timeout counter value has been reached
93 2 zero_gravi
 
94
  -- access control --
95 47 zero_gravi
  signal acc_en : std_ulogic; -- module access enable
96
  signal wren   : std_ulogic;
97
  signal rden   : std_ulogic;
98 2 zero_gravi
 
99 47 zero_gravi
  -- control register --
100 69 zero_gravi
  type ctrl_t is record
101 47 zero_gravi
    enable  : std_ulogic; -- 1=WDT enabled
102
    clk_sel : std_ulogic_vector(2 downto 0);
103
    mode    : std_ulogic; -- 0=trigger IRQ on overflow; 1=trigger hard reset on overflow
104
    rcause  : std_ulogic; -- cause of last system reset: '0' = external, '1' = watchdog
105
    reset   : std_ulogic; -- reset WDT
106 59 zero_gravi
    enforce : std_ulogic; -- force action
107 47 zero_gravi
    lock    : std_ulogic; -- lock control register
108 69 zero_gravi
    dben    : std_ulogic; -- allow operation also in debug mode
109 47 zero_gravi
  end record;
110 69 zero_gravi
  signal ctrl : ctrl_t;
111 2 zero_gravi
 
112
  -- prescaler clock generator --
113
  signal prsc_tick : std_ulogic;
114
 
115 47 zero_gravi
  -- WDT core --
116
  signal wdt_cnt : std_ulogic_vector(20 downto 0);
117
  signal hw_rst  : std_ulogic;
118
  signal rst_gen : std_ulogic_vector(03 downto 0);
119 69 zero_gravi
  signal cnt_en  : std_ulogic;
120 47 zero_gravi
 
121
  -- internal reset (sync, low-active) --
122
  signal rstn_sync : std_ulogic;
123
 
124 2 zero_gravi
begin
125
 
126
  -- Access Control -------------------------------------------------------------------------
127
  -- -------------------------------------------------------------------------------------------
128
  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';
129 47 zero_gravi
  wren   <= acc_en and wren_i;
130
  rden   <= acc_en and rden_i;
131 2 zero_gravi
 
132
 
133 47 zero_gravi
  -- Write Access ---------------------------------------------------------------------------
134 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
135 47 zero_gravi
  write_access: process(rstn_i, clk_i)
136 2 zero_gravi
  begin
137 47 zero_gravi
    if (rstn_i = '0') then
138 69 zero_gravi
      ctrl.reset   <= '1'; -- reset counter on start-up
139
      ctrl.enforce <= '0';
140
      ctrl.enable  <= '0'; -- disable WDT
141
      ctrl.mode    <= '0';
142
      ctrl.clk_sel <= (others => '0');
143
      ctrl.lock    <= '0';
144
      ctrl.dben    <= '0';
145 47 zero_gravi
    elsif rising_edge(clk_i) then
146
      if (rstn_sync = '0') then -- internal reset
147 69 zero_gravi
        ctrl.reset   <= '1'; -- reset counter on start-up
148
        ctrl.enforce <= '0';
149
        ctrl.enable  <= '0'; -- disable WDT
150
        ctrl.mode    <= '0';
151
        ctrl.clk_sel <= (others => '0');
152
        ctrl.lock    <= '0';
153
        ctrl.dben    <= '0';
154 2 zero_gravi
      else
155 47 zero_gravi
        -- auto-clear WDT reset and WDT force flags --
156 69 zero_gravi
        ctrl.reset   <= '0';
157
        ctrl.enforce <= '0';
158 47 zero_gravi
        -- actual write access --
159
        if (wren = '1') then
160 69 zero_gravi
          ctrl.reset   <= data_i(ctrl_reset_c);
161
          ctrl.enforce <= data_i(ctrl_force_c);
162
          if (ctrl.lock = '0') then -- update configuration only if not locked
163
            ctrl.enable  <= data_i(ctrl_enable_c);
164
            ctrl.mode    <= data_i(ctrl_mode_c);
165
            ctrl.clk_sel <= data_i(ctrl_clksel2_c downto ctrl_clksel0_c);
166
            ctrl.lock    <= data_i(ctrl_lock_c);
167
            ctrl.dben    <= data_i(ctrl_dben_c) and bool_to_ulogic_f(DEBUG_EN);
168 47 zero_gravi
          end if;
169 2 zero_gravi
        end if;
170
      end if;
171
    end if;
172 47 zero_gravi
  end process write_access;
173 2 zero_gravi
 
174 47 zero_gravi
  -- clock generator --
175 69 zero_gravi
  clkgen_en_o <= ctrl.enable; -- enable clock generator
176
  prsc_tick   <= clkgen_i(to_integer(unsigned(ctrl.clk_sel))); -- clock enable tick
177 2 zero_gravi
 
178
 
179 47 zero_gravi
  -- Watchdog Counter -----------------------------------------------------------------------
180 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
181 47 zero_gravi
  wdt_counter: process(clk_i)
182 2 zero_gravi
  begin
183
    if rising_edge(clk_i) then
184 69 zero_gravi
      if (ctrl.reset = '1') then -- watchdog reset
185 47 zero_gravi
        wdt_cnt <= (others => '0');
186 69 zero_gravi
      elsif (cnt_en = '1') then
187
        wdt_cnt <= std_ulogic_vector(unsigned('0' & wdt_cnt(wdt_cnt'left-1 downto 0)) + 1);
188 2 zero_gravi
      end if;
189
    end if;
190 47 zero_gravi
  end process wdt_counter;
191 2 zero_gravi
 
192 69 zero_gravi
  -- WDT counter enable --
193
  cnt_en <= ctrl.enable and prsc_tick and ((not cpu_debug_i) or ctrl.dben);
194
 
195 47 zero_gravi
  -- action trigger --
196 69 zero_gravi
  irq_o  <= ctrl.enable and (wdt_cnt(wdt_cnt'left) or ctrl.enforce) and (not ctrl.mode); -- mode 0: IRQ
197
  hw_rst <= ctrl.enable and (wdt_cnt(wdt_cnt'left) or ctrl.enforce) and (    ctrl.mode); -- mode 1: RESET
198 2 zero_gravi
 
199
 
200 47 zero_gravi
  -- Reset Generator & Action Cause Indicator -----------------------------------------------
201 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
202 47 zero_gravi
  reset_generator: process(rstn_i, clk_i)
203 2 zero_gravi
  begin
204
    if (rstn_i = '0') then
205 69 zero_gravi
      ctrl.rcause <= '0';
206
      rst_gen     <= (others => '1'); -- do NOT fire on reset!
207
      rstn_sync   <= '1';
208 2 zero_gravi
    elsif rising_edge(clk_i) then
209 69 zero_gravi
      ctrl.rcause <= ctrl.rcause or hw_rst; -- sticky-set on WDT timeout/force
210 47 zero_gravi
      if (hw_rst = '1') then
211
        rst_gen <= (others => '0');
212
      else
213
        rst_gen <= rst_gen(rst_gen'left-1 downto 0) & '1';
214
      end if;
215
      rstn_sync <= rst_gen(rst_gen'left);
216 2 zero_gravi
    end if;
217 47 zero_gravi
  end process reset_generator;
218 2 zero_gravi
 
219 47 zero_gravi
  -- system reset --
220
  rstn_o <= rst_gen(rst_gen'left);
221 2 zero_gravi
 
222 47 zero_gravi
 
223 2 zero_gravi
  -- Read Access ----------------------------------------------------------------------------
224
  -- -------------------------------------------------------------------------------------------
225
  read_access: process(clk_i)
226
  begin
227
    if rising_edge(clk_i) then
228 69 zero_gravi
      ack_o <= rden or wren;
229 47 zero_gravi
      if (rden = '1') then
230 69 zero_gravi
        data_o(ctrl_enable_c) <= ctrl.enable;
231
        data_o(ctrl_mode_c)   <= ctrl.mode;
232
        data_o(ctrl_rcause_c) <= ctrl.rcause;
233
        data_o(ctrl_clksel2_c downto ctrl_clksel0_c) <= ctrl.clk_sel;
234
        data_o(ctrl_lock_c)   <= ctrl.lock;
235
        data_o(ctrl_dben_c)   <= ctrl.dben;
236
        data_o(ctrl_half_c)   <= wdt_cnt(wdt_cnt'left-1);
237 47 zero_gravi
      else
238
        data_o <= (others => '0');
239 2 zero_gravi
      end if;
240
    end if;
241
  end process read_access;
242
 
243
 
244
end neorv32_wdt_rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.