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