|
| 1 | +/* |
| 2 | + Make the reset look like an EXT_RST reset by: |
| 3 | + * Set INTLEVEL to 15 blocking NMI Software WDT interference |
| 4 | + * set "restart reason" to REASON_EXT_SYS_RST |
| 5 | + * Config Hardware WDT for 1.6ms |
| 6 | + * Disable Hardware WDT Level-1 interrupt option |
| 7 | + * wait, ... |
| 8 | +
|
| 9 | + Inspired by RTOS SDK hardware_restart in panic.c |
| 10 | +*/ |
| 11 | + |
| 12 | +#include "Arduino.h" |
| 13 | +#include <user_interface.h> |
| 14 | +#include <ets_sys.h> |
| 15 | +#include "hardware_reset.h" |
| 16 | + |
| 17 | + |
| 18 | +// Extracted from RTOS_SDK eagle_soc.h |
| 19 | +/* |
| 20 | + * ESPRSSIF MIT License |
| 21 | + * |
| 22 | + * Copyright (c) 2015 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD> |
| 23 | + * |
| 24 | + * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case, |
| 25 | + * it is free of charge, to any person obtaining a copy of this software and associated |
| 26 | + * documentation files (the "Software"), to deal in the Software without restriction, including |
| 27 | + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 28 | + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished |
| 29 | + * to do so, subject to the following conditions: |
| 30 | + * |
| 31 | + * The above copyright notice and this permission notice shall be included in all copies or |
| 32 | + * substantial portions of the Software. |
| 33 | + * |
| 34 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 35 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 36 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 37 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 38 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 39 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 40 | + * |
| 41 | + */ |
| 42 | +#define REG_WRITE(_r, _v) (*(volatile uint32_t *)(_r)) = (_v) |
| 43 | +#define REG_READ(_r) (*(volatile uint32_t *)(_r)) |
| 44 | + |
| 45 | +//Watchdog reg {{ |
| 46 | +#define PERIPHS_WDT_BASEADDR 0x60000900 |
| 47 | + |
| 48 | +#define WDT_CTL_ADDRESS 0 |
| 49 | +#define WDT_OP_ADDRESS 0x4 |
| 50 | +#define WDT_OP_ND_ADDRESS 0x8 |
| 51 | +#define WDT_RST_ADDRESS 0x14 |
| 52 | + |
| 53 | +#define WDT_CTL_RSTLEN_MASK 0x38 |
| 54 | +#define WDT_CTL_RSPMOD_MASK 0x6 |
| 55 | +#define WDT_CTL_EN_MASK 0x1 |
| 56 | + |
| 57 | +#define WDT_CTL_RSTLEN_LSB 0x3 |
| 58 | +#define WDT_CTL_RSPMOD_LSB 0x1 |
| 59 | +#define WDT_CTL_EN_LSB 0 |
| 60 | + |
| 61 | +#define WDT_FEED_VALUE 0x73 |
| 62 | + |
| 63 | +#define WDT_REG_READ(_reg) REG_READ(PERIPHS_WDT_BASEADDR + _reg) |
| 64 | +#define WDT_REG_WRITE(_reg, _val) REG_WRITE(PERIPHS_WDT_BASEADDR + _reg, _val) |
| 65 | +#define CLEAR_WDT_REG_MASK(_reg, _mask) WDT_REG_WRITE(_reg, WDT_REG_READ(_reg) & (~_mask)) |
| 66 | +#define SET_WDT_REG_MASK(_reg, _mask, _val) SET_PERI_REG_BITS((PERIPHS_WDT_BASEADDR + _reg), _mask, _val, 0) |
| 67 | +#undef WDT_FEED |
| 68 | +#define WDT_FEED() WDT_REG_WRITE(WDT_RST_ADDRESS, WDT_FEED_VALUE) |
| 69 | +//}} |
| 70 | + |
| 71 | +// Inspired by RTOS SDK task_wdt.c and hardware_restart in panic.c |
| 72 | + |
| 73 | +// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD |
| 74 | +// |
| 75 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 76 | +// you may not use this file except in compliance with the License. |
| 77 | +// You may obtain a copy of the License at |
| 78 | +// |
| 79 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 80 | +// |
| 81 | +// Unless required by applicable law or agreed to in writing, software |
| 82 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 83 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 84 | +// See the License for the specific language governing permissions and |
| 85 | +// limitations under the License. |
| 86 | + |
| 87 | +extern "C" { |
| 88 | + void hardware_reset(void) { |
| 89 | + volatile uint32_t* const rtc_mem = (volatile uint32_t *)0x60001100u; |
| 90 | + |
| 91 | + // Block NMI or Software WDT from disturbing out restart reason |
| 92 | + xt_rsil(15); |
| 93 | + |
| 94 | + // An HWDT reason would imply a fault or bug, but this reset was requested. |
| 95 | + // Set hint reason to EXT_RST. From empirical evidence, an HWDT looks a lot |
| 96 | + // like an EXT_RST. The WDT registers are reset to zero like an EXT_RST; |
| 97 | + // however, the PLL initialization is still set. We can still read the Boot |
| 98 | + // ROM serial output messages. |
| 99 | + // SDK restart reason/hint location |
| 100 | + rtc_mem[0] = REASON_EXT_SYS_RST; |
| 101 | + |
| 102 | + // Disable WDT |
| 103 | + CLEAR_WDT_REG_MASK(WDT_CTL_ADDRESS, WDT_CTL_EN_MASK); |
| 104 | + |
| 105 | + // Set Reset pulse to maximum |
| 106 | + // Select Reset only - no level-1 interrupt |
| 107 | + SET_WDT_REG_MASK(WDT_CTL_ADDRESS, |
| 108 | + WDT_CTL_RSTLEN_MASK | WDT_CTL_RSPMOD_MASK, |
| 109 | + (7 << WDT_CTL_RSTLEN_LSB) | (2 << WDT_CTL_RSPMOD_LSB)); |
| 110 | + |
| 111 | + // Set WDT Reset timer to 1.6 ms. |
| 112 | + WDT_REG_WRITE(WDT_OP_ADDRESS, 1); // 2^n * 0.8ms, mask 0xf, n = 1 -> (2^1 = 2) * 0.8 * 0.001 = 0.0016 |
| 113 | + |
| 114 | + // Enable WDT |
| 115 | + SET_WDT_REG_MASK(WDT_CTL_ADDRESS, WDT_CTL_EN_MASK, 1 << WDT_CTL_EN_LSB); |
| 116 | + |
| 117 | + while (true); |
| 118 | + } |
| 119 | +}; |
0 commit comments