Boot Loader of ATmega328 of Arduino UNO Board

Being understood that the Arduino is an Open Source Platform, I would like to have a copy of the Source Codes (preferably of C/C++ version) of the Boot Loader of ATmega328 of Arduino UNO Board.

https://www.google.com/search?q=optiboot

First hit. The top half of the first page.

It's included with the Arduino IDE at {Arduino IDE installation folder}/hardware/arduino/avr/bootloaders/optiboot

Thanks to both Coding Badly and pert for directing me towards appropriate path for the Source Codes of the Boot Loader Program of the Arduino UNO. I have got it (optiboot.c).

To my surprise, the codes are not as simple as I expected. One of my objectives of collecting the source codes is to understand the working principle of the Boot Loader Program in respect of how it collects Intel-Hex frames of an application sketch from the Host Computer and fuse them into the application flash area of the target MCU (The ATmega328 of the Arduino Board).

Questions:

  1. How can I compile the optiboot.c program and produce the corresponding Intel-Hex file.

  2. If I fuse (using a Parallel Programmer) the Intel-Hex file of Step-1 into Boot Area of an erased
    ATmega32 and place it into the socket of Arduino UNO, will my Arduino be working? (The fuse bits
    are to be set at: EF = 0x05, HF = 0xDA, LF = 0xFF.)

  3. Is the optiboot_atmega328.hex, at the path
    {Arduino IDE installation folder}/hardware/arduino/avr/bootloaders/optiboot, is the Intel-Hex file
    of Step-1?

You could also look at the datasheet for the processor you are interested in.
The Nov 2016 '328P datasheet has assembly code for a bootloader

;-the routine writes one page of data from RAM to Flash
; the first data location in RAM is pointed to by the Y pointer
; the first data location in Flash is pointed to by the Z-pointer
;-error handling is not included
;-the routine must be placed inside the Boot space
; (at least the Do_spm sub routine). Only code inside NRWW section can
; be read during Self-Programming (Page Erase and Page Write).
;-registers used: r0, r1, temp1 (r16), temp2 (r17), looplo (r24),
; loophi (r25), spmcrval (r20)
; storing and restoring of registers is not included in the routine
; register usage can be optimized at the expense of code size
;-It is assumed that either the interrupt table is moved to the Boot
; loader section or that the interrupts are disabled.
.equ PAGESIZEB = PAGESIZE*2 ;PAGESIZEB is page size in BYTES, not words
.org SMALLBOOTSTART
Write_page:
; Page Erase
ldi spmcrval, (1<<PGERS) | (1<<SPMEN)
call Do_spm
; re-enable the RWW section
ldi spmcrval, (1<<RWWSRE) | (1<<SPMEN)
call Do_spm
; transfer data from RAM to Flash page buffer
ldi looplo, low(PAGESIZEB) ;init loop variable
ldi loophi, high(PAGESIZEB) ;not required for PAGESIZEB<=256
Wrloop:
ld r0, Y+
ld r1, Y+
ldi spmcrval, (1<<SPMEN)
call Do_spm
adiw ZH:ZL, 2
sbiw loophi:looplo, 2 ;use subi for PAGESIZEB<=256
brne Wrloop
; execute Page Write
subi ZL, low(PAGESIZEB) ;restore pointer
sbci ZH, high(PAGESIZEB) ;not required for PAGESIZEB<=256
ldi spmcrval, (1<<PGWRT) | (1<<SPMEN)
call Do_spm
; re-enable the RWW section
ldi spmcrval, (1<<RWWSRE) | (1<<SPMEN)
call Do_spm
; read back and check, optional
ldi looplo, low(PAGESIZEB) ;init loop variable
ldi loophi, high(PAGESIZEB) ;not required for PAGESIZEB<=256
subi YL, low(PAGESIZEB) ;restore pointer
sbci YH, high(PAGESIZEB)
Rdloop:
lpm r0, Z+
ld r1, Y+
cpse r0, r1
jmp Error
sbiw loophi:looplo, 1 ;use subi for PAGESIZEB<=256
brne Rdloop
; return to RWW section
; verify that RWW section is safe to read
Return:
in temp1, SPMCSR
sbrs temp1, RWWSB ; If RWWSB is set, the RWW section is not ready yet
ret
; re-enable the RWW section
ldi spmcrval, (1<<RWWSRE) | (1<<SPMEN)
call Do_spm
rjmp Return
Do_spm:
; check for previous SPM complete
Wait_spm:
in temp1, SPMCSR
sbrc temp1, SPMEN
rjmp Wait_spm
; input: spmcrval determines SPM action
; disable interrupts if enabled, store status
in temp2, SREG
cli
; check that no EEPROM write access is present
Wait_ee:
sbic EECR, EEPE
rjmp Wait_ee
; SPM timed sequence
out SPMCSR, spmcrval
spm
; restore SREG (to enable interrupts if originally enabled)
out SREG, temp2
ret

I think I've seen C code also, maybe in older datasheets. Nope, not finding that.

@CrossRoads

I hope that I would be able to give a final shape to your skeleton assembly codes, convert them into a Flow Chart and then into C codes.

GolamMostafa:
Thanks to both Coding Badly and pert for directing me towards appropriate path for the Source Codes of the Boot Loader Program of the Arduino UNO. I have got it (optiboot.c).

To my surprise, the codes are not as simple as I expected. One of my objectives of collecting the source codes is to understand the working principle of the Boot Loader Program in respect of how it collects Intel-Hex frames of an application sketch from the Host Computer and fuse them into the application flash area of the target MCU (The ATmega328 of the Arduino Board).

Questions:

  1. How can I compile the optiboot.c program and produce the corresponding Intel-Hex file.

  2. If I fuse (using a Parallel Programmer) the Intel-Hex file of Step-1 into Boot Area of an erased
    ATmega32 and place it into the socket of Arduino UNO, will my Arduino be working? (The fuse bits
    are to be set at: EF = 0x05, HF = 0xDA, LF = 0xFF.)

  3. Is the optiboot_atmega328.hex, at the path
    {Arduino IDE installation folder}/hardware/arduino/avr/bootloaders/optiboot, is the Intel-Hex file
    of Step-1?

You won't find the conversion from intel hex frames to flashing process in the optiboot source - it takes STK500. AVRdude parses the hex file and sends STK500 programming commands to the bootloader, which goes from that to flashing commands.

There are bootloaders that take the hex frames though - chipboot45 does, for example. Unfortunately, you have to pay to get access to the chipboot source code to make custom builds or see how the code works (it's targeted for industrial applications, where the fee is no big deal).

  1. Assuming those are the right fuse values (too lazy to check). You can also use an ICSP programmer (arduino as ISP, USBAsp, etc) to do this programming; doesn't need to be parallel.

  2. Yes.

GolamMostafa:
Questions:

  1. How can I compile the optiboot.c program and produce the corresponding Intel-Hex file.

You had asked where the bootloader source of the Uno was so I pointed you to that but if you're going to be delving more deeply into optiboot I recommend you to use the latest source:

There have been some changes made since the version included with the Arduino IDE. The Makefile found at the above link is set up so if you put the source code folder in {Arduino IDE installation folder}/hardware/arduino/avr/bootloaders/ the makefile will be able to find the AVR-GCC tools which are included with the Arduino IDE. The folder structure of the IDE has changed since optiboot was added to the Arduino IDE so the optiboot makefile included with the Arduino IDE does not work correctly but the up to date optiboot makefile has been updated to work with the new folder structure. You also need make, which is no longer included with the Arduino IDE so you will need to install that. If you're using windows you will also need rm. Then to build the bootloader you just need to run:

make atmega328

from the folder that contains the optiboot source.

There are also many other targets supported by optiboot.

  1. How can I compile the optiboot.c program and produce the corresponding Intel-Hex file.

There are significant amounts of documentation (painfully written!) at Home · Optiboot/optiboot Wiki · GitHub
It's gotten a bit more difficult since IDE v1.0.x, because the Arduino IDE download no longer includes all the tools that are needed to do the build. :frowning:
Note that the github repository is several versions newer than the source included with the IDE.

  1. If I fuse (using a Parallel Programmer) the Intel-Hex file of Step-1 into Boot Area of an erased
    ATmega32 and place it into the socket of Arduino UNO, will my Arduino be working? (The fuse bits
    are to be set at: EF = 0x05, HF = 0xDA, LF = 0xFF.)

An ATmega32 will not fit in the socket designed for the ATmega328 on the Uno. If you're going to be installing bootloader, you had better be very exact with everything you type.
A parallel programmer is not required. Unless you break the chip.

  1. Is the optiboot_atmega328.hex, at the path
    {Arduino IDE installation folder}/hardware/arduino/avr/bootloaders/optiboot, is the Intel-Hex file
    of Step-1?

Yes.

the working principle of the Boot Loader Program in respect of how it collects Intel-Hex frames of an application sketch

As DrAzzy mentioned, Optiboot doesn't contain any code that deals with "Intel Hex Format" - that's handled by the avrdude program on the PC side. GitHub - WestfW/OptiLoader: Arduino sketch for burning new bootloaders has code that parses Intel Hex, if you're interested...

An ATmega32 will not fit in the socket designed for the ATmega328 on the Uno

I am so unfortunate and unlucky that my statement (2. If I fuse (using a Parallel Programmer) the Intel-Hex file of Step-1 into Boot Area of an erased ATmega32 and place it into the socket of Arduino UNO, will my Arduino be working?) has not been seen as containing a typographical error (ATmega32 instead ATmega328); rather, it has been seen as something where I have intended to place a 40-pin ATmega32 (2.0" x 0.6") chip in the socket of 28-pin (1.4" x 0.3") ATmega328 chip of Arduino UNO Kit. Sometimes, I think (for many reasons) that I should voluntarily retire from the Forum; however, I can't as the Forum is providing so many information on so many things in so many styles that my co-learners including myself are largely benefited.

Boot Loader Program and My Revised Queries about it.

  1. From the fact that the Arduiono IDE assigns Virtual Serial Communication Port (COMX) to the
    plugged-in USB Cable, I have understood that the IDE must be transmitting (uploading) the
    executable codes (x.bin) of my application program (the Sketch) as Intel-Hex Frames (the
    Transmission File) using RS232 Communication Protocol.

  2. My understanding is that there must be a Communication Controller on the Arduino UNO Board to
    receive these frames, extract the binary codes and fuse them into the Application Flash of the target
    MCU (ATmega328 of Arduino Kit) as per Serial/Parallel Programming Algorithm of ATmega328.

  3. There is no such controller like Step-2 on the Arduino UNO Kit; rather, there is a USB <---->RS232
    Converter Chip (U3, ATmega16U2) on the Arduino Kit, and it communicates with the existing
    ATmega328 over USART Port.

  4. Therefore, there must be a Boot Loader Program within the Boot Section of the existing
    ATmega328, which receives the Intel-Hex Frames from the Host Computer and fuse them inside
    the Application Flash using Self-programming algorithm.

  5. I have simply expressed my desire to understand the working principles of this Boot Loader
    Program in respect of how it handles the incoming Intel-Hex Frames. Or, I don't know, it could be
    that the ATmega16U2 processes the Intel-Hex Frames, and then it sends only the binary codes to
    the Boot Loader which fuses them into the application flash.

  6. So, I thought that a copy of Source Codes (preferably in C) of the Boot Loader Program might help
    me understanding the program logic.

  7. The veteran Forum Members were kind enough to provide me the links for the source code, and I
    got it as optiboot.c.

  8. In response to my inquiry, I have been told that the optiboot_atmega328.hex file has been
    made from the optiboot.c. file. If this is the case, then my query: When the source file
    name is optiboot.c, how it could be that the output file has different name
    optiboot_atmega328.hex?. is it re-named? or, the compilation process takes option from the
    user as to making hex file for what type of MCU.

  9. I have writeen all the above points in honour of my respected respondents who have spent their
    valuable time to satisfy my thirst.

  10. I like to end with a question:

What control byte/command byte (in the form of numerical value) should I send from the Serial
Monitor of the IDE so that the PD7-line of ATmega16U2 (USB <---> RS232) is pulled down
momentarily in order to reset the ATmega328 of the Arduino UNO Kit.

GolamMostafa:

Boot Loader Program and My Revised Queries about it.

  1. From the fact that the Arduiono IDE assigns Virtual Serial Communication Port (COMX) to the
    plugged-in USB Cable, I have understood that the IDE must be transmitting (uploading) the
    executable codes (x.bin) of my application program (the Sketch) as Intel-Hex Frames (the
    Transmission File) using RS232 Communication Protocol.
    You have misunderstood. AVRDude parses the intel hex file, and sends STK500 programming commands over UART serial with TTL voltage levels. There is no RS232 - RS232 means +10/-10v voltage levels, TTL is 5v and 0v. Many people mistakenly refer to TTL serial as RS232; don't be one of those people

  2. My understanding is that there must be a Communication Controller on the Arduino UNO Board to
    receive these frames, extract the binary codes and fuse them into the Application Flash of the target
    MCU (ATmega328 of Arduino Kit) as per Serial/Parallel Programming Algorithm of ATmega328.
    No. The bootloader running on the '328p receives the STK500 commands, and translates those into what's required to write to the flash; this is simpler than processing the intel hex frames

  3. There is no such controller like Step-2 on the Arduino UNO Kit; rather, there is a USB <---->RS232
    Converter Chip (U3, ATmega16U2) on the Arduino Kit, and it communicates with the existing
    ATmega328 over USART Port.
    Huh? Also, see above, it is a USB-serial adapter, it is not RS232

  4. Therefore, there must be a Boot Loader Program within the Boot Section of the existing
    ATmega328, which receives the Intel-Hex Frames from the Host Computer and fuse them inside
    the Application Flash using Self-programming algorithm.
    See 2 above, it receives STK500 commands, not intel hex frames.

  5. I have simply expressed my desire to understand the working principles of this Boot Loader
    Program in respect of how it handles the incoming Intel-Hex Frames. Or, I don't know, it could be
    that the ATmega16U2 processes the Intel-Hex Frames, and then it sends only the binary codes to
    the Boot Loader which fuses them into the application flash.
    AVRDude, running on your PC, processes the intel hex frames and sends them as STK500 commands (which sends the data as binary data, not hex)

  6. So, I thought that a copy of Source Codes (preferably in C) of the Boot Loader Program might help
    me understanding the program logic.
    Studying bootloader source code is an excellent exercise.

  7. The veteran Forum Members were kind enough to provide me the links for the source code, and I
    got it as optiboot.c.

  8. In response to my inquiry, I have been told that the optiboot_atmega328.hex file has been
    made from the optiboot.c. file. If this is the case, then my query: When the source file
    name is optiboot.c, how it could be that the output file has different name
    optiboot_atmega328.hex?. is it re-named? or, the compilation process takes option from the
    user as to making hex file for what type of MCU.
    I think this is done in the makefile; I'm not good enough at reading makefiles to confirm.

  9. I have writeen all the above points in honour of my respected respondents who have spent their
    valuable time to satisfy my thirst.

  10. I like to end with a question:

What control byte/command byte (in the form of numerical value) should I send from the Serial
Monitor of the IDE so that the PD7-line of ATmega16U2 (USB <---> RS232) is pulled down
momentarily in order to reset the ATmega328 of the Arduino UNO Kit.
It's not part of the serial datastream; you tell the serial adapter to assert the DTR pin (this is communication over USB between the PC and the 16u2). Normally when you open a serial connection, DTR goes low and stays there while the connection is open (hence why we have that RC to turn it into a pulse to reset the '328p with)

The serial monitor doesn't give you control over commands related to the modem control pins, but advanced serial clients (like hTerm, what I use) do - I think the 16u2 with the stock f/w only has a DTR pin; most serial adapters have DTR, RTS (active low outputs), and CTS, DCD, DSR, and RI (active low inputs). I've wired up these pins on CH340G's and tied them to pins I want to monitor on the system I'm communicating with - it's pretty neat

@DrAzzy

First of all, many many thanks for taking time to deliver a fantastic, charming, clarifying, educating, and... reply.

  1. (a) Few years ago, I worked on STK500 Kit (Atmel's Learning Kit); just recently, I have learnt about
    AVRDude from web. Because of unfriendly documentation of STK500, I left working with STK500;
    I developed my own ISP Programmer with GUI Interface for ATmega32 (I call it RMCKIT,
    www.krdcbd.com), and I had been spending my play time with it. I had been observing that the
    students of my University had been doing projects on Projcet Day using Arduino Kits; I did
    not put much attention on the Arduino Kit due to my very personal bias on my own Kit which
    suffers from very high manufacturing cost and because of this it could not be made popular
    among the students and other users.

Recently, starting from Spring-2017 semester, the University has changed the syllabus of 3/2
students of EE Dept. from 'Microprocessor Interfacing and System Design' to 'Microcontroller
Microprocessor'. Being a Course Teacher, I have repackaged the Arduino UNO Kit containing
Arduino UNO, Arduino NANO, 8xCC7SDD, 4xSwitch, LCD, LM35 Sensor, and SH13 sensor.
The students have assembled their own kits; they are now having on their palms 'Powerful MCU
Mobile Lab', and they are enjoying boundless liberty to experiment their thoughts and ideas. All
the questions, queries, and technical threats are being directed at me; I have to defend all these
challenges having have information from my own and Forum Members' experiences. The students
are not familiar (and also not ready) with Arduino Forum.

(b) Now, it appears to me that the Arduiono IDE has borrowed many features of the STK500 and
AVRDude. The opportunity for me to learn the structure of Boot Loader Program of Arduiono
has narrowed down. May be, one has to begin designing his own Boot Loader Program to
understand its design topology.

(c) I have posted a revised diagram based on your feedback on UART protocol, UART_RS232 logic,
and UART_TTL logic. In fact, I knew about all these from practical/theoretical works; yet, I quoted
in my post misleading and incorrect information. How could it happen?/Why? I am asking to
myself. Thanks to your sportive criticism which has put me back in the track rather than to
back-gear.

  1. My understanding was based on my experience of designing In-system Programmer where the
    the Intel-Hex frames were parsed locally to retrieve codes for onward fusing into the Application
    Flash. The STK500 and its Uploading Protocol, certainly, reside at a higher level of understanding.

  2. Yes!, the data always travel as bits and not as a hex digit.

  3. A Flow Chart describing the design mechanism of the Boot Loader Program would facilitate my
    study.

  4. (a) In most of the UART communications, we do not use the modem control lines; so, we assert
    them making loop back connections: DTR/(4) -----> DSR/(6)----> CD/(1);
    RTS/(7) -----> CTS/(8); RI(9) remains unconnected.

(b) So, I have no chance to reset the ATmega328 of Arduino UNO sending command from the
Serial Monitor of IDE.

DrAzzy:
I think this is done in the makefile; I'm not good enough at reading makefiles to confirm.

Yes, for example:

atmega328: $(PROGRAM)_atmega328.hex

Or a little more fancy in MCUdude's version:

atmega328p: atmega328p/$(PROGRAM)_atmega328p_$(BAUD_RATE)_$(AVR_FREQ).hex

has not been seen as containing a typographical error (ATmega32 instead ATmega328); rather, it has been seen as something where I have intended to place a 40-pin ATmega32 (2.0" x 0.6") chip in the socket of 28-pin (1.4" x 0.3") ATmega328 chip of Arduino UNO Kit.

Well, optiboot does support the ATmega32 as well as the ATmega328p, and there have been other people on these forums who have ordered wrong chips accidentally, so it was a bit ambiguous...

"STK500" can mean one of several things. The STK500 board was an early Atmel development board that included chip programming capabilities.

It also refers to the communications protocol that Atmel implemented on that board for communications with a PC (described in this app note: Application Notes | Microchip Technology)

If you'd looked more carefully at the documentation I'd linked back in reply#8, you'd have known that. :frowning:
In particular, How Optiboot Works

and there have been other people on these forums who have ordered wrong chips accidentally, so it was a bit ambiguous...

Thanks for the clarification of the statement; it has really healed my burn.

You could also look at how Nick Gammon's code tha reads & extracts data from .hex files for loading a sketch into a chip.

I run his code on my board in this '328P based standalone programmer I offer. The '328P reads the .hex file contents and loads them in the target microprocessor over the SPI pins.
http://www.crossroadsfencing.com/BobuinoRev17/

Does this help any?

Does this help any?

It is a lovely Flow Chart. I have a very special fascination on Flow Chart as it always helps me (perhaps many others) to represent problem solving steps of a complex process in a pictorial way using few pre-defined geometrical figures. A Flow Chart allows viewing at a glance the totality of the solution of a problem. The developer is relaxed seeing the solution, he was painfully carrying in his head, is just on the Table by virtue of the Flow Chart. I, for the first time in 1979, saw Flow Chart of a 'Urea Production Process' of a Fertiliser Plant in Bangladesh being built by Foster Wheeler (UK).

Anyway, I hope that I will give a try to convert your Flow Chart into workable codes (perhaps in ASM first and then in Arduino C) and then test them using RMCKIT/AVR Studio 4 and Arduino.

I am posting below a Flow Chart which parses Intel-hex frames, coming from a Host Computer, and stores the code/data bytes into Flash(Code Memory)/EEPROM (Data Memory) of a target ATmega32/8 using Serial Programming Algorithm.

(How could be that the image you have posted has a very good resolution than my one. I make diagram using Visio, and then paste it into Paint and then save as *.png file.)

GolamMostafa:
(How could be that the image you have posted has a very good resolution than my one. I make diagram using Visio, and then paste it into Paint and then save as *.png file.)

If you mean why is it so small and hard to read, the forum scales the image down to a maximum width. If you right click on it and then select View Image you can see it in full size. The smaller text on the right does look a bit blurry though.