Skip to content

Commit

Permalink
kernel/sys_clock.h: Deprecate and convert uses of old conversions
Browse files Browse the repository at this point in the history
Mark the old time conversion APIs deprecated, leave compatibility
macros in place, and replace all usage with the new API.

Signed-off-by: Andy Ross <[email protected]>
  • Loading branch information
Andy Ross authored and carlescufi committed Nov 8, 2019
1 parent f2b75fd commit 8892406
Show file tree
Hide file tree
Showing 53 changed files with 122 additions and 114 deletions.
2 changes: 1 addition & 1 deletion arch/arc/core/timestamp.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ u64_t z_tsc_read(void)
t = (u64_t)z_tick_get();
count = z_arc_v2_aux_reg_read(_ARC_V2_TMR0_COUNT);
irq_unlock(key);
t *= (u64_t)sys_clock_hw_cycles_per_tick();
t *= k_ticks_to_cyc_floor64(1);
t += (u64_t)count;
return t;
}
2 changes: 1 addition & 1 deletion doc/reference/kernel/timing/clocks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ between two points in time.
/* compute how long the work took (assumes no counter rollover) */
cycles_spent = stop_time - start_time;
nanoseconds_spent = SYS_CLOCK_HW_CYCLES_TO_NS(cycles_spent);
nanoseconds_spent = (u32_t)k_cyc_to_ns_floor64(cycles_spent);
Suggested Uses
**************
Expand Down
8 changes: 4 additions & 4 deletions drivers/timer/altera_avalon_timer_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void timer_irq_handler(void *unused)
read_timer_start_of_tick_handler();
#endif

accumulated_cycle_count += sys_clock_hw_cycles_per_tick();
accumulated_cycle_count += k_ticks_to_cyc_floor32(1);

/* Clear the interrupt */
alt_handle_irq((void *)TIMER_0_BASE, TIMER_0_IRQ);
Expand All @@ -46,15 +46,15 @@ int z_clock_driver_init(struct device *device)
ARG_UNUSED(device);

IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_0_BASE,
sys_clock_hw_cycles_per_tick() & 0xFFFF);
k_ticks_to_cyc_floor32(1) & 0xFFFF);
IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_0_BASE,
(sys_clock_hw_cycles_per_tick() >> 16) & 0xFFFF);
(k_ticks_to_cyc_floor32(1) >> 16) & 0xFFFF);

IRQ_CONNECT(TIMER_0_IRQ, 0, timer_irq_handler, NULL, 0);
irq_enable(TIMER_0_IRQ);

alt_avalon_timer_sc_init((void *)TIMER_0_BASE, 0,
TIMER_0_IRQ, sys_clock_hw_cycles_per_tick());
TIMER_0_IRQ, k_ticks_to_cyc_floor32(1));

return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions drivers/timer/litex_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static void litex_timer_irq_handler(void *device)
int key = irq_lock();

sys_write8(TIMER_EV, TIMER_EV_PENDING_ADDR);
accumulated_cycle_count += sys_clock_hw_cycles_per_tick();
accumulated_cycle_count += k_ticks_to_cyc_floor32(1);
z_clock_announce(1);

irq_unlock(key);
Expand All @@ -57,9 +57,9 @@ int z_clock_driver_init(struct device *device)
sys_write8(TIMER_DISABLE, TIMER_EN_ADDR);

for (int i = 0; i < 4; i++) {
sys_write8(sys_clock_hw_cycles_per_tick() >> (24 - i * 8),
sys_write8(k_ticks_to_cyc_floor32(1) >> (24 - i * 8),
TIMER_RELOAD_ADDR + i * 0x4);
sys_write8(sys_clock_hw_cycles_per_tick() >> (24 - i * 8),
sys_write8(k_ticks_to_cyc_floor32(1) >> (24 - i * 8),
TIMER_LOAD_ADDR + i * 0x4);
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/timer/loapic_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ int z_clock_driver_init(struct device *device)
/* determine the timer counter value (in timer clock cycles/system tick)
*/

cycles_per_tick = sys_clock_hw_cycles_per_tick();
cycles_per_tick = k_ticks_to_cyc_floor32(1);

tickless_idle_init();

Expand Down
2 changes: 1 addition & 1 deletion drivers/timer/xlnx_psttc_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void _timer_int_handler(void *unused)
u32_t regval;

regval = sys_read32(TIMER_BASEADDR + XTTCPS_ISR_OFFSET);
accumulated_cycles += sys_clock_hw_cycles_per_tick();
accumulated_cycles += k_ticks_to_cyc_floor32(1);
z_clock_announce(_sys_idle_elapsed_ticks);
}

Expand Down
4 changes: 2 additions & 2 deletions include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,7 @@ __syscall u32_t k_timer_remaining_get(struct k_timer *timer);
static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
{
const s32_t ticks = z_timeout_remaining(&timer->timeout);
return (ticks > 0) ? (u32_t)__ticks_to_ms(ticks) : 0U;
return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U;
}

/**
Expand Down Expand Up @@ -3077,7 +3077,7 @@ static inline int k_delayed_work_submit(struct k_delayed_work *work,
*/
static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
{
return __ticks_to_ms(z_timeout_remaining(&work->timeout));
return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout));
}

/**
Expand Down
23 changes: 15 additions & 8 deletions include/sys_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,20 @@ extern void z_enable_sys_clock(void);

#endif

#define __ticks_to_ms(t) k_ticks_to_ms_floor64(t)
#define z_ms_to_ticks(t) k_ms_to_ticks_ceil32(t)
#define __ticks_to_us(t) k_ticks_to_us_floor64(t)
#define z_us_to_ticks(t) k_us_to_ticks_ceil64(t)
#define sys_clock_hw_cycles_per_tick() k_ticks_to_cyc_floor32(1)
#define SYS_CLOCK_HW_CYCLES_TO_NS64(t) (1000 * k_cyc_to_us_floor64(t))
#define SYS_CLOCK_HW_CYCLES_TO_NS(t) ((u32_t)(1000 * k_cyc_to_us_floor64(t)))
#define __ticks_to_ms(t) __DEPRECATED_MACRO \
k_ticks_to_ms_floor64(t)
#define z_ms_to_ticks(t) \
k_ms_to_ticks_ceil32(t)
#define __ticks_to_us(t) __DEPRECATED_MACRO \
k_ticks_to_us_floor64(t)
#define z_us_to_ticks(t) __DEPRECATED_MACRO \
k_us_to_ticks_ceil64(t)
#define sys_clock_hw_cycles_per_tick() __DEPRECATED_MACRO \
k_ticks_to_cyc_floor32(1)
#define SYS_CLOCK_HW_CYCLES_TO_NS64(t) __DEPRECATED_MACRO \
k_cyc_to_ns_floor64(t)
#define SYS_CLOCK_HW_CYCLES_TO_NS(t) __DEPRECATED_MACRO \
((u32_t)k_cyc_to_ns_floor64(t))

/* added tick needed to account for tick in progress */
#define _TICK_ALIGN 1
Expand All @@ -88,7 +95,7 @@ extern void z_enable_sys_clock(void);
* and calculates the average cycle time
*/
#define SYS_CLOCK_HW_CYCLES_TO_NS_AVG(X, NCYCLES) \
(u32_t)(SYS_CLOCK_HW_CYCLES_TO_NS64(X) / NCYCLES)
(u32_t)(k_cyc_to_ns_floor64(X) / NCYCLES)

/**
* @defgroup clock_apis Kernel Clock APIs
Expand Down
2 changes: 1 addition & 1 deletion kernel/poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ int k_work_poll_submit_to_queue(struct k_work_q *work_q,
if (timeout != K_FOREVER) {
z_add_timeout(&work->timeout,
triggered_work_expiration_handler,
z_ms_to_ticks(timeout));
k_ms_to_ticks_ceil32(timeout));
}

/* From now, any event will result in submitted work. */
Expand Down
12 changes: 6 additions & 6 deletions kernel/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void k_sched_time_slice_set(s32_t slice, int prio)
{
LOCKED(&sched_spinlock) {
_current_cpu->slice_ticks = 0;
slice_time = z_ms_to_ticks(slice);
slice_time = k_ms_to_ticks_ceil32(slice);
slice_max_prio = prio;
z_reset_time_slice();
}
Expand Down Expand Up @@ -368,7 +368,7 @@ static void pend(struct k_thread *thread, _wait_q_t *wait_q, s32_t timeout)
}

if (timeout != K_FOREVER) {
s32_t ticks = _TICK_ALIGN + z_ms_to_ticks(timeout);
s32_t ticks = _TICK_ALIGN + k_ms_to_ticks_ceil32(timeout);

z_add_thread_timeout(thread, ticks);
}
Expand Down Expand Up @@ -975,9 +975,9 @@ s32_t z_impl_k_sleep(int ms)
{
s32_t ticks;

ticks = z_ms_to_ticks(ms);
ticks = k_ms_to_ticks_ceil32(ms);
ticks = z_tick_sleep(ticks);
return __ticks_to_ms(ticks);
return k_ticks_to_ms_floor64(ticks);
}

#ifdef CONFIG_USERSPACE
Expand All @@ -992,9 +992,9 @@ s32_t z_impl_k_usleep(int us)
{
s32_t ticks;

ticks = z_us_to_ticks(us);
ticks = k_us_to_ticks_ceil64(us);
ticks = z_tick_sleep(ticks);
return __ticks_to_us(ticks);
return k_ticks_to_us_floor64(ticks);
}

#ifdef CONFIG_USERSPACE
Expand Down
2 changes: 1 addition & 1 deletion kernel/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ static void schedule_new_thread(struct k_thread *thread, s32_t delay)
if (delay == 0) {
k_thread_start(thread);
} else {
s32_t ticks = _TICK_ALIGN + z_ms_to_ticks(delay);
s32_t ticks = _TICK_ALIGN + k_ms_to_ticks_ceil32(delay);

z_add_thread_timeout(thread, ticks);
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/timeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ u32_t z_tick_get_32(void)

s64_t z_impl_k_uptime_get(void)
{
return __ticks_to_ms(z_tick_get());
return k_ticks_to_ms_floor64(z_tick_get());
}

#ifdef CONFIG_USERSPACE
Expand Down
4 changes: 2 additions & 2 deletions kernel/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ void z_impl_k_timer_start(struct k_timer *timer, s32_t duration, s32_t period)

volatile s32_t period_in_ticks, duration_in_ticks;

period_in_ticks = z_ms_to_ticks(period);
duration_in_ticks = z_ms_to_ticks(duration);
period_in_ticks = k_ms_to_ticks_ceil32(period);
duration_in_ticks = k_ms_to_ticks_ceil32(duration);

(void)z_abort_timeout(&timer->timeout);
timer->period = period_in_ticks;
Expand Down
2 changes: 1 addition & 1 deletion kernel/work_q.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ int k_delayed_work_submit_to_queue(struct k_work_q *work_q,

/* Add timeout */
z_add_timeout(&work->timeout, work_timeout,
_TICK_ALIGN + z_ms_to_ticks(delay));
_TICK_ALIGN + k_ms_to_ticks_ceil32(delay));

done:
k_spin_unlock(&lock, key);
Expand Down
2 changes: 1 addition & 1 deletion lib/cmsis_rtos_v1/cmsis_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ osEvent osSignalWait(int32_t signals, uint32_t millisec)
*/
hwclk_cycles_delta = (u64_t)k_cycle_get_32() - time_stamp_start;
time_delta_ns =
(u32_t)SYS_CLOCK_HW_CYCLES_TO_NS(hwclk_cycles_delta);
(u32_t)k_cyc_to_ns_floor64(hwclk_cycles_delta);
time_delta_ms = (u32_t)time_delta_ns/NSEC_PER_MSEC;

if (timeout > time_delta_ms) {
Expand Down
4 changes: 2 additions & 2 deletions lib/cmsis_rtos_v2/event_flags.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ uint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags,
struct cv2_event_flags *events = (struct cv2_event_flags *)ef_id;
int retval, key;
u32_t sig;
u32_t time_delta_ms, timeout_ms = __ticks_to_ms(timeout);
u32_t time_delta_ms, timeout_ms = k_ticks_to_ms_floor64(timeout);
u64_t time_stamp_start, hwclk_cycles_delta, time_delta_ns;

/* Can be called from ISRs only if timeout is set to 0 */
Expand Down Expand Up @@ -172,7 +172,7 @@ uint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags,
(u64_t)k_cycle_get_32() - time_stamp_start;

time_delta_ns =
(u32_t)SYS_CLOCK_HW_CYCLES_TO_NS(hwclk_cycles_delta);
(u32_t)k_cyc_to_ns_floor64(hwclk_cycles_delta);

time_delta_ms = (u32_t)time_delta_ns / NSEC_PER_MSEC;

Expand Down
4 changes: 2 additions & 2 deletions lib/cmsis_rtos_v2/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ osStatus_t osDelay(uint32_t ticks)
return osErrorISR;
}

k_sleep(__ticks_to_ms(ticks));
k_sleep(k_ticks_to_ms_floor64(ticks));

return osOK;
}
Expand All @@ -149,7 +149,7 @@ osStatus_t osDelayUntil(uint32_t ticks)
}

ticks_elapsed = osKernelGetTickCount();
k_sleep(__ticks_to_ms(ticks - ticks_elapsed));
k_sleep(k_ticks_to_ms_floor64(ticks - ticks_elapsed));

return osOK;
}
2 changes: 1 addition & 1 deletion lib/cmsis_rtos_v2/mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void *osMemoryPoolAlloc(osMemoryPoolId_t mp_id, uint32_t timeout)
} else {
retval = k_mem_slab_alloc(
(struct k_mem_slab *)(&mslab->z_mslab),
(void **)&ptr, __ticks_to_ms(timeout));
(void **)&ptr, k_ticks_to_ms_floor64(timeout));
}

if (retval == 0) {
Expand Down
4 changes: 2 additions & 2 deletions lib/cmsis_rtos_v2/msgq.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ osStatus_t osMessageQueuePut(osMessageQueueId_t msgq_id, const void *msg_ptr,
retval = k_msgq_put(&msgq->z_msgq, (void *)msg_ptr, K_FOREVER);
} else {
retval = k_msgq_put(&msgq->z_msgq, (void *)msg_ptr,
__ticks_to_ms(timeout));
k_ticks_to_ms_floor64(timeout));
}

if (retval == 0) {
Expand Down Expand Up @@ -142,7 +142,7 @@ osStatus_t osMessageQueueGet(osMessageQueueId_t msgq_id, void *msg_ptr,
retval = k_msgq_get(&msgq->z_msgq, msg_ptr, K_FOREVER);
} else {
retval = k_msgq_get(&msgq->z_msgq, msg_ptr,
__ticks_to_ms(timeout));
k_ticks_to_ms_floor64(timeout));
}

if (retval == 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/cmsis_rtos_v2/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout)
status = k_mutex_lock(&mutex->z_mutex, K_NO_WAIT);
} else {
status = k_mutex_lock(&mutex->z_mutex,
__ticks_to_ms(timeout));
k_ticks_to_ms_floor64(timeout));
}

if (status == -EBUSY) {
Expand Down
2 changes: 1 addition & 1 deletion lib/cmsis_rtos_v2/semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ osStatus_t osSemaphoreAcquire(osSemaphoreId_t semaphore_id, uint32_t timeout)
status = k_sem_take(&semaphore->z_semaphore, K_NO_WAIT);
} else {
status = k_sem_take(&semaphore->z_semaphore,
__ticks_to_ms(timeout));
k_ticks_to_ms_floor64(timeout));
}

if (status == -EBUSY) {
Expand Down
4 changes: 2 additions & 2 deletions lib/cmsis_rtos_v2/thread_flags.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ uint32_t osThreadFlagsWait(uint32_t flags, uint32_t options, uint32_t timeout)
struct cv2_thread *tid;
int retval, key;
u32_t sig;
u32_t time_delta_ms, timeout_ms = __ticks_to_ms(timeout);
u32_t time_delta_ms, timeout_ms = k_ticks_to_ms_floor64(timeout);
u64_t time_stamp_start, hwclk_cycles_delta, time_delta_ns;

if (k_is_in_isr()) {
Expand Down Expand Up @@ -155,7 +155,7 @@ uint32_t osThreadFlagsWait(uint32_t flags, uint32_t options, uint32_t timeout)
(u64_t)k_cycle_get_32() - time_stamp_start;

time_delta_ns =
(u32_t)SYS_CLOCK_HW_CYCLES_TO_NS(hwclk_cycles_delta);
(u32_t)k_cyc_to_ns_floor64(hwclk_cycles_delta);

time_delta_ms = (u32_t)time_delta_ns / NSEC_PER_MSEC;

Expand Down
2 changes: 1 addition & 1 deletion lib/cmsis_rtos_v2/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type,
osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks)
{
struct cv2_timer *timer = (struct cv2_timer *)timer_id;
u32_t millisec = __ticks_to_ms(ticks);
u32_t millisec = k_ticks_to_ms_floor64(ticks);

if (timer == NULL) {
return osErrorParameter;
Expand Down
2 changes: 1 addition & 1 deletion samples/smp/pi/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void main(void)
stop_time = k_cycle_get_32();

cycles_spent = stop_time - start_time;
nanoseconds_spent = SYS_CLOCK_HW_CYCLES_TO_NS(cycles_spent);
nanoseconds_spent = (u32_t)k_cyc_to_ns_floor64(cycles_spent);

for (i = 0; i < THREADS_NUM; i++)
printk("Pi value calculated by thread #%d: %s\n", i, buffer[i]);
Expand Down
2 changes: 1 addition & 1 deletion subsys/bluetooth/shell/gatt.c
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ static ssize_t write_met(struct bt_conn *conn, const struct bt_gatt_attr *attr,
memcpy(value + offset, buf, len);

delta = k_cycle_get_32() - cycle_stamp;
delta = SYS_CLOCK_HW_CYCLES_TO_NS(delta);
delta = (u32_t)k_cyc_to_ns_floor64(delta);

/* if last data rx-ed was greater than 1 second in the past,
* reset the metrics.
Expand Down
2 changes: 1 addition & 1 deletion subsys/bluetooth/shell/l2cap.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static int l2cap_recv_metrics(struct bt_l2cap_chan *chan, struct net_buf *buf)
u32_t delta;

delta = k_cycle_get_32() - cycle_stamp;
delta = SYS_CLOCK_HW_CYCLES_TO_NS(delta);
delta = (u32_t)k_cyc_to_ns_floor64(delta);

/* if last data rx-ed was greater than 1 second in the past,
* reset the metrics.
Expand Down
6 changes: 3 additions & 3 deletions subsys/debug/tracing/cpu_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ void cpu_stats_get_ns(struct cpu_stats *cpu_stats_ns)
int key = irq_lock();

cpu_stats_update_counters();
cpu_stats_ns->idle = SYS_CLOCK_HW_CYCLES_TO_NS(stats_hw_tick.idle);
cpu_stats_ns->non_idle = SYS_CLOCK_HW_CYCLES_TO_NS(
cpu_stats_ns->idle = (u32_t)k_cyc_to_ns_floor64(stats_hw_tick.idle);
cpu_stats_ns->non_idle = (u32_t)k_cyc_to_ns_floor64(
stats_hw_tick.non_idle);
cpu_stats_ns->sched = SYS_CLOCK_HW_CYCLES_TO_NS(stats_hw_tick.sched);
cpu_stats_ns->sched = (u32_t)k_cyc_to_ns_floor64(stats_hw_tick.sched);
irq_unlock(key);
}

Expand Down
Loading

0 comments on commit 8892406

Please sign in to comment.