# HG changeset patch # User Daniel O'Connor # Date 1362993723 -37800 # Node ID 1fdfbad9eca7cf7f6f54c5e68e4d3766539eb8ce # Parent 07589f738e5ec3ccb1289122cc54c3a777508f8f Add 'cyc' command to test CM3 debug cycle counter. diff -r 07589f738e5e -r 1fdfbad9eca7 main.c --- a/main.c Mon Mar 11 19:51:34 2013 +1030 +++ b/main.c Mon Mar 11 19:52:03 2013 +1030 @@ -349,6 +349,26 @@ printf("Max err = %.3f\r\n", maxerr); } else if (!strcmp("assert", argv[0])) { assert(0 == 1); + } else if (!strcmp("cyc", argv[0])) { + // From http://forums.arm.com/index.php?/topic/13949-cycle-count-in-cortex-m3/ + // via http://stackoverflow.com/questions/11530593/cycle-counter-on-arm-cortex-m4-or-m3/11530829#11530829 + uint32_t c1, c2, c; + volatile uint32_t *DWT_CYCCNT = (uint32_t *)0xe0001004; + volatile uint32_t *DWT_CONTROL = (uint32_t *)0xe0001000; + volatile uint32_t *SCB_DEMCR = (uint32_t *)0xe000edfc; + + *SCB_DEMCR = *SCB_DEMCR | 0x01000000; + *DWT_CONTROL = *DWT_CONTROL | 1 ; // enable the counter + c1 = *DWT_CYCCNT; + for (volatile int i = 0; i < 1000; i++) + ; + c2 = *DWT_CYCCNT; + if (c2 > c1) + c = c2 - c1; + else { + c = (0xffffffff - c1) + c2; + } + printf("Took %ld cycles\r\n", c); } else if (!strcmp("zz", argv[0])) { NVIC_SystemReset(); } else {