OSDN Git Service

[I2C] Test to use Real-machine, but... X-(
[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
41     // Set address.
42     EEADR = (offset & 0x0ff);
43 #if defined(pic46k20) || defined(pic26k20)
44     EEADRH = (offset & 0x300)>>8;
45 #endif
46     // set eecon to read.
47     EECON1bits.EEPGD = 0;
48     EECON1bits.CFGS  = 0;
49     EECON1bits.RD    = 1;
50
51     return EEDATA;
52 }
53
54 unsigned char eeprom_writebyte(unsigned int offset, unsigned char data)
55 {
56     EEDATA = data;
57     // Set address.
58     EEADR = offset & 0x0ff;
59 #if defined(pic46k20) || defined(pic26k20)
60     EEADRH = (offset & 0x300)>>8;
61 #endif
62     // set eecon to write.
63     EECON1bits.EEPGD = 0;
64     EECON1bits.CFGS  = 0;
65     EECON1bits.WREN = 1;
66
67     INTCONbits.GIE = 0; // Disable Interrupt
68     // Dummy write , needs from datasheet.
69     EECON2 = 0x55;
70     EECON2 = 0xaa;
71     EECON1bits.WR = 1;
72
73     do {
74         delay1ktcy(8);
75     } while(EECON1bits.WR != 0);
76   //  if(EECON1bits.WRERR != 0){
77         // Write-Error occured.
78   //     EECON1bits.WREN = 0;
79   //     INTCONbits.GIE = 1;
80   //     return 0; // Error
81   //  }
82     // Write OK.
83     INTCONbits.GIE = 1; // Enable Interrupt
84     EECON1bits.WREN = 0;
85     return 0xff;
86 }
87
88 unsigned char readbyte_eeprom(unsigned int p, unsigned int *sum)
89 {
90     unsigned char b;
91
92     ClrWdt();
93
94     b = eeprom_readbyte(p);
95     *sum = calcsum_byte(*sum, b);
96
97     return b;
98 }
99
100 unsigned int readword_eeprom(unsigned int p, unsigned int *sum)
101 {
102     unsigned char h,l;
103     unsigned int s;
104
105 //    ClrWdt();
106
107     h = readbyte_eeprom(p, sum);
108     l = readbyte_eeprom(p + 1, sum);
109
110     s = (h << 8) | l;
111     return s;
112 }
113
114
115
116 unsigned int calcsum_byte(unsigned int seed, unsigned char byte)
117 {
118     return seed + byte;
119 }
120
121 unsigned char checksum_eeprom(unsigned int seed, unsigned int offset, unsigned int bytes)
122 {
123     unsigned int i;
124     unsigned int p = offset;
125     unsigned int sum = seed;
126     unsigned char d;
127     unsigned int check;
128
129     for(i = 0; i < bytes; i++) {
130         d = eeprom_readbyte(p);
131         sum = calcsum_byte(sum, d);
132         p++;
133     }
134     check = (eeprom_readbyte(p) << 8) + eeprom_readbyte(p+1);
135     if(check != sum) return 0x00; // Bad
136     return 0xff;
137 }
138
139 unsigned int writebyte_eeprom(unsigned int p, unsigned int *sum, unsigned char b)
140 {
141     ClrWdt();
142     if(eeprom_writebyte(p, b) == 0) return p; // Error
143     *sum = calcsum_byte(*sum, b);
144     return 0xffff;
145 }
146
147 unsigned int writeword_eeprom(unsigned int p, unsigned int *sum, unsigned int word)
148 {
149 //    ClrWdt();
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
152     return 0xffff;
153 }
154
155
156 unsigned int format_eeprom(unsigned int start, unsigned int bytes)
157 {
158     unsigned int i;
159     unsigned int p = start;
160     
161     for(i = 0; i < bytes; i++) {
162         ClrWdt();
163         if(eeprom_writebyte(p, 0xff) == 0) {
164             return p;
165         }
166 //        _LOCATE(0,0);
167 //        print_numeric_nosupress(i, 3);
168 //        p = p + 1;
169     }
170     _CLS();
171     _LOCATE(0,0);
172     return 0xffff; // Normal end
173 }