annotate ds1307.c @ 19:b5e4591b6570

- Satisfy new compiler - Put strings in PROGMEM - Add various time functions
author Daniel O'Connor <darius@dons.net.au>
date Sat, 31 Jan 2015 23:27:10 +1030
parents 119688bb743f
children
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
8
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
41 /* Helper code to wait for the TWCR to do something */
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
42 #define WAITFORTWINT() do { \
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
43 uint8_t i; \
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
44 for (i = 0; i < 255 && (TWCR & _BV(TWINT)) == 0; i++) \
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
45 _delay_ms(1); \
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
46 if (i == 255) \
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
47 return(IIC_NORESP); \
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
48 } while (0)
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
49
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
50
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
51 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
52 * ds1307_init
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
53 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
54 * Setup TWI interface
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
55 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
56 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
57 int
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
58 ds1307_init(void) {
3
15d89caaf516 Add support for single UART devices, although untested apart from a compile.
darius@Inchoate
parents: 0
diff changeset
59 #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
60 PRR &= ~_BV(PRTWI); /* Power TWI on - note that the
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
61 * datasheet says this is already 0 at
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
62 * power on.. */
3
15d89caaf516 Add support for single UART devices, although untested apart from a compile.
darius@Inchoate
parents: 0
diff changeset
63 #endif
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
64 TWSR = 0; /* TWI Prescaler = 1 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
65 #if F_CPU < 3600000UL
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
66 TWBR = 10; /* Smallest valid TWBR */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
67 #else
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
68 TWBR = (F_CPU / 100000UL - 16) / 2;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
69 #endif
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 TWCR = _BV(TWEN);
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 return(0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
74 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
75
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
76 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
77 * iic_read
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
78 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
79 * 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
80 * data. Presume that the slave auto-increments the address on
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
81 * successive reads.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
82 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
83 * 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
84 * IIC_STFAIL Could generate START condition (broken bus or busy).
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
85 * IIC_FAILARB Failed bus arbitration.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
86 * IIC_SLNAK Slave NAK'd.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
87 * IIC_NOREPLY No reply (no such slave?)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
88 * IIC_UNKNOWN Unexpected return from TWI reg.
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 * Heaviy cribbed from twitest.c by Joerg Wunsch
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 int8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
93 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
94 uint8_t twst, twcr, cnt;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
95
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
96 /* Generate START */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
97 TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
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 /* Spin waiting for START to be generated */
8
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
100 WAITFORTWINT();
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
101
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
102 switch (twst = TW_STATUS) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
103 case TW_REP_START: /* OK but shouldn't happen */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
104 case TW_START:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
105 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
106
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
107 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
108 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
109 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
110
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
111 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
112 /* Not in start condition, bail */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
113 return IIC_UNKNOWN;
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 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
116 printf_P(PSTR("Sent START\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
117 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
118 /* Send SLA+W */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
119 TWDR = sla | TW_WRITE;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
120 TWCR = _BV(TWINT) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
121
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
122 /* Spin waiting for a response to be generated */
8
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
123 WAITFORTWINT();
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
124
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
125 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
126 printf_P(PSTR("Sent SLA+W\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
127 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
128 switch (twst = TW_STATUS) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
129 case TW_MT_SLA_ACK:
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 case TW_MT_SLA_NACK:
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_SLNAK;
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 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
138 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
139 break;
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 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
142 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
143 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
144 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
145 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
146 /* Send address */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
147 TWDR = adr;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
148 TWCR = _BV(TWINT) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
149
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
150 /* Spin waiting for a response to be generated */
8
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
151 WAITFORTWINT();
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
152
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
153 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
154 printf_P(PSTR("Sent address\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
155 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
156 switch ((twst = TW_STATUS)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
157 case TW_MT_DATA_ACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
158 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
159
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
160 case TW_MT_DATA_NACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
161 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
162 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
163 return IIC_SLNAK;
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 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
166 return IIC_FAILARB;
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 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
169 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
170 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
171 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
172 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
173
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
174 /* Master receive cycle */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
175 TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
8
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
176
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
177 /* wait for transmission */
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
178 WAITFORTWINT();
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
179
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
180 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
181 printf_P(PSTR("Sent START\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
182 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
183 switch ((twst = TW_STATUS)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
184 case TW_REP_START: /* OK but shouldn't happen */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
185 case TW_START:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
186 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
187
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
188 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
189 return IIC_FAILARB;
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 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
192 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
193 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
194 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
195 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
196
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
197 /* send SLA+R */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
198 TWDR = sla | TW_READ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
199 TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
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 /* Spin waiting for a response to be generated */
8
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
202 WAITFORTWINT();
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
203
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
204 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
205 printf_P(PSTR("Sent SLA+R\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
206 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
207 switch ((twst = TW_STATUS)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
208 case TW_MR_SLA_ACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
209 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
210
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
211 case TW_MR_SLA_NACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
212 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
213 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
214 return IIC_SLNAK;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
215
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
216 case TW_MR_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
217 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
218
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
219 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
220 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
221 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
222 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
223 }
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 cnt = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
226 for (twcr = _BV(TWINT) | _BV(TWEN) | _BV(TWEA);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
227 len > 0; len--) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
228 /* Send NAK on last byte */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
229 if (len == 1)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
230 twcr = _BV(TWINT) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
231 TWCR = twcr; /* clear int to start transmission */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
232 /* Spin waiting for a response to be generated */
8
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
233 WAITFORTWINT();
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
234
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
235 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
236 printf_P(PSTR("Data request done\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
237 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
238 switch ((twst = TW_STATUS)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
239 case TW_MR_DATA_NACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
240 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
241 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
242 //printf_P(PSTR("NACK on byte %d\r\n"), cnt);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
243 return cnt;
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 case TW_MR_DATA_ACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
246 *data++ = TWDR;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
247 //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
248 cnt++;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
249 break;
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 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
252 return IIC_UNKNOWN;
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 }
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 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
257 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
258 return cnt;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
259 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
260
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
261 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
262 * iic_write
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
263 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
264 * 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
265 * data. Presume that the slave auto-increments the address on
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
266 * successive writes.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
267 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
268 * 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
269 * IIC_STFAIL Could generate START condition (broken bus or busy).
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
270 * IIC_FAILARB Failed bus arbitration.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
271 * IIC_SLNAK Slave NAK'd.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
272 * IIC_NOREPLY No reply (no such slave?)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
273 * IIC_UNKNOWN Unexpected return from TWI reg.
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 * Heaviy cribbed from twitest.c by Joerg Wunsch
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 int8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
278 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
279 uint8_t twst, cnt;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
280
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
281 /* Generate START */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
282 TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
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 /* Spin waiting for START to be generated */
8
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
285 WAITFORTWINT();
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
286
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
287 switch (twst = TW_STATUS) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
288 case TW_REP_START: /* OK but shouldn't happen */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
289 case TW_START:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
290 break;
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 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
293 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
294 break;
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 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
297 /* Not in start condition, bail */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
298 return IIC_UNKNOWN;
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 START\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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
304 /* Send SLA+W */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
305 TWDR = sla | TW_WRITE;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
306 TWCR = _BV(TWINT) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
307
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
308 /* Spin waiting for a response to be generated */
8
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
309 WAITFORTWINT();
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
310
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
311 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
312 printf_P(PSTR("Sent SLA+W\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
313 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
314 switch (twst = TW_STATUS) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
315 case TW_MT_SLA_ACK:
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 case TW_MT_SLA_NACK:
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_SLNAK;
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 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
324 return IIC_FAILARB;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
325 break;
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 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
328 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
329 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
330 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
331 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
332 /* Send address */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
333 TWDR = adr;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
334 TWCR = _BV(TWINT) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
335
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
336 /* Spin waiting for a response to be generated */
8
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
337 WAITFORTWINT();
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
338
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
339 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
340 printf_P(PSTR("Sent address\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
341 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
342 switch ((twst = TW_STATUS)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
343 case TW_MT_DATA_ACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
344 break;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
345
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
346 case TW_MT_DATA_NACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
347 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
348 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
349 return IIC_SLNAK;
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 case TW_MT_ARB_LOST:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
352 return IIC_FAILARB;
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 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
355 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
356 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
357 return IIC_UNKNOWN;
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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
360 cnt = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
361 for (; len > 0; len--) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
362 TWDR = *data++;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
363 TWCR = _BV(TWINT) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
364
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
365 /* Spin waiting for a response to be generated */
8
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
366 WAITFORTWINT();
119688bb743f Don't spin forever waiting for the TWI hardware to do something.
darius@dons.net.au
parents: 6
diff changeset
367
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
368 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
369 printf_P(PSTR("Data sent\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
370 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
371 switch ((twst = TW_STATUS)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
372 case TW_MT_DATA_NACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
373 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
374 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
375 return cnt;
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 case TW_MT_DATA_ACK:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
378 cnt++;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
379 break;
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 default:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
382 return IIC_UNKNOWN;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
383 }
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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
386 /* Send STOP */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
387 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
388 return cnt;
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
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 * ds1307_gettod
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 * Read time of day from DS1307 into time
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
395 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
396 * Note that we canonify to 24hr mode.
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 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
399 int8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
400 ds1307_gettod(ds1307raw_t *time) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
401 int8_t len;
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 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
404 if (len < 0) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
405 printf_P(PSTR("iic_read failed - %d\r\n"), len);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
406 return(0);
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
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
409 #if 1
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
410 if (len != sizeof(ds1307raw_t)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
411 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
412 return(0);
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 #endif
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 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
417 int i;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
418
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
419 for (i = 0; i < len; i++)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
420 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
421 #endif
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 return(1);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
424 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
425
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
426 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
427 * ds1307_settod
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
428 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
429 * Set the DS1307 with the supplied time, format like so
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
430 * sc 2008/10/29 23:45:30
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 int8_t
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
434 ds1307_settod(char *date) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
435 ds1307raw_t rtime;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
436 uint16_t year;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
437 uint8_t i, month, day, hour, min, sec;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
438
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
439 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
440 printf_P(PSTR("Can't parse date\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
441 return(0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
442 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
443
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
444 if (year > 1900)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
445 year -= 1900;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
446
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
447 rtime.split.year10 = year / 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
448 rtime.split.year = year % 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
449 rtime.split.month10 = month / 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
450 rtime.split.month = month % 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
451 rtime.split.day10 = day / 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
452 rtime.split.day = day % 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
453 rtime.split.pmam = ((hour / 10) & 0x02) >> 1;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
454 rtime.split.hour10 = (hour / 10) & 0x01;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
455 rtime.split.hour = hour % 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
456 rtime.split.min10 = min / 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
457 rtime.split.min = min % 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
458 rtime.split.sec10 = sec / 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
459 rtime.split.sec = sec % 10;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
460
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
461 rtime.split.ch = 0; // Enable clock
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
462 rtime.split.s1224 = 0; // 24 hour mode
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
463 rtime.split.dow = 0; // XXX: unused
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
464 rtime.split.out = 0; // No clock out
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 #ifdef TWDEBUG
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
467 for (i = 0; i < sizeof(ds1307raw_t); i++)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
468 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
469 #endif
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
470 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
471 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
472
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
473 return(1);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
474 }
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 /*
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
477 * ds1307_printnow
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
478 *
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
479 * Print the current time of day
0
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 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
482 void
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
483 ds1307_printnow(const char *leader, const char *trailer) {
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
484 ds1307raw_t rtime;
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
485 ds1307_t time;
0
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 if (ds1307_gettod(&rtime) != 1)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
488 return;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
489
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
490 ds1307_cook(&rtime, &time);
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
491 ds1307_printtime(&time, leader, trailer);
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
492 }
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
493
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
494 /*
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
495 * ds1307_printtime
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
496 *
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
497 * Print time with header & trailer
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
498 *
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
499 */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
500 void
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
501 ds1307_printtime(ds1307_t *time, const char *leader, const char *trailer) {
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
502 printf_P(PSTR("%S%04d/%02d/%02d %02d:%02d:%02d%S"), leader,
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
503 1900 + time->year, time->month, time->day,
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
504 time->hour, time->min, time->sec, trailer);
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
505 }
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
506
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
507 /*
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
508 * ds1307_cook
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
509 *
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
510 * Convert a ds1307raw_t to ds1307_t
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
511 */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
512 void
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
513 ds1307_cook(ds1307raw_t *raw, ds1307_t *cooked) {
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
514 cooked->year = raw->split.year + raw->split.year10 * 10;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
515 cooked->month = raw->split.month + raw->split.month10 * 10;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
516 cooked->day = raw->split.day + raw->split.day10 * 10;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
517
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
518 /* Handle 12/24 hour time */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
519 cooked->hour = raw->split.hour10 * 10 + raw->split.hour;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
520 if (raw->split.s1224) {
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
521 if (raw->split.pmam)
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
522 cooked->hour += 12;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
523 } else
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
524 cooked->hour += (raw->split.pmam << 1) * 10;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
525
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
526 cooked->min = raw->split.min + raw->split.min10 * 10;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
527 cooked->sec = raw->split.sec + raw->split.sec10 * 10;
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
528 }
19
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
529
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
530 /*
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
531 * ds1307_totimet
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
532 *
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
533 * Convert a ds1307_t to time_t
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
534 *
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
535 */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
536 #define SECONDS_PER_DAY 86400
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
537 static uint8_t dayspermonth[] = { 31, 27, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
538 static uint8_t dayspermonth_leap[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
539
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
540 static int
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
541 isleapyear(int year) {
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
542 return ((!(year % 4) && (year % 100)) || !(year % 400)) ? 1 : 0;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
543 }
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
544
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
545 time_t
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
546 ds1307_totimet(ds1307_t *time) {
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
547 time_t t = 0;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
548 int16_t i;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
549
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
550 for (i = time->year + 1900; i > 1970; i--)
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
551 t += (isleapyear(i) ? 366 : 365) * SECONDS_PER_DAY;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
552 for (i = time->month; i > 1; i--)
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
553 t += (isleapyear(i) ? dayspermonth_leap[i] : dayspermonth[i]) * SECONDS_PER_DAY;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
554 t += (time->day - 1) * SECONDS_PER_DAY;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
555 t += time->hour * 60 * 60;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
556 t += time->min * 60;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
557 t += time->sec;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
558 t += 65535; // XXX: this is magical :(
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
559 return(t);
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
560 }
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
561
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
562 /*
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
563 * ds1307_time
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
564 *
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
565 * Return TOD as a time_t
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
566 *
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
567 */
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
568 time_t
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
569 ds1307_time(void) {
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
570 ds1307raw_t r;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
571 ds1307_t c;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
572
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
573 if (ds1307_gettod(&r) == 0)
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
574 return -1;
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
575
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
576 ds1307_cook(&r, &c);
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
577 return ds1307_totimet(&c);
b5e4591b6570 - Satisfy new compiler
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
578 }