{"id":1348,"date":"2016-02-25T04:12:27","date_gmt":"2016-02-25T12:12:27","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=1348"},"modified":"2016-02-25T04:12:27","modified_gmt":"2016-02-25T12:12:27","slug":"developing-mbed-applications-with-rtx-and-visual-studio","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/mbed\/rtx\/","title":{"rendered":"Developing mbed applications with RTX and Visual Studio"},"content":{"rendered":"<p>This tutorial shows how to use the <a href=\"http:\/\/www.keil.com\/rl-arm\/kernel.asp\">Keil RTX<\/a> real-time operating system to create multi-threaded embedded applications with <a href=\"http:\/\/mbed.com\/\">mbed<\/a> and VisualGDB. Before you begin, install VisualGDB 5.1 or later and update your embedded tools via Embedded Tool Manager.<\/p>\n<ol>\n<li>Start Visual Studio and begin creating a new project with the VisualGDB Embedded Project Wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/01-rtxdemo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1349\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/01-rtxdemo.png\" alt=\"01-rtxdemo\" width=\"786\" height=\"488\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/01-rtxdemo.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/01-rtxdemo-300x186.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>On the first page select &#8220;Create a new project&#8221; and ensure that the &#8220;Enable multi-threaded build&#8221; checkbox is checked:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/02-newbin.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1350\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/02-newbin.png\" alt=\"02-newbin\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/02-newbin.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/02-newbin-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Click &#8220;Use ARM mbed&#8221; and select your mbed board in the device list. In this tutorial we will use the STM32F4Discovery board (DISCO_F407VG). Ensure you have newlib-nano selected as the C library:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/03-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1351\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/03-device.png\" alt=\"03-device\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/03-device.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/03-device-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the sample selection page select &#8220;LEDBlink (RTOS)&#8221; to create a project with RTOS support:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/04-rtos.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1352\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/04-rtos.png\" alt=\"04-rtos\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/04-rtos.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/04-rtos-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the Debug Settings page select your debug method. If you are using Segger J-Link or a device that can be programmed with firmware from segger, use the J-Link debug method, otherwise use OpenOCD:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/05-debug1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1353\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/05-debug1.png\" alt=\"05-debug\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/05-debug1.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/05-debug1-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to generate your project. Build it via Build-&gt;Build Solution:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/06-build1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1354\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/06-build1.png\" alt=\"06-build\" width=\"802\" height=\"595\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/06-build1.png 802w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/06-build1-300x223.png 300w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a><\/li>\n<li>Start debugging by pressing F5. See how the 2 LEDs blink with different periods as 2 independent threads are controlling them. Set a breakpoint on the line modifying the value of g_LED1 and wait for it to trigger:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/07-breakpoint.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1355\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/07-breakpoint.png\" alt=\"07-breakpoint\" width=\"802\" height=\"594\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/07-breakpoint.png 802w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/07-breakpoint-300x222.png 300w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a><\/li>\n<li>The line gets the current state of the LED, inverts it and sets the new state. Step into the operator that sets the new state by selecting Step Into Specific -&gt; operator=(int):<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/08-stepin1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1356\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/08-stepin1.png\" alt=\"08-stepin\" width=\"802\" height=\"593\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/08-stepin1.png 802w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/08-stepin1-300x222.png 300w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a><\/li>\n<li>Step into the write() method function and\u00a0 into the gpio_write() function again to see what exactly happens when the new value is assigned to g_LED1:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/09-write.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1357\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/09-write.png\" alt=\"09-write\" width=\"802\" height=\"593\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/09-write.png 802w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/09-write-300x222.png 300w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a><\/li>\n<li>Now we will modify our source code to demonstrate communication between the 2 threads. Replace the contents of your main file with this:\n<pre class=\"\">#include &lt;mbed.h&gt;\r\n#include &lt;rtos.h&gt;\r\n\u00a0\r\nDigitalOut g_LED1(LED1);\r\nDigitalOut g_LED2(LED2);\r\nSemaphore g_Semaphore(0);\r\n\r\nstatic void ThreadBody(const void *) \r\n{\r\n\u00a0\u00a0 \u00a0for (;;)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0g_LED1 = 0;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0g_Semaphore.wait();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0g_LED1 = 1;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Thread::wait(100);\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n\r\nint main()\r\n{\r\n\u00a0\u00a0 \u00a0Thread thread(ThreadBody);\r\n\u00a0\u00a0 \u00a0for (;;)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0g_LED2 = !g_LED2;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0g_Semaphore.release();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Thread::wait(300);\r\n\u00a0\u00a0 \u00a0}\r\n}<\/pre>\n<p>This will create a global semaphore that will be signaled each time the main thread inverts the second LED. The background thread will wait for the signal and do a short pulse on the first LED when it receives it.<\/li>\n<li>Build and run your program:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/10-run.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1358\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/10-run.png\" alt=\"10-run\" width=\"802\" height=\"594\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/10-run.png 802w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/10-run-300x222.png 300w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a>Observe how the first LED pulses quickly each time the second one changes state.<\/li>\n<li>You can step into the RTOS methods (e.g. Semaphore::wait()) by pressing F11. As your project includes all necessary mbed code, you can easily step through it and even modify it:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/11-sema.png\"> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1359\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/11-sema.png\" alt=\"11-sema\" width=\"802\" height=\"594\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/11-sema.png 802w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/01\/11-sema-300x222.png 300w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the Keil RTX real-time operating system to create multi-threaded embedded applications with mbed and<\/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":[56,115,61],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1348"}],"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=1348"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1348\/revisions"}],"predecessor-version":[{"id":1364,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1348\/revisions\/1364"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=1348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=1348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=1348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}