Chip reset from Software

I’m using my AVR4809s in a remote location and on the odd occasion it might be helpful to do a hardware reset without having to travel to the site to physically press a button. My thought was to trigger a reset by accepting a serial input sequence such as reset# and use a gpio pin to pull the reset pin low. I guess I could try it, but I suspect there might be timing issues.

Otherwise, I could implement a watchdog timer and fail to ping that if a reset sequence is received.

any advice on a good approach much appreciated.

Per datasheet page 106 chapter 12.3.2.1.3

The software Reset makes it possible to issue a system Reset from software. The Reset is generated by writing a ‘1’ to the Software Reset Enable bit (SWRE) in the Software Reset register (RSTCTRL.SWRR).

The Reset will take place immediately after the bit is written and the device will be kept in reset until the Reset sequence is completed.

So the firmware itself is well-capable of doing a reset – that is, when the firmware can still execute code and is not stuck somewhere. That’s why it should be a must to activate the watchdog (which is hardware-supported) on top of that and always feed it in the regular codepath, so that a hangup results by design in a watchdog timeout and reset.

Thanks, I’ve tested out setting bit zero of the SWRR reg:

 RSTCTRL.SWRR |= (1<<0);

but on further reading of the datasheet on page 107, RSTCTRL.SWRR is under configuration Change Protection

12.3.4  Configuration Change Protection
This peripheral has registers that are under Configuration Change Protection (CCP). To write to these registers, a
certain key must first be written to the CPU.CCP register, followed by a write access to the protected bits within four
CPU instructions.
Attempting to write to a protected register without following the appropriate CCP unlock sequence leaves the
protected register unchanged.
The following registers are under CCP:
Table 12-2. RSTCTRL - Registers Under Configuration Change Protection
Register Key
RSTCTRL.SWRR IOREG

So i’ve found this from Microchip, but just recognising I’m ‘out of my depth’ and can’t see how to use the information to write IOREG to CPP.

Any help much appreciated.

Well as I read the datasheet chapter 12.3.4 and the ATMega4809 headers is that what they want is

  CPU_CCP = CCP_IOREG_gc;
  RSTCTRL.SWRR = 1;

which compiles, however, may not be safe against optimization because the write to SWRR has to occur within the next 4 cycles. That’s why the Microchip and the Arduino core readme https://github.com/SpenceKonde/megaTinyCore#triggering-a-reset-from-software correctly say

void resetViaSWR() {
  _PROTECTED_WRITE(RSTCTRL.SWRR,1);
}

is a correct way to implement it.

Thanks again, I did think about including the line you mentioned
CPU_CPP = CCP_IOREG_gc;

but couldn’t work out if that would work in the required 4 cycles. I’ll give the second method a try.