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