Medición del recuento del ciclo del reloj en la corteza m7
He estado midiendo el recuento del ciclo del reloj en la corteza m4 y ahora me gustaría hacerlo en la corteza m7. La placa que uso es STM32F746ZG.
Para el m4 todo funcionó con:
volatile unsigned int *DWT_CYCCNT;
volatile unsigned int *DWT_CONTROL;
volatile unsigned int *SCB_DEMCR;
void reset_cnt(){
DWT_CYCCNT = (volatile unsigned int *)0xE0001004; //address of the register
DWT_CONTROL = (volatile unsigned int *)0xE0001000; //address of the register
SCB_DEMCR = (volatile unsigned int *)0xE000EDFC; //address of the register
*SCB_DEMCR = *SCB_DEMCR | 0x01000000;
*DWT_CYCCNT = 0; // reset the counter
*DWT_CONTROL = 0;
}
void start_cnt(){
*DWT_CONTROL = *DWT_CONTROL | 0x00000001 ; // enable the counter
}
void stop_cnt(){
*DWT_CONTROL = *DWT_CONTROL & 0xFFFFFFFE ; // disable the counter
}
unsigned int getCycles(){
return *DWT_CYCCNT;
}
El problema es que el registro DWT_CTRL no cambia cuando ejecuto el m7 y permanece 0x40000000 en lugar de cambiar a 0x40000001, por lo que el recuento de ciclos siempre es cero. Por lo que he leído en otras publicaciones, parece que necesita establecer el registro FP_LAR en 0xC5ACCE55 para poder cambiar DWT_CTRL.
Agregué estas definiciones (he probado las dos direcciones FP_LAR_PTR a continuación):
#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
y esta función:
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_P,TR);
}
}
}
}
Cuando llamo a la impresión no comentada obtengo 0xC5ACCE55 pero cuando la imprimo después del retorno de la función obtengo 0x00000000 y no tengo idea de por qué. ¿Estoy en el camino correcto o esto está completamente mal?
Editar: creo que también sería bueno mencionar que he intentado sin todo el código adicional en la función y solo he intentado cambiar el registro LAR.
BR Gustav