2
\$\begingroup\$

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'.

\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
  • 3
    \$\begingroup\$ Note that most modern microcontrollers have similar schemes -- this is not unique to Atmel or the AVR. \$\endgroup\$ – TimWescott Jan 7 at 16:50
  • \$\begingroup\$ Thank you @TimWescott, I will update my answer with this valuable tidbit. \$\endgroup\$ – evildemonic Jan 7 at 16:51
  • \$\begingroup\$ Is it due to that "special hardware" I can't use UART protocol to upload the programs into 'microcontroller without bootloader' ? And I think I am correct with my analogy. Am I ? \$\endgroup\$ – Giga-Byte Jan 7 at 16:54
  • \$\begingroup\$ The special hardware isn't preventing you from using the UART, it is just that without the bootloader, the chip doesn't know how to accept programming via that method. And yes, your analogy sounds correct. \$\endgroup\$ – evildemonic Jan 7 at 16:56
  • 1
    \$\begingroup\$ And bootloaders are everywhere -- for all of the product lines I've worked on that had their own internal communications net, I think 80% ended up with a bootloader unique to that internal comms protocol. \$\endgroup\$ – TimWescott Jan 7 at 19:00
1
\$\begingroup\$

A bootloader is not the only way to program a microcontroller. In fact, you might need to use some other method to even put the bootloader program on to the microcontroller before you can use serial bootloading techniques. The ATMega you use is one such chip.

The Arduino family relies on a bootloader, but in the Arduino programming environment, you'll find an option to use a hardware programmer to load the bootloader on to a new chip.

The hardware programmers will run you about $10USD

\$\endgroup\$
  • \$\begingroup\$ And the programmer uses USB to SPI conversion. Am I right? Also, I found a way to use Arduino as the programmer. Since I already have Uno board, I don't want to buy an extra programmer. Is there any sort of complication I may face when using Arduino as programmer? \$\endgroup\$ – Giga-Byte Jan 7 at 17:02
  • \$\begingroup\$ I don't know the protocol the programmer uses. It will vary by chip family. \$\endgroup\$ – Scott Seidman Jan 7 at 17:08
  • \$\begingroup\$ The programmer contains MISO, MOSI, SS (Chip Select) pins. As far as I konw, SPI protocol uses these pins. Not sure if there are any other protocols. Let's wait for someone else to add to the answers. \$\endgroup\$ – Giga-Byte Jan 7 at 17:10
  • \$\begingroup\$ As far as I am aware (on AVR) the SPI/ISP programmers do not use the SS pin. They use MISO, MOSI, SCK, and RESET. \$\endgroup\$ – evildemonic Jan 7 at 17:21
  • \$\begingroup\$ Yeah. I made a mistake. Thanks for the correction @evildemonic \$\endgroup\$ – Giga-Byte Jan 7 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.