Electronic – Getting started with AVR programming concepts

avrbootloaderispspiuart

I am trying to get started with programming the AVR. I have chosen to use Atmega328P (which is in my Arduino Uno). When I did web search regarding the initial steps for learning the AVR programming, I came to know that the microcontroller requires a special program called "bootloader" to initailize the sketch we have created using the Arduino IDE. The bootloader checks if data (program) is present in RX pins. If the data is present, the bootloader grabs the program and places it in the flash memory. This process of loading the program into microcontroller takes place through UART protocol. In other words, the bootloader lets the program to be loaded into the microcontroller. I think I have understood the concept upto now. Any corrections are heartly welcome.

My actual confusion starts when I don't have any bootloaders in my microcontroller. I have my program ready to be loaded into the flash memory.But there is nothing to let the program to be loaded into the microcontroller. There is no bootloader, so there is nothing to let the program into the memory. This is where the programmer comes in action. The programmer takes .hex file from the PC via USB protocol. Then it sends this file to the microcontroller via SPI protocol(Since MISO-MOSI-SS pins are used). My question is:

Q1- Why don't I need bootloader to upload the program via SPI protocol. Why can't I upload the program to the microcontroller via UART protocol when there is no any bootloader present? What lets the microcontroller load program into its memory without bootloader when SPI protocol is used?

I went through various resources available to grab a concept of these things but I often get confused one way or other. I was studying "The Art of Electronics" and I am getting even confused regarding my first question. The book says:

"Contemporary microcontrollers use internal nonvolatile
(flash-memory) storage for program code, which you load (while the μC is in-circuit) by one of several methods. You
usually do the loading55 with a commercial “pod” (officially
called a “device programmer”), which you buy from
the chip manufacturer or third party. If you buy a development
kit (Figure 15.24), it will often include a programming
pod, along with software (for compiling, simulating,
assembling, and loading), and with a circuit board on
which there’s a microcontroller and other hardware (digital
and analog ports, LEDs, a serial port, a programming
header, and perhaps some display device). Here are the several
loading protocols: UART serial-port bootloader, SPI serial-port bootloader, JTAG serial-port bootloader, Proprietary serial-port bootloader, USB serial-port bootloader, Parallel loading."

Is the writer trying to tell me that the program I am uploading via SPI protocol is the bootloader? I can't rule out this possibility because the bootloader is uploaded into the microcontroller by the use of SPI protocol as well. Here's a tutorial from Arduino website.

Next part I have been studying is regarding the process of uploading the program into microcontroller. I have made a simple LED blinking program in Atmel Studio 7 and I have it's hex file ready to be transported to the microcontroller. I am taking this analogy to understand how hex file is uploaded into the microcontroller.

I have considered my hex file to be a product to be delivered to customer (microcontroller) by the seller (PC). The vehicle to be used is a delivery van (AVRDUDE). But the problem is the river between the seller and customer. So I need a bridge to connect them. This bridge is the "programmer". Is my analogy correct? If not, please help me understand what is the propose of AVRDUDE and the 'programmer'.

Best Answer

AVR microcontrollers (and most modern µC) contain special hardware specifically for programming directly using a combination of the SPI port (called ISP for this special case) and the reset pin.

This is a very convenient feature because you don't need to worry about bootloaders or memory offsets or anything else. You can program the chips separately or after they are installed.

To use a different method for programming the flash, you need to use a bootloader that instructs the chip on how to accept this. UART, for example.

Some AVRs can use other methods as well, high voltage programming for instance, that do not use a bootloader.

The choice comes down to what is most convenient/useful for you.

One nice thing about a bootloader, is it potentially allows an easy way to apply field upgrades to your firmware.

As for your analogy:

The hex file is the program you are loading into the flash on the microcontroller. The programmer is the physical hardware that will stream this data. AVRDUDE is the software that runs this process.