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.
34 #include <pic18fregs.h> /* ONLY FOR PIC18x */
38 #include "commondef.h"
43 #include "lcd_acm1602.h"
51 void save_eeprom(void)
54 unsigned int sum = 0x0000;
58 writeword_eeprom(p, &sum, 0x1298);
61 writeword_eeprom(p, &sum, amfreq);
64 writeword_eeprom(p, &sum, fmfreq);
67 writebyte_eeprom(p, &sum, amband);
69 writebyte_eeprom(p, &sum, fmband);
71 writebyte_eeprom(p, &sum, fm);
73 writebyte_eeprom(p, &sum, am_mode3k);
75 writebyte_eeprom(p, &sum, am_userbandnum);
77 writebyte_eeprom(p, &sum, fm_userbandnum);
80 for(i = 0 ; i < USER_BAND_NUM; i++){
81 writebyte_eeprom(p, &sum, am_usrbands[i].mode3k);
82 writebyte_eeprom(p + 1, &sum, am_usrbands[i].start);
83 writebyte_eeprom(p + 2, &sum, am_usrbands[i].stop);
84 writeword_eeprom(p + 3, &sum, am_usrbands[i].freq);
87 for(i = 0 ; i < USER_BAND_NUM; i++){
88 writebyte_eeprom(p, &sum, fm_usrbands[i].mode3k);
89 writebyte_eeprom(p + 1, &sum, fm_usrbands[i].start);
90 writebyte_eeprom(p + 2, &sum, fm_usrbands[i].stop);
91 writeword_eeprom(p + 3, &sum, fm_usrbands[i].freq);
94 for(i = 0; i < 19; i++){
95 writeword_eeprom(p , &sum, amfreq_bank[i]);
98 for(i = 0; i < 8; i++){
99 writeword_eeprom(p , &sum, fmfreq_bank[i]);
102 writebyte_eeprom(p, &sum, threshold);
104 writebyte_eeprom(p, &sum, lowboost);
106 writebyte_eeprom(p, &sum, stereo);
109 eeprom_writebyte(p, sum >> 8);
110 eeprom_writebyte(p + 1, sum & 0xff);
113 unsigned char load_eeprom(void)
116 unsigned int sum = 0x0000;
121 magic = readword_eeprom(p, &sum);
122 if(magic != 0x1298) return 0x01; // NO MAGICWORD
125 amfreq = readword_eeprom(p, &sum);
128 fmfreq = readword_eeprom(p, &sum);
131 amband = readbyte_eeprom(p, &sum);
133 fmband = readbyte_eeprom(p, &sum);
135 fm = readbyte_eeprom(p, &sum);
137 am_mode3k = readbyte_eeprom(p, &sum);
139 am_userbandnum = readbyte_eeprom(p, &sum);
141 fm_userbandnum = readbyte_eeprom(p, &sum);
144 for(i = 0 ; i < USER_BAND_NUM; i++){
145 am_usrbands[i].mode3k = readbyte_eeprom(p, &sum);
146 am_usrbands[i].start = readbyte_eeprom(p + 1, &sum);
147 am_usrbands[i].stop = readbyte_eeprom(p + 2, &sum);
148 am_usrbands[i].freq = readword_eeprom(p + 3, &sum);
151 for(i = 0 ; i < USER_BAND_NUM; i++){
152 fm_usrbands[i].mode3k = readbyte_eeprom(p, &sum);
153 fm_usrbands[i].start = readbyte_eeprom(p + 1, &sum);
154 fm_usrbands[i].stop = readbyte_eeprom(p + 2, &sum);
155 fm_usrbands[i].freq = readword_eeprom(p + 3, &sum);
158 for(i = 0; i < 19; i++){
159 amfreq_bank[i] = readword_eeprom(p , &sum);
162 for(i = 0; i < 8; i++){
163 fmfreq_bank[i] = readword_eeprom(p , &sum);
166 threshold = readbyte_eeprom(p , &sum);
168 lowboost = readbyte_eeprom(p , &sum);
170 stereo = readbyte_eeprom(p , &sum);
173 magic = (eeprom_readbyte(p) << 8) + eeprom_readbyte(p+1);
176 if(sum != magic) return 0x00;
181 * Check eeprom, and format/restore.
183 void check_eeprom(void)
186 switch(load_eeprom()) {
187 case 0x01: // No magic-word
189 c = printhelp_2lines("EEPROM FORMAT", "Press any key");
192 printstr("Formatting... ");
193 format_eeprom(2,250);
195 printstr("Save defaults ");
199 case 0x00: // Checksum error
201 c = printhelp_2lines("X-) Sum error", "Press any key");
202 c = pollkey_single();
205 printstr("Formatting...");
206 format_eeprom(2,250);
207 // writeword_eeprom(0, &sum, 0x1298);
209 printstr("Save defaults");
213 case 0xff: // Success
215 default: // Unknown error