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"
46 #include "i2c_eeprom.h"
48 #include "lcd_acm1602.h"
56 void save_eeprom(void)
62 unsigned char *q = (char *)(&setup);
67 writeword_eeprom(p, sum, 0x1298);
69 for(i = 0; i < sizeof(__radioset_t); i++) {
70 writebyte_eeprom(p, sum, *q);
75 eeprom_writebyte(*p, sum[0] >> 8);
76 eeprom_writebyte(*p + 1, sum[0] & 0xff);
80 unsigned int calcsum_frequencies(__freqset_t *p, unsigned int start)
82 unsigned int sum = start;
83 unsigned char *q = (unsigned char *)p;
87 for(i = 0; i < (sizeof(__freqset_t) - 2); i++){
97 * 0x00FF : User settings(Reserved not use)
101 * 0x0fff : User frequencies ( 12*2*28 = 672chs Max.)
104 char load_frequencies(unsigned int page, unsigned char check_only)
109 unsigned int addr = (page << 7) + 512;
115 if(addr > 0x0fff) return -1; // Address error
118 c = i2c_eeprom_burstread(0xa0, addr, (unsigned char *)p, sizeof(__freqset_t));
120 if(c == 0x00) return -2;
121 // Pass 1 check magic
122 if(freqset_temp.magic != 0xfabc) return -3;
124 sum = calcsum_frequencies(&freqset_temp, 0xa5a5);
125 if(sum != freqset_temp.checksum) return -4;
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++){
135 setup.pagenum = page;
142 void save_frequencies(void)
145 unsigned int addr = (setup.pagenum <<7) + 512;
146 __freqset_t *p = &freqset;
149 unsigned char *q, *r;
152 if(addr > 0x0fff) return;
153 // Pass 0 Calc checksum
154 freqset.magic = 0xfabc;
155 freqset.version = 0x0001;
156 sum = calcsum_frequencies(&freqset, 0xa5a5);
157 freqset.checksum = sum;
158 // Pass 1 check storaged page
159 if(load_frequencies(setup.pagenum, 0xff) < 0) {
160 // Sum ERROR = Force rewrite
161 c = i2c_eeprom_burstwrite(0xa0, addr, (unsigned char *)p, sizeof(__freqset_t));
167 q = (unsigned char *)(&freqset_temp);
168 r = (unsigned char *)(&freqset);
169 for(i = 0; i < sizeof(__freqset_t); i++) {
170 if(*q != *r) flg |= 0xff;
174 if(flg != 0x00){ // Changed
175 c = i2c_eeprom_burstwrite(0xa0, addr, (unsigned char *)p, sizeof(__freqset_t));
182 void format_frequencies(unsigned int page)
187 freqset_temp.magic = 0xfabc;
188 freqset_temp.version = 0x0001;
189 for(i = 0; i < USER_MEMORY_NUM; i++){
190 freqset_temp.memoryfreqs[i].band = AKC6955_BAND_MW2;
191 freqset_temp.memoryfreqs[i].fm = 0;
192 freqset_temp.memoryfreqs[i].freq = 594;
194 freqset_temp.pagenum = page;
195 sum = calcsum_frequencies(&freqset_temp, 0xa5a5);
196 freqset_temp.checksum = sum;
198 i = i2c_eeprom_burstwrite(0xa0, (page << 7)+ 512, (unsigned char *)(&freqset_temp), sizeof(__freqset_t));
201 void check_frequencies(void)
205 for(i = 0; i < 24; i++){
206 printstr("Check ExtROM:");
207 print_numeric_nosupress(i, 2);
210 if(load_frequencies(i, 0xff) < 0){
212 print_numeric_nosupress(i, 2);
214 format_frequencies(i);
219 unsigned char load_eeprom(void)
224 unsigned char *q = (unsigned char *)(&setup);
231 magic = readword_eeprom(p, sum);
232 if(magic != 0x1298) return 0x01; // NO MAGICWORD
234 for(i = 0; i < sizeof(__radioset_t); i++ ){
235 *q = readbyte_eeprom(p, sum);
238 magic = (eeprom_readbyte(p[0]) << 8) + eeprom_readbyte(p[0] + 1);
240 if(sum[0] != magic) return 0x00;
245 * Check eeprom, and format/restore.
247 void check_eeprom(void)
252 switch(load_eeprom()) {
253 case 0x01: // No magic-word
255 c = printhelp_2lines("EEPROM FORMAT", "Press any key");
258 printstr("Formatting... ");
259 format_eeprom(2,250);
261 printstr("Save defaults ");
264 // format_frequencies();
266 case 0x00: // Checksum error
268 c = printhelp_2lines("X-) Sum error", "Press any key");
269 c = pollkey_single();
272 printstr("Formatting...");
273 format_eeprom(2,250);
274 // writeword_eeprom(0, &sum, 0x1298);
276 printstr("Save defaults");
279 // format_frequencies();
281 case 0xff: // Success
283 load_frequencies(setup.pagenum, 0);
286 default: // Unknown error
291 load_frequencies(setup.pagenum, 0);