diff --git a/docs/api/drivers/ResetReason.md b/docs/api/drivers/ResetReason.md new file mode 100644 index 0000000000..b12989a159 --- /dev/null +++ b/docs/api/drivers/ResetReason.md @@ -0,0 +1,43 @@ +## ResetReason + +When the system restarts, the system registers contain the reason for the restart at boot time in a platform specific manner. This API provides a generic method of fetching the reason for the restart. + +You can use the ResetReason interface to determine the cause of the last system reset in a portable fashion. + +### ResetReason class reference + +[![View code](https://www.mbed.com/embed/?type=library)](https://os.mbed.com/docs/mbed-os/development/mbed-os-api-doxy/classmbed_1_1_reset_reason.html) + +### ResetReason example + +Check the cause of the last system reset: + +```c++ +#include "mbed.h" +#include "ResetReason.h" + +#include + +std::string reset_reason_to_string(const reset_reason_t reason) +{ + switch (reason) { + case RESET_REASON_POWER_ON: + return "Power On"; + case RESET_REASON_PIN_RESET: + return "Hardware Pin"; + case RESET_REASON_SOFTWARE: + return "Software Reset"; + case RESET_REASON_WATCHDOG: + return "Watchdog"; + default: + return "Other Reason"; + } +} + +int main() +{ + const reset_reason_t reason = ResetReason::get(); + + printf("Last system reset reason: %s\r\n", reset_reason_to_string(reason).c_str()); +} +``` diff --git a/docs/api/drivers/Watchdog.md b/docs/api/drivers/Watchdog.md index 5ef986b875..602de5daef 100644 --- a/docs/api/drivers/Watchdog.md +++ b/docs/api/drivers/Watchdog.md @@ -1,11 +1,51 @@ # Watchdog -[Add description here.] +You can use the Watchdog interface to set up a hardware watchdog timer that resets the system in the case of system failures or malfunctions. -## Watchdog class reference +**Note:** There is only one instance in the system. Use `Watchdog::get_instance()` to obtain a reference. -[Add class reference here. Ask your editor for help if you've never done this before.] +If you fail to refresh the watchdog periodically, it resets the system after a set period of time. -## Watchdog example +**Note:** The maximum amount of time you can set as the Watchdog timeout varies depending on the target hardware. You can check the maximum value by calling `Watchdog::get_instance().get_max_timeout()`. -[Add example here.] +### Watchdog class reference + +[![View code](https://www.mbed.com/embed/?type=library)](https://os.mbed.com/docs/mbed-os/development/mbed-os-api-doxy/classmbed_1_1_watchdog.html) + +### Watchdog example + +This example creates a watchdog timer that expires after five seconds and that you can refresh by pushing BUTTON1 on the target board: + +```c++ +#include "mbed.h" + +const uint32_t TIMEOUT_MS = 5000; +InterruptIn button(BUTTON1); +volatile int countdown = 9; + +void trigger() +{ + Watchdog::get_instance().kick(); + countdown = 9; +} + +int main() +{ + printf("\r\nTarget started.\r\n"); + + Watchdog &watchdog = Watchdog::get_instance(); + watchdog.start(TIMEOUT_MS); + button.rise(&trigger); + + uint32_t watchdog_timeout = watchdog.get_timeout(); + printf("Watchdog initialized to %lu ms.\r\n", watchdog_timeout); + printf("Press BUTTON1 at least once every %lu ms to kick the " + "watchdog and prevent system reset.\r\n", watchdog_timeout); + + while (1) { + printf("\r%3i", countdown--); + fflush(stdout); + wait(TIMEOUT_MS / 1000.0 / 10); + } +} +```