OSDN Git Service

[UI] Split ui-depend routines from main.c.
[openi2cradio/OpenI2CRadio.git] / eepromutil.c
1 /*
2  * OpenI2CRADIO
3  * EEPROM utils
4  * Copyright (C) 2013-06-10 K.Ohta <whatisthis.sowhat ai gmail.com>
5  * License: GPL2+LE
6  *
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.
15  *
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,
19  *  MA 02110-1301, USA.
20  *
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.
27  */
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sdcc-lib.h>
34 #include <pic18fregs.h> /* ONLY FOR PIC18x */
35 #include <signal.h>
36 #include <delay.h>
37
38 #include "commondef.h"
39 #include "iodef.h"
40 #include "idle.h"
41 #include "i2c_io.h"
42 #include "akc6955.h"
43 #include "lcd_acm1602.h"
44 #include "ui.h"
45 #include "eeprom.h"
46 #include "ioports.h"
47 #include "menu.h"
48 #include "power.h"
49 #include "adc_int.h"
50
51 void save_eeprom(void)
52 {
53     unsigned int p = 0;
54     unsigned int sum = 0x0000;
55     unsigned char i;
56
57     // Magic word
58     writeword_eeprom(p, &sum, 0x1298);
59     p+= 2;
60     // amfreq
61     writeword_eeprom(p, &sum, amfreq);
62     p+= 2;
63     // amfreq
64     writeword_eeprom(p, &sum, fmfreq);
65     p+= 2;
66
67     writebyte_eeprom(p, &sum, amband);
68     p++;
69     writebyte_eeprom(p, &sum, fmband);
70     p++;
71     writebyte_eeprom(p, &sum, fm);
72     p++;
73     writebyte_eeprom(p, &sum, am_mode3k);
74     p++;
75     writebyte_eeprom(p, &sum, am_userbandnum);
76     p++;
77     writebyte_eeprom(p, &sum, fm_userbandnum);
78     p++;
79
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);
85         p += 5;
86     }
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);
92         p += 5;
93     }
94     for(i = 0; i < 19; i++){
95         writeword_eeprom(p    , &sum, amfreq_bank[i]);
96         p += 2;
97     }
98     for(i = 0; i < 8; i++){
99         writeword_eeprom(p    , &sum, fmfreq_bank[i]);
100         p += 2;
101     }
102     writebyte_eeprom(p, &sum, threshold);
103     p++;
104     writebyte_eeprom(p, &sum, lowboost);
105     p++;
106     writebyte_eeprom(p, &sum, stereo);
107     p++;
108     // Write checksum
109     eeprom_writebyte(p, sum >> 8);
110     eeprom_writebyte(p + 1, sum & 0xff);
111 }
112
113 unsigned char load_eeprom(void)
114 {
115     unsigned int p = 0;
116     unsigned int sum = 0x0000;
117     unsigned char i;
118     unsigned int magic;
119
120     // Magic word
121     magic = readword_eeprom(p, &sum);
122     if(magic != 0x1298) return 0x01; // NO MAGICWORD
123     p+= 2;
124     // amfreq
125     amfreq = readword_eeprom(p, &sum);
126     p+= 2;
127     // fmfreq
128     fmfreq = readword_eeprom(p, &sum);
129     p+= 2;
130
131     amband = readbyte_eeprom(p, &sum);
132     p++;
133     fmband = readbyte_eeprom(p, &sum);
134     p++;
135     fm = readbyte_eeprom(p, &sum);
136     p++;
137     am_mode3k = readbyte_eeprom(p, &sum);
138     p++;
139     am_userbandnum = readbyte_eeprom(p, &sum);
140     p++;
141     fm_userbandnum = readbyte_eeprom(p, &sum);
142     p++;
143
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);
149         p += 5;
150     }
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);
156         p += 5;
157     }
158     for(i = 0; i < 19; i++){
159         amfreq_bank[i] = readword_eeprom(p    , &sum);
160         p += 2;
161     }
162     for(i = 0; i < 8; i++){
163         fmfreq_bank[i] = readword_eeprom(p    , &sum);
164         p += 2;
165     }
166     threshold = readbyte_eeprom(p    , &sum);
167     p++;
168     lowboost = readbyte_eeprom(p    , &sum);
169     p++;
170     stereo = readbyte_eeprom(p    , &sum);
171     p++;
172     // Write checksum
173     magic = (eeprom_readbyte(p) << 8) + eeprom_readbyte(p+1);
174
175     p+= 2;
176     if(sum != magic) return 0x00;
177     return 0xff;
178 }
179
180 /*
181  * Check eeprom, and format/restore.
182  */
183 void check_eeprom(void)
184 {
185     unsigned char c;
186         switch(load_eeprom()) {
187         case 0x01: // No magic-word
188             idle_time_ms(2000);
189             c = printhelp_2lines("EEPROM FORMAT", "Press any key");
190             _CLS();
191             _LOCATE(0,0);
192             printstr("Formatting...  ");
193             format_eeprom(2,250);
194             _LOCATE(0,0);
195             printstr("Save defaults  ");
196             setdefault();
197             save_eeprom();
198             break;
199         case 0x00: // Checksum error
200            idle_time_ms(2000);
201             c = printhelp_2lines("X-) Sum error", "Press any key");
202             c = pollkey_single();
203             _CLS();
204             _LOCATE(0,0);
205             printstr("Formatting...");
206             format_eeprom(2,250);
207 //            writeword_eeprom(0, &sum, 0x1298);
208             _LOCATE(0,0);
209             printstr("Save defaults");
210             setdefault();
211             save_eeprom();
212             break;
213         case 0xff: // Success
214             break;
215         default: // Unknown error
216             setdefault();
217             break;
218     }
219
220 }