{"id":6760,"date":"2020-10-07T10:05:08","date_gmt":"2020-10-07T17:05:08","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=6760"},"modified":"2021-09-16T08:11:39","modified_gmt":"2021-09-16T15:11:39","slug":"creating-advanced-stm32cubemx-based-projects-with-visualgdb","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/stm32\/cube\/advanced\/","title":{"rendered":"Creating Advanced STM32CubeMX-based Projects with VisualGDB"},"content":{"rendered":"<p>This tutorial shows how to create Advanced STM32CubeMX-based projects with VisualGDB. The Advanced STM32CubeMX projects offer seamless integration with STM32CubeMX, can automatically launch it to edit the project layout, and do not require downloading the STM32 BSP supplied by VisualGDB.<\/p>\n<p>Before you begin, install VisualGDB 5.5 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB STM32CubeMX Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-newprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6768\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-newprj.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>Enter the name and location for your project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-name-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6769\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-name-2.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>If you are running the STM32CubeMX wizard for the first time, VisualGDB may ask for the location of the STM32CubeMX tool and the Java environment required for running it. Once they are located, VisualGDB will scan the STM32CubeMX directory for the list of supported devices and will display them on the first VisualGDB-specific page of the wizard. Select an ARM toolchain, pick your device and click &#8220;Next&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/cube.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7497\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/cube.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>VisualGDB will now automatically launch the STM32CubeMX tool with the device selected in the wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/04-runcube.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6771\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/04-runcube.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>You can use the STM32CubeMX GUI to customize the device (e.g. change the functions of various peripherals or reference various frameworks). In this tutorial we will accept the default configuration and close STM32CubeMX. There is no need to press the &#8220;Generate Code&#8221; button, as VisualGDB will handle it automatically: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/05-launched.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6772\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/05-launched.png\" alt=\"\" width=\"1275\" height=\"809\" \/><\/a><\/li>\n<li>Connect your development board via USB and select the debug settings on the second page of the wizard: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/06-debug-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6773\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/06-debug-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Once you click &#8220;Finish&#8221;, VisualGDB will generate a project based on the template created by STM32CubeMX. Note how the source files are shown under the &#8220;STM32CubeMX Files&#8221; node:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/07-structure.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6774\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/07-structure.png\" alt=\"\" width=\"1290\" height=\"805\" \/><\/a>These files are automatically managed by STM32CubeMX. Note the &#8220;USER CODE BEGIN&#8221; and &#8220;USER CODE END&#8221; markers. Make sure to add any extra code between these markers. This way, you can change various device parameters via STM32CubeMX GUI and it will preserve your changes to the files, while automatically editing device-specific code, like <strong>SystemClock_Config()<\/strong>.<\/li>\n<li>Now we will show how to add a new source file to the project. Right-click on the <strong>STM32CubeMXDemo<\/strong> node in Solution Explorer and select <strong>Add-&gt;New Item<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/08-addnew.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6775\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/08-addnew.png\" alt=\"\" width=\"1290\" height=\"806\" \/><\/a><\/li>\n<li>Select &#8220;C++ Source File&#8221; and click &#8220;Add&#8221;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/09-source.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6776\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/09-source.png\" alt=\"\" width=\"1290\" height=\"806\" \/><\/a><\/li>\n<li>Add the following code to the newly added file:\n<pre class=\"\">#include &lt;stm32f4xx_hal.h&gt;\r\n\r\nextern \"C\" void Sleep()\r\n{\r\n    HAL_Delay(100);\r\n}<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/10-added.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6777\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/10-added.png\" alt=\"\" width=\"1290\" height=\"806\" \/><\/a>Note how Source.cpp appears outside of <strong>STM32CubeMX Files<\/strong> in Solution Explorer because this file was added explicitly.<\/li>\n<li>Update <strong>main()<\/strong> to call <strong>Sleep()<\/strong> from the <strong>while(1)<\/strong> loop: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/11-sleep.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6778\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/11-sleep.png\" alt=\"\" width=\"1290\" height=\"806\" \/><\/a>You can now try building\/debugging the project. The STM32CubeMX-generated <strong>main()<\/strong> function will successfully call <strong>Sleep()<\/strong>.<\/li>\n<li>Now we will show how to edit the project structure via the STM32CubeMX GUI. Right-click on the <strong>STM32CubeMX Files<\/strong> node in Solution Explorer and select &#8220;<strong>Configure with STM32CubeMX<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/12-configure.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6779\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/12-configure.png\" alt=\"\" width=\"1290\" height=\"805\" \/><\/a><\/li>\n<li>VisualGDB will launch STM32CubeMX for your project. Go to the <strong>Pinout &amp; Configuration<\/strong> view and enable System Core-&gt;RCC and SYS. Configure HSE to use a resonator (as long as your board supports it):<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/14-hse.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6781\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/14-hse.png\" alt=\"\" width=\"1290\" height=\"808\" \/><\/a><\/li>\n<li>Switch to the Clock Configuration view, enter the resonator speed and set the AHB bus clock to the maximum value supported by your device. STM32CubeMX will automatically compute the PLL parameters required to achieve this speed: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/15-clocks.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6782\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/15-clocks.png\" alt=\"\" width=\"1290\" height=\"808\" \/><\/a><\/li>\n<li>Close STM32CubeMX while saving changes. There is no need to press &#8220;Geenrate Code&#8221; as VisualGDB will do it automatically once you exit STM32CubeMX:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/16-regenerate.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6783\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/16-regenerate.png\" alt=\"\" width=\"1290\" height=\"808\" \/><\/a>The STM32CubeMX-generated part (e.g. configuring the clock) will be updated, while the user-specific part between the <strong>USER CODE<\/strong> labels will be preserved.<\/li>\n<li>Now we will show how to switch the project to use FreeRTOS. Launch STM32CubeMX from the context menu again. Reference <strong>Middleware-&gt;FreeRTOS<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/13-freertos.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6780\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/13-freertos.png\" alt=\"\" width=\"1290\" height=\"808\" \/><\/a><\/li>\n<li>If STM32CubeMX displays warnings about changing the timebase source, you can configure it as shown below:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/21-tim.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6789\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/21-tim.png\" alt=\"\" width=\"1010\" height=\"693\" \/><\/a><\/li>\n<li>Once you close STM32CubeMX, the project will be automatically updated to reference FreeRTOS. Note that as the <strong>main.c<\/strong> file now follows a completely different structure, the changes you made to it will not be preserved. This only happens when switching between incompatible file layouts (e.g. FreeRTOS vs barebone):<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/17-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6784\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/17-built.png\" alt=\"\" width=\"1290\" height=\"808\" \/><\/a><\/li>\n<li>Locate the <strong>StartDefaultTask()<\/strong> function and set a breakpoint in it. See how it calls the <strong>osDelay()<\/strong> function from CMSIS: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/18-run.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6785\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/18-run.png\" alt=\"\" width=\"1290\" height=\"808\" \/><\/a><\/li>\n<li>Update the <strong>Sleep()<\/strong> function we created to also call <strong>osDelay()<\/strong>: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/19-sleep2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6786\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/19-sleep2.png\" alt=\"\" width=\"1290\" height=\"808\" \/><\/a><\/li>\n<li>Switch <strong>StartDefaultTask()<\/strong> to call <strong>Sleep()<\/strong> as well: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/20-call.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6787\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/20-call.png\" alt=\"\" width=\"1290\" height=\"808\" \/><\/a><\/li>\n<li>Now you can build the project and confirm that STM32CubeMX-generated <strong>StartDefaultTask()<\/strong> function calls our <strong>Sleep()<\/strong> implementation. You can use Embedded Memory Explorer to observe the FLASH and RAM utilization by various parts of the project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/22-usage.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6788\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/22-usage.png\" alt=\"\" width=\"1290\" height=\"808\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create Advanced STM32CubeMX-based projects with VisualGDB. The Advanced STM32CubeMX projects offer seamless integration with STM32CubeMX,<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[53,61,224],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6760"}],"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=6760"}],"version-history":[{"count":3,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6760\/revisions"}],"predecessor-version":[{"id":7498,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6760\/revisions\/7498"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=6760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=6760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=6760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}