teste de publish
Dependencies: DS1820 HighSpeedAnalogIn devices mbed
Diff: bootloader.cpp
- Revision:
- 34:348991c54ff3
- Parent:
- 33:a60abda630f7
diff -r a60abda630f7 -r 348991c54ff3 bootloader.cpp --- a/bootloader.cpp Wed May 30 04:24:55 2018 +0000 +++ b/bootloader.cpp Sat Jun 02 14:48:26 2018 +0000 @@ -2,7 +2,11 @@ #define IAP_LOCATION 0x1fff1ff1 #define memBufTam 1024 bool callBootLoader = false; - +char memBufConfigBootLoader[256]; +char executaBootLoader; +uint32_t tamFirmware; +//firmwareDescriptor firmDesc; +IAP IAP_call; /*****************************************************************************//** * @brief De-initializes the NVIC peripheral registers to their default * reset values. @@ -31,6 +35,8 @@ } } + + /*****************************************************************************//** * @brief De-initializes the SCB peripheral registers to their default * reset values. @@ -76,6 +82,73 @@ LDR PC, [R0, #4] } +void flashPrepare(){ + IAP_call=(IAP) IAP_LOCATION; +} + +void firmDescToFlash(){ + //Guardo tamanho do firmware em bytes na flash no setor 29 + //Preparing flash sectors to write + unsigned int commandBytes[5]; + unsigned int outputBytes[5]; + + //Preparo setores + commandBytes[0]=50; + commandBytes[1]=29; + commandBytes[2]=29; + commandBytes[3]=96000; + commandBytes[4]=0x0; + IAP_call(commandBytes,outputBytes); + if(outputBytes[0]!=0x0){ + printf("Falha na escrita em flash parte 7 -> codigo [%lu].\r\n",outputBytes[0]); + return; + } + + //Erasing flash sectors + commandBytes[0]=52; + commandBytes[1]=29; + commandBytes[2]=29; + commandBytes[3]=96000; + commandBytes[4]=0x0; + IAP_call(commandBytes,outputBytes); + if(outputBytes[0]!=0x0){ + printf("Falha na escrita em flash parte 2 -> codigo [%lu].\r\n",outputBytes[0]); + return; + } + + //Preparo setores + commandBytes[0]=50; + commandBytes[1]=29; + commandBytes[2]=29; + commandBytes[3]=96000; + commandBytes[4]=0x0; + IAP_call(commandBytes,outputBytes); + if(outputBytes[0]!=0x0){ + printf("Falha na escrita em flash parte 7 -> codigo [%lu].\r\n",outputBytes[0]); + return; + } + + memBufConfigBootLoader[0]=(tamFirmware>>24)&0xFF; + memBufConfigBootLoader[1]=(tamFirmware>>16)&0xFF; + memBufConfigBootLoader[2]=(tamFirmware>>8)&0xFF; + memBufConfigBootLoader[3]=(tamFirmware>>0)&0xFF; + memBufConfigBootLoader[4]=executaBootLoader; + + //escrevo nos setores + commandBytes[0]=51; + commandBytes[1]=((unsigned long) sector_start_adress[29]); + commandBytes[2]=(unsigned long) memBufConfigBootLoader; + commandBytes[3]=256; + commandBytes[4]=96000; + + IAP_call(commandBytes,outputBytes); + + if(outputBytes[0]!=0x0){ + printf("Falha na escrita em flash parte 8 -> codigo [%lu].\r\n",outputBytes[0]); + return; + } +} + void bootLoader(void) { /* Change the Vector Table to the USER_FLASH_START @@ -92,25 +165,25 @@ uint8_t bootloader_fillUpFlash(arquivoSD *arquivo){ #define flashSectorFirmwareCopyOffset 20 - IAP IAP_call; - IAP_call=(IAP) IAP_LOCATION; + /*IAP IAP_call; + IAP_call=(IAP) IAP_LOCATION;*/ - firmwareDescriptor firmDesc; + //firmwareDescriptor firmDesc; unsigned int commandBytes[5]; unsigned int outputBytes[5]; - unsigned char memBuf[memBufTam]; - uint32_t tamFirmware=0,i; + uint32_t i; uint32_t chunkIndex=0; - uint32_t nChunks,nEndChunk; + uint32_t nChunks,nEndChunk; + unsigned char memBuf[memBufTam]; sdCard::getFileTam(arquivo); - + tamFirmware = 0; if(!sdCard::abreArquivo(arquivo,"r")){ printf("Arquivo nao encontrado.\r\n"); while(1); } //Excluindo o start no inicio - tamFirmware=arquivo->bytes; + tamFirmware=arquivo->bytes-5; for(i=0;i<5;i++){ memBuf[i] = fgetc(arquivo->fp); //printf("%c",memBuf[i]); @@ -225,34 +298,12 @@ return 0; } - //Guardo tamanho do firmware em bytes na flash no setor 29 - firmDesc.tam = tamFirmware; - strcpy(firmDesc.nome,"Versao tal"); + printf("Escrevendo tamanho de arquivo em firmDescToFlash = %lu.\r\n",tamFirmware); + - //Preparing flash sectors to write - commandBytes[0]=50; - commandBytes[1]=29; - commandBytes[2]=29; - commandBytes[3]=96000; - commandBytes[4]=0x0; - IAP_call(commandBytes,outputBytes); - if(outputBytes[0]!=0x0){ - printf("Falha na escrita em flash parte 7 -> codigo [%lu].\r\n",outputBytes[0]); - return 0; - } - - commandBytes[0]=51; - commandBytes[1]=((unsigned long) sector_start_adress[29]); - commandBytes[2]=(unsigned long) &firmDesc; - commandBytes[3]=256; - commandBytes[4]=96000; - - IAP_call(commandBytes,outputBytes); - - if(outputBytes[0]!=0x0){ - printf("Falha na escrita em flash parte 8 -> codigo [%lu].\r\n",outputBytes[0]); - return 0; - } + strcat((char*)&memBufConfigBootLoader[5],"executaBootLoader"); + executaBootLoader = 1; + firmDescToFlash(); sdCard::fechaArquivo(arquivo); printf("Escrita em swapfile concluido.\r\n"); @@ -260,7 +311,23 @@ return 0; } +void firmDescToRam(){ + #define flashSectorFirmwareCopyOffset 20 + uint32_t i; + //Recuperando a struct firmwareDescriptor + for(i=0;i<40;i++){ + memBufConfigBootLoader[i] = sector_start_adress[29][i]; + } + + tamFirmware = memBufConfigBootLoader[0]<<24; + tamFirmware += memBufConfigBootLoader[1]<<16; + tamFirmware += memBufConfigBootLoader[2]<<8; + tamFirmware += memBufConfigBootLoader[3]<<0; + executaBootLoader = memBufConfigBootLoader[4]; + + return; +} /* ========================================================================================