|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Texas Instruments Incorporated |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/sys/__assert.h> |
| 9 | +#include <zephyr/sys_clock.h> |
| 10 | +#include <ti/drivers/dpl/ClockP.h> |
| 11 | + |
| 12 | +/* |
| 13 | + * ClockP_STRUCT_SIZE in ClockP.h must be updated to match the size of this |
| 14 | + * struct |
| 15 | + */ |
| 16 | +typedef struct _ClockP_Obj |
| 17 | +{ |
| 18 | + struct k_timer timer; |
| 19 | + ClockP_Fxn clock_fxn; |
| 20 | + uintptr_t arg; |
| 21 | + uint32_t timeout; |
| 22 | + uint32_t period; |
| 23 | + bool active; |
| 24 | +} ClockP_Obj; |
| 25 | + |
| 26 | +static ClockP_Params ClockP_defaultParams = { |
| 27 | + .startFlag = false, |
| 28 | + .period = 0, |
| 29 | + .arg = 0, |
| 30 | +}; |
| 31 | + |
| 32 | +static void expiry_fxn(struct k_timer *timer_id) |
| 33 | +{ |
| 34 | + ClockP_Obj *obj = (ClockP_Obj *)k_timer_user_data_get(timer_id); |
| 35 | + |
| 36 | + obj->clock_fxn(obj->arg); |
| 37 | +} |
| 38 | + |
| 39 | +/* |
| 40 | + * ======== ClockP_construct ======== |
| 41 | + */ |
| 42 | +ClockP_Handle ClockP_construct(ClockP_Struct *handle, ClockP_Fxn clockFxn, uint32_t timeout, ClockP_Params *params) |
| 43 | +{ |
| 44 | + ClockP_Obj *obj = (ClockP_Obj *)handle; |
| 45 | + |
| 46 | + if (handle == NULL) |
| 47 | + { |
| 48 | + return NULL; |
| 49 | + } |
| 50 | + |
| 51 | + if (params == NULL) |
| 52 | + { |
| 53 | + params = &ClockP_defaultParams; |
| 54 | + } |
| 55 | + |
| 56 | + obj->clock_fxn = clockFxn; |
| 57 | + obj->arg = params->arg; |
| 58 | + obj->period = params->period * ClockP_getSystemTickPeriod() / USEC_PER_MSEC; |
| 59 | + obj->timeout = timeout; |
| 60 | + obj->active = false; |
| 61 | + |
| 62 | + k_timer_init(&obj->timer, expiry_fxn, NULL); |
| 63 | + k_timer_user_data_set(&obj->timer, obj); |
| 64 | + |
| 65 | + if (params->startFlag) |
| 66 | + { |
| 67 | + ClockP_start(obj); |
| 68 | + } |
| 69 | + |
| 70 | + return ((ClockP_Handle)handle); |
| 71 | +} |
| 72 | + |
| 73 | +/* |
| 74 | + * ======== ClockP_getSystemTickPeriod ======== |
| 75 | + */ |
| 76 | +uint32_t ClockP_tickPeriod = (USEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC); |
| 77 | +uint32_t ClockP_getSystemTickPeriod() |
| 78 | +{ |
| 79 | + return ClockP_tickPeriod; |
| 80 | +} |
| 81 | + |
| 82 | +uint32_t ClockP_getSystemTicks() |
| 83 | +{ |
| 84 | + return (uint32_t)k_ms_to_ticks_ceil32(k_uptime_get_32()); |
| 85 | +} |
| 86 | + |
| 87 | +/* |
| 88 | + * ======== ClockP_Params_init ======== |
| 89 | + */ |
| 90 | +void ClockP_Params_init(ClockP_Params *params) |
| 91 | +{ |
| 92 | + params->arg = 0; |
| 93 | + params->startFlag = false; |
| 94 | + params->period = 0; |
| 95 | +} |
| 96 | + |
| 97 | +/* |
| 98 | + * ======== ClockP_setTimeout ======== |
| 99 | + */ |
| 100 | +void ClockP_setTimeout(ClockP_Handle handle, uint32_t timeout) |
| 101 | +{ |
| 102 | + ClockP_Obj *obj = (ClockP_Obj *)handle; |
| 103 | + |
| 104 | + obj->timeout = timeout; |
| 105 | +} |
| 106 | + |
| 107 | +/* |
| 108 | + * ======== ClockP_start ======== |
| 109 | + */ |
| 110 | +void ClockP_start(ClockP_Handle handle) |
| 111 | +{ |
| 112 | + ClockP_Obj *obj = (ClockP_Obj *)handle; |
| 113 | + int32_t timeout; |
| 114 | + int32_t period; |
| 115 | + |
| 116 | + __ASSERT_NO_MSG(obj->timeout / CONFIG_SYS_CLOCK_TICKS_PER_SEC <= UINT32_MAX / USEC_PER_MSEC); |
| 117 | + __ASSERT_NO_MSG(obj->period / CONFIG_SYS_CLOCK_TICKS_PER_SEC <= UINT32_MAX / USEC_PER_MSEC); |
| 118 | + |
| 119 | + /* Avoid overflow */ |
| 120 | + if (obj->timeout > UINT32_MAX / USEC_PER_MSEC) |
| 121 | + { |
| 122 | + timeout = obj->timeout / CONFIG_SYS_CLOCK_TICKS_PER_SEC * USEC_PER_MSEC; |
| 123 | + } |
| 124 | + else if ((obj->timeout != 0) && (obj->timeout < CONFIG_SYS_CLOCK_TICKS_PER_SEC / USEC_PER_MSEC)) |
| 125 | + { |
| 126 | + /* For small timeouts we use 1 msec */ |
| 127 | + timeout = 1; |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + timeout = obj->timeout * USEC_PER_MSEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC; |
| 132 | + } |
| 133 | + |
| 134 | + if (obj->period > UINT32_MAX / USEC_PER_MSEC) |
| 135 | + { |
| 136 | + period = obj->period / CONFIG_SYS_CLOCK_TICKS_PER_SEC * USEC_PER_MSEC; |
| 137 | + } |
| 138 | + else if ((obj->period != 0) && (obj->period < CONFIG_SYS_CLOCK_TICKS_PER_SEC / USEC_PER_MSEC)) |
| 139 | + { |
| 140 | + period = 1; |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + period = obj->period * USEC_PER_MSEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC; |
| 145 | + } |
| 146 | + |
| 147 | + k_timer_start(&obj->timer, K_MSEC(timeout), K_MSEC(period)); |
| 148 | + |
| 149 | + obj->active = true; |
| 150 | +} |
| 151 | + |
| 152 | +/* |
| 153 | + * ======== ClockP_stop ======== |
| 154 | + */ |
| 155 | +void ClockP_stop(ClockP_Handle handle) |
| 156 | +{ |
| 157 | + ClockP_Obj *obj = (ClockP_Obj *)handle; |
| 158 | + |
| 159 | + k_timer_stop(&obj->timer); |
| 160 | + obj->active = false; |
| 161 | +} |
| 162 | + |
| 163 | +/* |
| 164 | + * ======== ClockP_usleep ======== |
| 165 | + */ |
| 166 | +void ClockP_usleep(uint32_t usec) |
| 167 | +{ |
| 168 | + k_sleep(K_USEC(usec)); |
| 169 | +} |
| 170 | + |
| 171 | +/* |
| 172 | + * ======== ClockP_getTimeout ======== |
| 173 | + */ |
| 174 | +uint32_t ClockP_getTimeout(ClockP_Handle handle) |
| 175 | +{ |
| 176 | + ClockP_Obj *obj = (ClockP_Obj *)handle; |
| 177 | + return k_timer_remaining_get(&obj->timer) * CONFIG_SYS_CLOCK_TICKS_PER_SEC / USEC_PER_MSEC; |
| 178 | +} |
| 179 | + |
| 180 | +/* |
| 181 | + * ======== ClockP_isActive ======== |
| 182 | + */ |
| 183 | +bool ClockP_isActive(ClockP_Handle handle) |
| 184 | +{ |
| 185 | + ClockP_Obj *obj = (ClockP_Obj *)handle; |
| 186 | + return obj->active; |
| 187 | +} |
| 188 | + |
| 189 | +void ClockP_destruct(ClockP_Struct *clockP) |
| 190 | +{ |
| 191 | + ClockP_Obj *obj = (ClockP_Obj *)clockP->data; |
| 192 | + |
| 193 | + obj->clock_fxn = NULL; |
| 194 | + obj->arg = 0; |
| 195 | + obj->period = 0; |
| 196 | + obj->timeout = 0; |
| 197 | + obj->active = false; |
| 198 | + |
| 199 | + k_timer_stop(&obj->timer); |
| 200 | +} |
0 commit comments