teste de publish
Dependencies: DS1820 HighSpeedAnalogIn devices mbed
bootloader.cpp@30:8a06a85d8807, 2018-05-25 (annotated)
- Committer:
- brunofgc
- Date:
- Fri May 25 23:46:10 2018 +0000
- Revision:
- 30:8a06a85d8807
- Parent:
- 29:823a9da3696b
- Child:
- 33:a60abda630f7
Concertado ROM e RAM para n?o resetar.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brunofgc | 13:b9183b4bc049 | 1 | #include "bootloader.h" |
brunofgc | 13:b9183b4bc049 | 2 | #define IAP_LOCATION 0x1fff1ff1 |
brunofgc | 13:b9183b4bc049 | 3 | #define memBufTam 1024 |
brunofgc | 13:b9183b4bc049 | 4 | bool callBootLoader = false; |
brunofgc | 13:b9183b4bc049 | 5 | |
brunofgc | 13:b9183b4bc049 | 6 | /*****************************************************************************//** |
brunofgc | 13:b9183b4bc049 | 7 | * @brief De-initializes the NVIC peripheral registers to their default |
brunofgc | 13:b9183b4bc049 | 8 | * reset values. |
brunofgc | 13:b9183b4bc049 | 9 | * @param None |
brunofgc | 13:b9183b4bc049 | 10 | * @return None |
brunofgc | 13:b9183b4bc049 | 11 | * |
brunofgc | 13:b9183b4bc049 | 12 | * These following NVIC peripheral registers will be de-initialized: |
brunofgc | 13:b9183b4bc049 | 13 | * - Disable Interrupt (32 IRQ interrupt sources that matched with LPC17xx) |
brunofgc | 13:b9183b4bc049 | 14 | * - Clear all Pending Interrupts (32 IRQ interrupt source that matched with LPC17xx) |
brunofgc | 13:b9183b4bc049 | 15 | * - Clear all Interrupt Priorities (32 IRQ interrupt source that matched with LPC17xx) |
brunofgc | 13:b9183b4bc049 | 16 | *******************************************************************************/ |
brunofgc | 13:b9183b4bc049 | 17 | void NVIC_DeInit(void) |
brunofgc | 13:b9183b4bc049 | 18 | { |
brunofgc | 13:b9183b4bc049 | 19 | uint8_t tmp; |
brunofgc | 13:b9183b4bc049 | 20 | |
brunofgc | 13:b9183b4bc049 | 21 | /* Disable all interrupts */ |
brunofgc | 13:b9183b4bc049 | 22 | NVIC->ICER[0] = 0xFFFFFFFF; |
brunofgc | 13:b9183b4bc049 | 23 | NVIC->ICER[1] = 0x00000001; |
brunofgc | 13:b9183b4bc049 | 24 | /* Clear all pending interrupts */ |
brunofgc | 13:b9183b4bc049 | 25 | NVIC->ICPR[0] = 0xFFFFFFFF; |
brunofgc | 13:b9183b4bc049 | 26 | NVIC->ICPR[1] = 0x00000001; |
brunofgc | 13:b9183b4bc049 | 27 | |
brunofgc | 13:b9183b4bc049 | 28 | /* Clear all interrupt priority */ |
brunofgc | 13:b9183b4bc049 | 29 | for (tmp = 0; tmp < 32; tmp++) { |
brunofgc | 13:b9183b4bc049 | 30 | NVIC->IP[tmp] = 0x00; |
brunofgc | 13:b9183b4bc049 | 31 | } |
brunofgc | 13:b9183b4bc049 | 32 | } |
brunofgc | 13:b9183b4bc049 | 33 | |
brunofgc | 13:b9183b4bc049 | 34 | /*****************************************************************************//** |
brunofgc | 13:b9183b4bc049 | 35 | * @brief De-initializes the SCB peripheral registers to their default |
brunofgc | 13:b9183b4bc049 | 36 | * reset values. |
brunofgc | 13:b9183b4bc049 | 37 | * @param none |
brunofgc | 13:b9183b4bc049 | 38 | * @return none |
brunofgc | 13:b9183b4bc049 | 39 | * |
brunofgc | 13:b9183b4bc049 | 40 | * These following SCB NVIC peripheral registers will be de-initialized: |
brunofgc | 13:b9183b4bc049 | 41 | * - Interrupt Control State register |
brunofgc | 13:b9183b4bc049 | 42 | * - Interrupt Vector Table Offset register |
brunofgc | 13:b9183b4bc049 | 43 | * - Application Interrupt/Reset Control register |
brunofgc | 13:b9183b4bc049 | 44 | * - System Control register |
brunofgc | 13:b9183b4bc049 | 45 | * - Configuration Control register |
brunofgc | 13:b9183b4bc049 | 46 | * - System Handlers Priority Registers |
brunofgc | 13:b9183b4bc049 | 47 | * - System Handler Control and State Register |
brunofgc | 13:b9183b4bc049 | 48 | * - Configurable Fault Status Register |
brunofgc | 13:b9183b4bc049 | 49 | * - Hard Fault Status Register |
brunofgc | 13:b9183b4bc049 | 50 | * - Debug Fault Status Register |
brunofgc | 13:b9183b4bc049 | 51 | *******************************************************************************/ |
brunofgc | 13:b9183b4bc049 | 52 | void NVIC_SCBDeInit(void) |
brunofgc | 13:b9183b4bc049 | 53 | { |
brunofgc | 13:b9183b4bc049 | 54 | uint8_t tmp; |
brunofgc | 13:b9183b4bc049 | 55 | |
brunofgc | 13:b9183b4bc049 | 56 | SCB->ICSR = 0x0A000000; |
brunofgc | 13:b9183b4bc049 | 57 | SCB->VTOR = 0x00000000; |
brunofgc | 13:b9183b4bc049 | 58 | SCB->AIRCR = 0x05FA0000; |
brunofgc | 13:b9183b4bc049 | 59 | SCB->SCR = 0x00000000; |
brunofgc | 13:b9183b4bc049 | 60 | SCB->CCR = 0x00000000; |
brunofgc | 13:b9183b4bc049 | 61 | |
brunofgc | 13:b9183b4bc049 | 62 | for (tmp = 0; tmp < 32; tmp++) { |
brunofgc | 13:b9183b4bc049 | 63 | SCB->SHP[tmp] = 0x00; |
brunofgc | 13:b9183b4bc049 | 64 | } |
brunofgc | 13:b9183b4bc049 | 65 | |
brunofgc | 13:b9183b4bc049 | 66 | SCB->SHCSR = 0x00000000; |
brunofgc | 13:b9183b4bc049 | 67 | SCB->CFSR = 0xFFFFFFFF; |
brunofgc | 13:b9183b4bc049 | 68 | SCB->HFSR = 0xFFFFFFFF; |
brunofgc | 13:b9183b4bc049 | 69 | SCB->DFSR = 0xFFFFFFFF; |
brunofgc | 13:b9183b4bc049 | 70 | } |
brunofgc | 13:b9183b4bc049 | 71 | |
brunofgc | 13:b9183b4bc049 | 72 | #define APPL_RUN_ADDR 0x70000 |
brunofgc | 13:b9183b4bc049 | 73 | __asm void boot_jump(uint32_t address) |
brunofgc | 13:b9183b4bc049 | 74 | { |
brunofgc | 13:b9183b4bc049 | 75 | LDR SP, [R0] |
brunofgc | 13:b9183b4bc049 | 76 | LDR PC, [R0, #4] |
brunofgc | 13:b9183b4bc049 | 77 | } |
brunofgc | 13:b9183b4bc049 | 78 | |
brunofgc | 13:b9183b4bc049 | 79 | void bootLoader(void) |
brunofgc | 13:b9183b4bc049 | 80 | { |
brunofgc | 13:b9183b4bc049 | 81 | /* Change the Vector Table to the USER_FLASH_START |
brunofgc | 13:b9183b4bc049 | 82 | in case the user application uses interrupts */ |
brunofgc | 30:8a06a85d8807 | 83 | diversos::wdt.kick(90.0); |
brunofgc | 13:b9183b4bc049 | 84 | SysTick->CTRL &= ~0x00000002; // disable SysTick interrupt |
brunofgc | 13:b9183b4bc049 | 85 | //NVIC_DeInit(); |
brunofgc | 13:b9183b4bc049 | 86 | //__disable_irq(); |
brunofgc | 13:b9183b4bc049 | 87 | LPC_SC->PCONP = 0x001817BE; |
brunofgc | 13:b9183b4bc049 | 88 | SCB->VTOR = APPL_RUN_ADDR & 0x1FFFF80; |
brunofgc | 13:b9183b4bc049 | 89 | boot_jump(APPL_RUN_ADDR); |
brunofgc | 13:b9183b4bc049 | 90 | } |
brunofgc | 13:b9183b4bc049 | 91 | |
brunofgc | 13:b9183b4bc049 | 92 | uint8_t bootloader_fillUpFlash(arquivoSD *arquivo){ |
brunofgc | 13:b9183b4bc049 | 93 | #define flashSectorFirmwareCopyOffset 20 |
brunofgc | 13:b9183b4bc049 | 94 | |
brunofgc | 13:b9183b4bc049 | 95 | IAP IAP_call; |
brunofgc | 13:b9183b4bc049 | 96 | IAP_call=(IAP) IAP_LOCATION; |
brunofgc | 13:b9183b4bc049 | 97 | |
brunofgc | 13:b9183b4bc049 | 98 | firmwareDescriptor firmDesc; |
brunofgc | 13:b9183b4bc049 | 99 | unsigned int commandBytes[5]; |
brunofgc | 13:b9183b4bc049 | 100 | unsigned int outputBytes[5]; |
brunofgc | 13:b9183b4bc049 | 101 | unsigned char memBuf[memBufTam]; |
brunofgc | 13:b9183b4bc049 | 102 | uint32_t tamFirmware=0,i; |
brunofgc | 13:b9183b4bc049 | 103 | uint32_t chunkIndex=0; |
brunofgc | 13:b9183b4bc049 | 104 | uint32_t nChunks,nEndChunk; |
brunofgc | 13:b9183b4bc049 | 105 | sdCard::getFileTam(arquivo); |
brunofgc | 13:b9183b4bc049 | 106 | |
brunofgc | 13:b9183b4bc049 | 107 | if(!sdCard::abreArquivo(arquivo,"r")){ |
brunofgc | 13:b9183b4bc049 | 108 | printf("Arquivo nao encontrado.\r\n"); |
brunofgc | 13:b9183b4bc049 | 109 | while(1); |
brunofgc | 13:b9183b4bc049 | 110 | } |
brunofgc | 13:b9183b4bc049 | 111 | |
brunofgc | 13:b9183b4bc049 | 112 | //Excluindo o start no inicio |
brunofgc | 13:b9183b4bc049 | 113 | tamFirmware=arquivo->bytes-5; |
brunofgc | 13:b9183b4bc049 | 114 | for(i=0;i<5;i++){ |
brunofgc | 13:b9183b4bc049 | 115 | memBuf[i] = fgetc(arquivo->fp); |
brunofgc | 13:b9183b4bc049 | 116 | //printf("%c",memBuf[i]); |
brunofgc | 13:b9183b4bc049 | 117 | } |
brunofgc | 13:b9183b4bc049 | 118 | nChunks = tamFirmware/memBufTam; |
brunofgc | 13:b9183b4bc049 | 119 | nEndChunk = tamFirmware%memBufTam; |
brunofgc | 13:b9183b4bc049 | 120 | |
brunofgc | 30:8a06a85d8807 | 121 | printf("Tamanho do arquivo %s = %lu bytes.\r\n",arquivo->nome,tamFirmware); |
brunofgc | 13:b9183b4bc049 | 122 | printf("nChunks = %lu, nEndChunk = %lu.\r\n",nChunks,nEndChunk); |
brunofgc | 13:b9183b4bc049 | 123 | printf("Iniciando processo de escrita em flash.\r\nDesligando interrupcoes.\r\n"); |
brunofgc | 13:b9183b4bc049 | 124 | |
brunofgc | 13:b9183b4bc049 | 125 | |
brunofgc | 13:b9183b4bc049 | 126 | //Preparing flash sectors to write |
brunofgc | 13:b9183b4bc049 | 127 | commandBytes[0]=50; |
brunofgc | 13:b9183b4bc049 | 128 | commandBytes[1]=20; |
brunofgc | 13:b9183b4bc049 | 129 | commandBytes[2]=26; |
brunofgc | 13:b9183b4bc049 | 130 | commandBytes[3]=96000; |
brunofgc | 13:b9183b4bc049 | 131 | commandBytes[4]=0x0; |
brunofgc | 13:b9183b4bc049 | 132 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 133 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 134 | printf("Falha na escrita em flash parte 1 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 135 | return 0; |
brunofgc | 13:b9183b4bc049 | 136 | } |
brunofgc | 13:b9183b4bc049 | 137 | |
brunofgc | 13:b9183b4bc049 | 138 | //Erasing flash sectors |
brunofgc | 13:b9183b4bc049 | 139 | commandBytes[0]=52; |
brunofgc | 13:b9183b4bc049 | 140 | commandBytes[1]=20; |
brunofgc | 13:b9183b4bc049 | 141 | commandBytes[2]=26; |
brunofgc | 13:b9183b4bc049 | 142 | commandBytes[3]=96000; |
brunofgc | 13:b9183b4bc049 | 143 | commandBytes[4]=0x0; |
brunofgc | 13:b9183b4bc049 | 144 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 145 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 146 | printf("Falha na escrita em flash parte 2 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 147 | return 0; |
brunofgc | 13:b9183b4bc049 | 148 | } |
brunofgc | 13:b9183b4bc049 | 149 | |
brunofgc | 13:b9183b4bc049 | 150 | //Flashing entire chunks |
brunofgc | 13:b9183b4bc049 | 151 | for(chunkIndex=0;chunkIndex<nChunks;chunkIndex++){ |
brunofgc | 13:b9183b4bc049 | 152 | //Load from file to ram |
brunofgc | 13:b9183b4bc049 | 153 | printf("Escrevendo o chunk %lu.\r\n",chunkIndex); |
brunofgc | 13:b9183b4bc049 | 154 | //printf("Conteudo do chunk <"); |
brunofgc | 13:b9183b4bc049 | 155 | for(i=0;i<memBufTam;i++){ |
brunofgc | 13:b9183b4bc049 | 156 | memBuf[i] = fgetc(arquivo->fp); |
brunofgc | 13:b9183b4bc049 | 157 | //printf("%c",memBuf[i]); |
brunofgc | 13:b9183b4bc049 | 158 | } |
brunofgc | 13:b9183b4bc049 | 159 | //printf(">.\r\n"); |
brunofgc | 13:b9183b4bc049 | 160 | |
brunofgc | 13:b9183b4bc049 | 161 | //Copy from ram to flash |
brunofgc | 13:b9183b4bc049 | 162 | //Preparing flash sectors to write |
brunofgc | 13:b9183b4bc049 | 163 | commandBytes[0]=50; |
brunofgc | 13:b9183b4bc049 | 164 | commandBytes[1]=20; |
brunofgc | 13:b9183b4bc049 | 165 | commandBytes[2]=26; |
brunofgc | 13:b9183b4bc049 | 166 | commandBytes[3]=96000; |
brunofgc | 13:b9183b4bc049 | 167 | commandBytes[4]=0x0; |
brunofgc | 13:b9183b4bc049 | 168 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 169 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 170 | printf("Falha na escrita em flash parte 3 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 171 | return 0; |
brunofgc | 13:b9183b4bc049 | 172 | } |
brunofgc | 13:b9183b4bc049 | 173 | |
brunofgc | 13:b9183b4bc049 | 174 | //Flashing this part |
brunofgc | 13:b9183b4bc049 | 175 | commandBytes[0]=51; |
brunofgc | 13:b9183b4bc049 | 176 | commandBytes[1]=((unsigned long) sector_start_adress[flashSectorFirmwareCopyOffset])+(chunkIndex*memBufTam); |
brunofgc | 13:b9183b4bc049 | 177 | commandBytes[2]=(unsigned long)memBuf; |
brunofgc | 13:b9183b4bc049 | 178 | commandBytes[3]=memBufTam; |
brunofgc | 13:b9183b4bc049 | 179 | commandBytes[4]=96000; |
brunofgc | 13:b9183b4bc049 | 180 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 181 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 182 | printf("Falha na escrita em flash parte 4 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 183 | return 0; |
brunofgc | 13:b9183b4bc049 | 184 | } |
brunofgc | 13:b9183b4bc049 | 185 | } |
brunofgc | 13:b9183b4bc049 | 186 | |
brunofgc | 13:b9183b4bc049 | 187 | |
brunofgc | 13:b9183b4bc049 | 188 | //Flashing the last part |
brunofgc | 13:b9183b4bc049 | 189 | //Preparing flash sectors to write |
brunofgc | 13:b9183b4bc049 | 190 | commandBytes[0]=50; |
brunofgc | 13:b9183b4bc049 | 191 | commandBytes[1]=20; |
brunofgc | 13:b9183b4bc049 | 192 | commandBytes[2]=26; |
brunofgc | 13:b9183b4bc049 | 193 | commandBytes[3]=96000; |
brunofgc | 13:b9183b4bc049 | 194 | commandBytes[4]=0x0; |
brunofgc | 13:b9183b4bc049 | 195 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 196 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 197 | printf("Falha na escrita em flash parte 5 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 198 | return 0; |
brunofgc | 13:b9183b4bc049 | 199 | } |
brunofgc | 13:b9183b4bc049 | 200 | |
brunofgc | 13:b9183b4bc049 | 201 | if(nEndChunk<256){nEndChunk = 256;} |
brunofgc | 13:b9183b4bc049 | 202 | else if(nEndChunk<512){nEndChunk = 512;} |
brunofgc | 13:b9183b4bc049 | 203 | else if(nEndChunk<1024){nEndChunk = 1024;} |
brunofgc | 13:b9183b4bc049 | 204 | else {nEndChunk = 4096;} |
brunofgc | 13:b9183b4bc049 | 205 | |
brunofgc | 13:b9183b4bc049 | 206 | for(i=0;i<nEndChunk;i++){ |
brunofgc | 13:b9183b4bc049 | 207 | memBuf[i] = fgetc(arquivo->fp); |
brunofgc | 13:b9183b4bc049 | 208 | } |
brunofgc | 13:b9183b4bc049 | 209 | |
brunofgc | 13:b9183b4bc049 | 210 | //Preenchendo o resto com 0xFF |
brunofgc | 13:b9183b4bc049 | 211 | for(i=i;i<memBufTam;i++){ |
brunofgc | 13:b9183b4bc049 | 212 | memBuf[i] = 0xFF; |
brunofgc | 13:b9183b4bc049 | 213 | } |
brunofgc | 13:b9183b4bc049 | 214 | |
brunofgc | 13:b9183b4bc049 | 215 | |
brunofgc | 13:b9183b4bc049 | 216 | |
brunofgc | 13:b9183b4bc049 | 217 | commandBytes[0]=51; |
brunofgc | 13:b9183b4bc049 | 218 | commandBytes[1]=((unsigned long) sector_start_adress[flashSectorFirmwareCopyOffset])+(nChunks*memBufTam); |
brunofgc | 13:b9183b4bc049 | 219 | commandBytes[2]=(unsigned long)memBuf; |
brunofgc | 13:b9183b4bc049 | 220 | commandBytes[3]=nEndChunk; |
brunofgc | 13:b9183b4bc049 | 221 | commandBytes[4]=96000; |
brunofgc | 13:b9183b4bc049 | 222 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 223 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 224 | printf("Falha na escrita em flash parte 6 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 225 | return 0; |
brunofgc | 13:b9183b4bc049 | 226 | } |
brunofgc | 13:b9183b4bc049 | 227 | |
brunofgc | 13:b9183b4bc049 | 228 | //Guardo tamanho do firmware em bytes na flash no setor 29 |
brunofgc | 13:b9183b4bc049 | 229 | firmDesc.tam = tamFirmware; |
brunofgc | 13:b9183b4bc049 | 230 | strcpy(firmDesc.nome,"Versao tal"); |
brunofgc | 13:b9183b4bc049 | 231 | |
brunofgc | 13:b9183b4bc049 | 232 | //Preparing flash sectors to write |
brunofgc | 13:b9183b4bc049 | 233 | commandBytes[0]=50; |
brunofgc | 13:b9183b4bc049 | 234 | commandBytes[1]=29; |
brunofgc | 13:b9183b4bc049 | 235 | commandBytes[2]=29; |
brunofgc | 13:b9183b4bc049 | 236 | commandBytes[3]=96000; |
brunofgc | 13:b9183b4bc049 | 237 | commandBytes[4]=0x0; |
brunofgc | 13:b9183b4bc049 | 238 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 239 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 240 | printf("Falha na escrita em flash parte 7 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 241 | return 0; |
brunofgc | 13:b9183b4bc049 | 242 | } |
brunofgc | 13:b9183b4bc049 | 243 | |
brunofgc | 13:b9183b4bc049 | 244 | commandBytes[0]=51; |
brunofgc | 13:b9183b4bc049 | 245 | commandBytes[1]=((unsigned long) sector_start_adress[29]); |
brunofgc | 13:b9183b4bc049 | 246 | commandBytes[2]=(unsigned long) &firmDesc; |
brunofgc | 13:b9183b4bc049 | 247 | commandBytes[3]=256; |
brunofgc | 13:b9183b4bc049 | 248 | commandBytes[4]=96000; |
brunofgc | 13:b9183b4bc049 | 249 | |
brunofgc | 13:b9183b4bc049 | 250 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 251 | |
brunofgc | 13:b9183b4bc049 | 252 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 253 | printf("Falha na escrita em flash parte 8 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 254 | return 0; |
brunofgc | 13:b9183b4bc049 | 255 | } |
brunofgc | 13:b9183b4bc049 | 256 | |
brunofgc | 13:b9183b4bc049 | 257 | sdCard::fechaArquivo(arquivo); |
brunofgc | 13:b9183b4bc049 | 258 | printf("Escrita em swapfile concluido.\r\n"); |
brunofgc | 13:b9183b4bc049 | 259 | //Nunca alcançará este ponto. |
brunofgc | 13:b9183b4bc049 | 260 | return 0; |
brunofgc | 13:b9183b4bc049 | 261 | } |
brunofgc | 13:b9183b4bc049 | 262 | |
brunofgc | 13:b9183b4bc049 | 263 | |
brunofgc | 13:b9183b4bc049 | 264 | |
brunofgc | 13:b9183b4bc049 | 265 | /* |
brunofgc | 13:b9183b4bc049 | 266 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 267 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 268 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 269 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 270 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 271 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 272 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 273 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 274 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 275 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 276 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 277 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 278 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 279 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 280 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 281 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 282 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 283 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 284 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 285 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 286 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 287 | ======================================================================================== |
brunofgc | 13:b9183b4bc049 | 288 | */ |
brunofgc | 13:b9183b4bc049 | 289 | |
brunofgc | 13:b9183b4bc049 | 290 | /*void bootloader_fillBootLoader(void){ |
brunofgc | 13:b9183b4bc049 | 291 | #define flashSectorFirmwareCopyOffset 20 |
brunofgc | 13:b9183b4bc049 | 292 | IAP IAP_call; |
brunofgc | 13:b9183b4bc049 | 293 | IAP_call=(IAP) IAP_LOCATION; |
brunofgc | 13:b9183b4bc049 | 294 | firmwareDescriptor firmDesc; |
brunofgc | 13:b9183b4bc049 | 295 | unsigned int commandBytes[5]; |
brunofgc | 13:b9183b4bc049 | 296 | unsigned int outputBytes[5]; |
brunofgc | 13:b9183b4bc049 | 297 | char memBuf[memBufTam]; |
brunofgc | 13:b9183b4bc049 | 298 | uint32_t i; |
brunofgc | 13:b9183b4bc049 | 299 | uint32_t chunkIndex=0; |
brunofgc | 13:b9183b4bc049 | 300 | uint32_t nChunks,nEndChunk; |
brunofgc | 13:b9183b4bc049 | 301 | uint32_t firmwareIndexBytes=0; |
brunofgc | 13:b9183b4bc049 | 302 | char *memRam = firmDesc.nome; |
brunofgc | 13:b9183b4bc049 | 303 | |
brunofgc | 13:b9183b4bc049 | 304 | //Recuperando a struct firmwareDescriptor |
brunofgc | 13:b9183b4bc049 | 305 | for(i=0;i<sizeof(firmwareDescriptor);i++){ |
brunofgc | 13:b9183b4bc049 | 306 | memRam[i] = sector_start_adress[29][i]; |
brunofgc | 13:b9183b4bc049 | 307 | } |
brunofgc | 13:b9183b4bc049 | 308 | |
brunofgc | 13:b9183b4bc049 | 309 | |
brunofgc | 13:b9183b4bc049 | 310 | printf("Iniciando processo de escrita em flash.\r\nDesligando interrupcoes.\r\n"); |
brunofgc | 13:b9183b4bc049 | 311 | NVIC_DeInit(); |
brunofgc | 13:b9183b4bc049 | 312 | printf("O firmware em area de swap file tem nome de [%s] e tamanho de %lu bytes.\r\n",firmDesc.nome,firmDesc.tam); |
brunofgc | 13:b9183b4bc049 | 313 | for(i=0;i<memBufTam;i++){ |
brunofgc | 13:b9183b4bc049 | 314 | memBuf[i] = sector_start_adress[flashSectorFirmwareCopyOffset][firmwareIndexBytes+i]; |
brunofgc | 13:b9183b4bc049 | 315 | } |
brunofgc | 13:b9183b4bc049 | 316 | //printf("Lendo conteudo da swapfile [%s].\r\n",memBuf); |
brunofgc | 13:b9183b4bc049 | 317 | |
brunofgc | 13:b9183b4bc049 | 318 | wait(1); |
brunofgc | 13:b9183b4bc049 | 319 | |
brunofgc | 13:b9183b4bc049 | 320 | |
brunofgc | 13:b9183b4bc049 | 321 | |
brunofgc | 13:b9183b4bc049 | 322 | //Preparing flash sectors to write |
brunofgc | 13:b9183b4bc049 | 323 | commandBytes[0]=50; |
brunofgc | 13:b9183b4bc049 | 324 | commandBytes[1]=0; |
brunofgc | 13:b9183b4bc049 | 325 | commandBytes[2]=19; |
brunofgc | 13:b9183b4bc049 | 326 | commandBytes[3]=96000; |
brunofgc | 13:b9183b4bc049 | 327 | commandBytes[4]=0x0; |
brunofgc | 13:b9183b4bc049 | 328 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 329 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 330 | //printf("Falha na escrita em flash parte 1 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 331 | //return; |
brunofgc | 13:b9183b4bc049 | 332 | } |
brunofgc | 13:b9183b4bc049 | 333 | |
brunofgc | 13:b9183b4bc049 | 334 | |
brunofgc | 13:b9183b4bc049 | 335 | //Erasing flash sectors |
brunofgc | 13:b9183b4bc049 | 336 | commandBytes[0]=52; |
brunofgc | 13:b9183b4bc049 | 337 | commandBytes[1]=0; |
brunofgc | 13:b9183b4bc049 | 338 | commandBytes[2]=19; |
brunofgc | 13:b9183b4bc049 | 339 | commandBytes[3]=96000; |
brunofgc | 13:b9183b4bc049 | 340 | commandBytes[4]=0x0; |
brunofgc | 13:b9183b4bc049 | 341 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 342 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 343 | //printf("Falha na escrita em flash parte 2 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 344 | //return; |
brunofgc | 13:b9183b4bc049 | 345 | } |
brunofgc | 13:b9183b4bc049 | 346 | |
brunofgc | 13:b9183b4bc049 | 347 | |
brunofgc | 13:b9183b4bc049 | 348 | |
brunofgc | 13:b9183b4bc049 | 349 | nChunks = firmDesc.tam/memBufTam; |
brunofgc | 13:b9183b4bc049 | 350 | nEndChunk = firmDesc.tam%memBufTam; |
brunofgc | 13:b9183b4bc049 | 351 | |
brunofgc | 13:b9183b4bc049 | 352 | //Flashing entire chunks |
brunofgc | 13:b9183b4bc049 | 353 | for(chunkIndex=0;chunkIndex<nChunks;chunkIndex++){ |
brunofgc | 13:b9183b4bc049 | 354 | //Load from file to ram |
brunofgc | 13:b9183b4bc049 | 355 | for(i=0;i<memBufTam;i++){ |
brunofgc | 13:b9183b4bc049 | 356 | memBuf[i] = sector_start_adress[flashSectorFirmwareCopyOffset][firmwareIndexBytes+i]; |
brunofgc | 13:b9183b4bc049 | 357 | } |
brunofgc | 13:b9183b4bc049 | 358 | |
brunofgc | 13:b9183b4bc049 | 359 | //Copy from ram to flash |
brunofgc | 13:b9183b4bc049 | 360 | //Preparing flash sectors to write |
brunofgc | 13:b9183b4bc049 | 361 | commandBytes[0]=50; |
brunofgc | 13:b9183b4bc049 | 362 | commandBytes[1]=0; |
brunofgc | 13:b9183b4bc049 | 363 | commandBytes[2]=19; |
brunofgc | 13:b9183b4bc049 | 364 | commandBytes[3]=96000; |
brunofgc | 13:b9183b4bc049 | 365 | commandBytes[4]=0x0; |
brunofgc | 13:b9183b4bc049 | 366 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 367 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 368 | //printf("Falha na escrita em flash parte 3 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 369 | //return; |
brunofgc | 13:b9183b4bc049 | 370 | } |
brunofgc | 13:b9183b4bc049 | 371 | |
brunofgc | 13:b9183b4bc049 | 372 | //Flashing this part |
brunofgc | 13:b9183b4bc049 | 373 | commandBytes[0]=51; |
brunofgc | 13:b9183b4bc049 | 374 | commandBytes[1]=firmwareIndexBytes; |
brunofgc | 13:b9183b4bc049 | 375 | commandBytes[2]=(unsigned long)memBuf; |
brunofgc | 13:b9183b4bc049 | 376 | commandBytes[3]=memBufTam; |
brunofgc | 13:b9183b4bc049 | 377 | commandBytes[4]=96000; |
brunofgc | 13:b9183b4bc049 | 378 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 379 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 380 | //printf("Falha na escrita em flash parte 4 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 381 | //return; |
brunofgc | 13:b9183b4bc049 | 382 | } |
brunofgc | 13:b9183b4bc049 | 383 | firmwareIndexBytes+=memBufTam; |
brunofgc | 13:b9183b4bc049 | 384 | } |
brunofgc | 13:b9183b4bc049 | 385 | |
brunofgc | 13:b9183b4bc049 | 386 | |
brunofgc | 13:b9183b4bc049 | 387 | //Flashing the last part |
brunofgc | 13:b9183b4bc049 | 388 | //Preparing flash sectors to write |
brunofgc | 13:b9183b4bc049 | 389 | commandBytes[0]=50; |
brunofgc | 13:b9183b4bc049 | 390 | commandBytes[1]=0; |
brunofgc | 13:b9183b4bc049 | 391 | commandBytes[2]=19; |
brunofgc | 13:b9183b4bc049 | 392 | commandBytes[3]=96000; |
brunofgc | 13:b9183b4bc049 | 393 | commandBytes[4]=0x0; |
brunofgc | 13:b9183b4bc049 | 394 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 395 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 396 | //printf("Falha na escrita em flash parte 5 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 397 | //return; |
brunofgc | 13:b9183b4bc049 | 398 | } |
brunofgc | 13:b9183b4bc049 | 399 | |
brunofgc | 13:b9183b4bc049 | 400 | for(i=0;i<nEndChunk;i++){ |
brunofgc | 13:b9183b4bc049 | 401 | memBuf[i] = sector_start_adress[flashSectorFirmwareCopyOffset][firmwareIndexBytes+i]; |
brunofgc | 13:b9183b4bc049 | 402 | } |
brunofgc | 13:b9183b4bc049 | 403 | |
brunofgc | 13:b9183b4bc049 | 404 | if(nEndChunk<256){nEndChunk = 256;} |
brunofgc | 13:b9183b4bc049 | 405 | else if(nEndChunk<512){nEndChunk = 512;} |
brunofgc | 13:b9183b4bc049 | 406 | else if(nEndChunk<1024){nEndChunk = 1024;} |
brunofgc | 13:b9183b4bc049 | 407 | else if(nEndChunk<4096){nEndChunk = 4096;} |
brunofgc | 13:b9183b4bc049 | 408 | |
brunofgc | 13:b9183b4bc049 | 409 | //Preenchendo o resto com 0xFF |
brunofgc | 13:b9183b4bc049 | 410 | for(i=i;i<memBufTam;i++){ |
brunofgc | 13:b9183b4bc049 | 411 | memBuf[i] = 0xFF; |
brunofgc | 13:b9183b4bc049 | 412 | } |
brunofgc | 13:b9183b4bc049 | 413 | |
brunofgc | 13:b9183b4bc049 | 414 | commandBytes[0]=51; |
brunofgc | 13:b9183b4bc049 | 415 | commandBytes[1]=firmwareIndexBytes; |
brunofgc | 13:b9183b4bc049 | 416 | commandBytes[2]=(unsigned long)memBuf; |
brunofgc | 13:b9183b4bc049 | 417 | commandBytes[3]=nEndChunk; |
brunofgc | 13:b9183b4bc049 | 418 | commandBytes[4]=96000; |
brunofgc | 13:b9183b4bc049 | 419 | IAP_call(commandBytes,outputBytes); |
brunofgc | 13:b9183b4bc049 | 420 | if(outputBytes[0]!=0x0){ |
brunofgc | 13:b9183b4bc049 | 421 | //printf("Falha na escrita em flash parte 6 -> codigo [%lu].\r\n",outputBytes[0]); |
brunofgc | 13:b9183b4bc049 | 422 | //return; |
brunofgc | 13:b9183b4bc049 | 423 | } |
brunofgc | 13:b9183b4bc049 | 424 | //printf("Dando boot.\r\n"); |
brunofgc | 13:b9183b4bc049 | 425 | NVIC_SystemReset(); |
brunofgc | 13:b9183b4bc049 | 426 | |
brunofgc | 13:b9183b4bc049 | 427 | //Nunca alcançará este ponto. |
brunofgc | 13:b9183b4bc049 | 428 | return; |
brunofgc | 13:b9183b4bc049 | 429 | }*/ |