OSDN Git Service

[Document] Release to v1.0beta2.
[openi2cradio/OpenI2CRadio.git] / eeprom.c
index 96e0f5e..50304a7 100644 (file)
--- a/eeprom.c
+++ b/eeprom.c
  *  the executable file might be covered by the GNU General Public License.
  */
 
+#if defined(__SDCC)
 #include <sdcc-lib.h>
 #include <pic18fregs.h> /* ONLY FOR PIC18x */
+#else
+#include <xc.h>
+#endif
 #include <signal.h>
 #include "iodef.h"
 #include "ui.h"
@@ -64,14 +68,18 @@ unsigned char eeprom_writebyte(unsigned int offset, unsigned char data)
     EECON1bits.CFGS  = 0;
     EECON1bits.WREN = 1;
 
-    INTCONbits.GIE = 0; // Disable Interrupt
+//    INTCONbits.GIE = 0; // Disable Interrupt
     // Dummy write , needs from datasheet.
     EECON2 = 0x55;
     EECON2 = 0xaa;
     EECON1bits.WR = 1;
 
     do {
+#ifdef __SDCC
         delay1ktcy(8);
+#else
+        __delay_ms(1);
+#endif 
     } while(EECON1bits.WR != 0);
   //  if(EECON1bits.WRERR != 0){
         // Write-Error occured.
@@ -80,41 +88,40 @@ unsigned char eeprom_writebyte(unsigned int offset, unsigned char data)
   //     return 0; // Error
   //  }
     // Write OK.
-    INTCONbits.GIE = 1; // Enable Interrupt
+//    INTCONbits.GIE = 1; // Enable Interrupt
     EECON1bits.WREN = 0;
     return 0xff;
 }
 
-unsigned int readword_eeprom(unsigned int p, unsigned int *sum)
+unsigned char readbyte_eeprom(unsigned int *p, unsigned int *sum)
 {
-    unsigned char h,l;
-    unsigned int s;
+    unsigned char b;
 
     ClrWdt();
 
-    h = eeprom_readbyte(p);
-    *sum = calcsum_byte(*sum, h);
-
-    l = eeprom_readbyte(p + 1);
-    *sum = calcsum_byte(*sum, l);
+    b = eeprom_readbyte(*p);
+    *sum = calcsum_byte(*sum, b);
+    *p = *p + 1;
 
-    s = (h << 8) | l;
-    return s;
+    return b;
 }
 
-unsigned char readbyte_eeprom(unsigned int p, unsigned int *sum)
+unsigned int readword_eeprom(unsigned int *p, unsigned int *sum)
 {
-    unsigned char b;
+    unsigned char h,l;
+    unsigned int s;
 
-    ClrWdt();
+//    ClrWdt();
 
-    b = eeprom_readbyte(p);
-    *sum = calcsum_byte(*sum, b);
+    h = readbyte_eeprom(p, sum);
+    l = readbyte_eeprom(p, sum);
 
-    return b;
+    s = (h << 8) | l;
+    return s;
 }
 
 
+
 unsigned int calcsum_byte(unsigned int seed, unsigned char byte)
 {
     return seed + byte;
@@ -138,22 +145,20 @@ unsigned char checksum_eeprom(unsigned int seed, unsigned int offset, unsigned i
     return 0xff;
 }
 
-unsigned int writeword_eeprom(unsigned int p, unsigned int *sum, unsigned int word)
+unsigned int writebyte_eeprom(unsigned int *p, unsigned int *sum, unsigned char b)
 {
     ClrWdt();
-    if(eeprom_writebyte(p, word >> 8) == 0) return p; // Error
-    *sum = calcsum_byte(*sum, word >> 8);
-
-    if(eeprom_writebyte(p + 1, word & 0xff) == 0) return p+1; // Error
-    *sum = calcsum_byte(*sum, word & 0xff);
+    if(eeprom_writebyte(*p, b) == 0) return *p; // Error
+    *sum = calcsum_byte(*sum, b);
+    *p = *p + 1;
     return 0xffff;
 }
 
-unsigned int writebyte_eeprom(unsigned int p, unsigned int *sum, unsigned char b)
+unsigned int writeword_eeprom(unsigned int *p, unsigned int *sum, unsigned int word)
 {
-    ClrWdt();
-    if(eeprom_writebyte(p, b) == 0) return p; // Error
-    *sum = calcsum_byte(*sum, b);
+//    ClrWdt();
+    if(writebyte_eeprom(p, sum, word >> 8) == 0) return *p; // Error
+    if(writebyte_eeprom(p, sum, word & 0xff) == 0) return *p; // Error
     return 0xffff;
 }
 
@@ -175,4 +180,4 @@ unsigned int format_eeprom(unsigned int start, unsigned int bytes)
     _CLS();
     _LOCATE(0,0);
     return 0xffff; // Normal end
-}
\ No newline at end of file
+}