OSDN Git Service

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