Implement reset_reason for raspberrypi port

This commit is contained in:
Robert Pafford 2021-09-08 18:34:34 -04:00
parent cfc4ed19aa
commit 65f8804816
No known key found for this signature in database
GPG Key ID: FE32F6181AF1C59E
3 changed files with 41 additions and 1 deletions

View File

@ -34,6 +34,11 @@
#include "src/rp2_common/hardware_adc/include/hardware/adc.h"
#include "src/rp2_common/hardware_clocks/include/hardware/clocks.h"
#include "src/rp2040/hardware_regs/include/hardware/regs/vreg_and_chip_reset.h"
#include "src/rp2040/hardware_regs/include/hardware/regs/watchdog.h"
#include "src/rp2040/hardware_structs/include/hardware/structs/vreg_and_chip_reset.h"
#include "src/rp2040/hardware_structs/include/hardware/structs/watchdog.h"
float common_hal_mcu_processor_get_temperature(void) {
adc_init();
adc_set_temp_sensor_enabled(true);
@ -60,5 +65,34 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
}
mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
return RESET_REASON_UNKNOWN;
mcu_reset_reason_t reason = RESET_REASON_UNKNOWN;
uint32_t watchdog_reset_reg = watchdog_hw->reason;
uint32_t chip_reset_reg = vreg_and_chip_reset_hw->chip_reset;
if (chip_reset_reg & VREG_AND_CHIP_RESET_CHIP_RESET_HAD_PSM_RESTART_BITS) {
reason = RESET_REASON_RESCUE_DEBUG;
}
if (chip_reset_reg & VREG_AND_CHIP_RESET_CHIP_RESET_HAD_RUN_BITS) {
reason = RESET_REASON_RESET_PIN;
}
if (chip_reset_reg & VREG_AND_CHIP_RESET_CHIP_RESET_HAD_POR_BITS) {
// NOTE: This register is also used for brownout, but there is no way to differentiate between power on and brown out
reason = RESET_REASON_POWER_ON;
}
// Check watchdog after chip reset since watchdog doesn't clear chip_reset, while chip_reset clears the watchdog
if (watchdog_reset_reg & WATCHDOG_REASON_TIMER_BITS) {
// This bit can also be set during a software reset because the pico-sdk performs a software reset by setting an extremely low timeout on the watchdog, rather than triggering a watchdog reset manually
reason = RESET_REASON_WATCHDOG;
}
if (watchdog_reset_reg & WATCHDOG_REASON_FORCE_BITS) {
reason = RESET_REASON_SOFTWARE;
}
return reason;
}

View File

@ -36,6 +36,7 @@ MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, DEEP_SLEEP_ALARM, RESET_REA
MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, RESET_PIN, RESET_REASON_RESET_PIN);
MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, WATCHDOG, RESET_REASON_WATCHDOG);
MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, UNKNOWN, RESET_REASON_UNKNOWN);
MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, RESCUE_DEBUG, RESET_REASON_RESCUE_DEBUG);
//| class ResetReason:
//| """The reason the microntroller was last reset"""
@ -61,6 +62,9 @@ MAKE_ENUM_VALUE(mcu_reset_reason_type, reset_reason, UNKNOWN, RESET_REASON_UNKNO
//| UNKNOWN: object
//| """The microntroller restarted for an unknown reason."""
//|
//| RESCUE_DEBUG: object
//| """The microntroller was reset by the rescue debug port."""
//|
MAKE_ENUM_MAP(mcu_reset_reason) {
MAKE_ENUM_MAP_ENTRY(reset_reason, POWER_ON),
MAKE_ENUM_MAP_ENTRY(reset_reason, BROWNOUT),
@ -69,6 +73,7 @@ MAKE_ENUM_MAP(mcu_reset_reason) {
MAKE_ENUM_MAP_ENTRY(reset_reason, RESET_PIN),
MAKE_ENUM_MAP_ENTRY(reset_reason, WATCHDOG),
MAKE_ENUM_MAP_ENTRY(reset_reason, UNKNOWN),
MAKE_ENUM_MAP_ENTRY(reset_reason, RESCUE_DEBUG),
};
STATIC MP_DEFINE_CONST_DICT(mcu_reset_reason_locals_dict, mcu_reset_reason_locals_table);

View File

@ -38,6 +38,7 @@ typedef enum {
RESET_REASON_RESET_PIN,
RESET_REASON_WATCHDOG,
RESET_REASON_UNKNOWN,
RESET_REASON_RESCUE_DEBUG,
} mcu_reset_reason_t;
extern const mp_obj_type_t mcu_reset_reason_type;