annotate ds1307.c @ 1:f5022e20d550

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