diff --git a/simplelink/kernel/zephyr/dpl/ClockP_zephyr.c b/simplelink/kernel/zephyr/dpl/ClockP_zephyr.c index 7c78995..70218cf 100644 --- a/simplelink/kernel/zephyr/dpl/ClockP_zephyr.c +++ b/simplelink/kernel/zephyr/dpl/ClockP_zephyr.c @@ -24,6 +24,12 @@ typedef struct _ClockP_Obj { bool active; } ClockP_Obj; +static ClockP_Params ClockP_defaultParams = { + .startFlag = false, + .period = 0, + .arg = 0, +}; + static void expiry_fxn(struct k_timer *timer_id) { ClockP_Obj *obj = (ClockP_Obj *)k_timer_user_data_get(timer_id); @@ -43,6 +49,10 @@ ClockP_Handle ClockP_construct(ClockP_Struct *handle, ClockP_Fxn clockFxn, return NULL; } + if (params == NULL) { + params = &ClockP_defaultParams; + } + obj->clock_fxn = clockFxn; obj->arg = params->arg; obj->period = params->period * ClockP_getSystemTickPeriod() / 1000; @@ -99,11 +109,36 @@ void ClockP_setTimeout(ClockP_Handle handle, uint32_t timeout) void ClockP_start(ClockP_Handle handle) { ClockP_Obj *obj = (ClockP_Obj *)handle; + s32_t timeout; + s32_t period; - __ASSERT_NO_MSG(obj->timeout > (UINT32_MAX / 1000)); + __ASSERT_NO_MSG(obj->timeout / + CONFIG_SYS_CLOCK_TICKS_PER_SEC <= UINT32_MAX / 1000); + __ASSERT_NO_MSG(obj->period / + CONFIG_SYS_CLOCK_TICKS_PER_SEC <= UINT32_MAX / 1000); - k_timer_start(&obj->timer, obj->timeout * 1000 / - CONFIG_SYS_CLOCK_TICKS_PER_SEC, obj->period); + /* Avoid overflow */ + if (obj->timeout > UINT32_MAX / 1000) { + timeout = obj->timeout / CONFIG_SYS_CLOCK_TICKS_PER_SEC * 1000; + } else if ((obj->timeout != 0) && + (obj->timeout < CONFIG_SYS_CLOCK_TICKS_PER_SEC / 1000)) { + /* For small timeouts we use 1 msec */ + timeout = 1; + } else { + timeout = obj->timeout * 1000 / CONFIG_SYS_CLOCK_TICKS_PER_SEC; + } + + if (obj->period > UINT32_MAX / 1000) { + period = obj->period / CONFIG_SYS_CLOCK_TICKS_PER_SEC * 1000; + } else if ((obj->period != 0) && + (obj->period < CONFIG_SYS_CLOCK_TICKS_PER_SEC / 1000)) { + period = 1; + } else { + period = obj->period * 1000 / CONFIG_SYS_CLOCK_TICKS_PER_SEC; + } + + k_timer_start(&obj->timer, timeout, period); + obj->active = true; } diff --git a/simplelink/source/ti/drivers/dpl/ClockP.h b/simplelink/source/ti/drivers/dpl/ClockP.h index ec40002..d8fcc6e 100644 --- a/simplelink/source/ti/drivers/dpl/ClockP.h +++ b/simplelink/source/ti/drivers/dpl/ClockP.h @@ -82,7 +82,7 @@ typedef void (*ClockP_Fxn)(uintptr_t arg); */ #define ClockP_STRUCT_SIZE (sizeof(struct k_timer) + \ sizeof(ClockP_Fxn) + sizeof(uintptr_t) + \ - sizeof(uint32_t) * 2) + sizeof(uint32_t) * 2) + sizeof(bool) /*! * @brief ClockP structure.