-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS1343.c
369 lines (337 loc) · 9.84 KB
/
DS1343.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
/*
* DS1343.c
* NAME: REAL TIME CLOCK with alarm and ISR
* Created: 9/27/2018 10:15:10 AM
* Author: Sarthak
*/
#include <DS1343.h>
struct spi_device ds1343_ce ={
.id=IOPORT_CREATE_PIN(PORTA,4)
};
void ds1343_init(void)
{
spi_master_init(&SPIC);
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1); // spi mode 3 & Freq: 1MHz
spi_enable(&SPIC);
}
void ds1343_interrupt_init(void)
{
PORTF.INT1MASK |= 0x04; // making the pin no 4 as interrupt
PORTF.INTCTRL |= 0x08; // configuring interrupt for portE
}
void ds1343_interrupt_disable(void)
{
PORTF.INT1MASK |= 0x04; // making the pin no 4 as interrupt
PORTF.INTCTRL &= 0xF7; // configuring interrupt for portE
}
uint8_t ds1343_read(void)
{
uint8_t data;
while(!(SPIC.STATUS & 0x80));
data=SPIC_DATA;
return data;
}
void ds1343_write(uint8_t value)
{
SPIC.DATA=value;
while (!spi_is_rx_full(&SPIC));
}
void ds1343_time_configuration(void)
{
uint8_t ret;
uint8_t clk[6];
/* Array to store the user provided time to configure RTC every alternate bytes are address of RTC &
adjacent bytes will be value to store at particular address
*/
int get_clk[14]={0x80,' ',0x81,' ',0x82,' ',0x83,' ',0x84,' ',0x85,' ',0x86,' '};
int get_ala[8]={0x8b,' ',0x8c,' ',0x8d,' ',0x8e,' '};
clk[0]=(date_tmp.second);
clk[1]=(date_tmp.minute);
clk[2]=(date_tmp.hour);
clk[3]=(date_tmp.date)+1;
clk[4]=(date_tmp.month)+1;
clk[5]=(date_tmp.year)-2000; // only one byte is allocated for year. so subtracted by current century.
user_debug_1("please input sec,min,hr\r\n");
/* configuring the RTC timing */
for(int i=1,j=0;i<6;)
{
get_clk[i]=dec_to_hex(clk[j]);
user_debug_1("get_clk[%d]=%x\r\n",i,get_clk[i]);
j=j+1;
i=i+2;
}
for(int i=9,j=3;i<14;)
{
get_clk[i]=dec_to_hex(clk[j]);
user_debug_1("get_clk[%d]=%x\r\n",i,get_clk[i]);
j=j+1;
i=i+2;
}
/* writes to the RTC through SPI communication
configures clock, calendar.
*/
for(int i=0;i<14;)
{
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1);
spi_deselect_device(&SPIC,&ds1343_ce);
ds1343_write(get_clk[i++]);
ds1343_write(get_clk[i++]);
spi_select_device(&SPIC,&ds1343_ce);
}
delay_ms(100);
ds1343_alarm_write(0,ALARM_ONCE_SEC,get_ala);
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1);
spi_deselect_device(&SPIC,&ds1343_ce);
ds1343_write(0x8f);
ds1343_write(0x06);
spi_select_device(&SPIC,&ds1343_ce);
delay_ms(1);
spi_deselect_device(&SPIC,&ds1343_ce);
ds1343_write(0x90);
ds1343_write(0x00);
spi_select_device(&SPIC,&ds1343_ce);
}
void ds1343_interrupt_off(void)
{
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1);
spi_deselect_device(&SPIC,&ds1343_ce);
ds1343_write(0x8f);
ds1343_write(0x00);
spi_select_device(&SPIC,&ds1343_ce);
}
void ds1343_alarm_write(uint32_t interval,int alarm_conf,int *get_ala)
{
uint8_t add_rtc[4]={0x00,0x01,0x02,0x03};
uint8_t get_clk[4];
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1);
/* gets the current time from RTC to configure Alarm0 as per interval */
for(int i=0;i<4;i++)
{
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1);
spi_deselect_device(&SPIC,&ds1343_ce);
ds1343_write(add_rtc[i]);
ds1343_read();
ds1343_write(0x00);
get_clk[i]=ds1343_read();
spi_select_device(&SPIC,&ds1343_ce);
delay_ms(1);
}
get_ala[1]=get_clk[0];
get_ala[3]=get_clk[1];
get_ala[5]=get_clk[2];
get_ala[7]=get_clk[3];
for(int i=0;i<interval;i++)
{
if(get_clk[0]==0x59) //if second equal to 59 then roll off to 0.
{
get_ala[1]=0x00;
get_clk[0]=0x00;
if(get_clk[1] == 0x59) // if minutes equal to 59 then roll off to 0.
{
get_ala[3]=0x00;
get_clk[1]=0x00;
if(get_clk[2]==0x23) // if hour equal to 23 then roll off to 0
{
get_ala[5] = 0x00;
get_clk[2]=0x00;
if(get_clk[3]==0x07) // if week equal to 7 then roll off to 0
{
get_ala[7]==0x00;
get_clk[3]=0x00;
}
else
{
get_clk[3] = get_clk[3] + 0x01; // increment the day by one.
get_ala[7]= get_clk[3];
}
}
else if(get_clk[2]==0x09||get_clk[2]==0x19) //if hour equal to 9 or 19 then BCD roll off to 10 or 20.
{
get_ala[5] = get_clk[2] & 0xf0;
get_ala[5] = get_ala[5] + 0x10;
get_clk[2] = get_ala[5];
}
else
{
get_clk[2] = get_clk[2] + 0x01; // increment the hour by one.
get_ala[5]= get_clk[2];
}
}
/* if minutes equal to 9 / 19 / 29 / 39 / 49 then BCD roll off to 10 / 20 / 30 / 40 / 50.*/
else if(get_clk[1] == 0x09 || get_clk[1] == 0x19 || get_clk[1] == 0x29 || get_clk[1] == 0x39 || get_clk[1] == 0x49)
{
get_ala[3] = get_clk[1] & 0xf0;
get_ala[3] = get_ala[3] + 0x10;
get_clk[1] = get_ala[3];
}
else
{
get_clk[1] = get_clk[1] + 0x01; // increment minute by one.
get_ala[3]= get_clk[1];
}
}
/* if seconds equal to 9 / 19 / 29 / 39 / 49 then BCD roll off to 10 / 20 / 30 / 40 / 50.*/
else if(get_clk[0] == 0x09 || get_clk[0] == 0x19 || get_clk[0] == 0x29 || get_clk[0] == 0x39 || get_clk[0] == 0x49)
{
get_ala[1] = get_clk[0] & 0xf0;
get_ala[1] = get_ala[1] + 0x10;
get_clk[0] = get_ala[1];
}
else
{
get_clk[0] = get_clk[0] + 0x01; // increment second by one
get_ala[1]= get_clk[0];
}
}
for(int i=1;i<8;)
{
user_debug_1("the alarm is %x\r\n",get_ala[i]);
i=i+2;
}
switch(alarm_conf)
{
case ALARM_ONCE_SEC:
for(int i=0;i<8;)
{
// configure Alarm0 as every second
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1);
spi_deselect_device(&SPIC,&ds1343_ce);
ds1343_write(get_ala[i++]);
ds1343_write(get_ala[i++]|0x80);
spi_select_device(&SPIC,&ds1343_ce);
}
break;
case ALARM_DHMS_MATCH:
for(int i=0;i<8;)
{
// configure Alarm0 as day, hour, minutes & seconds match with the RTC clock
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1);
spi_deselect_device(&SPIC,&ds1343_ce);
ds1343_write(get_ala[i++]);
ds1343_write(get_ala[i++]);
spi_select_device(&SPIC,&ds1343_ce);
}
break;
case ALARM_HMS_MATCH:
for(int i=0;i<8;)
{
// configure Alarm0 as hour, minutes & seconds match with the RTC clock
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1);
spi_deselect_device(&SPIC,&ds1343_ce);
if(get_ala[i]==0x8A)
{
ds1343_write(get_ala[i++]);
ds1343_write(get_ala[i++] + 0x80);
}
else
{
ds1343_write(get_ala[i++]);
ds1343_write(get_ala[i++]);
}
spi_select_device(&SPIC,&ds1343_ce);
}
break;
case ALARM_MS_MATCH:
for(int i=0;i<8;)
{
// configure Alarm0 as minutes & seconds match with the RTC clock
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1);
spi_deselect_device(&SPIC,&ds1343_ce);
if(get_ala[i]==0x89 || get_ala[i]==0x8a)
{
ds1343_write(get_ala[i++]);
ds1343_write(get_ala[i++] + 0x80);
}
else
{
ds1343_write(get_ala[i++]);
ds1343_write(get_ala[i++]);
}
spi_select_device(&SPIC,&ds1343_ce);
}
break;
case ALARM_SEC_MATCH:
for(int i=0;i<8;)
{
// configure Alarm0 as seconds match with the RTC clock
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1);
spi_deselect_device(&SPIC,&ds1343_ce);
if(get_ala[i]==0x87)
{
ds1343_write(get_ala[i++]);
ds1343_write(get_ala[i++]);
}
else
{
ds1343_write(get_ala[i++]);
ds1343_write(get_ala[i++] + 0x80);
}
spi_select_device(&SPIC,&ds1343_ce);
}
break;
default:
break;
}
}
void get_ds1343_time(void)
{
uint8_t add_rtc[6]={0x00,0x01,0x02,0x04,0x05,0x06};
uint8_t get_clk[6];
ds1343_init();
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1);
/* gets the current time from RTC to configure Alarm0 as per interval */
for(int i=0;i<6;i++)
{
spi_deselect_device(&SPIC,&ds1343_ce);
ds1343_write(add_rtc[i]);
ds1343_read();
ds1343_write(0x00);
get_clk[i]=ds1343_read();
spi_select_device(&SPIC,&ds1343_ce);
user_debug_1("the system clock is %x\r\n",get_clk[i]);
delay_ms(1);
}
date_tmp.second = hex_to_dec(get_clk[0],sizeof(get_clk[0]));
date_tmp.minute = hex_to_dec(get_clk[1],sizeof(get_clk[1]));
date_tmp.hour = hex_to_dec(get_clk[2],sizeof(get_clk[2]));
date_tmp.date = hex_to_dec(get_clk[3],sizeof(get_clk[3]))-1;
date_tmp.month = hex_to_dec(get_clk[4],sizeof(get_clk[4]))-1;
date_tmp.year = hex_to_dec(get_clk[5],sizeof(get_clk[5]))+2000; // only one byte is allocated for year. so subtracted by current century.
}
void Ack_ds1343_interrupt(void)
{
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3, 1000000,1);
spi_deselect_device(&SPIC,&ds1343_ce);
ds1343_write(0x90);
ds1343_write(0xFD);
spi_select_device(&SPIC,&ds1343_ce);
spi_master_setup_device(&SPIC, &ds1343_ce,SPI_MODE_3,1000000,1); // setting it to 1Mhz.
}
void service_rtc_interrupt(void)
{
if (rtc_commn_diff<=(ds1343_tick-rtc_commn_int))
{
rtc_commn_int=ds1343_tick;
rtc_1_isr=1;
}
if(rtc_rcd_diff<=(ds1343_tick-rtc_rcd_int))
{
rtc_rcd_int=ds1343_tick;
rtc_2_isr=1;
}
if(screen_diff<=(ds1343_tick-screen))
{
rtc_0_isr=1;
screen=ds1343_tick;
}
if(rtc_sd_diff<=(ds1343_tick-rtc_sd_int))
{
rtc_3_isr=1;
rtc_sd_int=ds1343_tick;
}
if(rtc_smp_diff<=(ds1343_tick-rtc_smp_int))
{
rtc_4_isr=1;
rtc_smp_int=ds1343_tick;
}
}