The NVIC_SystemReset() function can be used to trigger a soft reset in Cortex-M0 based microcontrollers. This function is part of the Nested Vectored Interrupt Controller (NVIC) peripheral found in ARM Cortex-M0 chips. Calling NVIC_SystemReset() will restart the microcontroller and reset most peripherals and registers to their power-on default states, while keeping power and clock configurations intact.
Introduction to Soft Resets
A soft reset, also known as a warm reset, restarts the microcontroller but does not completely reset all hardware like a power-on reset. It resets the processor, most peripherals, and resets registers like the stack pointer and program counter to known states. However, it does not reset clock or power configurations, so the microcontroller can boot up quicker than from a hard reset.
Soft resets are useful when you want to restart the firmware or reboot the system without losing power or clock configurations. It can be used to recover from software crashes, reset peripherals that are acting erratically, or restart the firmware as part of normal operation.
Using NVIC_SystemReset() for Soft Reset
The NVIC peripheral built into Cortex-M0 chips provides a special function for soft reset called NVIC_SystemReset(). This function handles the system reset process at the core/processor level. When called, it will reset the core registers, peripherals, and restart program execution, while retaining clock and power states.
To use NVIC_SystemReset(), simply call the function anywhere in your code. When executed, it will trigger the soft reset process. For example:
void restartSystem() {
NVIC_SystemReset();
}
Calling this function will perform the soft reset immediately. No other code after NVIC_SystemReset() will execute. The microcontroller will reset its states and start executing from the reset vector like on power on.
NVIC_SystemReset() Reset Details
Here are some key points on what NVIC_SystemReset() resets:
- Resets core processor registers like stack pointer, program counter, and general purpose registers.
- Resets most peripherals back to their default states. Specific behavior defined in chip reference manual.
- Retains clock configurations and power states.
- Triggers a reset exception which loads the stack pointer and branches to reset vector.
- Starts executing code from reset vector like on power-on.
So NVIC_SystemReset() will restart your program execution from the beginning while retaining your clock speeds and power states. Peripherals that rely on clocks or power will maintain their configurations, but interface registers are reset to defaults.
Using NVIC_SystemReset() for Recovery
One of the main uses of NVIC_SystemReset() is to recover from crashes or erratic peripheral behavior. For example, if your program enters an unexpected state or a peripheral stops responding, you can trigger a controlled soft reset to restart the system.
To implement this, you would call NVIC_SystemReset() inside an exception handler or check routine. For example:
void checkPeripheral() {
if (peripheralNotResponding()) {
NVIC_SystemReset();
}
}
This allows you to monitor for issues and reset the microcontroller to recover. The system will reboot while retaining previous power and clock configurations.
Using Soft Reset During Normal Operation
In some cases, you may want to use soft reset as part of application logic during normal operation. For example:
- Rebooting to periodically reset peripherals or clear resources.
- Restarting the firmware to implement update logic.
- Recovering from known error conditions gracefully.
In these cases, you can trigger NVIC_SystemReset() from the main application code when you want to restart the microcontroller. This performs a soft reboot without requiring actual power cycling.
Example Soft Reset on Timeout
Here is an example using NVIC_SystemReset() to reset the microcontroller after a timeout period for a periodic reset every 60 minutes:
uint32_t lastResetTime = 0;
void checkForReset() {
uint32_t now = getCurrentTime();
if (now - lastResetTime > 60*60*1000) {
lastResetTime = now;
NVIC_SystemReset();
}
}
This allows you to periodically reset peripherals and restart firmware without any external reboot signal. The system will reset itself every 60 minutes in this example.
Caveats of Using NVIC_SystemReset()
There are some limitations to keep in mind when using NVIC_SystemReset():
- Should not be called frequently or rapidly, allow time for reboot.
- May not reset deeper internal peripheral states.
- Does not clear SDRAM or flash memory.
- Does not reset backup domains that retain state.
So NVIC_SystemReset() is not a complete substitute for power-on reset in all cases. Check your device datasheet for details on what gets reset or retained after soft reset. Use an actual power cycle if you need to fully reset your board.
Summary
NVIC_SystemReset() provides an easy way to trigger a soft reset from firmware in Cortex-M0 microcontrollers. It resets the core processor and most peripherals while retaining clock and power configurations. This allows for recovery from crashes, restarting firmware, or periodic resets during normal operation.
Just call NVIC_SystemReset() wherever you want to trigger the reset in your code. Be aware of limitations compared to true power-on reset. With these caveats in mind, using NVIC_SystemReset() can simplify reset management and avoid external reset circuitry in many cases.
Frequently Asked Questions
Question: Does NVIC_SystemReset() reset flash memory?
Answer: No, NVIC_SystemReset() does not erase or reset flash memory. The contents of flash memory will remain intact through the soft reset.
Question: Does NVIC_SystemReset() reset analog peripherals like ADCs and DACs?
Answer: In most cases yes, analog peripherals like ADCs and DACs will be reset to their power-on defaults when NVIC_SystemReset() is called. However, check your specific chip datasheet to confirm.
Question: How long does NVIC_SystemReset() take to reset the system?
Answer: The soft reset is typically very fast, in the range of microseconds. The processor will restart and begin executing from the reset vector quickly. Total time to application restart depends on bootup initialization code.
Question: Can I force a hard reset instead with NVIC functions?
Answer: No, the NVIC peripheral only controls soft resets. To force a hard reset you will need external hardware like a reset supervisor circuit or MCU reset pin to cut power temporarily.
Question: Are there other microcontroller cores that support soft reset like NVIC_SystemReset()?
Answer: Yes, other ARM Cortex-M cores like Cortex-M3 and Cortex-M4 provide a similar SystemReset call in their NVIC peripheral. It provides consistent soft reset support across the Cortex-M family.
Conclusion
In summary, NVIC_SystemReset() is a simple but powerful function to trigger soft reset in Cortex-M0 microcontrollers. With an understanding of what gets reset vs retained, it can be used for system recovery, rebooting firmware, periodic resets, and other use cases requiring a controlled restart without full power cycle. Check your specific device for details and utilize NVIC_SystemReset() to simplify soft reset management in your Cortex-M0 projects.