comparison main.c @ 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 03592ca4d37e
children a38003b97de6
comparison
equal deleted inserted replaced
34:07589f738e5e 35:1fdfbad9eca7
347 maxerr = err; 347 maxerr = err;
348 } 348 }
349 printf("Max err = %.3f\r\n", maxerr); 349 printf("Max err = %.3f\r\n", maxerr);
350 } else if (!strcmp("assert", argv[0])) { 350 } else if (!strcmp("assert", argv[0])) {
351 assert(0 == 1); 351 assert(0 == 1);
352 } else if (!strcmp("cyc", argv[0])) {
353 // From http://forums.arm.com/index.php?/topic/13949-cycle-count-in-cortex-m3/
354 // via http://stackoverflow.com/questions/11530593/cycle-counter-on-arm-cortex-m4-or-m3/11530829#11530829
355 uint32_t c1, c2, c;
356 volatile uint32_t *DWT_CYCCNT = (uint32_t *)0xe0001004;
357 volatile uint32_t *DWT_CONTROL = (uint32_t *)0xe0001000;
358 volatile uint32_t *SCB_DEMCR = (uint32_t *)0xe000edfc;
359
360 *SCB_DEMCR = *SCB_DEMCR | 0x01000000;
361 *DWT_CONTROL = *DWT_CONTROL | 1 ; // enable the counter
362 c1 = *DWT_CYCCNT;
363 for (volatile int i = 0; i < 1000; i++)
364 ;
365 c2 = *DWT_CYCCNT;
366 if (c2 > c1)
367 c = c2 - c1;
368 else {
369 c = (0xffffffff - c1) + c2;
370 }
371 printf("Took %ld cycles\r\n", c);
352 } else if (!strcmp("zz", argv[0])) { 372 } else if (!strcmp("zz", argv[0])) {
353 NVIC_SystemReset(); 373 NVIC_SystemReset();
354 } else { 374 } else {
355 printf("Unknown command\r\n"); 375 printf("Unknown command\r\n");
356 } 376 }