-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS3231M.c
360 lines (239 loc) · 6.41 KB
/
DS3231M.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
/**
* @file DS3231M.c
* @brief I2C Real-Time Clock API
* @author Klaus Kiefer, Peter Wegmann
* @version 1.4
* @date 2020/03/25
*
* A detailed documentation of the timing chip can be found in the <a href="DS3231M Datasheet.pdf" target="_blank"><b>DS3231M Datasheet</b></a>.
*/
#include <string.h>
#include <time.h>
#include <stdbool.h>
#include <stdlib.h>
#include <util/eu_dst.h>
#include "../config.h"
#include "DS3231M.h"
#include "status.h"
#ifndef UNIT_TEST
#include "I2C_utilities.h"
#include <util/eu_dst.h>
#else
#include <stdio.h>
#include "TestMocks/Mock_I2C_utilities.h"
#include "TestMocks/Mock_main.h"
#include "TestMocks/Mock_LCD.h"
#endif
/**
* @brief Time and Date
*
*
* Holds current Time (seconds, minutes and hours), and Date (days, months, and year)
*/
struct tm Time;
time_t sys_Time;
int8_t temp_MSB;
uint8_t temp_LSB;
double DS3231M_Temperature;
/**
* @brief Holds Function to print Infoline
*
* Paints an info message on the LCD
*/
void (*print_info_DS3231M)(char * ,bool ) = NULL;
/**
* @brief Initialization of the external device DS3231M, a time holding unit. It's connected via I2C (Two Wire Interface).
*
*
* @return uint8_t 0 on success and greater zero if initialization was unsuccessful
*
*/
uint8_t init_DS3231M(void (*printInfoFun)(char *,bool))
{
print_info_DS3231M = printInfoFun;
Time.tm_sec = 0;
Time.tm_min = 0;
Time.tm_hour = 0;
Time.tm_mday = 0;
Time.tm_mon = 0;
Time.tm_year = 0;
Time.tm_isdst =0;
uint8_t data[2];
if (I2C_read_from(DS3231M_address,DS3231M_address_temp_MSB,data,2))
{
return 1;
}
uint16_t Temp = ((uint16_t) data[0] << 8) | data[1];
if(Temp == 0){
return 1 ;
}
I2C_read_from(DS3231M_address,DS3231M_address_control_reg,data,1);
uint8_t contr_reg = data[0];
data[0] = contr_reg | DS3231M_control_reg_CONV ;
I2C_write_to(DS3231M_address,DS3231M_address_control_reg,data,1);
return 0;
}
uint8_t check_valid_ranges(struct tm * newtime){
if (
INRANGE(newtime->tm_sec,0,59) &&
INRANGE(newtime->tm_min,0,59) &&
INRANGE(newtime->tm_hour,0,23) &&
INRANGE(newtime->tm_mday,1,31) &&
INRANGE(newtime->tm_mon,1,12) &&
INRANGE(newtime->tm_year,0,99))
{
return 1;
}
return 0;
}
/**
* @brief Saves the time data given in a struct.
*
* @param newtime A pointer to a struct with the new time.
*
* @return void
*/
void DS3231M_set_time(struct tm *newtime)
{
//Funtrace_enter(12);
//FUNCTION_TRACE
if (!check_valid_ranges(newtime)){return;}
uint8_t data[7];
data[0] = ((newtime->tm_sec / 10) << 4) | (newtime->tm_sec % 10);
data[1] = ((newtime->tm_min / 10) << 4) | (newtime->tm_min % 10);
data[2] = ((newtime->tm_hour / 10) << 4) | (newtime->tm_hour % 10);
data[3] = 1;
data[4] = ((newtime->tm_mday / 10) << 4) | (newtime->tm_mday % 10);
data[5] = ((newtime->tm_mon / 10) << 4) | (newtime->tm_mon % 10);
data[6] = ((newtime->tm_year / 10) << 4) | (newtime->tm_year % 10);
//newtime->tm_year += 100;
struct tm Time_copy;
Time_copy.tm_sec = newtime->tm_sec;
Time_copy.tm_min = newtime->tm_min;
Time_copy.tm_hour = newtime->tm_hour;
Time_copy.tm_mday = newtime->tm_mday;
Time_copy.tm_mon = newtime->tm_mon - 1 ;
Time_copy.tm_year = newtime->tm_year +100;
Time_copy.tm_isdst = -1;
set_dst(&eu_dst);
set_zone(0);
set_system_time(mktime(&Time_copy));
if (!connected.TWI) {return;}
uint8_t i2c_ret_code = I2C_write_to(DS3231M_address,DS3231M_address_seconds, data, 7);
if(i2c_ret_code == I2C_ERROR ){
print_info_DS3231M("i2cERR",0);
connected.TWI = 0;
SET_ERROR(I2C_BUS_ERROR);
SET_ERROR(TIMER_ERROR);
_delay_ms(2000);
return;
}
if(i2c_ret_code == DEVICE_NOT_CONNECTED){
connected.DS3231M = 0;
SET_ERROR(I2C_BUS_ERROR);
SET_ERROR(TIMER_ERROR);
return;
}
}
/**
* @brief Reads the current time and saves the values in the time variables
*
*
* @return void
*/
void DS3231M_read_time(void)
{
//Funtrace_enter(13);
if (!connected.TWI) {
DS3231M_estimate_sys_Time();
return;
}
uint8_t data[7];
uint8_t i2c_ret_code = I2C_read_from(DS3231M_address,DS3231M_address_seconds,data,7);
if(i2c_ret_code == I2C_ERROR ){
print_info_DS3231M("i2cERR",0);
connected.TWI = 0;
SET_ERROR(I2C_BUS_ERROR);
SET_ERROR(TIMER_ERROR);
_delay_ms(2000);
DS3231M_estimate_sys_Time();
return;
}
if(i2c_ret_code == DEVICE_NOT_CONNECTED){
connected.DS3231M = 0;
SET_ERROR(I2C_BUS_ERROR);
SET_ERROR(TIMER_ERROR);
DS3231M_estimate_sys_Time();
return;
}
struct tm ds_Time;
ds_Time.tm_sec = decodeDS3231M(data[0]);
ds_Time.tm_min = decodeDS3231M(data[1]);
ds_Time.tm_hour = decodeDS3231M(data[2]);
ds_Time.tm_mday = decodeDS3231M(data[4]);
ds_Time.tm_mon = decodeDS3231M(data[5]);
ds_Time.tm_year = decodeDS3231M(data[6]);
if (DS3231M_concurrency_check(&ds_Time))
{
connected.DS3231M = 1;
connected.TWI =1;
CLEAR_ERROR(TIMER_ERROR);
memcpy(&Time,&ds_Time,sizeof(struct tm));
}else{
SET_ERROR(TIMER_ERROR);
DS3231M_estimate_sys_Time();
return;
}
}
void DS3231M_read_temperature(void)
{
uint8_t data[2];
uint8_t i2c_ret_code = I2C_read_from(DS3231M_address,DS3231M_address_temp_MSB,data,2);
if(i2c_ret_code == I2C_ERROR ){
print_info_DS3231M("i2cERR",0);
connected.TWI = 0;
SET_ERROR(I2C_BUS_ERROR);
SET_ERROR(TIMER_ERROR);
_delay_ms(2000);
return;
}
if(i2c_ret_code == DEVICE_NOT_CONNECTED){
connected.DS3231M = 0;
SET_ERROR(I2C_BUS_ERROR);
SET_ERROR(TIMER_ERROR);
return;
}
temp_MSB=data[0];
temp_LSB=data[1];
DS3231M_Temperature = ((float) temp_MSB + ((float) (temp_LSB >> 6) / 4) + 273);
}
uint8_t encodeDS3231M(uint8_t element){
return ((element / 10) << 4) | (element % 10);
}
uint8_t decodeDS3231M(uint8_t element){
return (element & 0x0F) + (element >> 4)*10;
}
void DS3231M_estimate_sys_Time(void){
time(&sys_Time);
struct tm * curr_t_est = localtime(&sys_Time);
Time.tm_sec = curr_t_est->tm_sec;
Time.tm_min = curr_t_est->tm_min;
Time.tm_hour = curr_t_est->tm_hour;
Time.tm_mday = curr_t_est->tm_mday;
Time.tm_mon = curr_t_est->tm_mon + 1; // tm_mon (0...11) but in ds3231m --> 1..12
Time.tm_year = curr_t_est->tm_year - 100; // tm_year (1900...) but in ds3231m --> 2000...
}
uint8_t DS3231M_concurrency_check( struct tm *ds_Time){
if (
!(INRANGE(ds_Time->tm_sec,0,59) &&
INRANGE(ds_Time->tm_min,0,59) &&
INRANGE(ds_Time->tm_hour,0,23) &&
INRANGE(ds_Time->tm_mday,1,31) &&
INRANGE(ds_Time->tm_mon,1,12) &&
INRANGE(ds_Time->tm_year,21,99))
)
{
return 0;
}
return 1;
}