Browse Source

add a header that implement cycle counting for some arm cortex-m cores..

main
John-Mark Gurney 2 years ago
parent
commit
15cac28819
1 changed files with 38 additions and 0 deletions
  1. +38
    -0
      stm32/cycle.h

+ 38
- 0
stm32/cycle.h View File

@@ -0,0 +1,38 @@
/* From: https://stackoverflow.com/questions/32610019/arm-m4-instructions-per-cycle-ipc-counters */
/* minor formating + type fixes */

volatile unsigned int *DWT_CYCCNT ;
volatile unsigned int *DWT_CONTROL ;
volatile unsigned int *SCB_DEMCR ;

static void
reset_timer()
{

DWT_CYCCNT = (unsigned int *)0xE0001004; //address of the register
DWT_CONTROL = (unsigned int *)0xE0001000; //address of the register
SCB_DEMCR = (unsigned int *)0xE000EDFC; //address of the register
*SCB_DEMCR = *SCB_DEMCR | 0x01000000;
*DWT_CYCCNT = 0; // reset the counter
*DWT_CONTROL = 0;
}

static void start_timer()
{

*DWT_CONTROL = *DWT_CONTROL | 1 ; // enable the counter
}

static void
stop_timer()
{

*DWT_CONTROL = *DWT_CONTROL | 0 ; // disable the counter
}

static unsigned int
getCycles()
{

return *DWT_CYCCNT;
}

Loading…
Cancel
Save