#include "iodef.h"
#include "idle.h"
#include "i2c_io.h"
+#include "i2c_eeprom.h"
#include "akc6955.h"
#include "lcd_acm1602.h"
#include "ui.h"
void save_eeprom(void)
{
unsigned int p[1];
- unsigned int sum = 0x0000;
- unsigned char i;
+ unsigned int sum[1];
+ unsigned char i;
unsigned char j;
- unsigned char *q;
+ unsigned char *q = (char *)(&setup);
*p = 0;
+ *sum = 0;
// Magic word
- writeword_eeprom(p, &sum, 0x1298);
+ writeword_eeprom(p, sum, EEPROM_INTSET_MAGIC);
// amfreq
- writeword_eeprom(p, &sum, amfreq);
- // amfreq
- writeword_eeprom(p, &sum, fmfreq);
-
- writebyte_eeprom(p, &sum, amband);
- writebyte_eeprom(p, &sum, fmband);
- writebyte_eeprom(p, &sum, fm);
- writebyte_eeprom(p, &sum, am_mode3k);
- writebyte_eeprom(p, &sum, am_userbandnum);
- writebyte_eeprom(p, &sum, fm_userbandnum);
-
- for(j = 0; j < USER_BAND_NUM; j++){
- q = (unsigned char *)&am_usrbands[j];
- for(i = 0; i < sizeof(_userband_t); i++){
- writebyte_eeprom(p, &sum, *q);
- q++;
- }
+ for(i = 0; i < sizeof(__radioset_t); i++) {
+ writebyte_eeprom(p, sum, *q);
+ q++;
}
- for(j = 0; j < USER_BAND_NUM; j++){
- q = (unsigned char *)&fm_usrbands[j];
- for(i = 0; i < sizeof(_userband_t); i++){
- writebyte_eeprom(p, &sum, *q);
+
+ // Write checksum
+ eeprom_writebyte(*p, sum[0] >> 8);
+ eeprom_writebyte(*p + 1, sum[0] & 0xff);
+}
+
+
+unsigned int calcsum_frequencies(__freqset_t *p, unsigned int start)
+{
+ unsigned int sum = start;
+ unsigned char *q = (unsigned char *)p;
+ unsigned char c;
+ unsigned char i;
+
+ for(i = 0; i < (sizeof(__freqset_t) - 2); i++){
+ c = *q;
+ q++;
+ sum = sum + c;
+ }
+ return sum;
+}
+/*
+ * External I2C Map:
+ * 0x0000
+ * 0x01FF : User settings(Reserved not use)
+ * 0x0100
+ * 0x01ff : User bands
+ * 0x0200
+ * 0x0fff : User frequencies ( 12*2*28 = 672chs Max.)
+ * 0x1000- : Reserve.
+ */
+int load_frequencies(unsigned int page, unsigned char check_only)
+{
+
+ __freqset_t *p;
+ unsigned int sum;
+ unsigned int addr = (page << 7) + EEPROM_FREQSET_ADDR;
+ unsigned char *q;
+ unsigned char *r;
+ unsigned char c;
+ unsigned int i;
+
+// if(addr > 0x0fff) return -1; // Address error
+ if(page > USER_MEMORY_BANKS) return -1; // Address Error
+ p = &freqset_temp;
+ c = i2c_eeprom_burstread(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__freqset_t));
+ // Pass 1 Read OK?
+ if(c == 0x00) return -2;
+ // Pass 1 check magic
+ if(freqset_temp.magic != EEPROM_FREQSET_MAGIC) return -3;
+ // Pass 2 check sum
+ sum = calcsum_frequencies(&freqset_temp, 0xa5a5);
+ if(sum != freqset_temp.checksum) return -4;
+ // Move from
+ if(check_only == 0x00){
+ q = (unsigned char *)(&freqset_temp);
+ r = (unsigned char *)(&freqset);
+ for(i = 0; i < sizeof(__freqset_t); i++){
+ *r = *q;
q++;
+ r++;
}
+ setup.pagenum = page;
}
- amfreq_bank[amband] = amfreq;
- fmfreq_bank[fmband] = fmfreq;
+ return 0;
+}
+
+
- for(i = 0; i < 19; i++){
- writeword_eeprom(p , &sum, amfreq_bank[i]);
+void save_frequencies(void)
+{
+ unsigned char c;
+ unsigned int addr = (setup.pagenum <<7) + EEPROM_FREQSET_ADDR;
+ __freqset_t *p = &freqset;
+ unsigned char i;
+ unsigned int sum;
+
+ if(setup.pagenum > USER_MEMORY_BANKS) return; // Address Error
+ // Pass 0 Calc checksum
+ freqset.magic = EEPROM_FREQSET_MAGIC;
+ freqset.version = 0x0001;
+ sum = calcsum_frequencies(&freqset, 0xa5a5);
+ freqset.checksum = sum;
+ // Pass 1 write to rom.
+ c = i2c_eeprom_burstwrite(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__freqset_t));
+ return;
+}
+
+char save_frequencies_page(unsigned char page)
+{
+ unsigned char c;
+ unsigned int addr = (page <<7) + EEPROM_FREQSET_ADDR;
+ __freqset_t *p = &freqset_temp;
+ unsigned char i;
+ unsigned int sum;
+
+ if(page > USER_MEMORY_BANKS) return -3; // Address Error
+ // Pass 0 Calc checksum
+ if(p->magic != EEPROM_FREQSET_MAGIC) return -1; // Data error
+ sum = calcsum_frequencies(p, 0xa5a5);
+ if(p->checksum != sum) return -2; // Sum error
+ // Pass 1 write to rom.
+ c = i2c_eeprom_burstwrite(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__freqset_t));
+ return 0;
+}
+
+
+void format_frequencies(unsigned int page)
+{
+ unsigned char i;
+ unsigned int sum;
+
+ freqset_temp.magic = EEPROM_FREQSET_MAGIC;
+ freqset_temp.version = 0x0001;
+ for(i = 0; i < USER_MEMORY_NUM; i++){
+ freqset_temp.memoryfreqs[i].band = AKC6955_BAND_MW2;
+ freqset_temp.memoryfreqs[i].fm = 0;
+ freqset_temp.memoryfreqs[i].freq = 594;
}
- for(i = 0; i < 8; i++){
- writeword_eeprom(p , &sum, fmfreq_bank[i]);
+ freqset_temp.pagenum = page;
+ sum = calcsum_frequencies(&freqset_temp, 0xa5a5);
+ freqset_temp.checksum = sum;
+
+ i = i2c_eeprom_burstwrite(I2CEEPROM_ADDR, (page << 7)+ EEPROM_FREQSET_ADDR, (unsigned char *)(&freqset_temp), sizeof(__freqset_t));
+}
+
+void check_frequencies(void)
+{
+ unsigned int i;
+ _CLS();
+ for(i = 0; i < USER_MEMORY_BANKS; i++){
+ printstr("Check ExtROM:");
+ print_numeric_nosupress(i, 2);
+ _HOME();
+
+ if(load_frequencies(i, 0xff) < 0){
+// printstr("Format:");
+// print_numeric_nosupress(i, 2);
+ _HOME();
+ format_frequencies(i);
+ }
}
+ if(load_userbands() < 0) {
+ format_userbands();
+ }
+}
- writebyte_eeprom(p, &sum, threshold_amcnr);
- writebyte_eeprom(p, &sum, threshold_fmcnr);
- writebyte_eeprom(p, &sum, threshold_width);
- writebyte_eeprom(p, &sum, threshold_fmstereo);
- writebyte_eeprom(p, &sum, lowboost);
- writebyte_eeprom(p, &sum, stereo);
+static unsigned int calcsum_userband(__userband_t_t *p, unsigned int start)
+{
+ unsigned int sum = start;
+ unsigned char *q = (unsigned char *)p;
+ unsigned char c;
+ unsigned char i;
- writebyte_eeprom(p, &sum, volume);
- writebyte_eeprom(p, &sum, prevolume);
- writebyte_eeprom(p, &sum, fmbandwidth);
- writeword_eeprom(p, &sum, backlight_long);
- writeword_eeprom(p, &sum, ui_idlecount);
- writebyte_eeprom(p, &sum, backlight_level);
+ for(i = 0; i < (sizeof(__userband_t_t) - 2); i++){
+ c = *q;
+ q++;
+ sum = sum + c;
+ }
+ return sum;
+}
- // Write checksum
- eeprom_writebyte(*p, sum >> 8);
- eeprom_writebyte(*p + 1, sum & 0xff);
+int load_userbands(void)
+{
+
+ __userband_t_t *p = &userband;
+ unsigned int sum;
+ unsigned int addr = EEPROM_BANDSET_ADDR;
+ unsigned char c;
+
+// if(addr > 0x0fff) return -1; // Address error
+ c = i2c_eeprom_burstread(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__userband_t_t));
+ // Pass 1 Read OK?
+ if(c == 0x00) return -2;
+ // Pass 1 check magic
+ if(p->magic != EEPROM_BANDSET_MAGIC) return -3;
+ // Pass 2 check sum
+ sum = calcsum_userband(p, 0x5a5a);
+ if(sum != p->checksum) return -4;
+ return 0;
}
+void save_userbands(void)
+{
+ unsigned char c;
+ unsigned int addr = EEPROM_BANDSET_ADDR;
+ __userband_t_t *p = &userband;
+ unsigned char i;
+ unsigned int sum;
+
+ // Pass 0 Calc checksum
+ userband.magic = EEPROM_BANDSET_MAGIC;
+ userband.version = 0x0001;
+ sum = calcsum_userband(&userband, 0x5a5a);
+ userband.checksum = sum;
+ // Pass 1 write to rom.
+ c = i2c_eeprom_burstwrite(I2CEEPROM_ADDR, addr, (unsigned char *)p, sizeof(__userband_t_t));
+ return;
+}
+
+void format_userbands(void)
+{
+ unsigned char i;
+ for(i = 0; i < USER_BAND_NUM; i++) {
+ userband.am_usrbands[i].start = 0x19;
+ userband.am_usrbands[i].stop = 0x32;
+ userband.am_usrbands[i].freq = 5600; //(0x32 - (0x32-0x19) / 2 *32) * 5
+ userband.am_usrbands[i].mode3k = 0;
+ //
+ userband.fm_usrbands[i].start = 0x19;
+ userband.fm_usrbands[i].stop = 0x32;
+ userband.fm_usrbands[i].freq = 8600;
+ userband.fm_usrbands[i].mode3k = 0;
+ }
+ save_userbands(); // Re-init
+}
+
+
unsigned char load_eeprom(void)
{
unsigned int p[1];
unsigned int sum[1];
unsigned char i;
- unsigned char *q;
+ unsigned char *q = (unsigned char *)(&setup);
unsigned char j;
unsigned int magic;
sum[0] = 0;
// Magic word
magic = readword_eeprom(p, sum);
- if(magic != 0x1298) return 0x01; // NO MAGICWORD
+ if(magic != EEPROM_INTSET_MAGIC) return 0x01; // NO MAGICWORD
// amfreq
- amfreq = readword_eeprom(p, sum);
- // fmfreq
- fmfreq = readword_eeprom(p, sum);
- amband = readbyte_eeprom(p, sum);
- fmband = readbyte_eeprom(p, sum);
- fm = readbyte_eeprom(p, sum);
- am_mode3k = readbyte_eeprom(p, sum);
- am_userbandnum = readbyte_eeprom(p, sum);
- fm_userbandnum = readbyte_eeprom(p, sum);
- for(j = 0; j < USER_BAND_NUM; j++){
- q = (unsigned char *)&am_usrbands[j];
- for(i = 0; i < sizeof(_userband_t); i++){
- *q = readbyte_eeprom(p, sum);
- q++;
- }
- if(am_usrbands[j].mode3k != 0){
- am_userband_freq[j].start = am_usrbands[j].start * 96;
- am_userband_freq[j].end = am_usrbands[j].stop * 96;
- } else {
- am_userband_freq[j].start = am_usrbands[j].start * 160;
- am_userband_freq[j].end = am_usrbands[j].stop * 160;
- }
+ for(i = 0; i < sizeof(__radioset_t); i++ ){
+ *q = readbyte_eeprom(p, sum);
+ q++;
}
- for(j = 0; j < USER_BAND_NUM; j++){
- q = (unsigned char *)&fm_usrbands[j];
- for(i = 0; i < sizeof(_userband_t); i++){
- *q = readbyte_eeprom(p, sum);
- q++;
- }
- fm_userband_freq[j].start = fm_usrbands[j].start * 80 + 3000;
- fm_userband_freq[j].end = fm_usrbands[j].stop * 80 + 3000;
- }
- amfreq = amfreq_bank[amband];
- fmfreq = fmfreq_bank[fmband];
- for(i = 0; i < 19; i++){
- amfreq_bank[i] = readword_eeprom(p , sum);
- }
- for(i = 0; i < 8; i++){
- fmfreq_bank[i] = readword_eeprom(p , sum);
- }
- threshold_amcnr = readbyte_eeprom(p, sum);
- threshold_fmcnr = readbyte_eeprom(p, sum);
- threshold_width = readbyte_eeprom(p, sum);
- threshold_fmstereo = readbyte_eeprom(p, sum);
- lowboost = readbyte_eeprom(p , sum);
- stereo = readbyte_eeprom(p , sum);
- volume = readbyte_eeprom(p, sum);
- prevolume = readbyte_eeprom(p, sum);
- fmbandwidth = readbyte_eeprom(p, sum);
- backlight_long = readword_eeprom(p, sum);
- ui_idlecount = readword_eeprom(p, sum);
- backlight_level = readbyte_eeprom(p, sum);
- // Write checksum
magic = (eeprom_readbyte(p[0]) << 8) + eeprom_readbyte(p[0] + 1);
if(sum[0] != magic) return 0x00;
/*
* Check eeprom, and format/restore.
*/
+static void check_eeprom_sub(void)
+{
+ _CLS();
+ _LOCATE_0_0();
+ printstr("Formatting... ");
+ format_eeprom(2,250);
+ _LOCATE_0_0();
+ printstr("Save defaults ");
+ setdefault();
+ save_eeprom();
+}
+
+
void check_eeprom(void)
{
unsigned char c;
- switch(load_eeprom()) {
+
+ switch(load_eeprom()) {
case 0x01: // No magic-word
- idle_time_ms(2000);
+// idle_time_ms(2000);
c = printhelp_2lines("EEPROM FORMAT", "Press any key");
- _CLS();
- _LOCATE(0,0);
- printstr("Formatting... ");
- format_eeprom(2,250);
- _LOCATE(0,0);
- printstr("Save defaults ");
- setdefault();
- save_eeprom();
+ check_eeprom_sub();
break;
case 0x00: // Checksum error
- idle_time_ms(2000);
+// idle_time_ms(2000);
c = printhelp_2lines("X-) Sum error", "Press any key");
- c = pollkey_single();
- _CLS();
- _LOCATE(0,0);
- printstr("Formatting...");
- format_eeprom(2,250);
-// writeword_eeprom(0, &sum, 0x1298);
- _LOCATE(0,0);
- printstr("Save defaults");
- setdefault();
- save_eeprom();
+ check_eeprom_sub();
break;
case 0xff: // Success
+ check_frequencies();
+ load_frequencies(setup.pagenum, 0);
return;
break;
default: // Unknown error
break;
}
valinit();
+ check_frequencies();
+ load_frequencies(setup.pagenum, 0);
}