1 |
198 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # < Test program for the Watchdog Timer (WDT) > #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # BSD 3-Clause License #
|
5 |
|
|
// # #
|
6 |
|
|
// # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
7 |
|
|
// # #
|
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 NEO430 Processor - https://github.com/stnolting/neo430 #
|
33 |
|
|
// #################################################################################################
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
// Libraries
|
37 |
|
|
#include <stdint.h>
|
38 |
|
|
#include <neo430.h>
|
39 |
|
|
|
40 |
|
|
// Configuration
|
41 |
|
|
#define BAUD_RATE 19200
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
/* ------------------------------------------------------------
|
45 |
|
|
* INFO Main function
|
46 |
|
|
* ------------------------------------------------------------ */
|
47 |
|
|
int main(void) {
|
48 |
|
|
|
49 |
|
|
// setup UART
|
50 |
|
|
neo430_uart_setup(BAUD_RATE);
|
51 |
|
|
|
52 |
|
|
// intro text
|
53 |
|
|
neo430_uart_br_print("\n<<< Watchdog Test Program >>>\n\n");
|
54 |
|
|
|
55 |
|
|
// check if WDT was synthesized, exit if no WDT is available
|
56 |
|
|
if (!(SYS_FEATURES & (1<<SYS_WDT_EN))) {
|
57 |
|
|
neo430_uart_br_print("Error! No WDT synthesized!");
|
58 |
|
|
return 0;
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
neo430_uart_br_print("Cause of last processor reset: ");
|
62 |
|
|
uint16_t wdt_cause = WDT_CT;
|
63 |
|
|
if ((wdt_cause & (1<<WDT_CT_RCAUSE)) == 0) { // reset caused by WDT at all?
|
64 |
|
|
neo430_uart_br_print("EXTERNAL RESET");
|
65 |
|
|
}
|
66 |
|
|
else {
|
67 |
|
|
if ((wdt_cause & (1<<WDT_CT_RPWFAIL)) == 0) { // WDT reset caused by wrong password access?
|
68 |
|
|
neo430_uart_br_print("WATCHDOG Timeout");
|
69 |
|
|
}
|
70 |
|
|
else {
|
71 |
|
|
neo430_uart_br_print("WATCHDOG Access Error");
|
72 |
|
|
}
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
|
76 |
|
|
neo430_uart_br_print("\n\nWill reset WDT 64 times.\n"
|
77 |
|
|
"A system reset will be executed in the following time out.\n"
|
78 |
|
|
"Press any key to trigger manual WDT hardware reset by WDT access with wrong password.\n"
|
79 |
|
|
"Restart this program after reset to check for the reset cause.\n\n"
|
80 |
|
|
"WDT resets: [................................................................]");
|
81 |
|
|
neo430_uart_bs(65); // back-space terminal cursor by 65 positions
|
82 |
|
|
|
83 |
|
|
// activate watchdog: second largest period
|
84 |
|
|
neo430_wdt_enable(WDT_PRSC_2048);
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
uint8_t i;
|
88 |
|
|
for (i=0; i<64; i++) {
|
89 |
|
|
neo430_uart_putc('#');
|
90 |
|
|
neo430_wdt_reset(); // reset watchdog
|
91 |
|
|
neo430_cpu_delay_ms(80); // wait some time
|
92 |
|
|
|
93 |
|
|
// trigger manual reset if key pressed
|
94 |
|
|
if (neo430_uart_char_received()) {
|
95 |
|
|
neo430_wdt_force_hw_reset(); // access wdt with wrong password
|
96 |
|
|
}
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
while (1) { // wait for the watchdog time-out or key press
|
100 |
|
|
if (neo430_uart_char_received()) {
|
101 |
|
|
neo430_wdt_force_hw_reset();
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
return 0;
|
106 |
|
|
}
|