@@ -303,23 +303,27 @@ byte flashFile(SdFile* file, programmer::BBProgrammer* bbprogrammer)
303303
304304 Debug (DEBUG_DEBUG, F (" [FLASHED]" ));
305305
306- // Check bootloader positions *(1/2/4/8)
307- for ( int i = 1 ; i <= 8 ; i *= 2 )
306+ // Check for bootloader if target has bootloader support
307+ if (signature-> fuseWithBootloaderSize != NO_FUSE )
308308 {
309- // Possible bootlaoder position in current page?
310- unsigned long bootloaderAddress = signature->flashSize - signature->baseBootSize * i;
311- if (bootloaderAddress < pageaddr || bootloaderAddress > pageaddr + signature->pageSize - 1 )
312- continue ; // If not skip this position
313-
314- unsigned long bootloaderPageAddress = bootloaderAddress - pageaddr;
315-
316- // Check if bootloader position caintains instruction
317- if (page[bootloaderPageAddress] != 0xFF )
309+ // Check bootloader positions *(1/2/4/8)
310+ for (int i = 1 ; i <= 8 ; i *= 2 )
318311 {
319- Debug (DEBUG_DEBUG, F (" Found bootloader at 0x" ));
320- Debug (DEBUG_DEBUG, signature->flashSize - signature->baseBootSize , HEX);
321- if (bootloaderAddress < lowestBootLoader)
322- lowestBootLoader = bootloaderAddress;
312+ // Possible bootlaoder position in current page?
313+ unsigned long bootloaderAddress = signature->flashSize - signature->baseBootSize * i;
314+ if (bootloaderAddress < pageaddr || bootloaderAddress > pageaddr + signature->pageSize - 1 )
315+ continue ; // If not skip this position
316+
317+ unsigned long bootloaderPageAddress = bootloaderAddress - pageaddr;
318+
319+ // Check if bootloader position caintains instruction
320+ if (page[bootloaderPageAddress] != 0xFF )
321+ {
322+ Debug (DEBUG_DEBUG, F (" Found bootloader at 0x" ));
323+ Debug (DEBUG_DEBUG, signature->flashSize - signature->baseBootSize , HEX);
324+ if (bootloaderAddress < lowestBootLoader)
325+ lowestBootLoader = bootloaderAddress;
326+ }
323327 }
324328 }
325329 Debugln (DEBUG_DEBUG);
@@ -340,47 +344,56 @@ byte flashFile(SdFile* file, programmer::BBProgrammer* bbprogrammer)
340344 // Flashing done. Show user feedback
341345 Debugln (DEBUG_INFO, F (" Flashing complete." ));
342346
343- // Change bootloader fuse
344- if (signature->fuseWithBootloaderSize != highFuse)
345- {
346- // Bootloader setting not supported for current chip
347- bbprogrammer->stopProgramming ();
348- return error_bootloaderSupport;
349- }
350-
351- // Bootloader fuse:
352- // http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf page 243 and 239
353- byte newBootloaderFuse = 0x0 ;
354- if (lowestBootLoader == signature->flashSize )
347+ // If current chip supports bootloader, update corresponding fuse
348+ if (signature->fuseWithBootloaderSize == NO_FUSE)
355349 {
356- Debugln (DEBUG_INFO, F ( " No bootlaoder detected. " ));
357- newBootloaderFuse = (fuse. high & 0b11111000 ) | 0b001 ; // Application reset at 0x0000
350+ // Debug output, that we'll not look for a bootloader for this chip.
351+ Debugln (DEBUG_DEBUG, F ( " No bootloader capability for target. " ));
358352 }
359353 else
360354 {
361- Debug (DEBUG_INFO, F (" Bootloader detected at 0x" ));
362- Debugln (DEBUG_INFO, lowestBootLoader, HEX);
363-
364- // bootloader fuse depending on bootloader location
365- if (lowestBootLoader == (signature->flashSize - signature->baseBootSize ))
366- newBootloaderFuse = (fuse.high & 0b11111000 ) | 0b110 ;
367- else if (lowestBootLoader == (signature->flashSize - signature->baseBootSize * 2 ))
368- newBootloaderFuse = (fuse.high & 0b11111000 ) | 0b100 ;
369- else if (lowestBootLoader == (signature->flashSize - signature->baseBootSize * 4 ))
370- newBootloaderFuse = (fuse.high & 0b11111000 ) | 0b010 ;
371- else if (lowestBootLoader == (signature->flashSize - signature->baseBootSize * 8 ))
372- newBootloaderFuse = (fuse.high & 0b11111000 ) | 0b000 ;
373- }
374- Debug (DEBUG_INFO, F (" Changing high fuse: " ));
375- Debug (DEBUG_INFO, fuse.high , BIN);
376- Debug (DEBUG_INFO, F (" -> " ));
377- Debug (DEBUG_INFO, newBootloaderFuse, BIN);
378- Debug (DEBUG_INFO, F (" ..." ));
379-
380- // Write new fuse
381- bbprogrammer->setHighFuse (newBootloaderFuse);
382- Debugln (DEBUG_INFO, F (" [OK]" ));
355+ // Change bootloader fuse
356+ if (signature->fuseWithBootloaderSize != highFuse)
357+ {
358+ // Bootloader setting not supported for current chip
359+ bbprogrammer->stopProgramming ();
360+ return error_bootloaderSupport;
361+ }
383362
363+ // Bootloader fuse:
364+ // http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf page 243 and 239
365+ byte newBootloaderFuse = 0x0 ;
366+ if (lowestBootLoader == signature->flashSize )
367+ {
368+ Debugln (DEBUG_INFO, F (" No bootlaoder detected." ));
369+ newBootloaderFuse = (fuse.high & 0b11111000 ) | 0b001 ; // Application reset at 0x0000
370+ }
371+ else
372+ {
373+ Debug (DEBUG_INFO, F (" Bootloader detected at 0x" ));
374+ Debugln (DEBUG_INFO, lowestBootLoader, HEX);
375+
376+ // bootloader fuse depending on bootloader location
377+ if (lowestBootLoader == (signature->flashSize - signature->baseBootSize ))
378+ newBootloaderFuse = (fuse.high & 0b11111000 ) | 0b110 ;
379+ else if (lowestBootLoader == (signature->flashSize - signature->baseBootSize * 2 ))
380+ newBootloaderFuse = (fuse.high & 0b11111000 ) | 0b100 ;
381+ else if (lowestBootLoader == (signature->flashSize - signature->baseBootSize * 4 ))
382+ newBootloaderFuse = (fuse.high & 0b11111000 ) | 0b010 ;
383+ else if (lowestBootLoader == (signature->flashSize - signature->baseBootSize * 8 ))
384+ newBootloaderFuse = (fuse.high & 0b11111000 ) | 0b000 ;
385+ }
386+ Debug (DEBUG_INFO, F (" Changing high fuse: " ));
387+ Debug (DEBUG_INFO, fuse.high , BIN);
388+ Debug (DEBUG_INFO, F (" -> " ));
389+ Debug (DEBUG_INFO, newBootloaderFuse, BIN);
390+ Debug (DEBUG_INFO, F (" ..." ));
391+
392+ // Write new fuse
393+ bbprogrammer->setHighFuse (newBootloaderFuse);
394+ Debugln (DEBUG_INFO, F (" [OK]" ));
395+ }
396+
384397 // Change low fuse for external clock
385398 Debug (DEBUG_INFO, F (" Changing low fuse: " ));
386399 Debug (DEBUG_INFO, fuse.low , BIN);
0 commit comments