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 #include "defines.h"
00029 #include "serial.h"
00030
00031 #include "sp_driver.h"
00032 #include "eeprom_driver.h"
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #ifdef LARGE_MEMORY
00052 # define ADDR_T unsigned long
00053 #else
00054 # define ADDR_T unsigned int
00055 #endif
00056
00057 #ifndef REMOVE_BLOCK_SUPPORT
00058 unsigned char BlockLoad(unsigned int size, unsigned char mem, ADDR_T *address);
00059 void BlockRead(unsigned int size, unsigned char mem, ADDR_T *address);
00060
00061
00062 #define BLOCKSIZE PAGESIZE
00063
00064 #endif
00065
00066 #ifdef __ICCAVR__
00067 # define C_TASK __C_task
00068 #else
00069 # define C_TASK
00070 #endif
00071
00072 C_TASK void main(void)
00073 {
00074 ADDR_T address = 0;
00075
00076 unsigned int temp_int;
00077 unsigned char val;
00078
00079 void (*funcptr)( void ) = 0x0000;
00080
00081 EEPROM_FlushBuffer();
00082 EEPROM_DisableMapping();
00083
00084 PROGPORT |= (1<<PROG_NO);
00085 initbootuart();
00086
00087
00088 if( !(PROGPIN & (1<<PROG_NO)) )
00089 {
00090
00091 for(;;)
00092 {
00093 val = recchar();
00094
00095 if(val=='a')
00096 {
00097 sendchar('Y');
00098 }
00099
00100 else if(val == 'A')
00101 {
00102 address = (recchar() << 8) | recchar();
00103 sendchar('\r');
00104 }
00105
00106 else if(val=='e')
00107 {
00108 for(address = 0; address < APP_END; address += PAGESIZE)
00109 {
00110 SP_WaitForSPM();
00111 #ifdef __ICCAVR__
00112 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
00113 #endif
00114 SP_EraseApplicationPage( address );
00115 #ifdef __ICCAVR__
00116 #pragma diag_default=Pe1053 // Back to default.
00117 #endif
00118 }
00119
00120
00121 EEPROM_LoadPage(&val);
00122
00123 EEPROM_EraseAll();
00124
00125 sendchar('\r');
00126 }
00127
00128 #ifndef REMOVE_BLOCK_SUPPORT
00129
00130 else if(val=='b')
00131 {
00132 sendchar('Y');
00133 sendchar((BLOCKSIZE>>8) & 0xFF);
00134 sendchar(BLOCKSIZE&0xFF);
00135 }
00136
00137
00138 else if(val=='B')
00139 {
00140 temp_int = (recchar()<<8) | recchar();
00141 val = recchar();
00142 sendchar( BlockLoad(temp_int, val, &address) );
00143 }
00144
00145 else if(val=='g')
00146 {
00147 temp_int = (recchar()<<8) | recchar();
00148 val = recchar();
00149 BlockRead(temp_int, val, &address);
00150 }
00151 #endif
00152
00153 #ifndef REMOVE_FLASH_BYTE_SUPPORT
00154
00155 else if(val=='R')
00156 {
00157
00158 SP_WaitForSPM();
00159 #ifdef __ICCAVR__
00160 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
00161 #endif
00162 sendchar( SP_ReadByte( (address << 1)+1) );
00163 sendchar( SP_ReadByte( (address << 1)+0) );
00164 #ifdef __ICCAVR__
00165 #pragma diag_default=Pe1053 // Back to default.
00166 #endif
00167 address++;
00168 }
00169
00170
00171
00172 else if(val=='c')
00173 {
00174 temp_int=recchar();
00175 sendchar('\r');
00176 }
00177
00178
00179 else if(val=='C')
00180 {
00181 temp_int |= (recchar()<<8);
00182 SP_WaitForSPM();
00183 #ifdef __ICCAVR__
00184 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
00185 #endif
00186 SP_LoadFlashWord( (address << 1), temp_int );
00187 #ifdef __ICCAVR__
00188 #pragma diag_default=Pe1053 // Back to default.
00189 #endif
00190 address++;
00191 sendchar('\r');
00192 }
00193
00194
00195
00196 else if(val== 'm')
00197 {
00198 if( address >= (APP_END>>1) )
00199 {
00200 sendchar('?');
00201 }
00202 else
00203 {
00204 SP_WaitForSPM();
00205 #ifdef __ICCAVR__
00206 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
00207 #endif
00208
00209 SP_WriteApplicationPage( address << 1);
00210 #ifdef __ICCAVR__
00211 #pragma diag_default=Pe1053 // Back to default.
00212 #endif
00213
00214 sendchar('\r');
00215 }
00216 #endif // REMOVE_FLASH_BYTE_SUPPORT
00217
00218 #ifndef REMOVE_EEPROM_BYTE_SUPPORT
00219 }
00220
00221 else if (val == 'D')
00222 {
00223 EEPROM_WriteByte( (unsigned char)(address / EEPROM_BYTES_IN_PAGE) , (unsigned char)(address & EEPROM_BYTE_ADDRESS_MASK), recchar() );
00224
00225 address++;
00226 }
00227
00228
00229 else if (val == 'd')
00230 {
00231
00232 sendchar( EEPROM_ReadByte( (unsigned char)(address / EEPROM_BYTES_IN_PAGE), (unsigned char)(address & EEPROM_BYTE_ADDRESS_MASK) ) );
00233
00234 address++;
00235 }
00236
00237 #endif
00238
00239 #ifndef REMOVE_FUSE_AND_LOCK_BIT_SUPPORT
00240
00241 else if(val=='l')
00242 {
00243
00244 SP_WaitForSPM();
00245
00246 SP_WriteLockBits( recchar() );
00247 sendchar('\r');
00248 }
00249 #if defined(_GET_LOCK_BITS)
00250
00251 else if(val=='r')
00252 {
00253 SP_WaitForSPM();
00254 sendchar( SP_ReadLockBits() );
00255 }
00256
00257 else if(val=='F')
00258 {
00259 SP_WaitForSPM();
00260 sendchar(SP_ReadFuseByte(0));
00261 }
00262
00263 else if(val=='N')
00264 {
00265 SP_WaitForSPM();
00266 sendchar(SP_ReadFuseByte(1));
00267 }
00268
00269 else if(val=='Q')
00270 {
00271 SP_WaitForSPM();
00272 sendchar(SP_ReadFuseByte(2));
00273 }
00274 #endif
00275 #endif
00276
00277 #ifndef REMOVE_AVRPROG_SUPPORT
00278
00279 else if((val=='P')||(val=='L'))
00280 {
00281 sendchar('\r');
00282 }
00283
00284 else if(val=='E')
00285 {
00286 SP_WaitForSPM();
00287
00288 sendchar('\r');
00289 funcptr();
00290 }
00291
00292 else if (val=='p')
00293 {
00294 sendchar('S');
00295 }
00296
00297 else if(val=='t')
00298 {
00299 #if PARTCODE+0 > 0
00300 sendchar( PARTCODE );
00301 #endif
00302 sendchar( 0 );
00303 }
00304
00305 else if((val=='x')||(val=='y')||(val=='T'))
00306 {
00307 recchar();
00308 sendchar('\r');
00309 }
00310 #endif
00311
00312 else if(val=='S')
00313 {
00314 sendchar('A');
00315 sendchar('V');
00316 sendchar('R');
00317 sendchar('B');
00318 sendchar('O');
00319 sendchar('O');
00320 sendchar('T');
00321 }
00322
00323 else if(val=='V')
00324 {
00325 sendchar('1');
00326 sendchar('6');
00327 }
00328
00329 else if(val=='s')
00330 {
00331 sendchar( SIGNATURE_BYTE_3 );
00332 sendchar( SIGNATURE_BYTE_2 );
00333 sendchar( SIGNATURE_BYTE_1 );
00334 }
00335
00336 else if(val!=0x1b)
00337 {
00338 sendchar('?');
00339 }
00340 }
00341 }
00342 else
00343 {
00344 SP_WaitForSPM();
00345 SP_LockSPM();
00346 funcptr();
00347 }
00348 }
00349
00350
00351 #ifndef REMOVE_BLOCK_SUPPORT
00352
00353 unsigned char BlockLoad(unsigned int size, unsigned char mem, ADDR_T *address)
00354 {
00355 unsigned int data;
00356 ADDR_T tempaddress;
00357
00358
00359 if(mem=='E')
00360 {
00361 unsigned char pageAddr, byteAddr, value;
00362 unsigned char buffer[BLOCKSIZE];
00363
00364 EEPROM_FlushBuffer();
00365
00366 EEPROM_DisableMapping();
00367
00368
00369 for(tempaddress=0;tempaddress<size;tempaddress++){
00370 buffer[tempaddress] = recchar();
00371 }
00372
00373
00374 for( tempaddress=0; tempaddress < size; tempaddress++)
00375 {
00376
00377 pageAddr = (unsigned char)( (*address) / EEPROM_BYTES_IN_PAGE);
00378 byteAddr = (unsigned char)( (*address) & EEPROM_BYTE_ADDRESS_MASK);
00379 value = buffer[tempaddress];
00380
00381 EEPROM_WriteByte(pageAddr, byteAddr, value);
00382
00383 (*address)++;
00384 }
00385
00386 return '\r';
00387 }
00388
00389
00390 else if(mem=='F')
00391 {
00392 (*address) <<= 1;
00393 tempaddress = (*address);
00394
00395 do
00396 {
00397 data = recchar();
00398 data |= (recchar() << 8);
00399 #ifdef __ICCAVR__
00400 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
00401 #endif
00402 SP_LoadFlashWord(*address, data);
00403 #ifdef __ICCAVR__
00404 #pragma diag_default=Pe1053 // Back to default.
00405 #endif
00406 (*address)+=2;
00407 size -= 2;
00408 } while(size);
00409
00410 #ifdef __ICCAVR__
00411 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
00412 #endif
00413 SP_WriteApplicationPage(tempaddress);
00414
00415 #ifdef __ICCAVR__
00416 #pragma diag_default=Pe1053 // Back to default.
00417 #endif
00418
00419 SP_WaitForSPM();
00420 (*address) >>= 1;
00421 return '\r';
00422 }
00423
00424
00425 else
00426 {
00427 return '?';
00428 }
00429 }
00430
00431
00432
00433 void BlockRead(unsigned int size, unsigned char mem, ADDR_T *address)
00434 {
00435
00436
00437 if (mem=='E')
00438 {
00439 unsigned char byteAddr, pageAddr;
00440
00441 EEPROM_DisableMapping();
00442 EEPROM_FlushBuffer();
00443
00444 do
00445 {
00446 pageAddr = (unsigned char)(*address / EEPROM_BYTES_IN_PAGE);
00447 byteAddr = (unsigned char)(*address & EEPROM_BYTE_ADDRESS_MASK);
00448
00449 sendchar( EEPROM_ReadByte( pageAddr, byteAddr ) );
00450
00451 (*address)++;
00452 size--;
00453 } while (size);
00454 }
00455
00456
00457 else if(mem=='F')
00458 {
00459 (*address) <<= 1;
00460
00461 do
00462 {
00463 #ifdef __ICCAVR__
00464 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
00465 #endif
00466 sendchar( SP_ReadByte( *address) );
00467 sendchar( SP_ReadByte( (*address)+1) );
00468 #ifdef __ICCAVR__
00469 #pragma diag_default=Pe1053 // Back to default.
00470 #endif
00471 (*address) += 2;
00472 size -= 2;
00473 } while (size);
00474
00475 (*address) >>= 1;
00476 }
00477 }
00478
00479 #endif
00480
00481