changeset 35:1fdfbad9eca7

Add 'cyc' command to test CM3 debug cycle counter.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 11 Mar 2013 19:52:03 +1030
parents 07589f738e5e
children 2ecde7a4bc55
files main.c
diffstat 1 files changed, 20 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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 {