{"id":1162,"date":"2015-11-05T14:29:12","date_gmt":"2015-11-05T22:29:12","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=1162"},"modified":"2017-05-26T10:30:31","modified_gmt":"2017-05-26T17:30:31","slug":"debugging-esp8266-code-with-openocd-and-visual-studio","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/esp8266\/openocd\/","title":{"rendered":"Debugging ESP8266 code with OpenOCD and Visual Studio"},"content":{"rendered":"<p>This tutorial shows how to develop and debug a basic ESP8266 project using the <a href=\"https:\/\/github.com\/sysprogs\/esp8266-openocd\">ESP8266 OpenOCD port<\/a> provided by Sysprogs and Visual Studio. Our latest <a href=\"http:\/\/gnutoolchains.com\/esp8266\/\">ESP8266 toolchain<\/a> includes the OpenOCD build, so you don&#8217;t need to install it separately.<br \/>\nBefore you begin, install VisualGDB 5.0 or later and follow the instructions described in <a href=\"http:\/\/visualgdb.com\/tutorials\/esp8266\/\">this tutorial<\/a> to connect your JTAG debugger to the ESP8266 chip. If you don&#8217;t want to use the JTAG interface, you can also debug your programs using the ESP8266 GDB stub as described in <a href=\"http:\/\/visualgdb.com\/tutorials\/esp8266\/gdbstub\">this tutorial<\/a>.<\/p>\n<p><span style=\"color: red;\"><strong>Warning: if you are switching from xt-ocd to OpenOCD, please reconnect the reset signal (RSTB) from SRST (pin 15 on JTAG20) to TRST (pin 3 on JTAG20).<\/strong><\/span><\/p>\n<ol>\n<li>\u00a0Start Visual Studio and launch the VisualGDB Embedded Project Wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/01-prj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1163\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/01-prj.png\" alt=\"01-prj\" width=\"786\" height=\"488\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/01-prj.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/01-prj-300x186.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Select &#8220;Create a new project =&gt; Embedded binary&#8221; and uncheck the &#8220;bin&#8221; flag:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/02-bin.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1164\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/02-bin.png\" alt=\"02-bin\" width=\"688\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/02-bin.png 688w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/02-bin-300x246.png 300w\" sizes=\"(max-width: 688px) 100vw, 688px\" \/><\/a><\/li>\n<li>Select the ESP8266 toolchain and choose the normal ESP8266 device from the list:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/03-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1165\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/03-device.png\" alt=\"03-device\" width=\"688\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/03-device.png 688w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/03-device-300x246.png 300w\" sizes=\"(max-width: 688px) 100vw, 688px\" \/><\/a><\/li>\n<li>On the next page proceed with the default LEDBlink sample:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/04-sample.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1166\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/04-sample.png\" alt=\"04-sample\" width=\"688\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/04-sample.png 688w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/04-sample-300x246.png 300w\" sizes=\"(max-width: 688px) 100vw, 688px\" \/><\/a><\/li>\n<li>On the last page of the wizard select OpenOCD as the debug method, connect your JTAG programmer and click &#8220;Detect&#8221; to automatically select a compatible OpenOCD script. If this does not work, you can select a JTAG programmer model manually or specify a script file explicitly:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/05-method.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1167\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/05-method.png\" alt=\"05-method\" width=\"688\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/05-method.png 688w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/05-method-300x246.png 300w\" sizes=\"(max-width: 688px) 100vw, 688px\" \/><\/a>If OpenOCD does not appear in the list, you are using an old ESP8266 toolchain. You can update your toolchain via Tools-&gt;Embedded Tools Manager.<\/li>\n<li>Press &#8220;Finish&#8221; to generate the project. Build it with Ctrl-Shift-B:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/06-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1168\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/06-build.png\" alt=\"06-build\" width=\"838\" height=\"698\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/06-build.png 838w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/06-build-300x250.png 300w\" sizes=\"(max-width: 838px) 100vw, 838px\" \/><\/a><\/li>\n<li>Set a breakpoint in user_init() and hit F5 to start debugging. VisualGDB will program the SPI FLASH automatically and launch your program. If this does not happen, double-check the JTAG connection and ensure that the reset signal is connected to pin 3 of the JTAG20 connector. If OpenOCD fails to connect to your JTAG programmer, follow our <a href=\"http:\/\/visualgdb.com\/tutorials\/arm\/openocd\/\">tutorial on OpenOCD for ARM<\/a> to diagnose and repair driver problems automatically:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/07-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1169\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/07-bkpt.png\" alt=\"07-bkpt\" width=\"838\" height=\"736\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/07-bkpt.png 838w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/07-bkpt-300x263.png 300w\" sizes=\"(max-width: 838px) 100vw, 838px\" \/><\/a>If the debugging starts, but the breakpoint is never hit, try resetting the device by pressing the green &#8220;reset&#8221; button in the GDB Session window.<\/li>\n<li>Press F5 and observe how the LED on the ESP8266 board is blinking:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/led.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-580\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/led.png\" alt=\"led\" width=\"700\" height=\"338\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/led.png 700w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/08\/led-300x145.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/li>\n<li>Set a breakpoint in TimerFunction() and wait until the breakpoint is triggered:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/08-timer.png\"> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1170\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/08-timer.png\" alt=\"08-timer\" width=\"838\" height=\"696\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/08-timer.png 838w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/08-timer-300x249.png 300w\" sizes=\"(max-width: 838px) 100vw, 838px\" \/><\/a><\/li>\n<li>Try stepping over the function calls by pressing F10. If you are using OpenOCD, it will automatically feed the ESP8266 watchdog preventing it from restarting your system. Otherwise, you would need to uncomment the call to <strong>system_soft_wdt_stop()<\/strong>:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/09-step.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1171\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/09-step.png\" alt=\"09-step\" width=\"838\" height=\"697\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/09-step.png 838w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/09-step-300x250.png 300w\" sizes=\"(max-width: 838px) 100vw, 838px\" \/><\/a><\/li>\n<li>You can change various debug-related settings by right-clicking on your project in Solution Explorer, selecting VisualGDB Project Properties and going to the Debug Settings page:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/10-settings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1172\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/10-settings.png\" alt=\"10-settings\" width=\"731\" height=\"647\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/10-settings.png 731w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/11\/10-settings-300x266.png 300w\" sizes=\"(max-width: 731px) 100vw, 731px\" \/><\/a>E.g. you can reduce the JTAG speed to improve stability or disable interrupt suppression or watchdog feeding features if they are interfering with your code.<\/li>\n<\/ol>\n<p>Now that the basic LEDBlink project is working, you can try creating a basic WiFi HTTP server for ESP8266 by following <a href=\"http:\/\/visualgdb.com\/tutorials\/esp8266\/http\/\">this tutorial<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to develop and debug a basic ESP8266 project using the ESP8266 OpenOCD port provided by Sysprogs<\/p>\n","protected":false},"author":1,"featured_media":586,"comment_status":"closed","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[142],"tags":[100,60],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1162"}],"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=1162"}],"version-history":[{"count":5,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1162\/revisions"}],"predecessor-version":[{"id":1186,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1162\/revisions\/1186"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media\/586"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=1162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=1162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=1162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}