OSDN Git Service

[UI][DISP][v1.0] Refine display, printstr().
[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 "eeprom_util.h"
30
31 void save_eeprom(void)
32 {
33     unsigned int p[1];
34     unsigned int sum[1];
35     unsigned char i;
36     unsigned char j;
37     unsigned char *q = (char *)(&setup);
38
39     *p = 0;
40     *sum = 0;
41     // Magic word
42     writeword_eeprom(p, sum, 0x1298);
43     // amfreq
44     for(i = 0; i < sizeof(__radioset_t); i++) {
45         writebyte_eeprom(p, sum, *q);
46         q++;
47     }
48
49     // Write checksum
50     eeprom_writebyte(*p, sum[0] >> 8);
51     eeprom_writebyte(*p + 1, sum[0] & 0xff);
52 }
53
54 unsigned char load_eeprom(void)
55 {
56     unsigned int p;
57     unsigned int sum;
58     unsigned char i;
59     unsigned char *q = (unsigned char *)(&setup);
60     unsigned char j;
61     unsigned int magic;
62
63     p = 0;
64     sum = 0;
65     // Magic word
66     magic = readword_eeprom(&p, &sum);
67     if(magic != 0x1298) return 0x01; // NO MAGICWORD
68     // amfreq
69     for(i = 0; i < sizeof(__radioset_t); i++ ){
70         *q = readbyte_eeprom(&p, &sum);
71         q++;
72     }
73     magic = (eeprom_readbyte(p) << 8) + eeprom_readbyte(p + 1);
74
75     if(sum != magic) return 0x00;
76     return 0xff;
77 }
78
79 /*
80  * Check eeprom, and format/restore.
81  */
82 static void check_eeprom_sub(void)
83 {
84  _CLS();
85  _LOCATE(0,0);
86  printstr("Formatting...  ");
87  format_eeprom(2,250);
88  _LOCATE(0,0);
89  printstr("Save defaults  ");
90  setdefault();
91  save_eeprom();
92 }
93
94 void check_eeprom(void)
95 {
96     unsigned char c;
97
98     switch(load_eeprom()) {
99         case 0x01: // No magic-word
100             idle_time_ms(2000);
101             c = printhelp_2lines("EEPROM FORMAT", "Press any key");
102             check_eeprom_sub();
103             break;
104         case 0x00: // Checksum error
105            idle_time_ms(2000);
106             c = printhelp_2lines("X-) Sum error", "Press any key");
107             check_eeprom_sub();
108             break;
109         case 0xff: // Success
110             return;
111             break;
112         default: // Unknown error
113             break;
114     }
115     valinit();
116 }