annotate ds1307.c @ 7:d111f64eb619

Add example 1wire-config.h file for other people to look at.
author darius@inchoate.localdomain
date Mon, 20 Apr 2009 14:05:20 +0800
parents 3da232f97e81
children 119688bb743f
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) {
3
15d89caaf516 Add support for single UART devices, although untested apart from a compile.
darius@Inchoate
parents: 0
diff changeset
49 #ifdef PRR
6
3da232f97e81 Invert the bit mask, we only want to turn off this bit, not everything.
darius@inchoate.localdomain
parents: 3
diff changeset
50 PRR &= ~_BV(PRTWI); /* Power TWI on - note that the
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
51 * datasheet says this is already 0 at
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
52 * power on.. */
3
15d89caaf516 Add support for single UART devices, although untested apart from a compile.
darius@Inchoate
parents: 0
diff changeset
53 #endif
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
54 TWSR = 0; /* TWI Prescaler = 1 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
55 #if F_CPU < 3600000UL
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
56 TWBR = 10; /* Smallest valid TWBR */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
57 #else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
58 TWBR = (F_CPU / 100000UL - 16) / 2;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
59 #endif
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 TWCR = _BV(TWEN);
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 return(0);
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
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 * iic_read
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
68 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
69 * 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
70 * data. Presume that the slave auto-increments the address on
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
71 * successive reads.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
72 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
73 * 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
74 * IIC_STFAIL Could generate START condition (broken bus or busy).
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
75 * IIC_FAILARB Failed bus arbitration.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
76 * IIC_SLNAK Slave NAK'd.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
77 * IIC_NOREPLY No reply (no such slave?)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
78 * IIC_UNKNOWN Unexpected return from TWI reg.
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 * Heaviy cribbed from twitest.c by Joerg Wunsch
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
81 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
82 int8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
83 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
84 uint8_t twst, twcr, cnt;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
85
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
86 /* Generate START */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
87 TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
88
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
89 /* Spin waiting for START to be generated */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
90 while ((TWCR & _BV(TWINT)) == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
91 ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
92 switch (twst = TW_STATUS) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
93 case TW_REP_START: /* OK but shouldn't happen */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
94 case TW_START:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
95 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
96
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
97 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
98 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
99 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
100
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
101 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
102 /* Not in start condition, bail */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
103 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
104 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
105 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
106 printf_P(PSTR("Sent START\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
107 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
108 /* Send SLA+W */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
109 TWDR = sla | TW_WRITE;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
110 TWCR = _BV(TWINT) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
111
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
112 /* Spin waiting for a response to be generated */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
113 while ((TWCR & _BV(TWINT)) == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
114 ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
115
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
116 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
117 printf_P(PSTR("Sent SLA+W\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
118 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
119 switch (twst = TW_STATUS) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
120 case TW_MT_SLA_ACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
121 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
122
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
123 case TW_MT_SLA_NACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
124 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
125 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
126 return IIC_SLNAK;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
127
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
128 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
129 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
130 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
131
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
132 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
133 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
134 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
135 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
136 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
137 /* Send address */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
138 TWDR = adr;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
139 TWCR = _BV(TWINT) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
140
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
141 /* Spin waiting for a response to be generated */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
142 while ((TWCR & _BV(TWINT)) == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
143 ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
144 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
145 printf_P(PSTR("Sent address\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
146 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
147 switch ((twst = TW_STATUS)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
148 case TW_MT_DATA_ACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
149 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
150
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
151 case TW_MT_DATA_NACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
152 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
153 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
154 return IIC_SLNAK;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
155
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
156 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
157 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
158
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
159 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
160 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
161 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
162 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
163 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
164
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
165 /* Master receive cycle */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
166 TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
167 while ((TWCR & _BV(TWINT)) == 0) /* wait for transmission */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
168 ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
169
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
170 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
171 printf_P(PSTR("Sent START\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
172 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
173 switch ((twst = TW_STATUS)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
174 case TW_REP_START: /* OK but shouldn't happen */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
175 case TW_START:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
176 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
177
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
178 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
179 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
180
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
181 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
182 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
183 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
184 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
185 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
186
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
187 /* send SLA+R */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
188 TWDR = sla | TW_READ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
189 TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
190
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
191 /* Spin waiting for a response to be generated */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
192 while ((TWCR & _BV(TWINT)) == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
193 ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
194 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
195 printf_P(PSTR("Sent SLA+R\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
196 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
197 switch ((twst = TW_STATUS)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
198 case TW_MR_SLA_ACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
199 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
200
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
201 case TW_MR_SLA_NACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
202 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
203 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
204 return IIC_SLNAK;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
205
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
206 case TW_MR_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
207 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
208
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
209 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
210 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
211 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
212 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
213 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
214
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
215 cnt = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
216 for (twcr = _BV(TWINT) | _BV(TWEN) | _BV(TWEA);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
217 len > 0; len--) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
218 /* Send NAK on last byte */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
219 if (len == 1)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
220 twcr = _BV(TWINT) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
221 TWCR = twcr; /* clear int to start transmission */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
222 /* Spin waiting for a response to be generated */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
223 while ((TWCR & _BV(TWINT)) == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
224 ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
225 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
226 printf_P(PSTR("Data request done\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
227 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
228 switch ((twst = TW_STATUS)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
229 case TW_MR_DATA_NACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
230 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
231 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
232 //printf_P(PSTR("NACK on byte %d\r\n"), cnt);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
233 return cnt;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
234
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
235 case TW_MR_DATA_ACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
236 *data++ = TWDR;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
237 //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
238 cnt++;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
239 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
240
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
241 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
242 return IIC_UNKNOWN;
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 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
245
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
246 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
247 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
248 return cnt;
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
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 * iic_write
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
253 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
254 * 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
255 * data. Presume that the slave auto-increments the address on
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
256 * successive writes.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
257 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
258 * 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
259 * IIC_STFAIL Could generate START condition (broken bus or busy).
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
260 * IIC_FAILARB Failed bus arbitration.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
261 * IIC_SLNAK Slave NAK'd.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
262 * IIC_NOREPLY No reply (no such slave?)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
263 * IIC_UNKNOWN Unexpected return from TWI reg.
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 * Heaviy cribbed from twitest.c by Joerg Wunsch
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
266 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
267 int8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
268 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
269 uint8_t twst, cnt;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
270
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
271 /* Generate START */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
272 TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
273
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
274 /* Spin waiting for START to be generated */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
275 while ((TWCR & _BV(TWINT)) == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
276 ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
277 switch (twst = TW_STATUS) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
278 case TW_REP_START: /* OK but shouldn't happen */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
279 case TW_START:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
280 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
281
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
282 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
283 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
284 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
285
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
286 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
287 /* Not in start condition, bail */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
288 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
289 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
290 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
291 printf_P(PSTR("Sent START\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
292 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
293
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
294 /* Send SLA+W */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
295 TWDR = sla | TW_WRITE;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
296 TWCR = _BV(TWINT) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
297
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
298 /* Spin waiting for a response to be generated */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
299 while ((TWCR & _BV(TWINT)) == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
300 ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
301
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
302 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
303 printf_P(PSTR("Sent SLA+W\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
304 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
305 switch (twst = TW_STATUS) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
306 case TW_MT_SLA_ACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
307 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
308
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
309 case TW_MT_SLA_NACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
310 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
311 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
312 return IIC_SLNAK;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
313
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
314 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
315 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
316 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
317
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
318 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
319 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
320 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
321 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
322 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
323 /* Send address */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
324 TWDR = adr;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
325 TWCR = _BV(TWINT) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
326
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
327 /* Spin waiting for a response to be generated */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
328 while ((TWCR & _BV(TWINT)) == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
329 ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
330 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
331 printf_P(PSTR("Sent address\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
332 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
333 switch ((twst = TW_STATUS)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
334 case TW_MT_DATA_ACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
335 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
336
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
337 case TW_MT_DATA_NACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
338 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
339 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
340 return IIC_SLNAK;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
341
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
342 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
343 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
344
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
345 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
346 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
347 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
348 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
349 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
350
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
351 cnt = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
352 for (; len > 0; len--) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
353 TWDR = *data++;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
354 TWCR = _BV(TWINT) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
355
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
356 /* Spin waiting for a response to be generated */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
357 while ((TWCR & _BV(TWINT)) == 0)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
358 ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
359 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
360 printf_P(PSTR("Data sent\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
361 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
362 switch ((twst = TW_STATUS)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
363 case TW_MT_DATA_NACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
364 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
365 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
366 return cnt;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
367
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
368 case TW_MT_DATA_ACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
369 cnt++;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
370 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
371
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
372 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
373 return IIC_UNKNOWN;
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 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
376
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
377 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
378 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
379 return cnt;
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
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 * ds1307_gettod
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 * Read time of day from DS1307 into time
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 * Note that we canonify to 24hr mode.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
388 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
389 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
390 int8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
391 ds1307_gettod(ds1307raw_t *time) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
392 int8_t len;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
393
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
394 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
395 if (len < 0) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
396 printf_P(PSTR("iic_read failed - %d\r\n"), len);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
397 return(0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
398 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
399
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
400 #if 1
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
401 if (len != sizeof(ds1307raw_t)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
402 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
403 return(0);
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 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
406
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
407 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
408 int i;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
409
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
410 for (i = 0; i < len; i++)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
411 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
412 #endif
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 return(1);
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
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 * ds1307_settod
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
419 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
420 * Set the DS1307 with the supplied time, format like so
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
421 * sc 2008/10/29 23:45:30
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
422 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
423 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
424 int8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
425 ds1307_settod(char *date) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
426 ds1307raw_t rtime;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
427 uint16_t year;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
428 uint8_t i, month, day, hour, min, sec;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
429
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
430 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
431 printf_P(PSTR("Can't parse date\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
432 return(0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
433 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
434
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
435 if (year > 1900)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
436 year -= 1900;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
437
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
438 rtime.split.year10 = year / 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
439 rtime.split.year = year % 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
440 rtime.split.month10 = month / 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
441 rtime.split.month = month % 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
442 rtime.split.day10 = day / 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
443 rtime.split.day = day % 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
444 rtime.split.pmam = ((hour / 10) & 0x02) >> 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
445 rtime.split.hour10 = (hour / 10) & 0x01;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
446 rtime.split.hour = hour % 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
447 rtime.split.min10 = min / 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
448 rtime.split.min = min % 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
449 rtime.split.sec10 = sec / 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
450 rtime.split.sec = sec % 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
451
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
452 rtime.split.ch = 0; // Enable clock
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
453 rtime.split.s1224 = 0; // 24 hour mode
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
454 rtime.split.dow = 0; // XXX: unused
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
455 rtime.split.out = 0; // No clock out
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
456
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
457 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
458 for (i = 0; i < sizeof(ds1307raw_t); i++)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
459 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
460 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
461 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
462 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
463
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
464 return(1);
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
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 * ds1307_printtime
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 * Print the time in rtime with trailer
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
471 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
472 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
473 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
474 ds1307_printtime(char *leader, char *trailer) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
475 ds1307raw_t rtime;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
476 uint8_t hour;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
477
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
478 if (ds1307_gettod(&rtime) != 1)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
479 return;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
480
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
481 // Handle 12/24 hour time
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
482 hour = rtime.split.hour10 * 10 + rtime.split.hour;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
483 if (rtime.split.s1224) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
484 if (rtime.split.pmam)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
485 hour += 12;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
486 } else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
487 hour += (rtime.split.pmam << 1) * 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
488
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
489 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
490 1900 + rtime.split.year10 * 10 + rtime.split.year,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
491 rtime.split.month10 * 10 + rtime.split.month,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
492 rtime.split.day10 * 10 + rtime.split.day,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
493 hour,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
494 rtime.split.min10 * 10 + rtime.split.min,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
495 rtime.split.sec10 * 10 + rtime.split.sec,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
496 trailer);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
497 }