From 67cb65ba04aa59e6efd9a205a6bfa6a924229835 Mon Sep 17 00:00:00 2001 From: IFX-Anusha Date: Mon, 8 Apr 2024 18:10:47 +0530 Subject: [PATCH] ports/psoc6: Timer Module fix with tests. Signed-off-by: IFX-Anusha --- ports/psoc6/machine_timer.c | 29 +++++++++++++++---- tests/psoc6/timer.py | 56 ++++++++++++++++++++++--------------- tests/psoc6/timer.py.exp | 18 ++++++------ 3 files changed, 65 insertions(+), 38 deletions(-) diff --git a/ports/psoc6/machine_timer.c b/ports/psoc6/machine_timer.c index 8586838f9d893..399cdaf825285 100644 --- a/ports/psoc6/machine_timer.c +++ b/ports/psoc6/machine_timer.c @@ -40,27 +40,46 @@ static mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = TIMER_MODE_ONE_SHOT} }, { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 9999u} }, - { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 10000u} }, + { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_rom_obj = MP_ROM_NONE} }, }; // Parse args mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + float period; self->mode = args[ARG_mode].u_int; - self->freq = args[ARG_freq].u_int; - self->period = args[ARG_period].u_int; + + if (args[ARG_freq].u_obj != mp_const_none) { + self->freq = args[ARG_freq].u_int; + period = 1.0f/(float)(args[ARG_freq].u_int); + } + else { + self->period = args[ARG_period].u_int; + period = (float)args[ARG_period].u_int/1000.0f ; + } if (args[ARG_callback].u_obj != mp_const_none) { self->callback = args[ARG_callback].u_obj; } + + uint32_t period_hal; + uint32_t fz_hal = 1000000; + period_hal = (uint32_t)(period * fz_hal) - 1; + +// Adjust fz_hal if necessary + while (period_hal > 4294967296) { + fz_hal = fz_hal / 10; // Reduce the fz_hal value by 10% + period_hal = (uint32_t)(period * fz_hal) - 1; // Recalculate period_hal +} + // Timer initialisation of port cy_rslt_t rslt; const cyhal_timer_cfg_t timer_cfg = { .compare_value = 0, /* Timer compare value, not used */ - .period = self->period, /* Defines the timer period */ + .period = period_hal, /* Defines the timer period */ .direction = CYHAL_TIMER_DIR_UP, /* Timer counts up */ .is_compare = false, /* Don't use compare mode */ .is_continuous = self->mode, /* Run the timer */ @@ -77,7 +96,7 @@ static mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar rslt = cyhal_timer_configure(&self->timer_obj, &timer_cfg); /* Set the frequency of timer to Defined frequency */ - rslt = cyhal_timer_set_frequency(&self->timer_obj, self->freq); + rslt = cyhal_timer_set_frequency(&self->timer_obj,fz_hal); /* Assign the ISR to execute on timer interrupt */ cyhal_timer_register_callback(&self->timer_obj, isr_timer, self); diff --git a/tests/psoc6/timer.py b/tests/psoc6/timer.py index 54c736ec65855..3e3b31c725a0f 100644 --- a/tests/psoc6/timer.py +++ b/tests/psoc6/timer.py @@ -1,27 +1,37 @@ -import time from machine import Timer -import os +import uasyncio as asyncio +interrupt_triggered = False # Flag to indicate interrupt triggered -# machine = os.uname().machine -# if "CY8CPROTO-063-BLE" in machine: -# TODO: Not working correctly. Neither the timer timing is correct. -# TODO: Review test and module. -print("SKIP") -raise SystemExit +def call(timer): + global interrupt_triggered + interrupt_triggered = True -t = Timer(0) -t.init(period=2000, mode=Timer.ONE_SHOT, callback=lambda t: print("Oneshot Timer")) -time.sleep(30) -t.deinit() +async def print_message_thread(): + global interrupt_triggered + while True: + if interrupt_triggered: + print("Interrupt triggered") + interrupt_triggered = False # Reset the flag + await asyncio.sleep(0.1) # Use asyncio.sleep instead of time.sleep - -def blocking_delay_ms(delay_ms): - start = time.ticks_ms() - while time.ticks_diff(time.ticks_ms(), start) < delay_ms: - pass - - -t1 = Timer(0) -t1.init(period=2000, mode=Timer.PERIODIC, callback=lambda t: print("Periodic Timer")) -blocking_delay_ms(2000) -t.deinit() +async def main(): + print("Oneshot Timer Execution") + tim = Timer(0, period=1000, mode=Timer.ONE_SHOT, callback=call) + try: + logger_task = asyncio.create_task(print_message_thread()) + await asyncio.sleep(5) # Wait for 5 seconds + logger_task.cancel() # Cancel the logger task + finally: + tim.deinit() # Deinitialize the timer + print("Periodic Timer Execution") + tim = Timer(0, period=1000, mode=Timer.PERIODIC, callback=call) + try: + logger_task = asyncio.create_task(print_message_thread()) + await asyncio.sleep(5) # Wait for 5 seconds + logger_task.cancel() # Cancel the logger task + finally: + tim.deinit() # Deinitialize the timer + +if __name__ == "__main__": + print("Timer Test") + asyncio.run(main()) \ No newline at end of file diff --git a/tests/psoc6/timer.py.exp b/tests/psoc6/timer.py.exp index a23bb245d6f33..e677f41f5238e 100644 --- a/tests/psoc6/timer.py.exp +++ b/tests/psoc6/timer.py.exp @@ -1,10 +1,8 @@ -Oneshot Timer -Periodic Timer -Periodic Timer -Periodic Timer -Periodic Timer -Periodic Timer -Periodic Timer -Periodic Timer -Periodic Timer -Periodic Timer +Timer Test +Oneshot Timer Execution +Interrupt triggered +Periodic Timer Execution +Interrupt triggered +Interrupt triggered +Interrupt triggered +Interrupt triggered \ No newline at end of file