I measured the number of clock cycles on cortex m4 and now I would like to do this on cortex m7. The board I use is STM32F746ZG.
For m4, everything worked with:
volatile unsigned int *DWT_CYCCNT; volatile unsigned int *DWT_CONTROL; volatile unsigned int *SCB_DEMCR; void reset_cnt(){ DWT_CYCCNT = (volatile unsigned int *)0xE0001004;
The problem is that the DWT_CTRL register does not change when starting on m7 and remains 0x40000000 instead of changing to 0x40000001, so the number of cycles is always zero. From what I read in other posts, it looks like you need to set the FP_LAR register to 0xC5ACCE55 in order to be able to modify DWT_CTRL.
I added these definitions (tried both FP_LAR_PTR addresses below):
#define FP_LAR_PTR ((volatile unsigned int *) 0xe0000fb0) //according to reference //#define FP_LAR_PTR ((volatile unsigned int *) 0xe0002fb0) //according to guy on the internet // Lock Status Register lock status bit #define DWT_LSR_SLK_Pos 1 #define DWT_LSR_SLK_Msk (1UL << DWT_LSR_SLK_Pos) // Lock Status Register lock availability bit #define DWT_LSR_SLI_Pos 0 #define DWT_LSR_SLI_Msk (1UL << DWT_LSR_SLI_Pos) // Lock Access key, common for all #define DWT_LAR_KEY 0xC5ACCE55
and this function:
void dwt_access_enable(unsigned int ena){ volatile unsigned int *LSR; LSR = (volatile unsigned int *) 0xe0000fb4; uint32_t lsr = *LSR;; //printf("LSR: %.8X - SLI MASK: %.8X\n", lsr, DWT_LSR_SLI_Msk); if ((lsr & DWT_LSR_SLI_Msk) != 0) { if (ena) { //printf("LSR: %.8X - SLKMASK: %.8X\n", lsr, DWT_LSR_SLK_Msk); if ((lsr & DWT_LSR_SLK_Msk) != 0) { //locked: access need unlock *FP_LAR_PTR = DWT_LAR_KEY; printf("FP_LAR directly after change: 0x%.8X\n", *FP_LAR_PTR); } } else { if ((lsr & DWT_LSR_SLK_Msk) == 0) { //unlocked *FP_LAR_PTR = 0; //printf("FP_LAR directly after change: 0x%.8X\n", *FP_LAR_PTR); } } } }
When I call uncommented print, I get 0xC5ACCE55, but when I print it after the function returns, I get 0x00000000, and I have no idea why. Am I on the right track or is it completely wrong?
Edit: I think it would be nice to mention that I tried without all the extra code in the function and only tried to change the LAR register.
BR gustav
c arm embedded stm32f7
G. Johnsson
source share