Programing external STM Microcontroller via STM32F4xx Evaluation board
You have been playing with your newly bought STM43F4-Discovery board for a while and got a good grasp of all its peripherals. Now you are planning to build your own custom PCB/Product based on ARM Cortex-M based STM Microcontroller but you are worried that you will have to spend that extra money to buy a compatible programmer/debugger like JTAG, ST-Link, ULINK, JLINK etc. for programming and debugging your custom PCB/Product!
Buying these products is not a problem for a big company but it can be for a hobbyist like you :). It is worth mentioning that these interfaces though a bit expensive do come with some advance features like program and data tracing so you may need to consider them in case if you need these advance features.
Similarly; you have just bought your low-power ARM Cortex-M based STM minimum development kit like STM32F103C8 – Figure-1 that doesn’t come with a programmer/debugger on-board but do have a JTAG/SWD interface. If these are the cases with you, I have a good news for you! You don’t need to buy these expensive interfaces just to program and debug your ARM Cortex-M based STM MCUs. Your existing STM32F4-Discovery board provides you the facility to debug and program STM MCUs based external boards.
The following Figure-2 shows STM32F4-Discovery board. Physically the board can be divided into two distinct portions.
The upper portion is the ST-Microelectronics developed protocol to program and debug ARM Cortex-M based ST MCUs over SWD interface. This portion acts as programmer/debugger for the lower portion i.e. STM32F407VG Microcontroller. The lower portion is actually the one you mean when you were buying the board.
As shown in Figure-2, the upper portion contains two jumpers labeled Discovery on one side and ST-Link on other side. These are the jumpers that actually connects the upper portion (on-board ST-Link programmer/debugger) with the lower portion STM32F407VG MCU. If these jumpers are removed, the upper portion i.e. ST-Link is isolated from the rest of the board and the signals are generated on left most header pins labeled SWD. So this is where the magic is i.e. If you want to program the STM32F4-Discovery you need to place these jumpers and if you want to program external boards, you have to remove these jumpers and connect your external board with the SWD header pins – Figure-3.
1. Physical Connection:
Before we explain the background theory let’s do the actual connection and leave the theory for the end. If your target board is ARM Cortex-M based it must have a support for either JTAG or SWD or both with pins multiplexed (see the back end theory at the end).
Before moving ahead make sure you have removed the right side jumpers on STm32F4-Discovery board to isolate the ST-Link interface.
Case-1 – Target board with SWD port:
This is one of the easiest configuration, In this case all you have to do is just to connect the pins as bellow.
- SWCLK → SWCLK/DCLK
- GND → GND
- SWDIO → SWDIO/DIO
- NRST → NRST (If available)
- SWO → SWO (If available)
Note: You may need to connect 3.3V line as well in case if the target board is not external powered.
Case-2 – Target board with JTAG port:
JTAG port comes with different no of pins ranging from 10 to 20 as shown in Figure-5. Though i believe ARM based processors comes with 10 and 20 pins JTAG support only.
Whatever the case is, we are interested in physical connections between SWD and JTAG. In order to connect SWD with JTAG, following are the pin to pin connection.
- 3.3V → VCC
- SWCLK → TCLK
- GND → GND
- SWDIO → TMS
- NRST → RESET
- SWO → TDO
The following figure shows SWD connection to 20-pins JTAG connector.
2. Background Theory:
The ARM Cortex-M family of processors debug technology supports two types of interface connections i.e. either SWD or JTAG. The availability of one or both depends both on the specific ARM Cortex-M and silicon vendor. The IEEE 1149.1 called JTAG is a serial protocol initially developed for verifying inter-integrated circuits connectivity (called Boundary Scan) on highly dense PCBs. The JTAG serial protocol has been extended by adding facility to interact with debug hardware on many MCUs as this was the only interface available to embedded ICs by that time.
The only problem with JTAG is that it at least requires 4-pins i.e. TMS, TCLK, TDO, TDI. For some MCUs like 28-pins package, dedicating 4-pins (plus other optional pins ranging from 10 to 20) are too many. To tackle this issue, ARM developed its own debug protocol for debugging and tracing. The protocol is called Serial Wire Debug (SWD) which requires only two pins i.e. SWCLK, SDIO. A third pin called SWO is an additional pin used to emit trace information like printf etc. and is added to SWD. Collectively they are called Serial Wire Viewer (SWV).
To keep legacy support and to cover CoreSight debug enrich features, two wires are normally in-sufficient. So ARM kept options for both i.e. SWD and JTAG in CoreSight and left the implementation to the silicon vendors. So for basic debug features like halting, single stepping, breakpoints, watchpoints, and Software event tracing; SWD is more suitable. In order to use more advance features like instruction tracing etc. JTAG interface is used which has enough pins to be used for extra functionalities.
Some silicon vendors keep support for both implementation in their SoCs. In this case the pins of Serial Wire Debug (SWD) and JTAG are multiplexed. i.e. TCK with SWCLK and TMS with SDIO. Only one is active a time… which one??? The one that is selected by applying special bit patterns on TMS/SWDIO pin. And that’s why we can use SWD to JTAG connection to program and debug target Hardware.
 – Reference manual-RM0090