4 * Copyright (C) 2013-06-10 K.Ohta <whatisthis.sowhat ai gmail.com>
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.
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,
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.
35 #include <pic18fregs.h> /* ONLY FOR PIC18x */
42 #include "commondef.h"
47 #include "lcd_acm1602.h"
55 void save_eeprom(void)
58 unsigned int sum = 0x0000;
64 writeword_eeprom(p, &sum, 0x1298);
67 writeword_eeprom(p, &sum, amfreq);
70 writeword_eeprom(p, &sum, fmfreq);
73 writebyte_eeprom(p, &sum, amband);
75 writebyte_eeprom(p, &sum, fmband);
77 writebyte_eeprom(p, &sum, fm);
79 writebyte_eeprom(p, &sum, am_mode3k);
81 writebyte_eeprom(p, &sum, am_userbandnum);
83 writebyte_eeprom(p, &sum, fm_userbandnum);
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);
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);
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);
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);
119 amfreq_bank[amband] = amfreq;
120 fmfreq_bank[fmband] = fmfreq;
122 for(i = 0; i < 19; i++){
123 writeword_eeprom(p , &sum, amfreq_bank[i]);
126 for(i = 0; i < 8; i++){
127 writeword_eeprom(p , &sum, fmfreq_bank[i]);
131 writebyte_eeprom(p, &sum, threshold_amcnr);
133 writebyte_eeprom(p, &sum, threshold_fmcnr);
135 writebyte_eeprom(p, &sum, threshold_width);
137 writebyte_eeprom(p, &sum, threshold_fmstereo);
139 writebyte_eeprom(p, &sum, lowboost);
141 writebyte_eeprom(p, &sum, stereo);
144 writebyte_eeprom(p, &sum, volume);
146 writebyte_eeprom(p, &sum, prevolume);
148 writebyte_eeprom(p, &sum, fmbandwidth);
150 writeword_eeprom(p, &sum, backlight_long);
152 writeword_eeprom(p, &sum, ui_idlecount);
156 eeprom_writebyte(p, sum >> 8);
157 eeprom_writebyte(p + 1, sum & 0xff);
160 unsigned char load_eeprom(void)
163 unsigned int sum = 0x0000;
170 magic = readword_eeprom(p, &sum);
171 if(magic != 0x1298) return 0x01; // NO MAGICWORD
174 amfreq = readword_eeprom(p, &sum);
177 fmfreq = readword_eeprom(p, &sum);
180 amband = readbyte_eeprom(p, &sum);
182 fmband = readbyte_eeprom(p, &sum);
184 fm = readbyte_eeprom(p, &sum);
186 am_mode3k = readbyte_eeprom(p, &sum);
188 am_userbandnum = readbyte_eeprom(p, &sum);
190 fm_userbandnum = readbyte_eeprom(p, &sum);
193 for(i = 0 ; i < USER_BAND_NUM; i++){
194 am_usrbands[i].mode3k = readbyte_eeprom(p, &sum);
195 am_usrbands[i].start = readbyte_eeprom(p + 1, &sum);
196 am_usrbands[i].stop = readbyte_eeprom(p + 2, &sum);
197 am_usrbands[i].freq = readword_eeprom(p + 3, &sum);
200 for(i = 0 ; i < USER_BAND_NUM; i++){
201 fm_usrbands[i].mode3k = readbyte_eeprom(p, &sum);
202 fm_usrbands[i].start = readbyte_eeprom(p + 1, &sum);
203 fm_usrbands[i].stop = readbyte_eeprom(p + 2, &sum);
204 fm_usrbands[i].freq = readword_eeprom(p + 3, &sum);
208 for(j = 0; j < USER_BAND_NUM; j++){
209 q = (unsigned char *)&am_usrbands[j];
210 for(i = 0; i < sizeof(_userband_t); i++){
211 *q = readbyte_eeprom(p, &sum);
215 if(am_usrbands[j].mode3k != 0){
216 am_userband_freq[j].start = am_usrbands[j].start * 96;
217 am_userband_freq[j].end = am_usrbands[j].stop * 96;
219 am_userband_freq[j].start = am_usrbands[j].start * 160;
220 am_userband_freq[j].end = am_usrbands[j].stop * 160;
223 for(j = 0; j < USER_BAND_NUM; j++){
224 q = (unsigned char *)&fm_usrbands[j];
225 for(i = 0; i < sizeof(_userband_t); i++){
226 *q = readbyte_eeprom(p, &sum);
230 fm_userband_freq[j].start = fm_usrbands[j].start * 80 + 3000;
231 fm_userband_freq[j].end = fm_usrbands[j].stop * 80 + 3000;
234 amfreq = amfreq_bank[amband];
235 fmfreq = fmfreq_bank[fmband];
236 for(i = 0; i < 19; i++){
237 amfreq_bank[i] = readword_eeprom(p , &sum);
240 for(i = 0; i < 8; i++){
241 fmfreq_bank[i] = readword_eeprom(p , &sum);
244 threshold_amcnr = readbyte_eeprom(p, &sum);
246 threshold_fmcnr = readbyte_eeprom(p, &sum);
248 threshold_width = readbyte_eeprom(p, &sum);
250 threshold_fmstereo = readbyte_eeprom(p, &sum);
252 lowboost = readbyte_eeprom(p , &sum);
254 stereo = readbyte_eeprom(p , &sum);
256 volume = readbyte_eeprom(p, &sum);
258 prevolume = readbyte_eeprom(p, &sum);
260 fmbandwidth = readbyte_eeprom(p, &sum);
262 backlight_long = readword_eeprom(p, &sum);
264 ui_idlecount = readword_eeprom(p, &sum);
267 magic = (eeprom_readbyte(p) << 8) + eeprom_readbyte(p+1);
269 if(sum != magic) return 0x00;
274 * Check eeprom, and format/restore.
276 void check_eeprom(void)
279 switch(load_eeprom()) {
280 case 0x01: // No magic-word
282 c = printhelp_2lines("EEPROM FORMAT", "Press any key");
285 printstr("Formatting... ");
286 format_eeprom(2,250);
288 printstr("Save defaults ");
292 case 0x00: // Checksum error
294 c = printhelp_2lines("X-) Sum error", "Press any key");
295 c = pollkey_single();
298 printstr("Formatting...");
299 format_eeprom(2,250);
300 // writeword_eeprom(0, &sum, 0x1298);
302 printstr("Save defaults");
306 case 0xff: // Success
309 default: // Unknown error