Arduino Bootloader

Ever wondered how the code written in the IDE is actually executed ? Do you know where is the code stored in the Arduino and how does it get updated ?
ATmega328P is the chip usually used in Arduino uno and nano. When the sketch is loaded it is actually loaded inside the micro-controller chip. This chip then runs the sketch once the Arduino is powered up.

Almost all the micro-controllers use some form of a bootloader. When a micro-controller turns on,it runs a specific thing at a particular location (mostly 0x0000). This location usually contain a jump instruction to another place in the memory which is the start of the user program. The bootloader ,however, exits at a different memory location.

On a power-up or reset, a bootloader is a section of memory that runs before the main code runs. It is used to setup the micro-controller or provide ability to update main program's code.

The Arduino Bootloader supports re-programming the flash memory over serial. Like other micro-controller, the ATmega328p dedicates a part of its memory (0.5 KB of 32 KB) for bootloading the code. The Arduino bootloader waits for a short time while watching the USART pins. If the bootloader receives a unique sequence over the serial port, then the byte stream is programmed into the flash. Or else the bootloader turns the control over to the existing code.


Arduino Uno uses the Optiboot bootloader. It is actually a piece of code that allows us to program the flash memory of ATmega328 via serial or USB instead of using a ICSP programmer. These bootloaders implement a subset of  STK500  8-bit protocol. Once the Arduino IDE compiles everything down to intel hex file, it uses its avrdude command to program the Arduino. It uses the avrdude's STK500 protocol as the upload protocol.

When the verify button is pressed, the user's code in the Arduino IDE gets converted into an avr-g++ file. Once the file is compiled it gets converted into a hex file. When the upload button is pressed, the avrdude reads ATmega328 for a device signature. Then the avrdude writes the contents of the file to the flash of the micro-controller.

A Hex file
Working of a bootloader : When we press upload button, the reset on the reset pin is triggered. This causes the Data Transit Ready (DTR) of USB to go low. The Arduino board has a capacitor which causes the reset pin of ATmega328 to go low momentarily and go high again. So Arduino resets each time its serial port is opened.

Upon reset, Arduino enters bootloader. As it receives the valid commands, it will start accepting new Arduino code in Hex format and erase the existing code to load a new one. If the valid commands are not received, it times out after one second and triggers the Watch Dog Trigger (WDT) reset.

Once the bootloader runs again it will look at the source that caused reset. Once it is determined that it was WDT reset, it immediately jumps to the first line of the code stored in it. This way, when the Arduino is powered up it directly runs the code without waiting for the one second time out.

Comments

Popular posts from this blog

The move_base ROS node

Three Wheeled Omnidirectional Robot : Motion Analysis

Overview of ATmega328P