{"id":5164,"date":"2019-08-19T11:52:18","date_gmt":"2019-08-19T18:52:18","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=5164"},"modified":"2019-08-19T11:52:18","modified_gmt":"2019-08-19T18:52:18","slug":"debugging-the-arm-cortex-m4-core-of-the-stm32mp1-devices","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/stm32\/stm32mp1\/","title":{"rendered":"Debugging the ARM Cortex-M4 Core of the STM32MP1 Devices"},"content":{"rendered":"<p>This tutorial shows how to create a basic project for the Cortex-M4 core of the STM32MP1 device and debug it via the on-board ST-Link device in both engineering and production modes. We will create a basic &#8220;Blinking LED&#8221; project using Visual Studio and VisualGDB and will show how to upload it to the STM32MP1 device and debug it. Before you begin, ensure you have VisualGDB 5.4R11 or later installed.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Embedded Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/01-newprj-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5165\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/01-newprj-3.png\" alt=\"\" width=\"1024\" height=\"710\" \/><\/a><\/li>\n<li>Specify the location and the name for the project that is going to be created:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/02-prjname-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5166\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/02-prjname-3.png\" alt=\"\" width=\"1024\" height=\"710\" \/><\/a><\/li>\n<li>Proceed with the default settings on the first page of the VisualGDB&#8217;s wizard and click &#8220;Next&#8221; to go the next page: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/03-binary-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5167\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/03-binary-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the Device Selection page choose the ARM toolchain and select your STM32MP1 device from the list. If you have not created STM32MP1 projects before, click the &#8220;Install&#8221; button to automatically download the necessary files to your computer:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/04-install.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5168\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/04-install.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>In this tutorial we will use the STM32MP157C-DK2 board, hence we select the STM32MP157C device. If you are using a different board, select the device that matches your board:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/05-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5169\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/05-device.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the next page select the default &#8220;LEDBlink&#8221; example and pick the GPIO group and pin where the LED is connected. On the STM32MP157C-DK2 board the LED is connected to GPIOH7:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/06-gpioh7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5170\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/06-gpioh7.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Switch the board into engineering mode via the boot jumpers and connect the power and ST-Link USB connectors as shown below:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/engmode.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5176\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/engmode.jpg\" alt=\"\" width=\"1280\" height=\"711\" \/><\/a><\/li>\n<li>In engineering mode, the Cortex-M4 core will be automatically started once you power the board and the Cortex-A core will not run the regular SD card boot process. This allows quickly prototyping Cortex-M4 firmware without configuring the Linux-level settings (we will show those settings later in the tutorial). Ensure ST-Link is detected on the Debug Method page of the VisualGDB&#8217;s project wizard and select &#8220;STM32MP1xx (Engineering Mode)&#8221; as the debugged device:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/08-debugsettings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5172\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/08-debugsettings.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Press the &#8220;Test&#8221; button to verify the JTAG connection to the board. As the Cortex-A core is suspended in engineering mode, VisualGDB will display a warning about the registers having zero values (that would normally imply a JTAG issue). Ignore the warning and press &#8220;Finish&#8221; to create the project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/09-engmode-zero.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5173\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/09-engmode-zero.png\" alt=\"\" width=\"1024\" height=\"732\" \/><\/a><\/li>\n<li>Once the project is created, build it by pressing Ctrl-Shift-B:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/10-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5174\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/10-build.png\" alt=\"\" width=\"1377\" height=\"847\" \/><\/a><\/li>\n<li>Press F5 to start debugging. Observe how the on-board LED begins to blink:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/led.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5177\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/led.jpg\" alt=\"\" width=\"1280\" height=\"795\" \/><\/a><\/li>\n<li>Set a breakpoint inside the loop in <strong>main()<\/strong> and wait for it to trigger. Once the debugging session stops at the breakpoint, you will be able to step through the code and evaluate variables as usual:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/11-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5175\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/11-bkpt.png\" alt=\"\" width=\"1377\" height=\"847\" \/><\/a><\/li>\n<li>Now we will show how to control the STM32MP1&#8217;s Cortex-M4 core from the Linux running on the Cortex-A core and how to debug this setup. First of all, switch the board&#8217;s boot jumpers back to the default state, plug in the network connector and wait for the board to boot the Linux:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/netconn.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5143\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/netconn.jpg\" alt=\"\" width=\"1280\" height=\"610\" \/><\/a><\/li>\n<li>Locate the ELF file produced by VisualGDB (it will normally be located under &lt;Project folder&gt;\\VisualGDB\\Debug\\&lt;Project Name&gt; and will have no extension):<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/file.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5179\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/file.png\" alt=\"\" width=\"786\" height=\"593\" \/><\/a><\/li>\n<li>Upload the file into the <strong>\/lib\/firmware<\/strong> folder on your STM32MP1 board (you can use <a href=\"https:\/\/sysprogs.com\/SmarTTY\/\">SmarTTY<\/a> to do this via a simple drag-and-drop). Then you will be able to use the following commands to control the Cortex-M4 core from the Linux running on the Cortex-A core:<br \/>\n<table style=\"border-collapse: collapse; width: 100%; height: 96px;\" border=\"1\">\n<tbody>\n<tr style=\"height: 24px;\">\n<td style=\"width: 50%; height: 24px;\">Action<\/td>\n<td style=\"width: 50%; height: 24px;\">Command<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 50%; height: 24px;\">Stop and disable the Cortex-M4 core<\/td>\n<td style=\"width: 50%; height: 24px;\">echo stop &gt; \/sys\/class\/remoteproc\/remoteproc0\/state<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 50%; height: 24px;\">Set the name of the firmware file that will be loaded into the Cortex-M4 core once it is started<\/td>\n<td style=\"width: 50%; height: 24px;\">echo <em>[name of the file in \/lib\/firmware]<\/em> &gt;\u00a0 \/sys\/class\/remoteproc\/remoteproc0\/firmware<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 50%; height: 24px;\">Load the firmware file specified earlier to the Cortex-M4 core and start it<\/td>\n<td style=\"width: 50%; height: 24px;\">echo start &gt; \/sys\/class\/remoteproc\/remoteproc0\/state<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/12-upload.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5180\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/12-upload.png\" alt=\"\" width=\"1051\" height=\"660\" \/><\/a>Note that if the core was already stopped, the &#8220;echo stop&#8221; command will return an error that can be safely ignored.<\/li>\n<li>Verify that the LED started blinking the same way it did in the engineering mode. Then open VisualGDB Project Properties and switch the Debugged Device from <strong>STM32MP1 (Engineering Mode)<\/strong> to\u00a0 <strong>STM32MP1 (with PMIC)<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/13-mode.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5181\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/13-mode.png\" alt=\"\" width=\"1036\" height=\"694\" \/><\/a><\/li>\n<li>Start the debug session. VisualGDB will automatically reload and restart the firmware in the Cortex-M4 core, allowing you to debug it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/14-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5182\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/14-bkpt.png\" alt=\"\" width=\"1238\" height=\"863\" \/><\/a><\/li>\n<li>If you are using the Custom edition of VisualGDB, you can configure it to automatically upload the firmware and run the Linux-side commands each time you start debugging. To do this, open VisualGDB Project Properties on the Custom Debug Steps page and add the following actions:\n<ol>\n<li>Copy <strong>$(TargetPath)<\/strong> to <strong>\/lib\/firmware\/$(TargetFileName)<\/strong> on <strong>root@stm32mp1<\/strong><\/li>\n<li>Run &#8220;<strong>echo stop &gt;\/sys\/class\/remoteproc\/remoteproc0\/state || echo &#8220;Already stopped&#8221;<\/strong>&#8221; in &#8220;<strong>\/<\/strong>&#8221; on <strong>root@stm32mp1<\/strong>. Note that the &#8221; || echo &#8220;Already Stopped&#8221;&#8221; part is needed to prevent VisualGDB from aborting the debug session when the &#8220;stop&#8221; command fails if the firmware was already running.<\/li>\n<li>Run &#8220;<strong>echo $(TargetFileName) &gt; \/sys\/class\/remoteproc\/remoteproc0\/firmware<\/strong>&#8220;<\/li>\n<li>Run &#8220;<strong>echo stop &gt;\/sys\/class\/remoteproc\/remoteproc0\/state<\/strong>&#8220;.<\/li>\n<\/ol>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/15-actions.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5183\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/15-actions.png\" alt=\"\" width=\"1404\" height=\"863\" \/><\/a>You can copy the following text to the Clipboard and then use the &#8220;Paste&#8221; button in the Custom Debug Steps editor to automatically paste the actions into VisualGDB project Properties:<\/p>\n<pre class=\"\">&lt;?xml version=\"1.0\" encoding=\"utf-16\"?&gt;\r\n&lt;ArrayOfCustomActionBase xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\"&gt;\r\n  &lt;CustomActionBase xsi:type=\"FileTransferAction\"&gt;\r\n    &lt;SkipWhenRunningCommandList&gt;false&lt;\/SkipWhenRunningCommandList&gt;\r\n    &lt;SourceHost&gt;\r\n      &lt;HostName&gt;BuildMachine&lt;\/HostName&gt;\r\n      &lt;Transport&gt;BuiltinShortcut&lt;\/Transport&gt;\r\n    &lt;\/SourceHost&gt;\r\n    &lt;DestinationHost&gt;\r\n      &lt;HostName&gt;stm32mp1&lt;\/HostName&gt;\r\n      &lt;Transport&gt;SSH&lt;\/Transport&gt;\r\n      &lt;UserName&gt;root&lt;\/UserName&gt;\r\n    &lt;\/DestinationHost&gt;\r\n    &lt;SourceFilePath&gt;$(TargetPath)&lt;\/SourceFilePath&gt;\r\n    &lt;DestinationFilePath&gt;\/lib\/firmware\/$(TargetFileName)&lt;\/DestinationFilePath&gt;\r\n    &lt;OverwriteTrigger&gt;Always&lt;\/OverwriteTrigger&gt;\r\n  &lt;\/CustomActionBase&gt;\r\n  &lt;CustomActionBase xsi:type=\"CommandLineAction\"&gt;\r\n    &lt;SkipWhenRunningCommandList&gt;false&lt;\/SkipWhenRunningCommandList&gt;\r\n    &lt;RemoteHost&gt;\r\n      &lt;HostName&gt;stm32mp1&lt;\/HostName&gt;\r\n      &lt;Transport&gt;SSH&lt;\/Transport&gt;\r\n      &lt;UserName&gt;root&lt;\/UserName&gt;\r\n    &lt;\/RemoteHost&gt;\r\n    &lt;Command&gt;echo&lt;\/Command&gt;\r\n    &lt;Arguments&gt;stop &amp;gt; \/sys\/class\/remoteproc\/remoteproc0\/state || echo \"Already stopped\"&lt;\/Arguments&gt;\r\n    &lt;WorkingDirectory&gt;\/&lt;\/WorkingDirectory&gt;\r\n    &lt;BackgroundMode&gt;false&lt;\/BackgroundMode&gt;\r\n  &lt;\/CustomActionBase&gt;\r\n  &lt;CustomActionBase xsi:type=\"CommandLineAction\"&gt;\r\n    &lt;SkipWhenRunningCommandList&gt;false&lt;\/SkipWhenRunningCommandList&gt;\r\n    &lt;RemoteHost&gt;\r\n      &lt;HostName&gt;stm32mp1&lt;\/HostName&gt;\r\n      &lt;Transport&gt;SSH&lt;\/Transport&gt;\r\n      &lt;UserName&gt;root&lt;\/UserName&gt;\r\n    &lt;\/RemoteHost&gt;\r\n    &lt;Command&gt;echo&lt;\/Command&gt;\r\n    &lt;Arguments&gt;$(TargetFileName) &amp;gt; \/sys\/class\/remoteproc\/remoteproc0\/firmware&lt;\/Arguments&gt;\r\n    &lt;WorkingDirectory&gt;\/&lt;\/WorkingDirectory&gt;\r\n    &lt;BackgroundMode&gt;false&lt;\/BackgroundMode&gt;\r\n  &lt;\/CustomActionBase&gt;\r\n  &lt;CustomActionBase xsi:type=\"CommandLineAction\"&gt;\r\n    &lt;SkipWhenRunningCommandList&gt;false&lt;\/SkipWhenRunningCommandList&gt;\r\n    &lt;RemoteHost&gt;\r\n      &lt;HostName&gt;stm32mp1&lt;\/HostName&gt;\r\n      &lt;Transport&gt;SSH&lt;\/Transport&gt;\r\n      &lt;UserName&gt;root&lt;\/UserName&gt;\r\n    &lt;\/RemoteHost&gt;\r\n    &lt;Command&gt;echo&lt;\/Command&gt;\r\n    &lt;Arguments&gt;start &amp;gt; \/sys\/class\/remoteproc\/remoteproc0\/state&lt;\/Arguments&gt;\r\n    &lt;WorkingDirectory&gt;\/&lt;\/WorkingDirectory&gt;\r\n    &lt;BackgroundMode&gt;false&lt;\/BackgroundMode&gt;\r\n  &lt;\/CustomActionBase&gt;\r\n&lt;\/ArrayOfCustomActionBase&gt;<\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create a basic project for the Cortex-M4 core of the STM32MP1 device and debug it<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89],"tags":[53,59,187],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5164"}],"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=5164"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5164\/revisions"}],"predecessor-version":[{"id":5184,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5164\/revisions\/5184"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=5164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=5164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=5164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}