Sunday, May 26, 2013

Software Reset for Atmel AVR MCUs

______________________________________________

Section 1: The Problem
Section 2: Beware of the [watch] Dog!!
Section 3: The Code
______________________________________________


Despite the fact that the content of this post is targeted to AVR processors, everything explained here is valid for any other processor from any other manufacturers.

Section 1: The Problem

Well... there are no many words to say about this issue... just some processors have available some instructions to force a system RESET by software, and other processors do not.

Then, what can we do if we need to force a system reset and do not have an instruction to do it?

Ok, we are not discovering the fire. The best and simplest way to do this task is by forcing a condition for a peripheral, or system module that generates the system reset for us.

In most cases (and also for our case [AVR processors]), the simplest way is using the Watchdog Timer module to generate a system reset for us.

Basically, we will implement a function (i.e. void mySoftwareReset(void)) that performs the following tasks:

1. Configures Watchdog timer module to generate a system reset on timer overflow.

2. Enables and/or starts the Watchdog Timer.

3. Performs an endless loop (awaiting for watchdog overflow).


This is as simple as that.



Section 2: Beware of the [watch] Dog!!

Before going through the code just a very simple (yet mandatory) trick must be considered to avoid putting your processor in an endless reset loop...

You must keep the following two issues in mind:

1. After system reset caused by watchdog timer, the watchdog restarts counting and will generate another system reset on next overflow.

2. After system reset, MCU's startup code performs some tasks after calling your main firmware code, like initializing stacks, initializing .bss sections, moving some code to RAM (if it was specified in your linker file), etc. All these tasks require some computation time, of course.


If you set a very short watchdog timer period to force a system reset, and this period is smaller than system's startup code execution time, your processor's watchdog will overflow (and generate another system reset) before initialization process ends, so you will leave your system in a continuous and endless reset loop.

So remember, setting up your watchdog timer period long enough to let system execute startup code and watchdog stop/disable code once the system has been reset.



Section 3: The Code

In this section you have available some implementations of funtions performing software reset for some AVR processors.

Software reset code for Atmel AVR8 processors (includes ATtiny and ATmega)...




Software reset code for Atmel AVR32 processors...