view ds1307.h @ 8:119688bb743f

Don't spin forever waiting for the TWI hardware to do something. I _think_ this will help the case where I find the micro is hanging but I haven't seen an error from it yet.
author darius@dons.net.au
date Fri, 01 May 2009 15:21:31 +0930
parents 3879f487b661
children b5e4591b6570
line wrap: on
line source

/*
 * 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_NORESP	-5
#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);
int8_t	iic_read(uint8_t *data, uint8_t len, uint8_t adr, uint8_t sla);
int8_t	iic_write(uint8_t *data, uint8_t len, uint8_t adr, uint8_t sla);
int8_t	ds1307_gettod(ds1307raw_t *time);
int8_t	ds1307_settod(char *date);
void	ds1307_printtime(char *leader, char *trailer);