simplelink: dpl: fix issues in ClockP

This commit fixes ClockP to use correct struct size, to set the correct
period and timeout in call to k_timer_start, and to deal with NULL
parameters in ClockP_construct.

Signed-off-by: Vincent Wan <vincent.wan@linaro.org>
This commit is contained in:
Vincent Wan 2019-10-25 14:43:59 -07:00 committed by Kumar Gala
parent d3b9d78ea3
commit e45a3f8ed2
2 changed files with 39 additions and 4 deletions

View File

@ -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;
}

View File

@ -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.