1. 前言
“什么是 CMSIS-DAP / DAPLink?”
Arm Mbed DAPLink 是一个开源软件项目,它运行在连接到应用 CPU 的 SWD 或 JTAG 端口的辅助 MCU 上,在开发电脑和 CPU 调试访问端口之间建立了一座桥梁。它为开发者提供了免驱的 CMSIS-DAP 调试、拖拽烧录以及虚拟串口等功能。
为什么我们要折腾这个项目?
- 与昂贵且闭源的 J-Link、U-Link 不同,DAPLink 是完全开源免费的。
- RA4M2拥有512KB Flash + 128KB SRAM,支持USB FS足以支持本项目
- 作为新时代的嵌入式爱好者,怎能没有一个属于自己的调试器
RA-Link-RA?
目标功能:
ARM 官方的完整 DAPLink 包含了复杂的拖拽升级功能,但核心功能其实只有两块。为了保持代码的精简与高效,我们的 "RA-LINK-RA" 将重点实现最实用的两个核心功能:
- CMSIS-DAP v2 调试协议(基于 WinUSB 块传输,免驱且高速)
- USB 虚拟串口(CDC 通信)

2. 核心准备与底层配置
2.1 开发环境
- 母调试器:WCH-Link(仅用于将我们编写的 DAPLink 固件第一次烧录进 RA4M2)
- 目标板:RA-Eco-RA4M2开发板
- 工具链:ARM Toolchain for Embed (LLVM based)
- 软件栈:FreeRTOS(FSP集成) + Renesas Flexible Software Package v6.4.0
2.2 线程划分
┌──────────────┐ USB ┌─────────────────┐
│ PC (Host) │◄──────────────────►│ RA4M2 (DAPLink)│
│ │ CMSIS-DAP V2 │ │
│ Keil/IAR/ │ (Bulk EP) │ ┌───────────┐ │ SWD/JTAG ┌──────────────┐
│ PyOCD/probe-rs│ │ │ DAP Thread│──┼─────────────────►│ Target Board │
│ │ CDC ACM │ └───────────┘ │ │ (RA/STM32/ │
│ Serial Port │ (Interrupt EP) │ ┌───────────┐ │ SWO Trace │ nRF/...) │
│ │◄──────────────────►│ │ SWO Thread│◄─┼─────────────────►│ │
└──────────────┘ │ └───────────┘ │ └──────────────┘
└─────────────────┘
我们将在 FreeRTOS 中创建两个核心线程:
- DAP Thread:通过 USB 接收 PC 下发的 CMSIS-DAP 命令,解析后驱动 SWD/JTAG GPIO 时序,再将结果回传。
- CDC Thread (SWO):实时接收目标板的 SWO 跟踪数据,通过 USB CDC 回传给 PC。
2.3 引脚分配
| RA4M2 引脚 | DAPLink 功能定义 | 备注说明 |
|---|---|---|
| GND | GND | 必须共地 |
| P301 | PIN_CMSIS_DAP_SWCLK | SWD 时钟 |
| P303 | PIN_CMSIS_DAP_SWDIO | SWD 数据 |
| P302 | PIN_CMSIS_DAP_RESET | 目标板复位 |
| P100/ P101 | UART_RX / UART_TX | 虚拟串口收发 |
| P305 / P306 / P304 | TDI / TDO / NTRST | JTAG 全功能保留 |
| P100 | SWO | SWD 模式下的跟踪数据输出 |
在 SWD 模式下,SWO 数据通过 P100 RX异步接收;在 JTAG 模式下,TDO 数据通过 P306接收。两者功能独立,不冲突。
2.4 FSP 模块配置
配置时钟

配置管脚

配置USB Composite设备


配置线程与消息队列

配置VCOM UART3和SWO UART0


FreeRTOS 动态分配与堆内存
- 开启线程的
Dynamic Allocation support(RTOS 对象将使用 RAM 自动分配)。 - 将
Total Heap Size提升至 30000 (30KB) 甚至更高。因为 USB 描述符、消息队列和两个工作线程的栈空间都会大量使用 FreeRTOS 的堆内存。

3. 核心移植步骤与代码实现
由于瑞萨提供基于CMSIS-DAP 2.1.1的EK-RA4M2 dap实现参考renesas/ra-cmsis-dap-port: Example port of CMSIS-DAP debug probe firmware to Renesas RA MCU family,因此我们可以仿照该工程,轻松实现DAP移植。
3.1 提取 CMSIS-DAP 协议实现
从 ra-cmsis-dap-port 官方仓库中,提取 CMSIS-DAP 协议解析引擎放入我们的工程:
DAP.c / DAP.h:协议中枢,负责解析 PC 发来的 64 Byte 原始数据包。JTAG_DP.c / SW_DP.c:“协议翻译官”,把高级指令翻译成具体的电平时序。DAP_config.h:底层适配层,提供真实的 GPIO 翻转与时钟延时。
3.2 替换PIN定义
原工程基于瑞萨EK-RA4M2评估板,工程内对DAP指示LED进行了定义,我们需要根据当前开发板实现这些LED Pin的定义
#define LED2 (BSP_IO_PORT_04_PIN_04)
#define LED1 (BSP_IO_PORT_04_PIN_05)
#define LED3 (BSP_IO_PORT_00_PIN_02)
static const uint16_t g_bsp_prv_leds[] =
{
(uint16_t) LED1, ///< LED1
(uint16_t) LED2, ///< LED2
(uint16_t) LED3, ///< LED3
};
typedef struct st_bsp_leds
{
uint16_t led_count; ///< The number of LEDs on this board
uint16_t const * p_leds; ///< Pointer to an array of IOPORT pins for controlling LEDs
} bsp_leds_t;
const bsp_leds_t g_bsp_leds =
{
.led_count = (uint16_t) ((sizeof(g_bsp_prv_leds) / sizeof(g_bsp_prv_leds[0]))),
.p_leds = &g_bsp_prv_leds[0]
};
3.3 修改Pipe Endpoint判断
由于原工程基于FSP4.x实现,USB Pipe配置再FSP 6.4已不可用,我们还需要额外在dap_thread中修改process_usb_events内的Pipe Endpoint的判断,确保dap可以正确被处理
static fsp_err_t process_usb_events(usb_event_info_t *p_event_info)
{
fsp_err_t err = FSP_SUCCESS;
uint16_t used_pipe = 0;
uint8_t pipe = 0;
usb_pipe_t pipe_info;
/* USB event received */
switch (p_event_info->event)
{
case USB_STATUS_CONFIGURED: /* Configured State */
{
APP_PRINT("USB Configured Successfully\r\n");
R_USB_UsedPipesGet(&g_basic1_ctrl, &used_pipe, USB_CLASS_PVND);
for (pipe = START_PIPE; pipe < END_PIPE; pipe++)
{
if (0 != (used_pipe & (1 << pipe)))
{
R_USB_PipeInfoGet(&g_basic1_ctrl, &pipe_info, pipe);
if (USB_EP_DIR_IN != (pipe_info.endpoint & USB_EP_DIR_IN))
{
/* DAP Command Pipe */
if (USB_TRANSFER_TYPE_BULK == pipe_info.transfer_type)
{
if ((pipe_info.endpoint == 0x04) && (cmd_pipe == END_PIPE))
{
cmd_pipe = pipe;
APP_PRINT("\r\ncmd_pipe Pipe Number: %d Endpoint:0x%02X ", pipe, pipe_info.endpoint);
}
}
}
else
{
/* DAP Command Response Pipe */
if (USB_TRANSFER_TYPE_BULK == pipe_info.transfer_type)
{
// DAP Response 是 EP 0x85
if ((pipe_info.endpoint == 0x85) && (rsp_pipe == END_PIPE))
{
rsp_pipe = pipe;
APP_PRINT("\r\nrsp_pipe Pipe Number: %d Endpoint:0x%02X ", pipe, pipe_info.endpoint);
}
// DAP SWO 是 EP 0x86
else if ((pipe_info.endpoint == 0x86) && (swo_pipe == END_PIPE))
{
swo_pipe = pipe;
APP_PRINT("\r\nswo_pipe Pipe Number: %d Endpoint:0x%02X ", pipe, pipe_info.endpoint);
}
}
}
}
}
// Initialize variables
USB_RequestIndexI = 0U;
USB_RequestIndexO = 0U;
USB_RequestCountI = 0U;
USB_RequestCountO = 0U;
USB_RequestIdle = 1U;
USB_ResponseIndexI = 0U;
USB_ResponseIndexO = 0U;
USB_ResponseCountI = 0U;
USB_ResponseCountO = 0U;
USB_ResponseIdle = 1U;
PCDC_ToTargetIndexI = 0U;
PCDC_ToTargetIndexO = 0U;
PCDC_ToTargetCountI = 0U;
PCDC_ToTargetCountO = 0U;
PCDC_ToTargetIdle = 1U;
/* Read data from CMSIS-DAP Command pipe */
USB_RequestIdle = 0U;
err = R_USB_PipeRead(&g_basic1_ctrl, USB_Request[USB_RequestIndexI], DAP_PACKET_SIZE, cmd_pipe);
if (FSP_SUCCESS != err)
{
APP_ERR_PRINT("\r\nR_USB_PipeRead (USB_CLASS_PVND) failed.\r\n");
}
/* Read data from serial port */
PCDC_ToTargetIdle = 0U;
err = R_USB_Read(&g_basic1_ctrl, PCDC_ToTarget[PCDC_ToTargetIndexI], CDC_DATA_LEN, USB_CLASS_PCDC);
if (FSP_SUCCESS != err)
{
APP_ERR_PRINT("\r\nR_USB_Read (USB_CLASS_PCDC) failed.\r\n");
}
b_usb_configured = true;
break;
}
case USB_STATUS_WRITE_COMPLETE: /* Write Complete State */
{
if (b_usb_configured)
{
switch (p_event_info->type)
{
case USB_CLASS_PCDC:
{
BaseType_t err_semaphore = xSemaphoreGive(g_sem_pcdc_tx);
if (pdTRUE != err_semaphore)
{
handle_error(1, "\r\n xSemaphoreGive on g_sem_pcdc_tx Failed \r\n");
}
}
break;
default:
{
if (rsp_pipe == p_event_info->pipe)
{
/* Last write of CMSIS-DAP command response TO host machine has completed.
If we have another available, then that can be sent now otherwise we
set USB_ResponseIdle to indicate that we are free to send the next response
once one becomes available
*/
if (USB_ResponseCountI != USB_ResponseCountO)
{
// Load data from response buffer to be sent back
err = R_USB_PipeWrite(&g_basic1_ctrl, USB_Response[USB_ResponseIndexO], USB_RespSize[USB_ResponseIndexO], rsp_pipe);
if (FSP_SUCCESS != err)
{
APP_ERR_PRINT("\r\nR_USB_PipeWrite API failed.\r\n");
}
USB_ResponseIndexO++;
if (USB_ResponseIndexO == DAP_PACKET_COUNT)
{
USB_ResponseIndexO = 0U;
}
USB_ResponseCountO++;
}
else
{
USB_ResponseIdle = 1U;
}
}
else if (swo_pipe == p_event_info->pipe)
{
/* Let the SWO handler know the transfer is complete */
// APP_PRINT("process_usb_events USB_STATUS_WRITE_COMPLETE (SWO 0x%X)\r\n", swo_pipe);
SWO_TransferComplete();
}
}
break;
}
if (FSP_SUCCESS != err)
{
APP_ERR_PRINT("\r\nR_USB_Read API failed.\r\n");
}
}
break;
}
case USB_STATUS_READ_COMPLETE:
{
if (b_usb_configured)
{
switch (p_event_info->type)
{
case USB_CLASS_PCDC:
PCDC_ToTargetSize[PCDC_ToTargetIndexI] = (uint16_t)p_event_info->data_size;
PCDC_ToTargetIndexI++;
if (PCDC_ToTargetIndexI == UART_PACKET_COUNT)
{
PCDC_ToTargetIndexI = 0U;
}
PCDC_ToTargetCountI++;
if ((uint16_t)(PCDC_ToTargetCountI - PCDC_ToTargetCountO) != UART_PACKET_COUNT)
{
err = R_USB_Read(&g_basic1_ctrl, PCDC_ToTarget[PCDC_ToTargetIndexI], CDC_DATA_LEN, USB_CLASS_PCDC);
if (FSP_SUCCESS != err)
{
APP_ERR_PRINT("\r\nR_USB_Read (USB_CLASS_PCDC) failed.\r\n");
}
}
else
{
PCDC_ToTargetIdle = 1U;
}
break;
default:
if (cmd_pipe == p_event_info->pipe)
{
if (USB_Request[USB_RequestIndexI][0] == ID_DAP_TransferAbort)
{
DAP_TransferAbort = 1U;
}
else
{
USB_RequestIndexI++;
if (USB_RequestIndexI == DAP_PACKET_COUNT)
{
USB_RequestIndexI = 0U;
}
USB_RequestCountI++;
}
}
// Start reception of next request packet
if ((uint16_t)(USB_RequestCountI - USB_RequestCountO) != DAP_PACKET_COUNT)
{
err = R_USB_PipeRead(&g_basic1_ctrl, USB_Request[USB_RequestIndexI], DAP_PACKET_SIZE, cmd_pipe);
if (FSP_SUCCESS != err)
{
APP_ERR_PRINT("\r\nR_USB_PipeRead (USB_CLASS_PVND) failed.\r\n");
}
}
else
{
USB_RequestIdle = 1U;
}
break;
}
}
break;
}
case USB_STATUS_REQUEST: /* Receive Class Request */
{
/* Perform usb status request operation.*/
/* Check for the specific CDC class request IDs */
if (USB_PCDC_SET_LINE_CODING == (p_event_info->setup.request_type & USB_BREQUEST))
{
err = R_USB_PeriControlDataGet(&g_basic1_ctrl, (uint8_t *)&g_line_coding, LINE_CODING_LENGTH);
if (FSP_SUCCESS == err)
{
/* Line Coding information read from the control pipe */
sci_extend_cfg.p_baud_setting = &baud_setting;
g_uart_test_cfg.p_extend = &sci_extend_cfg;
/* Calculate the baud rate*/
g_baud_rate = g_line_coding.dw_dte_rate;
if (INVALID_SIZE < g_baud_rate)
{
/* Calculate baud rate setting registers */
err = R_SCI_UART_BaudCalculate(g_baud_rate, enable_bitrate_modulation, error_rate_x_1000, &baud_setting);
if (FSP_SUCCESS != err)
{
handle_error(err, "\r\nR_SCI_UART_BaudCalculate failed.\r\n");
}
}
/* Set number of parity bits */
set_uart_line_coding_cfg(&g_uart_test_cfg, &g_line_coding);
/* Close module */
err = R_SCI_UART_Close(&g_uart_ctrl);
if (FSP_SUCCESS != err)
{
APP_ERR_PRINT("\r\n** R_SCI_UART_Close API failed ** \r\n");
}
/* Open UART with changed UART settings */
err = R_SCI_UART_Open(&g_uart_ctrl, &g_uart_test_cfg);
if (FSP_SUCCESS != err)
{
handle_error(err, "\r\nR_SCI_UART_Open failed.\r\n");
}
}
}
else if (USB_PCDC_GET_LINE_CODING == (p_event_info->setup.request_type & USB_BREQUEST))
{
/* Set the class request.*/
err = R_USB_PeriControlDataSet(&g_basic1_ctrl, (uint8_t *)&g_line_coding, LINE_CODING_LENGTH);
if (FSP_SUCCESS != err)
{
handle_error(err, "\r\nR_USB_PeriControlDataSet failed.\r\n");
}
}
else if (USB_PCDC_SET_CONTROL_LINE_STATE == (p_event_info->setup.request_type & USB_BREQUEST))
{
/* Get the status of the control signals */
err = R_USB_PeriControlDataGet(&g_basic1_ctrl,
(uint8_t *)&g_control_line_state,
sizeof(usb_pcdc_ctrllinestate_t));
if (FSP_SUCCESS == err)
{
g_control_line_state.bdtr = (unsigned char)((p_event_info->setup.request_value >> 0) & 0x01);
g_control_line_state.brts = (unsigned char)((p_event_info->setup.request_value >> 1) & 0x01);
/* Toggle the line state if the flow control pin is set to a value (other than SCI_UART_INVALID_16BIT_PARAM) */
if (SCI_UART_INVALID_16BIT_PARAM != g_uart_ctrl.flow_pin)
{
R_BSP_PinWrite(g_uart_ctrl.flow_pin,
(g_control_line_state.brts == 0) ? BSP_IO_LEVEL_LOW : BSP_IO_LEVEL_HIGH);
}
}
/* Set the usb status as ACK response.*/
err = R_USB_PeriControlStatusSet(&g_basic1_ctrl, USB_SETUP_STATUS_ACK);
if (FSP_SUCCESS != err)
{
handle_error(err, "\r\nR_USB_PeriControlDataSet failed.\r\n");
}
}
else if (USB_GET_DESCRIPTOR == (p_event_info->setup.request_type & USB_BREQUEST))
{
/* check for request value */
}
else if (USB_VENDOR_GET_MS_DESCRIPTOR_DEVICE == p_event_info->setup.request_type)
{
if (p_event_info->setup.request_index == EXT_COMPATID_OS_DESCRIPTOR)
{
/* Ignore interface number for the device */
err = R_USB_PeriControlDataSet(&g_basic1_ctrl, (uint8_t *)&ecd, p_event_info->setup.request_length);
if (FSP_SUCCESS != err)
{
handle_error(err, "\r\nR_USB_PeriControlDataSet failed.\r\n");
}
}
}
else if (USB_VENDOR_GET_MS_DESCRIPTOR_INTERFACE == p_event_info->setup.request_type)
{
if (p_event_info->setup.request_index == EXT_PROP_OS_DESCRIPTOR)
{
if ((p_event_info->setup.request_value & 0x00FF) == INTERFACE_CMSIS_DAP)
{
err = R_USB_PeriControlDataSet(&g_basic1_ctrl, ExtendedPropertiesDescriptor, p_event_info->setup.request_length);
if (FSP_SUCCESS != err)
{
handle_error(err, "\r\nR_USB_PeriControlDataSet failed.\r\n");
}
}
}
}
else
{
/* ACK all other status requests */
err = R_USB_PeriControlStatusSet(&g_basic1_ctrl, USB_SETUP_STATUS_ACK);
if (FSP_SUCCESS != err)
{
;
}
}
if (FSP_SUCCESS != err)
{
APP_ERR_PRINT("\r\nusb_status_request failed.\r\n");
}
break;
}
case USB_STATUS_REQUEST_COMPLETE: /* Request Complete State */
{
if (USB_PCDC_SET_LINE_CODING == (p_event_info->setup.request_type & USB_BREQUEST))
{
APP_PRINT("\nUSB STATUS : USB_STATUS_REQUEST_COMPLETE \nRequest_Type: USB_PCDC_SET_LINE_CODING \n");
}
else if (USB_PCDC_GET_LINE_CODING == (p_event_info->setup.request_type & USB_BREQUEST))
{
APP_PRINT("\nUSB STATUS : USB_STATUS_REQUEST_COMPLETE \nRequest_Type: USB_PCDC_GET_LINE_CODING \n");
}
else
{
// Do Nothing.
}
break;
}
case USB_STATUS_DETACH:
case USB_STATUS_SUSPEND:
{
APP_PRINT("\nUSB STATUS : USB_STATUS_DETACH & USB_STATUS_SUSPEND\r\n");
/* Reset the usb attached flag as indicating usb is removed.*/
b_usb_configured = false;
break;
}
case USB_STATUS_RESUME:
{
APP_PRINT("\nUSB STATUS : USB_STATUS_RESUME\r\n");
break;
}
default:
{
break;
}
}
return err;
}
至此,一个纯正的 CMSIS-DAP v2 调试器就诞生了!
4. RA-LINK-RA
使用RA4M2调试RA4M2

将我们的“RA-LINK-RA”通过 USB 插入电脑,设备管理器中瞬间出现了 "CMSIS-DAP v2" 设备和一个串口。keil可以轻松识别到我们的RA4M2 DAP

我们使用 Rust 生态中的调试器前端 probe-rs,直接对它以及我们手头已有的WCH-LINK 1V1进行极限压力测试,目标芯片设置为另一块 RA4M2:


可以看到:
- 在 32KB 大数据块的连续读写测试中,我们自制的 DAPLink 跑出了稳稳的 ~51 KB/s 的速度,大概是WCH-LINK的2-3倍。
- 对于基于 USB 2.0 Full-Speed (12Mbps) 且通过 GPIO 软件翻转(Bit-banging)实现 SWD 协议的调试器来说,50KB/s 的纯净载荷吞吐量已经达到了极优秀的梯队,完全满足日常基于 VS Code 或 Keil 的固件下载与断点单步调试需求。
5. 总结
在这个项目中,我们将一块 RA4M2 芯片彻底改造成了生产力工具——RA-LINK-RA,实现了ra4m2调试ra4m2,虽然作为对比,J-Link 在同等条件下通常可达 200-400 KB/s(得益于硬件加速和 High-Speed USB),不过应该常见的调试已足够使用。最后固件hex已打包,欢迎作为玩具尝试。

全部评论