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
* 2019-07-29 zdzn first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#include "drv_rtc.h"
#ifdef BSP_USING_RTC
static struct rt_rtc_device rtc_device;
rt_uint8_t buf[]=
{
0x00, 0x00, 0x43, 0x15, 0x05, 0x01, 0x03, 0x19
};
static time_t raspi_get_timestamp(void)
{
struct tm tm_new = {0};
buf[0] = 0;
bcm283x_i2c_write_read_rs((char*)buf, 1, (char*)buf, 7);
tm_new.tm_year = ((buf[6] / 16) + 0x30) * 10 + (buf[6] % 16) + 0x30;
tm_new.tm_mon = ((buf[5] & 0x1F) / 16 + 0x30) + (buf[5] & 0x1F) % 16+ 0x30;
tm_new.tm_mday = ((buf[4] & 0x3F) / 16 + 0x30) + (buf[4] & 0x3F) % 16+ 0x30;
tm_new.tm_hour = ((buf[2] & 0x3F) / 16 + 0x30) + (buf[2] & 0x3F) % 16+ 0x30;
tm_new.tm_min = ((buf[1] & 0x7F) / 16 + 0x30) + (buf[1] & 0x7F) % 16+ 0x30;
tm_new.tm_sec = ((buf[0] & 0x7F) / 16 + 0x30) + (buf[0] & 0x7F) % 16+ 0x30;
return timegm(&tm_new);
}
static int raspi_set_timestamp(time_t timestamp)
{
struct tm tblock;
gmtime_r(×tamp, &tblock);
buf[0] = 0;
buf[1] = tblock.tm_sec;
buf[2] = tblock.tm_min;
buf[3] = tblock.tm_hour;
buf[4] = tblock.tm_wday;
buf[5] = tblock.tm_mday;
buf[6] = tblock.tm_mon;
buf[7] = tblock.tm_year;
bcm283x_i2c_write((PER_BASE + BCM283X_BSC0_BASE) ,buf, 8);
return RT_EOK;
}
static rt_err_t raspi_rtc_init(rt_device_t dev)
{
bcm283x_i2c_setSlaveAddress(0, 0x68);
bcm283x_i2c_set_baudrate(0, 10000);
raspi_set_timestamp(0);
return RT_EOK;
}
static rt_err_t raspi_rtc_open(rt_device_t dev, rt_uint16_t oflag)
{
bcm283x_i2c_begin(0);
return RT_EOK;
}
static rt_err_t raspi_rtc_close(rt_device_t dev)
{
bcm283x_i2c_end(0);
return RT_EOK;
}
static rt_err_t raspi_rtc_control(rt_device_t dev, int cmd, void *args)
{
RT_ASSERT(dev != RT_NULL);
switch (cmd)
{
case RT_DEVICE_CTRL_RTC_GET_TIME:
*(time_t *)args = raspi_get_timestamp();
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
raspi_set_timestamp(*(time_t *)args);
break;
default:
return -RT_EINVAL;
}
return RT_EOK;
}
static rt_ssize_t raspi_rtc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
raspi_rtc_control(dev, RT_DEVICE_CTRL_RTC_GET_TIME, buffer);
return size;
}
static rt_ssize_t raspi_rtc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
raspi_rtc_control(dev, RT_DEVICE_CTRL_RTC_SET_TIME, (void *)buffer);
return size;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops raspi_rtc_ops =
{
.init = raspi_rtc_init,
.open = raspi_rtc_open,
.close = raspi_rtc_close,
.read = raspi_rtc_read,
.write = raspi_rtc_write,
.control = raspi_rtc_control
};
#endif
int rt_hw_rtc_init(void)
{
rt_memset(&rtc_device, 0, sizeof(rtc_device));
rtc_device.device.type = RT_Device_Class_RTC;
rtc_device.device.rx_indicate = RT_NULL;
rtc_device.device.tx_complete = RT_NULL;
rtc_device.device.ops = &raspi_rtc_ops;
rtc_device.device.user_data = RT_NULL;
/* register a rtc device */
rt_device_register(&rtc_device.device, "rtc", RT_DEVICE_FLAG_RDWR);
return 0;
}
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
#endif /* BSP_USING_RTC */
|