3 * Internal eeprom Handler
4 * Copyright (C) 2013-06-13 K.Ohta <whatisthis.sowhat ai gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
11 * This library / program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this library; see the file COPYING. If not, write to the
18 * Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21 * As a special exception, if you link this(includeed from sdcc) library
22 * with other files, some of which are compiled with SDCC,
23 * to produce an executable, this library does not by itself cause
24 * the resulting executable to be covered by the GNU General Public License.
25 * This exception does not however invalidate any other reasons why
26 * the executable file might be covered by the GNU General Public License.
31 #include <pic18fregs.h> /* ONLY FOR PIC18x */
42 unsigned char eeprom_readbyte(unsigned int offset)
46 EEADR = (offset & 0x0ff);
47 #if defined(pic46k20) || defined(pic26k20)
48 EEADRH = (offset & 0x300)>>8;
58 unsigned char eeprom_writebyte(unsigned int offset, unsigned char data)
62 EEADR = offset & 0x0ff;
63 #if defined(pic46k20) || defined(pic26k20)
64 EEADRH = (offset & 0x300)>>8;
66 // set eecon to write.
71 // INTCONbits.GIE = 0; // Disable Interrupt
72 // Dummy write , needs from datasheet.
83 } while(EECON1bits.WR != 0);
84 // if(EECON1bits.WRERR != 0){
85 // Write-Error occured.
86 // EECON1bits.WREN = 0;
87 // INTCONbits.GIE = 1;
91 // INTCONbits.GIE = 1; // Enable Interrupt
96 unsigned char readbyte_eeprom(unsigned int *p, unsigned int *sum)
102 b = eeprom_readbyte(*p);
103 *sum = calcsum_byte(*sum, b);
109 unsigned int readword_eeprom(unsigned int *p, unsigned int *sum)
116 h = readbyte_eeprom(p, sum);
117 l = readbyte_eeprom(p, sum);
125 unsigned int calcsum_byte(unsigned int seed, unsigned char byte)
130 unsigned char checksum_eeprom(unsigned int seed, unsigned int offset, unsigned int bytes)
133 unsigned int p = offset;
134 unsigned int sum = seed;
138 for(i = 0; i < bytes; i++) {
139 d = eeprom_readbyte(p);
140 sum = calcsum_byte(sum, d);
143 check = (eeprom_readbyte(p) << 8) + eeprom_readbyte(p+1);
144 if(check != sum) return 0x00; // Bad
148 unsigned int writebyte_eeprom(unsigned int *p, unsigned int *sum, unsigned char b)
151 if(eeprom_writebyte(*p, b) == 0) return *p; // Error
152 *sum = calcsum_byte(*sum, b);
157 unsigned int writeword_eeprom(unsigned int *p, unsigned int *sum, unsigned int word)
160 if(writebyte_eeprom(p, sum, word >> 8) == 0) return *p; // Error
161 if(writebyte_eeprom(p, sum, word & 0xff) == 0) return *p; // Error
166 unsigned int format_eeprom(unsigned int start, unsigned int bytes)
169 unsigned int p = start;
171 for(i = 0; i < bytes; i++) {
173 if(eeprom_writebyte(p, 0xff) == 0) {
177 // print_numeric_nosupress(i, 3);
182 return 0xffff; // Normal end