1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! ARM Data Watchpoint and Trace Unit
//!
//! <https://developer.arm.com/documentation/100166/0001/Data-Watchpoint-and-Trace-Unit/DWT-Programmers--model?lang=en>

use super::dcb;
use kernel::hil;
use kernel::utilities::registers::interfaces::{ReadWriteable, Readable, Writeable};
use kernel::utilities::registers::{register_bitfields, register_structs, ReadOnly, ReadWrite};
use kernel::utilities::StaticRef;

register_structs! {
    /// In an ARMv7-M processor, a System Control Block (SCB) in the SCS
    /// provides key status information and control features for the processor.
    DwtRegisters {
        // Control Register
        (0x00 => ctrl: ReadWrite<u32, Control::Register>),

        // Cycle Count Register
        (0x04 => cyccnt: ReadWrite<u32, CycleCount::Register>),

        // CPI Count Register
        (0x08 => cpicnt: ReadWrite<u32, CpiCount::Register>),

        // Exception Overhead Register
        (0x0C => exccnt: ReadWrite<u32, ExceptionOverheadCount::Register>),

        // Sleep Count Register
        (0x10 => sleepcnt: ReadWrite<u32, SleepCount::Register>),

        // LSU Count Register
        (0x14 => lsucnt: ReadWrite<u32, LsuCount::Register>),

        // Folder-Instruction Count Register
        (0x18 => foldcnt: ReadWrite<u32, FoldedInstructionCount::Register>),

        // Program Count Sample Register
        (0x1C => pcsr: ReadOnly<u32, ProgramCounterSample::Register>),

        // Comparator Register0
        (0x20 => comp0: ReadWrite<u32, Comparator0::Register>),

        // Mask Regsiter0
        // The maximum mask size is 32KB
        (0x24 => mask0: ReadWrite<u32, Comparator0Mask::Register>),

        // Function Register0
        (0x28 => function0: ReadWrite<u32, Comparator0Function::Register>),

        (0x2c => _reserved0),

        // Comparator Register1
        (0x30 => comp1: ReadWrite<u32, Comparator1::Register>),

        // Mask Regsiter1
        // The maximum mask size is 32KB
        (0x34 => mask1: ReadWrite<u32, Comparator1Mask::Register>),

        // Function Register1
        (0x38 => function1: ReadWrite<u32, Comparator1Function::Register>),

        (0x3c => _reserved1),

        // Comparator Register2
        (0x40 => comp2: ReadWrite<u32, Comparator2::Register>),

        // Mask Regsiter2
        // The maximum mask size is 32KB
        (0x44 => mask2: ReadWrite<u32, Comparator2Mask::Register>),

        // Function Register2
        (0x48 => function2: ReadWrite<u32, Comparator2Function::Register>),

        (0x4c => _reserved2),

        // Comparator Register3
        (0x50 => comp3: ReadWrite<u32, Comparator3::Register>),

        // Mask Regsiter3
        // The maximum mask size is 33KB
        (0x54 => mask3: ReadWrite<u32, Comparator3Mask::Register>),

        // Function Register3
        (0x58 => function3: ReadWrite<u32, Comparator3Function::Register>),

        (0x5c => @END),
    }
}

register_bitfields![u32,
    Control [
        /// Number of Camparators implemented.
        /// RO.
        NUMCOMP         OFFSET(28)  NUMBITS(4),

        /// Shows if trace sampling and exception tracing is implemented
        /// Is 0 if supported, is 1 if it is not.
        /// RO
        NOTRCPKT        OFFSET(27)  NUMBITS(1),

        /// Shows if external match signals ([`CMPMATCH`]) are implemented
        /// Is 0 if supported, is 1 if it is not.
        /// RO
        NOEXITTRIG      OFFSET(26)  NUMBITS(1),

        /// Shows if the cycle counter is implemented
        /// Is 0 if supported, is 1 if it is not.
        /// RO
        NOCYCCNT        OFFSET(25)  NUMBITS(1),

        /// Shows if profiling counters are supported.
        /// Is 0 if supported, is 1 if it is not.
        /// RO
        NOPERFCNT       OFFSET(24)  NUMBITS(1),

        /// Writing 1 enables event counter packets generation if [`PCSAMPLENA`] is set to 0.
        /// Defaults to 0b0 on reset.
        /// WARN: This bit is UNKNOWN if `NOTPRCPKT` or `NOCYCCNT` is read as one.
        /// RW
        CYCEVTENA       OFFSET(22)  NUMBITS(1),

        /// Writing 1 enables generation of folded instruction counter overflow event. Defaults to
        /// 0b0 on reset.
        /// WARN: This bit is UNKNOWN if `NOPERFCNT` reads as one.
        /// RW
        FOLDEVTENA      OFFSET(21)  NUMBITS(1),

        /// Writing 1 enables generation of LSU counter overflow event.
        /// Defaults to 0b0 on reset.
        /// WARN: This bit is UNKNOWN if `NOPERFCNT` reads as one.
        /// RW
        LSUEVTENA       OFFSET(20)  NUMBITS(1),

        /// Writing 1 enables generation of sleep counter overflow event.
        /// Defaults to 0b0 on reset.
        /// WARN: This bit is UNKNOWN if `NOPERFCNT` reads as one.
        /// RW
        SLEEPEVTENA     OFFSET(19)  NUMBITS(1),

        /// Writing 1 enables generation of exception overhead counter overflow event.
        /// Defaults to 0b0 on reset.
        /// WARN: This bit is UNKNOWN if `NOPERFCNT` reads as one.
        /// RW
        EXCEVTENA       OFFSET(18)  NUMBITS(1),

        /// Writing 1 enables generation of the CPI counter overlow event.
        /// Defaults to 0b0 on reset.
        /// WARN: This bit is UNKNOWN if `NOPERFCNT` reads as one.
        /// RW
        CPIEVTENA       OFFSET(17)  NUMBITS(1),

        /// Writing 1 enables generation of exception trace.
        /// Defaults to 0b0 on reset.
        /// WARN: This bit is UNKNOWN if `NOTRCPKT` reads as one.
        /// RW
        EXCTRCENA       OFFSET(16)  NUMBITS(1),

        /// Writing 1 enables use of [`POSTCNT`] counter as a timer for Periodic PC sample packet
        /// generation.
        /// Defaults to 0b0 on reset.
        /// WARN: This bit is UNKNOWN if `NOTRCPKT` or `NOCYCCNT` read as one.
        /// RW
        PCSAMPLENA      OFFSET(12)  NUMBITS(1),

        /// Determines the position of synchronisation packet counter tap on the `CYCCNT` counter
        /// and thus the synchronisation packet rate.
        /// Defaults to UNKNOWN on reset.
        /// WARN: This bit is UNKNOWN if `NOCYCCNT` reads as one.
        /// RW
        SYNCTAP         OFFSET(10)  NUMBITS(2),

        /// Determines the position of the `POSTCNT` tap on the `CYCCNT` counter.
        /// Defaults to UNKNOWN on reset.
        /// WARN: This bit is UNKNOWN if `NOCYCCNT` reads as one.
        /// RW
        CYCTAP          OFFSET(9)  NUMBITS(1),

        /// Initial value for the `POSTCNT` counter.
        /// Defaults to UNKNOWN on reset.
        /// WARN: This bit is UNKNOWN if `NOCYCCNT` reads as one.
        /// RW
        POSTINIT        OFFSET(8)  NUMBITS(4),

        /// Reload value for the `POSTCNT` counter.
        /// Defaults to UNKNOWN on reset.
        /// WARN: This bit is UNKNOWN if `NOCYCCNT` reads as one.
        /// RW
        POSTPRESET      OFFSET(1)  NUMBITS(4),

        /// Writing 1 enables `CYCCNT`.
        /// Defaults to 0b0 on reset.
        /// WARN: This bit is UNKNOWN if `NOCYCCNT` reads as one.
        /// RW
        CYCNTENA       OFFSET(0)  NUMBITS(1),
    ],

    CycleCount[
        /// When enabled, increases on each processor clock cycle when Control::CYCNTENA and
        /// DEMCRL::TRCENA read as one.
        /// Wraps to zero on overflow.
        CYCCNT          OFFSET(0)   NUMBITS(32),
    ],

    CpiCount[
        /// Base instruction overhead counter.
        CPICNT          OFFSET(0)   NUMBITS(8),
    ],

    ExceptionOverheadCount[
        /// Counts cycles spent in exception processing.
        EXCCNT          OFFSET(0)   NUMBITS(8),
    ],

    SleepCount[
        /// Counts each cycle the processor is sleeping.
        SLEEPCNT        OFFSET(0)   NUMBITS(8),
    ],

    LsuCount[
        /// Counts additional cycles required to excecute all load store instructions
        LSUCNT          OFFSET(0)   NUMBITS(8),
    ],

    FoldedInstructionCount[
        /// Increments by one for each instruction that takes 0 cycles to excecute.
        FOLDCNT          OFFSET(0)   NUMBITS(8),
    ],

    ProgramCounterSample[
        /// Samples current value of the program counter
        /// RO.
        EIASAMPLE       OFFSET(0)   NUMBITS(32),
    ],

    Comparator0[
        /// Reference value for comparator 0.
        COMP       OFFSET(0)   NUMBITS(32),
    ],

    Comparator0Mask[
        /// Size of ignore mask aplied to the access address for address range matching by comparator 0.
        /// WARN: Maximum Mask size is IMPLEMENTATION DEFINED.
        MASK       OFFSET(0)   NUMBITS(5),
    ],

    Comparator0Function[
        /// Is one if comparator matches. Reading the register clears it to 0.
        /// RO.
        MATCHED     OFFSET(24)   NUMBITS(1),

        /// Second comparator number for linked address comparison.
        /// Works, when `DATAVMATCH` and `LNK1ENA` read as one.
        /// RW.
        DATAVADDR1  OFFSET(16)   NUMBITS(4),

        /// Comparator number for linked address comparison.
        /// Works, when `DATAVMATCH` reads as one.
        /// RW.
        DATAVADDR0  OFFSET(12)   NUMBITS(4),

        /// Size of data comparision (Byte, Halfword, Word).
        /// RW.
        DATAVSIZE   OFFSET(10)   NUMBITS(2),

        /// Reads as one if a second linked comparator is supported.
        LNK1ENA     OFFSET(9)    NUMBITS(1),

        /// Enables data value comparison
        /// When 0: Perform address comparison, when 1: data value comparison.
        /// RW.
        DATAVMATCH  OFFSET(8)    NUMBITS(1),

        /// Enable cycle count comparision for comparator 0.
        /// WARN: Only supported by FUNCTION0
        /// RW.
        CYCMATCH    OFFSET(7)    NUMBITS(1),

        /// Write 1 to enable generation of data trace address packets.
        /// WARN: If `Control::NOTRCPKT` reads as zero, this bit is UNKNOWN.
        /// RW.
        EMITRANGE   OFFSET(5)    NUMBITS(1),

        /// Selects action taken on comparator match.
        /// Resets to 0b0000.
        /// RW.
        FUNCTION    OFFSET(0)    NUMBITS(4),
    ],

    Comparator1[
        /// Reference value for comparator 0.
        COMP       OFFSET(0)   NUMBITS(32),
    ],

    Comparator1Mask[
        /// Size of ignore mask aplied to the access address for address range matching by comparator 0.
        /// WARN: Maximum Mask size is IMPLEMENTATION DEFINED.
        MASK       OFFSET(0)   NUMBITS(5),
    ],

    Comparator1Function[
        /// Is one if comparator matches. Reading the register clears it to 0.
        /// RO.
        MATCHED     OFFSET(24)   NUMBITS(1),

        /// Second comparator number for linked address comparison.
        /// Works, when `DATAVMATCH` and `LNK1ENA` read as one.
        /// RW.
        DATAVADDR1  OFFSET(16)   NUMBITS(4),

        /// Comparator number for linked address comparison.
        /// Works, when `DATAVMATCH` reads as one.
        /// RW.
        DATAVADDR0  OFFSET(12)   NUMBITS(4),

        /// Size of data comparision (Byte, Halfword, Word).
        /// RW.
        DATAVSIZE   OFFSET(10)   NUMBITS(2),

        /// Reads as one if a second linked comparator is supported.
        LNK1ENA     OFFSET(9)    NUMBITS(1),

        /// Enables data value comparison
        /// When 0: Perform address comparison, when 1: data value comparison.
        /// RW.
        DATAVMATCH  OFFSET(8)    NUMBITS(1),

        /// Write 1 to enable generation of data trace address packets.
        /// WARN: If `Control::NOTRCPKT` reads as zero, this bit is UNKNOWN.
        /// RW.
        EMITRANGE   OFFSET(5)    NUMBITS(1),

        /// Selects action taken on comparator match.
        /// Resets to 0b0000.
        /// RW.
        FUNCTION    OFFSET(0)    NUMBITS(4),
    ],

    Comparator2[
        /// Reference value for comparator 0.
        COMP       OFFSET(0)   NUMBITS(32),
    ],

    Comparator2Mask[
        /// Size of ignore mask aplied to the access address for address range matching by comparator 0.
        /// WARN: Maximum Mask size is IMPLEMENTATION DEFINED.
        MASK       OFFSET(0)   NUMBITS(5),
    ],

    Comparator2Function[
        /// Is one if comparator matches. Reading the register clears it to 0.
        /// RO.
        MATCHED     OFFSET(24)   NUMBITS(1),

        /// Second comparator number for linked address comparison.
        /// Works, when `DATAVMATCH` and `LNK1ENA` read as one.
        /// RW.
        DATAVADDR1  OFFSET(16)   NUMBITS(4),

        /// Comparator number for linked address comparison.
        /// Works, when `DATAVMATCH` reads as one.
        /// RW.
        DATAVADDR0  OFFSET(12)   NUMBITS(4),

        /// Size of data comparision (Byte, Halfword, Word).
        /// RW.
        DATAVSIZE   OFFSET(10)   NUMBITS(2),

        /// Reads as one if a second linked comparator is supported.
        LNK1ENA     OFFSET(9)    NUMBITS(1),

        /// Enables data value comparison
        /// When 0: Perform address comparison, when 1: data value comparison.
        /// RW.
        DATAVMATCH  OFFSET(8)    NUMBITS(1),

        /// Write 1 to enable generation of data trace address packets.
        /// WARN: If `Control::NOTRCPKT` reads as zero, this bit is UNKNOWN.
        /// RW.
        EMITRANGE   OFFSET(5)    NUMBITS(1),

        /// Selects action taken on comparator match.
        /// Resets to 0b0000.
        /// RW.
        FUNCTION    OFFSET(0)    NUMBITS(4),
    ],

    Comparator3[
        /// Reference value for comparator 0.
        COMP       OFFSET(0)   NUMBITS(32),
    ],

    Comparator3Mask[
        /// Size of ignore mask aplied to the access address for address range matching by comparator 0.
        /// WARN: Maximum Mask size is IMPLEMENTATION DEFINED.
        MASK       OFFSET(0)   NUMBITS(5),
    ],

    Comparator3Function[
        /// Is one if comparator matches. Reading the register clears it to 0.
        /// RO.
        MATCHED     OFFSET(24)   NUMBITS(1),

        /// Second comparator number for linked address comparison.
        /// Works, when `DATAVMATCH` and `LNK1ENA` read as one.
        /// RW.
        DATAVADDR1  OFFSET(16)   NUMBITS(4),

        /// Comparator number for linked address comparison.
        /// Works, when `DATAVMATCH` reads as one.
        /// RW.
        DATAVADDR0  OFFSET(12)   NUMBITS(4),

        /// Size of data comparision (Byte, Halfword, Word).
        /// RW.
        DATAVSIZE   OFFSET(10)   NUMBITS(2),

        /// Reads as one if a second linked comparator is supported.
        LNK1ENA     OFFSET(9)    NUMBITS(1),

        /// Enables data value comparison
        /// When 0: Perform address comparison, when 1: data value comparison.
        /// RW.
        DATAVMATCH  OFFSET(8)    NUMBITS(1),

        /// Write 1 to enable generation of data trace address packets.
        /// WARN: If `Control::NOTRCPKT` reads as zero, this bit is UNKNOWN.
        /// RW.
        EMITRANGE   OFFSET(5)    NUMBITS(1),

        /// Selects action taken on comparator match.
        /// Resets to 0b0000.
        /// RW.
        FUNCTION    OFFSET(0)    NUMBITS(4),
    ],

];

const DWT: StaticRef<DwtRegisters> = unsafe { StaticRef::new(0xE0001000 as *const DwtRegisters) };

pub struct Dwt {
    registers: StaticRef<DwtRegisters>,
}

impl Dwt {
    pub const fn new() -> Self {
        Self { registers: DWT }
    }

    /// Returns wether a cycle counter is present on the chip.
    pub fn is_cycle_counter_present(&self) -> bool {
        DWT.ctrl.read(Control::NOCYCCNT) == 0
    }
}

impl hil::hw_debug::CycleCounter for Dwt {
    fn start(&self) {
        if self.is_cycle_counter_present() {
            // The cycle counter has to be enabled in the DCB block
            dcb::enable_debug_and_trace();
            self.registers.ctrl.modify(Control::CYCNTENA::SET);
        }
    }

    fn stop(&self) {
        self.registers.ctrl.modify(Control::CYCNTENA::CLEAR);
    }

    fn count(&self) -> u64 {
        self.registers.cyccnt.read(CycleCount::CYCCNT) as u64
    }

    fn reset(&self) {
        self.registers.ctrl.modify(Control::CYCNTENA::CLEAR); // disable the counter
        self.registers.cyccnt.set(0); // reset the counter
    }
}