]> bicyclesonthemoon.info Git - staf/staf/commitdiff
keep year digits, workaround RTC bug
authorb <rowerynaksiezycu@gmail.com>
Thu, 10 Sep 2015 07:36:58 +0000 (09:36 +0200)
committerb <rowerynaksiezycu@gmail.com>
Thu, 10 Sep 2015 07:36:58 +0000 (09:36 +0200)
definitions.h [changed mode: 0644->0755]
main.c [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
diff --git a/main.c b/main.c
old mode 100644 (file)
new mode 100755 (executable)
index 86fb16f..c2e9f14
--- a/main.c
+++ b/main.c
@@ -10,7 +10,7 @@ unsigned char settime[3];
 unsigned char mode;\r
 unsigned char alarm[3];\r
 unsigned char lastcdn[2];\r
-unsigned char lastyear[2];\r
+unsigned char lastyear[5];\r
 unsigned char digit;\r
 unsigned char changed;\r
 unsigned char run;\r
@@ -138,6 +138,7 @@ void spi(
     }\r
   }\r
   P3OUT|=0x01; //chip select up\r
+  for(i=0;i<255;++i); //looks like the RTC might not like another SPI transmission exactly before the previous one, sometimes.\r
 }\r
 \r
 #pragma vector=PORT2_VECTOR\r
@@ -158,7 +159,7 @@ __interrupt void P2_ISR()
     time[7]=inbuf[7];//year\r
     leap=inbuf[6]&0x20;\r
     \r
-    spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (?)\r
+    spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (probably not needed anymore?)\r
     if(!((time[2]^alarm[1])|(time[3]^alarm[2])) && alarm[0])\r
     {\r
       mode=MODE_ALARM;\r
@@ -200,20 +201,33 @@ __interrupt void P2_ISR()
       //P2SEL&= ~0x40;\r
     }\r
     \r
+    lastyear[4]=(time[7]==0x00 && lastyear[0]==0x99)?__bcd_add_short(lastyear[1],0x01):lastyear[1];\r
     if (time[7]!=lastyear[0])\r
     {\r
-      if(time[7]<lastyear[0])\r
-        lastyear[1]=__bcd_add_short(lastyear[1],0x01);\r
-      \r
-      lastyear[0]=time[7];\r
+      if(time[7]==lastyear[2])\r
+        ++(lastyear[3]);\r
+      else\r
+        lastyear[3]=0;\r
       \r
-      outbuf[0]=ALARMVALUE;\r
-      outbuf[1]=lastyear[0];\r
-      outbuf[2]=lastyear[1];\r
+      lastyear[2]=time[7];\r
       \r
-      spi(RTC_WRITE,0x26,inbuf,outbuf,3);\r
+      if(lastyear[3]>55)\r
+      {\r
+        lastyear[3]=0;\r
+        \r
+        if(time[7]==0x00 && lastyear[0]==0x99)\r
+          lastyear[1]=__bcd_add_short(lastyear[1],0x01);\r
+        \r
+        lastyear[0]=time[7];\r
+        \r
+        outbuf[0]=ALARMVALUE;\r
+        outbuf[1]=lastyear[0];\r
+        outbuf[2]=lastyear[1];\r
+        spi(RTC_WRITE,0x26,inbuf,outbuf,3);\r
+      }      \r
     }\r
     \r
+    \r
     if(mode==MODE_DEBUG && changed)\r
       spi(RTC_READ,settime[1],settime,outbuf,1);\r
   }\r
@@ -601,8 +615,8 @@ __interrupt void P2_ISR()
     {\r
       dispvalue[0]=symbol[time[7]&0x0f];\r
       dispvalue[1]=symbol[(time[7]&0xf0)>>4];\r
-      dispvalue[2]=symbol[lastyear[1]&0x0f];\r
-      dispvalue[3]=symbol[(lastyear[1]&0xf0)>>4]&(alarm[0]?SYMB_DOT:SYMB_NUL);\r
+      dispvalue[2]=symbol[lastyear[4]&0x0f];\r
+      dispvalue[3]=symbol[(lastyear[4]&0xf0)>>4]&(alarm[0]?SYMB_DOT:SYMB_NUL);\r
     }\r
     else\r
     {\r
@@ -642,7 +656,7 @@ __interrupt void P2_ISR()
     else\r
     {\r
       dispvalue[0]=SYMB_NUL;\r
-      dispvalue[1]=SYMB_3&SYMB_DOT;\r
+      dispvalue[1]=SYMB_4&SYMB_DOT;\r
       dispvalue[2]=SYMB_1;\r
       dispvalue[3]=SYMB_NUL&(alarm[0]?SYMB_DOT:SYMB_NUL);\r
     }\r
@@ -739,8 +753,15 @@ int main( void )
   ME2|=USPIE0;\r
   U0CTL &= ~SWRST;\r
   \r
+  \r
   //init RTC\r
   \r
+  //Looks like sometimes the first SPI reads rom the RTC may be incorrect.\r
+  //To be sure, let's do a few redundant reads before starting the real work\r
+  \r
+  for (delay = 0xFF; delay > 0; --delay)\r
+     spi(RTC_READ,0x00,inbuf,outbuf,1);\r
+  \r
   //set 24 hour mode\r
   spi(RTC_READ,0x03,inbuf,outbuf,1);\r
   if(inbuf[0]&0x40)\r
@@ -772,7 +793,7 @@ int main( void )
   alarm[1]=inbuf[1];\r
   alarm[2]=inbuf[2];\r
   \r
-  spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (?)\r
+  spi(RTC_READ,0x2f,inbuf,outbuf,1); // <- because of RTC bug (?)\r
   \r
   //get last countdown value from RTC SRAM\r
   spi(RTC_READ,0x23,inbuf,outbuf,3);\r
@@ -787,7 +808,7 @@ int main( void )
     lastcdn[1]=0;\r
   }\r
   \r
-  spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (?)\r
+  spi(RTC_READ,0x2f,inbuf,outbuf,1); // <- because of RTC bug (?)\r
   \r
   //get last year value from RTC SRAM\r
   spi(RTC_READ,0x26,inbuf,outbuf,3);\r
@@ -801,6 +822,7 @@ int main( void )
     lastyear[0]=0x00;\r
     lastyear[1]=0x20;\r
   }\r
+  lastyear[3]=0;\r
   \r
   //start oscillator\r
   spi(RTC_READ,0x01,inbuf,outbuf,1);\r