[TUT][SOFT] Program Arduino & AVR Chips via Atmel Studio 7

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

NOTE: This article is being evolved. [Revision 5 - June 15, 2020]

 

Contents

 

  1. Preface
  2. Let's get started!
  3. Appendix

 


 

Preface

 

When you're working on a specific project, you have to update your chip's firmware frequently.

What if you could do that via a simple hotkey on the IDE?

As a Visual Studio based IDP, Atmel Studio 7 lets us Program AVR Chips and development boards like Arduino by pressing a simple hotkey.

But this is not a default feature and you should add it to the platform.

We're going to add some tools to the AS7 via the External Tools Section.

 

Here is a summary of the tools that will eventually be added to the software:

  1. UNO - Program via Bootloader --- [Ctrl+Num 1]
    This tool will upload the current Arduino-Sketch/C-Project on the UNO board.
    Here I'm using an Arduino UNO development board. You can define other Arduino boards too.
  2. ArduinoISP - Upload --- [Ctrl+Num 2]
    This tool will upload the ArduinoISP project on the UNO board. This Project converts the Arduino board to an ICSP Programmer.
  3. ArduinoISP - Program Chip --- [Ctrl+Num 3]
    Following tool #2, this tool will upload your Arduino-Sketch/C-Project on the intended AVR Chip using the Arduino board as an ICSP programmer.
  4. ArduinoISP - Read Chip --- [Ctrl+Num 4]
    Following tool #2, this tool will read the intended AVR Chip's Signature and Fuses using the Arduino board as an ICSP programmer.
  5. ArduinoISP - Program Fuse --- [Ctrl+Num 5]
    Following tool #2, this tool will Program the intended AVR Chip's Fuses using the Arduino board as an ICSP programmer.
    This tool is compatible with AVR Chips having 2 Fuse Bytes.
  6. ArduinoISP - Program Fuse (Extended) --- [Ctrl+Num 6]
    Following tool #2, this tool will Program the intended AVR Chip's Fuses using the Arduino board as an ICSP programmer.
    This tool is compatible with AVR Chips having 3 Fuse Bytes. 
  7. USBasp - Program Chip --- [Ctrl+Num 7]
    This tool will program the current Arduino-Sketch/C-Project on the intended AVR Chip using the USBasp Programmer.
    Here I'm using the USBasp as a low cost and easy to use Serial Programmer. You can use your desired programmer instead. (like stk500, dt006, etc.)
  8. USBasp - Read Chip --- [Ctrl+Num 8]
    This tool will read the intended AVR Chip's Signature and Fuses using the USBasp Programmer.

 

AS7-External-Tools

[My External Tools on AS7]
(Click on the Image to Enlarge it)


 

Let's get started!

 

UNO - Program via Bootloader:

 

To get rid of the Arduino IDE, we should learn how this platform uploads a sketch to the board.

  • Open the Arduino IDE.
  • To Open the Blink Sketch as an example, go to "File >> Examples >> 01.Basics >> Blink".
  • Connect your Arduino (UNO) board to the computer.
  • Upload the sketch.

 

Arduino-IDE-Blink

[Blink Sketch on Arduino IDE]

(Click on the Image to Enlarge it

 

Look at the Log:
This Log is very short. We need a more detailed one.

Sketch uses 924 bytes (2%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.

To add more details to the Log you should Enable the "verbose output during the upload".

  • Go to "File >> Preferences".
  • Go to the "Settings" Tab.
  • Check the "upload" Checkbox of "Show verbose output during".
  • Press the "OK" button.
  • Upload the Sketch again.

 

Arduino-IDE-Preferences

[Arduino IDE's Preferences]

(Click on the Image to Enlarge it)

 

Look at the Log! It can be separated into 3 sections:

  • 1st - The Arduino IDE Log that is the same as the previous one:
Sketch uses 924 bytes (2%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.
  • 2nd - Some Commands that are given by Arduino IDE to an interface Software called AVRdude.
    This kind of code is called "Command Line".
    AVRdude is a program for download/uploading AVR microcontroller flash and EEPROM.
    AVRdude is a command-line tool. Command-line options are used to control AVRdude’s behavior. Read more on [AVRdude's Option Descriptions]
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf
-v -patmega328p -carduino -PCOM3 -b115200 -D -Uflash:w:C:\Users\Mohammad\AppData\Local\Temp\arduino_build_105574/Blink.ino.hex:i
  • 3rd - AVRdude Log:
    Here, We will study important parts of the Log. Click here to read it in full.
...

System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

...

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "C:\Users\Mohammad\AppData\Local\Temp\arduino_build_105574/Blink.ino.hex"
avrdude: writing flash (924 bytes):

Writing | ################################################## | 100% 0.16s

avrdude: 924 bytes of flash written
avrdude: verifying flash memory against C:\Users\Mohammad\AppData\Local\Temp\arduino_build_105574/Blink.ino.hex:
avrdude: load data flash data from input file C:\Users\Mohammad\AppData\Local\Temp\arduino_build_105574/Blink.ino.hex:
avrdude: input file C:\Users\Mohammad\AppData\Local\Temp\arduino_build_105574/Blink.ino.hex contains 924 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.13s

avrdude: verifying ...
avrdude: 924 bytes of flash verified

avrdude done.  Thank you.

Log's Summary:
 

  • AVRdude reads the config file and initializes the AVR chip.
  • AVRdude identifies the chip.
  • AVRdude reads the Hex file and writes it on the flash memory.
  • AVRdude reads on-chip flash data to verify it against the Hex file.
  • The sentence "avrdude: 924 bytes of flash verified" means that the AVR chip is successfully programmed.

Let's look at the 2nd part again:

Here, the IDE is calling the AVRdude Software.

C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude

Here, the Command Lines begin:
Every Command starts with a dash (-) and is specified by a letter.
Note that command lines are case sensitive. For example -C and -c are totally different commands.

-C C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf
-v
-p atmega328p
-c arduino
-P COM3
-b 115200
-D
-U flash:w:C:\Users\Mohammad\AppData\Local\Temp\arduino_build_105574/Blink.ino.hex:i
  • -C <config-file> calls the config file.
    AVRdude.exe needs a config file to run. This file has plenty of information about various AVR Chips and Programmers.
  • -v increases the verbose level.
    It's something like Enabling verbose output in Arduino IDE. But there is a difference! As its definition indicates, you can increase the verbose level by giving more -v commands. For example -v -v -v -v increases the verbose level to level 4. In this case, you will observe a very long Log.
  • -p <partno> determines the name of the chip.
    AVRdude knows Atmega328p as "m328p". Arduino IDE uses "atmega328p" and hopefully, AVRdude understands.
  • -c <programmer> determines the programmer.
    Here we're using the Arduino.
  • -P <port> determines the Serial port. (USB)
    You should determine which Serial port your board is connected to.
    This Setting probably won't change for a specific Arduino board and Computer.
  • -b <baudrate> determines the Baud rate.
  • -D Disables auto erase for the flash before programming.
  • -U <memtype>:r|w|v:<filename>:[format] is used for memory operations. Here we use it to write our Sketch or Project's compiled HEX file on the chip's flash memory.
    "-U" means we're going to perform a memory operation.
    "<memtype>" determines the memory type on which we're going to do an operation. Here we're going to do it on flash.
    The colons act as separators.
    "r|w|v" determines the type of operation. "w" means we're going to write on a memory.
    "<filename>" will be replaced with our hex file's address.
    "[format]" determines the file's format. "i" means "<filename>" is a "hex" file.

 

After giving these commands, AVRdude Dives in...

 

Now that we've learned the process, we can do it via Atmel Studio 7:

 

Let's prepare the necessary ingredients:

  • AVRdude's address in Arduino Folder.
    Note: Don't use my AVRdude's address and Copy it from your own Arduino Log.
    You can copy it from the Arduino Log by selecting the address and pressing Ctrl+C Hotkey.
    It's better to add ".exe" to the end.
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude.exe
  • AVRdude's Commands.
    Note: Don't use my command lines and Copy them from your own Arduino Log.
    You can copy the commands from the Arduino Log by selecting them and pressing Ctrl+C Hotkey.
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf
-v -patmega328p -carduino -PCOM3 -b115200 -D -Uflash:w:C:\Users\Mohammad\AppData\Local\Temp\arduino_build_105574/Blink.ino.hex:i

It's time to add the tool to the AS7:

  • Open an Arduino Sketch (C++) or C Project on AS7.

 

AS7-Blink-Sketch

[Arduino Blink Sketch on AS7]

(Click on the Image to Enlarge it)
 

AS7-Blink-Project

[Blink C Project on AS7]

(Click on the Image to Enlarge it)

 

  • Build the solution --- [F7].
  • Go to "Tools >> External Tools"
  • Since it's the 1st time you're adding a new tool, [New Tool 1] is added by default.
  • Change the Title from [New Tool 1] to "&UNO - Program via Bootloader".
    Note: You should add an "&" to the beginning of the title. This is necessary for adding a Hotkey to this tool.
  • Copy AVRdude's address and paste it in the "Command" input box.
  • Copy AVRdude's Commands and paste them in a new text file (notepad).
  • Replace the -U command with the following -U command:
    The general address lets us use this tool in all Sketchs/Projects.
-U flash:w:"$(ProjectDir)Debug\$(TargetName).hex":i
  • The desired outcome should be something like this:
    Note: all commands must be in one line!

-CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM3 -b115200 -D -Uflash:w:"$(ProjectDir)Debug\$(TargetName).hex":i
  • Copy all the commands and paste them in the "Arguments" input box.
  • Check the "Use output window" checkbox.
  • Press the "OK" button.

 

AS7-External-Tools-Window

[AS7 External Tools Window]

(Click on the Image to Enlarge it)

 

To use the tool, go to "Tools >> UNO - Program via Bootloader".
Then look at the output window of AS7. You should see the same Log as Arduino IDE's.

 

AS7-Output-Window

[AS7 Output Window]

(Click on the Image to Enlarge it)

 

AVRdude's Log on AS7:

AVRdude Log on AS7 will be more detailed. As you can see, AVRdude is trying to read the Fuse bytes. But you will see no results but some zeros!

Why? As we're using Bootloader to program the chip, we're not able to read the Fuse bytes. To read the Fuse bytes you should use a Programmer like ArduinoISP, USBasp, STK500, etc. We will do this on "ArduinoISP - Read Chip" and "USBasp - Read Chip".

...

avrdude.exe: safemode: lfuse reads as 0
avrdude.exe: safemode: hfuse reads as 0
avrdude.exe: safemode: efuse reads as 0

...

avrdude.exe: safemode: lfuse reads as 0
avrdude.exe: safemode: hfuse reads as 0
avrdude.exe: safemode: efuse reads as 0
avrdude.exe: safemode: Fuses OK (E:00, H:00, L:00)

 

Tool's Summary:

*Title:
&UNO - Program via Bootloader

*Command:
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude.exe

*Arguments:
-p atmega328p -c arduino -P COM3 -b 115200 -C "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -D -U flash:w:"$(ProjectDir)Debug\$(TargetName).hex":i -v

NOTE1: Before using this tool, you should install the Arduino IDE.
NOTE2: The address of "avrdude.exe" and "avrdude.conf" may be different on your computer.
NOTE3: Your Port number may be different. To get the COM port number, Connect your Arduino board to the computer and check out Arduino IDE's tools menu.
NOTE4: Change <partno> of -p command if you're using an Arduino board with a different MCU.

 

ArduinoISP - Upload:

 

some text...

 

Tool's Summary:

*Title:
&ArduinoISP - Upload

*Command:
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude.exe

*Arguments:
-p atmega328p -c arduino -P COM3 -b 115200 -C "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -D -U flash:w:"C:\Users\Mohammad\Documents\Atmel Studio\External Tools\Source Files\ArduinoISP (upload)\ArduinoISP.ino.hex":i

NOTE1: Before using this tool, you should install the Arduino IDE.
NOTE2: The address of "avrdude.exe" and "avrdude.conf" may be different on your computer.
NOTE3: Your Port number may be different. To get the COM port number, Connect your Arduino board to the computer and check out Arduino IDE's tools menu.
NOTE4: Change <partno> of -p command if you're using an Arduino board with a different MCU.
NOTE5: Your "ArduinoISP.ino.hex" file's address will certainly be different.

 

ArduinoISP - Program Chip:

 

some text...

 

Tool's Summary:

*Title:
&ArduinoISP - Program Chip

*Command:
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude.exe

*Arguments:
-p atmega328p -c arduino -P COM3 -b 19200 -C "C:\Users\Mohammad\Documents\Atmel Studio\External Tools\Source Files\ArduinoISP (to use)\avrdude-edited.conf" -e -D -U flash:w:"$(ProjectDir)Debug\$(TargetName).hex":i -v

NOTE1: Before using this tool, you should install the Arduino IDE.
NOTE2: The address of "avrdude.exe" and "avrdude.conf" may be different on your computer.
NOTE3: Here, I've used "avrdude-edited.conf" instead of "avrdude.conf". why? Read more on (https://www.avrfreaks.net/comment/2876461#comment-2876461)
NOTE4: Your Port number may be different. To get the COM port number, Connect your Arduino board to the computer and check out Arduino IDE's tools menu.
NOTE5: Change <partno> of -p command if you're going to program a different MCU Chip.
NOTE6: The Baud rate should be matched with the baud rate defined on the ArduinoISP Sketch. Here we used 19200 bits per second.

 

ArduinoISP - Read Chip:

 

some text...

 

Tool's Summary:

*Title:
&ArduinoISP - Read Chip

*Command:
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude.exe

*Arguments:
-p atmega328p -B5 -c arduino -P COM3 -b 19200 -v -C "C:\Users\Mohammad\Documents\Atmel Studio\External Tools\Source Files\ArduinoISP (to use)\avrdude-edited.conf"

NOTE1: Before using this tool, you should install the Arduino IDE.
NOTE2: The address of "avrdude.exe" and "avrdude.conf" may be different on your computer.
NOTE3: Here, I've used "avrdude-edited.conf" instead of "avrdude.conf". why? Read more on (https://www.avrfreaks.net/comment/2876461#comment-2876461)
NOTE4: Your Port number may be different. To get the COM port number, Connect your Arduino board to the computer and check out Arduino IDE's tools menu.
NOTE5: Change <partno> of -p command if you're going to read a different MCU Chip.
NOTE6: The Baud rate should be matched with the baud rate defined on the ArduinoISP Sketch. Here we used 19200 bits per second.
NOTE7: You can omit the -B5 command if your chip is running > 2MHz.

 

ArduinoISP - Program Fuse:

 

some text...

 

Tool's Summary:

*Title:
&ArduinoISP - Program Fuse

*Command:
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude.exe

*Arguments:
-p m8 -U lfuse:w:0xE4:m -U hfuse:w:0xD9:m -c arduino -P COM3 -b 19200 -v -C "C:\Users\Mohammad\Documents\Atmel Studio\External Tools\Source Files\ArduinoISP (to use)\avrdude-edited.conf"

NOTE1: Before using this tool, you should install the Arduino IDE.
NOTE2: The address of "avrdude.exe" and "avrdude.conf" may be different on your computer.
NOTE3: Here, I've used "avrdude-edited.conf" instead of "avrdude.conf". why? Read more on (https://www.avrfreaks.net/comment/2876461#comment-2876461)
NOTE4: Your Port number may be different. To get the COM port number, Connect your Arduino board to the computer and check out Arduino IDE's tools menu.
NOTE5: Change <partno> of -p command if you're going to program a different MCU Chip.
NOTE6: In this example, hfuse has default value.
NOTE7: The default value of CKSEL bits of lfuse is 0001 that provides 1MHz internal RC oscillation. we have set CKSEL bits to 0100 to use 8MHz internal RC oscillation.

 

ArduinoISP - Program Fuse (Extended):

 

some text...

 

Tool's Summary:

*Title:
&ArduinoISP - Program Fuse (Extended)

*Command:
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude.exe

*Arguments:
-p atmega328p -U lfuse:w:0xE2:m -U hfuse:w:0xD9:m -U efuse:w:0xFF:m -c arduino -P COM3 -b 19200 -v -C "C:\Users\Mohammad\Documents\Atmel Studio\External Tools\Source Files\ArduinoISP (to use)\avrdude-edited.conf"

NOTE1: Before using this tool, you should install the Arduino IDE.
NOTE2: The address of "avrdude.exe" and "avrdude.conf" may be different on your computer.
NOTE3: Here, I've used "avrdude-edited.conf" instead of "avrdude.conf". why? Read more on (https://www.avrfreaks.net/comment/2876461#comment-2876461)
NOTE4: Your Port number may be different. To get the COM port number, Connect your Arduino board to the computer and check out Arduino IDE's tools menu.
NOTE5: Change <partno> of -p command if you're going to program a different MCU Chip.
NOTE6: In this example, efuse & hfuse have default values.
NOTE7: All bits of lfuse are default except the CKDIV8 bit that has been diactivated to reach 8MHz internal RC oscillator.

 

USBasp - Program Chip:

 

some text...

 

Tool's Summary:

*Title:
&USBasp - Program Chip

*Command:
C:\Users\Mohammad\Documents\Atmel Studio\External Tools\Source Files\USBasp\avrdude.exe

*Arguments:
-p m8 -c usbasp -P usb -b 19200 -C "C:\Users\Mohammad\Documents\Atmel Studio\External Tools\Source Files\USBasp\avrdude.conf" -e -D -U flash:w:"$(ProjectDir)Debug\$(TargetName).hex":i -v

NOTE1: The address of "avrdude.exe" and "avrdude.conf" may be different on your computer.
NOTE2: If AVRdude was unable to recognize USBasp, use "usbasp-clone" instead of "usbasp" in -c <programmer> command.
NOTE3: As USBasp is not a Virtual Chassis Port (VCP) device, -P <port> is not necessary. You can omit it or leave it like this.
NOTE4: Change <partno> of -p command if you're going to program a different MCU Chip.
NOTE5: Do NOT use Arduino's "avrdude.exe" and config file!
       why? Read more on (https://www.avrfreaks.net/forum/solved-avrdude-error-usbasptransmit-no-such-file-or-directory)
       Download the original "avrdude.exe" and "avrdude.conf" as a zip file on (http://download.savannah.gnu.org/releases/avrdude/).
       "avrdude-6.3-mingw32.zip" is recommended.

 

USBasp - Read Chip:

 

some text...

 

Tool's Summary:

*Title:
&USBasp - Read Chip

*Command:
C:\Users\Mohammad\Documents\Atmel Studio\External Tools\Source Files\USBasp\avrdude.exe

*Arguments:
-p m8 -B5 -c usbasp -P usb -b 19200 -v -C "C:\Users\Mohammad\Documents\Atmel Studio\External Tools\Source Files\USBasp\avrdude.conf"

NOTE1: The address of "avrdude.exe" and "avrdude.conf" may be different on your computer.
NOTE2: If AVRdude was unable to recognize USBasp, use "usbasp-clone" instead of "usbasp" in -c <programmer> command.
NOTE3: As USBasp is not a Virtual Chassis Port (VCP) device, -P <port> is not necessary. You can omit it or leave it like this.
NOTE4: Change <partno> of -p command if you're going to read a different MCU Chip.
NOTE5: You can omit the -B5 command if your chip is running > 2MHz.
NOTE6: Do NOT use Arduino's "avrdude.exe" and config file!
       why? Read more on (https://www.avrfreaks.net/forum/solved-avrdude-error-usbasptransmit-no-such-file-or-directory)
       Download the original "avrdude.exe" and "avrdude.conf" as a zip file on (http://download.savannah.gnu.org/releases/avrdude/).
       "avrdude-6.3-mingw32.zip" is recommended.

 

Appendix:

 

How to add a Hotkey to an External Tool on AS7?

 

some text...

 

How to divide External tools on AS7 into Groups?

 

some text...


 

To Be Continued... 

Attachment(s): 

I'm just trying to learn how to be an Embedded Systems Developer. ¯\_(ツ)_/¯

Last Edited: Mon. Jun 15, 2020 - 06:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Very niceheart

m.S

Electronic engineering and software developer. (My Blog)

 

Check out my serial terminal application.

Bingo Terminal