OSDN Git Service

[SCHEMATIC] Modify SW/MW/LW Preamp, insert galbanic-isolator replace of common-mode...
[openi2cradio/OpenI2CRadio.git] / eepromutil.c
1 /*
2  * OpenI2CRADIO
3  * EEPROM utils
4  * Copyright (C) 2013-06-10 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 <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #if defined(__SDCC)
34 #include <sdcc-lib.h>
35 #include <pic18fregs.h> /* ONLY FOR PIC18x */
36 #include <delay.h>
37 #else
38 #include <xc.h>
39 #endif
40 #include <signal.h>
41
42 #include "commondef.h"
43 #include "iodef.h"
44 #include "idle.h"
45 #include "i2c_io.h"
46 #include "i2c_eeprom.h"
47 #include "akc6955.h"
48 #include "lcd_acm1602.h"
49 #include "ui.h"
50 #include "eeprom.h"
51 #include "ioports.h"
52 #include "menu.h"
53 #include "power.h"
54 #include "adc_int.h"
55
56 void save_eeprom(void)
57 {
58     unsigned int p[1];
59     unsigned int sum[1];
60         unsigned char i;
61     unsigned char j;
62     unsigned char *q = (char *)(&setup);
63
64     *p = 0;
65     *sum = 0;
66     // Magic word
67     writeword_eeprom(p, sum, EEPROM_INTSET_MAGIC);
68     // amfreq
69     for(i = 0; i < sizeof(__radioset_t); i++) {
70         writebyte_eeprom(p, sum, *q);
71         q++;
72     }
73
74     // Write checksum
75     eeprom_writebyte(*p, sum[0] >> 8);
76     eeprom_writebyte(*p + 1, sum[0] & 0xff);
77 }
78
79
80 unsigned int calcsum_frequencies(__freqset_t *p, unsigned int start)
81 {
82     unsigned int sum = start;
83     unsigned char *q = (unsigned char *)p;
84     unsigned char c;
85     unsigned char i;
86
87     for(i = 0; i < (sizeof(__freqset_t) - 2); i++){
88         c = *q;
89         q++;
90         sum = sum + c;
91     }
92     return sum;
93 }
94 /*
95   * External I2C Map:
96  *  0x0000
97  *  0x01FF : User settings(Reserved not use)
98  *  0x0100
99  *  0x01ff : User bands
100  *  0x0200
101  *  0x0fff : User frequencies ( 12*2*28 = 672chs Max.)
102  *  0x1000- : Reserve.
103  */
104 int load_frequencies(unsigned int page, unsigned char check_only)
105 {
106
107     __freqset_t *p;
108     unsigned int sum;
109     unsigned int addr = (page << 7) + EEPROM_FREQSET_ADDR;
110     unsigned char *q;
111     unsigned char *r;
112     unsigned char c;
113     unsigned int i;
114
115 //    if(addr > 0x0fff) return -1; // Address error
116     if(page > USER_MEMORY_BANKS) return -1; // Address Error
117     p = &freqset_temp;
118     c = i2c_eeprom_burstread(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__freqset_t));
119     // Pass 1 Read OK?
120     if(c == 0x00) return -2;
121     // Pass 1 check magic
122     if(freqset_temp.magic != EEPROM_FREQSET_MAGIC) return -3;
123     // Pass 2 check sum
124     sum = calcsum_frequencies(&freqset_temp, 0xa5a5);
125     if(sum != freqset_temp.checksum) return -4;
126     // Move from
127     if(check_only == 0x00){
128         q = (unsigned char *)(&freqset_temp);
129         r = (unsigned char *)(&freqset);
130         for(i = 0; i < sizeof(__freqset_t); i++){
131             *r = *q;
132             q++;
133             r++;
134         }
135         setup.pagenum = page;
136     }
137     return 0;
138 }
139
140
141
142 void save_frequencies(void)
143 {
144     unsigned char c;
145     unsigned int addr = (setup.pagenum <<7) + EEPROM_FREQSET_ADDR;
146     __freqset_t *p = &freqset;
147     unsigned char i;
148     unsigned int sum;
149
150     if(setup.pagenum > USER_MEMORY_BANKS) return; // Address Error
151     // Pass 0 Calc checksum
152     freqset.magic = EEPROM_FREQSET_MAGIC;
153     freqset.version = 0x0001;
154     sum = calcsum_frequencies(&freqset, 0xa5a5);
155     freqset.checksum = sum;
156     // Pass 1 write to rom.
157     c = i2c_eeprom_burstwrite(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__freqset_t));
158     return;
159 }
160
161 char save_frequencies_page(unsigned char page)
162 {
163     unsigned char c;
164     unsigned int addr = (page <<7) + EEPROM_FREQSET_ADDR;
165     __freqset_t *p = &freqset_temp;
166     unsigned char i;
167     unsigned int sum;
168
169     if(page > USER_MEMORY_BANKS) return -3; // Address Error
170     // Pass 0 Calc checksum
171     if(p->magic != EEPROM_FREQSET_MAGIC) return -1; // Data error
172     sum = calcsum_frequencies(p, 0xa5a5);
173     if(p->checksum != sum) return -2; // Sum error
174     // Pass 1 write to rom.
175     c = i2c_eeprom_burstwrite(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__freqset_t));
176     return 0;
177 }
178
179
180 void format_frequencies(unsigned int page)
181 {
182     unsigned char i;
183     unsigned int sum;
184
185     freqset_temp.magic = EEPROM_FREQSET_MAGIC;
186     freqset_temp.version = 0x0001;
187     for(i = 0; i < USER_MEMORY_NUM; i++){
188         freqset_temp.memoryfreqs[i].band = AKC6955_BAND_MW2;
189         freqset_temp.memoryfreqs[i].fm = 0;
190         freqset_temp.memoryfreqs[i].freq = 594;
191     }
192     freqset_temp.pagenum = page;
193     sum = calcsum_frequencies(&freqset_temp, 0xa5a5);
194     freqset_temp.checksum = sum;
195
196     i = i2c_eeprom_burstwrite(I2CEEPROM_ADDR, (page << 7)+ EEPROM_FREQSET_ADDR, (unsigned char *)(&freqset_temp), sizeof(__freqset_t));
197 }
198
199 void check_frequencies(void)
200 {
201     unsigned int i;
202     _CLS();
203     for(i = 0; i < USER_MEMORY_BANKS; i++){
204         printstr("Check ExtROM:");
205         print_numeric_nosupress(i, 2);
206        _HOME();
207         
208         if(load_frequencies(i, 0xff) < 0){
209 //            printstr("Format:");
210 //            print_numeric_nosupress(i, 2);
211             _HOME();
212             format_frequencies(i);
213         }
214     }
215    if(load_userbands() < 0) {
216       format_userbands();
217    }
218 }
219
220 static unsigned int calcsum_userband(__userband_t_t *p, unsigned int start)
221 {
222     unsigned int sum = start;
223     unsigned char *q = (unsigned char *)p;
224     unsigned char c;
225     unsigned char i;
226
227     for(i = 0; i < (sizeof(__userband_t_t) - 2); i++){
228         c = *q;
229         q++;
230         sum = sum + c;
231     }
232     return sum;
233 }
234
235 int load_userbands(void)
236 {
237
238     __userband_t_t *p = &userband;
239     unsigned int sum;
240     unsigned int addr = EEPROM_BANDSET_ADDR;
241     unsigned char c;
242
243 //    if(addr > 0x0fff) return -1; // Address error
244    c = i2c_eeprom_burstread(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__userband_t_t));
245     // Pass 1 Read OK?
246    if(c == 0x00) return -2;
247     // Pass 1 check magic
248    if(p->magic != EEPROM_BANDSET_MAGIC) return -3;
249     // Pass 2 check sum
250    sum = calcsum_userband(p, 0x5a5a);
251    if(sum != p->checksum) return -4;
252    return 0;
253 }
254
255 void save_userbands(void)
256 {
257     unsigned char c;
258     unsigned int addr = EEPROM_BANDSET_ADDR;
259     __userband_t_t *p = &userband;
260     unsigned char i;
261     unsigned int sum;
262
263     // Pass 0 Calc checksum
264     userband.magic = EEPROM_BANDSET_MAGIC;
265     userband.version = 0x0001;
266     sum = calcsum_userband(&userband, 0x5a5a);
267     userband.checksum = sum;
268     // Pass 1 write to rom.
269     c = i2c_eeprom_burstwrite(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__userband_t_t));
270     return;
271 }
272
273 void format_userbands(void)
274 {
275    unsigned char i;
276    for(i = 0; i < USER_BAND_NUM; i++) {
277         userband.am_usrbands[i].start = 0x19;
278         userband.am_usrbands[i].stop = 0x32;
279         userband.am_usrbands[i].freq = 5600; //(0x32 - (0x32-0x19) / 2 *32) * 5
280         userband.am_usrbands[i].mode3k = 0;
281         //
282         userband.fm_usrbands[i].start = 0x19;
283         userband.fm_usrbands[i].stop = 0x32;
284         userband.fm_usrbands[i].freq = 8600;
285         userband.fm_usrbands[i].mode3k = 0;
286    }
287    save_userbands(); // Re-init
288 }
289
290
291 unsigned char load_eeprom(void)
292 {
293     unsigned int p[1];
294     unsigned int sum[1];
295     unsigned char i;
296     unsigned char *q = (unsigned char *)(&setup);
297     unsigned char j;
298     unsigned int magic;
299
300     p[0] = 0;
301     sum[0] = 0;
302     // Magic word
303     magic = readword_eeprom(p, sum);
304     if(magic != EEPROM_INTSET_MAGIC) return 0x01; // NO MAGICWORD
305     // amfreq
306     for(i = 0; i < sizeof(__radioset_t); i++ ){
307         *q = readbyte_eeprom(p, sum);
308         q++;
309     }
310     magic = (eeprom_readbyte(p[0]) << 8) + eeprom_readbyte(p[0] + 1);
311
312     if(sum[0] != magic) return 0x00;
313     return 0xff;
314 }
315
316 /*
317  * Check eeprom, and format/restore.
318  */
319 static void check_eeprom_sub(void)
320 {
321  _CLS();
322  _LOCATE_0_0();
323  printstr("Formatting...  ");
324  format_eeprom(2,250);
325  _LOCATE_0_0();
326  printstr("Save defaults  ");
327  setdefault();
328  save_eeprom();
329 }
330
331
332 void check_eeprom(void)
333 {
334     unsigned char c;
335
336     switch(load_eeprom()) {
337         case 0x01: // No magic-word
338 //            idle_time_ms(2000);
339             c = printhelp_2lines("EEPROM FORMAT", "Press any key");
340             check_eeprom_sub();
341             break;
342         case 0x00: // Checksum error
343 //           idle_time_ms(2000);
344             c = printhelp_2lines("X-) Sum error", "Press any key");
345             check_eeprom_sub();
346             break;
347         case 0xff: // Success
348             check_frequencies();
349             load_frequencies(setup.pagenum, 0);
350             return;
351             break;
352         default: // Unknown error
353             break;
354     }
355     valinit();
356     check_frequencies();
357     load_frequencies(setup.pagenum, 0);
358 }