annotate ds1307.c @ 62:e955aa7047ed

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