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.
30 #include <pic18fregs.h> /* ONLY FOR PIC18x */
38 unsigned char eeprom_readbyte(unsigned int offset)
43 EEADR = (offset & 0x0ff);
44 #if defined(pic46k20) || defined(pic26k20)
45 EEADRH = (offset & 0x300)>>8;
55 unsigned char eeprom_writebyte(unsigned int offset, unsigned char data)
59 EEADR = offset & 0x0ff;
60 #if defined(pic46k20) || defined(pic26k20)
61 EEADRH = (offset & 0x300)>>8;
63 // set eecon to write.
68 INTCONbits.GIE = 0; // Disable Interrupt
69 // Dummy write , needs from datasheet.
76 } while(EECON1bits.WR != 0);
78 if(EECON1bits.WRERR != 0){
79 // Write-Error occured.
85 INTCONbits.GIE = 1; // Enable Interrupt
90 unsigned int readword_eeprom(unsigned int p, unsigned int *sum)
97 h = eeprom_readbyte(p);
98 *sum = calcsum_byte(*sum, h);
100 l = eeprom_readbyte(p + 1);
101 *sum = calcsum_byte(*sum, l);
107 unsigned char readbyte_eeprom(unsigned int p, unsigned int *sum)
113 b = eeprom_readbyte(p);
114 *sum = calcsum_byte(*sum, b);
120 unsigned int calcsum_byte(unsigned int seed, unsigned char byte)
125 unsigned char checksum_eeprom(unsigned int seed, unsigned int offset, unsigned int bytes)
128 unsigned int p = offset;
129 unsigned int sum = seed;
133 for(i = 0; i < bytes; i++) {
134 d = eeprom_readbyte(p);
135 sum = calcsum_byte(sum, d);
138 check = (eeprom_readbyte(p) << 8) + eeprom_readbyte(p+1);
139 if(check != sum) return 0x00; // Bad
143 unsigned int writeword_eeprom(unsigned int p, unsigned int *sum, unsigned int word)
146 if(eeprom_writebyte(p, word >> 8) == 0) return p; // Error
147 *sum = calcsum_byte(*sum, word >> 8);
149 if(eeprom_writebyte(p + 1, word & 0xff) == 0) return p+1; // Error
150 *sum = calcsum_byte(*sum, word & 0xff);
154 unsigned int writebyte_eeprom(unsigned int p, unsigned int *sum, unsigned char b)
157 if(eeprom_writebyte(p, b) == 0) return p; // Error
158 *sum = calcsum_byte(*sum, b);
163 unsigned int format_eeprom(unsigned int start, unsigned int bytes)
166 unsigned int p = start;
168 for(i = 0; i < bytes; i++) {
170 if(eeprom_writebyte(p, 0xff) == 0) {
174 // print_numeric_nosupress(i, 3);
179 return 0xffff; // Normal end