teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bootloader.cpp Source File

bootloader.cpp

00001 #include "bootloader.h"
00002 #define IAP_LOCATION    0x1fff1ff1
00003 #define memBufTam 1024
00004 bool callBootLoader = false;
00005 char memBufConfigBootLoader[256];
00006 char executaBootLoader;
00007 uint32_t tamFirmware;
00008 //firmwareDescriptor firmDesc;
00009 IAP IAP_call;
00010 /*****************************************************************************//**
00011  * @brief       De-initializes the NVIC peripheral registers to their default
00012  *              reset values.
00013  * @param       None
00014  * @return      None
00015  *
00016  * These following NVIC peripheral registers will be de-initialized:
00017  * - Disable Interrupt (32 IRQ interrupt sources that matched with LPC17xx)
00018  * - Clear all Pending Interrupts (32 IRQ interrupt source that matched with LPC17xx)
00019  * - Clear all Interrupt Priorities (32 IRQ interrupt source that matched with LPC17xx)
00020  *******************************************************************************/
00021 void NVIC_DeInit(void)
00022 {
00023     uint8_t tmp;
00024 
00025     /* Disable all interrupts */
00026     NVIC->ICER[0] = 0xFFFFFFFF;
00027     NVIC->ICER[1] = 0x00000001;
00028     /* Clear all pending interrupts */
00029     NVIC->ICPR[0] = 0xFFFFFFFF;
00030     NVIC->ICPR[1] = 0x00000001;
00031 
00032     /* Clear all interrupt priority */
00033     for (tmp = 0; tmp < 32; tmp++) {
00034         NVIC->IP[tmp] = 0x00;
00035     }
00036 }
00037 
00038 
00039 
00040 /*****************************************************************************//**
00041  * @brief           De-initializes the SCB peripheral registers to their default
00042  *                  reset values.
00043  * @param           none
00044  * @return          none
00045  *
00046  * These following SCB NVIC peripheral registers will be de-initialized:
00047  * - Interrupt Control State register
00048  * - Interrupt Vector Table Offset register
00049  * - Application Interrupt/Reset Control register
00050  * - System Control register
00051  * - Configuration Control register
00052  * - System Handlers Priority Registers
00053  * - System Handler Control and State Register
00054  * - Configurable Fault Status Register
00055  * - Hard Fault Status Register
00056  * - Debug Fault Status Register
00057  *******************************************************************************/
00058 void NVIC_SCBDeInit(void)
00059 {
00060     uint8_t tmp;
00061 
00062     SCB->ICSR = 0x0A000000;
00063     SCB->VTOR = 0x00000000;
00064     SCB->AIRCR = 0x05FA0000;
00065     SCB->SCR = 0x00000000;
00066     SCB->CCR = 0x00000000;
00067 
00068     for (tmp = 0; tmp < 32; tmp++) {
00069         SCB->SHP[tmp] = 0x00;
00070     }
00071 
00072     SCB->SHCSR = 0x00000000;
00073     SCB->CFSR = 0xFFFFFFFF;
00074     SCB->HFSR = 0xFFFFFFFF;
00075     SCB->DFSR = 0xFFFFFFFF;
00076 }
00077 
00078 #define APPL_RUN_ADDR 0x70000
00079 __asm void boot_jump(uint32_t address)
00080 {
00081     LDR SP, [R0]
00082     LDR PC, [R0, #4]
00083 }
00084 
00085 void flashPrepare(){
00086     IAP_call=(IAP) IAP_LOCATION;
00087 }
00088 
00089 void firmDescToFlash(){
00090     //Guardo tamanho do firmware em bytes na flash no setor 29    
00091     //Preparing flash sectors to write    
00092     unsigned int commandBytes[5];
00093     unsigned int outputBytes[5]; 
00094     
00095     //Preparo setores
00096     commandBytes[0]=50;
00097     commandBytes[1]=29;
00098     commandBytes[2]=29;
00099     commandBytes[3]=96000;
00100     commandBytes[4]=0x0;
00101     IAP_call(commandBytes,outputBytes);    
00102     if(outputBytes[0]!=0x0){
00103         printf("Falha na escrita em flash parte 7 -> codigo [%lu].\r\n",outputBytes[0]);
00104         return;
00105     }    
00106     
00107     //Erasing flash sectors
00108     commandBytes[0]=52;
00109     commandBytes[1]=29;
00110     commandBytes[2]=29;
00111     commandBytes[3]=96000;
00112     commandBytes[4]=0x0;
00113     IAP_call(commandBytes,outputBytes);    
00114     if(outputBytes[0]!=0x0){
00115         printf("Falha na escrita em flash parte 2 -> codigo [%lu].\r\n",outputBytes[0]);
00116         return;
00117     }
00118     
00119     //Preparo setores
00120     commandBytes[0]=50;
00121     commandBytes[1]=29;
00122     commandBytes[2]=29;
00123     commandBytes[3]=96000;
00124     commandBytes[4]=0x0;
00125     IAP_call(commandBytes,outputBytes);    
00126     if(outputBytes[0]!=0x0){
00127         printf("Falha na escrita em flash parte 7 -> codigo [%lu].\r\n",outputBytes[0]);
00128         return;
00129     }  
00130     
00131     memBufConfigBootLoader[0]=(tamFirmware>>24)&0xFF;
00132     memBufConfigBootLoader[1]=(tamFirmware>>16)&0xFF;
00133     memBufConfigBootLoader[2]=(tamFirmware>>8)&0xFF;
00134     memBufConfigBootLoader[3]=(tamFirmware>>0)&0xFF;
00135     memBufConfigBootLoader[4]=executaBootLoader;
00136     
00137     //escrevo nos setores
00138     commandBytes[0]=51;
00139     commandBytes[1]=((unsigned long) sector_start_adress[29]);
00140     commandBytes[2]=(unsigned long) memBufConfigBootLoader;
00141     commandBytes[3]=256;
00142     commandBytes[4]=96000;
00143     
00144     IAP_call(commandBytes,outputBytes);
00145     
00146     if(outputBytes[0]!=0x0){
00147         printf("Falha na escrita em flash parte 8 -> codigo [%lu].\r\n",outputBytes[0]);
00148         return;
00149     }    
00150 }
00151 
00152 void bootLoader(void)
00153 {
00154      /* Change the Vector Table to the USER_FLASH_START
00155      in case the user application uses interrupts */
00156      diversos::wdt.kick(90.0);
00157      SysTick->CTRL &= ~0x00000002;           // disable SysTick interrupt
00158      //NVIC_DeInit();     
00159      //__disable_irq();
00160      LPC_SC->PCONP = 0x001817BE;
00161      SCB->VTOR = APPL_RUN_ADDR & 0x1FFFF80;
00162      boot_jump(APPL_RUN_ADDR);
00163 }
00164 
00165 uint8_t bootloader_fillUpFlash(arquivoSD *arquivo){
00166     #define flashSectorFirmwareCopyOffset 20
00167     
00168     /*IAP IAP_call;
00169     IAP_call=(IAP) IAP_LOCATION;*/
00170     
00171     //firmwareDescriptor firmDesc;
00172     unsigned int commandBytes[5];
00173     unsigned int outputBytes[5]; 
00174     uint32_t i;
00175     uint32_t chunkIndex=0;
00176     uint32_t nChunks,nEndChunk; 
00177     unsigned char memBuf[memBufTam];   
00178     sdCard::getFileTam(arquivo);
00179     tamFirmware = 0;
00180     if(!sdCard::abreArquivo(arquivo,"r")){
00181         printf("Arquivo nao encontrado.\r\n");
00182         while(1);    
00183     }
00184     
00185     //Excluindo o start no inicio
00186     tamFirmware=arquivo->bytes-5;
00187     for(i=0;i<5;i++){
00188         memBuf[i] = fgetc(arquivo->fp);
00189         //printf("%c",memBuf[i]);
00190     }    
00191     nChunks   = tamFirmware/memBufTam;
00192     nEndChunk = tamFirmware%memBufTam;       
00193     
00194     printf("Tamanho do arquivo %s = %lu bytes.\r\n",arquivo->nome,tamFirmware);
00195     printf("nChunks = %lu, nEndChunk = %lu.\r\n",nChunks,nEndChunk);
00196     printf("Iniciando processo de escrita em flash.\r\nDesligando interrupcoes.\r\n");
00197     
00198     
00199     //Preparing flash sectors to write
00200     commandBytes[0]=50;
00201     commandBytes[1]=20;
00202     commandBytes[2]=26;
00203     commandBytes[3]=96000;
00204     commandBytes[4]=0x0;
00205     IAP_call(commandBytes,outputBytes);    
00206     if(outputBytes[0]!=0x0){
00207         printf("Falha na escrita em flash parte 1 -> codigo [%lu].\r\n",outputBytes[0]);
00208         return 0;
00209     }
00210         
00211     //Erasing flash sectors
00212     commandBytes[0]=52;
00213     commandBytes[1]=20;
00214     commandBytes[2]=26;
00215     commandBytes[3]=96000;
00216     commandBytes[4]=0x0;
00217     IAP_call(commandBytes,outputBytes);    
00218     if(outputBytes[0]!=0x0){
00219         printf("Falha na escrita em flash parte 2 -> codigo [%lu].\r\n",outputBytes[0]);
00220         return 0;
00221     }    
00222     
00223     //Flashing entire chunks
00224     for(chunkIndex=0;chunkIndex<nChunks;chunkIndex++){        
00225         //Load from file to ram
00226         printf("Escrevendo o chunk %lu.\r\n",chunkIndex);
00227         //printf("Conteudo do chunk <");
00228         for(i=0;i<memBufTam;i++){
00229             memBuf[i] = fgetc(arquivo->fp);
00230             //printf("%c",memBuf[i]);
00231         }
00232         //printf(">.\r\n");                
00233                 
00234         //Copy from ram to flash
00235         //Preparing flash sectors to write
00236         commandBytes[0]=50;
00237         commandBytes[1]=20;
00238         commandBytes[2]=26;
00239         commandBytes[3]=96000;
00240         commandBytes[4]=0x0;
00241         IAP_call(commandBytes,outputBytes);    
00242         if(outputBytes[0]!=0x0){
00243             printf("Falha na escrita em flash parte 3 -> codigo [%lu].\r\n",outputBytes[0]);
00244             return 0;
00245         }
00246         
00247         //Flashing this part
00248         commandBytes[0]=51;
00249         commandBytes[1]=((unsigned long) sector_start_adress[flashSectorFirmwareCopyOffset])+(chunkIndex*memBufTam);
00250         commandBytes[2]=(unsigned long)memBuf;
00251         commandBytes[3]=memBufTam;
00252         commandBytes[4]=96000;
00253         IAP_call(commandBytes,outputBytes);
00254         if(outputBytes[0]!=0x0){
00255             printf("Falha na escrita em flash parte 4 -> codigo [%lu].\r\n",outputBytes[0]);
00256             return 0;
00257         }
00258     }
00259     
00260     
00261     //Flashing the last part     
00262     //Preparing flash sectors to write
00263     commandBytes[0]=50;
00264     commandBytes[1]=20;
00265     commandBytes[2]=26;
00266     commandBytes[3]=96000;
00267     commandBytes[4]=0x0;
00268     IAP_call(commandBytes,outputBytes);    
00269     if(outputBytes[0]!=0x0){
00270         printf("Falha na escrita em flash parte 5 -> codigo [%lu].\r\n",outputBytes[0]);
00271         return 0;
00272     }
00273    
00274    if(nEndChunk<256){nEndChunk = 256;}
00275         else if(nEndChunk<512){nEndChunk = 512;}
00276             else if(nEndChunk<1024){nEndChunk = 1024;}
00277                 else {nEndChunk = 4096;}                    
00278    
00279     for(i=0;i<nEndChunk;i++){
00280         memBuf[i] = fgetc(arquivo->fp);    
00281     }
00282     
00283     //Preenchendo o resto com 0xFF
00284     for(i=i;i<memBufTam;i++){
00285         memBuf[i] = 0xFF;
00286     }    
00287     
00288     
00289     
00290     commandBytes[0]=51;
00291     commandBytes[1]=((unsigned long) sector_start_adress[flashSectorFirmwareCopyOffset])+(nChunks*memBufTam);
00292     commandBytes[2]=(unsigned long)memBuf;
00293     commandBytes[3]=nEndChunk;
00294     commandBytes[4]=96000;
00295     IAP_call(commandBytes,outputBytes);
00296     if(outputBytes[0]!=0x0){
00297         printf("Falha na escrita em flash parte 6 -> codigo [%lu].\r\n",outputBytes[0]);
00298         return 0;
00299     }
00300     
00301     printf("Escrevendo tamanho de arquivo em firmDescToFlash = %lu.\r\n",tamFirmware);
00302     
00303     
00304     strcat((char*)&memBufConfigBootLoader[5],"executaBootLoader");
00305     executaBootLoader = 1;
00306     firmDescToFlash();
00307     
00308     sdCard::fechaArquivo(arquivo);
00309     printf("Escrita em swapfile concluido.\r\n");
00310     wait(0.2);
00311     return 0;
00312 }
00313 
00314 void firmDescToRam(){
00315     #define flashSectorFirmwareCopyOffset 20
00316 
00317     uint32_t i;    
00318     //Recuperando a struct firmwareDescriptor
00319     for(i=0;i<40;i++){
00320         memBufConfigBootLoader[i] = sector_start_adress[29][i];
00321     }
00322     
00323     tamFirmware  = memBufConfigBootLoader[0]<<24;
00324     tamFirmware += memBufConfigBootLoader[1]<<16;
00325     tamFirmware += memBufConfigBootLoader[2]<<8;
00326     tamFirmware += memBufConfigBootLoader[3]<<0;
00327     executaBootLoader = memBufConfigBootLoader[4];
00328     
00329     return;
00330 }
00331 
00332 /*
00333 ========================================================================================
00334 ========================================================================================
00335 ========================================================================================
00336 ========================================================================================
00337 ========================================================================================
00338 ========================================================================================
00339 ========================================================================================
00340 ========================================================================================
00341 ========================================================================================
00342 ========================================================================================
00343 ========================================================================================
00344 ========================================================================================
00345 ========================================================================================
00346 ========================================================================================
00347 ========================================================================================
00348 ========================================================================================
00349 ========================================================================================
00350 ========================================================================================
00351 ========================================================================================
00352 ========================================================================================
00353 ========================================================================================
00354 ========================================================================================
00355 */
00356 
00357 /*void bootloader_fillBootLoader(void){
00358     #define flashSectorFirmwareCopyOffset 20    
00359     IAP IAP_call;
00360     IAP_call=(IAP) IAP_LOCATION;    
00361     firmwareDescriptor firmDesc;    
00362     unsigned int commandBytes[5];
00363     unsigned int outputBytes[5]; 
00364     char memBuf[memBufTam];
00365     uint32_t i;
00366     uint32_t chunkIndex=0;
00367     uint32_t nChunks,nEndChunk;
00368     uint32_t firmwareIndexBytes=0;    
00369     char *memRam = firmDesc.nome;
00370     
00371     //Recuperando a struct firmwareDescriptor
00372     for(i=0;i<sizeof(firmwareDescriptor);i++){
00373         memRam[i] = sector_start_adress[29][i];
00374     }
00375     
00376     
00377     printf("Iniciando processo de escrita em flash.\r\nDesligando interrupcoes.\r\n");
00378     NVIC_DeInit();
00379     printf("O firmware em area de swap file tem nome de [%s] e tamanho de %lu bytes.\r\n",firmDesc.nome,firmDesc.tam);
00380     for(i=0;i<memBufTam;i++){
00381         memBuf[i] = sector_start_adress[flashSectorFirmwareCopyOffset][firmwareIndexBytes+i];
00382     }
00383     //printf("Lendo conteudo da swapfile [%s].\r\n",memBuf);
00384     
00385     wait(1);
00386     
00387     
00388     
00389     //Preparing flash sectors to write
00390     commandBytes[0]=50;
00391     commandBytes[1]=0;
00392     commandBytes[2]=19;
00393     commandBytes[3]=96000;
00394     commandBytes[4]=0x0;
00395     IAP_call(commandBytes,outputBytes);    
00396     if(outputBytes[0]!=0x0){
00397         //printf("Falha na escrita em flash parte 1 -> codigo [%lu].\r\n",outputBytes[0]);
00398         //return;
00399     }
00400         
00401     
00402     //Erasing flash sectors
00403     commandBytes[0]=52;
00404     commandBytes[1]=0;
00405     commandBytes[2]=19;
00406     commandBytes[3]=96000;
00407     commandBytes[4]=0x0;
00408     IAP_call(commandBytes,outputBytes);    
00409     if(outputBytes[0]!=0x0){
00410         //printf("Falha na escrita em flash parte 2 -> codigo [%lu].\r\n",outputBytes[0]);
00411         //return;
00412     }    
00413     
00414     
00415     
00416     nChunks   = firmDesc.tam/memBufTam;
00417     nEndChunk = firmDesc.tam%memBufTam;
00418     
00419     //Flashing entire chunks
00420     for(chunkIndex=0;chunkIndex<nChunks;chunkIndex++){        
00421         //Load from file to ram
00422         for(i=0;i<memBufTam;i++){
00423             memBuf[i] = sector_start_adress[flashSectorFirmwareCopyOffset][firmwareIndexBytes+i];
00424         }
00425                 
00426         //Copy from ram to flash
00427         //Preparing flash sectors to write
00428         commandBytes[0]=50;
00429         commandBytes[1]=0;
00430         commandBytes[2]=19;
00431         commandBytes[3]=96000;
00432         commandBytes[4]=0x0;
00433         IAP_call(commandBytes,outputBytes);    
00434         if(outputBytes[0]!=0x0){
00435             //printf("Falha na escrita em flash parte 3 -> codigo [%lu].\r\n",outputBytes[0]);
00436             //return;
00437         }
00438         
00439         //Flashing this part
00440         commandBytes[0]=51;
00441         commandBytes[1]=firmwareIndexBytes;
00442         commandBytes[2]=(unsigned long)memBuf;
00443         commandBytes[3]=memBufTam;
00444         commandBytes[4]=96000;
00445         IAP_call(commandBytes,outputBytes);
00446         if(outputBytes[0]!=0x0){
00447             //printf("Falha na escrita em flash parte 4 -> codigo [%lu].\r\n",outputBytes[0]);
00448             //return;
00449         }
00450         firmwareIndexBytes+=memBufTam;
00451     }
00452     
00453     
00454     //Flashing the last part     
00455     //Preparing flash sectors to write
00456     commandBytes[0]=50;
00457     commandBytes[1]=0;
00458     commandBytes[2]=19;
00459     commandBytes[3]=96000;
00460     commandBytes[4]=0x0;
00461     IAP_call(commandBytes,outputBytes);    
00462     if(outputBytes[0]!=0x0){
00463         //printf("Falha na escrita em flash parte 5 -> codigo [%lu].\r\n",outputBytes[0]);
00464         //return;
00465     }
00466    
00467     for(i=0;i<nEndChunk;i++){
00468         memBuf[i] = sector_start_adress[flashSectorFirmwareCopyOffset][firmwareIndexBytes+i];
00469     }    
00470     
00471     if(nEndChunk<256){nEndChunk = 256;}
00472         else if(nEndChunk<512){nEndChunk = 512;}
00473             else if(nEndChunk<1024){nEndChunk = 1024;}
00474                 else if(nEndChunk<4096){nEndChunk = 4096;}
00475                 
00476     //Preenchendo o resto com 0xFF
00477     for(i=i;i<memBufTam;i++){
00478         memBuf[i] = 0xFF;
00479     }                    
00480     
00481     commandBytes[0]=51;
00482     commandBytes[1]=firmwareIndexBytes;
00483     commandBytes[2]=(unsigned long)memBuf;
00484     commandBytes[3]=nEndChunk;
00485     commandBytes[4]=96000;
00486     IAP_call(commandBytes,outputBytes);
00487     if(outputBytes[0]!=0x0){
00488         //printf("Falha na escrita em flash parte 6 -> codigo [%lu].\r\n",outputBytes[0]);
00489         //return;
00490     }
00491     //printf("Dando boot.\r\n");    
00492     NVIC_SystemReset();
00493     
00494     //Nunca alcançará este ponto.
00495     return;
00496 }*/