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, EEPROM_INTSET_MAGIC);
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 * 0x01FF : User settings(Reserved not use)
101 * 0x0fff : User frequencies ( 12*2*28 = 672chs Max.)
104 int load_frequencies(unsigned int page, unsigned char check_only)
109 unsigned int addr = (page << 7) + EEPROM_FREQSET_ADDR;
115 // if(addr > 0x0fff) return -1; // Address error
116 if(page > USER_MEMORY_BANKS) return -1; // Address Error
118 c = i2c_eeprom_burstread(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__freqset_t));
120 if(c == 0x00) return -2;
121 // Pass 1 check magic
122 if(freqset_temp.magic != EEPROM_FREQSET_MAGIC) 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) + EEPROM_FREQSET_ADDR;
146 __freqset_t *p = &freqset;
150 if(setup.pagenum > USER_MEMORY_BANKS) return; // Address Error
151 // Pass 0 Calc checksum
152 freqset.magic = EEPROM_FREQSET_MAGIC;
153 freqset.version = 0x0001;
154 sum = calcsum_frequencies(&freqset, 0xa5a5);
155 freqset.checksum = sum;
156 // Pass 1 write to rom.
157 c = i2c_eeprom_burstwrite(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__freqset_t));
161 void format_frequencies(unsigned int page)
166 freqset_temp.magic = EEPROM_FREQSET_MAGIC;
167 freqset_temp.version = 0x0001;
168 for(i = 0; i < USER_MEMORY_NUM; i++){
169 freqset_temp.memoryfreqs[i].band = AKC6955_BAND_MW2;
170 freqset_temp.memoryfreqs[i].fm = 0;
171 freqset_temp.memoryfreqs[i].freq = 594;
173 freqset_temp.pagenum = page;
174 sum = calcsum_frequencies(&freqset_temp, 0xa5a5);
175 freqset_temp.checksum = sum;
177 i = i2c_eeprom_burstwrite(I2CEEPROM_ADDR, (page << 7)+ EEPROM_FREQSET_ADDR, (unsigned char *)(&freqset_temp), sizeof(__freqset_t));
180 void check_frequencies(void)
184 for(i = 0; i < USER_MEMORY_BANKS; i++){
185 printstr("Check ExtROM:");
186 print_numeric_nosupress(i, 2);
189 if(load_frequencies(i, 0xff) < 0){
190 // printstr("Format:");
191 // print_numeric_nosupress(i, 2);
193 format_frequencies(i);
196 if(load_userbands() < 0) {
201 static unsigned int calcsum_userband(__userband_t_t *p, unsigned int start)
203 unsigned int sum = start;
204 unsigned char *q = (unsigned char *)p;
208 for(i = 0; i < (sizeof(__userband_t_t) - 2); i++){
216 int load_userbands(void)
219 __userband_t_t *p = &userband;
221 unsigned int addr = EEPROM_BANDSET_ADDR;
224 // if(addr > 0x0fff) return -1; // Address error
225 c = i2c_eeprom_burstread(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__userband_t_t));
227 if(c == 0x00) return -2;
228 // Pass 1 check magic
229 if(p->magic != EEPROM_BANDSET_MAGIC) return -3;
231 sum = calcsum_userband(p, 0x5a5a);
232 if(sum != p->checksum) return -4;
236 void save_userbands(void)
239 unsigned int addr = EEPROM_BANDSET_ADDR;
240 __userband_t_t *p = &userband;
244 // Pass 0 Calc checksum
245 userband.magic = EEPROM_BANDSET_MAGIC;
246 userband.version = 0x0001;
247 sum = calcsum_userband(&userband, 0x5a5a);
248 userband.checksum = sum;
249 // Pass 1 write to rom.
250 c = i2c_eeprom_burstwrite(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__userband_t_t));
254 void format_userbands(void)
257 for(i = 0; i < USER_BAND_NUM; i++) {
258 userband.am_usrbands[i].start = 0x19;
259 userband.am_usrbands[i].stop = 0x32;
260 userband.am_usrbands[i].freq = 5600; //(0x32 - (0x32-0x19) / 2 *32) * 5
261 userband.am_usrbands[i].mode3k = 0;
263 userband.fm_usrbands[i].start = 0x19;
264 userband.fm_usrbands[i].stop = 0x32;
265 userband.fm_usrbands[i].freq = 8600;
266 userband.fm_usrbands[i].mode3k = 0;
268 save_userbands(); // Re-init
272 unsigned char load_eeprom(void)
277 unsigned char *q = (unsigned char *)(&setup);
284 magic = readword_eeprom(p, sum);
285 if(magic != EEPROM_INTSET_MAGIC) return 0x01; // NO MAGICWORD
287 for(i = 0; i < sizeof(__radioset_t); i++ ){
288 *q = readbyte_eeprom(p, sum);
291 magic = (eeprom_readbyte(p[0]) << 8) + eeprom_readbyte(p[0] + 1);
293 if(sum[0] != magic) return 0x00;
298 * Check eeprom, and format/restore.
300 static void check_eeprom_sub(void)
304 printstr("Formatting... ");
305 format_eeprom(2,250);
307 printstr("Save defaults ");
313 void check_eeprom(void)
317 switch(load_eeprom()) {
318 case 0x01: // No magic-word
319 // idle_time_ms(2000);
320 c = printhelp_2lines("EEPROM FORMAT", "Press any key");
323 case 0x00: // Checksum error
324 // idle_time_ms(2000);
325 c = printhelp_2lines("X-) Sum error", "Press any key");
328 case 0xff: // Success
330 load_frequencies(setup.pagenum, 0);
333 default: // Unknown error
338 load_frequencies(setup.pagenum, 0);