Mercurial > ~darius > hgwebdir.cgi > avr
diff ds1307.h @ 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ds1307.h Thu Oct 30 11:53:32 2008 +1030 @@ -0,0 +1,93 @@ +/* + * DS1307 headers + * + * Copyright (c) 2008 + * Daniel O'Connor <darius@dons.net.au>. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#define IIC_STFAIL -1 +#define IIC_FAILARB -2 +#define IIC_SLNAK -3 +#define IIC_NOREPLY -4 +#define IIC_UNKNOWN -99 + +#define DS1307_ADR 0xd0 // DS1307's TWI address + +typedef struct { + uint8_t year; + uint8_t month; + uint8_t day; + uint8_t hour; + uint8_t min; + uint8_t sec; + + uint8_t ctrl; + uint8_t ram[56]; +} ds1307_t; + +typedef union { + uint8_t raw[8]; + struct { + uint8_t sec : 4; + uint8_t sec10 : 3; // Seconds (0-59) + uint8_t ch : 1; // Clock enable (1 = off) + + uint8_t min : 4; + uint8_t min10 : 3; // Minutes (0-59) + uint8_t pad0 : 1; + + uint8_t hour : 4; + uint8_t hour10 : 1; // Hours + uint8_t pmam : 1; // AM/PM => 1 = PM or extra bit for hour10 + uint8_t s1224 : 1; // 1 = 12 hour mode + uint8_t pad1 : 1; + + uint8_t dow : 3; // Day of the week (1-7) + uint8_t pad2 : 5; + + uint8_t day : 4; + uint8_t day10 : 2; // Day of the month (1-31) + uint8_t pad3 : 2; + + uint8_t month : 4; + uint8_t month10 : 1; + uint8_t pad4 : 3; // Month (1-12) + + uint8_t year : 4; + uint8_t year10 : 4; // Year (0-99) + + uint8_t rs : 2; // Rate select + uint8_t pad6 : 2; + uint8_t sqwe : 1; // Square wave enable + uint8_t pad5 : 2; + uint8_t out : 1; // Output control enable + } split; +} ds1307raw_t; + +int ds1307_init(void); +int iic_read(uint8_t *data, uint8_t len, uint8_t adr, uint8_t sla); +int iic_write(uint8_t *data, uint8_t len, uint8_t adr, uint8_t sla); +int ds1307_gettod(ds1307raw_t *time); +int ds1307_settod(char *date); +void ds1307_printtime(char *leader, char *trailer);