1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017-11-06 Haley the first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "am_mcu_apollo.h"
#include <sys/time.h>
#define XT 1
#define LFRC 2
#define RTC_CLK_SRC XT
//connect am drv to rt drv.
static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
{
if (dev->rx_indicate != RT_NULL)
{
/* Open Interrupt */
}
return RT_EOK;
}
static rt_ssize_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{
return 0;
}
static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
{
time_t *time;
struct tm time_temp;
struct tm time_new;
am_hal_rtc_time_t hal_time;
RT_ASSERT(dev != RT_NULL);
rt_memset(&time_temp, 0, sizeof(struct tm));
switch (cmd)
{
case RT_DEVICE_CTRL_RTC_GET_TIME:
time = (time_t *)args;
/* Get the current Time */
am_hal_rtc_time_get(&hal_time);
/* Years since 1900 : 0-99 range */
time_temp.tm_year = hal_time.ui32Year + 2000 - 1900;
/* Months *since* january 0-11 : RTC_Month_Date_Definitions 1 - 12 */
time_temp.tm_mon = hal_time.ui32Month - 1;
/* Day of the month 1-31 : 1-31 range */
time_temp.tm_mday = hal_time.ui32DayOfMonth;
/* Hours since midnight 0-23 : 0-23 range */
time_temp.tm_hour = hal_time.ui32Hour;
/* Minutes 0-59 : the 0-59 range */
time_temp.tm_min = hal_time.ui32Minute;
/* Seconds 0-59 : the 0-59 range */
time_temp.tm_sec = hal_time.ui32Second;
*time = timegm(&time_temp);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
time = (time_t *)args;
gmtime_r(time, &time_new);
hal_time.ui32Hour = time_new.tm_hour;
hal_time.ui32Minute = time_new.tm_min;
hal_time.ui32Second = time_new.tm_sec;
hal_time.ui32Hundredths = 00;
hal_time.ui32Weekday = time_new.tm_wday;
hal_time.ui32DayOfMonth = time_new.tm_mday;
hal_time.ui32Month = time_new.tm_mon + 1;
hal_time.ui32Year = time_new.tm_year + 1900 - 2000;
hal_time.ui32Century = 0;
am_hal_rtc_time_set(&hal_time);
break;
}
return RT_EOK;
}
int rt_hw_rtc_init(void)
{
static struct rt_device rtc;
#if RTC_CLK_SRC == LFRC
/* Enable the LFRC for the RTC */
am_hal_clkgen_osc_start(AM_HAL_CLKGEN_OSC_LFRC);
/* Select LFRC for RTC clock source */
am_hal_rtc_osc_select(AM_HAL_RTC_OSC_LFRC);
#endif
#if RTC_CLK_SRC == XT
/* Enable the XT for the RTC */
am_hal_clkgen_osc_start(AM_HAL_CLKGEN_OSC_XT);
/* Select XT for RTC clock source */
am_hal_rtc_osc_select(AM_HAL_RTC_OSC_XT);
#endif
/* Enable the RTC */
am_hal_rtc_osc_enable();
/* register rtc device */
rtc.type = RT_Device_Class_RTC;
rtc.init = RT_NULL;
rtc.open = rt_rtc_open;
rtc.close = RT_NULL;
rtc.read = rt_rtc_read;
rtc.write = RT_NULL;
rtc.control = rt_rtc_control;
/* no private */
rtc.user_data = RT_NULL;
rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
return 0;
}
#ifdef RT_USING_COMPONENTS_INIT
INIT_BOARD_EXPORT(rt_hw_rtc_init);
#endif
/*@}*/
|