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