wctrl.c

00001 /*
00002   ターゲット側の書き込みプログラム
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "wctrl.h"
00008 #include "tiny_sci.h"
00009 
00010 
00011 struct st_flm {
00012   struct { unsigned char BYTE; } FLMCR1;
00013 };
00014 #define LOOP(X) for(i = X; i > 0; i--) /* Wait Loop */
00015 
00016 
00017 // 符号なし数字文字列を数値に変換する
00018 static int atoi(char *str) {
00019   int ret_value = 0;
00020   int index;
00021 
00022   for (index = 0; (str[index] >= '0') && (str[index] <= '9'); ++index) {
00023     ret_value *= 10;
00024     ret_value += str[index] - '0';
00025   }
00026 
00027   return ret_value;
00028 }
00029 
00030 
00031 static int adjustBaudrate(void) {
00032   char recv_buffer[16];
00033   int n;
00034 
00035   // ボーレート設定の受け取り
00036   n = recvLine(recv_buffer, 16);
00037   if (recv_buffer[0] == 'S') {
00038     if (!setBaudrate(atoi(&recv_buffer[1]))) {
00039       putch('0');
00040       return 0;
00041     }
00042   }
00043   putch('E');
00044   return -1;
00045 }
00046 
00047 
00048 static int writeFlushRom(long addr, char *data) {
00049   volatile char *address = (char *)addr;
00050   volatile char *flmcr = (char *)FLMCR1_ADDR;
00051   volatile int i = 0;           /* use LOOP() */
00052   char rewrite[32];
00053   char verify;
00054   int n, m, j;
00055 
00056   if (addr > 0x0001ffff) {
00057     flmcr += 1;
00058   }
00059 
00060   FLM.FLMCR1.BYTE |= 0x40;      /* FLMR1 SWE=1 */
00061   LOOP(WAIT_10u);
00062 
00063   for (j = 0; j < 32; ++j) {
00064     rewrite[j] = data[j];
00065   }
00066 
00067   n = 1;
00068   do {
00069     for (j = 0; j < 32; ++j) {  /* data byte write */
00070       *(address + j) = rewrite[j];
00071     }
00072 
00073     WDT_TCSR_WORD = WDT_573u; /* set WDT */
00074 
00075     *flmcr = 0x50;              /* set FLMCR1(2) PSU1(2)=1 */
00076     LOOP(WAIT_50u);
00077 
00078     *flmcr = 0x51;              /* set FLMCR1(2) P1(2)=1 */
00079     LOOP(WAIT_200u);
00080 
00081     *flmcr = 0x50;              /* set FLMCR1(2) P1(2)=0 */
00082     LOOP(WAIT_10u);
00083 
00084     *flmcr = 0x40;              /* set FLMCR1(2) PSU1(2)=0 */
00085     LOOP(WAIT_10u);
00086 
00087     WDT_TCSR_WORD = WDT_STOP;   /* stop WDT */
00088     *flmcr = 0x44;              /* set FLMCR1(2) PV1(2)=1 */
00089     LOOP(WAIT_4u);
00090 
00091     m = 0;
00092     for (j = 0; j < 32; j++) {
00093       *(address + j) = 0xff;
00094       rewrite[j] = 0xff;
00095       LOOP(WAIT_2u);
00096       verify = *(address + j);
00097       if (data[j] != verify) {
00098         m = 1;
00099       }
00100       rewrite[j] = ~(data[j] ^ verify) | data[j];
00101     }
00102 
00103     *flmcr = 0x40;              /* set FLMCR1(2) PV1(2)=0 */
00104     LOOP(WAIT_4u);
00105 
00106   } while ((m != 0) && (n++ < WRITE_TIMES));
00107 
00108   FLM.FLMCR1.BYTE = 0x00;       /* FLMR1 SWE=0 */
00109   if (m != 0) {
00110     return -1;
00111   }
00112   return 0;
00113 }
00114 
00115 
00116 static void writeRom(void) {
00117   unsigned char data[32];
00118   unsigned long addr;
00119   unsigned char check_sum;
00120   unsigned char sum;
00121   int retry_times = 0;
00122   int i;
00123   char ch;
00124 
00125   while (1) {
00126     sum = 0x00;
00127     addr = 0x0000;
00128     for (i = 0; i < 4; ++i) {
00129       ch = getch();
00130       addr = (addr << 8) + (unsigned char)ch;
00131       sum += ch;
00132     }
00133     for (i = 0; i < 32; ++i) {
00134       ch = getch();
00135       data[i] = (unsigned char)ch;
00136       sum += ch;
00137     }
00138     check_sum = getch();
00139 
00140     // チェックサム
00141     if (sum != check_sum) {
00142       ++retry_times;
00143       putch('0' + retry_times++);
00144       continue;
00145     }
00146 
00147     /* F-ZTAT への書き込み */
00148     if (writeFlushRom(addr, data)) {
00149       putch('E');
00150       break;
00151     }
00152     retry_times = 0;
00153     putch('0');
00154   }
00155 }
00156 
00157 
00158 void __main(void){};
00159 int main(void) {
00160 
00161   // シリアルデバイスの初期化
00162   initSCI();
00163 
00164   // データ転送ボーレート設定の受け取りと更新
00165   if (!adjustBaudrate()) {
00166     writeRom();                 // データ受信と書き込み
00167   }
00168 
00169   // 終了
00170   while (1)
00171     ;
00172 }
00173 

Generated on Mon Apr 13 22:52:05 2009 by  doxygen 1.5.7.1