C语言实现ARM MCUJTAG离线调试器

以下是一个使用C语言实现ARM MCU JTAG离线调试器的示例代码,该代码可以读取目标设备的寄存器和内存:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#define TCK_PIN 0
#define TMS_PIN 1
#define TDI_PIN 2
#define TDO_PIN 3

#define TCK_H() GPIO_SetBits(GPIOA, GPIO_Pin_0)
#define TCK_L() GPIO_ResetBits(GPIOA, GPIO_Pin_0)
#define TMS_H() GPIO_SetBits(GPIOA, GPIO_Pin_1)
#define TMS_L() GPIO_ResetBits(GPIOA, GPIO_Pin_1)
#define TDI_H() GPIO_SetBits(GPIOA, GPIO_Pin_2)
#define TDI_L() GPIO_ResetBits(GPIOA, GPIO_Pin_2)

#define TDO_IN() GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING
#define TDO_OUT() GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP

GPIO_InitTypeDef GPIO_InitStructure;

void JTAG_Init(void);
void JTAG_SendData(uint8_t *data, uint32_t len);
void JTAG_ReceiveData(uint8_t *data, uint32_t len);
void JTAG_ReadReg(uint32_t addr, uint32_t *data);
void JTAG_WriteReg(uint32_t addr, uint32_t data);
void JTAG_ReadMem(uint32_t addr, uint8_t *data, uint32_t len);
void JTAG_WriteMem(uint32_t addr, uint8_t *data, uint32_t len);

int main(void)
{
    uint32_t reg_value;
    uint8_t mem_data[256];

    // 初始化GPIO
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // 初始化JTAG接口
    JTAG_Init();

    // 读取寄存器
    JTAG_ReadReg(0xE000EDFC, &reg_value);
    printf("SP: 0x%08X\n", reg_value);
    JTAG_ReadReg(0xE000EDF0, &reg_value);
    printf("PC: 0x%08X\n", reg_value);

    // 读取内存
    JTAG_ReadMem(0x08000000, mem_data, sizeof(mem_data));
    for (int i = 0; i < sizeof(mem_data); i += 4) {
        printf("0x%08X: %02X %02X %02X %02X\n",
            0x08000000 + i,
            mem_data[i], mem_data[i+1], mem_data[i+2], mem_data[i+3]);
    }

    return 0;
}

void JTAG_Init(void)
{
    // 发送复位序列
    TCK_L();
    TMS_H();
    TDI_L();
    for (int i = 0; i < 5; i++) {
        TCK_H();
        TCK_L();
    }

    // 发送Test-Logic-Reset序列
    TCK_L();
    TMS_H();
    TCK_H();
    TCK_L();
    TCK_H();
    TCK_L();
    TCK_H();
    TCK_L();
    TMS_L();
    TCK_H();
    TCK_L();

    // 发送Run-Test/Idle序列
    for (int i = 0; i < 10; i++) {
        TCK_H();
        TCK_L();
    }
}

void JTAG_SendData(uint8_t *data, uint32_t len)
{
    for (int i = 0; i < len; i++) {
        for (int j = 0; j < 8; j++) {
            if (data[i] & (1 << j)) {
                TDI_H();
            } else {
                TDI_L();
            }

            TCK_H();
            TCK_L();
        }
    }
}

void JTAG_ReceiveData(uint8_t *data, uint32_t len)
{
    for (int i = 0; i < len; i++) {
        data[i] = 0;

        for (int j = 0; j < 8; j++) {
            TCK_H();
            data[i] |= GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) << j;
            TCK_L();
        }
    }
}

void JTAG_ReadReg(uint32_t addr, uint32_t *data)
{
    uint8_t req[5];
    uint8_t res[4];

    req[0] = 0x8E;
    req[1] = (addr >> 16) & 0xFF;
    req[2] = (addr >> 8) & 0xFF;
    req[3] = (addr >> 0) & 0xFF;
    req[4] = 0x00;

    JTAG_SendData(req, sizeof(req));
    JTAG_ReceiveData(res, sizeof(res));

    *data = (res[0] << 0) | (res[1] << 8) | (res[2] << 16) | (res[3] << 24);
}

void JTAG_WriteReg(uint32_t addr, uint32_t data)
{
    uint8_t req[9];

    req[0] = 0xAE;
    req[1] = (addr >> 16) & 0xFF;
    req[2] = (addr >> 8) & 0xFF;
    req[3] = (addr >> 0) & 0xFF;
    req[4] = (data >> 24) & 0xFF;
    req[5] = (data >> 16) & 0xFF;
    req[6] = (data >> 8) & 0xFF;
    req[7] = (data >> 0) & 0xFF;
    req[8] = 0x00;

    JTAG_SendData(req, sizeof(req));
}

void JTAG_ReadMem(uint32_t addr, uint8_t *data, uint32_t len)
{
    uint8_t req[7];

    req[0] = 0x9E;
    req[1] = (addr >> 16) & 0xFF;
    req[2] = (addr >> 8) & 0xFF;
    req[3] = (addr >> 0) & 0xFF;
    req[4] = (len >> 16) & 0xFF;
    req[5] = (len >> 8) & 0xFF;
    req[6] = (len >> 0) & 0xFF;

    JTAG_SendData(req, sizeof(req));
    JTAG_ReceiveData(data, len);
}

void JTAG_WriteMem(uint32_t addr, uint8_t *data, uint32_t len)
{
    uint8_t req[7];

    req[0] = 0xBE;
    req[1] = (addr >> 16) & 0xFF;
    req[2] = (addr >> 8) & 0xFF;
    req[3] = (addr >> 0) & 0xFF;
    req[4] = (len >> 16) & 0xFF;
    req[5] = (len >> 8) & 0xFF;
    req[6] = (len >> 0) & 0xFF;

    JTAG_SendData(req, sizeof(req));
    JTAG_SendData(data, len);
}

该代码使用STM32F103C8T6作为调试器,通过JTAG接口连接目标设备。调试器通过GPIO控制JTAG接口的时钟和数据线,实现JTAG协议的相关功能。该代码实现了读取寄存器和读取内存的功能,可以用于基本的ARM MCU离线调试。需要注意的是,该代码仅供参考,实际应用需要根据具体的需求进行修改和优化。

相关推荐
爱编程— 的小李8 分钟前
结构体(c语言)
c语言·开发语言
Kasen's experience37 分钟前
STM32 GPIO 配置
stm32·单片机·嵌入式硬件
知行电子-1 小时前
Proteus中数码管动态扫描显示不全(已解决)
单片机·proteus·嵌入式
学习路上_write2 小时前
FPGA/Verilog,Quartus环境下if-else语句和case语句RT视图对比/学习记录
单片机·嵌入式硬件·qt·学习·fpga开发·github·硬件工程
非概念2 小时前
stm32学习笔记----51单片机和stm32单片机的区别
笔记·stm32·单片机·学习·51单片机
iiiiiankor3 小时前
C/C++内存管理 | new的机制 | 重载自己的operator new
java·c语言·c++
小辛学西嘎嘎3 小时前
C/C++精品项目之图床共享云存储(3):网络缓冲区类和main
c语言·开发语言·c++
jjjxxxhhh1233 小时前
FPGA,使用场景,相比于单片机的优势
单片机·嵌入式硬件·fpga开发
无敌最俊朗@3 小时前
stm32学习之路——八种GPIO口工作模式
c语言·stm32·单片机·学习
EterNity_TiMe_3 小时前
【论文复现】STM32设计的物联网智能鱼缸
stm32·单片机·嵌入式硬件·物联网·学习·性能优化