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

Subversion Repositories neorv32

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

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
    elsif rising_edge(clk_i) then
144 65 zero_gravi
      -- acknowledge interrupt when resetting WDT --
145 47 zero_gravi
      if (rstn_sync = '0') then -- internal reset
146
        ctrl_reg.reset   <= '0';
147 59 zero_gravi
        ctrl_reg.enforce <= '0';
148 47 zero_gravi
        ctrl_reg.enable  <= '0'; -- disable WDT
149
        ctrl_reg.mode    <= '0'; -- trigger interrupt on WDT overflow
150
        ctrl_reg.clk_sel <= (others => '1'); -- slowest clock source
151
        ctrl_reg.lock    <= '0';
152 2 zero_gravi
      else
153 47 zero_gravi
        -- auto-clear WDT reset and WDT force flags --
154 59 zero_gravi
        ctrl_reg.reset   <= '0';
155
        ctrl_reg.enforce <= '0';
156 47 zero_gravi
        -- actual write access --
157
        if (wren = '1') then
158 59 zero_gravi
          ctrl_reg.reset   <= data_i(ctrl_reset_c);
159
          ctrl_reg.enforce <= data_i(ctrl_force_c);
160 47 zero_gravi
          if (ctrl_reg.lock = '0') then -- update configuration only if unlocked
161
            ctrl_reg.enable  <= data_i(ctrl_enable_c);
162
            ctrl_reg.mode    <= data_i(ctrl_mode_c);
163
            ctrl_reg.clk_sel <= data_i(ctrl_clksel2_c downto ctrl_clksel0_c);
164
            ctrl_reg.lock    <= data_i(ctrl_lock_c);
165
          end if;
166 2 zero_gravi
        end if;
167
      end if;
168
    end if;
169 47 zero_gravi
  end process write_access;
170 2 zero_gravi
 
171 47 zero_gravi
  -- clock generator --
172
  clkgen_en_o <= ctrl_reg.enable; -- enable clock generator
173
  prsc_tick   <= clkgen_i(to_integer(unsigned(ctrl_reg.clk_sel))); -- clock enable tick
174 2 zero_gravi
 
175
 
176 47 zero_gravi
  -- Watchdog Counter -----------------------------------------------------------------------
177 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
178 47 zero_gravi
  wdt_counter: process(clk_i)
179 2 zero_gravi
  begin
180
    if rising_edge(clk_i) then
181 47 zero_gravi
      if (ctrl_reg.reset = '1') then -- watchdog reset
182
        wdt_cnt <= (others => '0');
183
      elsif (ctrl_reg.enable = '1') and (prsc_tick = '1') then
184
        wdt_cnt <= std_ulogic_vector(unsigned(wdt_cnt) + 1);
185 2 zero_gravi
      end if;
186
    end if;
187 47 zero_gravi
  end process wdt_counter;
188 2 zero_gravi
 
189 47 zero_gravi
  -- action trigger --
190 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
191 68 zero_gravi
  cpu_irq.clr <= ctrl_reg.reset; -- ack IRQ on WDT reset
192 65 zero_gravi
  hw_rst      <= ctrl_reg.enable and (wdt_cnt(wdt_cnt'left) or ctrl_reg.enforce) and (    ctrl_reg.mode); -- mode 1: RESET
193 2 zero_gravi
 
194
 
195 65 zero_gravi
  -- Interrupt ------------------------------------------------------------------------------
196
  -- -------------------------------------------------------------------------------------------
197
  irq_gen: process(clk_i)
198
  begin
199
    if rising_edge(clk_i) then
200
      if (ctrl_reg.enable = '0') then
201
        cpu_irq.pending <= '0';
202
      else
203
        if (cpu_irq.set = '1') then
204
          cpu_irq.pending <= '1';
205
        elsif(cpu_irq.clr = '1') then
206
          cpu_irq.pending <= '0';
207
        else
208
          cpu_irq.pending <= cpu_irq.pending;
209
        end if;
210
      end if;
211
    end if;
212
  end process irq_gen;
213
 
214
  -- CPU IRQ --
215
  irq_o <= cpu_irq.pending;
216
 
217
 
218 47 zero_gravi
  -- Reset Generator & Action Cause Indicator -----------------------------------------------
219 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
220 47 zero_gravi
  reset_generator: process(rstn_i, clk_i)
221 2 zero_gravi
  begin
222
    if (rstn_i = '0') then
223 47 zero_gravi
      ctrl_reg.rcause <= '0';
224
      rst_gen         <= (others => '1'); -- do NOT fire on reset!
225
      rstn_sync       <= '1';
226 2 zero_gravi
    elsif rising_edge(clk_i) then
227 47 zero_gravi
      ctrl_reg.rcause <= ctrl_reg.rcause or hw_rst; -- sticky-set on WDT timeout/force
228
      if (hw_rst = '1') then
229
        rst_gen <= (others => '0');
230
      else
231
        rst_gen <= rst_gen(rst_gen'left-1 downto 0) & '1';
232
      end if;
233
      rstn_sync <= rst_gen(rst_gen'left);
234 2 zero_gravi
    end if;
235 47 zero_gravi
  end process reset_generator;
236 2 zero_gravi
 
237 47 zero_gravi
  -- system reset --
238
  rstn_o <= rst_gen(rst_gen'left);
239 2 zero_gravi
 
240 47 zero_gravi
 
241 2 zero_gravi
  -- Read Access ----------------------------------------------------------------------------
242
  -- -------------------------------------------------------------------------------------------
243
  read_access: process(clk_i)
244
  begin
245
    if rising_edge(clk_i) then
246 47 zero_gravi
      ack_o  <= rden or wren;
247
      if (rden = '1') then
248
        data_o(ctrl_enable_c) <= ctrl_reg.enable;
249
        data_o(ctrl_mode_c)   <= ctrl_reg.mode;
250
        data_o(ctrl_rcause_c) <= ctrl_reg.rcause;
251
        data_o(ctrl_clksel2_c downto ctrl_clksel0_c) <= ctrl_reg.clk_sel;
252
        data_o(ctrl_lock_c)   <= ctrl_reg.lock;
253
      else
254
        data_o <= (others => '0');
255 2 zero_gravi
      end if;
256
    end if;
257
  end process read_access;
258
 
259
 
260
end neorv32_wdt_rtl;

powered by: WebSVN 2.1.0

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