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)
42 EEADR = (offset & 0x0ff);
43 #if defined(pic46k20) || defined(pic26k20)
44 EEADRH = (offset & 0x300)>>8;
54 unsigned char eeprom_writebyte(unsigned int offset, unsigned char data)
58 EEADR = offset & 0x0ff;
59 #if defined(pic46k20) || defined(pic26k20)
60 EEADRH = (offset & 0x300)>>8;
62 // set eecon to write.
67 INTCONbits.GIE = 0; // Disable Interrupt
68 // Dummy write , needs from datasheet.
75 } while(EECON1bits.WR != 0);
76 // if(EECON1bits.WRERR != 0){
77 // Write-Error occured.
78 // EECON1bits.WREN = 0;
79 // INTCONbits.GIE = 1;
83 INTCONbits.GIE = 1; // Enable Interrupt
88 unsigned char readbyte_eeprom(unsigned int p, unsigned int *sum)
94 b = eeprom_readbyte(p);
95 *sum = calcsum_byte(*sum, b);
100 unsigned int readword_eeprom(unsigned int p, unsigned int *sum)
107 h = readbyte_eeprom(p, sum);
108 l = readbyte_eeprom(p + 1, sum);
116 unsigned int calcsum_byte(unsigned int seed, unsigned char byte)
121 unsigned char checksum_eeprom(unsigned int seed, unsigned int offset, unsigned int bytes)
124 unsigned int p = offset;
125 unsigned int sum = seed;
129 for(i = 0; i < bytes; i++) {
130 d = eeprom_readbyte(p);
131 sum = calcsum_byte(sum, d);
134 check = (eeprom_readbyte(p) << 8) + eeprom_readbyte(p+1);
135 if(check != sum) return 0x00; // Bad
139 unsigned int writebyte_eeprom(unsigned int p, unsigned int *sum, unsigned char b)
142 if(eeprom_writebyte(p, b) == 0) return p; // Error
143 *sum = calcsum_byte(*sum, b);
147 unsigned int writeword_eeprom(unsigned int p, unsigned int *sum, unsigned int word)
150 if(writebyte_eeprom(p, sum, word >> 8) == 0) return p; // Error
151 if(writebyte_eeprom(p + 1, sum, word & 0xff) == 0) return p + 1; // Error
156 unsigned int format_eeprom(unsigned int start, unsigned int bytes)
159 unsigned int p = start;
161 for(i = 0; i < bytes; i++) {
163 if(eeprom_writebyte(p, 0xff) == 0) {
167 // print_numeric_nosupress(i, 3);
172 return 0xffff; // Normal end