When the reset is executed on an ARM Cortex-M based microcontroller, the processor will restart and reload the program counter to the start of the code memory. This resets the entire state of the microcontroller including all registers, peripherals, and program flow. The reset reinitializes the device to its power-on state.
Detailed Reset Sequence
Here are the key things that happen during a reset in an ARM Cortex-M microcontroller:
- The Cortex-M processor core resets, which resets all core registers like program counter, stack pointer, and general purpose registers back to their initial values.
- The Nested Vectored Interrupt Controller (NVIC) is reset, clearing any pending interrupts or active exception processing.
- All peripherals like timers, UARTs, I/O ports are reset back to their default configuration. Any pending states are cleared.
- Internal clocks used by the processor core, bus matrix, and peripherals are reinitialized.
- The system clock source is switched to the default out of reset.
- Flash memory and RAM controllers are reset, reinitializing any wait states or timings.
- The program counter is set to point to the start of the code memory, which contains the reset vector table.
- The Cortex-M processor executes the reset vector, starting execution at the reset handler function.
There are several sources that can trigger a microcontroller reset:
- Power-on reset – Generated on initial power-up of the microcontroller.
- External reset pin – Activated by driving the nRESET pin low.
- Software reset – Executed by software writing to the AIRCR register in the NVIC.
- Watchdog reset – Caused by the watchdog timer expiring.
- Brown-out reset – Triggered by the supply voltage falling below a safe operating level.
- Debug reset – Issued by an external debugger tool.
Reset Handler Execution
After a reset, the Cortex-M processor will start executing code at the beginning of the reset handler function. This function is usually defined at the start of the code memory, pointed to by the reset vector.
A typical reset handler will perform tasks like:
- Set up the stack pointer for the processor mode it will run in.
- Copy initialized values from ROM to RAM if necessary.
- Configure clock sources and PLLs.
- Initialize memory controllers and wait states.
- Configure I/O ports and peripherals.
- Clear any pending interrupts.
- Branch to the main application.
The reset handler runs with interrupts disabled initially. It needs to set up the necessary system state before it can re-enable interrupts and transition to the main application.
Resetting an ARM Cortex-M microcontroller is a useful technique for the following reasons:
- Recover from crashes – A reset can bring the system out of a lock-up or malformed state.
- Initialize on power-up – The first reset on boot puts the microcontroller in an initialized state.
- Transition between firmwares – A reset synchronizes the system when changing firmware images.
- Cleanup state – Resets peripheral states which can be useful when testing configurations.
- Entry point for debugging – Debuggers use reset to halt execution and get control.
Here are some examples of how reset can be used:
- Trigger a reset in firmware with a sysreset request when a severe error occurs.
- Press an external reset button to revive a crashed system.
- Use a watchdog reset to recover from a fault that hangs the processor.
- Perform a debug reset through JTAG/SWD when debugging code.
- Reset before jumping to a bootloader to update the main firmware image.
Common Reset Issues
Some common issues that developers may encounter with reset include:
- Forgetting to reconfigure peripherals after reset, leading to defaults which may cause issues.
- Neglecting to re-enable interrupts soon enough after reset, impacting system response.
- Stack corruption from not initializing the stack pointer correctly on reset.
- Leaving wake-up sources enabled that can wake processor immediately after reset.
- Uninitialized variables if reset handler does not handle copied RAM variables.
- Reset not deasserting and getting stuck in reset if reset duration is too short.
Debugging Reset Issues
Debugging reset related issues on Cortex-M microcontrollers can be performed by:
- Single stepping through reset handler code to validate correct setup sequence.
- Inspecting stack pointer alignment and RAM contents after reset.
- Checking peripheral registers to confirm reset values.
- Monitoring reset and clock signals using an oscilloscope.
- Measuring reset duration and voltage thresholds.
- Verifying reset vector loaded address is as expected.
- Tracing through alternate reset paths like soft reset.
- Testing reset with voltage supply perturbations.
Debuggers and development boards with external monitors and probes provide visibility into reset behavior. Monitoring tools like logic analyzers can capture reset timing characteristics.
In summary, reset is a fundamental mechanism used to restart ARM Cortex-M based microcontrollers in a known initialized state. It resets the processor core, peripherals, and interrupts while reinitializing clocks and memories. Executing the reset handler starts the software from a known point. Reset allows systems to recover from faults and provides an entry point for debuggers. Careful handling of reset ensures proper device operation.