{"id":4935,"date":"2019-06-18T19:38:55","date_gmt":"2019-06-19T02:38:55","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=4935"},"modified":"2019-06-18T19:38:55","modified_gmt":"2019-06-19T02:38:55","slug":"developing-arduino-code-for-stm32-boards-with-visual-studio","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arduino\/stm32\/","title":{"rendered":"Developing Arduino code for STM32 boards with Visual Studio"},"content":{"rendered":"<p>This tutorial shows how to develop Arduino-based projects for the STM32 boards using the <a href=\"https:\/\/github.com\/stm32duino\">STM32Duino project<\/a>, Visual Studio and VisualGDB.<\/p>\n<p>We will import the STM32 Arduino cores into VisualGDB and will show how to create and debug a basic project for the STM32F4Discovery board, and also how to programmatically print text into a virtual USB-based COM port provided by the STM32 Arduino framework.<\/p>\n<p>Before you begin, install VisualGDB 5.4R7 or later.<\/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-demo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4937\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/01-demo.png\" alt=\"\" width=\"941\" height=\"653\" \/><\/a><\/li>\n<li>On the first page of the wizard select <strong>Create a new &#8220;Blinking LED&#8221; project<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/02-blink-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4938\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/02-blink-1.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>If you don&#8217;t have the STM32 Arduino cores installed, select your board on the next page and click &#8220;Install&#8221;. If the board is not shown, ensure you have <strong>STM32 Boards<\/strong> checkbox enabled: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/03-discovery.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4939\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/03-discovery.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>Once the STM32 Arduino targets are installed, the target list will look differently: instead of listing all boards, the STM32 Arduino package lists board types and lets you specify an exact board via project properties. In this tutorial we are targeting the STM32F4Discovery board, so select <strong>Discovery<\/strong> and press &#8220;Next&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/04-board-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4940\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/04-board-1.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>Connect your board to the USB port and let VisualGDB detect the ST-Link you are using. As the exact board type is not chosen yet, VisualGDB will not automatically detect the debugged device type. Select it manually to match the board type you are using:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/05-family.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4941\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/05-family.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to create the project. VisualGDB will generate a basic &#8220;Blinking LED&#8221; project and will query its structure from Arduino:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/06-created.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4942\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/06-created.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Right-click on the project in Solution Explorer and select <strong>VisualGDB Project Properties<\/strong>. Then pick the exact board you are using and switch the &#8220;Optimize&#8221; setting to &#8220;Debug (-g)&#8221; to allow stepping through the code:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/07-settings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4943\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/07-settings.png\" alt=\"\" width=\"934\" height=\"696\" \/><\/a>Note that the debug settings chosen in the wizard will override the Arduino-level <strong>Upload method<\/strong> setting and hence there is no need to change it.<\/li>\n<li>Press F5 to build the project and start debugging it. Note how the on-board LED begins blinking:<img loading=\"lazy\" decoding=\"async\" class=\"size-full aligncenter\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/arm\/stm32\/f4_discovery\/led_on.png\" width=\"700\" height=\"466\" \/><\/li>\n<li>Set a breakpoint in the loop() function and wait for it to trigger. Once the breakpoint triggers, you will be able to step through the code, view variables and use other debugging functionality:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/08-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4944\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/08-debug.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Use can use the <strong>View-&gt;Embedded Memory Explorer<\/strong> command to display a detailed report about the memory utilization by the built project and compare different builds: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/09-memexp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4945\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/09-memexp.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Now we will show how to use the virtual COM port provided by the ESP32 Arduino cores. Open VisualGDB Project Properties and set the &#8220;USB Support&#8221; setting to &#8220;CDC&#8221;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/10-serial-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4951\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/10-serial-1.png\" alt=\"\" width=\"934\" height=\"696\" \/><\/a><\/li>\n<li>Add the following line inside the loop() function:\n<pre class=\"\">Serial.print(\"Hello\\r\\n\");<\/pre>\n<p>Then build the project and start debugging it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/11-built-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4947\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/11-built-1.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Most ST development boards have 2 USB connectors. The first one is connected to the on-board ST-Link, while the second one is connected to the target device. Locate the second USB connector on your board and ensure it is plugged in:<img loading=\"lazy\" decoding=\"async\" class=\"size-full aligncenter\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/arm\/stm32\/usb\/07-usb.jpg\" width=\"700\" height=\"262\" \/>Once you connect it, the board will be recognized as a USB-based COM port.<\/li>\n<li>Open VisualGDB project Properties and set the <strong>COM port for uploading the sketch<\/strong> to the virtual COM port:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/12-comport.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4948\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/12-comport.png\" alt=\"\" width=\"934\" height=\"696\" \/><\/a>Note that unlike other Arduino targets, STM32 targets do not actually use the Arduino-level COM port for programming the FLASH memory. Hence it will only be used to view the program output.<\/li>\n<li>Go to the Arduino Serial Terminal page of VisualGDB Project Properties and enable the raw terminal:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/13-terminal-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4949\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/13-terminal-1.png\" alt=\"\" width=\"934\" height=\"696\" \/><\/a><\/li>\n<li>Restart debugging. You will now see the output from the <strong>Serial.print()<\/strong> method inside Visual Studio: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/14-hello.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4950\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/14-hello.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to develop Arduino-based projects for the STM32 boards using the STM32Duino project, 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,61],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4935"}],"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=4935"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4935\/revisions"}],"predecessor-version":[{"id":4952,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4935\/revisions\/4952"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=4935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=4935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=4935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}