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

Subversion Repositories neorv32

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

Go to most recent revision | 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
  port (
52
    -- host access --
53
    clk_i       : in  std_ulogic; -- global clock line
54
    rstn_i      : in  std_ulogic; -- global reset line, low-active
55
    addr_i      : in  std_ulogic_vector(31 downto 0); -- address
56
    rden_i      : in  std_ulogic; -- read enable
57
    wren_i      : in  std_ulogic; -- write enable
58
    data_i      : in  std_ulogic_vector(31 downto 0); -- data in
59
    data_o      : out std_ulogic_vector(31 downto 0); -- data out
60
    ack_o       : out std_ulogic; -- transfer acknowledge
61
    -- clock generator --
62
    clkgen_en_o : out std_ulogic; -- enable clock generator
63
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
64
    -- timeout event --
65
    irq_o       : out std_ulogic; -- timeout IRQ
66
    rstn_o      : out std_ulogic  -- timeout reset, low_active, use as async
67
  );
68
end neorv32_wdt;
69
 
70
architecture neorv32_wdt_rtl of neorv32_wdt is
71
 
72
  -- IO space: module base address --
73
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
74
  constant lo_abb_c : natural := index_size_f(wdt_size_c); -- low address boundary bit
75
 
76
  -- Control register bits --
77 47 zero_gravi
  constant ctrl_enable_c  : natural := 0; -- r/w: WDT enable
78
  constant ctrl_clksel0_c : natural := 1; -- r/w: prescaler select bit 0
79
  constant ctrl_clksel1_c : natural := 2; -- r/w: prescaler select bit 1
80
  constant ctrl_clksel2_c : natural := 3; -- r/w: prescaler select bit 2
81
  constant ctrl_mode_c    : natural := 4; -- r/w: 0: WDT timeout triggers interrupt, 1: WDT timeout triggers hard reset
82
  constant ctrl_rcause_c  : natural := 5; -- r/-: cause of last action (reset/IRQ): 0=external reset, 1=watchdog overflow
83
  constant ctrl_reset_c   : natural := 6; -- -/w: reset WDT if set
84
  constant ctrl_force_c   : natural := 7; -- -/w: force WDT action
85
  constant ctrl_lock_c    : natural := 8; -- r/w: lock access to control register when set
86 2 zero_gravi
 
87
  -- access control --
88 47 zero_gravi
  signal acc_en : std_ulogic; -- module access enable
89
  signal wren   : std_ulogic;
90
  signal rden   : std_ulogic;
91 2 zero_gravi
 
92 47 zero_gravi
  -- control register --
93
  type ctrl_reg_t is record
94
    enable  : std_ulogic; -- 1=WDT enabled
95
    clk_sel : std_ulogic_vector(2 downto 0);
96
    mode    : std_ulogic; -- 0=trigger IRQ on overflow; 1=trigger hard reset on overflow
97
    rcause  : std_ulogic; -- cause of last system reset: '0' = external, '1' = watchdog
98
    reset   : std_ulogic; -- reset WDT
99 59 zero_gravi
    enforce : std_ulogic; -- force action
100 47 zero_gravi
    lock    : std_ulogic; -- lock control register
101
  end record;
102
  signal ctrl_reg : ctrl_reg_t;
103 2 zero_gravi
 
104
  -- prescaler clock generator --
105
  signal prsc_tick : std_ulogic;
106
 
107 47 zero_gravi
  -- WDT core --
108
  signal wdt_cnt : std_ulogic_vector(20 downto 0);
109
  signal hw_rst  : std_ulogic;
110
  signal rst_gen : std_ulogic_vector(03 downto 0);
111
 
112
  -- internal reset (sync, low-active) --
113
  signal rstn_sync : std_ulogic;
114
 
115 65 zero_gravi
  -- cpu interrupt --
116
  type cpu_irq_t is record
117
    pending : std_ulogic;
118
    set     : std_ulogic;
119
    clr     : std_ulogic;
120
  end record;
121
  signal cpu_irq : cpu_irq_t;
122
 
123 2 zero_gravi
begin
124
 
125
  -- Access Control -------------------------------------------------------------------------
126
  -- -------------------------------------------------------------------------------------------
127
  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';
128 47 zero_gravi
  wren   <= acc_en and wren_i;
129
  rden   <= acc_en and rden_i;
130 2 zero_gravi
 
131
 
132 47 zero_gravi
  -- Write Access ---------------------------------------------------------------------------
133 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
134 47 zero_gravi
  write_access: process(rstn_i, clk_i)
135 2 zero_gravi
  begin
136 47 zero_gravi
    if (rstn_i = '0') then
137
      ctrl_reg.reset   <= '0';
138 59 zero_gravi
      ctrl_reg.enforce <= '0';
139 47 zero_gravi
      ctrl_reg.enable  <= '0'; -- disable WDT
140
      ctrl_reg.mode    <= '0'; -- trigger interrupt on WDT overflow
141
      ctrl_reg.clk_sel <= (others => '1'); -- slowest clock source
142
      ctrl_reg.lock    <= '0';
143 65 zero_gravi
      cpu_irq.clr      <= '-';
144 47 zero_gravi
    elsif rising_edge(clk_i) then
145 65 zero_gravi
      -- acknowledge interrupt when resetting WDT --
146
      cpu_irq.clr <= ctrl_reg.reset;
147 47 zero_gravi
      if (rstn_sync = '0') then -- internal reset
148
        ctrl_reg.reset   <= '0';
149 59 zero_gravi
        ctrl_reg.enforce <= '0';
150 47 zero_gravi
        ctrl_reg.enable  <= '0'; -- disable WDT
151
        ctrl_reg.mode    <= '0'; -- trigger interrupt on WDT overflow
152
        ctrl_reg.clk_sel <= (others => '1'); -- slowest clock source
153
        ctrl_reg.lock    <= '0';
154 2 zero_gravi
      else
155 47 zero_gravi
        -- auto-clear WDT reset and WDT force flags --
156 59 zero_gravi
        ctrl_reg.reset   <= '0';
157
        ctrl_reg.enforce <= '0';
158 47 zero_gravi
        -- actual write access --
159
        if (wren = '1') then
160 59 zero_gravi
          ctrl_reg.reset   <= data_i(ctrl_reset_c);
161
          ctrl_reg.enforce <= data_i(ctrl_force_c);
162 47 zero_gravi
          if (ctrl_reg.lock = '0') then -- update configuration only if unlocked
163
            ctrl_reg.enable  <= data_i(ctrl_enable_c);
164
            ctrl_reg.mode    <= data_i(ctrl_mode_c);
165
            ctrl_reg.clk_sel <= data_i(ctrl_clksel2_c downto ctrl_clksel0_c);
166
            ctrl_reg.lock    <= data_i(ctrl_lock_c);
167
          end if;
168 2 zero_gravi
        end if;
169
      end if;
170
    end if;
171 47 zero_gravi
  end process write_access;
172 2 zero_gravi
 
173 47 zero_gravi
  -- clock generator --
174
  clkgen_en_o <= ctrl_reg.enable; -- enable clock generator
175
  prsc_tick   <= clkgen_i(to_integer(unsigned(ctrl_reg.clk_sel))); -- clock enable tick
176 2 zero_gravi
 
177
 
178 47 zero_gravi
  -- Watchdog Counter -----------------------------------------------------------------------
179 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
180 47 zero_gravi
  wdt_counter: process(clk_i)
181 2 zero_gravi
  begin
182
    if rising_edge(clk_i) then
183 47 zero_gravi
      if (ctrl_reg.reset = '1') then -- watchdog reset
184
        wdt_cnt <= (others => '0');
185
      elsif (ctrl_reg.enable = '1') and (prsc_tick = '1') then
186
        wdt_cnt <= std_ulogic_vector(unsigned(wdt_cnt) + 1);
187 2 zero_gravi
      end if;
188
    end if;
189 47 zero_gravi
  end process wdt_counter;
190 2 zero_gravi
 
191 47 zero_gravi
  -- action trigger --
192 65 zero_gravi
  cpu_irq.set <= ctrl_reg.enable and (wdt_cnt(wdt_cnt'left) or ctrl_reg.enforce) and (not ctrl_reg.mode); -- mode 0: IRQ
193
  hw_rst      <= ctrl_reg.enable and (wdt_cnt(wdt_cnt'left) or ctrl_reg.enforce) and (    ctrl_reg.mode); -- mode 1: RESET
194 2 zero_gravi
 
195
 
196 65 zero_gravi
  -- Interrupt ------------------------------------------------------------------------------
197
  -- -------------------------------------------------------------------------------------------
198
  irq_gen: process(clk_i)
199
  begin
200
    if rising_edge(clk_i) then
201
      if (ctrl_reg.enable = '0') then
202
        cpu_irq.pending <= '0';
203
      else
204
        if (cpu_irq.set = '1') then
205
          cpu_irq.pending <= '1';
206
        elsif(cpu_irq.clr = '1') then
207
          cpu_irq.pending <= '0';
208
        else
209
          cpu_irq.pending <= cpu_irq.pending;
210
        end if;
211
      end if;
212
    end if;
213
  end process irq_gen;
214
 
215
  -- CPU IRQ --
216
  irq_o <= cpu_irq.pending;
217
 
218
 
219 47 zero_gravi
  -- Reset Generator & Action Cause Indicator -----------------------------------------------
220 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
221 47 zero_gravi
  reset_generator: process(rstn_i, clk_i)
222 2 zero_gravi
  begin
223
    if (rstn_i = '0') then
224 47 zero_gravi
      ctrl_reg.rcause <= '0';
225
      rst_gen         <= (others => '1'); -- do NOT fire on reset!
226
      rstn_sync       <= '1';
227 2 zero_gravi
    elsif rising_edge(clk_i) then
228 47 zero_gravi
      ctrl_reg.rcause <= ctrl_reg.rcause or hw_rst; -- sticky-set on WDT timeout/force
229
      if (hw_rst = '1') then
230
        rst_gen <= (others => '0');
231
      else
232
        rst_gen <= rst_gen(rst_gen'left-1 downto 0) & '1';
233
      end if;
234
      rstn_sync <= rst_gen(rst_gen'left);
235 2 zero_gravi
    end if;
236 47 zero_gravi
  end process reset_generator;
237 2 zero_gravi
 
238 47 zero_gravi
  -- system reset --
239
  rstn_o <= rst_gen(rst_gen'left);
240 2 zero_gravi
 
241 47 zero_gravi
 
242 2 zero_gravi
  -- Read Access ----------------------------------------------------------------------------
243
  -- -------------------------------------------------------------------------------------------
244
  read_access: process(clk_i)
245
  begin
246
    if rising_edge(clk_i) then
247 47 zero_gravi
      ack_o  <= rden or wren;
248
      if (rden = '1') then
249
        data_o(ctrl_enable_c) <= ctrl_reg.enable;
250
        data_o(ctrl_mode_c)   <= ctrl_reg.mode;
251
        data_o(ctrl_rcause_c) <= ctrl_reg.rcause;
252
        data_o(ctrl_clksel2_c downto ctrl_clksel0_c) <= ctrl_reg.clk_sel;
253
        data_o(ctrl_lock_c)   <= ctrl_reg.lock;
254
      else
255
        data_o <= (others => '0');
256 2 zero_gravi
      end if;
257
    end if;
258
  end process read_access;
259
 
260
 
261
end neorv32_wdt_rtl;

powered by: WebSVN 2.1.0

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