Skip to content

Commit 842a6ba

Browse files
FRASTMfabiobaltieri
authored andcommitted
drivers: watchdog: stm32 iwdt enable and update during setup
To follow the IWDG configuration sequence, the timeout install is just preparing the reload and prescaler parameters. Then during the iwdg setup the watchdog is enabled and configured at the same time. Signed-off-by: Francois Ramu <[email protected]>
1 parent 66a4fab commit 842a6ba

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

drivers/watchdog/wdt_iwdg_stm32.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ static void iwdg_stm32_convert_timeout(uint32_t timeout,
8484

8585
static int iwdg_stm32_setup(const struct device *dev, uint8_t options)
8686
{
87+
struct iwdg_stm32_data *data = IWDG_STM32_DATA(dev);
8788
IWDG_TypeDef *iwdg = IWDG_STM32_STRUCT(dev);
89+
uint32_t tickstart;
8890

8991
/* Deactivate running when debugger is attached. */
9092
if (options & WDT_OPT_PAUSE_HALTED_BY_DBG) {
@@ -106,8 +108,24 @@ static int iwdg_stm32_setup(const struct device *dev, uint8_t options)
106108
return -ENOTSUP;
107109
}
108110

109-
/* Enable the IWDG now (timeout has been installed previoulsy) */
110-
LL_IWDG_Enable(iwdg); /* No need to Reload counter */
111+
/* Enable the IWDG now and write IWDG registers at the same time */
112+
LL_IWDG_Enable(iwdg);
113+
LL_IWDG_EnableWriteAccess(iwdg);
114+
/* Write the prescaler and reload counter to the IWDG registers*/
115+
LL_IWDG_SetPrescaler(iwdg, data->prescaler);
116+
LL_IWDG_SetReloadCounter(iwdg, data->reload);
117+
118+
tickstart = k_uptime_get_32();
119+
120+
/* Wait for the update operation completed */
121+
while (LL_IWDG_IsReady(iwdg) == 0) {
122+
if ((k_uptime_get_32() - tickstart) > IWDG_SR_UPDATE_TIMEOUT) {
123+
return -ENODEV;
124+
}
125+
}
126+
127+
/* Reload counter just before leaving */
128+
LL_IWDG_ReloadCounter(iwdg);
111129

112130
return 0;
113131
}
@@ -123,16 +141,16 @@ static int iwdg_stm32_disable(const struct device *dev)
123141
static int iwdg_stm32_install_timeout(const struct device *dev,
124142
const struct wdt_timeout_cfg *config)
125143
{
126-
IWDG_TypeDef *iwdg = IWDG_STM32_STRUCT(dev);
144+
struct iwdg_stm32_data *data = IWDG_STM32_DATA(dev);
127145
uint32_t timeout = config->window.max * USEC_PER_MSEC;
128146
uint32_t prescaler = 0U;
129147
uint32_t reload = 0U;
130-
uint32_t tickstart;
131148

132149
if (config->callback != NULL) {
133150
return -ENOTSUP;
134151
}
135152

153+
/* Calculating parameters to be applied later, on setup */
136154
iwdg_stm32_convert_timeout(timeout, &prescaler, &reload);
137155

138156
if (!(IS_IWDG_TIMEOUT(timeout) && IS_IWDG_PRESCALER(prescaler) &&
@@ -141,19 +159,11 @@ static int iwdg_stm32_install_timeout(const struct device *dev,
141159
return -EINVAL;
142160
}
143161

144-
tickstart = k_uptime_get_32();
145-
146-
/* Do not enable the wdg during install but during wdt_setup() */
147-
LL_IWDG_EnableWriteAccess(iwdg);
148-
LL_IWDG_SetPrescaler(iwdg, prescaler);
149-
150-
/* Wait for the update operation completed */
151-
while (LL_IWDG_IsActiveFlag_PVU(iwdg) == 0) {
152-
if ((k_uptime_get_32() - tickstart) > IWDG_SR_UPDATE_TIMEOUT) {
153-
return -ENODEV;
154-
}
155-
}
162+
/* Store the calculated values to write in the iwdg registers */
163+
data->prescaler = prescaler;
164+
data->reload = reload;
156165

166+
/* Do not enable and update the iwdg here but during wdt_setup() */
157167
return 0;
158168
}
159169

drivers/watchdog/wdt_iwdg_stm32.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
struct iwdg_stm32_data {
2525
/* IWDG peripheral instance. */
2626
IWDG_TypeDef *Instance;
27+
uint32_t prescaler;
28+
uint32_t reload;
2729
};
2830

2931
#define IWDG_STM32_DATA(dev) \

0 commit comments

Comments
 (0)