|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2025, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2025-01-01 RT-Thread Compatibility layer for legacy cputime API |
| 9 | + * |
| 10 | + * COMPATIBILITY HEADER: |
| 11 | + * This header provides backward compatibility for code using the old cputime API. |
| 12 | + * The old cputime subsystem has been removed and replaced with the unified |
| 13 | + * clock_time subsystem. |
| 14 | + */ |
| 15 | + |
| 16 | +#ifndef __DRIVERS_CPUTIME_H__ |
| 17 | +#define __DRIVERS_CPUTIME_H__ |
| 18 | + |
| 19 | +#include <rtthread.h> |
| 20 | + |
| 21 | +#ifdef __cplusplus |
| 22 | +extern "C" { |
| 23 | +#endif |
| 24 | + |
| 25 | +#ifdef RT_USING_CLOCK_TIME |
| 26 | +/* When clock_time is enabled, use the new APIs */ |
| 27 | +#include <drivers/clock_time.h> |
| 28 | + |
| 29 | +/* Map old cputime APIs to new clock_time APIs */ |
| 30 | +#define clock_cpu_getres() rt_clock_cputimer_getres() |
| 31 | +#define clock_cpu_gettime() rt_clock_cputimer_getcnt() |
| 32 | +#define clock_cpu_microsecond(tick) ((tick) * 1000000ULL / rt_clock_cputimer_getfrq()) |
| 33 | +#define clock_cpu_millisecond(tick) ((tick) * 1000ULL / rt_clock_cputimer_getfrq()) |
| 34 | + |
| 35 | +/* Delay functions for BSP compatibility */ |
| 36 | +rt_inline void clock_cpu_delay_us(rt_uint32_t us) |
| 37 | +{ |
| 38 | + rt_uint64_t start = rt_clock_cputimer_getcnt(); |
| 39 | + rt_uint64_t freq = rt_clock_cputimer_getfrq(); |
| 40 | + rt_uint64_t delta = (rt_uint64_t)us * freq / 1000000ULL; |
| 41 | + while ((rt_clock_cputimer_getcnt() - start) < delta); |
| 42 | +} |
| 43 | + |
| 44 | +rt_inline void clock_cpu_delay_ms(rt_uint32_t ms) |
| 45 | +{ |
| 46 | + rt_uint64_t start = rt_clock_cputimer_getcnt(); |
| 47 | + rt_uint64_t freq = rt_clock_cputimer_getfrq(); |
| 48 | + rt_uint64_t delta = (rt_uint64_t)ms * freq / 1000ULL; |
| 49 | + while ((rt_clock_cputimer_getcnt() - start) < delta); |
| 50 | +} |
| 51 | + |
| 52 | +/* Stub for riscv_cputime_init - now handled by clock_time */ |
| 53 | +rt_inline int riscv_cputime_init(void) |
| 54 | +{ |
| 55 | + /* Initialization is now handled by clock_time subsystem */ |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
| 59 | +#else |
| 60 | +/* When clock_time is not enabled, provide stub implementations */ |
| 61 | + |
| 62 | +/* These are stub implementations for backward compatibility */ |
| 63 | +rt_inline rt_uint64_t clock_cpu_getres(void) |
| 64 | +{ |
| 65 | + return ((1000ULL * 1000 * 1000) * 1000000ULL) / RT_TICK_PER_SECOND; |
| 66 | +} |
| 67 | + |
| 68 | +rt_inline rt_uint64_t clock_cpu_gettime(void) |
| 69 | +{ |
| 70 | + return rt_tick_get(); |
| 71 | +} |
| 72 | + |
| 73 | +rt_inline rt_uint64_t clock_cpu_microsecond(rt_uint64_t cpu_tick) |
| 74 | +{ |
| 75 | + return (cpu_tick * 1000000ULL) / RT_TICK_PER_SECOND; |
| 76 | +} |
| 77 | + |
| 78 | +rt_inline rt_uint64_t clock_cpu_millisecond(rt_uint64_t cpu_tick) |
| 79 | +{ |
| 80 | + return (cpu_tick * 1000ULL) / RT_TICK_PER_SECOND; |
| 81 | +} |
| 82 | + |
| 83 | +/* Tick-based delay functions */ |
| 84 | +rt_inline void clock_cpu_delay_us(rt_uint32_t us) |
| 85 | +{ |
| 86 | + rt_uint32_t start = rt_tick_get(); |
| 87 | + rt_uint32_t delta = (us * RT_TICK_PER_SECOND + 999999) / 1000000; |
| 88 | + if (delta == 0) delta = 1; |
| 89 | + while ((rt_tick_get() - start) < delta); |
| 90 | +} |
| 91 | + |
| 92 | +rt_inline void clock_cpu_delay_ms(rt_uint32_t ms) |
| 93 | +{ |
| 94 | + rt_uint32_t start = rt_tick_get(); |
| 95 | + rt_uint32_t delta = (ms * RT_TICK_PER_SECOND + 999) / 1000; |
| 96 | + if (delta == 0) delta = 1; |
| 97 | + while ((rt_tick_get() - start) < delta); |
| 98 | +} |
| 99 | + |
| 100 | +rt_inline int riscv_cputime_init(void) |
| 101 | +{ |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
| 105 | +#endif /* RT_USING_CLOCK_TIME */ |
| 106 | + |
| 107 | +#ifdef __cplusplus |
| 108 | +} |
| 109 | +#endif |
| 110 | + |
| 111 | +#endif /* __DRIVERS_CPUTIME_H__ */ |
0 commit comments