{"id":3120,"date":"2024-10-30T17:08:11","date_gmt":"2024-10-30T17:08:11","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=3120"},"modified":"2024-10-30T17:37:23","modified_gmt":"2024-10-30T17:37:23","slug":"iwdg-wwdg-sensor-checking","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=3120","title":{"rendered":"IWDG &amp; WWDG : Sensor Checking"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-1 wp-block-paragraph\" style=\"color:#5c5c5c\">In ATmel section we learn about watchdog timer. In STm32 we have system window watchdog (WWDG) &amp; independent watchdog (IWDG). Not all STm32 family has IWDG feature but all have WWDG feature. The system window watchdog (WWDG) is used to detect the occurrence of a software fault, usually generated by external interference or by unforeseen logical conditions, which causes the application program to abandon its normal sequence.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"305\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/function-11-1024x305.jpg\" alt=\"\" class=\"wp-image-3123\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/function-11-1024x305.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/function-11-300x89.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/function-11-768x229.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/function-11-1536x458.jpg 1536w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/function-11.jpg 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-2 wp-block-paragraph\" style=\"color:#5c5c5c\">It is basically a window were you have to refresh the counter otherwise the mcu will reset. The formula to calculate the timeout value is given by:<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3 wp-block-paragraph\" style=\"color:#252525\">             t<sub>WWDG <\/sub>= t<sub>PCLK<\/sub> x 4096 x2<sup>WDGTB[1:0] <\/sup>x (T[5:0]+1)\u00a0 (ms)<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4 wp-block-paragraph\" style=\"color:#5c5c5c\">Here t<sub>WWDG<\/sub>: WWDG timeout &amp; T<sub>PCLK<\/sub>: APB clock period measured in ms<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-5 wp-block-paragraph\" style=\"color:#5c5c5c\">As an example, lets assume APB frequency is equal to 48 MHz, WDGTB[1:0] is set to 3 and T[5:0] is set to 63, the window minimum time will be<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6 wp-block-paragraph\" style=\"color:#252525\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t<sub>WWDG <\/sub>= (1\/48000)*4096*2<sup>3<\/sup>*(63+1) = 43.69ms ~ 43ms<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-7 wp-block-paragraph\" style=\"color:#5c5c5c\">The maximum window value will be at T = 127 ,<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-8 wp-block-paragraph\" style=\"color:#252525\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t<sub>WWDG<\/sub> = (1\/48000)*4096*2<sup>3<\/sup>*(127+1) ~ 87ms<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-9 wp-block-paragraph\" style=\"color:#5c5c5c\">Thus if we refresh after 43ms the program will flow otherwise the mcu will reset continuously &amp; the range from 43ms to 87ms. Hall library function to refresh-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nHAL_WWDG_Refresh(&amp;hwwdg);   \/\/ refresh WWDG\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-10 wp-block-paragraph\" style=\"color:#5c5c5c\">IWDG : The devices feature an embedded watchdog peripheral that offers a combination of high safety level, timing accuracy and flexibility of use. The Independent watchdog peripheral detects and solves malfunctions due to software failure, and triggers system reset when the counter reaches a given timeout value.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-11 wp-block-paragraph\" style=\"color:#5c5c5c\">The independent watchdog (IWDG) is clocked by its own dedicated low-speed clock (LSI) and thus stays active even if the main clock fails.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-12 wp-block-paragraph\" style=\"color:#5c5c5c\">The formula to calculate the timeout value is given by:<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-13 wp-block-paragraph\" style=\"color:#252525\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 t<sub>IWDG<\/sub> = t<sub>LSI<\/sub> x 4 x 2<sup>PR<\/sup> x (RL +1) <\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-14 wp-block-paragraph\" style=\"color:#5c5c5c\">Where t<sub>LSI \u00a0<\/sub>is low-speed clock (LSI) frequency and PR &amp; RL are fields of IWDG registers. In our timer basic section we learn to interface digital temperature sensor DS18B20. Let look at the datasheet of STm mcu, there is a dedicated f<sub>LSI <\/sub>= 40kHz. If the prescaler for LSI clock 16, then every counting frequency will be 2.5kHz and time 0.4ms. If we set counting value 2<sup>12<\/sup> or 4096 then maximum time to reset will be 1.6384s.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"369\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/function-12-1024x369.jpg\" alt=\"\" class=\"wp-image-3130\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/function-12-1024x369.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/function-12-300x108.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/function-12-768x276.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/function-12.jpg 1403w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-15 wp-block-paragraph\" style=\"color:#5c5c5c\">f<sub>LSI &nbsp;&nbsp;<\/sub>= 40kHz &amp; &nbsp;prescaler 16 , then new f<sub>LSI <\/sub>= 2.5kHz , t<sub>LSI <\/sub>= 1\/f<sub>LSI<\/sub> = 0.4ms and RL = 4095,<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-16 wp-block-paragraph\" style=\"color:#252525\">\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 t<sub>IWDG <\/sub>= 0.4ms * (RL+1) \u00a0= 0.4 x 10<sup>-3 <\/sup>* (4095+1) = 1.6384s <\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-17 wp-block-paragraph\" style=\"color:#5c5c5c\">If we activate IWDG at prescaler 16, f<sub>LSI \u00a0<\/sub>40kHz &amp; counter reload value 4095, then we have 1.6384s to read the sensor data &amp; refresh the counter. If the counter will not refresh within 1.6s the microcontroller halt and restart again &amp; again. Hall library function to refresh-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nHAL_IWDG_Refresh(&amp;hiwdg);  \/\/ refresh IWDG Counter\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-18 wp-block-paragraph\" style=\"color:#5c5c5c\">The other logic will be same in timer section one additional part is to activate IWDG timer with the above parameter. In my example I used I<sup>2<\/sup>C1 for OLED &amp; GPIOB-> PINB5 for sensor reading. The main program as follow (only the change parameter)-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#include &quot;ssd1306.h&quot;\n#include &quot;ds18b20.h&quot;\n  uint8_t temp;\n  char display&#x5B;16]; \n  init_OLED();\n  clear_display();\n  OLCD_write_string(0,0,&quot;iotthinghub.com&quot;);\n  OLCD_write_string(2,0,&quot;Connecting Sen..&quot;);\n  HAL_Delay(1000);\n  MX_IWDG_Init();\nwhile (1)\n  {\n      temp=DS18B20ReadTemp();\n      if(temp) HAL_IWDG_Refresh(&amp;hiwdg);\n      else OLCD_write_string(2,0,&quot;Sensor Missing..&quot;); \/\/This line will not execute\n      if(temp) sprintf(display,&quot;Tem:%.4f C   &quot;,temperature);\n      OLCD_write_string(2,0,display);\n  }\n\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/STm32-DS18B20-1024x288.jpg\" alt=\"\" class=\"wp-image-2968\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/STm32-DS18B20-1024x288.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/STm32-DS18B20-300x84.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/STm32-DS18B20-768x216.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/STm32-DS18B20.jpg 1100w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-19 wp-block-paragraph\" style=\"color:#5c5c5c\">Connect as shown in the diagram, if you disconnect the GPIO pin PINB5 the mcu will restart again &amp; again. By modification the program GPIO &amp; \u00a0I<sup>2<\/sup>C you can used it in any STm32 microcontroller.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/IWDG-DS18B20.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--50);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--50)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"has-text-color has-link-color wp-elements-20 wp-block-paragraph\" style=\"color:#252525;font-size:16px\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/10\/IWDG-DS18B20.rar\">IWDG DS18B20.rar<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In ATmel section we learn about watchdog timer. In STm32 we have system window watchdog (WWDG) &amp; independent watchdog (IWDG). Not all STm32 family has IWDG feature but all have WWDG feature. The system window watchdog (WWDG) is used to detect the occurrence of a software fault, usually generated by external interference or by unforeseen [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-3120","post","type-post","status-publish","format-standard","hentry","category-stm-arm-tutorials"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3120"}],"version-history":[{"count":11,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3120\/revisions"}],"predecessor-version":[{"id":3142,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3120\/revisions\/3142"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}