Skip to content

Commit

Permalink
ports/psoc6: Timer Module fix with tests.
Browse files Browse the repository at this point in the history
Signed-off-by: IFX-Anusha <Anusha.TR@infineon.com>
  • Loading branch information
IFX-Anusha committed Apr 9, 2024
1 parent 09a6c26 commit 67cb65b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 38 deletions.
29 changes: 24 additions & 5 deletions ports/psoc6/machine_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand Down
56 changes: 33 additions & 23 deletions tests/psoc6/timer.py
Original file line number Diff line number Diff line change
@@ -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())
18 changes: 8 additions & 10 deletions tests/psoc6/timer.py.exp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 67cb65b

Please sign in to comment.