-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrtc.c
More file actions
155 lines (124 loc) · 2.65 KB
/
rtc.c
File metadata and controls
155 lines (124 loc) · 2.65 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include "rtc.h"
#include "common.h"
const char year_monthdays[2][13] = {
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
char int2bcd(int num)
{
char result;
result = 0xF0 & ((num/10)<<4);
result += (0x0F&(num%10));
return result;
}
int get_rtctime(unsigned char *rx_buf)
{
int GiFd;
unsigned int uiRet;
int i;
int waittime;
unsigned char tx_buf[RTC_DATA_LEN];
unsigned char addr[2] ;
addr[0] = 0x00;
waittime = 20;
while((g_sys_info.state_rtc != 1)&&(waittime--))
{
sleep(1);
}
pthread_mutex_lock(&g_rtc_mutex);
GiFd = open("/dev/i2c-1", O_RDWR);
if(GiFd == -1)
{
return -1;
perror("open rtc\n");
}
uiRet = ioctl(GiFd, I2C_SLAVE, I2C_ADDR >> 1);
if (uiRet < 0) {
return -1;
}
tx_buf[0] = addr[0];
for (i = 1; i < RTC_DATA_LEN; i++)
tx_buf[i] = i;
write(GiFd, addr, 1);
read(GiFd, rx_buf, RTC_DATA_LEN - 1);
close(GiFd);
pthread_mutex_unlock(&g_rtc_mutex);
return 0;
}
int set_rtctime(unsigned char *cst)
{
int GiFd;
unsigned int uiRet;
unsigned char tx_buf[RTC_DATA_LEN];
unsigned char addr[2];
addr[0] = 0x00;
tx_buf[0] = 0x00;
tx_buf[7] = int2bcd(cst[0]); //year
tx_buf[6] = int2bcd(cst[1]); //month
tx_buf[5] = int2bcd(cst[2]); //date
tx_buf[4] = 0x07 & 1; //day
tx_buf[3] = int2bcd(cst[3]); tx_buf[3] += 0x80; //hour
tx_buf[2] = int2bcd(cst[4]); //minute
tx_buf[1] = int2bcd(cst[5]); //secend
pthread_mutex_lock(&g_rtc_mutex);
GiFd = open("/dev/i2c-1", O_RDWR);
if(GiFd == -1)
{
perror("open rtc\n");
return -1;
}
uiRet = ioctl(GiFd, I2C_SLAVE, I2C_ADDR >> 1);
if (uiRet < 0) {
return -1;
}
write(GiFd, tx_buf, 8);
pthread_mutex_unlock(&g_rtc_mutex);
return 1;
}
void utc2cst(unsigned char *utc,unsigned char *cst)
{
volatile unsigned char year,mounth,date,hour,minute,second;
unsigned char isLeapYear;
year = utc[0];
mounth = utc[1];
date = utc[2];
hour = utc[3];
minute = utc[4];
second = utc[5];
if(year % 4)
isLeapYear = 0;
else
isLeapYear = 1;
hour += 8;
if(hour >= 24)
{
hour -= 24;
date++;
if(date > year_monthdays[isLeapYear][mounth])
{
date = 1;
mounth++;
if(mounth > 12)
{
mounth = 1;
year++;
}
}
}
cst[0] = year;
cst[1] = mounth;
cst[2] = date;
cst[3] = hour;
cst[4] = minute;
cst[5] = second;
}