Help: Add serial flashing to SD bootloader.

I'm looking to merge the serial flashing part of Optiboot in to another Bootloader I have forked on Github that loads a .BIN file from an attached SD card. the SD boot loader works flawlessly. there is quite a bit of room still left to implement Serial flashing of the program space. I am kind of new to this and have had a hard time finding good reading material on how Arduino communicates with the bootloader upon reset. What I'm looking for is the isolation of the Optiboot code that does all this so that I can easily port it over. I have tried looking at the code but find myself getting lost. Can anyone help me figure this out? I'm sure once I dig out the basics I can work it out. I know Optiboot simulates the STK500 Protocol.

The idea is simple. The current bootloader checks the SD card attached to the SPI bus and if a file named FIRMWARE.BIN is located in the root directory it flashes the program space instantly and runs it. simple.
I understand that Optiboot will wait an amount of time for a serial host to communicate under the stk500 protocol and then do the same.
I want the current bootloader to check for serial host first. then after the time has expired without success, check the SD directory for the FIRMWARE.BIN file. If there WAS a successful connection and flash of the program space, skip the SD directory check and just run the program code. Should be simple but I'm not versed in boot loaders and the source code is ambiguous to me. I can do the heavy lifting if someone can do the "fine print" reading for me.

Here are the Git repositories for both bootloaders.

Any suggestions or help is very much so appreciated ahead of time. Even if its criticism to tell me I'm an idiot. :slight_smile:

Well thanks for all your help arduino forum. We have instead attempted to merge some of the code from 2boots in to the repository and now have the daunting task of rewriting the board definitions and adding 1284p support to the serial loader portion.

Still open to comments and suggestions.

Have you looked at Nick Gammon's bootloader sketches here?

Maybe they're not applicable because it is one arduino bootloading another. It does cover a lot of chips. Especially the hex uploader from SD card sketch.

Thanks, Yeah i have looked over it briefly, Its not entirely applicable. I think I'm on the right track, I need some help writing UART routines in to the uart/uart.c file to add receive routines on top of the already provided transmit routines.

The UART debugging that's existing is supposed to work on the majority of chips already so besides adding the receive pin defines for each board and pulling them high to prevent noise etc. there shouldn't be a hole lot of work to do. I have the file stk500v1.c and stk500v1.c.OLD in the git. the .OLD is the reference of what was existing, I stripped out the UART stuff that was existing in favor of using the debugging UART baud settings. the old file had functions for getCH and putCH , need to find something similar in the uart.c file or write new routines to fill that gap. the old definitions and UART stuff was a mess and didn't seem portable. I'll keep playing with it, but any help is appreciated if anyone knows anything about serial communication routines and the stk500v1 protocol.

Also, looking for tips on optimizing the code to save some flash space. trying to reduce the bloat so it'll fit on smaller chips.

SargentSeven:
I'm looking to merge the serial flashing part of Optiboot in to another Bootloader I have forked on Github that loads a .BIN file from an attached SD card. the SD boot loader works flawlessly.

hey can you help me out with the steps you followed
i can't seemed to get it to work . i have never worked with bootloaders and when i try to burn the booloader after installing the avr_boot i get the error

Arduino: 1.8.12 (Windows 10), Board: "ATmega328/P, ATmega328P, Uno, 16MHz External Low Power, 4, 2.7V"

C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Users\Ramsha\AppData\Local\Arduino15\packages\avr_boot\hardware\avr\1.3.0/avrdude.conf -v -patmega328p -cstk500v1 -PCOM3 -b19200 -e -Ulock:w:0x3F:m -Uefuse:w:0x05:m -Uhfuse:w:0xD8:m -Ulfuse:w:0xFF:m

avrdude: Version 6.3-20190619
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

System wide configuration file is "C:\Users\Ramsha\AppData\Local\Arduino15\packages\avr_boot\hardware\avr\1.3.0/avrdude.conf"

Using Port : COM3
Using Programmer : stk500v1
Overriding Baud Rate : 19200
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x03

avrdude done. Thank you.

Error while burning bootloader.