{"id":2128,"date":"2016-02-06T10:33:30","date_gmt":"2016-02-06T09:33:30","guid":{"rendered":"https:\/\/playembedded.org\/?p=2128"},"modified":"2020-04-17T21:09:31","modified_gmt":"2020-04-17T19:09:31","slug":"mikroe-clicker-2-for-stm32-and-stlink-v2","status":"publish","type":"post","link":"https:\/\/playembedded.org\/blog\/mikroe-clicker-2-for-stm32-and-stlink-v2\/","title":{"rendered":"Mikroe Clicker 2 for STM32 and STLink v2"},"content":{"rendered":"<h3 id=\"1_Introduction\" class=\"level_1\">Introduction<\/h3>\n<p>In this tutorial we are going to see how to connect a STLink v2 debugger to a Clicker 2 for STM32 enabling users to do debugging which could be very useful in development phases. A debugger is a computer program which is used to test and debug another program usually named Target.<\/p>\n<p>In embedded things are slightly different since the code is executed on an external MCU and it is required an interface Computer-MCU. In this case the <i>Target<\/i> is the MCU and a debugger is composed by the ensemble of a hardware and a software tool. Nonetheless, when the majority of people is talking about debugger,they are referring only to the hardware part.<\/p>\n<p>A debugger allows the connection to a Target enabling the <b>Host<\/b> to load a firmware on it managing its code execution, allowing the inspection of its registers and much more. To do this, it uses some specific commands that usually depend on the MCU architecture: this means that there are specific debuggers for specific MCUs. The STLink v2 is a low cost debugger supporting both <b>STM32<\/b> and <b>STM8<\/b>. In what follows we are going to use <b>OpenOCD<\/b>, an open source debugging software largely used in <a href=\"https:\/\/playembedded.org\/en\/2014\/10\/19\/a-quick-view-of-chibistudio\/\" target=\"_blank\" rel=\"noopener noreferrer\">ChibiStudio<\/a> since it is a reliable solution for debugging.<!--more--><\/p>\n<h3 id=\"2_The_JTAG_SWD_connector\" class=\"level_1\">The JTAG\/SWD connector<\/h3>\n<h4 id=\"3_Joint_Test_Action_Group\" class=\"level_2\">Joint Test Action Group<\/h4>\n<p>The <b>JTAG<\/b> also know as <i>Joint Test Action Group<\/i> is an electronics industry association founded in 1985 for developing standards for on-chip instrumentation in electronic design automation as an alternative to simulation tools. Today, the JTAG is a standard used by many semiconductor chip manufacturers which have extended it with commands to provide vendor-specific features.<\/p>\n<figure id=\"attachment_2561\" aria-describedby=\"caption-attachment-2561\" style=\"width: 272px\" class=\"wp-caption alignright\"><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/20-PIN-con.jpg\" rel=\"attachment wp-att-2561\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2561\" src=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/20-PIN-con-300x227.jpg\" alt=\"ARM 20-pin connector\" width=\"272\" height=\"206\" srcset=\"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/20-PIN-con-300x227.jpg 300w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/20-PIN-con-150x114.jpg 150w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/20-PIN-con-24x18.jpg 24w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/20-PIN-con-36x27.jpg 36w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/20-PIN-con-48x36.jpg 48w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/20-PIN-con.jpg 600w\" sizes=\"auto, (max-width: 272px) 100vw, 272px\"><\/a><figcaption id=\"caption-attachment-2561\" class=\"wp-caption-text\">Pin map of the ARM 20-pin connector.<\/figcaption><\/figure>\n<p>There are different JTAG connectors but almost everyone contains these PINs:<\/p>\n<ol>\n<li>TDI, or Test Data In;<\/li>\n<li>TDO, or Test Data Out;<\/li>\n<li>TCK, or Test Clock;<\/li>\n<li>TMS, or Test Mode Select;<\/li>\n<li>TRST, or Test Reset which is optional.<\/li>\n<\/ol>\n<p>As example in Fig.1 we can see a <b>ARM 20-PIN JTAG connector<\/b> with the pin-map. The connector shown in figure is the same available on the STLink v2 and we are going to use it and certain PINs (the ones orange and azure) have a dual function.<\/p>\n<h4 id=\"4_ARM_Serial_Wire_Debug\" class=\"level_2\">ARM Serial Wire Debug<\/h4>\n<p>The <b>SWD<\/b> also know as <i>Serial Wire Debug<\/i> is an interface developed by <i>ARM<\/i> and is an alternative to JTAG for these packages with limited PINs. SWD uses a standard bi-directional wire protocol defined in the <i>ARM Debug Interface v5<\/i>.<br>\nSWD replaces the 5-pin JTAG port with a clock + single bi-directional data PIN having data rate up to 4 MB\/s at 50 MHz. Additional features could be achieved using one more PIN named TRACESWO.&nbsp;On JTAG devices with SWD capability:<\/p>\n<ol>\n<li>TMS, is used as SWDIO;<\/li>\n<li>TCK, is used as SWCLK;<\/li>\n<li>TDO, is used as SWO.<\/li>\n<\/ol>\n<h4 id=\"5_Connecting_the_STLink_v2\" class=\"level_2\">Connecting the STLink v2<\/h4>\n<figure id=\"attachment_2560\" aria-describedby=\"caption-attachment-2560\" style=\"width: 176px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/10-PIN-con.jpg\" rel=\"attachment wp-att-2560\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2560\" src=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/10-PIN-con-300x135.jpg\" alt=\"ARM 10-pin connector\" width=\"176\" height=\"79\" srcset=\"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/10-PIN-con-300x135.jpg 300w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/10-PIN-con-150x68.jpg 150w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/10-PIN-con-24x11.jpg 24w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/10-PIN-con-36x16.jpg 36w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/10-PIN-con-48x22.jpg 48w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/10-PIN-con.jpg 600w\" sizes=\"auto, (max-width: 176px) 100vw, 176px\"><\/a><figcaption id=\"caption-attachment-2560\" class=\"wp-caption-text\">Fig.2 &ndash; Pin map of the ARM 10-pin connector.<\/figcaption><\/figure>\n<p>The STLink v2 supports both JTAG and SWD interfaces but STM32 uses SWD only. Let&rsquo;s see how to connect the STLink to our Clicker 2 using some <b>Dupont wires<\/b>. The schematic of STLink v2 JTAG\/SWD connector is shown in Fig.1. Opening the Clicker 2 For STM32 User Manual we can notice that this board uses a <b>ARM 10 PIN JTAG Connector<\/b> which is shown in Fig.2.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_2528\" aria-describedby=\"caption-attachment-2528\" style=\"width: 235px\" class=\"wp-caption alignright\"><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/Connections.jpg\" rel=\"attachment wp-att-2528\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2528\" src=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/Connections-300x206.jpg\" alt=\"STLink V2 and Clicker 2\" width=\"235\" height=\"161\" srcset=\"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/Connections-300x206.jpg 300w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/Connections-150x103.jpg 150w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/Connections-1024x704.jpg 1024w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/Connections-1200x825.jpg 1200w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/Connections-24x16.jpg 24w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/Connections-36x25.jpg 36w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/Connections-48x33.jpg 48w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/Connections.jpg 1571w\" sizes=\"auto, (max-width: 235px) 100vw, 235px\"><\/a><figcaption id=\"caption-attachment-2528\" class=\"wp-caption-text\">Connection between STLink V2 and Clicker 2 for STM32.<\/figcaption><\/figure>\n<p>We just need to connect 4 wire between STLink and Clicker 2 to establish a communication. A feasible connection (STLink to Clicker) could be:<\/p>\n<ol>\n<li>SWDIO, 2 to 1;<\/li>\n<li>SWCLK, 4 to 9;<\/li>\n<li>VCC, 7 to 2 ;<\/li>\n<li>GND, 9 to 4.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h3 id=\"6_How_to_unlock_and_erase_Flash_Memory\" class=\"level_1\">How to unlock and erase Flash Memory<\/h3>\n<p>The embedded flash memory of the <b>STM32F4<\/b> has the Read\/Write protection and it can be activated acting mainly on the FLASH_CR register. It seems like the latest version of <b>mikroe Bootloader<\/b> enables this protection so we need to variate a little bit the flash and run procedure to unlock the flash.<\/p>\n<figure id=\"attachment_2531\" aria-describedby=\"caption-attachment-2531\" style=\"width: 293px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/MSYS2_telnet.jpg\" rel=\"attachment wp-att-2531\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2531\" src=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/MSYS2_telnet-300x166.jpg\" alt=\"Using OpenOCD over Telnet\" width=\"293\" height=\"162\" srcset=\"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/MSYS2_telnet-300x166.jpg 300w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/MSYS2_telnet-150x83.jpg 150w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/MSYS2_telnet-24x13.jpg 24w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/MSYS2_telnet-36x20.jpg 36w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/MSYS2_telnet-48x27.jpg 48w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/MSYS2_telnet.jpg 583w\" sizes=\"auto, (max-width: 293px) 100vw, 293px\"><\/a><figcaption id=\"caption-attachment-2531\" class=\"wp-caption-text\">Using OpenOCD over Telnet.<\/figcaption><\/figure>\n<p>We will use a command prompt like <b>MSYS2<\/b> (or even <b>Windows CMD<\/b>) and <b>OpenOCD<\/b>. Note that we already have a copy of OpenOCD inside ChibiStudio and that we have already used MSYS2 in <a href=\"https:\/\/playembedded.org\/en\/2015\/10\/11\/building-openocd-under-windows-using-msys2\/\" target=\"_blank\" rel=\"noopener noreferrer\">this article<\/a>.<\/p>\n<p>Taking stock of the situation, we will launch OpenOCD making some additional operations and we will perform a classical Flash and Run procedure. We need for a working project for Clicker 2 and we will use that from the <a href=\"https:\/\/playembedded.org\/en\/2015\/12\/17\/getting-started-with-mikroe-clicker-2-for-stm32-with-chibios\/\" target=\"_blank\" rel=\"noopener noreferrer\">previous tutorial<\/a>. First step would be launching the OpenOCD and we can do this directly from ChibiStudio, into the External Tools menu choosing &ldquo;OpenOCD on ST-Link V2 (prompts for .cfg target configuration)&rdquo; and selecting the configuration file &ldquo;C:\\ChibiStudio\\tools\\openocd\\scripts\\target\\stm32f4x.cfg&rdquo;.<\/p>\n<p>At this point, keeping ChibiStudio opened, we have to launch the MSYS2 bash. We will use it to force the memory unlock launching some OpenOCD commands through Telnet (See Fig.4). First command establish a Telnet communication on the localhost on port 4444:<\/p>\n<pre class=\"\">telnet localhost 4444<\/pre>\n<p>Once the communication has been established we have to issue a halt command to the Target to break the program execution.<\/p>\n<pre>reset halt<\/pre>\n<p>Now we can use the official unlock procedure for STM32. Don&rsquo;t worry about the command name because the flash controller is the same in both STM32F2 and STM32F4. This procedure can take some seconds and is completed only when bash replies with the sentence &ldquo;stm32f2x unlocked&rdquo;: be patient and wait.<\/p>\n<pre class=\"\">stm32f2x unlock 0<\/pre>\n<p>Push the reset button on the board before to proceed. Now we have to rewrite the value of the FLASH_OPTCR field into the FLASH_CR register to unlock the memory. This requires a special procedure described into the STM32F4 Reference Manual. Note that these commands will not produce any output on the command line:<\/p>\n<pre>mww 0x40023C08 0x08192A3B; mww 0x40023C08 0x4C5D6E7F; mww 0x40023C14 0x0fffaaed<\/pre>\n<p>Now we can come back to ChibiStudio and perform the flash and run procedure which will be successful.<\/p>\n<h3 id=\"7_Restoring_the_mikroe_Bootloader\" class=\"level_1\">Restoring the mikroe Bootloader<\/h3>\n<figure id=\"attachment_2533\" aria-describedby=\"caption-attachment-2533\" style=\"width: 221px\" class=\"wp-caption alignright\"><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/STlink-utility.jpg\" rel=\"attachment wp-att-2533\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2533\" src=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/STlink-utility-300x210.jpg\" alt=\"STlink utility\" width=\"221\" height=\"155\" srcset=\"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/STlink-utility-300x210.jpg 300w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/STlink-utility-150x105.jpg 150w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/STlink-utility-24x17.jpg 24w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/STlink-utility-36x25.jpg 36w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/STlink-utility-48x34.jpg 48w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/STlink-utility.jpg 631w\" sizes=\"auto, (max-width: 221px) 100vw, 221px\"><\/a><figcaption id=\"caption-attachment-2533\" class=\"wp-caption-text\">A screenshot of the STLink utility.<\/figcaption><\/figure>\n<p>To restore the mikroe Bootloader we need to download it from the <a href=\"http:\/\/www.mikroe.com\/stm32\/clicker-2\/\" target=\"_blank\" rel=\"noopener noreferrer\">mikroe website<\/a>. It is included with the package of the <a href=\"http:\/\/www.mikroe.com\/downloads\/get\/2153\/clicker_2_stm32_bootloader.zip\" target=\"_blank\" rel=\"noopener noreferrer\">USB HID Bootloader<\/a> under the folder &ldquo;firmware&rdquo;. It is a simple .hex file that could be flashed using both OpenOCD or <a href=\"http:\/\/www.st.com\/web\/en\/catalog\/tools\/PF258168\" target=\"_blank\" rel=\"noopener noreferrer\">ST Link Utility<\/a>.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this tutorial we are going to see how to connect a STLink v2 debugger to a Clicker 2 for STM32 enabling users to do debugging which could be very useful in development phases. A debugger is a computer program which is used to test and debug another program usually named Target. In embedded [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":2234,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1221],"tags":[],"coauthors":[241],"class_list":["post-2128","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-further-readings","pumpkin"],"views":23996,"jetpack_featured_media_url":"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/03\/tut008.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/posts\/2128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/comments?post=2128"}],"version-history":[{"count":0,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/posts\/2128\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/media\/2234"}],"wp:attachment":[{"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/media?parent=2128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/categories?post=2128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/tags?post=2128"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/coauthors?post=2128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}