wctrl.c
00001
00002
00003
00004
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--)
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;
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;
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) {
00070 *(address + j) = rewrite[j];
00071 }
00072
00073 WDT_TCSR_WORD = WDT_573u;
00074
00075 *flmcr = 0x50;
00076 LOOP(WAIT_50u);
00077
00078 *flmcr = 0x51;
00079 LOOP(WAIT_200u);
00080
00081 *flmcr = 0x50;
00082 LOOP(WAIT_10u);
00083
00084 *flmcr = 0x40;
00085 LOOP(WAIT_10u);
00086
00087 WDT_TCSR_WORD = WDT_STOP;
00088 *flmcr = 0x44;
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;
00104 LOOP(WAIT_4u);
00105
00106 } while ((m != 0) && (n++ < WRITE_TIMES));
00107
00108 FLM.FLMCR1.BYTE = 0x00;
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
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