{"id":1384,"date":"2016-02-28T18:55:13","date_gmt":"2016-02-29T02:55:13","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=1384"},"modified":"2016-02-28T18:55:13","modified_gmt":"2016-02-29T02:55:13","slug":"porting-embedded-code-with-mbed-and-visualgdb","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/mbed\/porting\/","title":{"rendered":"Porting embedded code with mbed and VisualGDB"},"content":{"rendered":"<p>This tutorial shows how to create portable embedded applications with the <a href=\"http:\/\/mbed.com\/\">ARM mbed<\/a> framework and VisualGDB. We will show how to create a basic multi-threaded application for the STM32F4Discovery and then quickly port it to the Freescale FRDM-KL25Z board.<\/p>\n<ol>\n<li>Begin with starting Visual Studio and opening the VisualGDB Embedded Project wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/01-prjname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1385\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/01-prjname.png\" alt=\"01-prjname\" width=\"786\" height=\"519\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/01-prjname.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/01-prjname-300x198.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Continue with the default &#8220;Create a new project&#8221; setting on the first page:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/02-prjtype.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1386\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/02-prjtype.png\" alt=\"02-prjtype\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/02-prjtype.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/02-prjtype-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the next page select the first device you want to target. We will start with the STM32F4Discovery board, so we select DISCO_F407 here. It is recommended to select newlib-nano as the C library to reduce the size of your binary:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/03-device2.png\"> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1387\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/03-device2.png\" alt=\"03-device\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/03-device2.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/03-device2-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the next page select &#8220;LEDBlink (RTOS)&#8221; sample and keep the default LED numbers:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/04-sample.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1388\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/04-sample.png\" alt=\"04-sample\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/04-sample.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/04-sample-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the last page select OpenOCD as the debug method:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/05-stlink.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1389\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/05-stlink.png\" alt=\"05-stlink\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/05-stlink.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/05-stlink-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Press Finish to generate your project. Build it by pressing Ctrl-Shift-B:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/06-build2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1390\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/06-build2.png\" alt=\"06-build\" width=\"802\" height=\"633\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/06-build2.png 802w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/06-build2-300x237.png 300w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a><\/li>\n<li>Hit F5 to start debugging. Observe how the two LEDs begin to blink with different periods as 2 independent threads are controlling them:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/f4disco1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1410\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/f4disco1.jpg\" alt=\"f4disco\" width=\"700\" height=\"404\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/f4disco1.jpg 700w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/f4disco1-300x173.jpg 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/li>\n<li>If you click on the LED1 text in your code and select &#8220;Go to definition&#8221;, you will see that it&#8217;s defined in the PinName enum as an alias for PD_13. You will also see other meaningful names like USER_BUTTON. Those names are defined independently for each supported mbed platform so that the application developers don&#8217;t need to look up the pin numbers in the schematic.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/07-led1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1391\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/07-led1.png\" alt=\"07-led1\" width=\"802\" height=\"633\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/07-led1.png 802w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/07-led1-300x237.png 300w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a><\/li>\n<li>Now we will switch the project to the KL25Z platform. First, follow <a href=\"http:\/\/visualgdb.com\/tutorials\/arm\/kinetis\/frdm-kl25z\/\">this tutorial<\/a> to ensure that you can program the KL25Z board successfully. Then open VisualGDB Project Properties for the mbed project and switch the device to KL25Z:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/08-kl25z.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1392\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/08-kl25z.png\" alt=\"08-kl25z\" width=\"867\" height=\"726\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/08-kl25z.png 867w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/08-kl25z-300x251.png 300w\" sizes=\"(max-width: 867px) 100vw, 867px\" \/><\/a><\/li>\n<li>Then go to the Debug Settings page and switch the debug method to Segger J-Link. Select the device manually (MKL25Z128xxx4) as the KL25Z name is too generic and check the &#8220;Reset device after programming&#8221; checkbox:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/09-segger.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1393\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/09-segger.png\" alt=\"09-segger\" width=\"867\" height=\"776\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/09-segger.png 867w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/09-segger-300x269.png 300w\" sizes=\"(max-width: 867px) 100vw, 867px\" \/><\/a><\/li>\n<li>Try building your project. The compiler will show an error indicating that the r7 register cannot be accessed explicitly:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/10-error.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1394\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/10-error.png\" alt=\"10-error\" width=\"802\" height=\"633\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/10-error.png 802w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/10-error-300x237.png 300w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a><\/li>\n<li>This happens because the debug build uses <strong>r7<\/strong> to store the frame pointer and the syscall code on Cortex M0 assumes that it is available. Fix this by adding the following line right after the SVC_Call() definition for Cortex M0:\n<pre class=\"\">#pragma GCC optimize (\"-fomit-frame-pointer\")<\/pre>\n<p><a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/11-omitfp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1395\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/11-omitfp.png\" alt=\"11-omitfp\" width=\"802\" height=\"633\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/11-omitfp.png 802w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/11-omitfp-300x237.png 300w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a><\/li>\n<li>Build and run your project. Observe how the red and green channels of the RGB LED show the same behavior as the green and yellow LEDs did on STM32F4Discovery:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/kl25z.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1411\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/kl25z.png\" alt=\"kl25z\" width=\"700\" height=\"379\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/kl25z.png 700w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/kl25z-300x162.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/li>\n<li>If you go to the definition of the LED1 now, you will see that it is defined differently for KL25Z reflecting different board schematic:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/12-newled1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1409\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/12-newled1.png\" alt=\"12-newled\" width=\"802\" height=\"633\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/12-newled1.png 802w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/12-newled1-300x237.png 300w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a>You can repeat the steps shown above to retarget the project to any device supported by mbed as long as you update the debug settings so that VisualGDB can program and debug your board.<\/li>\n<li>If you are using one of the boards not directly supported by VisualGDB, you can enable manual setup in the OpenOCD settings and pick the interface and board\/target scripts manually from the list, however they may need to be edited before they can work:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/13-openocd.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1412\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/13-openocd.png\" alt=\"13-openocd\" width=\"865\" height=\"775\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/13-openocd.png 865w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/13-openocd-300x269.png 300w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/a>The scripts are located under <strong>%LOCALAPPDATA%\\VisualGDB\\EmbeddedDebugPackages\\com.sysprogs.arm.openocd\\share\\openocd\\scripts<\/strong>.<\/li>\n<li>If you are using Segger J-Link, the setup will be much easier. Simply pick the device name from the list and the Segger software will configure everything automatically:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/14-segger.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1413\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/14-segger.png\" alt=\"14-segger\" width=\"867\" height=\"776\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/14-segger.png 867w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/14-segger-300x269.png 300w\" sizes=\"(max-width: 867px) 100vw, 867px\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create portable embedded applications with the ARM mbed framework and VisualGDB. We will show how<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[53,56,57,115,84,61],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1384"}],"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=1384"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1384\/revisions"}],"predecessor-version":[{"id":1495,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1384\/revisions\/1495"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=1384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=1384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=1384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}