You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cortex-M33配置DWT写内存监视后DebugMon_Handler未触发问题

问题分析与解决方案

问题背景

使用以下代码在Arm Development Studio中配置DWT watchpoint,在Cortex-M3/M4平台可正常触发DebugMon_Handler,但在Cortex-M33平台无法触发,且发现Cortex-M33不存在DWT->MASKx寄存器:

#include "RTE_Components.h"                 /* This mechanism is the standard way to include */
#include CMSIS_device_header                /* the device header file, in this case ARMCM33.h */

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

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#pragma GCC diagnostic ignored "-Wreturn-type"

void watchpoint_enable()
{
    CoreDebug->DEMCR = CoreDebug_DEMCR_TRCENA_Msk /*enable tracing*/ |
                       CoreDebug_DEMCR_MON_EN_Msk /*enable debug interrupt*/;
    uint32_t test_variable;
    printf("enable watch points.... \n");
    DWT->COMP1 = (uint32_t)&test_variable;
    DWT->MASK1 = 0;                         // match all comparator bits, don't ignore any
    DWT->FUNCTION1 = (1 << 11)             /*DATAVSIZE 1 - match whole word*/
                     | (1 << 1) | (1 << 2) /*generate a watchpoint event on write*/;
    printf("watch points enabled....\n");
    test_variable = 5; //                   <<----- CPU stops after this line
}

int main(int argc, char *argv[])
{
    watchpoint_enable();
}

void DebugMon_Handler(void)
{
    printf("Debug handler in action...\n");
}

#pragma GCC diagnostic pop

核心原因

  1. DWT寄存器架构重构
    Cortex-M33对DWT模块进行了优化,将原Cortex-M3/M4中独立的MASKx寄存器功能整合到了COMPx寄存器中:
  • 在M3/M4中,COMPx存储目标地址,MASKx设置地址匹配的掩码位;
  • 在M33中,COMPx的低5位直接作为MASK字段,用于定义地址匹配的忽略位数,无需单独操作MASKx寄存器。直接访问M33不存在的MASKx寄存器会触发未定义行为,导致watchpoint配置失效。
  1. FUNCTIONx寄存器位定义差异
    Cortex-M33的FUNCTIONx寄存器部分位的偏移与M3/M4不同:
  • M3/M4中DATAVSIZE字段位于位11;
  • M33中DATAVSIZE字段位于位[10:8],配置32位字匹配需设置为1 << 9(对应值为1)。
  1. TrustZone权限限制(可选)
    如果你的Cortex-M33启用了TrustZone安全扩展,需确保调试配置允许非安全域访问DWT资源,否则非安全域代码无法正确配置watchpoint。

修正后的代码

针对Cortex-M33调整后的watchpoint_enable函数:

void watchpoint_enable()
{
    CoreDebug->DEMCR = CoreDebug_DEMCR_TRCENA_Msk | CoreDebug_DEMCR_MON_EN_Msk;
    uint32_t test_variable;
    printf("enable watch points.... \n");
    
    // M33中COMPx低5位为MASK字段,0表示匹配所有地址位
    DWT->COMP1 = ((uint32_t)&test_variable) | 0x00; 
    
    // 配置:32位字匹配 + 写操作触发watchpoint
    // DATAVSIZE=1(位9),触发条件为写(位1+位2)
    DWT->FUNCTION1 = (1 << 9) | (1 << 1) | (1 << 2); 
    
    printf("watch points enabled....\n");
    test_variable = 5;
}

验证要点

  • 确认使用的CMSIS头文件(ARMCM33.h)正确定义了M33的DWT寄存器结构;
  • 如果启用TrustZone,需在调试配置中设置允许非安全域调试访问;
  • 调试时可通过Arm Development Studio的寄存器窗口查看DWT寄存器状态,确认COMP1和FUNCTION1配置值正确。

内容的提问来源于stack exchange,提问作者one sunny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 12:12:46