OSDN Git Service

[Backlight] Use PWM mode for backlight, to save battery.
[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 "akc6955.h"
47 #include "lcd_acm1602.h"
48 #include "ui.h"
49 #include "eeprom.h"
50 #include "ioports.h"
51 #include "menu.h"
52 #include "power.h"
53 #include "adc_int.h"
54
55 void save_eeprom(void)
56 {
57     unsigned int p = 0;
58     unsigned int sum = 0x0000;
59     unsigned char i;
60     unsigned char j;
61     unsigned char *q;
62
63     // Magic word
64     writeword_eeprom(p, &sum, 0x1298);
65     p+= 2;
66     // amfreq
67     writeword_eeprom(p, &sum, amfreq);
68     p+= 2;
69     // amfreq
70     writeword_eeprom(p, &sum, fmfreq);
71     p+= 2;
72
73     writebyte_eeprom(p, &sum, amband);
74     p++;
75     writebyte_eeprom(p, &sum, fmband);
76     p++;
77     writebyte_eeprom(p, &sum, fm);
78     p++;
79     writebyte_eeprom(p, &sum, am_mode3k);
80     p++;
81     writebyte_eeprom(p, &sum, am_userbandnum);
82     p++;
83     writebyte_eeprom(p, &sum, fm_userbandnum);
84     p++;
85
86 #if 0
87     for(i = 0 ; i < USER_BAND_NUM; i++){
88         writebyte_eeprom(p, &sum, am_usrbands[i].mode3k);
89         writebyte_eeprom(p + 1, &sum, am_usrbands[i].start);
90         writebyte_eeprom(p + 2, &sum, am_usrbands[i].stop);
91         writeword_eeprom(p + 3, &sum, am_usrbands[i].freq);
92         p += 5;
93     }
94     for(i = 0 ; i < USER_BAND_NUM; i++){
95         writebyte_eeprom(p, &sum, fm_usrbands[i].mode3k);
96         writebyte_eeprom(p + 1, &sum, fm_usrbands[i].start);
97         writebyte_eeprom(p + 2, &sum, fm_usrbands[i].stop);
98         writeword_eeprom(p + 3, &sum, fm_usrbands[i].freq);
99         p += 5;
100     }
101 #else
102     for(j = 0; j < USER_BAND_NUM; j++){
103         q = (unsigned char *)&am_usrbands[j];
104         for(i = 0; i < sizeof(_userband_t); i++){
105             writebyte_eeprom(p, &sum, *q);
106             p++;
107             q++;
108         }
109     }
110     for(j = 0; j < USER_BAND_NUM; j++){
111         q = (unsigned char *)&fm_usrbands[j];
112         for(i = 0; i < sizeof(_userband_t); i++){
113             writebyte_eeprom(p, &sum, *q);
114             p++;
115             q++;
116         }
117     }
118 #endif
119     amfreq_bank[amband] = amfreq;
120     fmfreq_bank[fmband] = fmfreq;
121
122     for(i = 0; i < 19; i++){
123         writeword_eeprom(p    , &sum, amfreq_bank[i]);
124         p += 2;
125     }
126     for(i = 0; i < 8; i++){
127         writeword_eeprom(p    , &sum, fmfreq_bank[i]);
128         p += 2;
129     }
130
131     writebyte_eeprom(p, &sum, threshold_amcnr);
132     p++;
133     writebyte_eeprom(p, &sum, threshold_fmcnr);
134     p++;
135     writebyte_eeprom(p, &sum, threshold_width);
136     p++;
137     writebyte_eeprom(p, &sum, threshold_fmstereo);
138     p++;
139     writebyte_eeprom(p, &sum, lowboost);
140     p++;
141     writebyte_eeprom(p, &sum, stereo);
142     p++;
143
144     writebyte_eeprom(p, &sum, volume);
145     p++;
146     writebyte_eeprom(p, &sum, prevolume);
147     p++;
148     writebyte_eeprom(p, &sum, fmbandwidth);
149     p++;
150     writeword_eeprom(p, &sum, backlight_long);
151     p += 2;
152     writeword_eeprom(p, &sum, ui_idlecount);
153     p += 2;
154     writebyte_eeprom(p, &sum, backlight_level);
155     p += 1;
156
157     // Write checksum
158     eeprom_writebyte(p, sum >> 8);
159     eeprom_writebyte(p + 1, sum & 0xff);
160 }
161
162 unsigned char load_eeprom(void)
163 {
164     unsigned int p = 0;
165     unsigned int sum = 0x0000;
166     unsigned char i;
167     unsigned char *q;
168     unsigned char j;
169     unsigned int magic;
170
171     // Magic word
172     magic = readword_eeprom(p, &sum);
173     if(magic != 0x1298) return 0x01; // NO MAGICWORD
174     p+= 2;
175     // amfreq
176     amfreq = readword_eeprom(p, &sum);
177     p+= 2;
178     // fmfreq
179     fmfreq = readword_eeprom(p, &sum);
180     p+= 2;
181
182     amband = readbyte_eeprom(p, &sum);
183     p++;
184     fmband = readbyte_eeprom(p, &sum);
185     p++;
186     fm = readbyte_eeprom(p, &sum);
187     p++;
188     am_mode3k = readbyte_eeprom(p, &sum);
189     p++;
190     am_userbandnum = readbyte_eeprom(p, &sum);
191     p++;
192     fm_userbandnum = readbyte_eeprom(p, &sum);
193     p++;
194 #if 0
195     for(i = 0 ; i < USER_BAND_NUM; i++){
196         am_usrbands[i].mode3k = readbyte_eeprom(p, &sum);
197         am_usrbands[i].start  = readbyte_eeprom(p + 1, &sum);
198         am_usrbands[i].stop   = readbyte_eeprom(p + 2, &sum);
199         am_usrbands[i].freq   = readword_eeprom(p + 3, &sum);
200         p += 5;
201     }
202     for(i = 0 ; i < USER_BAND_NUM; i++){
203         fm_usrbands[i].mode3k = readbyte_eeprom(p, &sum);
204         fm_usrbands[i].start  = readbyte_eeprom(p + 1, &sum);
205         fm_usrbands[i].stop   = readbyte_eeprom(p + 2, &sum);
206         fm_usrbands[i].freq   = readword_eeprom(p + 3, &sum);
207         p += 5;
208     }
209 #else
210     for(j = 0; j < USER_BAND_NUM; j++){
211         q = (unsigned char *)&am_usrbands[j];
212         for(i = 0; i < sizeof(_userband_t); i++){
213             *q = readbyte_eeprom(p, &sum);
214             p++;
215             q++;
216         }
217         if(am_usrbands[j].mode3k != 0){
218             am_userband_freq[j].start = am_usrbands[j].start * 96;
219             am_userband_freq[j].end   = am_usrbands[j].stop  * 96;
220         } else {
221             am_userband_freq[j].start = am_usrbands[j].start * 160;
222             am_userband_freq[j].end   = am_usrbands[j].stop  * 160;
223         }
224     }
225     for(j = 0; j < USER_BAND_NUM; j++){
226         q = (unsigned char *)&fm_usrbands[j];
227         for(i = 0; i < sizeof(_userband_t); i++){
228             *q = readbyte_eeprom(p, &sum);
229             p++;
230             q++;
231         }
232         fm_userband_freq[j].start = fm_usrbands[j].start * 80 + 3000;
233         fm_userband_freq[j].end   = fm_usrbands[j].stop * 80 + 3000;
234     }
235 #endif
236     amfreq = amfreq_bank[amband];
237     fmfreq = fmfreq_bank[fmband];
238     for(i = 0; i < 19; i++){
239         amfreq_bank[i] = readword_eeprom(p    , &sum);
240         p += 2;
241     }
242     for(i = 0; i < 8; i++){
243         fmfreq_bank[i] = readword_eeprom(p    , &sum);
244         p += 2;
245     }
246     threshold_amcnr = readbyte_eeprom(p, &sum);
247     p++;
248     threshold_fmcnr = readbyte_eeprom(p, &sum);
249     p++;
250     threshold_width = readbyte_eeprom(p, &sum);
251     p++;
252     threshold_fmstereo = readbyte_eeprom(p, &sum);
253     p++;
254     lowboost = readbyte_eeprom(p    , &sum);
255     p++;
256     stereo = readbyte_eeprom(p    , &sum);
257     p++;
258     volume = readbyte_eeprom(p, &sum);
259     p++;
260     prevolume = readbyte_eeprom(p, &sum);
261     p++;
262     fmbandwidth = readbyte_eeprom(p, &sum);
263     p++;
264     backlight_long = readword_eeprom(p, &sum);
265     p += 2;
266     ui_idlecount = readword_eeprom(p, &sum);
267     p += 2;
268     backlight_level = readbyte_eeprom(p, &sum);
269     p += 1;
270      // Write checksum
271     magic = (eeprom_readbyte(p) << 8) + eeprom_readbyte(p+1);
272
273     if(sum != magic) return 0x00;
274     return 0xff;
275 }
276
277 /*
278  * Check eeprom, and format/restore.
279  */
280 void check_eeprom(void)
281 {
282     unsigned char c;
283         switch(load_eeprom()) {
284         case 0x01: // No magic-word
285             idle_time_ms(2000);
286             c = printhelp_2lines("EEPROM FORMAT", "Press any key");
287             _CLS();
288             _LOCATE(0,0);
289             printstr("Formatting...  ");
290             format_eeprom(2,250);
291             _LOCATE(0,0);
292             printstr("Save defaults  ");
293             setdefault();
294             save_eeprom();
295             break;
296         case 0x00: // Checksum error
297            idle_time_ms(2000);
298             c = printhelp_2lines("X-) Sum error", "Press any key");
299             c = pollkey_single();
300             _CLS();
301             _LOCATE(0,0);
302             printstr("Formatting...");
303             format_eeprom(2,250);
304 //            writeword_eeprom(0, &sum, 0x1298);
305             _LOCATE(0,0);
306             printstr("Save defaults");
307             setdefault();
308             save_eeprom();
309             break;
310         case 0xff: // Success
311             return;
312             break;
313         default: // Unknown error
314             break;
315     }
316     valinit();
317 }