|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2019-04-01 wangyq the first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include <rthw.h> |
| 12 | +#include <rtthread.h> |
| 13 | +#include <rtdevice.h> |
| 14 | +#include <string.h> |
| 15 | +#include "board.h" |
| 16 | +#include "drv_rtc.h" |
| 17 | +#include <ald_cmu.h> |
| 18 | +#include <ald_rtc.h> |
| 19 | + |
| 20 | +#ifdef RT_USING_RTC |
| 21 | + |
| 22 | +static void rtc_init(rtc_init_t *init) |
| 23 | +{ |
| 24 | + assert_param(IS_RTC_HOUR_FORMAT(init->hour_format)); |
| 25 | + assert_param(IS_RTC_OUTPUT_SEL(init->output)); |
| 26 | + assert_param(IS_RTC_OUTPUT_POLARITY(init->output_polarity)); |
| 27 | + |
| 28 | + rtc_reset(); |
| 29 | + RTC_UNLOCK(); |
| 30 | + |
| 31 | + MODIFY_REG(RTC->CON, RTC_CON_HFM_MSK, init->hour_format << RTC_CON_HFM_POS); |
| 32 | + MODIFY_REG(RTC->CON, RTC_CON_EOS_MSK, init->output << RTC_CON_EOS_POSS); |
| 33 | + MODIFY_REG(RTC->CON, RTC_CON_POL_MSK, init->output_polarity << RTC_CON_POL_POS); |
| 34 | + MODIFY_REG(RTC->PSR, RTC_PSR_SPRS_MSK, init->synch_pre_div << RTC_PSR_SPRS_POSS); |
| 35 | + MODIFY_REG(RTC->PSR, RTC_PSR_APRS_MSK, init->asynch_pre_div << RTC_PSR_APRS_POSS); |
| 36 | + |
| 37 | + RTC_LOCK(); |
| 38 | + return; |
| 39 | +} |
| 40 | + |
| 41 | +static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args) |
| 42 | +{ |
| 43 | + rt_err_t result = RT_EOK; |
| 44 | + |
| 45 | + struct tm time_temp; |
| 46 | + struct tm *pNow; |
| 47 | + rtc_date_t date; |
| 48 | + rtc_time_t time; |
| 49 | + |
| 50 | + switch (cmd) |
| 51 | + { |
| 52 | + case RT_DEVICE_CTRL_RTC_GET_TIME: |
| 53 | + |
| 54 | + rtc_get_date_time(&date, &time, RTC_FORMAT_DEC); |
| 55 | + time_temp.tm_sec = time.second; |
| 56 | + time_temp.tm_min = time.minute; |
| 57 | + time_temp.tm_hour = time.hour; |
| 58 | + time_temp.tm_mday = date.day; |
| 59 | + time_temp.tm_mon = date.month - 1; |
| 60 | + time_temp.tm_year = date.year - 1900 + 2000; |
| 61 | + *((time_t *)args) = mktime(&time_temp); |
| 62 | + break; |
| 63 | + |
| 64 | + case RT_DEVICE_CTRL_RTC_SET_TIME: |
| 65 | + |
| 66 | + rt_enter_critical(); |
| 67 | + /* converts calendar time time into local time. */ |
| 68 | + pNow = localtime((const time_t *)args); |
| 69 | + /* copy the statically located variable */ |
| 70 | + memcpy(&time_temp, pNow, sizeof(struct tm)); |
| 71 | + /* unlock scheduler. */ |
| 72 | + rt_exit_critical(); |
| 73 | + |
| 74 | + time.hour = time_temp.tm_hour; |
| 75 | + time.minute = time_temp.tm_min; |
| 76 | + time.second = time_temp.tm_sec; |
| 77 | + date.year = time_temp.tm_year + 1900 - 2000; |
| 78 | + date.month = time_temp.tm_mon + 1; |
| 79 | + date.day = time_temp.tm_mday; |
| 80 | + rtc_set_time(&time, RTC_FORMAT_DEC); |
| 81 | + rtc_set_date(&date, RTC_FORMAT_DEC); |
| 82 | + /* start RTC */ |
| 83 | + RTC_UNLOCK(); |
| 84 | + SET_BIT(RTC->CON, RTC_CON_GO_MSK); |
| 85 | + RTC_LOCK(); |
| 86 | + break; |
| 87 | + |
| 88 | + case RT_DEVICE_CTRL_RTC_GET_ALARM: |
| 89 | + break; |
| 90 | + |
| 91 | + case RT_DEVICE_CTRL_RTC_SET_ALARM: |
| 92 | + break; |
| 93 | + |
| 94 | + default: |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + return result; |
| 99 | +} |
| 100 | + |
| 101 | +#ifdef RT_USING_DEVICE_OPS |
| 102 | +const static struct rt_device_ops es32f0_rtc_ops = |
| 103 | +{ |
| 104 | + RT_NULL, |
| 105 | + RT_NULL, |
| 106 | + RT_NULL, |
| 107 | + RT_NULL, |
| 108 | + RT_NULL, |
| 109 | + es32f0_rtc_control |
| 110 | +}; |
| 111 | +#endif |
| 112 | + |
| 113 | +int rt_hw_rtc_init(void) |
| 114 | +{ |
| 115 | + rt_err_t ret = RT_EOK; |
| 116 | + static struct rt_device rtc_dev; |
| 117 | + rtc_init_t rtc_initstruct; |
| 118 | + |
| 119 | + /* enable external 32.768kHz */ |
| 120 | + CMU_LOSC_ENABLE(); |
| 121 | + cmu_losc_safe_config(ENABLE); |
| 122 | + /* set default time */ |
| 123 | + RTC_UNLOCK(); |
| 124 | + WRITE_REG(RTC->TIME, 0x134251); |
| 125 | + WRITE_REG(RTC->DATE, 0x1190401); |
| 126 | + RTC_LOCK(); |
| 127 | + /* RTC function initialization */ |
| 128 | + rtc_initstruct.hour_format = RTC_HOUR_FORMAT_24; |
| 129 | + rtc_initstruct.asynch_pre_div = 0; |
| 130 | + rtc_initstruct.synch_pre_div = 32767; |
| 131 | + rtc_initstruct.output = RTC_OUTPUT_DISABLE; |
| 132 | + rtc_init(&rtc_initstruct); |
| 133 | + |
| 134 | + rtc_dev.type = RT_Device_Class_RTC; |
| 135 | + rtc_dev.rx_indicate = RT_NULL; |
| 136 | + rtc_dev.tx_complete = RT_NULL; |
| 137 | + |
| 138 | +#ifdef RT_USING_DEVICE_OPS |
| 139 | + rtc_dev.ops = &es32f0_rtc_ops; |
| 140 | +#else |
| 141 | + rtc_dev.init = RT_NULL; |
| 142 | + rtc_dev.open = RT_NULL; |
| 143 | + rtc_dev.close = RT_NULL; |
| 144 | + rtc_dev.read = RT_NULL; |
| 145 | + rtc_dev.write = RT_NULL; |
| 146 | + rtc_dev.control = es32f0_rtc_control; |
| 147 | +#endif |
| 148 | + |
| 149 | + rtc_dev.user_data = RTC; |
| 150 | + |
| 151 | + ret = rt_device_register(&rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR); |
| 152 | + |
| 153 | + return ret; |
| 154 | +} |
| 155 | +INIT_DEVICE_EXPORT(rt_hw_rtc_init); |
| 156 | + |
| 157 | +#endif |
0 commit comments