説明を見る。00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <p24FJ64GA002.h>
00019
00020 #include "types.h"
00021 #include "table.h"
00022
00023 #include "libeeprom.h"
00024 #include "libuart.h"
00025 #include "libvideo.h"
00026
00027 #include "ssm.h"
00028 #include "main.h"
00029 #include "extmeter.h"
00030 #include "screen.h"
00031
00032
00033 #include "config.h"
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 typedef struct CONFIG {
00045 void *ptr;
00046 unsigned char size;
00047 } CONFIG_T;
00048 typedef CONFIG_T* pCONFIG_T;
00049
00050
00051
00052
00053
00054
00055 static const CONFIG_T config[] = {
00056 { &extmeter, sizeof(EXTMETER_T), },
00057 { &screen, sizeof(SCREEN_T), },
00058 { &ssm, sizeof(SSM_T), },
00059
00060 { NULL, 0x00, },
00061 };
00062
00063
00064
00065
00066
00067 static BOOL CONFIG_write_and_verify(const unsigned int ptr, const unsigned char data);
00068 static unsigned int CONFIG_make_checksum(void);
00069 static unsigned int CONFIG_read_checksum(void);
00070
00071
00072
00073
00074
00075 BOOL CONFIG_init(void)
00076 {
00077 return(TRUE);
00078 }
00079
00080
00081
00082
00083
00084
00085
00086 BOOL CONFIG_save(void)
00087 {
00088 unsigned char *target_ptr;
00089 unsigned int i, j, eeprom_ptr, checksum;
00090
00091 eeprom_ptr = 0;
00092
00093
00094 for(i = 0; config[i].size != 0; i++){
00095 ClrWdt();
00096 target_ptr = config[i].ptr;
00097
00098
00099 for(j = 0; j < config[i].size; j++){
00100 while(!CONFIG_write_and_verify(eeprom_ptr, *target_ptr));
00101 target_ptr++;
00102 eeprom_ptr++;
00103 }
00104 }
00105
00106
00107 checksum = CONFIG_make_checksum();
00108 while(!CONFIG_write_and_verify(eeprom_ptr, (checksum >> 8)));
00109 eeprom_ptr++;
00110 while(!CONFIG_write_and_verify(eeprom_ptr, (checksum )));
00111
00112 return(TRUE);
00113 }
00114
00115
00116
00117
00118
00119
00120
00121 BOOL CONFIG_load(void)
00122 {
00123 unsigned char *target_ptr;
00124 unsigned int i, j, eeprom_ptr, checksum, verify, buf;
00125
00126 eeprom_ptr = 0;
00127
00128
00129 checksum = CONFIG_make_checksum();
00130 verify = CONFIG_read_checksum();
00131
00132
00133 if(checksum != verify) return(FALSE);
00134
00135
00136
00137 for(i = 0; config[i].size != 0; i++){
00138 ClrWdt();
00139 target_ptr = config[i].ptr;
00140
00141
00142 for(j = 0; j < config[i].size; j++){
00143 while((buf = EEPROM_read(eeprom_ptr)) == -1);
00144 *target_ptr = buf;
00145 target_ptr++;
00146 eeprom_ptr++;
00147 }
00148 }
00149
00150
00151
00152 return(TRUE);
00153 }
00154
00155
00156
00157
00158
00159 void CONFIG_dump_config(void)
00160 {
00161 unsigned char *ptr;
00162 unsigned int i, j;
00163
00164 for(i = 0; config[i].size != NULL; i++){
00165 ptr = config[i].ptr;
00166
00167 UART1_puthex(i);
00168 UART1_puthex(config[i].size);
00169 UART1_putch(' ');
00170
00171 for(j = 0; j < config[i].size; j++){
00172 ClrWdt();
00173 UART1_puthex(*ptr);
00174 UART1_putch(' ');
00175 ptr++;
00176 while(UART1_get_sendbuf() < 10);
00177 }
00178
00179 UART1_putch('¥r');
00180 UART1_putch('¥n');
00181 }
00182
00183 return;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192 static BOOL CONFIG_write_and_verify(const unsigned int ptr, const unsigned char data)
00193 {
00194 int buf;
00195
00196 do {
00197 EEPROM_write(ptr, data);
00198 buf = EEPROM_read(ptr);
00199 } while (buf != data || buf == -1);
00200
00201 return(TRUE);
00202 }
00203
00204
00205
00206
00207
00208 static unsigned int CONFIG_make_checksum(void)
00209 {
00210 int buf;
00211 unsigned int i, j, eeprom_ptr, checksum;
00212
00213 eeprom_ptr = 0;
00214 checksum = 0;
00215
00216 for(i = 0; config[i].size != 0; i++){
00217 ClrWdt();
00218
00219 for(j = 0; j < config[i].size; j++){
00220 while((buf = EEPROM_read(eeprom_ptr)) == -1);
00221 checksum += buf;
00222 eeprom_ptr++;
00223 }
00224 }
00225
00226 return(checksum);
00227 }
00228
00229
00230
00231
00232
00233 static unsigned int CONFIG_read_checksum(void)
00234 {
00235 int buf;
00236 unsigned int i, eeprom_ptr, checksum;
00237
00238
00239 eeprom_ptr = 0;
00240 for(i = 0; config[i].size != 0; i++){
00241 eeprom_ptr += config[i].size;
00242 }
00243
00244
00245 while((buf = EEPROM_read(eeprom_ptr)) == -1);
00246 checksum = buf << 8;
00247 eeprom_ptr++;
00248 while((buf = EEPROM_read(eeprom_ptr)) == -1);
00249 checksum = checksum + buf;
00250
00251 return(checksum);
00252 }