Skip to content

Commit

Permalink
ports/psoc6: Add system reset cause and clear.
Browse files Browse the repository at this point in the history
Signed-off-by: NikhitaR-IFX <[email protected]>
  • Loading branch information
NikhitaR-IFX committed Aug 9, 2024
1 parent 77f1a05 commit b8e5166
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 35 deletions.
37 changes: 35 additions & 2 deletions ports/psoc6/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,40 @@ void mpy_task(void *arg);

TaskHandle_t mpy_task_handle;

void wdt_test() {
cyhal_wdt_t wdt_obj;
printf("******************"
"HAL: Watchdog Timer"
"****************** \r\n\n");

/* Check the reason for device restart */
uint32_t reset_reason = cyhal_system_get_reset_reason();
printf("RESET_REASON: %ld\r\n", reset_reason);

/* Clears the reset cause register */
cyhal_system_clear_reset_reason();

/* Initialize WDT */
cy_rslt_t result = cyhal_wdt_init(&wdt_obj, 2000);

/* WDT initialization failed. Stop program execution */
if (result != CY_RSLT_SUCCESS) {
printf("WDT init failed \r\n");
}

cyhal_system_delay_ms(2200);

for (;;)
{
#if (1)
while (1) {
;
}
#endif
}

}

int main(int argc, char **argv) {
// Initialize the device and board peripherals
cy_rslt_t result = cybsp_init();
Expand Down Expand Up @@ -106,9 +140,8 @@ void mpy_task(void *arg) {
// Initialize modules. Or to be redone after a reset and therefore to be placed next to machine_init below ?
os_init();
time_init();

// wdt_test();
soft_reset:

mod_rtc_init();
mp_init();

Expand Down
23 changes: 5 additions & 18 deletions ports/psoc6/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ static uint32_t system_get_cpu_freq(void) {

void machine_init(void) {
mplogger_print("machine init\n");

// mp_obj_t reset = system_reset_cause();
// reset_cause = mp_obj_get_int(reset);
// TODO: put all module init functions here ?
// machine_pin_init(); ?
}

void machine_deinit(void) {
// we are doing a soft-reset so change the reset_cause
reset_cause = CYHAL_SYSTEM_RESET_SOFT;
reset_cause = CYHAL_SYSTEM_RESET_SOFT; // mpy_soft_reset = true;
mplogger_print("machine deinit\n");
mod_wdt_deinit();
mod_pin_deinit();
Expand Down Expand Up @@ -256,23 +257,9 @@ NORETURN static void mp_machine_reset(void) {
}
;
}

// This function is called from MPY side and is for addressing soft reset from micropython side. This does not indicate a system level soft reset.
static mp_int_t mp_machine_reset_cause(void) {
reset_cause = cyhal_system_get_reset_reason();
uint32_t ret_reset_cause = cyhal_system_get_reset_reason();
if (reset_cause == CYHAL_SYSTEM_RESET_SOFT) {
ret_reset_cause = MACHINE_SOFT_RESET;
} else if (reset_cause == 0UL) {
ret_reset_cause = MACHINE_HARD_RESET;
} else if (reset_cause == CYHAL_SYSTEM_RESET_WDT) {
ret_reset_cause = MACHINE_WDT_RESET;
} else if (reset_cause == CYHAL_SYSTEM_RESET_DEEPSLEEP_FAULT) {
ret_reset_cause = MACHINE_DEEPSLEEP_RESET;
} else {
ret_reset_cause = MACHINE_PWRON_RESET;
}
cyhal_system_clear_reset_reason();
return ret_reset_cause;
return MACHINE_SOFT_RESET;
}

// machine.disable_irq()
Expand Down
3 changes: 3 additions & 0 deletions ports/psoc6/modmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ should stay that way. */
void machine_init(void);
void machine_deinit(void);
#endif // MICROPY_INCLUDED_PSOC6_MODMACHINE_H

mp_obj_t system_reset_cause(void);
mp_obj_t clear_system_reset_cause(void);
60 changes: 57 additions & 3 deletions ports/psoc6/modpsoc6.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,72 @@

// port-specific includes
#include "modpsoc6.h"
#include "cyhal.h"
typedef enum
{
SYSTEM_RESET_NONE, /**< No cause */
SYSTEM_RESET_WDT, /**< A watchdog timer (WDT) reset has occurred */
SYSTEM_RESET_ACTIVE_FAULT, /**< The fault logging system requested a reset from its Active logic. */
SYSTEM_RESET_DEEPSLEEP_FAULT, /**< The fault logging system requested a reset from its Deep-Sleep logic. */
SYSTEM_RESET_SOFT, /**< The CPU requested a system reset through it's SYSRESETREQ. */
SYSTEM_RESET_HIB_WAKEUP, /**< A reset has occurred due to a a wakeup from hibernate power mode. */
SYSTEM_RESET_WCO_ERR, /**< A reset has occurred due to a watch-crystal clock error */
SYSTEM_RESET_SYS_CLK_ERR, /**< A reset has occurred due to a system clock error */
SYSTEM_RESET_PROTECTION, /**< A reset has occurred due to a protection violation */
SYSTEM_RESET_WARMBOOT, /**< A reset has occurred due wake up from DSRAM, which is a Warm Boot */
SYSTEM_RESET_MP_SOFT /**< A reset has occurred due to a soft reset from micropython side*/
} system_reset_reason_t;

mp_obj_t clear_system_reset_cause(void) {
cyhal_system_clear_reset_reason();
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(clear_system_reset_cause_obj, clear_system_reset_cause);


mp_obj_t system_reset_cause(void) {
uint32_t set_reset_cause = SYSTEM_RESET_NONE;

uint32_t reset_reason = cyhal_system_get_reset_reason();

if (reset_reason & CYHAL_SYSTEM_RESET_NONE) {
set_reset_cause = SYSTEM_RESET_NONE;
} else if (reset_reason & CYHAL_SYSTEM_RESET_WDT) {
set_reset_cause = SYSTEM_RESET_WDT;
} else if (reset_reason & CYHAL_SYSTEM_RESET_ACTIVE_FAULT) {
set_reset_cause = SYSTEM_RESET_ACTIVE_FAULT;
} else if (reset_reason & CYHAL_SYSTEM_RESET_DEEPSLEEP_FAULT) {
set_reset_cause = SYSTEM_RESET_DEEPSLEEP_FAULT;
} else if (reset_reason & CYHAL_SYSTEM_RESET_SOFT) {
set_reset_cause = SYSTEM_RESET_SOFT;
} else if (reset_reason & CYHAL_SYSTEM_RESET_HIB_WAKEUP) {
set_reset_cause = SYSTEM_RESET_HIB_WAKEUP;
} else if (reset_reason & CYHAL_SYSTEM_RESET_WCO_ERR) {
set_reset_cause = SYSTEM_RESET_WCO_ERR;
} else if (reset_reason & CYHAL_SYSTEM_RESET_SYS_CLK_ERR) {
set_reset_cause = SYSTEM_RESET_SYS_CLK_ERR;
} else if (reset_reason & CYHAL_SYSTEM_RESET_PROTECTION) {
set_reset_cause = SYSTEM_RESET_PROTECTION;
} else if (reset_reason & CYHAL_SYSTEM_RESET_WARMBOOT) {
set_reset_cause = SYSTEM_RESET_WARMBOOT;
}
cyhal_system_clear_reset_reason();
return MP_OBJ_NEW_SMALL_INT(set_reset_cause);
}
MP_DEFINE_CONST_FUN_OBJ_0(system_reset_cause_obj, system_reset_cause);

static const mp_rom_map_elem_t psoc6_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_psoc6) },
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_psoc6) },
#if MICROPY_ENABLE_EXT_QSPI_FLASH
{ MP_ROM_QSTR(MP_QSTR_QSPI_Flash), MP_ROM_PTR(&psoc6_qspi_flash_type) },
{ MP_ROM_QSTR(MP_QSTR_QSPI_Flash), MP_ROM_PTR(&psoc6_qspi_flash_type) },
#else
{ MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&psoc6_flash_type) },
{ MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&psoc6_flash_type) },
#endif
#if MICROPY_ENABLE_SD_CARD
{ MP_ROM_QSTR(MP_QSTR_SD_CARD), MP_ROM_PTR(&machine_sdcard_type) },
#endif
{ MP_ROM_QSTR(MP_QSTR_system_reset_cause), MP_ROM_PTR(&system_reset_cause_obj)},
{ MP_ROM_QSTR(MP_QSTR_clear_system_reset_cause), MP_ROM_PTR(&clear_system_reset_cause_obj)},
};
static MP_DEFINE_CONST_DICT(psoc6_module_globals, psoc6_module_globals_table);

Expand Down
13 changes: 5 additions & 8 deletions tests/ports/psoc6/test_scripts/wdt_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,11 @@ def wdt_reset_check():
import os


def wdt_reset_check():
print("\n***** Test 1: Check if WDT triggered reset *****\n")
print("True" if machine.reset_cause() == machine.WDT_RESET else " ")


device = sys.argv[1]
wdt = "ports/psoc6/wdt.py"
mpr_cmd = f"../tools/mpremote/mpremote.py connect {device} run {wdt} resume exec \"import machine; print('True' if machine.reset_cause()==machine.WDT_RESET else ' ')\""
mpr_cmd = f'../tools/mpremote/mpremote.py connect {device} run {wdt} resume exec "import psoc6; print(psoc6.system_reset_cause())"'
# mpr_cmd = f"../tools/mpremote/mpremote.py connect {device} run {wdt} resume exec \"import machine; print('True' if machine.reset_cause()==machine.WDT_RESET else ' ')\""

wdt_op_fp = "./ports/psoc6/test_scripts/wdt.py.out"
mpr_connect_cmd_out = "./ports/psoc6/test_scripts/connect.py.out"
exp_wdt = "./ports/psoc6/test_scripts/wdt.py.exp"
Expand Down Expand Up @@ -136,5 +133,5 @@ def wdt_test(validate=False):


wdt_test()
time.sleep(1)
wdt_test(True)
# time.sleep(1)
# wdt_test(True)
8 changes: 4 additions & 4 deletions tests/ports/psoc6/wdt.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@


import machine
import psoc6
import time

# invalid test cases
Expand All @@ -75,11 +76,10 @@

# valid test cases

print("Test 4: Rest cause check executing...")
print("Test 4: Reset cause check executing...")
try:
wdt = machine.WDT(id=0, timeout=500)
wdt = machine.WDT(id=0, timeout=1000)
except Exception:
print("WDT instance creation failed!")

time.sleep_ms(500)
print("Sleep over")
time.sleep_ms(1200)

0 comments on commit b8e5166

Please sign in to comment.