Rev 62 | 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/bits.h>
#include <useful/datetime.h>
#include <avr/io.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
/******************************************************************************
* Private macros.
******************************************************************************/
#define DS1307__ADDRESS 0xD0
#define DS1307__REG_SECONDS 0x00
#define DS1307__REG_MINUTES 0x01
#define DS1307__REG_HOURS 0x02
#define DS1307__REG_DAY 0x03
#define DS1307__REG_DATE 0x04
#define DS1307__REG_MONTH 0x05
#define DS1307__REG_YEAR 0x06
#define DS1307__REG_CONTROL 0x07
#define DS1307__RS0 0x00
#define DS1307__RS1 0x01
#define DS1307__SQWE 0x04
#define DS1307__OUT 0x07
/**************************************************************************//**
* \def DS1307__REG_RAM
* \brief Start address of RAM registers (locations 0x08 to 0x3F).
******************************************************************************/
#define DS1307__REG_RAM 0x08
/******************************************************************************
* 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__ADDRESS);
twi__send_data(DS1307__REG_SECONDS);
twi__stop();
twi__start();
twi__send_slar(DS1307__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__ADDRESS);
twi__send_data(DS1307__REG_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__ADDRESS);
twi__send_data(DS1307__REG_DAY);
twi__stop();
twi__start();
twi__send_slar(DS1307__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__ADDRESS);
twi__send_data(DS1307__REG_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();
}
/**************************************************************************//**
* \fn void ds1307__set_square_wave_output_level(ds1307__sqw_out_level_t level)
*
* \brief Set square wave output pin level.
*
* \param level Level to set.
******************************************************************************/
void
ds1307__set_square_wave_output_level
(
ds1307__sqw_out_level_t level
){
twi__start();
twi__send_slaw(DS1307__ADDRESS);
twi__send_data(DS1307__REG_CONTROL);
uint8_t register_value = (level == DS1307__SQW_LEVEL__LOW) ? 0x00 : 0x80;
twi__send_data(register_value);
twi__stop();
}
/**************************************************************************//**
* \fn void ds1307__set_square_wave_output_signal(
* ds1307__sqw_out__frequency_t frequency,
* bool enable)
*
* \brief Set Square-Wave output signal.
*
* \param frequency Frequency.
* \param enable Status.
******************************************************************************/
void
ds1307__set_square_wave_output_signal
(
ds1307__sqw_out__frequency_t frequency,
bool enable
){
uint8_t register_value = 0;
// Rate select.
switch (frequency)
{
case DS1307__SQW_FREQUENCY__4096_HZ:
register_value = _BV(DS1307__RS0);
break;
case DS1307__SQW_FREQUENCY__8192_HZ:
register_value = _BV(DS1307__RS1);
break;
case DS1307__SQW_FREQUENCY__32768_HZ:
register_value = _BV(DS1307__RS1) | _BV(DS1307__RS0);
break;
case DS1307__SQW_FREQUENCY__1_HZ:
default:
register_value = 0;
break;
}
// Square-Wave enable.
if (enable)
{
BIT__SET(register_value, DS1307__SQWE);
}
twi__start();
twi__send_slaw(DS1307__ADDRESS);
twi__send_data(DS1307__REG_CONTROL);
twi__send_data(register_value);
twi__stop();
}
/**************************************************************************//**
* \fn uint8_t ds1307__read_byte_in_ram(uint8_t address)
*
* \brief Read a byte in DS1307 RAM.
*
* \param address Address to read.
*
* \return Read byte.
******************************************************************************/
uint8_t
ds1307__read_byte_in_ram
(
uint8_t address
){
// Check the preconditions.
assert(address >= 0x08);
assert(address <= 0x3F);
uint8_t data = 0;
twi__start();
twi__send_slaw(DS1307__ADDRESS);
twi__send_data(address);
twi__stop();
twi__start();
twi__send_slar(DS1307__ADDRESS);
twi__receive_data_nack(&data);
twi__stop();
return data;
}
/**************************************************************************//**
* \fn void ds1307__read_bytes_in_ram(
* uint8_t address,
* uint8_t length,
* uint8_t* p_data)
*
* \brief Read bytes in DS1307 RAM.
*
* \param address Address to read.
* \param length Number of bytes to read.
* \param[in] p_data Buffer to fill.
******************************************************************************/
void
ds1307__read_bytes_in_ram
(
uint8_t address,
uint8_t length,
uint8_t* p_data
){
// Check the preconditions.
assert(NULL != p_data);
assert(address >= 0x08);
assert(address <= 0x3F);
assert(length <= 56);
twi__start();
twi__send_slaw(DS1307__ADDRESS);
twi__send_data(address);
twi__stop();
twi__start();
twi__send_slar(DS1307__ADDRESS);
for (uint8_t i = 0; i < length - 1; i++)
{
twi__receive_data_ack(p_data + i);
}
twi__receive_data_nack(p_data + length - 1);
twi__stop();
}
/**************************************************************************//**
* \fn void ds1307__write_byte_in_ram(uint8_t address, uint8_t data)
*
* \brief Write a byte in DS1307 RAM.
*
* \param address Address to write.
* \param data Byte to write.
******************************************************************************/
void
ds1307__write_byte_in_ram
(
uint8_t address,
uint8_t data
){
// Check the preconditions.
assert(address >= 0x08);
assert(address <= 0x3F);
twi__start();
twi__send_slaw(DS1307__ADDRESS);
twi__send_data(address);
twi__send_data(data);
twi__stop();
}
/**************************************************************************//**
* \fn void ds1307__write_bytes_in_ram(uint8_t address, uint8_t data)
*
* \brief Write a byte in DS1307 RAM.
*
* \param address Address to write.
* \param length Number of bytes to write.
* \param[in] p_data Bytes to write.
******************************************************************************/
void
ds1307__write_bytes_in_ram
(
uint8_t address,
uint8_t length,
uint8_t* p_data
){
// Check the preconditions.
assert(NULL != p_data);
assert(address >= 0x08);
assert(address <= 0x3F);
assert(length <= 56);
twi__start();
twi__send_slaw(DS1307__ADDRESS);
twi__send_data(address);
for (uint8_t i = 0; i < length; i++)
{
twi__send_data(p_data[i]);
}
twi__stop();
}