* the executable file might be covered by the GNU General Public License.
*/
-#include "iodef.h"
+#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"
+#include "eeprom.h"
+#include "idle.h"
+#include "ioports.h"
unsigned char eeprom_readbyte(unsigned int offset)
{
- unsigned char eecon1;
// Set address.
EEADR = (offset & 0x0ff);
EEADRH = (offset & 0x300)>>8;
#endif
// set eecon to read.
- eecon1 = EECON1;
- eecon1 &= ~(_EEPGD | _CFGS);
- EECON1 = eecon1 | _RD;
+ EECON1bits.EEPGD = 0;
+ EECON1bits.CFGS = 0;
+ EECON1bits.RD = 1;
return EEDATA;
}
unsigned char eeprom_writebyte(unsigned int offset, unsigned char data)
{
- unsigned char eecon1;
-
+ EEDATA = data;
// Set address.
- EEADR = (offset & 0x0ff);
+ EEADR = offset & 0x0ff;
#if defined(pic46k20) || defined(pic26k20)
EEADRH = (offset & 0x300)>>8;
#endif
- EEDATA = data;
- // set eecon to read.
- eecon1 = EECON1;
- eecon1 &= ~(_EEPGD | _CFGS );
- EECON1 = eecon1 | _WREN;
- INTCONbits.GIE = 0; // Disable Interrupt
+ // set eecon to write.
+ EECON1bits.EEPGD = 0;
+ EECON1bits.CFGS = 0;
+ EECON1bits.WREN = 1;
+
+// INTCONbits.GIE = 0; // Disable Interrupt
// Dummy write , needs from datasheet.
EECON2 = 0x55;
- Nop();
EECON2 = 0xaa;
- Nop();
- eecon1 = EECON1;
- Nop();
- INTCONbits.GIE = 1; // Enable Interrupt
- EECON1 = eecon1 & ~_WREN;
-
- // Verify
- if(eeprom_readbyte(offset) != data) {
- return 0x00; // Bad
- } else {
- return 0xff;
- }
+ EECON1bits.WR = 1;
+
+ do {
+#ifdef __SDCC
+ delay1ktcy(8);
+#else
+ __delay_ms(1);
+#endif
+ } while(EECON1bits.WR != 0);
+ // if(EECON1bits.WRERR != 0){
+ // Write-Error occured.
+ // EECON1bits.WREN = 0;
+ // INTCONbits.GIE = 1;
+ // return 0; // Error
+ // }
+ // Write OK.
+// INTCONbits.GIE = 1; // Enable Interrupt
+ EECON1bits.WREN = 0;
+ return 0xff;
+}
+
+unsigned char readbyte_eeprom(unsigned int *p, unsigned int *sum)
+{
+ unsigned char b;
+
+ ClrWdt();
+
+ b = eeprom_readbyte(*p);
+ *sum = calcsum_byte(*sum, b);
+ *p = *p + 1;
+
+ return b;
}
+unsigned int readword_eeprom(unsigned int *p, unsigned int *sum)
+{
+ unsigned char h,l;
+ unsigned int s;
+
+// ClrWdt();
+
+ h = readbyte_eeprom(p, sum);
+ l = readbyte_eeprom(p, sum);
+
+ s = (h << 8) | l;
+ return s;
+}
+
+
+
unsigned int calcsum_byte(unsigned int seed, unsigned char byte)
{
return seed + byte;
return 0xff;
}
+unsigned int writebyte_eeprom(unsigned int *p, unsigned int *sum, unsigned char b)
+{
+ ClrWdt();
+ if(eeprom_writebyte(*p, b) == 0) return *p; // Error
+ *sum = calcsum_byte(*sum, b);
+ *p = *p + 1;
+ return 0xffff;
+}
+
+unsigned int writeword_eeprom(unsigned int *p, unsigned int *sum, unsigned int word)
+{
+// 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;
+}
+
+
unsigned int format_eeprom(unsigned int start, unsigned int bytes)
{
unsigned int i;
unsigned int p = start;
for(i = 0; i < bytes; i++) {
- if(eeprom_writebyte(p, 0xff) == 0) return p;
- p++;
+ ClrWdt();
+ if(eeprom_writebyte(p, 0xff) == 0) {
+ return p;
+ }
+// _LOCATE(0,0);
+// print_numeric_nosupress(i, 3);
+// p = p + 1;
}
+ _CLS();
+ _LOCATE(0,0);
return 0xffff; // Normal end
-}
\ No newline at end of file
+}