{"id":6998,"date":"2020-10-21T15:41:56","date_gmt":"2020-10-21T22:41:56","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=6998"},"modified":"2020-10-21T15:41:56","modified_gmt":"2020-10-21T22:41:56","slug":"creating-embedded-bootloader-projects-with-advanced-cmake","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/bootloader\/cmake\/","title":{"rendered":"Creating Embedded Bootloader Projects with Advanced CMake"},"content":{"rendered":"<p>This tutorial shows how to use the Advanced CMake Project Subsystem to create a multi-target project consisting of a main application and a separate bootloader stored at the beginning of the FLASH memory:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-overview.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1523\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-overview.png\" alt=\"\" width=\"800\" height=\"128\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-overview.png 800w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-overview-300x48.png 300w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a>This is a\u00a0simplified version of the <a href=\"https:\/\/visualgdb.com\/tutorials\/arm\/bootloader\/\">GNU Make-based bootloader tutorial<\/a>. We will focus on CMake-specific aspects of bootloader projects, and will leave out details about interrupt handling and system calls that are described in the GNU Make-based tutorial.<\/p>\n<ol>\n<li>Start Visual Studio and locate the VisualGDB Embedded Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-emb-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7001\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-emb-1.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>Enter the name and location of the project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-name-5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7002\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-name-5.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a>Note that Advanced CMake projects can contain multiple targets, so the <strong>BootloaderDemo<\/strong> project shown here will contain both the main application and the bootloader.<\/li>\n<li>Select &#8220;<strong>Create a new project -&gt; Embedded Application -&gt; Advanced CMake<\/strong>&#8221; and make sure the generation of <strong>.bin<\/strong> files is enabled:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/03-cmake-6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7003\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/03-cmake-6.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the next page of the wizard select the toolchain and the device you would like to target: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/04-device-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7004\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/04-device-2.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Proceed with the simplest <strong>LEDBlink<\/strong> sample:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/05-sample-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7005\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/05-sample-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Configure the debug settings that work with your board and click &#8220;Finish&#8221; to generate the project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/06-debug-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7006\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/06-debug-4.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>We will now replace the generated LEDBlink project with a very simple bootloader that will blink the LEDs twice and pass control to the main application. Locate the linker script in Solution Explorer, right-click on it and select &#8220;<strong>Copy to Project Directory<\/strong>&#8220;:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/07-script.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7007\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/07-script.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a>Once the linker script is copied, we can modify it to reflect the memory layout used by the bootloader and the application. The only modification required on the bootloader side is to define the _app_start symbol to point to the start of the actual application (see the entire linker script <a href=\"https:\/\/raw.githubusercontent.com\/sysprogs\/tutorials\/master\/visualgdb\/ARM\/CMake\/BootloaderDemo\/bootloader.lds\">here<\/a>):<\/p>\n<pre class=\"\">_app_start = 0x08000000 + 0x4000;<\/pre>\n<\/li>\n<li>Replace the contents of the main file with <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/blob\/master\/visualgdb\/ARM\/CMake\/BootloaderDemo\/BootloaderDemo.cpp\">this code<\/a>. It will blink the on-board LEDs twice and then pass control to the main application by calling the function stored at the second slot after the <strong>_app_start<\/strong> address (we will later place the main application\u2019s reset handler there): <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/08-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7008\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/08-built.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>You can quickly test out the project by setting a breakpoint on the call to <strong>_app_start()<\/strong> and pressing F5 to begin debugging. Do not step into <strong>_app_start<\/strong> yet, as it doesn&#8217;t contain anything meaningful:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/09-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7009\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/09-bkpt.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>Now we will create a separate target for the main embedded application. Right-click on the project node in Solution Explorer and select <strong>Add-&gt;New Item<\/strong>: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/10-newitem.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7010\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/10-newitem.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>Proceed with adding a new embedded executable: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/11-app.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7011\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/11-app.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>In order to avoid confusion between the bootloader&#8217;s and application&#8217;s linker scripts, rename the one in the <strong>BootloaderDemo<\/strong> target to <strong>bootloader.lds<\/strong>: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/12-rename.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7012\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/12-rename.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>Then copy the main application&#8217;s linker script file to the project directory and insert the .bootldr section in it:\n<pre class=\"\">.bootldr :\r\n{\r\n    . = ALIGN(4);\r\n    KEEP(*(.bootldr))\r\n    FILL(0xFFFFFF)\r\n    . = 0x4000;\r\n} &gt; FLASH<\/pre>\n<p>You can find the entire linker script <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/blob\/master\/visualgdb\/ARM\/CMake\/BootloaderDemo\/application.lds\">here<\/a>.<\/li>\n<li>The last modification made to the linker script will place the <strong>.bootldr<\/strong> section at the beginning of the FLASH, however we still need make sure the contents of the <strong>BootloaderDemo<\/strong> target ends up in that section. Right-click on the <strong>TargetApplication<\/strong> node in Solution Explorer and select &#8220;<strong>Add-&gt;Embed Another Target<\/strong>&#8220;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/13-embed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7013\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/13-embed.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>VisualGDB will ask for the target you would like to embed and will allow specifying the exact section name. Make sure the target gets placed in the <strong>.bootldr<\/strong> section: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/14-embedded.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7014\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/14-embedded.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a>Once you click OK, VisualGDB will automatically create an <a href=\"https:\/\/visualgdb.com\/reference\/cmake\/embed_target_output\">embed_target_output<\/a> statement, instructing the build system to embed the <strong>BootloaderDemo<\/strong> target into TargetApplication. You can always edit the statement in CMakeLists.txt to change this behavior, or use the properties in Solution Explorer to have VisualGDB edit it for you.<\/li>\n<li>Right-click on the <strong>TargetApplication<\/strong> node in Solution Explorer and select &#8220;<strong>Set as Startup Target<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/15-startup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7015\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/15-startup.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>Open VisualGDB Project Properties and enable the &#8220;<strong>Reset after programming<\/strong>&#8221; option on the Debug Settings page:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/16-resetafter-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7020\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/16-resetafter-1.png\" alt=\"\" width=\"995\" height=\"695\" \/><\/a>Note that for Advanced CMake projects the debug settings are shared between all targets of the same project. When you press F5, VisualGDB will use the startup target selected in Solution Explorer. You can also debug a specific target by selecting the &#8220;Debug&#8221; command in its context menu.<\/li>\n<li>Replace the contents of <strong>TargetApplication.cpp<\/strong> with <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/blob\/master\/visualgdb\/ARM\/CMake\/BootloaderDemo\/TargetApplication.cpp\">this code<\/a> and try building the project. The build will fail due to the missing <strong>SystemInit()<\/strong> call:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/17-nobuild.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7017\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/17-nobuild.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>You can reuse the <strong>system_stm32f4xx.c<\/strong> file from the bootloader by right-clicking on the application and selecting &#8220;<strong>Add-&gt;Existing Item<\/strong>&#8220;. Once it is added to the target, the project will build successfully. Note how most of the FLASH memory is occupied by the fixed-size bootloader block: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/18-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7018\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/18-built.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>Press F5 to begin debugging. The LEDs will briefly blink a couple of times, indicating that the bootloader has finished running, and the main program loop will then begin:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/19-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7019\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/19-bkpt.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<\/ol>\n<p>You can find the project shown in this tutorial in our <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/tree\/master\/visualgdb\/ARM\/CMake\/BootloaderDemo\">Github repository<\/a>.<br \/>\nTo learn about handling interrupts in bootloader-based setups and using syscalls from the application to the bootloader, follow the second part of the <a href=\"https:\/\/visualgdb.com\/tutorials\/arm\/bootloader\/\">GNU Make-based bootloader tutorial<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the Advanced CMake Project Subsystem to create a multi-target project consisting of a main<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[222],"tags":[119],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6998"}],"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=6998"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6998\/revisions"}],"predecessor-version":[{"id":7021,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6998\/revisions\/7021"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=6998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=6998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=6998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}