{"id":1699,"date":"2016-04-19T22:09:19","date_gmt":"2016-04-20T05:09:19","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=1699"},"modified":"2016-04-19T22:09:19","modified_gmt":"2016-04-20T05:09:19","slug":"developing-freertos-applications-for-kinetis-devices","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/kinetis\/freertos\/","title":{"rendered":"Developing FreeRTOS Applications for Kinetis Devices"},"content":{"rendered":"<p>This tutorial shows how to develop embedded applications based on FreeRTOS for the Kinetis devices. We will create a basic multi-threaded application and run it on the FRDM-KL25Z board. Before you begin, install VisualGDB 5.0 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Embedded Project Wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-prjname2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1701\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-prjname2.png\" alt=\"01-prjname\" width=\"786\" height=\"534\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-prjname2.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/01-prjname2-300x204.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Select &#8220;Create a new project -&gt; Embedded Binary&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-binary1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1702\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-binary1.png\" alt=\"02-binary\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-binary1.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/02-binary1-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Select the ARM toolchain and choose your Kinetis device. If you don&#8217;t have the Kinetis KSDK package installed, VisualGDB will download and install it automatically once you select the device. In this tutorial we will use the MKL25Z128VLH4 device that is installed on the FRDM-KL25Z board:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-device2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1703\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-device2.png\" alt=\"03-device\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-device2.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/03-device2-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the sample selection page select the LEDBlink (FreeRTOS) sample and proceed with the default settings:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-sample1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1704\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-sample1.png\" alt=\"04-sample\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-sample1.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/04-sample1-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the Debug Method page select the Segger J-Link method and follow the instructions described in <a href=\"http:\/\/visualgdb.com\/tutorials\/arm\/kinetis\/frdm-kl25z\/\">this tutorial<\/a> to download the Segger J-Link firmware into the on-board OpenSDA programmer:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1705\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-debug.png\" alt=\"05-debug\" width=\"738\" height=\"580\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-debug.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/05-debug-300x236.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\/03\/06-build1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1706\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-build1.png\" alt=\"06-build\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-build1.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/06-build1-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>The created project contains just one thread that will blink the red LED on the board that is connected to port B18. Before we begin modifying it, press F5 to program it and verify that the LED actually blinks. Then set a breakpoint in MainTaskBody() and try stepping through it:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1707\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-bkpt.png\" alt=\"07-bkpt\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-bkpt.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/07-bkpt-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>Now we will modify the source code to have 2 threads instead of one. First of all replace the MainTaskBody() method to change the output of either port 18 (red LED) or port 19 (green LED) depending on the argument value and use different periods for these 2 LED channels:\n<pre class=\"\">void MainTaskBody(task_param_t param)\r\n{\r\n\u00a0\u00a0 \u00a0for (;;)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GPIO_HAL_ClearPinOutput(GPIOB_BASE_PTR, param ? 19 : 18);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0OSA_TimeDelay(param ? 300 : 500);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GPIO_HAL_SetPinOutput(GPIOB_BASE_PTR, param ? 19 : 18);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0OSA_TimeDelay(param ? 300 : 500);\r\n\u00a0\u00a0 \u00a0}\r\n}<\/pre>\n<\/li>\n<li>Then add initialization for port 19 similar to port 18 to the main() function:\n<pre class=\"\">\u00a0\u00a0\u00a0 PORTB_BASE_PTR-&gt;PCR[18] = PORT_PCR_MUX(1);\r\n\u00a0\u00a0\u00a0 PORTB_BASE_PTR-&gt;PCR[19] = PORT_PCR_MUX(1);\r\n\u00a0\u00a0 \u00a0OSA_Init();\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 GPIO_HAL_SetPinDir(GPIOB_BASE_PTR, 18, kGpioDigitalOutput);\r\n\u00a0\u00a0\u00a0 GPIO_HAL_SetPinDir(GPIOB_BASE_PTR, 19, kGpioDigitalOutput);<\/pre>\n<\/li>\n<li>Finally create another task in the main() function that will call the same MainTaskBody() method with an argument of 1 so that it will control channel 19 instead of 18:\n<pre class=\"\">\u00a0\u00a0\u00a0 OSA_TASK_DEFINE(task_func2, kMainTaskStackSize);\r\n\u00a0\u00a0\u00a0 status = OSA_TaskCreate(MainTaskBody,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (uint8_t *)\"task2\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 kMainTaskStackSize,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 task_func_stack,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 kMainTaskPriority,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (task_param_t)1,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 false,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &amp;task_func2_task_handler);<\/pre>\n<p><a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-task2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1708\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-task2.png\" alt=\"08-task2\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-task2.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/08-task2-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>Press F5 to build the project and ensure that the RGB LED color alternates between red, green and yellow (red + green). Then set a breakpoint in MainTaskBody() and see how it sometimes runs within a thread where param is 0 and sometimes in a thread where param is 1:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-thread2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1709\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-thread2.png\" alt=\"09-thread2\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-thread2.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/09-thread2-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>\u00a0\u00a0 Note that instead of calling the FreeRTOS functions directly the code calls OS Abstraction Layer functions like OSA_TimeDelay(). If you step into one of those, you will see that they simply call the FreeRTOS functions:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/delay.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1711\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/delay.png\" alt=\"delay\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/delay.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/delay-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>The Kinetis abstraction layer heavily relies on the preprocessor macros like OSA_TASK_DEFINE(). You can quickly see the actual code behind those macros by right-clicking on the lines containing them and selecting &#8220;Preprocess selected lines&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/prepro.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1712\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/prepro.png\" alt=\"prepro\" width=\"956\" height=\"676\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/prepro.png 956w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/prepro-300x212.png 300w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/a><\/li>\n<li>You can manage the frameworks referenced by the project (such as FreeRTOS) via the Embedded Frameworks page of VisualGDB Project Properties:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/10-frameworks.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1710\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/10-frameworks.png\" alt=\"10-frameworks\" width=\"834\" height=\"655\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/10-frameworks.png 834w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/03\/10-frameworks-300x236.png 300w\" sizes=\"(max-width: 834px) 100vw, 834px\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to develop embedded applications based on FreeRTOS for the Kinetis devices. We will create a basic<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[53,67,57],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1699"}],"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=1699"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1699\/revisions"}],"predecessor-version":[{"id":1713,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1699\/revisions\/1713"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=1699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=1699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=1699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}