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

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

S32K1xx Automotive MCU course( Intermediate)

DAC PDB MTB(Micro Trace Buffer) Miscellaneous Control Module(MCM) System Integration Module Crossbar Switch Lite Memory Protection Unit Peripheral Bridge Trigger MUX Control External watchdog Monitor Error Injection Module Error reporting module CRC Watchdog timer DAC Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo. PDB Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo. MTB(Micro Trace Buffer) Tab Content Miscellaneous Control Module(MCM) Tab Content System Integration Module Tab Content Crossbar Switch Lite Tab Content Memory Protection Unit Tab Content Peripheral Bridge Tab Content Trigger MUX Control Tab Content External watchdog Monitor Tab Content Error Injection Module Tab Content Error reporting module Tab Content CRC Tab Content Watchdog timer Tab Content CAN FlexIO LIN CAN Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo. FlexIO Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo. LIN Tab Content Author: Kunal Gupta

Read More »
ARM processors
Kunal Gupta

Debug and Trace: ARM CORESIGHT ARCHITECTURE

Table of Contents Other Processors to know about Overview In my Last Blog We talk about Trace and Debug Features in Firmware development. What role does they play and their importance in Developing Firmware for any project and Product can be of Great Importance. Now If you are an enthusiastic engineer just like me. You might be interested in how Debug and Trace things are working and implemented in our Embedded Controllers. Like Our CPU processors are running at higher frequency of more than 10 MHz (Just to tell Some even run at more then 100-200 MHz). That is, they can perform 1000000 instructions in just a single second!!!! That is fast and insane. As I have mentioned in Previous Blog about the Debugging Features, one can execute the instructions line by line. By that I mean that one can execute 1 instruction at a time. Just imagine how fast it executes 1 instruction and one can control those executions which are happening in ns,ms,us. Doesn’t this excite you; how does that happen????????  Or if We talk about Setting Breakpoint and then halting the processor, like it means when the CPU is executing 10000000 instructions per second. One can stop Our Machine that is running at the speed of MHZ at any Particular Instruction!!!! If talking About Trace Features, it tells about our code Performance and efficiency. So, generating data like How much time does the instructions take, time for their execution, space used by them and the effect of them in our Code and Performance. Does it not excite and make you think that how actually all these things are happening inside our MCU Core Processor. And remember guys all such things are happening at a time delay of ms, ns, us. That is fast and insane. So, telling You Answer for these Questions, As I have mentioned in My previous Blog also that Trace and Debug is just like a Peripheral in our Core Processors. And they have their Own Circuitry, Components, Communication Protocol, Communication Pins, Communication Busses and Own Programmable Registers for configuring Which Debug and tracer features to use and setting them up. In the case of ARM Processors This Peripheral is designed by the name of CoreSight Architecture. Now it has a number of components like ITM, DWT, STM, DAP etc. which collectively provides features of debug, Trace, Timestamp, Prolific Counters and many more. CoreSight Debug Architecture “The ARM Cortex M/R/A processor uses the CoreSight for on-chip Debug and Trace capabilities.” CoreSight Architecture is designed in a very modular way which has Number of Components and Units providing debug and trace solutions with high bandwidth for whole systems, including trace and monitor of the system bus. These Components can be categorized in following Sections: Control and Access Components à Configure, access, and control the generation of trace/Debug. They do not generate traces, nor process the trace data.    Debug Access Port (DAP)    Embedded Cross Trigger (ECT) Trace Sources-> Components which generate trace data.    Embedded Trace Macrocell (ETM)    Instrumentation Trace Macrocell (ITM)    AHB Trace Macrocell (HTM)    Flash Patch and Breakpoint unit (FPB)    Data Watchpoint and Trace Unit (DWT)    Program Trace Macrocell (PTM)    System Trace Macrocell (STM)    Embedded Logic Analyzer (ELA)   Trace Sink -> Sinks are the endpoints for trace data on the SoC.     Trace Port Interface Unit (TPIU)     Trace Memory Controller (TMC)     Serial Wire Output (SWO)     Embedded trace Buffer (ETB) Trace Links-> provide Connection, triggering and flow of traced data between Source and Sink.     Trace Funnel     Trace Replicator     ATB Bridge Debug and Trace Buses à The CoreSight systems use the following bus protocols to connect components together, and to enable integration in a SoC.     AMBA Trace Bus (ATB).     AMBA 3 Advanced Peripheral Bus (AMBA 3 APB).     Advanced High-Performance Bus (AHB).     AMBA Advanced extensible Interface (AXI) ARM licenses the different Components of CoreSight Architecture to the SoC manufacture vendors.According to the ARM Processor these Components vary like in Cortex -A72, Trace Source components ELA and STM are there whereas in Cortex M3/M4, Trace Source Components ELA and STM are not included in its Architecture.Same way-out Trace Sink Components also vary from ARM Processor to processor.Depending on these Units and Components used by vendors for their Processor design there can be changes in features available for trace and Debug. Debug Access Port (DAP) Enables the Debug Access between SoC and Host Debugger. Debug Ports are used to access External Debugger and Access Ports are used for on chip System Resources. Debug Port (DP) JTAG and SWD (+SWO pin – for trace) are the communication Protocols which can be used for Debug/Trace. Now for Connecting and having communication with MCU core to the host debugger (Stlinkv2), we need special Port (I/O pins) on the MCU as these protocols work at very high bandwidth and are directly in play with the Processor. This port is called Debug Port. There are 3 Debug port modules which are available in all ARM cortex M processors:    SWJ-DP (Serial wire JTAG Debug Port) à Supports both Serial Wire and JTAG protocols    SW-DP (Serial wire Debug Port) à Supports only Serial Wire Protocol.    JTAG-DP (JTAG Debug Port)à Supports only the JTAG Protocol, available in older generations of ARM Processor and almost every Processor. Debug and Trace Buses & Access Port (AP) Access Port is a Port that connects the DP and the Components of the CoreSight Architecture. The CoreSight systems use the following bus protocols to connect components together, and to enable integration in a SoC. AMBA Trace Bus (ATB-AP) AMBA 3 Advanced Peripheral Bus (AMBA 3 APB) Advanced High-Performance Bus (AHB-AP) AMBA Advanced extensible Interface (AXI-AP) ARM Cortex M3/M4 Advanced High-Performance Bus (AHB-AP) is used for internal debug bus protocol to connect different Debug Components together. Embedded Cross Trigger The ECT is a modular component that supports the interaction and synchronization of multiple triggering events within a SoC. There are

Read More »

Introduction to Atmel studio

I assume if you are here to do projects on Atmel studio using embedded c then you are familiar with Arduino IDE. If not then I would recommend you to get familiar with  Arduino IDE because it will build a basic foundation you need to get started. Arduino IDE will make it easy for you to relate variables, functions, etc. How to execute our first code on Atmel studio These are the steps you need to follow to run your first code. Open your Arduino IDe and copy the path I’ve shown below. For this, you need to scroll up. At the top, you will find it. Copy that path and paste it somewhere as I have pasted it in notepad++. You can see I’ve made a few changes in that path you need to do the same.  Now go to tools and open external tools. Here writes the name of the Board you are using as I am using Arduino Uno. Paste the commands in the command section and arguments in the arguments section after making the necessary changes and then click OK.  Now Go to file and create a New Project. Write the name of your program and select the compiler I’ve selected. After selecting the compiler press OK. Here you can see numerous microcontrollers. Now you need to select the one you are using. The microcontroller I am using is Atmega328p. Instead of searching, you can also write it in the search box. It is at the top right corner. Great! Your main.cpp file is open and nowhere you can start programming and making super cool projects. After writing your first program click on build -> build solution. Go to tools and select the External tool title you have saved in point 5). Here to run this code you just need to plug in your Arduino board and make a circuit for blinking LEDs, the same as you must have done while working on Arduino IDE. For reference, you can see the output. CODE #define F_CPU 16000000UL #include <avr/io.h> #include <util/delay.h> int main(void) { DDRB|= (0B00100000); while (1) { PORTB=0B00100000; _delay_ms(1000); PORTB=0B11011111; _delay_ms(1000); } return(0); } /* This is just a demo code so that you can confirm that your code is running properly. For now, you don’t need to worry about the code or what is happening inside. I will share the link down below for the same. */ OUTPUT   Author: Kunal Gupta

Read More »
Kunal Gupta
Author: Kunal Gupta

Author

Kunal Gupta

Leave a comment

Stay Updated With Us

Error: Contact form not found.

      Blog