• メインページ
  • データ構造
  • ファイル
  • ファイル一覧
  • グローバル

OpenSSM/config.c

説明を見る。
00001 /**************************************************************************************************
00002     Title           : Configuration Save / Load Interface
00003     Programmer      : Yosuke FURUSAWA.
00004     Copyright       : Copyright (C) 2010 Yosuke FURUSAWA.
00005     Since           : 2010/07/03
00006 
00007     Filename        : config.c
00008     Last up date    : 2010/08/31
00009     Kanji-Code      : Shift-JIS
00010     TAB Space       : 4
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 #include "libvideo.h"
00025 
00026 #include "ssm.h"
00027 #include "main.h"
00028 #include "extmeter.h"
00029 #include "screen.h"
00030 
00031 
00032 #include "config.h"
00033 
00034 
00035 /*================================================================================================
00036 マクロ定義
00037 =================================================================================================*/
00038 
00039 
00040 /*================================================================================================
00041 構造体
00042 =================================================================================================*/
00043 typedef struct CONFIG {
00044     void *ptr;
00045     unsigned char size;
00046 } CONFIG_T;
00047 typedef CONFIG_T* pCONFIG_T;
00048 
00049 
00050 /*================================================================================================
00051 グローバル変数
00052 =================================================================================================*/
00053 /* 保存対象を設定する */
00054 static const CONFIG_T config[] = {
00055     { &extmeter,            sizeof(EXTMETER_T),     },
00056     { &screen,              sizeof(SCREEN_T),       },
00057     { &ssm,                 sizeof(SSM_T),          },
00058 //  { &ntsc,                sizeof(NTSC_T),         },      /* 問題があったとき表示されなくなる */
00059     { NULL,                 0x00,                   },
00060 };
00061 
00062 
00063 /*================================================================================================
00064 プロトタイプ宣言
00065 =================================================================================================*/
00066 static  BOOL CONFIG_write_and_verify(const unsigned int ptr, const unsigned char data);
00067 static  unsigned int CONFIG_make_checksum(void);
00068 static  unsigned int CONFIG_read_checksum(void);
00069 
00070 
00071 /**************************************************************************************************
00072 初期化
00073 **************************************************************************************************/
00074 BOOL CONFIG_init(void)
00075 {
00076     return(TRUE);
00077 }
00078 
00079 
00080 /**************************************************************************************************
00081 保存する
00082 ---------------------------------------------------------------------------------------------------
00083 復帰処理が面倒なので、何かあったら WDTリセットさせる
00084 **************************************************************************************************/
00085 BOOL CONFIG_save(void)
00086 {
00087     unsigned char *target_ptr;
00088     unsigned int i, j, eeprom_ptr, checksum;
00089 
00090     eeprom_ptr = 0;
00091 
00092     /* CONFIGリストの処理 */
00093     for(i = 0; config[i].size != 0; i++){
00094         ClrWdt();
00095         target_ptr = config[i].ptr;
00096 
00097         /* 保存対象の構造体を書き込む */
00098         for(j = 0; j < config[i].size; j++){
00099             while(!CONFIG_write_and_verify(eeprom_ptr, *target_ptr));
00100             target_ptr++;
00101             eeprom_ptr++;
00102         }
00103     }
00104 
00105     /* チェックサムの記録 */
00106     checksum = CONFIG_make_checksum();
00107     while(!CONFIG_write_and_verify(eeprom_ptr, (checksum >> 8)));
00108     eeprom_ptr++;
00109     while(!CONFIG_write_and_verify(eeprom_ptr, (checksum     )));
00110 
00111     return(TRUE);
00112 }
00113 
00114 
00115 /**************************************************************************************************
00116 読み込む
00117 ---------------------------------------------------------------------------------------------------
00118 復帰処理が面倒なので、何かあったら WDTリセットさせる
00119 **************************************************************************************************/
00120 BOOL CONFIG_load(void)
00121 {
00122     unsigned char *target_ptr;
00123     unsigned int i, j, eeprom_ptr, checksum, verify, buf;
00124 
00125     eeprom_ptr = 0;
00126 
00127     /* チェックサムを確認する */
00128     checksum = CONFIG_make_checksum();
00129     verify   = CONFIG_read_checksum();
00130 
00131     /* チェックサムが異なるときはエラーを返す */
00132     if(checksum != verify) return(FALSE);
00133 
00134 
00135     /* メモリに書き込む */
00136     for(i = 0; config[i].size != 0; i++){
00137         ClrWdt();
00138         target_ptr = config[i].ptr;
00139 
00140         /* 保存対象の構造体を読込 */
00141         for(j = 0; j < config[i].size; j++){
00142             while((buf = EEPROM_read(eeprom_ptr)) == -1);
00143             *target_ptr = buf;
00144             target_ptr++;
00145             eeprom_ptr++;
00146         }
00147     }
00148 
00149 
00150 
00151     return(TRUE);
00152 }
00153 
00154 
00155 /**************************************************************************************************
00156 データを覗く
00157 **************************************************************************************************/
00158 void CONFIG_dump_config(void)
00159 {
00160     unsigned char *ptr;
00161     unsigned int i, j;
00162 
00163     for(i = 0; config[i].size != NULL; i++){
00164         ptr = config[i].ptr;
00165 
00166         UART1_puthex(i);
00167         UART1_puthex(config[i].size);
00168         UART1_putch(' ');
00169 
00170         for(j = 0; j < config[i].size; j++){
00171             ClrWdt();
00172             UART1_puthex(*ptr);
00173             UART1_putch(' ');
00174             ptr++;
00175             while(UART1_get_sendbuf() < 10);
00176         }
00177 
00178         UART1_putch('¥r');
00179         UART1_putch('¥n');
00180     }
00181 
00182     return;
00183 }
00184 
00185 
00186 /*-------------------------------------------------------------------------------------------------
00187 ベリファイ付書込
00188 ---------------------------------------------------------------------------------------------------
00189 復帰処理が面倒なので、何かあったら WDTリセットさせる
00190 -------------------------------------------------------------------------------------------------*/
00191 static  BOOL CONFIG_write_and_verify(const unsigned int ptr, const unsigned char data)
00192 {
00193     int buf;
00194 
00195     do {
00196         EEPROM_write(ptr, data);
00197         buf = EEPROM_read(ptr);
00198     } while (buf != data || buf == -1);
00199 
00200     return(TRUE);
00201 }
00202 
00203 
00204 /*-------------------------------------------------------------------------------------------------
00205 EEPROMのデータからチェックサムを生成する
00206 -------------------------------------------------------------------------------------------------*/
00207 static  unsigned int CONFIG_make_checksum(void)
00208 {
00209     int buf;
00210     unsigned int i, j, eeprom_ptr, checksum;
00211 
00212     eeprom_ptr = 0;
00213     checksum = 0;
00214 
00215     for(i = 0; config[i].size != 0; i++){
00216         ClrWdt();
00217 
00218         for(j = 0; j < config[i].size; j++){
00219             while((buf = EEPROM_read(eeprom_ptr)) == -1);
00220             checksum += buf;
00221             eeprom_ptr++;
00222         }
00223     }
00224 
00225     return(checksum);
00226 }
00227 
00228 
00229 /*-------------------------------------------------------------------------------------------------
00230 EEPROMのチェックサムを読み込む
00231 -------------------------------------------------------------------------------------------------*/
00232 static  unsigned int CONFIG_read_checksum(void)
00233 {
00234     int buf;
00235     unsigned int i, eeprom_ptr, checksum;
00236 
00237     /* チェックサムのアドレスを求める */
00238     eeprom_ptr = 0;
00239     for(i = 0; config[i].size != 0; i++){
00240         eeprom_ptr += config[i].size;
00241     }
00242 
00243     /* チェックサムを読み込む */
00244     while((buf = EEPROM_read(eeprom_ptr)) == -1);
00245     checksum = buf << 8;
00246     eeprom_ptr++;
00247     while((buf = EEPROM_read(eeprom_ptr)) == -1);
00248     checksum = checksum + buf;
00249 
00250     return(checksum);
00251 }

OpenSSMに対してThu Sep 9 2010 00:03:04に生成されました。  doxygen 1.7.1