Disable peripherals in debug mode on STM32F4xx
Have you ever used watchdog in your application? Have you ever debug your application when watchdog was enabled and you hit stop? Watchdog did not stop in this case and when you pressed “continue” in your debugger, your program starts over because WDG has reset your system. One choice is that you remove line where you initialize IWDG, but you then must recompile and redownload and all that stuff. Wasting useful time.
To avoid this problem, STM32 devices have possibility to disable some peripherals when you are in debug mode. And one of that peripherals is also WDG timer which won’t count and won’t reset your system when you will be in debug mode, even if you enable it somewhere in your code. This gives you a possibility to STOP execution in your program (breakpoint) anytime and continue program without worries about system reset.
DBGMCU (DeBuG MicroController Unit) is called section in STM32 with possibility to disable other peripherals when you are in debug mode. For STM32F4xx series, you can disable these peripherals:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#define DBGMCU_TIM2_STOP ((uint32_t)0x00000001) #define DBGMCU_TIM3_STOP ((uint32_t)0x00000002) #define DBGMCU_TIM4_STOP ((uint32_t)0x00000004) #define DBGMCU_TIM5_STOP ((uint32_t)0x00000008) #define DBGMCU_TIM6_STOP ((uint32_t)0x00000010) #define DBGMCU_TIM7_STOP ((uint32_t)0x00000020) #define DBGMCU_TIM12_STOP ((uint32_t)0x00000040) #define DBGMCU_TIM13_STOP ((uint32_t)0x00000080) #define DBGMCU_TIM14_STOP ((uint32_t)0x00000100) #define DBGMCU_RTC_STOP ((uint32_t)0x00000400) #define DBGMCU_WWDG_STOP ((uint32_t)0x00000800) #define DBGMCU_IWDG_STOP ((uint32_t)0x00001000) #define DBGMCU_I2C1_SMBUS_TIMEOUT ((uint32_t)0x00200000) #define DBGMCU_I2C2_SMBUS_TIMEOUT ((uint32_t)0x00400000) #define DBGMCU_I2C3_SMBUS_TIMEOUT ((uint32_t)0x00800000) #define DBGMCU_CAN1_STOP ((uint32_t)0x02000000) #define DBGMCU_CAN2_STOP ((uint32_t)0x04000000) #define DBGMCU_TIM1_STOP ((uint32_t)0x00000001) #define DBGMCU_TIM8_STOP ((uint32_t)0x00000002) #define DBGMCU_TIM9_STOP ((uint32_t)0x00010000) #define DBGMCU_TIM10_STOP ((uint32_t)0x00020000) #define DBGMCU_TIM11_STOP ((uint32_t)0x00040000) |
Basically all timers only can be disabled with CAN and I2C. So, to disable IWDG in debug mode, you can do like in example below.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
/** * Keil project for independent watchdog, which is disabled in debug mode * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_disco.h" #include "tm_stm32f4_watchdog.h" int main(void) { /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize leds on board */ TM_DISCO_LedInit(); /* Initialize button */ TM_DISCO_ButtonInit(); /* Disable watchdog when we are in debug mode */ DBGMCU->APB1FZ |= DBGMCU_IWDG_STOP; /* Initialize watchdog timer */ /* Set timeout to 1s */ /* If we are in debug mode, watchdog won't start even if we enable it */ if (TM_WATCHDOG_Init(TM_WATCHDOG_Timeout_1s)) { /* System was reset by watchdog */ TM_DISCO_LedOn(LED_RED); } else { /* System was not reset by watchdog */ TM_DISCO_LedOn(LED_GREEN); } while (1) { /* If button is pressed, do nothing and system will be reset after 1 second */ while (TM_DISCO_ButtonPressed()); /* Reset watchdog */ TM_WATCHDOG_Reset(); } } |
Recent comments