{"id":4953,"date":"2019-09-10T12:25:18","date_gmt":"2019-09-10T19:25:18","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=4953"},"modified":"2019-09-10T12:25:18","modified_gmt":"2019-09-10T19:25:18","slug":"debugging-the-stm32-blue-pill-board-with-visual-studio","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arduino\/stm32\/bluepill\/","title":{"rendered":"Debugging the STM32 Blue Pill board with Visual Studio"},"content":{"rendered":"<p>This tutorial shows how to create a basic project for the STM32 Blue Pill board with Visual Studio and VisualGDB.<\/p>\n<p>We will show the necessary wiring to program the FLASH memory using the ST-Link programmer from a separate STM32 Nucleo board and will then proceed with debugging the Blue Pill board with SWD.<\/p>\n<p>In order to debug the Blue Pill board via SWD, we would need to connect the following signals to an ST-Link programmer:<\/p>\n<ul>\n<li>Power and Ground<\/li>\n<li>SWD clock (SWCLK)<\/li>\n<li>SWD data (SWIO)<\/li>\n<li>Reset<\/li>\n<\/ul>\n<p>The firmware that is pre-loaded into the Blue Pill board reconfigures the SWD pins as generic I\/O pins, hence we will not be able to debug the board via SWD unless we also connect the reset pin and enable the &#8220;Connect under reset&#8221; setting in the software. The picture below shows the location of the signals required for SWD-based debugging: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/bluepill.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4961\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/bluepill.jpg\" alt=\"\" width=\"1121\" height=\"490\" \/><\/a>Although we can use a stand-alone ST-Link programmer, in this tutorial we will use the one embedded into a STM32 Nucleo board. In order to do that, remove the ST-Link\/Nucleo jumpers and use the signals shown below:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/stlink.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4962\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/stlink.jpg\" alt=\"\" width=\"700\" height=\"787\" \/><\/a>The final wiring should look as shown below:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/swd.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4963\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/swd.jpg\" alt=\"\" width=\"1200\" height=\"1037\" \/><\/a>Now we will proceed with creating a VisualGDB project for the board.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Arduino Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/01-newprj-9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4954\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/01-newprj-9.png\" alt=\"\" width=\"820\" height=\"613\" \/><\/a><\/li>\n<li>Proceed with the default &#8220;Blinking LED&#8221; option:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/02-blink-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4955\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/02-blink-2.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>The STM32 Arduino package organizes the supported targets in 2 levels: first you need to select the target family via the project wizard and then specify the exact board type via VisualGDB Project Properties. The STM32 Blue Pill board is based on the STM32F1 device, so pick <strong>Generic STM32F1 series<\/strong> in the device selector:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/03-generic-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4960\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/03-generic-1.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>Ensure you have both the board and the ST-Link connected via USB and select them on the Debug Method page of the wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/04-swd.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4965\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/04-swd.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a>Ensure you set the &#8220;Connect under reset&#8221; checkbox, as the default Blue Pill firmware disables the SWD pins and will not allow connecting a debugger in the regular mode.<\/li>\n<li>\u00a0 Press &#8220;Finish&#8221; to create the project. Once it is created and loaded, open VisualGDB Project Properties and set the correct board type and also set the &#8220;Optimize&#8221; field to <strong>Debug (-g):<\/strong><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/05-board.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4958\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/05-board.png\" alt=\"\" width=\"934\" height=\"696\" \/><\/a><\/li>\n<li>Now we will show how to step through the code in a debugger and to view the contents of global variables while the target is running. Add a global variable called <strong>g_Counter<\/strong> and modify the <strong>loop()<\/strong> method to increase it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/06-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4966\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/06-build.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Set a breakpoint in <strong>loop()<\/strong> and start debugging. Once a breakpoint hits, add <strong>g_Counter<\/strong> to the Live Variables window:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/07-live.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4967\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/07-live.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Press F5 to continue debugging and enable plotting in the Live Variables window. The Blue Pill board will continue blinking the LED, while VisualGDB will show how the value of <strong>g_Counter<\/strong> is slowly increasing:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/08-plot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4968\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/08-plot.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create a basic project for the STM32 Blue Pill board with Visual Studio and VisualGDB.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[170,62,61],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4953"}],"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=4953"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4953\/revisions"}],"predecessor-version":[{"id":4969,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4953\/revisions\/4969"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=4953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=4953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=4953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}