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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_wdt.c] - Blame information for rev 74

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
// #################################################################################################
2
// # << NEORV32: neorv32_wdt.c - Watchdog Timer (WDT) HW Driver >>                                 #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6 44 zero_gravi
// # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
7 2 zero_gravi
// #                                                                                               #
8
// # Redistribution and use in source and binary forms, with or without modification, are          #
9
// # permitted provided that the following conditions are met:                                     #
10
// #                                                                                               #
11
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
// #    conditions and the following disclaimer.                                                   #
13
// #                                                                                               #
14
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
// #    conditions and the following disclaimer in the documentation and/or other materials        #
16
// #    provided with the distribution.                                                            #
17
// #                                                                                               #
18
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
// #    endorse or promote products derived from this software without specific prior written      #
20
// #    permission.                                                                                #
21
// #                                                                                               #
22
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
// # ********************************************************************************************* #
32
// # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
// #################################################################################################
34
 
35
 
36
/**********************************************************************//**
37
 * @file neorv32_wdt.c
38
 * @brief Watchdog Timer (WDT) HW driver source file.
39
 *
40 44 zero_gravi
 * @note These functions should only be used if the WDT unit was synthesized (IO_WDT_EN = true).
41 2 zero_gravi
 **************************************************************************/
42
 
43
#include "neorv32.h"
44
#include "neorv32_wdt.h"
45
 
46
 
47
/**********************************************************************//**
48
 * Check if WDT unit was synthesized.
49
 *
50
 * @return 0 if WDT was not synthesized, 1 if WDT is available.
51
 **************************************************************************/
52
int neorv32_wdt_available(void) {
53
 
54 64 zero_gravi
  if (NEORV32_SYSINFO.SOC & (1 << SYSINFO_SOC_IO_WDT)) {
55 2 zero_gravi
    return 1;
56
  }
57
  else {
58
    return 0;
59
  }
60
}
61
 
62
 
63
/**********************************************************************//**
64 64 zero_gravi
 * Configure and enable watchdog timer. The WDT control register bits are listed in #NEORV32_WDT_CTRL_enum.
65 2 zero_gravi
 *
66 61 zero_gravi
 * @param[in] prsc Clock prescaler to select timeout interval. See #NEORV32_CLOCK_PRSC_enum.
67 47 zero_gravi
 * @param[in] mode Trigger system reset on timeout when 1, trigger interrupt on timeout when 0.
68 61 zero_gravi
 * @param[in] lock Control register will be locked when 1 (until next reset).
69 2 zero_gravi
 **************************************************************************/
70 47 zero_gravi
void neorv32_wdt_setup(uint8_t prsc, uint8_t mode, uint8_t lock) {
71 2 zero_gravi
 
72 64 zero_gravi
  NEORV32_WDT.CTRL = (1 << WDT_CTRL_RESET); // reset WDT counter
73 2 zero_gravi
 
74 47 zero_gravi
  uint32_t prsc_int = (uint32_t)(prsc & 0x07);
75 64 zero_gravi
  prsc_int = prsc_int << WDT_CTRL_CLK_SEL0;
76 2 zero_gravi
 
77 47 zero_gravi
  uint32_t mode_int = (uint32_t)(mode & 0x01);
78 64 zero_gravi
  mode_int = mode_int << WDT_CTRL_MODE;
79 2 zero_gravi
 
80 47 zero_gravi
  uint32_t lock_int = (uint32_t)(lock & 0x01);
81 64 zero_gravi
  lock_int = lock_int << WDT_CTRL_LOCK;
82 2 zero_gravi
 
83 64 zero_gravi
  const uint32_t enable = (uint32_t)(1 << WDT_CTRL_EN);
84 47 zero_gravi
 
85
  // update WDT control register
86 64 zero_gravi
  NEORV32_WDT.CTRL = enable | mode_int | prsc_int | lock_int;
87 2 zero_gravi
}
88
 
89
 
90
/**********************************************************************//**
91
 * Disable watchdog timer.
92 47 zero_gravi
 *
93 61 zero_gravi
 * @return Returns 0 if WDT is really deactivated, -1 otherwise.
94 2 zero_gravi
 **************************************************************************/
95 47 zero_gravi
int neorv32_wdt_disable(void) {
96
 
97 64 zero_gravi
  NEORV32_WDT.CTRL = 0;
98 2 zero_gravi
 
99 47 zero_gravi
  // check if wdt is really off
100 64 zero_gravi
  if (NEORV32_WDT.CTRL & (1 << WDT_CTRL_EN)) {
101 47 zero_gravi
    return -1; // WDT still active
102
  }
103
  else {
104
    return 0;
105
  }
106 2 zero_gravi
}
107
 
108
 
109
/**********************************************************************//**
110
 * Reset (running) watchdog.
111
 **************************************************************************/
112
void neorv32_wdt_reset(void) {
113
 
114 64 zero_gravi
  NEORV32_WDT.CTRL = NEORV32_WDT.CTRL | (1 << WDT_CTRL_RESET);
115 2 zero_gravi
}
116
 
117
 
118
/**********************************************************************//**
119 47 zero_gravi
 * Get cause of last system reset.
120 2 zero_gravi
 *
121 47 zero_gravi
 * @return Cause of last reset/IRQ (0: external reset, 1: watchdog timeout).
122 2 zero_gravi
 **************************************************************************/
123 47 zero_gravi
int neorv32_wdt_get_cause(void) {
124 2 zero_gravi
 
125 64 zero_gravi
  if (NEORV32_WDT.CTRL & (1 << WDT_CTRL_RCAUSE)) { // reset caused by watchdog
126 47 zero_gravi
    return 1;
127 2 zero_gravi
  }
128
  else { // external reset
129 47 zero_gravi
    return 0;
130 2 zero_gravi
  }
131
}
132
 
133
 
134
/**********************************************************************//**
135 47 zero_gravi
 * Force immediate watchdog action (reset/IRQ).
136 2 zero_gravi
 **************************************************************************/
137
void neorv32_wdt_force(void) {
138
 
139 64 zero_gravi
  NEORV32_WDT.CTRL = NEORV32_WDT.CTRL | (1 << WDT_CTRL_FORCE);
140 2 zero_gravi
}

powered by: WebSVN 2.1.0

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