From fc726574650a348bf2248d1938dfcaefe38b5fe0 Mon Sep 17 00:00:00 2001
From: pennam <m.pennasilico@arduino.cc>
Date: Mon, 14 Nov 2022 17:28:43 +0100
Subject: [PATCH] Store reset reason in RTC backup register 8

---
 app/main.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/app/main.cpp b/app/main.cpp
index dcc0f0c..4b3e43a 100644
--- a/app/main.cpp
+++ b/app/main.cpp
@@ -224,9 +224,11 @@ int main(void) {
   BOOT_LOG_INF("Starting Arduino bootloader");
 
   int magic = RTCGetBKPRegister(RTC_BKP_DR0);
+  int reset = ResetReason::get();
+  RTCSetBKPRegister(RTC_BKP_DR8, reset);
 
   // in case we have been reset let's wait 500 ms to see if user is trying to stay in bootloader
-  if (ResetReason::get() == RESET_REASON_PIN_RESET) {
+  if (reset == RESET_REASON_PIN_RESET) {
     // now that we've been reset let's set magic. resetting with this set will
     // flag we need to stay in bootloader.
     RTCSetBKPRegister(RTC_BKP_DR0, 0xDF59);