Akafugu / Mbed 2 deprecated vfd_modular_clock_mbed Featured

Dependencies:   DipCortex-EEprom RTC flw mbed

RTC/ds1302.cpp

Committer:
Backstrom
Date:
2015-02-09
Revision:
0:f6e68b4ce169

File content as of revision 0:f6e68b4ce169:

/*
 * VFD Modular Clock - mbed
 * (C) 2011-14 Akafugu Corporation
 *
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program 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 General Public License for more details.
 *
 */

#include "global.h"
#include "ds1302.h"

DS1302::DS1302(PinName sclk, PinName io, PinName rst)
 : m_sclk(sclk)
 , m_io(io)
 , m_rst(rst)
{
}

void DS1302::begin()
{
    uint8_t x;
    
    m_rst = 0;
    wait_us(2);
    m_sclk = 0;
    
    write_byte(0x8e,0);
    write_byte(0x90,0xa4);
    
    x=read_byte(0x81);
    
    if((x & 0x80) != 0) // start oscillator
        write_byte(0x80,0);
}


time_t DS1302::time()
{
    return m_time;
}

struct tm* DS1302::getTime()
{    
    m_tm.tm_sec  = bcd2dec(read_byte(0x81));
    m_tm.tm_min  = bcd2dec(read_byte(0x83));
    m_tm.tm_hour = bcd2dec(read_byte(0x84));

    return &m_tm;    
}

void DS1302::getTime(uint8_t* hour, uint8_t* min, uint8_t* sec)
{
    if (sec)  *sec =  bcd2dec(read_byte(0x81));
    if (min)  *min =  bcd2dec(read_byte(0x83));
    if (hour) *hour = bcd2dec(read_byte(0x84));
}

void DS1302::setTime(time_t t)
{
}

void DS1302::setTime(uint8_t hour, uint8_t min, uint8_t sec)
{   
    write_byte(0x84, dec2bcd(hour));
    write_byte(0x82, dec2bcd(min));
    write_byte(0x80, dec2bcd(sec));
}

void DS1302::setTime(struct tm* tm)
{
/*
    write_byte(0x86, dec2bcd(day));
    write_byte(0x88, dec2bcd(month));
    write_byte(0x8c, dec2bcd(year));
    write_byte(0x8a, dec2bcd(dayofweek));
    write_byte(0x84, dec2bcd(hour));
    write_byte(0x82, dec2bcd(min));
    write_byte(0x80, dec2bcd(sec));

void ds1302::set_datetime(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min)
{
    write(0x86,get_bcd(day));
    write(0x88,get_bcd(mth));
    write(0x8c,get_bcd(year));
    write(0x8a,get_bcd(dow));
    write(0x84,get_bcd(hr));
    write(0x82,get_bcd(min));
    write(0x80,get_bcd(0));
}
*/
}

void DS1302::setAlarm(time_t t)
{
}

void DS1302::setAlarm(uint8_t hour, uint8_t min, uint8_t sec)
{
}

struct tm* DS1302::getAlarm(void)
{
    return 0;
}

void DS1302::getAlarm(uint8_t* hour, uint8_t* min, uint8_t* sec)
{
}

bool DS1302::checkAlarm(void)
{
    return false;
}

void DS1302::SQWEnable(bool enable)
{
}

void DS1302::SQWSetFreq(enum RTC_SQW_FREQ freq)
{
}

uint8_t DS1302::read_byte(uint8_t cmd)
{
    uint8_t data=0;
    
    m_rst = 1;
    write_byte(cmd);
    
    m_io.input();
    wait_us(1);
    
    for (uint8_t i = 0; i <= 7; i++)
    {
       data += m_io<<i;
       m_sclk = 1;
       wait_us(1);
       m_sclk = 0;
       wait_us(1);
    }
    
    m_rst = 0;
    
    return data;
}

void DS1302::write_byte(uint8_t cmd, uint8_t data)
{
    m_rst = 1;
    wait_us(1);
    
    write_byte(cmd);
    write_byte(data);
    
    m_rst = 0;
}

void DS1302::write_byte(uint8_t cmd)
{
    m_io.output();
    
    for (uint8_t i=0; i<=7; i++)
    {
        m_io = (cmd >> i) & 0x01;
        wait_us(1);
        m_sclk = 1;
        wait_us(1);
        m_sclk = 0;
    }
    
    m_io.input();
}

void DS1302::writeRam(uint8_t addr, uint8_t data)
{
    write_byte(addr|0xC0, data);
}

uint8_t DS1302::readRam(uint8_t addr)
{
    return read_byte(addr|0xC1);
}