Embedded MCU's Getting Started and Peripheral Coding STM32 MCU's

How to Use Trace Features in STM32 with STLINKv2 in CubeIDE

Table of Contents

Overview

Okay so hello guys and hope u are doing great; in the last 2 Blogs we get to know about Debug Trace Features in Embedded Systems and about ARM CoreSight Architecture for Debug and Trace in ARM based SoC/MCU.

Now, in this Blog we will implement the theoretical knowledge of previous blogs to action and do them practically! We will do so by exploring the Trace Features in STM32MCU practically (STM32F103 Blue pill) using STLINKv2-B ( Official one white one) in STM32CubeIDE. 

This Topic has been divided into 2 Blogs.

In this Blog Hardware connections between STLINKv2-STM32MCU  and Code Generation is explained. The Video For that is also Uploaded on Gettobyte YouTube channel here.

In next Blog How to use Trace Features using SWV is explained. The Video for that too is Uploaded on Gettobyte YouTube Channel here.

Intro on STLINKv2 Programmers and Debuggers

Now STLINKv2 which you can see in the above image is different from USB based STLINKv2 Programmer and debugger( Chinese clone one). It is different in terms of more features and JTAG support for Debugging the Firmware’s. 

Original STLINKv2 can be used for Programming and Debugging Both STM8 and STM32 MCU. For STM8 it uses SWIM Protocol and for STM32 it has SWD, JTAG Communication Protocol and SWO pin for trace features.

In USB based STLINKv2 Programmer Chinese cloned one also both STM8 and STM32 MCU can be programmed and debugged. For STM8 it also uses SWI Protocol but for STM32 MCU it has only SWD Protocol and pins available.

For using the trace features, In USB based Chinese cloned one STLINKv2 (which is relatively cheaper than above STLINKv2) we must solder the 5V pin of Programmer by one of the pins of the onboard pin of the MCU used in Programmer. And it is a kind of long and Cumbersome Process. 

But in Official STLINKv2 which is shown in above pic, we do not have to solder any of the pins for using Trace Features, The Programmer has Pinout and dedicated pins for using Trace feature using (SWD + SWO pins) and it has JTAG Dedicated pins too for Doing Debugging .

Now as given u some comparisons between Official and Unofficial STLINKv2 Programmer and Debugger let us dive into some Practical Part. In the rest of the doc,  STLINKv2 programmer and Debugger will signify the Official white in one derivative which is shown in above pic.

Hardware Connections for STLINKv2 and MCU

The STLINKv2 Programmer comes in with following things as u open its Box.

  • Main STLINKv2 Programmer and Debugger
  •  
  • 20 Pin JTAG connector
  • USB 2.0 A to USB 2.0 Mini B
  • 2 x Connecting Cable for STM8 SWIM

Now coming to the pain Connections for connecting STM32 MCU with STLINKv2.
Programmer has the following pins for connecting it to STM32 MCU(Blue Circle).

These Pins have Pinout like these:

STLINKV2 Pinout

Right side from the Embarked Area (Orange Color), is pin 1 of the Programmer and then the pinout goes as shown in above pic.

Now the Essential Pins out of these 20 pins of the STLINKv2 programmer to Program and Debug STM32 MCU using ARM Proprietary SWD Protocol are only 5 pins.

  • SWIO/TMS(Pin 7) à SWD PIN
  • SWCLK/TCK(Pin 9) à SWD PIN
  • TDO/SWO(Pin 13) à SWO PIN
  • STM32 RESET(Pin 15) à RESET PIN
  • VCC(Pin1&2) à POWER PIN
  • GND(Below Horizontal line’s any pin can be used as GND except 2) à POWER PIN
STLINKV2 Connection for STM32

SWD Pins of STM32 MCU will be connected to SWD PIN if we want to only use Debugging features alongside Programming the Microcontroller. Like as shown in PIC. 

Now for Using the Trace Features we must connect one additional Pin from the Programmer to OUR Microcontroller and that is SWO Pin (13th pin) of Programmer to the Respective Trace SWO Pin Of our STM32 Microcontroller.

In STM32F103 SWO PIN is PB3 as shown in this schematic.

So, for Using Trace Features Connections will be like this.

SWO Connection for STM32

To be noted down:

  • Any pin from the below horizontal line except the VCC pin of that line can be taken as GND pin when using SWD/SWD+SWO pins and can be connected to GND pin of our STM32 MCU.
  •  Sometimes or in some Programmers we have to short all the VCC pins by Jumper wire for Proper connections or encase device isn’t able to power up MCU or Program it.
  •  In some cases, VCC pins 1&2 can be wrongly mapped in Some Programmer, and they can be at left hand side of the programmer that is at pins 19 and 20. So in case MCU does not Power up by above connections, then do not panic just change the VCC Pins and it might work out. ( This is in my case, so below pitcher is according to that)

Now so after successful Connections between STLINKv2 and STM32 MCU, Connect the USB cable between STLINKv2 and Laptop and LED light on the programmer will lighten up in RED color.

         Figure 5 (These are the Final Connections For SWD+SWO)

Software Part used

Now I will be Showing STM32CubeProgrammer and STM32Cube IDE In this blog in software part.

First with STM32CubeProgarmmer:

As u connect your STLINKv2 Programmer to your computer, IN STM32CubeProgrammer u will see the Serial Number of Programmer if all goes well. As shown in Pic.

Figure 6

Now as u click on Connect Button by selecting STLINK, u will see all the details of Your respective MCU details and showing its memory Content. (In doing so Your programmer will start blinking in red, green color and Screen of STM32CubeProgrammer might take 1-2 secs of time for showing Memory contents and MCU details.)

Figure 7

This is just Demonstration of Successful connections of STLINKv2 and MCU for SWO+SWO pins and established Communication between Programmer and Laptop via STM32Cube Programmer.

As if now, we have not used the trace features, for using that we must create a project and Upload in MCU by things mentioned in below section.

For STM32CubeIDE also follow the below section

If any Error comes in while connecting the STLINKV2 , FOLLOW  this video for solving all kinds of Errors and to get Solutions for them.

Code for using Trace Features in STM32 MCU

Create a New Project in STM32CubeIDE by choosing your corresponding Workspace (Figure 1), select Your MCU in Board Configuration Window (Figure2) by selecting the MCU you are using.

Figure 8

\"\"

Figure 9

Now name Your Project in next window and choose the targeted language according to your choice and Select Executable in Targeted Binary Type.

In Target Project type their will be 2 Options,

STM32Cube and Empty.

(In this Blog I will be telling things according to STM32cube project type. By selecting STM32Cube, Auto generated code STM 32 HAL initializes all the Processor registers for using Debug and Trace features according to the configurations made in STM32CubeMX window.)

Now in Cube MX configuration window we must Select RCC Mode for HSE as Crystal/Ceramic Resonator and Debug Mode as Serial Wire,

Under Pinout and Configuration window select the System Core and in it Select the RCC and SYS. Select the corresponding Options as shown in pics below.


Figure 10

And then in the clock Configuration window Select the Clock Frequency according to your choice. I am selecting 32Mhz in my case for STM32F103 using HSE (which has input frequency of 8 MHz and selecting PLL as clock Source for System clock).

I am configuring the Onboard LED on STM32F103 Blue Pill as GPIO OUTPUT just for demonstration purpose. That is pin PC13.

Figure 11

And then save the file and Autogenerated code Process using STM32HAL will be started.

Figure 12

Now open the main.c file of Your project from the Project explorer window.

Figure 13

That is all for this Blog, second part of the Topic is in next Blog.

Follow the next Part for NEXT Part of the blog.

Playlist for Debugging techniques in Firmware development: https://www.youtube.com/playlist?list=PLb_Q-Ps0nJou2Ped93q02JRfiMCJorLYX

Previous and Next Blog

Other blogs to explore

Silicon Labs Thunderbolt EFM32BG22

IDE To be used: Simplicity Studio – Silicon Labs (silabs.com) SDK to be used: Gecko Platform Silicon Labs (silabs.com) SDK documentation: EFR32BG22 Gecko MCU and Peripheral Software Documentation – v5.9 – MCU EFR32BG22 API Documentation Silicon Labs (silabs.com) HDK to be used:SLTB010A EFR32BG22 Thunderboard Kit – Silicon Labs (silabs.com) HDK manual: UG415: Thunderboard EFR32BG22 User’s Guide (silabs.com) Microcontroller datasheet: EFR32BG22C112 Wireless Gecko SoC Data Sheet (silabs.com) Microcontroller Reference manual: EFR32xG22 Wireless Gecko Reference Manual (silabs.com) Author: Kunal Gupta

Read More »

HLW8012 AC Energy metering IC: Part 1

Table of Contents So hello guys, welcome back to Gettobyte Once again. Todays blog is going to be on interfacing the Energy metering IC : HLW8012 which is manufactured by HLW Technology.On previous blogs we have developed the application driver   for W25Q SPI based flash memory’s, is time we are going to develop the Application driver for HLW8012. So first question that must come in your mind is that what is Energy Metering IC. Lets first Understand this term. What are Energy Metering Integrated Chips ? Before Understanding Energy metering IC’s their is one term that i would like to get familiar my viewers that is ASIC’s. ASIC’s are Application Specific Integrated Chips i.e. chips which are designed, developed and fabricated for particular applications.  Energy Metering IC’s are one of the ASIC’s. In this case the specific application is that of Electrical Energy Measurement. By the use and emergence of Energy Metering IC’s we can calculate the Electrical Energy parameters like RMS/Instantaneous Voltage, RMS/Instantaneous Current, Reactive/Active/Apparent Power, Energy Consumed, Power loss/dissipation, Power factor, Frequency digitally. Using this electrical Energy Measurements only our monthly electrical energy bill is calculated. These electrical energy parameters play very important role for monitoring and analyzing of Power consumption in our homes, offices, factories, city’s and all such infrastructures where their is use of electricity. These Ic’s are widely used in Energy Meters, Multifunction Meters, Smart Energy meters, Solar Energy control panels, Factory Automation Panels and etc. Energy metering IC’s are designed using the knowledge of ADC, Opamps, Low pass/High pass filters, use of Analog Electronics with Power Electronics, DSP(Digital Signal processing), Signal Conditioning circuits. All these circuits are designed inside the Energy Metering IC’s using VLSI/VHDL. That is where the concept of Application Specific comes in. So when reading the datasheet of Energy Metering IC’s some overview about these terms and terminologies would be beneficial for understanding the functioning of these IC’s. Voltage and current drive the world in every sense. Using these 2 parameters we can calculate all other electrical energy parameters be it reactive power, active power, energy, frequency, power factor and etc. Energy Metering IC’s primarily measures/senses only Voltage and Current by the use of Signal conditioning circuits, ADC, Opamps, Analog Electronics. Further these IC’s calculates all other Electrical energy parameters using DSP, Low pass/High pass filters and etc. All of these things are designed inside the Energy metering IC’s. Input voltage and current to these IC’s are fed depending upon the principal which we are using for sensing Voltage and current. After feeding the Input to these IC’s via one of the above principals, comes the role of above mentioned circuits/units which are designed inside the chips for their efficient functioning and digital accurate readings. Energy metering IC’s which are widely used are of The above listed metering IC’s and their development boards are very expensive, so for my use case i have found out some cheap/alternative Energy Metering IC’s, but their manufacturer are Chinese companies: STMicroelectronics Metering ICSTMP32/33/34 seriesClick HereTexas Instruments Metering ICsClick HereAnalog DevicesADE seriesClick HereNXP semiconductorsClick Here Previous Next HLW8012( the one we are using this blog) HLW8032 BLO937 PZEM-004T module  CS5460 bi-directional energy meter module This is the First string, Second string, Third string of the sentence. General Description of HLW8012 Energy Metering IC HLW8012 is a single phase energy metering IC, which is based on shunt resistor based principal. The HLW8012 module has on board 1 milli-ohm copper-manganese shunt resistor, which acts as a shunt resistor and used to sense the voltage and current. One of the biggest problem with IC, is its datasheet. The datasheet of this IC is in Chinese language, so likely it would be difficult to understand from it. The IC can measure the RMS Voltage, RMS Current, Active power and Outputs these reading digitally in the form of PWM signals. HLW8012 is a good choice for making small and hobbyist kind of projects which requires the measurement of AC electrical parameters. One such project/product that I found, is made on HLW8012. HLW8012 is inexpensive and good alternative to be used in comparison to  other expensive metering IC’s. The module of HLW8012 is very handy and easy to use. It has 4 input terminals: 2 terminals are connected with AC Load and other 2 terminals are connected with AC Power supply. HLW8012 Outputs the Readings of Voltage, Current and power via PWM signals of 50 percent duty cycle, Their are two pins in HLW8012 CF and CF1 which outputs the voltage, current and power readings in a digital PWM square signal, which host MCU can capture using Timer Input Capture mode. The readings are directly proportional to the Frequency of the PWM signal. Every modern MCU has Timer peripheral, which we can configure in Timer Input capture mode. In this mode Host MCU can capture the PWM signals from external world (here our external world is HLW8012). That’s how HLW8012 will communicate with the Host MCU. Features of HLW8012 Energy Metering IC Based on shunt resistor based principal. Can measure current upto 20Amps and Voltage upto 300 Volts Easy interface to Host MCU sends voltage, current and power readings digitally via PWM square signals. Needs +5 volt to operate the IC. Block Diagram of HLW8012 Metering IC PINOUT of the HLW8012 Energy metering IC Pin Number 1 Pin number 1 is VCC (Chip Select), which is used to operate the power the HLW8012 IC. Connect this pin to +5 Volts of the host MCU. Pin Number 2 & 3 Pin number 2 &3 are V1P/V1N, which are Current differential input pins. These pins are connected in parallel to the shunt resistor(See the application circuit below). This connection is already made on the Module of HLW8012. The maximum voltage that can be input to these pins are 43.75mV. Pin Number 4 Pin number 4 is V2P, which is Voltage differential input pin, this pin is connected to  input AC voltage via resistor network of 47k ohms Pin Number 5 Pin

Read More »
Kunal Gupta
Author: Kunal Gupta

Author

Kunal Gupta

Leave a comment

Stay Updated With Us

Error: Contact form not found.

      Blog