{"id":6224,"date":"2020-06-01T12:52:01","date_gmt":"2020-06-01T19:52:01","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=6224"},"modified":"2020-06-01T12:55:22","modified_gmt":"2020-06-01T19:55:22","slug":"debugging-the-sipeed-maixduino-board-with-jtag-and-visualgdb","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arduino\/maixduino\/","title":{"rendered":"Debugging the Sipeed Maixduino board over JTAG with VisualGDB"},"content":{"rendered":"<p>This tutorial shows how to create a basic Arduino-based application for the Sipeed Maixduino board and debug it with JTAG. We clone the &#8220;selfie&#8221; sample from the Maixduino platform, that takes pictures using the included camera and shows them on the LCD screen, program it into the FLASH memory using the COM port, and then show how to debug the board with JTAG.<\/p>\n<p>In order to debug the board over JTAG, make sure you solder the following pins of the on-board K210 module to a JTAG20 cable (you can skip this for now, as we will first show how to build and upload the program using the on-board COM port):<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 25%;\">Pin Name<\/td>\n<td style=\"width: 25%;\">Pin Number<\/td>\n<td style=\"width: 25%;\">JTAG Signal<\/td>\n<td style=\"width: 25%;\">JTAG20 Pin<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">IO0\/JTAG_TCK<\/td>\n<td style=\"width: 25%;\">1<\/td>\n<td style=\"width: 25%;\">TCK<\/td>\n<td style=\"width: 25%;\">9<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">IO1\/JTAG_TDI<\/td>\n<td style=\"width: 25%;\">2<\/td>\n<td style=\"width: 25%;\">TDI<\/td>\n<td style=\"width: 25%;\">5<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">IO2\/JTAG_TMS<\/td>\n<td style=\"width: 25%;\">3<\/td>\n<td style=\"width: 25%;\">TMS<\/td>\n<td style=\"width: 25%;\">7<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">IO3\/JTAG_TDO<\/td>\n<td style=\"width: 25%;\">4<\/td>\n<td style=\"width: 25%;\">TDO<\/td>\n<td style=\"width: 25%;\">13<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">RST<\/td>\n<td style=\"width: 25%;\">55<\/td>\n<td style=\"width: 25%;\">SRST<\/td>\n<td style=\"width: 25%;\">15<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">3V3<\/td>\n<td style=\"width: 25%;\">(see power header on the board)<\/td>\n<td style=\"width: 25%;\">VDD<\/td>\n<td style=\"width: 25%;\">1<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">GND<\/td>\n<td style=\"width: 25%;\">(see power header on the board)<\/td>\n<td style=\"width: 25%;\">GND<\/td>\n<td style=\"width: 25%;\">4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Use the schematic below to double-check the wiring:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/schematic.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6225\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/schematic.jpg\" alt=\"\" width=\"1333\" height=\"594\" \/><\/a>The final JTAG setup should look as shown below:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/olimex.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6226\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/olimex.jpg\" alt=\"\" width=\"1280\" height=\"646\" \/><\/a>Now we will show how to create an Arduino-based VisualGDB project for Maixduino and upload it to the board. If you do not want to debug the board, you can skip the JTAG wiring setup and simply use the on-board COM port to upload the program to the FLASH memory.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB&#8217;s Arduino Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/01-arduino.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6227\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/01-arduino.png\" alt=\"\" width=\"966\" height=\"624\" \/><\/a><\/li>\n<li>Enter the name and location for your project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/02-loc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6228\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/02-loc.png\" alt=\"\" width=\"966\" height=\"624\" \/><\/a><\/li>\n<li>On the first page of the wizard select &#8220;Clone one of the sample sketches&#8221;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/03-clone.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6229\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/03-clone.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>On the next page of the wizard select the <strong>Sipeed Maixduino Board<\/strong>. If it does not appear, make sure you are using VisualGDB 5.5 or later and the Maixduino checkbox below the target list is checked: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/04-maixboard.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6230\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/04-maixboard.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>If this is the first time you create a project for Maixduino, click &#8220;Install&#8221; to automatically download the necessary Arduino packages. Once all the packages are ready, you will be able to select the board and proceed to the next page. As we will first use the on-board COM port to program the FLASH memory, make sure you select the correct port before proceeding to the next page: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/05-target.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6231\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/05-target.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>On the Project Sample page, pick the <strong>selfie<\/strong> sample and click &#8220;Next&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/06-selfie.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6232\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/06-selfie.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>Finally, you can set the debug settings for the project. If you don&#8217;t have the JTAG wiring setup yet, simply choose &#8220;built-in GDB simulator&#8221;. It won&#8217;t do any meaningful simulation for the K210 target, but will allow completing the wizard: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/07-sim.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6233\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/07-sim.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a> We will show the debug settings for JTAG-based debugging later in the tutorial.<\/li>\n<li>Press &#8220;Finish&#8221; to create the sample project, then try building it with Ctrl-Shift-B. As of June 2020, the build will fail due to a missing implementation for <strong>Camera::setRotation(uint8_t)<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/08-fail.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6234\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/08-fail.png\" alt=\"\" width=\"1180\" height=\"805\" \/><\/a><\/li>\n<li>You can fix the build error by navigating to definition of <strong>Sipeed_OV2640<\/strong> and adding an empty implementation of <strong>setRotation()<\/strong> to it:\n<pre class=\"\">    virtual void setRotation(uint8_t rotation) { }<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/09-impl.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6235\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/09-impl.png\" alt=\"\" width=\"1180\" height=\"805\" \/><\/a>Note that as of June 2020, the Maixduino Arduino platform implementation does not properly generate dependency files, so changing any of the sources will rebuild the entire project.<\/li>\n<li>Once the project is built, right-click on it in Solution Explorer and select &#8220;Program FLASH memory&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/10-program.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6236\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/10-program.png\" alt=\"\" width=\"1180\" height=\"805\" \/><\/a><\/li>\n<li>VisualGDB will invoke the Maixduino FLASH script to automatically program the FLASH memory via the COM port. You can change the COM port number via the first page of VisualGDB Project Properties. Once the FLASH is programmed, the on-board LCD screen will show the live feed from the camera:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/screen.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6241\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/screen.jpg\" alt=\"\" width=\"1280\" height=\"527\" \/><\/a><\/li>\n<li>Before you go further, we recommend patching the <strong>&lt;Documents&gt;\\ArduinoData\\packages\\Maixduino\\hardware\\k210\\0.3.11\\platform.txt<\/strong> file as shown below (you can find out the exact path by opening any file from the <strong>Sipeed Maixduino Board<\/strong> folder in Solution Explorer and checking its\u00a0 full path in Visual Studio):\n<ol>\n<li>Remove &#8220;-Os&#8221; from <strong>compiler.both.flags<\/strong> and add it to <strong>compiler.c.flags<\/strong> and <strong>compiler.cpp.flags<\/strong>.<\/li>\n<li>Add &#8220;-Os&#8221; to <strong>compiler.c.flags<\/strong> and <strong>compiler.cpp.flags<\/strong>.<\/li>\n<li>Add &#8220;-MMD&#8221; to <strong>compiler.c.flags<\/strong>, <strong>compiler.cpp.flags<\/strong> and <strong>compiler.S.flags<\/strong><\/li>\n<\/ol>\n<p>This will enable dependency file generation, greatly reducing the build time, and will allow VisualGDB to disable optimization for debug builds if enabled via VisualGDB Project Properties: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/patch.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6242\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/patch.png\" alt=\"\" width=\"1180\" height=\"805\" \/><\/a><\/li>\n<li>Now we will show how to debug the Maixduino board with JTAG. Make sure you followed the JTAG wiring setup instructions from the beginning of the page, connect your JTAG debugger to the board and open VisualGDB Project Properties. Select the probe in the &#8220;Debug using&#8221; field an click &#8220;Test&#8221;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/12-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6238\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/12-debug.png\" alt=\"\" width=\"812\" height=\"556\" \/><\/a>If everything is connected correctly, the test will succeed:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/test.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6243\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/test.png\" alt=\"\" width=\"786\" height=\"434\" \/><\/a>We have tested the Maixduino board with both Olimex ARM-USB-OCD-H and Segger J-Link debuggers (both using OpenOCD).<\/li>\n<li>Try adding a simple frame counter variable to <strong>loop()<\/strong>, set a breakpoint there and begin debugging. VisualGDB will load the program into the Maixduino RAM and will start debugging:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/13-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6239\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/13-bkpt.png\" alt=\"\" width=\"1180\" height=\"805\" \/><\/a>Note that the debugging experience using the K210 OpenOCD is less reliable compared to ARM (and other RISC-V) devices (see the end of <a href=\"https:\/\/visualgdb.com\/tutorials\/risc-v\/kendryte\/k210\/\">this tutorial<\/a> for details). Although the regular breakpoints and stepping work, we have found the following debugging limitations during our tests:\n<ul>\n<li>The debug session sometimes doesn&#8217;t start properly. Restarting it a few times fixes the problem.<\/li>\n<li>Adding\/removing breakpoints while the target is running often crashes the target.<\/li>\n<li>Resetting the target programmatically may not always work.<\/li>\n<\/ul>\n<\/li>\n<li>You can also use VisualGDB&#8217;s Live Watch window to monitor the values of different variables while the target is running. This will not interfere with the debug session:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/14-live.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6240\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/14-live.png\" alt=\"\" width=\"1180\" height=\"805\" \/><\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create a basic Arduino-based application for the Sipeed Maixduino board and debug it with JTAG.<\/p>\n","protected":false},"author":1,"featured_media":6245,"comment_status":"closed","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[170,188,151],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6224"}],"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=6224"}],"version-history":[{"count":4,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6224\/revisions"}],"predecessor-version":[{"id":6248,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6224\/revisions\/6248"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media\/6245"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=6224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=6224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=6224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}