From ea43a087117e9b0a69e5785842fdfa47e9aa4664 Mon Sep 17 00:00:00 2001 From: pennam Date: Fri, 21 Oct 2022 17:17:33 +0200 Subject: [PATCH 1/2] Add function to get reset reson and read/write RTC backup registers - Reset reason is readed from RTC backup register 8 and is written by bootloader. --- .../STM32H747_System/src/Portenta_System.h | 1 + .../STM32H747_System/src/STM32H747_System.cpp | 15 ++++++- .../STM32H747_System/src/STM32H747_System.h | 39 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/libraries/STM32H747_System/src/Portenta_System.h b/libraries/STM32H747_System/src/Portenta_System.h index 40dbb47cb..0c9b469e6 100644 --- a/libraries/STM32H747_System/src/Portenta_System.h +++ b/libraries/STM32H747_System/src/Portenta_System.h @@ -12,6 +12,7 @@ class Portenta_System: public STM32H747 Portenta_System() {}; virtual bool begin(); virtual bool enterLowPower(); + String getBoardRevision(); uint16_t getCarrierSpecs(); }; diff --git a/libraries/STM32H747_System/src/STM32H747_System.cpp b/libraries/STM32H747_System/src/STM32H747_System.cpp index 16ddeef8f..e54736b95 100644 --- a/libraries/STM32H747_System/src/STM32H747_System.cpp +++ b/libraries/STM32H747_System/src/STM32H747_System.cpp @@ -1,9 +1,10 @@ #include "STM32H747_System.h" #include "Wire.h" -#include "mbed.h" #define PMIC_ADDRESS 0x08 +extern RTC_HandleTypeDef RTCHandle; + uint8_t STM32H747::readReg(uint8_t subAddress) { char response = 0xFF; Wire1.beginTransmission(PMIC_ADDRESS); @@ -23,6 +24,18 @@ void STM32H747::setRegister(uint8_t reg, uint8_t val) { Wire1.endTransmission(); } +uint32_t STM32H747::readBackupRegister(RTCBackup reg) { + return HAL_RTCEx_BKUPRead(&RTCHandle, (uint32_t)reg); +} + +void STM32H747::writeBackupRegister(RTCBackup reg, uint32_t data) { + HAL_RTCEx_BKUPWrite(&RTCHandle, (uint32_t)reg, data); +} + +reset_reason_t STM32H747::getResetReason() { + return (reset_reason_t)readBackupRegister(RTCBackup::DR8); +} + /* * This function disables the external oscillators and use the HSI instead. * If lowspeed = true : f = 100MHz diff --git a/libraries/STM32H747_System/src/STM32H747_System.h b/libraries/STM32H747_System/src/STM32H747_System.h index 72076d644..b2056b47b 100644 --- a/libraries/STM32H747_System/src/STM32H747_System.h +++ b/libraries/STM32H747_System/src/STM32H747_System.h @@ -2,12 +2,51 @@ #define H747_System_h_ #include "Arduino.h" +#include "mbed.h" + +enum class RTCBackup { + DR0, /* RESERVED Arduino Magic */ + DR1, /* RESERVED OTA storage type */ + DR2, /* RESERVED OTA offset */ + DR3, /* RESERVED OTA update size */ + DR4, /* RESERVED MCUboot scratch storage type */ + DR5, /* RESERVED MCUboot scratch offset */ + DR6, /* RESERVED MCUboot scratch size */ + DR7, /* RESERVED MCUboot debug */ + DR8, /* RESERVED Reset reason */ + DR9, + DR10, + DR11, + DR12, + DR13, + DR14, + DR15, + DR16, + DR17, + DR18, + DR19, + DR20, + DR21, + DR22, + DR23, + DR24, + DR25, + DR26, + DR27, + DR28, + DR29, + DR30, + DR31, +}; class STM32H747 { public: virtual bool begin() = 0; virtual bool enterLowPower() = 0; + static reset_reason_t getResetReason(); + static uint32_t readBackupRegister(RTCBackup register); + static void writeBackupRegister(RTCBackup register, uint32_t data); protected: uint8_t readReg(uint8_t subAddress); From 4969b78b9348f6f2e390564f404dd93a890e01e4 Mon Sep 17 00:00:00 2001 From: pennam Date: Fri, 21 Oct 2022 17:20:36 +0200 Subject: [PATCH 2/2] Add example sketch to read reset reason --- .../STM32H747_getResetReason.ino | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 libraries/STM32H747_System/examples/STM32H747_getResetReason/STM32H747_getResetReason.ino diff --git a/libraries/STM32H747_System/examples/STM32H747_getResetReason/STM32H747_getResetReason.ino b/libraries/STM32H747_System/examples/STM32H747_getResetReason/STM32H747_getResetReason.ino new file mode 100644 index 000000000..05ea161d9 --- /dev/null +++ b/libraries/STM32H747_System/examples/STM32H747_getResetReason/STM32H747_getResetReason.ino @@ -0,0 +1,44 @@ +#include "STM32H747_System.h" + +void setup() { + Serial.begin(115200); + while (!Serial) {} + + reset_reason_t resetReason = STM32H747::getResetReason(); + Serial.println(getString(resetReason)); +} + +String getString(reset_reason_t val) { + switch (val){ + case RESET_REASON_POWER_ON: + return "Reset Reason Power ON"; + case RESET_REASON_PIN_RESET: + return "Reset Reason PIN Reset"; + case RESET_REASON_BROWN_OUT: + return "Reset Reason Brown Out"; + case RESET_REASON_SOFTWARE: + return "Reset Reason Software"; + case RESET_REASON_WATCHDOG: + return "Reset Reason Watchdog"; + case RESET_REASON_LOCKUP: + return "Reset Reason Lockup"; + case RESET_REASON_WAKE_LOW_POWER: + return "Reset Reason Wake Low Power"; + case RESET_REASON_ACCESS_ERROR: + return "Reset Reason Access Error"; + case RESET_REASON_BOOT_ERROR: + return "Reset Reason Boot Error"; + case RESET_REASON_MULTIPLE: + return "Reset Reason Multiple"; + case RESET_REASON_PLATFORM: + return "Reset Reason Platform"; + case RESET_REASON_UNKNOWN: + return "Reset Reason Unknown"; + default: + return "N/A"; + } +} + +void loop() { + delay(1000); +}