OSDN Git Service

[IDLE] Maybe correct time.
[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[1];
58     unsigned int sum = 0x0000;
59     unsigned char i;
60     unsigned char j;
61     unsigned char *q;
62
63     *p = 0;
64     // Magic word
65     writeword_eeprom(p, &sum, 0x1298);
66     // amfreq
67     writeword_eeprom(p, &sum, amfreq);
68     // amfreq
69     writeword_eeprom(p, &sum, fmfreq);
70
71     writebyte_eeprom(p, &sum, amband);
72     writebyte_eeprom(p, &sum, fmband);
73     writebyte_eeprom(p, &sum, fm);
74     writebyte_eeprom(p, &sum, am_mode3k);
75     writebyte_eeprom(p, &sum, am_userbandnum);
76     writebyte_eeprom(p, &sum, fm_userbandnum);
77
78     for(j = 0; j < USER_BAND_NUM; j++){
79         q = (unsigned char *)&am_usrbands[j];
80         for(i = 0; i < sizeof(_userband_t); i++){
81             writebyte_eeprom(p, &sum, *q);
82             q++;
83         }
84     }
85     for(j = 0; j < USER_BAND_NUM; j++){
86         q = (unsigned char *)&fm_usrbands[j];
87         for(i = 0; i < sizeof(_userband_t); i++){
88             writebyte_eeprom(p, &sum, *q);
89             q++;
90         }
91     }
92     amfreq_bank[amband] = amfreq;
93     fmfreq_bank[fmband] = fmfreq;
94
95     for(i = 0; i < 19; i++){
96         writeword_eeprom(p    , &sum, amfreq_bank[i]);
97     }
98     for(i = 0; i < 8; i++){
99         writeword_eeprom(p    , &sum, fmfreq_bank[i]);
100     }
101
102     writebyte_eeprom(p, &sum, threshold_amcnr);
103     writebyte_eeprom(p, &sum, threshold_fmcnr);
104     writebyte_eeprom(p, &sum, threshold_width);
105     writebyte_eeprom(p, &sum, threshold_fmstereo);
106     writebyte_eeprom(p, &sum, lowboost);
107     writebyte_eeprom(p, &sum, stereo);
108
109     writebyte_eeprom(p, &sum, volume);
110     writebyte_eeprom(p, &sum, prevolume);
111     writebyte_eeprom(p, &sum, fmbandwidth);
112     writeword_eeprom(p, &sum, backlight_long);
113     writeword_eeprom(p, &sum, ui_idlecount);
114     writebyte_eeprom(p, &sum, backlight_level);
115
116     // Write checksum
117     eeprom_writebyte(*p, sum >> 8);
118     eeprom_writebyte(*p + 1, sum & 0xff);
119 }
120
121 unsigned char load_eeprom(void)
122 {
123     unsigned int p[1];
124     unsigned int sum[1];
125     unsigned char i;
126     unsigned char *q;
127     unsigned char j;
128     unsigned int magic;
129
130     p[0] = 0;
131     sum[0] = 0;
132     // Magic word
133     magic = readword_eeprom(p, sum);
134     if(magic != 0x1298) return 0x01; // NO MAGICWORD
135     // amfreq
136     amfreq = readword_eeprom(p, sum);
137     // fmfreq
138     fmfreq = readword_eeprom(p, sum);
139     amband = readbyte_eeprom(p, sum);
140     fmband = readbyte_eeprom(p, sum);
141     fm = readbyte_eeprom(p, sum);
142     am_mode3k = readbyte_eeprom(p, sum);
143     am_userbandnum = readbyte_eeprom(p, sum);
144     fm_userbandnum = readbyte_eeprom(p, sum);
145     for(j = 0; j < USER_BAND_NUM; j++){
146         q = (unsigned char *)&am_usrbands[j];
147         for(i = 0; i < sizeof(_userband_t); i++){
148             *q = readbyte_eeprom(p, sum);
149             q++;
150         }
151         if(am_usrbands[j].mode3k != 0){
152             am_userband_freq[j].start = am_usrbands[j].start * 96;
153             am_userband_freq[j].end   = am_usrbands[j].stop  * 96;
154         } else {
155             am_userband_freq[j].start = am_usrbands[j].start * 160;
156             am_userband_freq[j].end   = am_usrbands[j].stop  * 160;
157         }
158     }
159     for(j = 0; j < USER_BAND_NUM; j++){
160         q = (unsigned char *)&fm_usrbands[j];
161         for(i = 0; i < sizeof(_userband_t); i++){
162             *q = readbyte_eeprom(p, sum);
163             q++;
164         }
165         fm_userband_freq[j].start = fm_usrbands[j].start * 80 + 3000;
166         fm_userband_freq[j].end   = fm_usrbands[j].stop * 80 + 3000;
167     }
168     amfreq = amfreq_bank[amband];
169     fmfreq = fmfreq_bank[fmband];
170     for(i = 0; i < 19; i++){
171         amfreq_bank[i] = readword_eeprom(p    , sum);
172     }
173     for(i = 0; i < 8; i++){
174         fmfreq_bank[i] = readword_eeprom(p    , sum);
175     }
176     threshold_amcnr = readbyte_eeprom(p, sum);
177     threshold_fmcnr = readbyte_eeprom(p, sum);
178     threshold_width = readbyte_eeprom(p, sum);
179     threshold_fmstereo = readbyte_eeprom(p, sum);
180     lowboost = readbyte_eeprom(p    , sum);
181     stereo = readbyte_eeprom(p    , sum);
182     volume = readbyte_eeprom(p, sum);
183     prevolume = readbyte_eeprom(p, sum);
184     fmbandwidth = readbyte_eeprom(p, sum);
185     backlight_long = readword_eeprom(p, sum);
186     ui_idlecount = readword_eeprom(p, sum);
187     backlight_level = readbyte_eeprom(p, sum);
188      // Write checksum
189     magic = (eeprom_readbyte(p[0]) << 8) + eeprom_readbyte(p[0] + 1);
190
191     if(sum[0] != magic) return 0x00;
192     return 0xff;
193 }
194
195 /*
196  * Check eeprom, and format/restore.
197  */
198 void check_eeprom(void)
199 {
200     unsigned char c;
201         switch(load_eeprom()) {
202         case 0x01: // No magic-word
203             idle_time_ms(2000);
204             c = printhelp_2lines("EEPROM FORMAT", "Press any key");
205             _CLS();
206             _LOCATE(0,0);
207             printstr("Formatting...  ");
208             format_eeprom(2,250);
209             _LOCATE(0,0);
210             printstr("Save defaults  ");
211             setdefault();
212             save_eeprom();
213             break;
214         case 0x00: // Checksum error
215            idle_time_ms(2000);
216             c = printhelp_2lines("X-) Sum error", "Press any key");
217             c = pollkey_single();
218             _CLS();
219             _LOCATE(0,0);
220             printstr("Formatting...");
221             format_eeprom(2,250);
222 //            writeword_eeprom(0, &sum, 0x1298);
223             _LOCATE(0,0);
224             printstr("Save defaults");
225             setdefault();
226             save_eeprom();
227             break;
228         case 0xff: // Success
229             return;
230             break;
231         default: // Unknown error
232             break;
233     }
234     valinit();
235 }