Subversion Repositories idreammicro-avr

Rev

Rev 36 | Rev 62 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/**************************************************************************//**
 * \brief DS1307 RTC library
 * \author Copyright (C) 2009  Julien Le Sech - www.idreammicro.com
 * \version 1.0
 * \date 20090501
 *
 * This file is part of the iDreamMicro library.
 *
 * This library is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 ******************************************************************************/


/**************************************************************************//**
 * \file ds1307.c
 ******************************************************************************/


/******************************************************************************
 * Header file inclusions.
 ******************************************************************************/


#include <ds1307/ds1307.h>

#include <twi/twi.h>
#include <useful/datetime.h>

#include <assert.h>
#include <stdlib.h>

/******************************************************************************
 * Private macros.
 ******************************************************************************/


#define DS1307__ADDRESS                                         0x68
#define DS1307__SLAVE_ADDRESS               (DS1307__ADDRESS << 1)

#define DS1307__REGISTER_ADDRESS__SECONDS       0x00
#define DS1307__REGISTER_ADDRESS__MINUTES       0x01
#define DS1307__REGISTER_ADDRESS__HOURS         0x02
#define DS1307__REGISTER_ADDRESS__DAY           0x03
#define DS1307__REGISTER_ADDRESS__DATE          0x04
#define DS1307__REGISTER_ADDRESS__MONTH         0x05
#define DS1307__REGISTER_ADDRESS__YEAR          0x06
#define DS1307__REGISTER_ADDRESS__CONTROL       0x07

/******************************************************************************
 * Public function definitions.
 ******************************************************************************/


/**************************************************************************//**
 * \fn void ds1307__initialize(void)
 *
 * \brief Initialize RTC.
 ******************************************************************************/

void
ds1307__initialize
(
    void
){
    // We don't initialize TWI here: we may have several devices on the bus.
    // Initialize TWI.
    //twi__initialize(DS1307__CLOCK_RATE);
}

/**************************************************************************//**
 * \fn void ds1307__get_time(
 * date_time__time_t*   p_time,
 * ds1307__hour_mode_t* p_hour_mode)
 *
 * \brief Get RTC time.
 *
 * \param p_time a pointer to fill with RTC time
 * \param p_hour_mode a pointer to fill with RTC hour mode
 ******************************************************************************/

void
ds1307__get_time
(
    datetime__time_t*      p_time,
    ds1307__hour_mode_t*    p_hour_mode
){
    // Check the preconditions.
    assert(NULL != p_time);
    assert(NULL != p_hour_mode);

    twi__start();
    twi__send_slaw(DS1307__SLAVE_ADDRESS);
    twi__send_data(DS1307__REGISTER_ADDRESS__SECONDS);
    twi__stop();

    twi__start();
    twi__send_slar(DS1307__SLAVE_ADDRESS);
    uint8_t seconds = 0;
    twi__receive_data_ack(&seconds);
    uint8_t minutes = 0;
    twi__receive_data_ack(&minutes);
    uint8_t hours = 0;
    twi__receive_data_nack(&hours);
    p_time->seconds = (((seconds & 0x70) >> 4) * 10) + (seconds & 0x0F);
    p_time->minutes = (((minutes & 0x70) >> 4) * 10) + (minutes & 0x0F);
    *p_hour_mode = (ds1307__hour_mode_t)((hours & 0x40) >> 6);
    if (DS1307__HOUR_MODE__12_HOUR == *p_hour_mode)
    {
        p_time->meridiem = (datetime__meridiem_t)((hours & 0x20) >> 5);
        p_time->hours = (((hours & 0x10) >> 4) * 10) + (hours & 0x0F);
    }
    else
    {
        p_time->hours = (((hours & 0x30) >> 4) * 10) + (hours & 0x0F);
    }
    twi__stop();
}

/**************************************************************************//**
 * \fn void ds1307__set_time(
 * date_time__time_t* p_time,
 * ds1307__hour_mode_t hour_mode)
 *
 * \brief Set RTC time.
 *
 * \param p_time time to set. p_time->meridiem isn't used in 12-hour mode.
 * \param hour_mode hour mode
 ******************************************************************************/

void
ds1307__set_time
(
    datetime__time_t*  p_time,
    ds1307__hour_mode_t hour_mode
){
    // Check the preconditions.
    assert(NULL != p_time);

    twi__start();
    twi__send_slaw(DS1307__SLAVE_ADDRESS);
    twi__send_data(DS1307__REGISTER_ADDRESS__SECONDS);

    uint8_t seconds = ((p_time->seconds / 10) << 4) + (p_time->seconds % 10);
    uint8_t minutes = ((p_time->minutes / 10) << 4) + (p_time->minutes % 10);
    uint8_t hours = 0;
    if (DS1307__HOUR_MODE__12_HOUR == hour_mode)
    {
        hours = (hour_mode << 6) + (p_time->meridiem << 5)
            + ((p_time->hours / 10) << 4) + (p_time->hours % 10);
    }
    else
    {
        hours = (hour_mode << 6) + ((p_time->hours / 10) << 4)
            + (p_time->hours % 10);
    }

    twi__send_data(seconds);
    twi__send_data(minutes);
    twi__send_data(hours);

    twi__stop();
}

/**************************************************************************//**
 * \fn void ds1307__get_date(date_time__date_t* p_date)
 *
 * \brief Get RTC date.
 *
 * \param p_date a pointer to fill with RTC date
 ******************************************************************************/

void
ds1307__get_date
(
    datetime__date_t* p_date
){
    // Check the preconditions.
    assert(NULL != p_date);

    twi__start();
    twi__send_slaw(DS1307__SLAVE_ADDRESS);
    twi__send_data(DS1307__REGISTER_ADDRESS__DAY);
    twi__stop();

    twi__start();
    twi__send_slar(DS1307__SLAVE_ADDRESS);

    uint8_t day = 0;
    twi__receive_data_ack(&day);
    uint8_t date = 0;
    twi__receive_data_ack(&date);
    uint8_t month = 0;
    twi__receive_data_ack(&month);
    uint8_t year = 0;
    twi__receive_data_nack(&year);

    p_date->day = day;
    p_date->date = (((date & 0x30) >> 4) * 10) + (date & 0x0F);
    p_date->month = (((month & 0x10) >> 4) * 10) + (month & 0x0F);
    p_date->year = (((year & 0xF0) >> 4) * 10) + (year & 0x0F);

    twi__stop();
}

/**************************************************************************//**
 * \fn void ds1307__set_date(date_time__date_t* p_date)
 *
 * \brief Set RTC date.
 *
 * \param p_date date to set
 ******************************************************************************/

void
ds1307__set_date
(
    datetime__date_t* p_date
){
    // Check the preconditions.
    assert(NULL != p_date);

    twi__start();
    twi__send_slaw(DS1307__SLAVE_ADDRESS);
    twi__send_data(DS1307__REGISTER_ADDRESS__DAY);

    uint8_t date = ((p_date->date / 10) << 4) + (p_date->date % 10);
    uint8_t month = ((p_date->month / 10) << 4) + (p_date->month % 10);
    uint8_t year = ((p_date->year / 10) << 4) + (p_date->year % 10);

    twi__send_data(p_date->day);
    twi__send_data(date);
    twi__send_data(month);
    twi__send_data(year);

    twi__stop();
}