1 /* 2 * Copyright (c) 2024, Your Company Name 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2024-09-04 Alex First version for MCXC444 9 */ 10 11 #include <rtthread.h> 12 #include <rtdevice.h> 13 #include <sys/time.h> 14 15 #include "drv_rtc.h" 16 17 #include "fsl_rtc.h" 18 #include "fsl_clock.h" 19 20 #define DBG_TAG "drv.rtc" 21 #define DBG_LVL DBG_INFO 22 #include <rtdbg.h> 23 mcxc444_rtc_init(rt_device_t dev)24static rt_err_t mcxc444_rtc_init(rt_device_t dev) 25 { 26 rtc_config_t rtcConfig; 27 28 CLOCK_EnableClock(kCLOCK_Rtc0); 29 30 RTC_GetDefaultConfig(&rtcConfig); 31 32 RTC_Init(RTC, &rtcConfig); 33 34 RTC_SetClockSource(RTC); 35 36 rt_thread_mdelay(10); 37 38 RTC_StartTimer(RTC); 39 40 return RT_EOK; 41 } 42 mcxc444_rtc_get_time(time_t * ts)43static rt_err_t mcxc444_rtc_get_time(time_t *ts) 44 { 45 if (ts == RT_NULL) 46 { 47 return -RT_EINVAL; 48 } 49 50 *ts = RTC->TSR; 51 52 return RT_EOK; 53 } 54 mcxc444_rtc_set_time(time_t * ts)55static rt_err_t mcxc444_rtc_set_time(time_t *ts) 56 { 57 if (ts == RT_NULL) 58 { 59 return -RT_EINVAL; 60 } 61 62 RTC_StopTimer(RTC); 63 RTC->TSR = *ts; 64 RTC_StartTimer(RTC); 65 66 return RT_EOK; 67 } 68 mcxc444_rtc_control(rt_device_t dev,int cmd,void * args)69static rt_err_t mcxc444_rtc_control(rt_device_t dev, int cmd, void *args) 70 { 71 rt_err_t result = RT_EOK; 72 73 switch (cmd) 74 { 75 case RT_DEVICE_CTRL_RTC_GET_TIME: 76 result = mcxc444_rtc_get_time((time_t *)args); 77 break; 78 79 case RT_DEVICE_CTRL_RTC_SET_TIME: 80 result = mcxc444_rtc_set_time((time_t *)args); 81 break; 82 83 default: 84 return -RT_EINVAL; 85 } 86 87 return result; 88 } 89 90 static struct rt_device rtc_device; 91 rt_hw_rtc_init(void)92int rt_hw_rtc_init(void) 93 { 94 rt_err_t ret; 95 96 rtc_device.type = RT_Device_Class_RTC; 97 rtc_device.init = mcxc444_rtc_init; 98 rtc_device.open = RT_NULL; 99 rtc_device.close = RT_NULL; 100 rtc_device.read = RT_NULL; 101 rtc_device.write = RT_NULL; 102 rtc_device.control = mcxc444_rtc_control; 103 104 ret = rt_device_register(&rtc_device, "rtc", RT_DEVICE_FLAG_RDWR); 105 if (ret != RT_EOK) 106 { 107 LOG_E("rtc register err code: %d\n", ret); 108 return ret; 109 } 110 111 return RT_EOK; 112 } 113 114 INIT_DEVICE_EXPORT(rt_hw_rtc_init); 115 116 117