{"id":2374,"date":"2016-11-23T17:05:29","date_gmt":"2016-11-24T01:05:29","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=2374"},"modified":"2016-11-23T17:05:29","modified_gmt":"2016-11-24T01:05:29","slug":"debugging-stm32l0-firmware-with-sleep-mode-with-openocd","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/stm32\/sleep\/","title":{"rendered":"Debugging STM32L0 Firmware with Sleep Mode with OpenOCD"},"content":{"rendered":"<p>This tutorial shows how to configure VisualGDB and OpenOCD to debug STM32L0 firmware that puts the microcontroller in the sleep mode.<\/p>\n<p>We will create a basic project for the NUCLEO-L031K6\u00a0 board, show why the default configuration does not work when the CPU enters the sleep mode and how to work around it by using a special reset mode in debug settings.<\/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\/11\/01-prjname3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2375\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/01-prjname3.png\" alt=\"01-prjname\" width=\"941\" height=\"653\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/01-prjname3.png 941w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/01-prjname3-300x208.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/01-prjname3-392x272.png 392w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/01-prjname3-130x90.png 130w\" sizes=\"(max-width: 941px) 100vw, 941px\" \/><\/a><\/li>\n<li>Select &#8220;Create a new project -&gt; Embedded binary&#8221; and click &#8220;next&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/02-prjtype.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2376\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/02-prjtype.png\" alt=\"02-prjtype\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/02-prjtype.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/02-prjtype-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Select the ARM toolchain and your device from the list. The NUCLEO-L031K6 board shown in this tutorial has the STM32L031K6 microcontroller:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/03-device1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2377\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/03-device1.png\" alt=\"03-device\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/03-device1.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/03-device1-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Proceed with the default &#8220;LEDBlink&#8221; sample:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/04-sample1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2378\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/04-sample1.png\" alt=\"04-sample\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/04-sample1.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/04-sample1-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the last page select &#8220;OpenOCD&#8221;, connect your board via USB and click &#8220;Detect&#8221; to automatically detect debugging settings:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/05-debug3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2379\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/05-debug3.png\" alt=\"05-debug\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/05-debug3.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/05-debug3-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to generate your project. Replace the contents of the main() function with the following:\n<pre class=\"\">int main(void)\r\n{\r\n\u00a0\u00a0 \u00a0HAL_Init();\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 __GPIOB_CLK_ENABLE();\r\n\u00a0\u00a0\u00a0 GPIO_InitTypeDef GPIO_InitStructure;\r\n\r\n\u00a0\u00a0\u00a0 GPIO_InitStructure.Pin = GPIO_PIN_3;\r\n\r\n\u00a0\u00a0\u00a0 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;\r\n\u00a0\u00a0\u00a0 GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;\r\n\u00a0\u00a0\u00a0 GPIO_InitStructure.Pull = GPIO_NOPULL;\r\n\u00a0\u00a0\u00a0 HAL_GPIO_Init(GPIOB, &amp;GPIO_InitStructure);\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 for (;;)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0 \u00a0\r\n}<\/pre>\n<p>This will put the device in the sleep mode until a SysTick interrupt occurs.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/06-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2380\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/06-build.png\" alt=\"06-build\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/06-build.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/06-build-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>Press F5 to start debugging. The debugging should start normally and you should be able to step through the code:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/07-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2381\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/07-debug.png\" alt=\"07-debug\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/07-debug.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/07-debug-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>Press Shift-F5 to stop debugging and try starting it again. Debugging will now fail with the following error shown by OpenOCD:\n<pre class=\"\">Error: timed out while waiting for target halted<\/pre>\n<p><a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/08-problem.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2382\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/08-problem.png\" alt=\"08-problem\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/08-problem.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/08-problem-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a>The error happens because while the device is in the sleep mode, OpenOCD cannot properly connect to it over JTAG.<\/li>\n<li>Now we will show how to fix this by editing the OpenOCD configuration. Open the Debug Settings page of VisualGDB Project Properties and switch to the <strong>Manual setup<\/strong> mode:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/09-debugsetting.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2383\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/09-debugsetting.png\" alt=\"09-debugsetting\" width=\"1159\" height=\"967\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/09-debugsetting.png 1159w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/09-debugsetting-300x250.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/09-debugsetting-1024x854.png 1024w\" sizes=\"(max-width: 1159px) 100vw, 1159px\" \/><\/a><\/li>\n<li>Click the &#8220;Open&#8221; button in the &#8220;Target&#8221; field, make a copy of the original file (e.g. to stm32l0_reset.cfg) and open it in an editor:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/11-reset.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2384\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/11-reset.png\" alt=\"11-reset\" width=\"1159\" height=\"968\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/11-reset.png 1159w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/11-reset-300x251.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/11-reset-1024x855.png 1024w\" sizes=\"(max-width: 1159px) 100vw, 1159px\" \/><\/a><\/li>\n<li>Locate the line starting with reset_config and replace it with the following one:\n<pre class=\"\">reset_config trst_and_srst srst_nogate connect_assert_srst<\/pre>\n<p><a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/12-reset.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2385\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/12-reset.png\" alt=\"12-reset\" width=\"832\" height=\"428\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/12-reset.png 832w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/12-reset-300x154.png 300w\" sizes=\"(max-width: 832px) 100vw, 832px\" \/><\/a>The <strong>connect_assert_rst<\/strong> flag will force OpenOCD to reset the CPU during the initial connection, forcing it out of the sleep mode and ensuring that the debug communication succeeds.<\/li>\n<li>Save the new script and ensure that it is selected in the VisualGDB Project Properties:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/13-newcfg.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2386\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/13-newcfg.png\" alt=\"13-newcfg\" width=\"1159\" height=\"967\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/13-newcfg.png 1159w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/13-newcfg-300x250.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/13-newcfg-1024x854.png 1024w\" sizes=\"(max-width: 1159px) 100vw, 1159px\" \/><\/a><\/li>\n<li>Press F5 to start debugging. The debugging should now work normally despite the firmware entering the sleep mode:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/14-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2387\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/14-debug.png\" alt=\"14-debug\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/14-debug.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/14-debug-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to configure VisualGDB and OpenOCD to debug STM32L0 firmware that puts the microcontroller in the sleep<\/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":[61],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2374"}],"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=2374"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2374\/revisions"}],"predecessor-version":[{"id":2388,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2374\/revisions\/2388"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=2374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=2374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=2374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}