qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 5/?] hw/ds1338.c: Fix handling of DATE (wday) register
@ 2012-12-03 20:10 Antoine Mathys
  2012-12-04 18:17 ` Peter Maydell
  0 siblings, 1 reply; 2+ messages in thread
From: Antoine Mathys @ 2012-12-03 20:10 UTC (permalink / raw)
  To: qemu-devel; +Cc: peter.maydell, paul

Per the datasheet, the DATE (wday) register is user defined. Implement this.

Signed-off-by: Antoine Mathys <barsamin@gmail.com>
---
  hw/ds1338.c |   14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/hw/ds1338.c b/hw/ds1338.c
index 8f85635..c502934 100644
--- a/hw/ds1338.c
+++ b/hw/ds1338.c
@@ -20,6 +20,7 @@
  typedef struct {
      I2CSlave i2c;
      int64_t offset;
+    uint8_t wday_offset;
      uint8_t nvram[NVRAM_SIZE];
      int32_t ptr;
      bool addr_byte;
@@ -33,6 +34,7 @@ static const VMStateDescription vmstate_ds1338 = {
      .fields = (VMStateField[]) {
          VMSTATE_I2C_SLAVE(i2c, DS1338State),
          VMSTATE_INT64(offset, DS1338State),
+        VMSTATE_UINT8(wday_offset, DS1338State),
          VMSTATE_UINT8_ARRAY(nvram, DS1338State, NVRAM_SIZE),
          VMSTATE_INT32(ptr, DS1338State),
          VMSTATE_BOOL(addr_byte, DS1338State),
@@ -62,7 +64,7 @@ static void write_time(DS1338State *s, const struct tm 
*tm)
      } else {
          s->nvram[2] = to_bcd(tm->tm_hour);
      }
-    s->nvram[3] = to_bcd(tm->tm_wday + 1);
+    s->nvram[3] = to_bcd((tm->tm_wday + s->wday_offset) % 7 + 1);
      s->nvram[4] = to_bcd(tm->tm_mday);
      s->nvram[5] = to_bcd(tm->tm_mon + 1);
      s->nvram[6] = to_bcd(tm->tm_year - 100);
@@ -164,7 +166,12 @@ static int ds1338_send(I2CSlave *i2c, uint8_t data)
              }
              break;
          case 3:
-            now.tm_wday = from_bcd(data & 0x07) - 1;
+            {
+                int user_wday = from_bcd(data & 0x07) - 1;
+                if ((user_wday >= 0) && (user_wday <= 6)) {
+                    s->wday_offset = (user_wday - now.tm_wday + 7) % 7;
+                }
+            }
              break;
          case 4:
              now.tm_mday = from_bcd(data & 0x3f);
@@ -194,6 +201,9 @@ static int ds1338_send(I2CSlave *i2c, uint8_t data)

  static int ds1338_init(I2CSlave *i2c)
  {
+    DS1338State *s = FROM_I2C_SLAVE(DS1338State, i2c);
+    s->wday_offset = 0;
+
      return 0;
  }

-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-12-04 18:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-12-03 20:10 [Qemu-devel] [PATCH 5/?] hw/ds1338.c: Fix handling of DATE (wday) register Antoine Mathys
2012-12-04 18:17 ` Peter Maydell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).