uint8_t TM_GENERAL_DWTCounterEnable(void) { uint32_t c; /* Set clock speed if not already */ if (GENERAL_SystemSpeedInMHz == 0) { /* Get clock speed in MHz */ GENERAL_SystemSpeedInMHz = TM_GENERAL_GetClockSpeed(TM_GENERAL_Clock_SYSCLK) / 1000000; } /* Enable TRC */ CoreDebug->DEMCR &= ~0x01000000; CoreDebug->DEMCR |= 0x01000000; /* Enable counter */ DWT->CTRL &= ~0x00000001; DWT->CTRL |= 0x00000001; /* Reset counter */ DWT->CYCCNT = 0; /* Check if DWT has started */ c = DWT->CYCCNT; /* 2 dummys */ __ASM volatile ("NOP"); __ASM volatile ("NOP"); /* Return difference, if result is zero, DWT has not started */ return (DWT->CYCCNT - c); }
int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize leds on board */ TM_DISCO_LedInit(); /* Init button */ TM_DISCO_ButtonInit(); /* Init USART6, TX: PC6, 921600 baud */ TM_USART_Init(USART6, TM_USART_PinsPack_1, 921600); /* Get system reset source and clear flags after read */ printf("System reset source: %d\n", (uint8_t)TM_GENERAL_GetResetSource(1)); /* Get system reset source and clear flags after read */ /* You should see number which corresponds to "None", because we cleared flags in statement above */ printf("System reset source: %d\n", (uint8_t)TM_GENERAL_GetResetSource(1)); /* Get system core and PCLK1 (Peripheral Clock 1, APB1) clocks */ printf("System core clock: %u Hz; PCLK1 clock: %u Hz\n", TM_GENERAL_GetClockSpeed(TM_GENERAL_Clock_SYSCLK), TM_GENERAL_GetClockSpeed(TM_GENERAL_Clock_PCLK1) ); while (1) { /* If button pressed */ if (TM_DISCO_ButtonOnPressed()) { /* Send to USER */ printf("Software reset will happen in a moment\n"); /* Wait a little */ Delayms(500); /* Perform system software reset */ TM_GENERAL_SystemReset(); } } }