{"id":5690,"date":"2020-03-22T09:19:29","date_gmt":"2020-03-22T16:19:29","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=5690"},"modified":"2020-05-10T10:06:18","modified_gmt":"2020-05-10T17:06:18","slug":"stm32-project-structure","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/documentation\/embedded\/stm32\/","title":{"rendered":"Typical STM32 Project Structure"},"content":{"rendered":"<p>This page explains the typical structure of an STM32 project, lists the involved components, and outlines the most common errors that may prevent the project from working correctly.<\/p>\n<h1>Main Parts of an STM32 Project<\/h1>\n<p>A typical STM32 project consists of the following parts:<\/p>\n<ol>\n<li>The STM32 HAL library containing implementations\u00a0 for functions like <strong>HAL_GPIO_WritePin()<\/strong>.<\/li>\n<li>HAL configuration file. It is typically located in the project directory and is called <strong>stm32xxxx_hal_conf.h<\/strong>.<\/li>\n<li>Startup file (<strong>startup_stm32xxxxx.c<\/strong>) that defines the interrupt vector table.<\/li>\n<li>Project-specific code, such as the <strong>main()<\/strong> function and other functions it calls.<\/li>\n<li>Other libraries and frameworks (e.g. FatFS).<\/li>\n<\/ol>\n<p>Many strange problems with STM32 projects are caused by combining incompatible versions of those components. E.g. trying to use a newer HAL library with an older <strong>hal_conf.h<\/strong> file could result in build errors because the new library would be expecting some macros that are not present in the old file.<\/p>\n<p>Before you begin troubleshooting any STM32-specific errors, ensure you locate each of those components in Solution Explorer and understand where they are physically located:<\/p>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/stm32components.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5695\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/stm32components.png\" alt=\"\" width=\"539\" height=\"345\" \/><\/a>You can find the physical location of each file via the Properties window in Solution Explorer:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/props.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5696\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/props.png\" alt=\"\" width=\"854\" height=\"301\" \/><\/a>Alternatively, you can open the mouse in the VS editor and hover the mouse over its tab header to find out the full path:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/popup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5697\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/popup.png\" alt=\"\" width=\"1332\" height=\"127\" \/><\/a><\/p>\n<h1>Regular STM32 Projects<\/h1>\n<p>When you create a regular project with the VisualGDB Embedded Project Wizard, VisualGDB will automatically download a copy of the STM32 library files (including the HAL library) and will store them under <strong>%LOCALAPPDATA%\\VisualGDB<\/strong>. Those files will appear inside the <strong>Device-Specific Files<\/strong> folder in Solution Explorer. You can use the <strong>VisualGDB Project Properties -&gt; Embedded Frameworks<\/strong> to add or remove the VisualGDB-managed copy of the HAL library:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/frameworks.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5698\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/frameworks.png\" alt=\"\" width=\"1147\" height=\"523\" \/><\/a>When you reference the any framework via <strong>VisualGDB Project Properties<\/strong> -&gt; <strong>Embedded Frameworks<\/strong>, it will do the following:<\/p>\n<ul>\n<li>Automatically add the necessary sources to Solution Explorer under the <strong>Device-Specific Files<\/strong>. <strong>WARNING: this will override any manually added files there<\/strong>.<\/li>\n<li>Automatically add the related include directories and preprocessor macros to the <strong>stm32.props<\/strong> (or <strong>stm32.mak<\/strong>) file. They will be stored separately from the regular VS project properties, so you won&#8217;t accidentally overwrite them.<\/li>\n<\/ul>\n<p>When you open the project on a different machine, VisualGDB would automatically download the STM32 BSP (including the HAL library) under %LOCALAPPDATA%, so you won&#8217;t need to copy those files along with your project. You can find out the currently installed version of the STM32 BSP via <strong>Tools-&gt;VisualGDB-&gt;Manage VisualGDB Packages<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/installed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5699\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/installed.png\" alt=\"\" width=\"1186\" height=\"498\" \/><\/a>VisualGDB Custom Edition or higher allows installing multiple versions of the same BSP and selecting the version for each project via <strong>VisualGDB Project Properties -&gt; Embedded Project<\/strong>.<\/p>\n<p><strong>WARNING: <\/strong>Each time you change any target-related settings in VisualGDB Project Properties, the list of files under the <strong>Device-Specific files<\/strong> folder will be overwritten. If you would like to manually add new files from the VisualGDB&#8217;s BSP to the project, make sure you add them outside the <strong>Device-Specific Files<\/strong> folder. If you want to remove some of the files that were automatically added, use the &#8220;<strong>Excluded From Build<\/strong>&#8221; flag instead:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/excl.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6007\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/excl.png\" alt=\"\" width=\"1164\" height=\"193\" \/><\/a>Files excluded from build will remain in Solution Explorer, but will be skipped when building the project. VisualGDB will not overwrite this flag when updating the list of files in Solution Explorer. Note that the flag works independently for each configuration.<\/p>\n<h1>Cloned STM32CubeMX Samples<\/h1>\n<p>If you create the project by cloning one of the STM32CubeMX samples via the VisualGDB Project Wizard, it will not reference the STM32 frameworks via the <strong>Embedded Frameworks<\/strong> mechanism. Instead, it will directly include just the files from the original ST sample. They will be located in the <strong>Shared sources folder<\/strong> outside <strong>Device-specific files<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/imported.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5700\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/imported.png\" alt=\"\" width=\"1330\" height=\"335\" \/><\/a>This project type is recommended for quickly exploring the STM32 code samples, however it may require non-trivial changes when upgrading to the newer STM32 BSP, or when trying to reference further libraries via <strong>VisualGDB Project Properties -&gt; Embedded Frameworks<\/strong>.<\/p>\n<h1>Imported STM32CubeMX Projects<\/h1>\n<p>Similarly to the cloned STM32CubeMX samples, the imported projects generated by the STM32CubeMX generator will not reference the regular HAL library. Instead, they will include their own copy of the HAL files and will reference them directly. The only VisualGDB-managed file will be the <strong>startup_stm32xxxx.c<\/strong> file containing the interrupt vector table:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/cube.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5702\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/cube.png\" alt=\"\" width=\"1218\" height=\"346\" \/><\/a><\/p>\n<h1>Stand-Alone Projects<\/h1>\n<p>If you are using VisualGDB Custom Edition or higher, you can convert any regular STM32 project to a stand-alone one. Stand-alone projects include their own copy of all the necessary files in the <strong>&lt;ProjectDirectory&gt;\\BSP<\/strong> folder, but are completely detached from the regular STM32 BSP and do not allow referencing any further frameworks: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/standalone.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5703\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/standalone.png\" alt=\"\" width=\"1408\" height=\"524\" \/><\/a><\/p>\n<h1>Project Type Summary<\/h1>\n<p>The following table lists the common STM32 project types, along with their advantages and disadvantages:<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 25%; text-align: center;\">Project Type<\/td>\n<td style=\"width: 25%; text-align: center;\">Recommended Use<\/td>\n<td style=\"width: 25%; text-align: center;\">STM32 HAL Location<\/td>\n<td style=\"width: 25%; text-align: center;\">Remarks<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">Regular<\/td>\n<td style=\"width: 25%;\">Long-term projects<\/td>\n<td style=\"width: 25%;\">%LOCALAPPDATA%\\VisualGDB\\&lt;&#8230;&gt;<\/td>\n<td style=\"width: 25%;\">Can be relatively easy upgraded to newer STM32 BSP versions.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">Cloned STM32CubeMX Samples<\/td>\n<td style=\"width: 25%;\">Quickly Exploring STM32 Examples<\/td>\n<td style=\"width: 25%;\">%LOCALAPPDATA%\\VisualGDB\\&lt;&#8230;&gt;<\/td>\n<td style=\"width: 25%;\">Upgrading the STM32 BSP, or referencing new frameworks may require manual changes to the project.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">Imported STM32CubeMX Projects<\/td>\n<td style=\"width: 25%;\">Long-term projects that require changes via STM32CubeMX.<\/td>\n<td style=\"width: 25%;\">Per STM32CubeMX Settings<\/td>\n<td style=\"width: 25%;\">Some Embedded Frameworks may conflict with STM32CubeMX-supplied versions.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">Stand-alone projects<\/td>\n<td style=\"width: 25%;\">Projects that require patches to the STM32 libraries<\/td>\n<td style=\"width: 25%;\">&lt;Project Directory&gt;\\BSP<\/td>\n<td style=\"width: 25%;\">Including a separate copy of the STM32 files with the project considerably increases its size.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>Troubleshooting Project Problems<\/h1>\n<p>Most problems with STM32 projects come from using multiple copies of the STM32 HAL (e.g. referencing one via <strong>VisualGDB Project Properties -&gt; Embedded Frameworks<\/strong> on top of the one included with the STM32CubeMX project), or by using incompatible versions of components (e.g. using an older configuration file with a newer HAL).<\/p>\n<p>In order to troubleshoot the build problems, make sure you understand which copy of the HAL libraries the project is supposed to use. E.g. if it was generated by STM32CubeMX, ensure it doesn&#8217;t reference the VisualGDB-supplied HAL library, or any other libraries. Also creating a new project of the same type from scratch, and comparing it to the broken project may help locating the root cause of the problems.<\/p>\n<p>If nothing helps, try clearing the <strong>Additional Include Directories<\/strong> setting:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/includes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5705\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/03\/includes.png\" alt=\"\" width=\"786\" height=\"544\" \/><\/a>If you were using <a href=\"https:\/\/visualgdb.com\/tutorials\/intellisense\/headers\/\">VisualGDB header discovery<\/a>, you might have inadvertently added incompatible include directories to the project (e.g. directories from the VisualGDB-supplied STM32 HAL to a project managed by STM32CubeMX, or vice versa).<\/p>\n<p>If you are new to C\/C++ and find the error messages confusing, check out <a href=\"https:\/\/visualgdb.com\/tutorials\/linux\/libraries\/diagnosing\/\">this tutorial<\/a>. It lists the common error messages and ways to fix them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This page explains the typical structure of an STM32 project, lists the involved components, and outlines the most common errors<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[197],"tags":[61],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5690"}],"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=5690"}],"version-history":[{"count":7,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5690\/revisions"}],"predecessor-version":[{"id":6008,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5690\/revisions\/6008"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=5690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=5690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=5690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}