OSDN Git Service

[EEPROM] Move some EEPROM related routines frome main.c to eeprom.c .
[openi2cradio/OpenI2CRadio.git] / eeprom.c
1 /*
2  * OpenI2CRADIO
3  * Internal eeprom Handler
4  * Copyright (C) 2013-06-13 K.Ohta <whatisthis.sowhat ai gmail.com>
5  * License: GPL2+LE
6  *
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.
15  *
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,
19  *  MA 02110-1301, USA.
20  *
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.
27  */
28
29 #include <sdcc-lib.h>
30 #include <pic18fregs.h> /* ONLY FOR PIC18x */
31 #include <signal.h>
32 #include "iodef.h"
33 #include "ui.h"
34 #include "eeprom.h"
35 #include "idle.h"
36 #include "ioports.h"
37
38 unsigned char eeprom_readbyte(unsigned int offset)
39 {
40     unsigned char eecon1;
41
42     // Set address.
43     EEADR = (offset & 0x0ff);
44 #if defined(pic46k20) || defined(pic26k20)
45     EEADRH = (offset & 0x300)>>8;
46 #endif
47     // set eecon to read.
48     EECON1bits.EEPGD = 0;
49     EECON1bits.CFGS  = 0;
50     EECON1bits.RD    = 1;
51
52     return EEDATA;
53 }
54
55 unsigned char eeprom_writebyte(unsigned int offset, unsigned char data)
56 {
57     EEDATA = data;
58     // Set address.
59     EEADR = offset & 0x0ff;
60 #if defined(pic46k20) || defined(pic26k20)
61     EEADRH = (offset & 0x300)>>8;
62 #endif
63     // set eecon to write.
64     EECON1bits.EEPGD = 0;
65     EECON1bits.CFGS  = 0;
66     EECON1bits.WREN = 1;
67
68     INTCONbits.GIE = 0; // Disable Interrupt
69     // Dummy write , needs from datasheet.
70     EECON2 = 0x55;
71     EECON2 = 0xaa;
72     EECON1bits.WR = 1;
73     do {
74         Nop();
75         Nop();
76     } while(EECON1bits.WR != 0);
77     
78     if(EECON1bits.WRERR != 0){
79         // Write-Error occured.
80        EECON1bits.WREN = 0;
81        INTCONbits.GIE = 1;
82        return 0; // Error
83     }
84     // Write OK.
85     INTCONbits.GIE = 1; // Enable Interrupt
86     EECON1bits.WREN = 0;
87     return 0xff;
88 }
89
90 unsigned int readword_eeprom(unsigned int p, unsigned int *sum)
91 {
92     unsigned char h,l;
93     unsigned int s;
94
95     ClrWdt();
96
97     h = eeprom_readbyte(p);
98     *sum = calcsum_byte(*sum, h);
99
100     l = eeprom_readbyte(p + 1);
101     *sum = calcsum_byte(*sum, l);
102
103     s = (h << 8) | l;
104     return s;
105 }
106
107 unsigned char readbyte_eeprom(unsigned int p, unsigned int *sum)
108 {
109     unsigned char b;
110
111     ClrWdt();
112
113     b = eeprom_readbyte(p);
114     *sum = calcsum_byte(*sum, b);
115
116     return b;
117 }
118
119
120 unsigned int calcsum_byte(unsigned int seed, unsigned char byte)
121 {
122     return seed + byte;
123 }
124
125 unsigned char checksum_eeprom(unsigned int seed, unsigned int offset, unsigned int bytes)
126 {
127     unsigned int i;
128     unsigned int p = offset;
129     unsigned int sum = seed;
130     unsigned char d;
131     unsigned int check;
132
133     for(i = 0; i < bytes; i++) {
134         d = eeprom_readbyte(p);
135         sum = calcsum_byte(sum, d);
136         p++;
137     }
138     check = (eeprom_readbyte(p) << 8) + eeprom_readbyte(p+1);
139     if(check != sum) return 0x00; // Bad
140     return 0xff;
141 }
142
143 unsigned int writeword_eeprom(unsigned int p, unsigned int *sum, unsigned int word)
144 {
145     ClrWdt();
146     if(eeprom_writebyte(p, word >> 8) == 0) return p; // Error
147     *sum = calcsum_byte(*sum, word >> 8);
148
149     if(eeprom_writebyte(p + 1, word & 0xff) == 0) return p+1; // Error
150     *sum = calcsum_byte(*sum, word & 0xff);
151     return 0xffff;
152 }
153
154 unsigned int writebyte_eeprom(unsigned int p, unsigned int *sum, unsigned char b)
155 {
156     ClrWdt();
157     if(eeprom_writebyte(p, b) == 0) return p; // Error
158     *sum = calcsum_byte(*sum, b);
159     return 0xffff;
160 }
161
162
163 unsigned int format_eeprom(unsigned int start, unsigned int bytes)
164 {
165     unsigned int i;
166     unsigned int p = start;
167     
168     for(i = 0; i < bytes; i++) {
169         ClrWdt();
170         if(eeprom_writebyte(p, 0xff) == 0) {
171             return p;
172         }
173 //        _LOCATE(0,0);
174 //        print_numeric_nosupress(i, 3);
175 //        p = p + 1;
176     }
177     _CLS();
178     _LOCATE(0,0);
179     return 0xffff; // Normal end
180 }