@@ -84,7 +84,9 @@ static void iwdg_stm32_convert_timeout(uint32_t timeout,
8484
8585static 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)
123141static 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
0 commit comments