flasher.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <mc1322x.h>
00037 #include <board.h>
00038
00039 #include "tests.h"
00040 #include "config.h"
00041
00042 #define DEBUG 1
00043 #if DEBUG
00044 #define dbg_putchr(...) putchr(__VA_ARGS__)
00045 #define dbg_putstr(...) putstr(__VA_ARGS__)
00046 #define dbg_put_hex(...) put_hex(__VA_ARGS__)
00047 #define dbg_put_hex16(...) put_hex16(__VA_ARGS__)
00048 #define dbg_put_hex32(...) put_hex32(__VA_ARGS__)
00049 #else
00050 #define dbg_putchr(...)
00051 #define dbg_putstr(...)
00052 #define dbg_put_hex(...)
00053 #define dbg_put_hex16(...)
00054 #define dbg_put_hex32(...)
00055 #endif
00056
00057 uint8_t getc(void)
00058 {
00059 volatile uint8_t c;
00060 while(*UART1_URXCON == 0);
00061
00062 c = *UART1_UDATA;
00063 return c;
00064 }
00065
00066
00067 void flushrx(void);
00068 uint32_t to_u32(volatile uint32_t *c);
00069
00070 enum parse_states {
00071 SCAN_X,
00072 READ_CHARS,
00073 PROCESS,
00074 MAX_STATE,
00075 };
00076
00077 void main(void) {
00078 nvmType_t type=0;
00079 nvmErr_t err;
00080 volatile uint8_t c;
00081 volatile uint32_t i;
00082 volatile uint32_t buf[4];
00083 volatile uint32_t len=0;
00084 volatile uint32_t state = SCAN_X;
00085 volatile uint32_t addr,data;
00086
00087
00088 uart_init(INC, MOD, SAMP);
00089 disable_irq(UART1);
00090
00091 vreg_init();
00092
00093 dbg_putstr("Detecting internal nvm\n\r");
00094
00095 err = nvm_detect(gNvmInternalInterface_c, &type);
00096
00097 dbg_putstr("nvm_detect returned: 0x");
00098 dbg_put_hex(err);
00099 dbg_putstr(" type is: 0x");
00100 dbg_put_hex32(type);
00101 dbg_putstr("\n\r");
00102
00103
00104 err = nvm_erase(gNvmInternalInterface_c, type, 0x7fffffff);
00105
00106 dbg_putstr("nvm_erase returned: 0x");
00107 dbg_put_hex(err);
00108 dbg_putstr("\n\r");
00109
00110 dbg_putstr(" type is: 0x");
00111 dbg_put_hex32(type);
00112 dbg_putstr("\n\r");
00113
00114
00115 len = 0;
00116 putstr("ready");
00117 flushrx();
00118
00119
00120 for(i=0; i<4; i++) {
00121 c = uart1_getc();
00122
00123 len += (c<<(i*8));
00124 }
00125
00126 dbg_putstr("len: ");
00127 dbg_put_hex32(len);
00128 dbg_putstr("\n\r");
00129
00130
00131
00132 #if BOOT_OK
00133 ((uint8_t *)buf)[0] = 'O'; ((uint8_t *)buf)[1] = 'K'; ((uint8_t *)buf)[2] = 'O'; ((uint8_t *)buf)[3] = 'K';
00134 #elif BOOT_SECURE
00135 ((uint8_t *)buf)[0] = 'S'; ((uint8_t *)buf)[1] = 'E'; ((uint8_t *)buf)[2] = 'C'; ((uint8_t *)buf)[3] = 'U';
00136 #else
00137 ((uint8_t *)buf)[0] = 'N'; ((uint8_t *)buf)[1] = 'O'; ((uint8_t *)buf)[2] = 'N'; ((uint8_t *)buf)[3] = 'O';
00138 #endif
00139
00140 dbg_putstr(" type is: 0x");
00141 dbg_put_hex32(type);
00142 dbg_putstr("\n\r");
00143
00144
00145 if(len == 0) {
00146 ((uint8_t *)buf)[0] = 'N';
00147 ((uint8_t *)buf)[1] = 'O';
00148 ((uint8_t *)buf)[2] = 'N';
00149 ((uint8_t *)buf)[3] = 'O';
00150 }
00151
00152 err = nvm_write(gNvmInternalInterface_c, type, (uint8_t *)buf, 0, 4);
00153
00154 dbg_putstr("nvm_write returned: 0x");
00155 dbg_put_hex(err);
00156 dbg_putstr("\n\r");
00157
00158
00159 err = nvm_write(gNvmInternalInterface_c, type, (uint8_t *)&len, 4, 4);
00160
00161
00162 for(i=0; i<len; i++) {
00163 c = getc();
00164 err = nvm_write(gNvmInternalInterface_c, type, (uint8_t *)&c, 8+i, 1);
00165 }
00166
00167 putstr("flasher done\n\r");
00168
00169 state = SCAN_X; addr=0;
00170 while((c=getc())) {
00171 if(state == SCAN_X) {
00172
00173 if(c==0) { break; }
00174 if(c!='x'){ continue; }
00175
00176 state = READ_CHARS;
00177 i = 0;
00178 }
00179 if(state == READ_CHARS) {
00180
00181 ((uint8_t *)buf)[i++] = c;
00182
00183
00184 if((c == ',') || (c == 0)) { state = PROCESS; }
00185 }
00186 if(state == PROCESS) {
00187 if(addr==0) {
00188
00189 addr = to_u32(buf);
00190 } else {
00191
00192 data = to_u32(buf);
00193 putstr("writing addr ");
00194 put_hex32(addr);
00195 putstr(" data ");
00196 put_hex32(data);
00197 putstr("\n\r");
00198 err = nvm_write(gNvmInternalInterface_c, 1, (uint8_t *)&data, addr, 4);
00199 addr += 4;
00200 }
00201
00202 state=SCAN_X;
00203 }
00204 }
00205
00206 while(1) {continue;};
00207 }
00208
00209 void flushrx(void)
00210 {
00211 volatile uint8_t c;
00212 while(*UART1_URXCON !=0) {
00213 c = *UART1_UDATA;
00214 }
00215 }
00216
00217
00218
00219
00220 uint8_t from_hex(uint8_t ch)
00221 {
00222 if(ch==' ' || ch=='\r' || ch=='\n')
00223 return 16;
00224
00225 if(ch < '0')
00226 goto bad;
00227 if(ch <= '9')
00228 return ch - '0';
00229 ch |= 0x20;
00230 if(ch < 'a')
00231 goto bad;
00232 if(ch <= 'f')
00233 return ch - 'a' + 10;
00234 bad:
00235 return 32;
00236 }
00237
00238 uint32_t to_u32(volatile uint32_t *c)
00239 {
00240 volatile uint32_t ret=0;
00241 volatile uint32_t i,val;
00242
00243
00244 i=1;
00245 while(((uint8_t *)c)[i] != ',') {
00246 ret = ret<<4;
00247 val = from_hex(((uint8_t *)c)[i++]);
00248 ret += val;
00249 }
00250 return ret;
00251 }
00252
00253
00254