|
| 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 Unified clock_time subsystem implementation |
| 9 | + */ |
| 10 | + |
| 11 | +#include <rtthread.h> |
| 12 | +#include <rtdevice.h> |
| 13 | +#include <drivers/clock_time.h> |
| 14 | + |
| 15 | +#define DBG_TAG "clock_time" |
| 16 | +#define DBG_LVL DBG_INFO |
| 17 | +#include <rtdbg.h> |
| 18 | + |
| 19 | +/* Default system clock time device */ |
| 20 | +static rt_clock_time_t _default_device = RT_NULL; |
| 21 | + |
| 22 | +/** |
| 23 | + * @brief Register a clock time device |
| 24 | + */ |
| 25 | +rt_err_t rt_clock_time_device_register(struct rt_clock_time_device *dev, |
| 26 | + const char *name, |
| 27 | + rt_uint8_t caps) |
| 28 | +{ |
| 29 | + rt_err_t result; |
| 30 | + |
| 31 | + RT_ASSERT(dev != RT_NULL); |
| 32 | + RT_ASSERT(name != RT_NULL); |
| 33 | + RT_ASSERT(dev->ops != RT_NULL); |
| 34 | + |
| 35 | + /* Initialize parent device structure */ |
| 36 | + dev->parent.type = RT_Device_Class_Timer; |
| 37 | + dev->parent.rx_indicate = RT_NULL; |
| 38 | + dev->parent.tx_complete = RT_NULL; |
| 39 | + |
| 40 | + dev->parent.init = RT_NULL; |
| 41 | + dev->parent.open = RT_NULL; |
| 42 | + dev->parent.close = RT_NULL; |
| 43 | + dev->parent.read = RT_NULL; |
| 44 | + dev->parent.write = RT_NULL; |
| 45 | + dev->parent.control = RT_NULL; |
| 46 | + |
| 47 | + dev->caps = caps; |
| 48 | + |
| 49 | + /* Calculate resolution scale factor */ |
| 50 | + if (dev->ops->get_freq) |
| 51 | + { |
| 52 | + rt_uint64_t freq = dev->ops->get_freq(); |
| 53 | + if (freq > 0) |
| 54 | + { |
| 55 | + /* res_scale = (1e9 * RT_CLOCK_TIME_RESMUL) / freq */ |
| 56 | + dev->res_scale = ((1000000000ULL * RT_CLOCK_TIME_RESMUL) / freq); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + dev->res_scale = RT_CLOCK_TIME_RESMUL; |
| 61 | + } |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + dev->res_scale = RT_CLOCK_TIME_RESMUL; |
| 66 | + } |
| 67 | + |
| 68 | + /* Register device */ |
| 69 | + result = rt_device_register(&dev->parent, name, RT_DEVICE_FLAG_RDWR); |
| 70 | + if (result != RT_EOK) |
| 71 | + { |
| 72 | + LOG_E("Failed to register clock_time device: %s", name); |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + /* Set as default if none exists */ |
| 77 | + if (_default_device == RT_NULL) |
| 78 | + { |
| 79 | + _default_device = dev; |
| 80 | + LOG_D("Set %s as default clock_time device", name); |
| 81 | + } |
| 82 | + |
| 83 | + LOG_I("Registered clock_time device: %s (caps: 0x%02x)", name, caps); |
| 84 | + |
| 85 | + return RT_EOK; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @brief Get the default system clock time device |
| 90 | + */ |
| 91 | +rt_clock_time_t rt_clock_time_default(void) |
| 92 | +{ |
| 93 | + return _default_device; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * @brief Set the default system clock time device |
| 98 | + */ |
| 99 | +rt_err_t rt_clock_time_set_default(rt_clock_time_t dev) |
| 100 | +{ |
| 101 | + RT_ASSERT(dev != RT_NULL); |
| 102 | + |
| 103 | + _default_device = dev; |
| 104 | + LOG_D("Changed default clock_time device to: %s", dev->parent.parent.name); |
| 105 | + |
| 106 | + return RT_EOK; |
| 107 | +} |
0 commit comments