-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrtc.c
385 lines (321 loc) · 8.36 KB
/
rtc.c
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/**
@file hardware/rtc.c
@brief DS1307 RTC Driver AVR8.
@par Edit History
- [1.0] [Mike Gore] Initial revision of file.
@par Copyright © 2014-2020 Mike Gore, Inc. All rights reserved.
*/
#include "user_config.h"
#include "time.h"
/// ================================================================================
///@brief RTC BCD conversion functions
/// @brief Convert number >= 0 and <= 99 to BCD.
///
/// - BCD format has each hex nibble has a digit 0 .. 9
///
/// @param[in] data: number to convert.
/// @return BCD value
/// @warning we assume the number is in range.
uint8_t BINtoBCD(uint8_t data)
{
return( ( (data/10U) << 4 ) | (data%10U) );
}
/// @brief Convert two "digit" BCD number to binary.
///
/// @param[in] data: BCD number.
/// - number >= 0 and <= 99 to BCD
/// @return Binary value.
uint8_t BCDtoBIN(uint8_t data)
{
return ( ( ( (data&0xf0U) >> 4) * 10 ) + (data&0x0fU) );
}
/// ================================================================================
///@brief RTC HAL
///@brief RTC I2C WRITE function
///@param[in] address: DS1307 I2C address
///@param[in] index: DS1307 internal address index pointer
///@param[out] buf: write buffer
///@param[in] len: buffer length
///
///@return 1 = OK, 0 = ERROR
int8_t i2c_rtc_write(uint8_t address, uint8_t index, uint8_t *buf, uint8_t len)
{
int8_t retry;
int8_t ret = 0;
int8_t i;
uint8_t reg[DS1307_REG_SIZE+1];
if(len < 1 || len > DS1307_REG_SIZE)
return(0);
reg[0] = index; /// DS1307 internal Address Pointer
for(i=0;i<len;++i)
reg[i+1] = buf[i];
retry = 2;
while(retry--)
{
if ( i2c_fn(address, TW_WRITE, reg, len+1) )
{
ret = 1;
break;
}
printf("i2c_rtc_write data error\n");
ret = 0;
}
return(ret);
}
///@brief RTC I2C READ function
///@param[in] address: DS1307 I2C address
///@param[in] index: DS1307 internal address index pointer
///@param[out] buf: read buffer
///@param[in] len: buffer length
///
///@return 1 = OK, 0 = ERROR
int8_t i2c_rtc_read(uint8_t address, uint8_t index, uint8_t *buf, uint8_t len)
{
int8_t retry;
int8_t ret = 0;
if(len < 1 || len > DS1307_REG_SIZE)
return(0);
retry = 2;
while(retry--)
{
if ( i2c_fn(address, TW_WRITE, (uint8_t *) &index, sizeof(index)) )
{
ret = 1;
break;
}
printf("i2c_rtc_read address error\n");
ret = 0;
}
if(!ret)
return(0);
if ( !i2c_fn(address, TW_READ, buf,len) )
{
printf("i2c_rtc_read data error\n");
return(0);
}
return(1);
}
///@brief RTC I2C initialization code
///
///@return void
void i2c_rtc_init()
{
i2c_init(100000);
}
/// ================================================================================
/// @brief RTC functions
/// @brief Set DS1307 RTC from POSIX struct tm * structure.
///
/// @param[in] t: POSIX struct tm * time to set.
///
/// @return 1 on sucess.
/// @return 0 on fail.
uint8_t rtc_write(tm_t *t)
{
uint8_t buf[DS1307_REG_SIZE];
buf[0] = BINtoBCD(t->tm_sec) & 0x7f;
buf[1] = BINtoBCD(t->tm_min) & 0x7f;
buf[2] = BINtoBCD(t->tm_hour) & 0x3f;
buf[3] = ((t->tm_wday & 7) + 1) & 0x0f;
buf[4] = BINtoBCD(t->tm_mday ) & 0x3f;
buf[5] = BINtoBCD(t->tm_mon + 1) & 0x1f;
buf[6] = BINtoBCD(t->tm_year - 100) & 0xff; // 2000 = 0
buf[7] = 0x93; // 32khz, out square wave
#ifdef RTC_DEBUG
printf("rtc_write():%d, day:%d,mon:%d,hour:%d,min:%d,sec:%d, wday:%d\n",
t->tm_year + 1900,
t->tm_mon,
t->tm_mday,
t->tm_hour,
t->tm_min,
t->tm_sec,
t->tm_wday);
#endif
#ifdef RTC_DEBUG
printf("%4x\n", t);
int i;
printf("rtc_write buf: ");
for(i=0;i<7;++i)
printf("%02x ", 0xff & buf[i]);
printf("\n");
#endif
if( !i2c_rtc_write(DS1307, 0, buf, sizeof(buf)) )
{
printf("rtc_write error\n");
return (0);
}
return(1);
}
/// @brief Read DS1307 RTC into POSIX struct tm * structure.
///
/// @param[out] t: struct tm * POSIX time returned.
/// @return 1 on sucess.
/// @return 0 on fail.
uint8_t rtc_read(tm_t *t)
{
uint8_t buf[DS1307_REG_SIZE];
if( !i2c_rtc_read(DS1307, 0, buf, sizeof(buf)) )
{
printf("rtc_read error\n");
return (0);
}
#ifdef RTC_DEBUG
printf("%4x\n", t);
int i;
printf("rtc_read buf: ");
for(i=0;i<7;++i)
printf("%02x ", 0xff & buf[i]);
printf("\n");
#endif
t->tm_sec = BCDtoBIN( buf[0] & 0x7f);
t->tm_min = BCDtoBIN( buf[1] & 0x7f);
t->tm_hour = BCDtoBIN( buf[2] & 0x3f);
t->tm_wday = ( buf[3] & 0x07) - 1;
t->tm_mday = BCDtoBIN( buf[4] & 0x3f) ;
t->tm_mon= BCDtoBIN( buf[5] & 0x1f) - 1;
t->tm_year = BCDtoBIN( buf[6] & 0xff) + 100;
#ifdef RTC_DEBUG
printf("rtc_read():%d, day:%d,mon:%d,hour:%d,min:%d,sec:%d, wday:%d\n",
t->tm_year + 1900,
t->tm_mon,
t->tm_mday,
t->tm_hour,
t->tm_min,
t->tm_sec,
t->tm_wday);
#endif
return 1;
}
/// @brief Set DS1307 run state.
/// @param[in] run state.
/// - 1 = run.
/// - 0 = stop.
/// - -1 = check run state.
///
/// @return run state 0/1 on success.
/// @return -1 on error.
int rtc_run(int run)
{
uint8_t reg;
if( !i2c_rtc_read(DS1307, 0, (uint8_t *) ®, sizeof(reg)) )
{
printf("rtc_run read error\n");
return(-1);
}
if(run == -1)
return ((reg & 0x80) ? 0 : 1);
reg = ( reg & 0x7f) | (run ? 0 : 0x80);
if( !i2c_rtc_write(DS1307, 0, (uint8_t *) ®, sizeof(reg)) )
{
printf("rtc_run write error\n");
return -1;
}
return(run);
}
/// @brief DS1307 run test
///
/// - display error if read error occurs.
///
/// @return 1 if running.
/// @return 0 if not running.
/// @return -1 ERROR
int8_t rtc_run_test()
{
return ( rtc_run(-1) );
}
/// @brief Initialize DS1307 rtc if not initialied - or if forced.
///
/// @param[in] force: force initialiation flag.
/// - If 1 then alwasy force initialiation.
/// @param[in] seconds: POSIX EPOCH time in seconds.
///
/// @return run state 0 = STOP, 1 = RUN
/// @return -1 = ERROR
uint8_t rtc_init (int force, time_t seconds)
{
int8_t state;
tm_t *tmp;
i2c_rtc_init();
if(!force)
{
state = rtc_run(-1);
if(state < 0)
{
rtc_ok = 0;
return 0;
}
if(state == 0) // stopped
force = 1;
}
if(force) // INIT
{
if(rtc_run(0) < 0) // STOP RTC
{
rtc_ok = 0; // Fail
return 0;
}
tmp = gmtime(&seconds);
if(tmp == NULL || !rtc_write(tmp))
{
printf("rtc_init write epoch failed\n");
rtc_ok = 0;
return 0;
}
if(rtc_run(1) < 0) // START RTC
{
rtc_ok = 0; // Fail
return 0;
}
}
rtc_ok = 1;
return 1;
}
/// These are defined in my FatFS code support
/// @brief FAT time structure reference.
/// @see rtc.h
/// @see http://lxr.free-electrons.com/source/fs/fat/misc.c
/// @verbatim
/// typedef struct
/// {
/// WORD year; /* 2000..2099 */
/// BYTE month; /* 1..12 */
/// BYTE mday; /* 1.. 31 */
/// BYTE wday; /* 1..7 */
/// BYTE hour; /* 0..23 */
/// BYTE min; /* 0..59 */
/// BYTE sec; /* 0..59 */
/// } RTC;
/// @endverbatim
#if 0
/// @brief Convert Linux POSIX tm_t * to FAT32 time.
///
/// @param[in] t: POSIX struct tm * to convert.
///
/// @return FAT32 time.
uint32_t tm_to_fat(tm_t *t)
{
uint32_t fat;
/* Pack date and time into a uint32_t variable */
fat = ((uint32_t)(t->tm_year - 80) << 25)
| (((uint32_t)t->tm_mon+1) << 21)
| (((uint32_t)t->tm_mday) << 16)
| ((uint32_t)t->tm_hour << 11)
| ((uint32_t)t->tm_min << 5)
| ((uint32_t)t->tm_sec >> 1);
return(fat);
}
#endif
#if 0
/// @brief Read DS1307 RTC and convert to FAT32 time.
///
/// @return FAT32 time.
/// @see tm_to_fat().
uint32_t get_fattime (void)
{
tm_t t;
/* Get local time */
if(!rtc_read(&t))
return(0);
return( tm_to_fat(&t) );
}
#endif