Skip to content

Commit 177c4ff

Browse files
committed
Unlock DWT registers only if necessary
1 parent 0779932 commit 177c4ff

File tree

1 file changed

+15
-1
lines changed
  • libs/trace/include/orbcode/trace

1 file changed

+15
-1
lines changed

libs/trace/include/orbcode/trace/dwt.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,21 @@ extern "C"
197197
void DWTSetup(const DWTOptions* options)
198198
{
199199
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; // Enable ITM and DWT
200-
DWT->LAR = 0xC5ACCE55; // Unlock DWT access via magic number
200+
201+
// Some CPU do not have DWT lock registers
202+
// All CoreSight compoents that might have lock, will have
203+
// lock status register at offset 0xFB4
204+
// lock access register at offset 0xFB0
205+
// If locking is not implemented, lock status register will read as zero
206+
// See: ARMv7-M Architecture Reference Manual, section D.1.1, table D1-2
207+
volatile uint32_t* lockStatus = ((volatile uint32_t*)DWT) + (0xFB4 / 4);
208+
volatile uint32_t* lockAccess = ((volatile uint32_t*)DWT) + (0xFB0 / 4);
209+
210+
if(*lockStatus != 0)
211+
{
212+
// lock status is not zero, meaning that unlock is necessary
213+
*lockAccess = 0xC5ACCE55; // Unlock DWT access via magic number
214+
}
201215

202216
uint32_t ctrl = 0;
203217
ctrl |= (options->FoldedInstructionCounterEvent ? 1 : 0) << DWT_CTRL_FOLDEVTENA_Pos;

0 commit comments

Comments
 (0)