1 /* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2017-12-23 Bernard first version 9 */ 10 11 #include <rtdevice.h> 12 #include <rtthread.h> 13 14 static const struct rt_clock_cputime_ops *_cputime_ops = RT_NULL; 15 16 /** 17 * The clock_cpu_getres() function shall return the resolution of CPU time, the 18 * number of nanosecond per tick. 19 * 20 * @return the number of nanosecond per tick 21 */ clock_cpu_getres(void)22float clock_cpu_getres(void) 23 { 24 if (_cputime_ops) 25 return _cputime_ops->cputime_getres(); 26 27 rt_set_errno(-ENOSYS); 28 return 0; 29 } 30 31 /** 32 * The clock_cpu_gettime() function shall return the current value of cpu time tick. 33 * 34 * @return the cpu tick 35 */ clock_cpu_gettime(void)36uint32_t clock_cpu_gettime(void) 37 { 38 if (_cputime_ops) 39 return _cputime_ops->cputime_gettime(); 40 41 rt_set_errno(-ENOSYS); 42 return 0; 43 } 44 45 /** 46 * The clock_cpu_microsecond() fucntion shall return the microsecond according to 47 * cpu_tick parameter. 48 * 49 * @param cpu_tick the cpu tick 50 * 51 * @return the microsecond 52 */ clock_cpu_microsecond(uint32_t cpu_tick)53uint32_t clock_cpu_microsecond(uint32_t cpu_tick) 54 { 55 float unit = clock_cpu_getres(); 56 57 return (uint32_t)((cpu_tick * unit) / 1000); 58 } 59 60 /** 61 * The clock_cpu_microsecond() fucntion shall return the millisecond according to 62 * cpu_tick parameter. 63 * 64 * @param cpu_tick the cpu tick 65 * 66 * @return the millisecond 67 */ clock_cpu_millisecond(uint32_t cpu_tick)68uint32_t clock_cpu_millisecond(uint32_t cpu_tick) 69 { 70 float unit = clock_cpu_getres(); 71 72 return (uint32_t)((cpu_tick * unit) / (1000 * 1000)); 73 } 74 75 /** 76 * The clock_cpu_seops() function shall set the ops of cpu time. 77 * 78 * @return always return 0. 79 */ clock_cpu_setops(const struct rt_clock_cputime_ops * ops)80int clock_cpu_setops(const struct rt_clock_cputime_ops *ops) 81 { 82 _cputime_ops = ops; 83 if (ops) 84 { 85 RT_ASSERT(ops->cputime_getres != RT_NULL); 86 RT_ASSERT(ops->cputime_gettime != RT_NULL); 87 } 88 89 return 0; 90 } 91