1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2019-07-29 zdzn first version
9 */
10
11 #include <rtthread.h>
12 #include <rtdevice.h>
13 #include <sys/time.h>
14 #include "drv_rtc.h"
15
16 #ifdef BSP_USING_RTC
17
18 static struct rt_rtc_device rtc_device;
19
20 rt_uint8_t buf[]=
21 {
22 0x00, 0x00, 0x43, 0x15, 0x05, 0x01, 0x03, 0x19
23 };
24
raspi_get_timestamp(void)25 static time_t raspi_get_timestamp(void)
26 {
27 struct tm tm_new = {0};
28 buf[0] = 0;
29 bcm283x_i2c_write_read_rs((char*)buf, 1, (char*)buf, 7);
30
31 tm_new.tm_year = ((buf[6] / 16) + 0x30) * 10 + (buf[6] % 16) + 0x30;
32 tm_new.tm_mon = ((buf[5] & 0x1F) / 16 + 0x30) + (buf[5] & 0x1F) % 16+ 0x30;
33 tm_new.tm_mday = ((buf[4] & 0x3F) / 16 + 0x30) + (buf[4] & 0x3F) % 16+ 0x30;
34 tm_new.tm_hour = ((buf[2] & 0x3F) / 16 + 0x30) + (buf[2] & 0x3F) % 16+ 0x30;
35 tm_new.tm_min = ((buf[1] & 0x7F) / 16 + 0x30) + (buf[1] & 0x7F) % 16+ 0x30;
36 tm_new.tm_sec = ((buf[0] & 0x7F) / 16 + 0x30) + (buf[0] & 0x7F) % 16+ 0x30;
37
38 return timegm(&tm_new);
39 }
40
raspi_set_timestamp(time_t timestamp)41 static int raspi_set_timestamp(time_t timestamp)
42 {
43 struct tm tblock;
44 gmtime_r(×tamp, &tblock);
45 buf[0] = 0;
46 buf[1] = tblock.tm_sec;
47 buf[2] = tblock.tm_min;
48 buf[3] = tblock.tm_hour;
49 buf[4] = tblock.tm_wday;
50 buf[5] = tblock.tm_mday;
51 buf[6] = tblock.tm_mon;
52 buf[7] = tblock.tm_year;
53 bcm283x_i2c_write((PER_BASE + BCM283X_BSC0_BASE) ,buf, 8);
54 return RT_EOK;
55 }
56
raspi_rtc_init(rt_device_t dev)57 static rt_err_t raspi_rtc_init(rt_device_t dev)
58 {
59 bcm283x_i2c_setSlaveAddress(0, 0x68);
60 bcm283x_i2c_set_baudrate(0, 10000);
61 raspi_set_timestamp(0);
62 return RT_EOK;
63 }
64
raspi_rtc_open(rt_device_t dev,rt_uint16_t oflag)65 static rt_err_t raspi_rtc_open(rt_device_t dev, rt_uint16_t oflag)
66 {
67 bcm283x_i2c_begin(0);
68 return RT_EOK;
69 }
70
raspi_rtc_close(rt_device_t dev)71 static rt_err_t raspi_rtc_close(rt_device_t dev)
72 {
73 bcm283x_i2c_end(0);
74 return RT_EOK;
75 }
76
raspi_rtc_control(rt_device_t dev,int cmd,void * args)77 static rt_err_t raspi_rtc_control(rt_device_t dev, int cmd, void *args)
78 {
79
80 RT_ASSERT(dev != RT_NULL);
81
82 switch (cmd)
83 {
84 case RT_DEVICE_CTRL_RTC_GET_TIME:
85 *(rt_uint32_t *)args = raspi_get_timestamp();
86 break;
87 case RT_DEVICE_CTRL_RTC_SET_TIME:
88 raspi_set_timestamp(*(time_t *)args);
89 break;
90 default:
91 return -RT_EINVAL;
92 }
93 return RT_EOK;
94 }
95
raspi_rtc_read(rt_device_t dev,rt_off_t pos,void * buffer,rt_size_t size)96 static rt_ssize_t raspi_rtc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
97 {
98 raspi_rtc_control(dev, RT_DEVICE_CTRL_RTC_GET_TIME, buffer);
99 return size;
100 }
101
raspi_rtc_write(rt_device_t dev,rt_off_t pos,const void * buffer,rt_size_t size)102 static rt_ssize_t raspi_rtc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
103 {
104 raspi_rtc_control(dev, RT_DEVICE_CTRL_RTC_SET_TIME, (void *)buffer);
105 return size;
106 }
107
108 #ifdef RT_USING_DEVICE_OPS
109 const static struct rt_device_ops raspi_rtc_ops =
110 {
111 .init = raspi_rtc_init,
112 .open = raspi_rtc_open,
113 .close = raspi_rtc_close,
114 .read = raspi_rtc_read,
115 .write = raspi_rtc_write,
116 .control = raspi_rtc_control
117 };
118 #endif
119
rt_hw_rtc_init(void)120 int rt_hw_rtc_init(void)
121 {
122 rt_memset(&rtc_device, 0, sizeof(rtc_device));
123
124 rtc_device.device.type = RT_Device_Class_RTC;
125 rtc_device.device.rx_indicate = RT_NULL;
126 rtc_device.device.tx_complete = RT_NULL;
127 rtc_device.device.ops = &raspi_rtc_ops;
128 rtc_device.device.user_data = RT_NULL;
129
130 /* register a rtc device */
131 rt_device_register(&rtc_device.device, "rtc", RT_DEVICE_FLAG_RDWR);
132
133 return 0;
134 }
135 INIT_DEVICE_EXPORT(rt_hw_rtc_init);
136 #endif /* BSP_USING_RTC */
137
138