Orbcode Trace functions library
dwt.h
1 #pragma once
2 #include <stdint.h>
3 #include <stdbool.h>
4 
5 #if !defined(CoreDebug)
6 # error \
7  "CoreDebug not defined. Include dwt.h AFTER core_cmX.h (typically after including device-specific header)"
8 #endif
9 
10 #if !defined(DWT)
11 # error \
12  "DWT not defined. Include dwt.h AFTER core_cmX.h (typically after including device-specific header)"
13 #endif
14 
15 #ifdef __cplusplus
16 extern "C"
17 {
18 #endif
19 
48  typedef enum
49  {
66 
73  } DWTSyncTap;
74 
80  typedef enum
81  {
94  } DWTCycleTap;
95 
103  typedef struct
104  {
153  } DWTOptions;
154 
163  static inline void DWTSetup(const DWTOptions* options);
164 
182  static inline void
183  DWTEnableComparator(uint8_t comparator, uintptr_t address, uint8_t ignoreBits, bool emitRange, uint8_t function);
184 
193  static inline void DWTDisableComparator(uint8_t comparator);
194 
197  void DWTSetup(const DWTOptions* options)
198  {
199  CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; // Enable ITM and DWT
200 
201  uint32_t ctrl = 0;
202  ctrl |= (options->FoldedInstructionCounterEvent ? 1 : 0) << DWT_CTRL_FOLDEVTENA_Pos;
203  ctrl |= (options->LSUCounterEvent ? 1 : 0) << DWT_CTRL_LSUEVTENA_Pos;
204  ctrl |= (options->SleepCounterEvent ? 1 : 0) << DWT_CTRL_SLEEPEVTENA_Pos;
205  ctrl |= (options->ExceptionOverheadCounterEvent ? 1 : 0) << DWT_CTRL_EXCEVTENA_Pos;
206  ctrl |= (options->CPICounterEvent ? 1 : 0) << DWT_CTRL_CPIEVTENA_Pos;
207  ctrl |= (options->ExceptionTrace ? 1 : 0) << DWT_CTRL_EXCTRCENA_Pos;
208  ctrl |= (options->PCSampling ? 1 : 0) << DWT_CTRL_PCSAMPLENA_Pos;
209  ctrl |= (((int)options->SyncTap) & 3) << DWT_CTRL_SYNCTAP_Pos;
210  ctrl |= (((int)options->CycleTap) & 3) << DWT_CTRL_CYCTAP_Pos;
211  ctrl |= (options->SamplingPrescaler - 1) << DWT_CTRL_POSTPRESET_Pos;
212  ctrl |= DWT_CTRL_CYCCNTENA_Msk;
213 
214  DWT->CTRL = ctrl;
215  }
216 
217  void DWTEnableComparator(uint8_t comparator, uintptr_t address, uint8_t ignoreBits, bool emitRange, uint8_t function)
218  {
219  uint32_t funcRaw = ((function << DWT_FUNCTION_FUNCTION_Pos) & DWT_FUNCTION_FUNCTION_Msk) |
220  ((emitRange ? 1 : 0) << DWT_FUNCTION_EMITRANGE_Pos);
221 
222  switch(comparator)
223  {
224  case 0:
225  DWT->COMP0 = address;
226  DWT->MASK0 = ignoreBits;
227  DWT->FUNCTION0 = funcRaw;
228  break;
229  case 1:
230  DWT->COMP1 = address;
231  DWT->MASK1 = ignoreBits;
232  DWT->FUNCTION1 = funcRaw;
233  break;
234  case 2:
235  DWT->COMP2 = address;
236  DWT->MASK2 = ignoreBits;
237  DWT->FUNCTION2 = funcRaw;
238  break;
239  case 3:
240  DWT->COMP3 = address;
241  DWT->MASK3 = ignoreBits;
242  DWT->FUNCTION3 = funcRaw;
243  break;
244  }
245  }
246 
247  void DWTDisableComparator(uint8_t comparator)
248  {
249  switch(comparator)
250  {
251  case 0:
252  DWT->FUNCTION0 = 0;
253  break;
254  case 1:
255  DWT->FUNCTION1 = 0;
256  break;
257  case 2:
258  DWT->FUNCTION2 = 0;
259  break;
260  case 3:
261  DWT->FUNCTION3 = 0;
262  break;
263  }
264  }
265 
266 #ifdef __cplusplus
267 }
268 #endif
DWTCycleTap
Interval of PC sampling and event counter packets.
Definition: dwt.h:81
static void DWTSetup(const DWTOptions *options)
Configures DWT component.
Definition: dwt.h:197
static void DWTDisableComparator(uint8_t comparator)
Disables DWT comparator.
Definition: dwt.h:247
DWTSyncTap
Interval of ITM synchronization packet.
Definition: dwt.h:49
static void DWTEnableComparator(uint8_t comparator, uintptr_t address, uint8_t ignoreBits, bool emitRange, uint8_t function)
Configures DWT comparator.
Definition: dwt.h:217
@ DWTCycleTap10
Output packet when bit CYCCNT[10] changes.
Definition: dwt.h:93
@ DWTCycleTap6
Output packet when bit CYCCNT[6] changes.
Definition: dwt.h:87
@ DWTSyncTap28
Output synchronization when CYCCNT[28] bit changes.
Definition: dwt.h:72
@ DWTSyncTap24
Output synchronization when CYCCNT[24] bit changes.
Definition: dwt.h:59
@ DWTSyncTapDisabled
Disabled.
Definition: dwt.h:53
@ DWTSyncTap26
Output synchronization when CYCCNT[26] bit changes.
Definition: dwt.h:65
DWT configuration.
Definition: dwt.h:104
bool FoldedInstructionCounterEvent
Enable folded instruction event counter.
Definition: dwt.h:108
DWTSyncTap SyncTap
Specifies interval for ITM synchronization and timestamp packets.
Definition: dwt.h:140
bool SleepCounterEvent
Enable sleep event counter.
Definition: dwt.h:116
uint8_t SamplingPrescaler
Defines prescaler for PC sampling.
Definition: dwt.h:152
bool CPICounterEvent
Enable Cycles-per-Instruction counter event.
Definition: dwt.h:124
bool LSUCounterEvent
Enable load/store event counter.
Definition: dwt.h:112
bool ExceptionOverheadCounterEvent
Enable exception overhead counter event.
Definition: dwt.h:120
bool ExceptionTrace
Enable exception entry/exit trace.
Definition: dwt.h:130
bool PCSampling
Enable PC sampling.
Definition: dwt.h:136
DWTCycleTap CycleTap
Specifies divider for counter used in PC sampling and event counters.
Definition: dwt.h:144