From d7b19977f2b42ebf21d9604fafa31e97702f06ff Mon Sep 17 00:00:00 2001 From: Ramya Subramanyam Date: Mon, 22 Apr 2024 14:37:40 +0530 Subject: [PATCH] ports/psoc6: Implementation of machine WDT APIs. Signed-off-by: Ramya Subramanyam --- ports/psoc6/Makefile | 1 + ports/psoc6/machine_wdt.c | 93 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 ports/psoc6/machine_wdt.c diff --git a/ports/psoc6/Makefile b/ports/psoc6/Makefile index 2fc87c61a865f..1ad143d2386f6 100644 --- a/ports/psoc6/Makefile +++ b/ports/psoc6/Makefile @@ -152,6 +152,7 @@ MOD_SRC_C += \ machine_pin_phy.c \ machine_pin.c \ machine_rtc.c \ + machine_wdt.c \ machine_spi.c \ machine_timer.c \ machine_adc.c \ diff --git a/ports/psoc6/machine_wdt.c b/ports/psoc6/machine_wdt.c new file mode 100644 index 0000000000000..3c2aab94fb5b6 --- /dev/null +++ b/ports/psoc6/machine_wdt.c @@ -0,0 +1,93 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 "Krzysztof Adamski" + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// std includes +#include +#include +#include +#include + + +// micropython includes +#include "py/nlr.h" +#include "py/obj.h" +#include "py/runtime.h" +#include "py/mphal.h" +#include "py/mperrno.h" +#include "shared/timeutils/timeutils.h" +#include "py/misc.h" + + +// MTB includes +#include "cyhal.h" + + +// port-specific includes +#include "modmachine.h" + +cyhal_wdt_t psoc6_wdt; + + +typedef struct _machine_wdt_obj_t { + mp_obj_base_t base; +} machine_wdt_obj_t; + + +// singleton WDT object +static const machine_wdt_obj_t machine_wdt = {{&machine_wdt_type}}; + +/* This function is run from main.c to init the WDT at boot time. This will set the timeout of the WDT to 2ms */ +void wdt_init(void) { + uint32_t timeout_ms = 2000; + cy_rslt_t result = cyhal_wdt_init(&psoc6_wdt, timeout_ms); + + if (CY_RSLT_SUCCESS != result) { + mp_raise_ValueError(MP_ERROR_TEXT("cyhal_wdt_init failed !")); + } +} + +static machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms) { + if (id != 0) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("WDT(%d) doesn't exist"), id); + } + + if (timeout_ms <= 0) { + mp_raise_ValueError(MP_ERROR_TEXT("WDT timeout too low")); + } else if (timeout_ms > cyhal_wdt_get_max_timeout_ms()) { + mp_raise_ValueError(MP_ERROR_TEXT("WDT timeout too high")); + } + + cy_rslt_t result = cyhal_wdt_init(&psoc6_wdt, timeout_ms); + if (CY_RSLT_SUCCESS != result) { + mp_raise_ValueError(MP_ERROR_TEXT("cyhal_wdt_init failed !")); + } + + return (machine_wdt_obj_t *)&machine_wdt; +} + +static void mp_machine_wdt_feed(machine_wdt_obj_t *self) { + cyhal_wdt_kick(&self); +}