Index: apps/plugins/clock.c
===================================================================
RCS file: /cvsroot/rockbox/apps/plugins/clock.c,v
retrieving revision 1.39
diff -u -r1.39 clock.c
--- apps/plugins/clock.c	5 Dec 2006 10:13:14 -0000	1.39
+++ apps/plugins/clock.c	5 Dec 2006 12:07:39 -0000
@@ -152,7 +152,7 @@
 #define MENU_BUTTON_TEXT "SELECT"
 #define COUNTER_BUTTON_TEXT "PLAY"
 
-#elif (CONFIG_KEYPAD == IRIVER_H300_PAD)
+#elif (CONFIG_KEYPAD == IRIVER_H300_PAD) || (RTC_CONFIG == RTC_SD1339C)
 
 #define COUNTER_TOGGLE_BUTTON (BUTTON_ON|BUTTON_REL)
 #define COUNTER_RESET_BUTTON (BUTTON_ON|BUTTON_REPEAT)
Index: firmware/SOURCES
===================================================================
RCS file: /cvsroot/rockbox/firmware/SOURCES,v
retrieving revision 1.145
diff -u -r1.145 SOURCES
--- firmware/SOURCES	3 Dec 2006 22:13:44 -0000	1.145
+++ firmware/SOURCES	5 Dec 2006 21:53:29 -0000
@@ -121,6 +121,7 @@
 /* EEPROM */
 #ifdef HAVE_EEPROM
 drivers/eeprom_24cxx.c
+drivers/i2c-bitbang.c
 #ifdef HAVE_EEPROM_SETTINGS
 eeprom_settings.c
 #endif /* HAVE_EEPROM_SETTINGS */
@@ -136,6 +137,8 @@
 drivers/rtc/rtc_pcf50605.c
 #elif (CONFIG_RTC == RTC_E8564)
 drivers/rtc/rtc_e8564.c
+#elif (CONFIG_RTC == RTC_DS1339C)
+drivers/rtc/rtc_ds1339c.c
 #endif /* (CONFIG_RTC == RTC_) */
 #endif /* SIMULATOR */
 
Index: firmware/drivers/eeprom_24cxx.c
===================================================================
RCS file: /cvsroot/rockbox/firmware/drivers/eeprom_24cxx.c,v
retrieving revision 1.9
diff -u -r1.9 eeprom_24cxx.c
--- firmware/drivers/eeprom_24cxx.c	26 Nov 2006 14:50:15 -0000	1.9
+++ firmware/drivers/eeprom_24cxx.c	6 Dec 2006 10:29:36 -0000
@@ -16,277 +16,24 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
-#include "lcd.h"
-#include "cpu.h"
-#include "system.h"
-#include "kernel.h"
-#include "thread.h"
-#include "debug.h"
+
 #include "logf.h"
-#include "sprintf.h"
 #include "string.h"
 #include "inttypes.h"
 
+#include "i2c-bitbang.h"
 #include "eeprom_24cxx.h"
 
-/**
- * I2C-functions are copied and ported from fmradio.c.
- */
-
-#define SW_I2C_WRITE 0
-#define SW_I2C_READ  1
-
 /* Use cache to speedup writing to the chip. */
 static char data_cache[EEPROM_SIZE];
 static uint8_t cached_bitfield[EEPROM_SIZE/8];
 
 #define IS_CACHED(addr) (cached_bitfield[addr/8] & (1 << (addr % 8)))
-#define SET_CACHED(addr) (cached_bitfield[addr/8] |= 1 << (addr % 8))
-
-/* h1x0 needs its own i2c driver,
-   h3x0 uses the pcf i2c driver */
-
-#ifdef IRIVER_H100_SERIES
-
-/* cute little functions, atomic read-modify-write */
-
-/* SCL is GPIO, 12 */
-#define SCL             ( 0x00001000 & GPIO_READ)
-#define SCL_OUT_LO and_l(~0x00001000, &GPIO_OUT)
-#define SCL_LO      or_l( 0x00001000, &GPIO_ENABLE)
-#define SCL_HI     and_l(~0x00001000, &GPIO_ENABLE)
-
-/* SDA is GPIO1, 13 */
-#define SDA             ( 0x00002000 & GPIO1_READ)
-#define SDA_OUT_LO and_l(~0x00002000, &GPIO1_OUT)
-#define SDA_LO      or_l( 0x00002000, &GPIO1_ENABLE)
-#define SDA_HI     and_l(~0x00002000, &GPIO1_ENABLE)
-
-/* delay loop to achieve 400kHz at 120MHz CPU frequency */
-#define DELAY    do { int _x; for(_x=0;_x<22;_x++);} while(0)
-
-static void sw_i2c_init(void)
-{
-    logf("sw_i2c_init");
-    or_l(0x00001000, &GPIO_FUNCTION);
-    or_l(0x00002000, &GPIO1_FUNCTION);
-    SDA_HI;
-    SCL_HI;
-    SDA_OUT_LO;
-    SCL_OUT_LO;
-}
-
-static void sw_i2c_start(void)
-{
-    SCL_LO;
-    DELAY;
-    SDA_HI;
-    DELAY;
-    SCL_HI;
-    DELAY;
-    SDA_LO;
-    DELAY;
-    SCL_LO;
-}
-
-static void sw_i2c_stop(void)
-{
-    SCL_HI;
-    DELAY;
-    SDA_HI;
-    DELAY;
-}
-
-static void sw_i2c_ack(void)
-{
-    SCL_LO;
-    DELAY;
-    SDA_LO;
-    DELAY;
-    
-    SCL_HI;
-    DELAY;
-}
-
-static bool sw_i2c_getack(void)
-{
-    bool ret = true;
-    int count = 10;
-
-    SCL_LO;
-    DELAY;
-    SDA_HI;   /* sets to input */
-    DELAY;
-    SCL_HI;
-    DELAY;
-
-    while (SDA && count--)
-        DELAY;
-    
-    if (SDA)
-        /* ack failed */
-        ret = false;
-    
-    SCL_LO;
-    DELAY;
-    SDA_LO;
-
-    return ret;
-}
-
-static void sw_i2c_outb(unsigned char byte)
-{
-    int i;
-
-    /* clock out each bit, MSB first */
-    for ( i=0x80; i; i>>=1 )
-    {
-        SCL_LO;
-        DELAY;
-        if ( i & byte )
-            SDA_HI;
-        else
-            SDA_LO;
-        DELAY;
-        SCL_HI;
-        DELAY;
-    }
-}
-
-static unsigned char sw_i2c_inb(void)
-{
-    int i;
-    unsigned char byte = 0;
-
-    SDA_HI;   /* sets to input */
-    
-    /* clock in each bit, MSB first */
-    for ( i=0x80; i; i>>=1 ) 
-    {
-        SCL_HI;
-        DELAY;
-        if ( SDA )
-            byte |= i;
-        SCL_LO;
-        DELAY;
-    }
-
-    sw_i2c_ack();
-    
-    return byte;
-}
-
-#else
-
-#include "pcf50606.h"
-
-#define sw_i2c_init()       /* no extra init required */
-#define sw_i2c_start()      pcf50606_i2c_start()
-#define sw_i2c_stop()       pcf50606_i2c_stop()
-#define sw_i2c_ack()        pcf50606_i2c_ack(true)
-#define sw_i2c_getack()     pcf50606_i2c_getack()
-#define sw_i2c_outb(x)      pcf50606_i2c_outb(x)
-#define sw_i2c_inb()        pcf50606_i2c_inb(false)
-
-#endif /* IRIVER_H100_SERIES */
-
-
-int sw_i2c_write(int location, const unsigned char* buf, int count)
-{
-    int i;
-
-    sw_i2c_start();
-    sw_i2c_outb((EEPROM_ADDR & 0xfe) | SW_I2C_WRITE);
-    if (!sw_i2c_getack())
-    {
-        sw_i2c_stop();
-        return -1;
-    }
-    
-    sw_i2c_outb(location);
-    if (!sw_i2c_getack())
-    {
-        sw_i2c_stop();
-        return -2;
-    }
-    
-    for (i=0; i<count; i++)
-    {
-        sw_i2c_outb(buf[i]);
-        if (!sw_i2c_getack())
-        {
-            sw_i2c_stop();
-            return -3;
-        }
-    }
-
-    sw_i2c_stop();
-    
-    return 0;
-}
-
-int sw_i2c_write_byte(int location, unsigned char byte)
-{
-    sw_i2c_start();
-    sw_i2c_outb((EEPROM_ADDR & 0xfe) | SW_I2C_WRITE);
-    if (!sw_i2c_getack())
-    {
-        sw_i2c_stop();
-        return -1;
-    }
-    
-    sw_i2c_outb(location);
-    if (!sw_i2c_getack())
-    {
-        sw_i2c_stop();
-        return -2;
-    }
-    
-    sw_i2c_outb(byte);
-    if (!sw_i2c_getack())
-    {
-        sw_i2c_stop();
-        return -3;
-    }
-
-    sw_i2c_stop();
-    
-    return 0;
-}
-
-int sw_i2c_read(unsigned char location, unsigned char* byte)
-{
-    sw_i2c_start();
-    sw_i2c_outb((EEPROM_ADDR & 0xfe) | SW_I2C_WRITE);
-    if (!sw_i2c_getack())
-    {
-        sw_i2c_stop();
-        return -1;
-    }
-
-    sw_i2c_outb(location);
-    if (!sw_i2c_getack())
-    {
-        sw_i2c_stop();
-        return -2;
-    }
-    
-    sw_i2c_start();
-    sw_i2c_outb((EEPROM_ADDR & 0xfe) | SW_I2C_READ);
-    if (!sw_i2c_getack())
-    {
-        sw_i2c_stop();
-        return -3;
-    }
-    
-    *byte = sw_i2c_inb();
-    sw_i2c_stop();
-    
-    return 0;
-}
+#define SET_CACHED(addr) (cached_bitfield[addr/8] |= 1 << (addr % 8)) 
 
 void eeprom_24cxx_init(void)
 {
+   logf("ROOLKU: eeprom_24cxx_init");
     sw_i2c_init();
     memset(cached_bitfield, 0, sizeof cached_bitfield);
 }
@@ -314,7 +61,7 @@
     *c = 0;
     do
     {
-        ret = sw_i2c_read(address, &byte);
+        ret = sw_i2c_read(EEPROM_ADDR, address, &byte, 1);
     } while (ret < 0 && count++ < 200);
 
     if (ret < 0)
@@ -357,7 +104,7 @@
     
     do
     {
-        ret = sw_i2c_write_byte(address, c);
+        ret = sw_i2c_write(EEPROM_ADDR, address, &c, 1);
     } while (ret < 0 && count++ < 200) ;
 
     if (ret < 0)
Index: firmware/export/config-h120.h
===================================================================
RCS file: /cvsroot/rockbox/firmware/export/config-h120.h,v
retrieving revision 1.53
diff -u -r1.53 config-h120.h
--- firmware/export/config-h120.h	21 Nov 2006 17:14:07 -0000	1.53
+++ firmware/export/config-h120.h	6 Dec 2006 23:53:21 -0000
@@ -45,7 +45,10 @@
 /* Define this if you do software codec */
 #define CONFIG_CODEC SWCODEC
 
-/* Define this if you have an remote lcd */
+/* define this if you have a real-time clock */
+#define CONFIG_RTC RTC_DS1339C
+ 
+ /* Define this if you have an remote lcd */
 #define HAVE_REMOTE_LCD
 
 #define CONFIG_LCD LCD_S1D15E06
--- firmware/drivers/i2c-bitbang.c	3 Dec 2006 22:13:44 -0000	1.145
+++ firmware/drivers/i2c-bitbang.c	5 Dec 2006 21:53:29 -0000
@@ -0,0 +1,281 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: i2c-bitbang.c,v 1.1 2006-11-26 14:50:15 miipekk Exp $
+ *
+ * Copyright (C) 2006 by Miika Pekkarinen
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#include "system.h"
+#include "logf.h"
+#include "inttypes.h"
+
+#include "i2c-bitbang.h"
+
+/**
+ * I2C-functions are copied and ported from fmradio.c.
+ * later fixed, adapted and moved to a seperate file so they can be re-used by the rtc-ds1339c code by Robert Kukla
+ */
+
+#define SW_I2C_WRITE 0
+#define SW_I2C_READ  1
+
+/* h1x0 needs its own i2c driver,
+   h3x0 uses the pcf i2c driver */
+
+#ifdef IRIVER_H100_SERIES
+
+/* cute little functions, atomic read-modify-write */
+
+/* SCL is GPIO, 12 */
+#define SCL             ( 0x00001000 & GPIO_READ)
+#define SCL_OUT_LO and_l(~0x00001000, &GPIO_OUT)
+#define SCL_LO      or_l( 0x00001000, &GPIO_ENABLE)
+#define SCL_HI     and_l(~0x00001000, &GPIO_ENABLE)
+
+/* SDA is GPIO1, 13 */
+#define SDA             ( 0x00002000 & GPIO1_READ)
+#define SDA_OUT_LO and_l(~0x00002000, &GPIO1_OUT)
+#define SDA_LO      or_l( 0x00002000, &GPIO1_ENABLE)
+#define SDA_HI     and_l(~0x00002000, &GPIO1_ENABLE)
+
+/* delay loop to achieve 400kHz at 120MHz CPU frequency */
+#define DELAY    do { int _x; for(_x=0;_x<22;_x++);} while(0)
+
+void sw_i2c_init(void)
+{
+    or_l(0x00001000, &GPIO_FUNCTION);
+    or_l(0x00002000, &GPIO1_FUNCTION);
+    SDA_HI;
+    SCL_HI;
+    SDA_OUT_LO;
+    SCL_OUT_LO;
+}
+
+/*   in: C=? D=?
+ *  out: C=L D=L
+ */
+static void sw_i2c_start(void)
+{
+    SCL_LO;
+    DELAY;
+    SDA_HI;
+    DELAY;
+    SCL_HI;
+    DELAY;
+    SDA_LO;
+    DELAY;
+    SCL_LO;
+}
+
+/*   in: C=L D=?
+ *  out: C=H D=H
+ */
+static void sw_i2c_stop(void)
+{
+    SDA_LO;
+    DELAY;
+    SCL_HI;
+    DELAY;
+    SDA_HI;
+}
+
+/*   in: C=L D=H
+ *  out: C=L D=L
+ */
+static void sw_i2c_ack(void)
+{
+    SDA_LO;
+    DELAY;
+    
+    SCL_HI;
+    DELAY;
+    SCL_LO;
+}
+
+/*   in: C=L D=H
+ *  out: C=L D=H
+ */
+static void sw_i2c_nack(void)
+{
+    SDA_HI;          /* redundant */
+    DELAY;
+    
+    SCL_HI;
+    DELAY;
+    SCL_LO;
+}
+
+/*   in: C=L D=?
+ *  out: C=L D=H
+ */
+static bool sw_i2c_getack(void)
+{
+    bool ret = true;
+/*    int count = 10; */
+
+    SDA_HI;   /* sets to input */
+    DELAY;
+    SCL_HI;
+    DELAY;
+
+/*    while (SDA && count--) */
+/*        DELAY; */
+    
+    if (SDA)
+        /* ack failed */
+        ret = false;
+    
+    SCL_LO;
+
+    return ret;
+}
+
+/*   in: C=L D=?
+ *  out: C=L D=?
+ */
+static void sw_i2c_outb(unsigned char byte)
+{
+    int i;
+
+    /* clock out each bit, MSB first */
+    for ( i=0x80; i; i>>=1 )
+    {
+        if ( i & byte )
+            SDA_HI;
+        else
+            SDA_LO;
+        DELAY;
+
+        SCL_HI;
+        DELAY;
+        SCL_LO;
+    }
+}
+
+/*   in: C=L D=?
+ *  out: C=L D=H
+ */
+static unsigned char sw_i2c_inb(void)
+{
+    int i;
+    unsigned char byte = 0;
+
+    SDA_HI;   /* sets to input */
+    
+    /* clock in each bit, MSB first */
+    for ( i=0x80; i; i>>=1 ) 
+    {
+        DELAY;
+        do {
+          SCL_HI;
+          DELAY;
+        }
+        while(SCL==0);    /* wait for any SCL clock stretching */
+        if ( SDA )
+            byte |= i;
+        SCL_LO;
+    }
+
+    return byte;
+}
+
+#else
+
+#include "pcf50606.h"
+
+#define sw_i2c_init()       /* no extra init required */
+#define sw_i2c_start()      pcf50606_i2c_start()
+#define sw_i2c_stop()       pcf50606_i2c_stop()
+#define sw_i2c_ack()        pcf50606_i2c_ack(true)
+#define sw_i2c_getack()     pcf50606_i2c_getack()
+#define sw_i2c_outb(x)      pcf50606_i2c_outb(x)
+#define sw_i2c_inb()        pcf50606_i2c_inb(false)
+
+#endif /* IRIVER_H100_SERIES */
+
+int sw_i2c_write(unsigned char chip, unsigned char location, const unsigned char* buf, int count)
+{
+    int i;
+
+    sw_i2c_start();
+    sw_i2c_outb((chip & 0xfe) | SW_I2C_WRITE);
+    if (!sw_i2c_getack())
+    {
+        sw_i2c_stop();
+        return -1;
+    }
+    
+    sw_i2c_outb(location);
+    if (!sw_i2c_getack())
+    {
+        sw_i2c_stop();
+        return -2;
+    }
+    
+    for (i=0; i<count; i++)
+    {
+        sw_i2c_outb(buf[i]);
+        if (!sw_i2c_getack())
+        {
+            sw_i2c_stop();
+            return -3;
+        }
+    }
+
+    sw_i2c_stop();
+    
+    return 0;
+}
+
+int sw_i2c_read(unsigned char chip, unsigned char location, unsigned char* buf, int count)
+{
+    int i;
+  
+    sw_i2c_start();
+    sw_i2c_outb((chip & 0xfe) | SW_I2C_WRITE);
+    if (!sw_i2c_getack())
+    {
+        sw_i2c_stop();
+        return -1;
+    }
+
+    sw_i2c_outb(location);
+    if (!sw_i2c_getack())
+    {
+        sw_i2c_stop();
+        return -2;
+    }
+    
+    sw_i2c_start();
+    sw_i2c_outb((chip & 0xfe) | SW_I2C_READ);
+    if (!sw_i2c_getack())
+    {
+        sw_i2c_stop();
+        return -3;
+    }
+    
+    for (i=0; i<count-1; i++)
+    {
+      buf[i] = sw_i2c_inb();
+      sw_i2c_ack();
+    }
+
+    /* 1byte min */
+    buf[i] = sw_i2c_inb();
+    sw_i2c_nack();
+        
+    sw_i2c_stop();
+    
+    return 0;
+}
--- firmware/drivers/rtc/rtc_ds1339c.c	3 Dec 2006 22:13:44 -0000	1.145
+++ firmware/drivers/rtc/rtc_ds1339c.c	5 Dec 2006 21:53:29 -0000
@@ -0,0 +1,52 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: rtc_e8564.c,v 1.1 2006-11-27 09:44:56 markun Exp $
+ *
+ * Copyright (C) 2002 by Linus Nielsen Feltzing, Uwe Freese, Laurent Baum
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#include "rtc.h"
+#include "logf.h"
+#include "i2c-bitbang.h"
+
+#define RTC_ADDR   0xD0
+
+void rtc_init(void)
+{
+   logf("ROOLKU: rtc_init");
+   sw_i2c_init();
+}
+
+int rtc_read_datetime(unsigned char* buf)
+{
+    int i;
+
+    i = sw_i2c_read(RTC_ADDR, 0, buf, 7);        
+    
+    buf[3]--; /* timefuncs wants 0..6 for wday */
+
+    return i;
+}
+
+int rtc_write_datetime(unsigned char* buf)
+{
+    int i;
+
+    buf[3]++;       /* chip wants 1..7 for wday */
+    buf[5]|=0x80;   /* chip wants century (always 20xx) */
+
+    i = sw_i2c_write(RTC_ADDR, 0, buf, 7);
+
+    return i;
+}
--- firmware/export/i2c-bitbang.h	3 Dec 2006 22:13:44 -0000	1.145
+++ firmware/export/i2c-bitbang.h	5 Dec 2006 21:53:29 -0000
@@ -0,0 +1,27 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: i2c-bitbang.h,v 1.1 2006-11-26 14:50:15 miipekk Exp $
+ *
+ * Copyright (C) 2006 by Miika Pekkarinen
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#ifndef _I2C_BITBANG_H
+#define _I2C_BITBANG_H
+
+void sw_i2c_init(void);
+int sw_i2c_write(unsigned char chip, unsigned char location, const unsigned char* buf, int count);
+int sw_i2c_read (unsigned char chip, unsigned char location,       unsigned char* buf, int count);
+
+#endif
