comparison ds1307.c @ 60:50fca9562310

Add support for reading/writing to a DS1307 over TWI/IIC. Note that iic_read() appears to have a bug where it stops 1 byte earlier than expected, work around it for now by adding 1 in the gettod() function. tempctrl.c now prints the TOD as read from the RTC for each line.
author darius@Inchoate
date Thu, 30 Oct 2008 11:53:32 +1030
parents
children 0910ab6f0095
comparison
equal deleted inserted replaced
59:c72cf25881fe 60:50fca9562310
1 /*
2 * Interface to a DS1307
3 *
4 * Copyright (c) 2008
5 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <inttypes.h>
32 #include <avr/io.h>
33 #include <avr/pgmspace.h>
34 #include <util/twi.h>
35 #include <util/delay.h>
36
37 #include "ds1307.h"
38
39 //#define TWDEBUG
40
41 /*
42 * ds1307_init
43 *
44 * Setup TWI interface
45 *
46 */
47 int
48 ds1307_init(void) {
49 TWSR = 0; /* TWI Prescaler = 1 */
50
51 #if F_CPU < 3600000UL
52 TWBR = 10; /* Smallest valid TWBR */
53 #else
54 TWBR = (F_CPU / 100000UL - 16) / 2;
55 #endif
56
57 TWCR = _BV(TWEN);
58
59 return(0);
60 }
61
62 /*
63 * iic_read
64 *
65 * Read len bytes of data from address adr in slave sla into
66 * data. Presume that the slave auto-increments the address on
67 * successive reads.
68 *
69 * Returns the number of bytes read, or the following on failure.
70 * IIC_STFAIL Could generate START condition (broken bus or busy).
71 * IIC_FAILARB Failed bus arbitration.
72 * IIC_SLNAK Slave NAK'd.
73 * IIC_NOREPLY No reply (no such slave?)
74 * IIC_UNKNOWN Unexpected return from TWI reg.
75 */
76 int
77 iic_read(uint8_t *data, uint8_t len, uint8_t adr, uint8_t sla) {
78 uint8_t twst, twcr, cnt;
79
80 /* Generate START */
81 TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
82
83 /* Spin waiting for START to be generated */
84 while ((TWCR & _BV(TWINT)) == 0)
85 ;
86 switch (twst = TW_STATUS) {
87 case TW_REP_START: /* OK but shouldn't happen */
88 case TW_START:
89 break;
90
91 case TW_MT_ARB_LOST:
92 return IIC_FAILARB;
93 break;
94
95 default:
96 /* Not in start condition, bail */
97 return IIC_UNKNOWN;
98 }
99 #ifdef TWDEBUG
100 printf_P(PSTR("Sent START\r\n"));
101 #endif
102 /* Send SLA+W */
103 TWDR = sla | TW_WRITE;
104 TWCR = _BV(TWINT) | _BV(TWEN);
105
106 /* Spin waiting for a response to be generated */
107 while ((TWCR & _BV(TWINT)) == 0)
108 ;
109
110 #ifdef TWDEBUG
111 printf_P(PSTR("Sent SLA+W\r\n"));
112 #endif
113 switch (twst = TW_STATUS) {
114 case TW_MT_SLA_ACK:
115 break;
116
117 case TW_MT_SLA_NACK:
118 /* Send STOP */
119 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
120 return IIC_SLNAK;
121
122 case TW_MT_ARB_LOST:
123 return IIC_FAILARB;
124 break;
125
126 default:
127 /* Send STOP */
128 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
129 return IIC_UNKNOWN;
130 }
131 /* Send address */
132 TWDR = adr;
133 TWCR = _BV(TWINT) | _BV(TWEN);
134
135 /* Spin waiting for a response to be generated */
136 while ((TWCR & _BV(TWINT)) == 0)
137 ;
138 #ifdef TWDEBUG
139 printf_P(PSTR("Sent address\r\n"));
140 #endif
141 switch ((twst = TW_STATUS)) {
142 case TW_MT_DATA_ACK:
143 break;
144
145 case TW_MT_DATA_NACK:
146 /* Send STOP */
147 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
148 return IIC_SLNAK;
149
150 case TW_MT_ARB_LOST:
151 return IIC_FAILARB;
152
153 default:
154 /* Send STOP */
155 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
156 return IIC_UNKNOWN;
157 }
158
159 /* Master receive cycle */
160 TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
161 while ((TWCR & _BV(TWINT)) == 0) /* wait for transmission */
162 ;
163
164 #ifdef TWDEBUG
165 printf_P(PSTR("Sent START\r\n"));
166 #endif
167 switch ((twst = TW_STATUS)) {
168 case TW_REP_START: /* OK but shouldn't happen */
169 case TW_START:
170 break;
171
172 case TW_MT_ARB_LOST:
173 return IIC_FAILARB;
174
175 default:
176 /* Send STOP */
177 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
178 return IIC_UNKNOWN;
179 }
180
181 /* send SLA+R */
182 TWDR = sla | TW_READ;
183 TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
184
185 /* Spin waiting for a response to be generated */
186 while ((TWCR & _BV(TWINT)) == 0)
187 ;
188 #ifdef TWDEBUG
189 printf_P(PSTR("Sent SLA+R\r\n"));
190 #endif
191 switch ((twst = TW_STATUS)) {
192 case TW_MR_SLA_ACK:
193 break;
194
195 case TW_MR_SLA_NACK:
196 /* Send STOP */
197 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
198 return IIC_SLNAK;
199
200 case TW_MR_ARB_LOST:
201 return IIC_FAILARB;
202
203 default:
204 /* Send STOP */
205 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
206 return IIC_UNKNOWN;
207 }
208
209 cnt = 0;
210 for (twcr = _BV(TWINT) | _BV(TWEN) | _BV(TWEA);
211 len > 0; len--) {
212 /* Send NAK on last byte */
213 if (len == 1)
214 twcr = _BV(TWINT) | _BV(TWEN);
215 TWCR = twcr; /* clear int to start transmission */
216 /* Spin waiting for a response to be generated */
217 while ((TWCR & _BV(TWINT)) == 0)
218 ;
219 #ifdef TWDEBUG
220 printf_P(PSTR("Data request done\r\n"));
221 #endif
222 switch ((twst = TW_STATUS)) {
223 case TW_MR_DATA_NACK:
224 /* Send STOP */
225 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
226 return cnt;
227
228 case TW_MR_DATA_ACK:
229 *data++ = TWDR;
230 cnt++;
231 break;
232
233 default:
234 return IIC_UNKNOWN;
235 }
236 }
237
238 /* Send STOP */
239 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
240 return cnt;
241 }
242
243 /*
244 * iic_write
245 *
246 * Write len bytes of data from address adr in slave sla into
247 * data. Presume that the slave auto-increments the address on
248 * successive writes.
249 *
250 * Returns the number of bytes read, or the following on failure.
251 * IIC_STFAIL Could generate START condition (broken bus or busy).
252 * IIC_FAILARB Failed bus arbitration.
253 * IIC_SLNAK Slave NAK'd.
254 * IIC_NOREPLY No reply (no such slave?)
255 * IIC_UNKNOWN Unexpected return from TWI reg.
256 */
257 int
258 iic_write(uint8_t *data, uint8_t len, uint8_t adr, uint8_t sla) {
259 uint8_t twst, cnt;
260
261 /* Generate START */
262 TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
263
264 /* Spin waiting for START to be generated */
265 while ((TWCR & _BV(TWINT)) == 0)
266 ;
267 switch (twst = TW_STATUS) {
268 case TW_REP_START: /* OK but shouldn't happen */
269 case TW_START:
270 break;
271
272 case TW_MT_ARB_LOST:
273 return IIC_FAILARB;
274 break;
275
276 default:
277 /* Not in start condition, bail */
278 return IIC_UNKNOWN;
279 }
280 #ifdef TWDEBUG
281 printf_P(PSTR("Sent START\r\n"));
282 #endif
283
284 /* Send SLA+W */
285 TWDR = sla | TW_WRITE;
286 TWCR = _BV(TWINT) | _BV(TWEN);
287
288 /* Spin waiting for a response to be generated */
289 while ((TWCR & _BV(TWINT)) == 0)
290 ;
291
292 #ifdef TWDEBUG
293 printf_P(PSTR("Sent SLA+W\r\n"));
294 #endif
295 switch (twst = TW_STATUS) {
296 case TW_MT_SLA_ACK:
297 break;
298
299 case TW_MT_SLA_NACK:
300 /* Send STOP */
301 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
302 return IIC_SLNAK;
303
304 case TW_MT_ARB_LOST:
305 return IIC_FAILARB;
306 break;
307
308 default:
309 /* Send STOP */
310 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
311 return IIC_UNKNOWN;
312 }
313 /* Send address */
314 TWDR = adr;
315 TWCR = _BV(TWINT) | _BV(TWEN);
316
317 /* Spin waiting for a response to be generated */
318 while ((TWCR & _BV(TWINT)) == 0)
319 ;
320 #ifdef TWDEBUG
321 printf_P(PSTR("Sent address\r\n"));
322 #endif
323 switch ((twst = TW_STATUS)) {
324 case TW_MT_DATA_ACK:
325 break;
326
327 case TW_MT_DATA_NACK:
328 /* Send STOP */
329 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
330 return IIC_SLNAK;
331
332 case TW_MT_ARB_LOST:
333 return IIC_FAILARB;
334
335 default:
336 /* Send STOP */
337 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
338 return IIC_UNKNOWN;
339 }
340
341 cnt = 0;
342 for (; len > 0; len--) {
343 TWDR = *data++;
344 TWCR = _BV(TWINT) | _BV(TWEN);
345
346 /* Spin waiting for a response to be generated */
347 while ((TWCR & _BV(TWINT)) == 0)
348 ;
349 #ifdef TWDEBUG
350 printf_P(PSTR("Data sent\r\n"));
351 #endif
352 switch ((twst = TW_STATUS)) {
353 case TW_MT_DATA_NACK:
354 /* Send STOP */
355 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
356 return cnt;
357
358 case TW_MT_DATA_ACK:
359 cnt++;
360 break;
361
362 default:
363 return IIC_UNKNOWN;
364 }
365 }
366
367 /* Send STOP */
368 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
369 return cnt;
370 }
371
372 /*
373 * ds1307_gettod
374 *
375 * Read time of day from DS1307 into time
376 *
377 * Note that we canonify to 24hr mode.
378 *
379 */
380 int
381 ds1307_gettod(ds1307raw_t *time) {
382 int len;
383
384 len = iic_read((uint8_t *)time, sizeof(ds1307raw_t) + 1, 0, DS1307_ADR);
385 if (len < 0) {
386 printf_P(PSTR("iic_read failed - %d\r\n"), len);
387 return(0);
388 }
389
390 #if 1
391 if (len != sizeof(ds1307raw_t)) {
392 printf_P(PSTR("Couldn't read from RTC, got %d bytes (vs %d)\r\n"), len, sizeof(ds1307raw_t));
393 return(0);
394 }
395 #endif
396
397 #ifdef TWDEBUG
398 int i;
399
400 for (i = 0; i < len; i++)
401 printf_P(PSTR("0x%02x: 0x%02x\r\n"), i, *(((uint8_t *)time) + i));
402 #endif
403
404 return(1);
405 }
406
407 /*
408 * ds1307_settod
409 *
410 * Set the DS1307 with the supplied time, format like so
411 * sc 2008/10/29 23:45:30
412 *
413 */
414 int
415 ds1307_settod(char *date) {
416 ds1307raw_t rtime;
417 int i, year, month, day, hour, min, sec;
418
419 if ((i = sscanf_P(date, PSTR("%d/%d/%d %d:%d:%d"), &year, &month, &day, &hour, &min, &sec)) != 6) {
420 printf_P(PSTR("Couldn't parse date, got %d\r\n"), i);
421 return(0);
422 }
423
424 if (year > 1900)
425 year -= 1900;
426
427 rtime.split.year10 = year / 10;
428 rtime.split.year = year % 10;
429 rtime.split.month10 = month / 10;
430 rtime.split.month = month % 10;
431 rtime.split.day10 = day / 10;
432 rtime.split.day = day % 10;
433 rtime.split.pmam = ((hour / 10) & 0x02) >> 1;
434 rtime.split.hour10 = (hour / 10) & 0x01;
435 rtime.split.hour = hour % 10;
436 rtime.split.min10 = min / 10;
437 rtime.split.min = min % 10;
438 rtime.split.sec10 = sec / 10;
439 rtime.split.sec = sec % 10;
440
441 rtime.split.ch = 0; // Enable clock
442 rtime.split.s1224 = 0; // 24 hour mode
443 rtime.split.dow = 0; // XXX: unused
444 rtime.split.out = 0; // No clock out
445
446 #if TWDEBUG
447 for (i = 0; i < sizeof(ds1307raw_t); i++)
448 printf_P(PSTR("0x%02x: 0x%02x\r\n"), i, *(((uint8_t *)rtime) + i));
449 #endif
450 if ((i = iic_write((uint8_t *)&rtime, sizeof(ds1307raw_t), 0, DS1307_ADR)) != sizeof(ds1307raw_t))
451 printf_P(PSTR("Unable to write to RTC, only sent %d (vs %d)\r\n"), i, sizeof(ds1307raw_t));
452
453 return(1);
454 }
455
456 /*
457 * ds1307_printtime
458 *
459 * Print the time in rtime with trailer
460 *
461 */
462 void
463 ds1307_printtime(char *leader, char *trailer) {
464 ds1307raw_t rtime;
465 uint8_t hour;
466
467 if (ds1307_gettod(&rtime) != 1)
468 return;
469
470 // Handle 12/24 hour time
471 hour = rtime.split.hour10 * 10 + rtime.split.hour;
472 if (rtime.split.s1224) {
473 if (rtime.split.pmam)
474 hour += 12;
475 } else
476 hour += (rtime.split.pmam << 1) * 10;
477
478 printf_P(PSTR("%S%04d/%02d/%d %02d:%02d:%02d%S"), leader,
479 1900 + rtime.split.year10 * 10 + rtime.split.year,
480 rtime.split.month10 * 10 + rtime.split.month,
481 rtime.split.day10 * 10 + rtime.split.day,
482 hour,
483 rtime.split.min10 * 10 + rtime.split.min,
484 rtime.split.sec10 * 10 + rtime.split.sec,
485 trailer);
486 }