{"id":1816,"date":"2016-04-01T21:27:50","date_gmt":"2016-04-02T04:27:50","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=1816"},"modified":"2017-05-26T10:33:13","modified_gmt":"2017-05-26T17:33:13","slug":"developing-firmware-for-avr-devices-with-visual-studio","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/avr\/","title":{"rendered":"Developing firmware for AVR devices with Visual Studio"},"content":{"rendered":"<p>This tutorial shows how to develop and debug firmware for AVR devices with Visual Studio using VisualGDB.<\/p>\n<p>Before you begin, install <a href=\"http:\/\/visualgdb.com\/download\/\">VisualGDB<\/a>.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Embedded Project Wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/01-prj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1817\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/01-prj.png\" alt=\"01-prj\" width=\"786\" height=\"557\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/01-prj.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/01-prj-300x213.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Proceed with the default &#8220;Create new project -&gt; Embedded Binary&#8221; setting. If you want VisualGDB to create a .hex file additionally to the .bin and .elf files, check the &#8220;ihex&#8221; checkbox:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/02-binhex.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1818\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/02-binhex.png\" alt=\"02-binhex\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/02-binhex.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/02-binhex-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the next page select the AVR toolchain. If it is not installed, VisualGDB will offer installing it automatically:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/03-avr.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1819\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/03-avr.png\" alt=\"03-avr\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/03-avr.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/03-avr-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a>You can also download the toolchain manually from the <a href=\"http:\/\/gnutoolchains.com\/avr\/\">gnutoolchains.com<\/a> site.<\/li>\n<li>Simply specify a directory where you want to install the toolchain and click &#8220;Install&#8221; to begin installation:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/04-getavr.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1820\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/04-getavr.png\" alt=\"04-getavr\" width=\"562\" height=\"157\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/04-getavr.png 562w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/04-getavr-300x84.png 300w\" sizes=\"(max-width: 562px) 100vw, 562px\" \/><\/a><\/li>\n<li>Once the toolchain is installed, select the device you are targeting from the list. We will demonstrate debugging based on the MEGA-1284P Xplained board, so we select ATMEGA1284p in this step:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/05-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1821\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/05-device.png\" alt=\"05-device\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/05-device.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/05-device-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the Sample Selection page enter select the port and the pin of the LED that you want to blink. The MEGA-1284P Xplained board as 4 LEDs connected to ports B0, B1, B2 and B3, so we select PORTB0:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/06-sample.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1822\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/06-sample.png\" alt=\"06-sample\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/06-sample.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/06-sample-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>When you open the Debug Method page, connect your debug adapter to the USB port and click &#8220;Detect&#8221; to auto-detect it. VisualGDB supports AVR debugging using the <a href=\"http:\/\/avarice.sourceforge.net\/\">AVaRICE <\/a>tool that works with the JTAG ICE Mk I, JTAG ICE Mk II and AVR Dragon devices. VisualGDB can automatically detect the JTAG ICE Mk II and AVR Dragon and setup the drivers for them:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/07-detect.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1823\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/07-detect.png\" alt=\"07-detect\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/07-detect.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/07-detect-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a>Note that debugging AVR devices with VisualGDB\/AVaRICE requires a JTAG connection.<\/li>\n<li>Ensure both debug adapter and the programmed device are powered and connected to each other using the connectors shown below (the arrow shows the location of pin #1 on both connectors):<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/conn.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1830\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/conn.jpg\" alt=\"conn\" width=\"700\" height=\"544\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/conn.jpg 700w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/conn-300x233.jpg 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/li>\n<li>Then press &#8220;Test AVaRICE settings&#8221; button to check that the tool can communicate to your device. VisualGDB will attempt to read the lock bits of the device and if it fails, the error message should provide more details:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/08-test.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1824\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/08-test.png\" alt=\"08-test\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/08-test.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/08-test-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a>It is recommended to set the Debug Bitrate value to 1\/4 of your device frequency, as the default 250K setting will result in very slow debugging. However, setting the value too high would make the debugging impossible and could cause strange bugs.<\/li>\n<li>Press &#8220;Finish&#8221; to generate your project. Build it by pressing Ctrl-Shift-B:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/09-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1825\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/09-build.png\" alt=\"09-build\" width=\"925\" height=\"678\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/09-build.png 925w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/09-build-300x220.png 300w\" sizes=\"(max-width: 925px) 100vw, 925px\" \/><\/a><\/li>\n<li>Press F5 to start debugging. Observe how the yellow LED in the upper left corner of the board starts to blink:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/board.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1829\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/board.jpg\" alt=\"board\" width=\"700\" height=\"496\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/board.jpg 700w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/board-300x213.jpg 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/li>\n<li>Set a breakpoint at the line that modifies the PORTB register and wait for it to trigger. You can step over the assignment statement to see how the LED turns on or off:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/10-breakpoint.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1826\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/10-breakpoint.png\" alt=\"10-breakpoint\" width=\"925\" height=\"678\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/10-breakpoint.png 925w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/10-breakpoint-300x220.png 300w\" sizes=\"(max-width: 925px) 100vw, 925px\" \/><\/a><\/li>\n<li>You can use the Hardware Registers window to view and quickly change the values of various registers. E.g. you can set DDRB1 to 1 and PORTB1 to 0 to turn on the second LED on the board:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/11-b1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1827\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/11-b1.png\" alt=\"11-b1\" width=\"925\" height=\"678\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/11-b1.png 925w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/11-b1-300x220.png 300w\" sizes=\"(max-width: 925px) 100vw, 925px\" \/><\/a><\/li>\n<li>Some definitions in the AVR library heavily rely on preprocessor macros. You can simplify understanding them by right-clicking on the lines that use those macros and selecting &#8220;Preprocess Selected Lines&#8221;. VisualGDB will expand the macros in the line making it clear what that line of code does:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/12-prepro.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1828\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/12-prepro.png\" alt=\"12-prepro\" width=\"925\" height=\"678\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/12-prepro.png 925w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/04\/12-prepro-300x220.png 300w\" sizes=\"(max-width: 925px) 100vw, 925px\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to develop and debug firmware for AVR devices with Visual Studio using VisualGDB. Before you begin,<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89],"tags":[126,59],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1816"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=1816"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1816\/revisions"}],"predecessor-version":[{"id":2496,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1816\/revisions\/2496"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=1816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=1816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=1816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}