]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/pcf8583.c
phys. address 0x00040 points to fifo_list
[z180-stamp.git] / avr / pcf8583.c
index d33c9b0dae5d108a2130c0ee3dc7c1331b7fe21b..169e4e937baa4ad7aee475316c6332c0ae86cb94 100644 (file)
@@ -1,15 +1,25 @@
 /*
- * Date & Time support for Philips PCF8583 RTC
+ * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
-/* #define     DEBUG   */
+/*
+ * Date & Time support for Philips PCF8583 RTC
+ */
 
 #include "common.h"
 #include <stdlib.h>
-#include "debug.h"
-#include "command.h"
+#include "time.h"
 #include "rtc.h"
 #include "i2c.h"
+#include "command.h"
+#include "debug.h"
+
+#define  DEBUG_RTC 0
+
+#define debug_rtc(fmt, args...)                        \
+       debug_cond(DEBUG_RTC, fmt, ##args)
 
 #define REG_CS         0x00    /* control/status */
 #define REG_CSEC       0x01    /* hundredth of a second */
@@ -31,21 +41,21 @@ static uint_fast8_t bcd2bin(uint8_t val)
 static uint8_t bin2bcd (uint_fast8_t val)
 {
        div_t d = div(val, 10);
-       
+
        return (d.quot << 4) | d.rem;
 }
 
 
-int rtc_get (struct rtc_time *tmp)
+int rtc_get (struct tm *tmp)
 {
        int rel = 0;
        uint8_t rtcbuf[NR_OF_REGS];
-       uint16_t year;
-       
+       int16_t year;
+
        i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, rtcbuf, NR_OF_REGS);
        i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0x10, 1, (uint8_t *) &year, 2);
 
-       debug("Get RTC year: %u, year/date: %02x, wdays/month: %02x, "
+       debug_rtc("Get RTC year: %u, year/date: %02x, wdays/month: %02x, "
                "hour: %02x, min: %02x, sec: %02x, (stat: %02x)\n", year,
                rtcbuf[6], rtcbuf[5], rtcbuf[4], rtcbuf[3], rtcbuf[2], rtcbuf[0]);
 
@@ -53,35 +63,35 @@ int rtc_get (struct rtc_time *tmp)
        tmp->tm_min  = bcd2bin (rtcbuf[REG_MIN]    & 0x7F);
        tmp->tm_hour = bcd2bin (rtcbuf[REG_HOUR]   & 0x3F);
        tmp->tm_mday = bcd2bin (rtcbuf[REG_YRDATE] & 0x3F);
-       tmp->tm_mon  = bcd2bin (rtcbuf[REG_WDMON]  & 0x1F);
+       tmp->tm_mon  = bcd2bin (rtcbuf[REG_WDMON]  & 0x1F) - 1;
        while (year%4 < (rtcbuf[REG_YRDATE]>>6)) {
                year++;
                /* TODO: update RTC ram */
        }
-       tmp->tm_year  = year; 
+       tmp->tm_year  = year;
        tmp->tm_wday = rtcbuf[REG_WDMON] >> 5;
        tmp->tm_yday = 0;
        tmp->tm_isdst= 0;
 
 
-       debug ( "Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
+       debug_rtc( "Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
                tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
                tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
 
        return rel;
 }
 
-int rtc_set (struct rtc_time *tmp)
+int rtc_set (struct tm *tmp)
 {
        uint8_t rtcbuf[NR_OF_REGS];
 
-       debug ( "Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
+       debug_rtc("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
                tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
                tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
 
        rtcbuf[REG_CS] = 0x84;
        rtcbuf[REG_CSEC] = 0x00;
-       rtcbuf[REG_WDMON ] = bin2bcd(tmp->tm_mon) | ((tmp->tm_wday) << 5);
+       rtcbuf[REG_WDMON ] = (bin2bcd(tmp->tm_mon) + 1) | ((tmp->tm_wday) << 5);
        rtcbuf[REG_YRDATE] = ((tmp->tm_year % 4) << 6) | bin2bcd(tmp->tm_mday);
        rtcbuf[REG_HOUR  ] = bin2bcd(tmp->tm_hour);
        rtcbuf[REG_MIN   ] = bin2bcd(tmp->tm_min);
@@ -95,12 +105,3 @@ int rtc_set (struct rtc_time *tmp)
 
        return 0;
 }
-
-void rtc_reset (void)
-{
-       uint8_t c = 0;
-       
-       i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, &c, 1);
-}
-
-