Bird
Raised Fist0
Embedded Cprogramming~5 mins

JTAG and SWD debug interfaces in Embedded C

Choose your learning style10 modes available
Introduction

JTAG and SWD are ways to connect a computer to a microcontroller to check and fix the program while it runs. They help find mistakes and control the device easily.

When you want to find and fix bugs in your microcontroller program.
When you need to load new code into a microcontroller without removing it.
When you want to watch how your program runs step-by-step.
When you want to test hardware connections on a circuit board.
When you want to control the microcontroller from a computer during development.
Syntax
Embedded C
// Example of initializing SWD interface in embedded C
void init_swd(void) {
    // Configure SWD pins
    SWDIO_PIN = OUTPUT;
    SWCLK_PIN = OUTPUT;
    // Enable SWD clock
    ENABLE_SWD_CLOCK();
}

// Example of using JTAG interface
void init_jtag(void) {
    // Configure JTAG pins
    TCK_PIN = OUTPUT;
    TMS_PIN = OUTPUT;
    TDI_PIN = OUTPUT;
    TDO_PIN = INPUT;
    // Enable JTAG clock
    ENABLE_JTAG_CLOCK();
}

JTAG uses multiple pins (usually 4 or 5) for debugging.

SWD uses fewer pins (2 pins) and is simpler for ARM microcontrollers.

Examples
This sets two pins as outputs for SWD communication.
Embedded C
// Simple SWD pin setup
#define SWDIO_PIN 10
#define SWCLK_PIN 11

void setup_swd_pins() {
    pinMode(SWDIO_PIN, OUTPUT);
    pinMode(SWCLK_PIN, OUTPUT);
}
This sets four pins for JTAG, with one pin as input to read data.
Embedded C
// Simple JTAG pin setup
#define TCK_PIN 2
#define TMS_PIN 3
#define TDI_PIN 4
#define TDO_PIN 5

void setup_jtag_pins() {
    pinMode(TCK_PIN, OUTPUT);
    pinMode(TMS_PIN, OUTPUT);
    pinMode(TDI_PIN, OUTPUT);
    pinMode(TDO_PIN, INPUT);
}
Sample Program

This program simulates setting up the SWD debug interface pins as outputs and prints their modes.

Embedded C
#include <stdio.h>

// Simulated pin modes
#define OUTPUT 1
#define INPUT 0

int SWDIO_PIN_MODE = 0;
int SWCLK_PIN_MODE = 0;

void pinMode(int pin, int mode) {
    if (pin == 10) SWDIO_PIN_MODE = mode;
    if (pin == 11) SWCLK_PIN_MODE = mode;
}

void init_swd(void) {
    pinMode(10, OUTPUT); // SWDIO
    pinMode(11, OUTPUT); // SWCLK
    printf("SWD interface initialized with pins set as output.\n");
}

int main() {
    init_swd();
    printf("SWDIO pin mode: %d\n", SWDIO_PIN_MODE);
    printf("SWCLK pin mode: %d\n", SWCLK_PIN_MODE);
    return 0;
}
OutputSuccess
Important Notes

JTAG is older and uses more pins, but supports more features.

SWD is simpler and uses fewer pins, ideal for ARM Cortex microcontrollers.

Both interfaces allow you to pause, step through, and inspect your program while it runs.

Summary

JTAG and SWD help connect your computer to microcontrollers for debugging.

SWD uses 2 pins, JTAG uses 4 or 5 pins.

They let you find bugs and control your program during development.

Practice

(1/5)
1. Which debug interface uses fewer pins to connect to a microcontroller?
easy
A. JTAG uses 2 pins, fewer than SWD
B. SWD uses 2 pins, fewer than JTAG
C. Both use the same number of pins
D. Neither uses pins; they use wireless signals

Solution

  1. Step 1: Recall pin counts for SWD and JTAG

    SWD uses 2 pins, while JTAG uses 4 or 5 pins.
  2. Step 2: Compare the number of pins

    Since 2 is fewer than 4 or 5, SWD uses fewer pins.
  3. Final Answer:

    SWD uses 2 pins, fewer than JTAG -> Option B
  4. Quick Check:

    SWD = 2 pins [OK]
Hint: Remember SWD is the simpler 2-pin interface [OK]
Common Mistakes:
  • Confusing which interface uses fewer pins
  • Thinking JTAG uses fewer pins
  • Assuming wireless connection
2. Which of the following is the correct way to declare a function prototype for initializing SWD in embedded C?
easy
A. initSWD(void) void;
B. int initSWD[];
C. void initSWD(void);
D. void initSWD();

Solution

  1. Step 1: Understand function prototype syntax in C

    A function prototype includes return type, function name, and parameter list with types.
  2. Step 2: Check each option

    void initSWD(void); correctly declares a void function with no parameters: void initSWD(void); int initSWD[]; is an array declaration, not a function. void initSWD(); is old style but less precise. initSWD(void) void; is invalid syntax.
  3. Final Answer:

    void initSWD(void); -> Option C
  4. Quick Check:

    Proper C function prototype = void initSWD(void); [OK]
Hint: Use 'void' inside parentheses for no parameters [OK]
Common Mistakes:
  • Using empty parentheses without void
  • Declaring arrays instead of functions
  • Incorrect syntax order
3. Given this code snippet for SWD pin setup, what will be the output if pin_state is 1?
void setSWDPin(int pin_state) {
    if (pin_state == 1) {
        printf("SWD pin is HIGH\n");
    } else {
        printf("SWD pin is LOW\n");
    }
}

int main() {
    setSWDPin(1);
    return 0;
}
medium
A. SWD pin is HIGH
B. No output
C. SWD pin is LOW
D. Compilation error

Solution

  1. Step 1: Analyze the function call with pin_state = 1

    The function checks if pin_state equals 1, which it does.
  2. Step 2: Determine which printf executes

    Since pin_state == 1, it prints "SWD pin is HIGH" followed by a newline.
  3. Final Answer:

    SWD pin is HIGH -> Option A
  4. Quick Check:

    pin_state=1 prints HIGH [OK]
Hint: Check if condition matches input value [OK]
Common Mistakes:
  • Confusing HIGH and LOW outputs
  • Ignoring the if condition
  • Assuming no output
4. Identify the error in this JTAG initialization code snippet:
void initJTAG() {
    int tck = 1;
    int tms = 0;
    int tdi = 1;
    int tdo;
    tdo = tdi + tck + tms
    printf("JTAG initialized\n");
}
medium
A. Missing semicolon after tdo assignment
B. Incorrect variable types for pins
C. printf statement syntax error
D. Function missing return type

Solution

  1. Step 1: Check each line for syntax errors

    All lines end with semicolons except the line: tdo = tdi + tck + tms
  2. Step 2: Identify missing semicolon

    This line lacks a semicolon at the end, causing a syntax error.
  3. Final Answer:

    Missing semicolon after tdo assignment -> Option A
  4. Quick Check:

    Semicolon missing = Missing semicolon after tdo assignment [OK]
Hint: Look for missing semicolons at line ends [OK]
Common Mistakes:
  • Ignoring missing semicolon errors
  • Thinking variable types are wrong
  • Assuming printf is incorrect
5. You want to switch from JTAG to SWD debugging on a microcontroller. Which change is essential in your embedded C code?
hard
A. Add wireless communication support
B. Increase clock speed to match SWD requirements
C. Change all printf statements to UART communication
D. Reduce pin initialization from 5 pins to 2 pins

Solution

  1. Step 1: Understand hardware differences between JTAG and SWD

    JTAG uses 4 or 5 pins; SWD uses only 2 pins for debugging.
  2. Step 2: Identify code changes needed

    To switch, reduce pin initialization and control code from 5 pins to 2 pins accordingly.
  3. Final Answer:

    Reduce pin initialization from 5 pins to 2 pins -> Option D
  4. Quick Check:

    Pin count change is key for SWD [OK]
Hint: Adjust pin setup to match SWD's 2-pin design [OK]
Common Mistakes:
  • Changing clock speed unnecessarily
  • Confusing communication methods
  • Adding unrelated wireless features
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard