1# Watchdog<a name="EN-US_TOPIC_0000001052935969"></a> 2 3- [Overview](#section14918241977) 4 - [Available APIs](#section20177131219818) 5 6- [Usage Guidelines](#section10103184312813) 7 - [How to Use](#section10181195910815) 8 - [Opening a Watchdog](#section66089201107) 9 - [Obtaining the Watchdog Status](#section786624341011) 10 - [Setting the Timeout Duration](#section182386137111) 11 - [Obtaining the Timeout Duration](#section1883310371114) 12 - [Starting a Watchdog](#section82501405123) 13 - [Feeding a Watchdog](#section3547530101211) 14 - [Stopping a Watchdog](#section944595841217) 15 - [Closing a Watchdog](#section96561824121311) 16 17- [Usage Example](#section1724514523135) 18 19## Overview<a name="section14918241977"></a> 20 21A watchdog, also called a watchdog timer, is a hardware timing device. If an error occurs in the main program of the system and fails to reset the watchdog timer, the watchdog timer sends a reset signal to restore the system to a normal state. 22 23### Available APIs<a name="section20177131219818"></a> 24 25**Table 1** Watchdog APIs 26 27<a name="table1731550155318"></a> 28<table><thead align="left"><tr id="row4419501537"><th class="cellrowborder" valign="top" width="26.619999999999997%" id="mcps1.2.4.1.1"><p id="p641050105320"><a name="p641050105320"></a><a name="p641050105320"></a>Capability</p> 29</th> 30<th class="cellrowborder" valign="top" width="32.800000000000004%" id="mcps1.2.4.1.2"><p id="p54150165315"><a name="p54150165315"></a><a name="p54150165315"></a>Function</p> 31</th> 32<th class="cellrowborder" valign="top" width="40.58%" id="mcps1.2.4.1.3"><p id="p941150145313"><a name="p941150145313"></a><a name="p941150145313"></a>Description</p> 33</th> 34</tr> 35</thead> 36<tbody><tr id="row837081981712"><td class="cellrowborder" rowspan="2" valign="top" width="26.619999999999997%" headers="mcps1.2.4.1.1 "><p id="p681292481718"><a name="p681292481718"></a><a name="p681292481718"></a>Open/Close</p> 37</td> 38<td class="cellrowborder" valign="top" width="32.800000000000004%" headers="mcps1.2.4.1.2 "><p id="p183701419141710"><a name="p183701419141710"></a><a name="p183701419141710"></a>WatchdogOpen</p> 39</td> 40<td class="cellrowborder" valign="top" width="40.58%" headers="mcps1.2.4.1.3 "><p id="p17370161911710"><a name="p17370161911710"></a><a name="p17370161911710"></a>Opens a watchdog.</p> 41</td> 42</tr> 43<tr id="row5610415171719"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p661171510173"><a name="p661171510173"></a><a name="p661171510173"></a>WatchdogClose</p> 44</td> 45<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p11611715161713"><a name="p11611715161713"></a><a name="p11611715161713"></a>Closes a watchdog.</p> 46</td> 47</tr> 48<tr id="row337105133315"><td class="cellrowborder" rowspan="2" valign="top" width="26.619999999999997%" headers="mcps1.2.4.1.1 "><p id="p07631557153319"><a name="p07631557153319"></a><a name="p07631557153319"></a>Start/Stop</p> 49</td> 50<td class="cellrowborder" valign="top" width="32.800000000000004%" headers="mcps1.2.4.1.2 "><p id="p163765113337"><a name="p163765113337"></a><a name="p163765113337"></a>WatchdogStart</p> 51</td> 52<td class="cellrowborder" valign="top" width="40.58%" headers="mcps1.2.4.1.3 "><p id="p18376517332"><a name="p18376517332"></a><a name="p18376517332"></a>Starts a watchdog.</p> 53</td> 54</tr> 55<tr id="row18399184610337"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1740010461335"><a name="p1740010461335"></a><a name="p1740010461335"></a>WatchdogStop</p> 56</td> 57<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p19400194633318"><a name="p19400194633318"></a><a name="p19400194633318"></a>Stops a watchdog.</p> 58</td> 59</tr> 60<tr id="row34145016535"><td class="cellrowborder" rowspan="2" valign="top" width="26.619999999999997%" headers="mcps1.2.4.1.1 "><p id="p229610227124"><a name="p229610227124"></a><a name="p229610227124"></a>Timeout duration</p> 61</td> 62<td class="cellrowborder" valign="top" width="32.800000000000004%" headers="mcps1.2.4.1.2 "><p id="p8296182221219"><a name="p8296182221219"></a><a name="p8296182221219"></a>WatchdogSetTimeout</p> 63</td> 64<td class="cellrowborder" valign="top" width="40.58%" headers="mcps1.2.4.1.3 "><p id="p16297172213125"><a name="p16297172213125"></a><a name="p16297172213125"></a>Sets the watchdog timeout duration.</p> 65</td> 66</tr> 67<tr id="row11585016539"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1095722493616"><a name="p1095722493616"></a><a name="p1095722493616"></a>WatchdogGetTimeout</p> 68</td> 69<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p15297162215122"><a name="p15297162215122"></a><a name="p15297162215122"></a>Obtains the watchdog timeout duration.</p> 70</td> 71</tr> 72<tr id="row105701653185811"><td class="cellrowborder" valign="top" width="26.619999999999997%" headers="mcps1.2.4.1.1 "><p id="p2571145325819"><a name="p2571145325819"></a><a name="p2571145325819"></a>Status</p> 73</td> 74<td class="cellrowborder" valign="top" width="32.800000000000004%" headers="mcps1.2.4.1.2 "><p id="p175711953195814"><a name="p175711953195814"></a><a name="p175711953195814"></a>WatchdogGetStatus</p> 75</td> 76<td class="cellrowborder" valign="top" width="40.58%" headers="mcps1.2.4.1.3 "><p id="p331961319210"><a name="p331961319210"></a><a name="p331961319210"></a>Obtains the watchdog status.</p> 77</td> 78</tr> 79<tr id="row1028182217215"><td class="cellrowborder" valign="top" width="26.619999999999997%" headers="mcps1.2.4.1.1 "><p id="p182818227214"><a name="p182818227214"></a><a name="p182818227214"></a>Feeding</p> 80</td> 81<td class="cellrowborder" valign="top" width="32.800000000000004%" headers="mcps1.2.4.1.2 "><p id="p17281223219"><a name="p17281223219"></a><a name="p17281223219"></a>WatchdogFeed</p> 82</td> 83<td class="cellrowborder" valign="top" width="40.58%" headers="mcps1.2.4.1.3 "><p id="p62815221125"><a name="p62815221125"></a><a name="p62815221125"></a>Feeds a watchdog, or resets a watchdog timer.</p> 84</td> 85</tr> 86</tbody> 87</table> 88 89> **NOTE:** 90>All watchdog functions provided in this document can be called only in kernel mode. 91 92## Usage Guidelines<a name="section10103184312813"></a> 93 94### How to Use<a name="section10181195910815"></a> 95 96[Figure 1](#fig19134125410189) illustrates the process of using a watchdog. 97 98**Figure 1** Process of using a watchdog<a name="fig19134125410189"></a> 99 100 101 102 103### Opening a Watchdog<a name="section66089201107"></a> 104 105Use **WatchdogOpen** to open a watchdog. A system may have multiple watchdogs. You can open the specified watchdog by using the ID. 106 107int32\_t WatchdogOpen\(int16\_t wdtId\); 108 109**Table 2** Description of WatchdogOpen 110 111<a name="table1413702552814"></a> 112<table><thead align="left"><tr id="row131371325142819"><th class="cellrowborder" valign="top" width="44.99%" id="mcps1.2.3.1.1"><p id="p191372254283"><a name="p191372254283"></a><a name="p191372254283"></a><strong id="b9239182472212"><a name="b9239182472212"></a><a name="b9239182472212"></a>Parameter</strong></p> 113</th> 114<th class="cellrowborder" valign="top" width="55.010000000000005%" id="mcps1.2.3.1.2"><p id="p113819255284"><a name="p113819255284"></a><a name="p113819255284"></a><strong id="b2595142572219"><a name="b2595142572219"></a><a name="b2595142572219"></a>Description</strong></p> 115</th> 116</tr> 117</thead> 118<tbody><tr id="row813812259282"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p101381625162813"><a name="p101381625162813"></a><a name="p101381625162813"></a>wdtId</p> 119</td> 120<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p191381425142813"><a name="p191381425142813"></a><a name="p191381425142813"></a>Watchdog ID.</p> 121</td> 122</tr> 123<tr id="row2138202515281"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p141387252287"><a name="p141387252287"></a><a name="p141387252287"></a><strong id="b185321027172216"><a name="b185321027172216"></a><a name="b185321027172216"></a>Return Value</strong></p> 124</td> 125<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p12138192512281"><a name="p12138192512281"></a><a name="p12138192512281"></a><strong id="b29359286226"><a name="b29359286226"></a><a name="b29359286226"></a>Description</strong></p> 126</td> 127</tr> 128<tr id="row9138182519287"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p5138102532814"><a name="p5138102532814"></a><a name="p5138102532814"></a>NULL</p> 129</td> 130<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p3138192512815"><a name="p3138192512815"></a><a name="p3138192512815"></a>Failed to open the watchdog.</p> 131</td> 132</tr> 133<tr id="row15138192518283"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p1850115512916"><a name="p1850115512916"></a><a name="p1850115512916"></a><strong id="b17275323221"><a name="b17275323221"></a><a name="b17275323221"></a>DevHandle</strong> pointer</p> 134</td> 135<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p16138122512817"><a name="p16138122512817"></a><a name="p16138122512817"></a>Pointer to the watchdog handle.</p> 136</td> 137</tr> 138</tbody> 139</table> 140 141``` 142DevHandle handle = NULL; 143handle = WatchdogOpen(0); /* Open watchdog 0.*/ 144if (handle == NULL) { 145 HDF_LOGE("WatchdogOpen: failed, ret %d\n", ret); 146 return; 147} 148``` 149 150### Obtaining the Watchdog Status<a name="section786624341011"></a> 151 152int32\_t WatchdogGetStatus\(DevHandle handle, int32\_t \*status\); 153 154**Table 3** Description of WatchdogGetStatus 155 156<a name="table1018490043"></a> 157<table><thead align="left"><tr id="row31848013417"><th class="cellrowborder" valign="top" width="44.99%" id="mcps1.2.3.1.1"><p id="p1415816132411"><a name="p1415816132411"></a><a name="p1415816132411"></a><strong id="b8395121072312"><a name="b8395121072312"></a><a name="b8395121072312"></a>Parameter</strong></p> 158</th> 159<th class="cellrowborder" valign="top" width="55.010000000000005%" id="mcps1.2.3.1.2"><p id="p11158111316410"><a name="p11158111316410"></a><a name="p11158111316410"></a><strong id="b20193201115238"><a name="b20193201115238"></a><a name="b20193201115238"></a>Description</strong></p> 160</th> 161</tr> 162</thead> 163<tbody><tr id="row3264122711222"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p15264727182211"><a name="p15264727182211"></a><a name="p15264727182211"></a>handle</p> 164</td> 165<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p2026452772210"><a name="p2026452772210"></a><a name="p2026452772210"></a>Watchdog handle.</p> 166</td> 167</tr> 168<tr id="row928111518418"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p4282955412"><a name="p4282955412"></a><a name="p4282955412"></a>status</p> 169</td> 170<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p7282752412"><a name="p7282752412"></a><a name="p7282752412"></a>Pointer to the watchdog status.</p> 171</td> 172</tr> 173<tr id="row17393154515328"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p8158313248"><a name="p8158313248"></a><a name="p8158313248"></a><strong id="b12817162152319"><a name="b12817162152319"></a><a name="b12817162152319"></a>Return Value</strong></p> 174</td> 175<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p161591413741"><a name="p161591413741"></a><a name="p161591413741"></a><strong id="b112451923142314"><a name="b112451923142314"></a><a name="b112451923142314"></a>Description</strong></p> 176</td> 177</tr> 178<tr id="row339324593215"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p103191916578"><a name="p103191916578"></a><a name="p103191916578"></a>0</p> 179</td> 180<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p1231981611712"><a name="p1231981611712"></a><a name="p1231981611712"></a>The watchdog status is obtained.</p> 181</td> 182</tr> 183<tr id="row15393184519323"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p531916166716"><a name="p531916166716"></a><a name="p531916166716"></a>Negative value</p> 184</td> 185<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p93191161174"><a name="p93191161174"></a><a name="p93191161174"></a>Failed to obtain the watchdog status.</p> 186</td> 187</tr> 188</tbody> 189</table> 190 191``` 192int32_t ret; 193int32_t status; 194/* Obtain the watchdog status. */ 195ret = WatchdogGetStatus(handle, &status); 196if (ret != 0) { 197 HDF_LOGE("WatchdogGetStatus: failed, ret %d\n", ret); 198 return; 199} 200``` 201 202### Setting the Timeout Duration<a name="section182386137111"></a> 203 204int32\_t WatchdogSetTimeout\(PalHandle \*handle, uint32\_t seconds\); 205 206**Table 4** Description of WatchdogSetTimeout 207 208<a name="table9159112182210"></a> 209<table><thead align="left"><tr id="row1216012212212"><th class="cellrowborder" valign="top" width="44.99%" id="mcps1.2.3.1.1"><p id="p1416017262215"><a name="p1416017262215"></a><a name="p1416017262215"></a><strong id="b16573184672318"><a name="b16573184672318"></a><a name="b16573184672318"></a>Parameter</strong></p> 210</th> 211<th class="cellrowborder" valign="top" width="55.010000000000005%" id="mcps1.2.3.1.2"><p id="p16160182192213"><a name="p16160182192213"></a><a name="p16160182192213"></a><strong id="b8612347182320"><a name="b8612347182320"></a><a name="b8612347182320"></a>Description</strong></p> 212</th> 213</tr> 214</thead> 215<tbody><tr id="row199536232314"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p17685481236"><a name="p17685481236"></a><a name="p17685481236"></a>handle</p> 216</td> 217<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p196852811232"><a name="p196852811232"></a><a name="p196852811232"></a>Watchdog handle.</p> 218</td> 219</tr> 220<tr id="row141601729228"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p191601126226"><a name="p191601126226"></a><a name="p191601126226"></a>seconds</p> 221</td> 222<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p5160172182214"><a name="p5160172182214"></a><a name="p5160172182214"></a>Timeout duration, in seconds.</p> 223</td> 224</tr> 225<tr id="row18160192172212"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p101601123222"><a name="p101601123222"></a><a name="p101601123222"></a><strong id="b12598352112317"><a name="b12598352112317"></a><a name="b12598352112317"></a>Return Value</strong></p> 226</td> 227<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p17160192182212"><a name="p17160192182212"></a><a name="p17160192182212"></a><strong id="b145511753122319"><a name="b145511753122319"></a><a name="b145511753122319"></a>Description</strong></p> 228</td> 229</tr> 230<tr id="row171600202220"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p121601226224"><a name="p121601226224"></a><a name="p121601226224"></a>0</p> 231</td> 232<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p151607218222"><a name="p151607218222"></a><a name="p151607218222"></a>The setting is successful.</p> 233</td> 234</tr> 235<tr id="row916012252211"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p19160026224"><a name="p19160026224"></a><a name="p19160026224"></a>Negative value</p> 236</td> 237<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p816092142210"><a name="p816092142210"></a><a name="p816092142210"></a>Setting failed.</p> 238</td> 239</tr> 240</tbody> 241</table> 242 243``` 244int32_t ret; 245uint32_t timeOut = 60; 246/* Set the timeout duration, in seconds. */ 247ret = WatchdogSetTimeout(handle, timeOut); 248if (ret != 0) { 249 HDF_LOGE("WatchdogSetTimeout: failed, ret %d\n", ret); 250 return; 251} 252``` 253 254### Obtaining the Timeout Duration<a name="section1883310371114"></a> 255 256int32\_t WatchdogGetTimeout\(PalHandle \*handle, uint32\_t \*seconds\); 257 258**Table 5** Description of WatchdogGetTimeout 259 260<a name="table10147164819233"></a> 261<table><thead align="left"><tr id="row14147848142313"><th class="cellrowborder" valign="top" width="44.99%" id="mcps1.2.3.1.1"><p id="p4147124892316"><a name="p4147124892316"></a><a name="p4147124892316"></a><strong id="b1387481413247"><a name="b1387481413247"></a><a name="b1387481413247"></a>Parameter</strong></p> 262</th> 263<th class="cellrowborder" valign="top" width="55.010000000000005%" id="mcps1.2.3.1.2"><p id="p12147144817232"><a name="p12147144817232"></a><a name="p12147144817232"></a><strong id="b7800151515242"><a name="b7800151515242"></a><a name="b7800151515242"></a>Description</strong></p> 264</th> 265</tr> 266</thead> 267<tbody><tr id="row8147124819230"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p21471248142313"><a name="p21471248142313"></a><a name="p21471248142313"></a>handle</p> 268</td> 269<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p12147134815233"><a name="p12147134815233"></a><a name="p12147134815233"></a>Watchdog handle.</p> 270</td> 271</tr> 272<tr id="row514754818232"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p1614713484235"><a name="p1614713484235"></a><a name="p1614713484235"></a>seconds</p> 273</td> 274<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p71478484238"><a name="p71478484238"></a><a name="p71478484238"></a>Pointer to the timeout duration, in seconds.</p> 275</td> 276</tr> 277<tr id="row214784814239"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p5147848152314"><a name="p5147848152314"></a><a name="p5147848152314"></a><strong id="b2468182016249"><a name="b2468182016249"></a><a name="b2468182016249"></a>Return Value</strong></p> 278</td> 279<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p914724811236"><a name="p914724811236"></a><a name="p914724811236"></a><strong id="b18272182132419"><a name="b18272182132419"></a><a name="b18272182132419"></a>Description</strong></p> 280</td> 281</tr> 282<tr id="row714744892312"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p1014764832315"><a name="p1014764832315"></a><a name="p1014764832315"></a>0</p> 283</td> 284<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p1314824872310"><a name="p1314824872310"></a><a name="p1314824872310"></a>The watchdog status is obtained.</p> 285</td> 286</tr> 287<tr id="row1514884815230"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p10148114822319"><a name="p10148114822319"></a><a name="p10148114822319"></a>Negative value</p> 288</td> 289<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p1314864822311"><a name="p1314864822311"></a><a name="p1314864822311"></a>Failed to obtain the watchdog status.</p> 290</td> 291</tr> 292</tbody> 293</table> 294 295``` 296int32_t ret; 297uint32_t timeOut; 298/* Obtain the timeout duration, in seconds. */ 299ret = WatchdogGetTimeout(handle, &timeOut); 300if (ret != 0) { 301 HDF_LOGE("WatchdogGetTimeout: failed, ret %d\n", ret); 302 return; 303} 304``` 305 306### Starting a Watchdog<a name="section82501405123"></a> 307 308int32\_t WatchdogStart\(DevHandle handle\); 309 310**Table 6** Description of WatchdogStart 311 312<a name="table529165182515"></a> 313<table><thead align="left"><tr id="row92915122513"><th class="cellrowborder" valign="top" width="44.99%" id="mcps1.2.3.1.1"><p id="p5292582517"><a name="p5292582517"></a><a name="p5292582517"></a><strong id="b99151951162417"><a name="b99151951162417"></a><a name="b99151951162417"></a>Parameter</strong></p> 314</th> 315<th class="cellrowborder" valign="top" width="55.010000000000005%" id="mcps1.2.3.1.2"><p id="p929554258"><a name="p929554258"></a><a name="p929554258"></a><strong id="b121171651142418"><a name="b121171651142418"></a><a name="b121171651142418"></a>Description</strong></p> 316</th> 317</tr> 318</thead> 319<tbody><tr id="row629852250"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p22975122515"><a name="p22975122515"></a><a name="p22975122515"></a>handle</p> 320</td> 321<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p7290515256"><a name="p7290515256"></a><a name="p7290515256"></a>Watchdog handle.</p> 322</td> 323</tr> 324<tr id="row183035162514"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p8302511255"><a name="p8302511255"></a><a name="p8302511255"></a><strong id="b1190315442414"><a name="b1190315442414"></a><a name="b1190315442414"></a>Return Value</strong></p> 325</td> 326<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p16307522515"><a name="p16307522515"></a><a name="p16307522515"></a><strong id="b333525652414"><a name="b333525652414"></a><a name="b333525652414"></a>Description</strong></p> 327</td> 328</tr> 329<tr id="row12305552510"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p1730175132513"><a name="p1730175132513"></a><a name="p1730175132513"></a>0</p> 330</td> 331<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p630754259"><a name="p630754259"></a><a name="p630754259"></a>The watchdog is started.</p> 332</td> 333</tr> 334<tr id="row4306516252"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p15304502515"><a name="p15304502515"></a><a name="p15304502515"></a>Negative value</p> 335</td> 336<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p0301559254"><a name="p0301559254"></a><a name="p0301559254"></a>Failed to start the watchdog.</p> 337</td> 338</tr> 339</tbody> 340</table> 341 342``` 343int32_t ret; 344/* Start the watchdog. */ 345ret = WatchdogStart(handle); 346if (ret != 0) { 347 HDF_LOGE("WatchdogStart: failed, ret %d\n", ret); 348 return; 349} 350``` 351 352### Feeding a Watchdog<a name="section3547530101211"></a> 353 354int32\_t WatchdogFeed\(DevHandle handle\); 355 356**Table 7** Description of WatchdogFeed 357 358<a name="table091163515394"></a> 359<table><thead align="left"><tr id="row891133515393"><th class="cellrowborder" valign="top" width="44.99%" id="mcps1.2.3.1.1"><p id="p1911143513918"><a name="p1911143513918"></a><a name="p1911143513918"></a><strong id="b16850010142518"><a name="b16850010142518"></a><a name="b16850010142518"></a>Parameter</strong></p> 360</th> 361<th class="cellrowborder" valign="top" width="55.010000000000005%" id="mcps1.2.3.1.2"><p id="p1191173553917"><a name="p1191173553917"></a><a name="p1191173553917"></a><strong id="b8711511102516"><a name="b8711511102516"></a><a name="b8711511102516"></a>Description</strong></p> 362</th> 363</tr> 364</thead> 365<tbody><tr id="row189111635143918"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p189111435173917"><a name="p189111435173917"></a><a name="p189111435173917"></a>handle</p> 366</td> 367<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p11911143511397"><a name="p11911143511397"></a><a name="p11911143511397"></a>Watchdog handle.</p> 368</td> 369</tr> 370<tr id="row15911835173916"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p7911123516396"><a name="p7911123516396"></a><a name="p7911123516396"></a><strong id="b11359914162517"><a name="b11359914162517"></a><a name="b11359914162517"></a>Return Value</strong></p> 371</td> 372<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p169118356399"><a name="p169118356399"></a><a name="p169118356399"></a><strong id="b7180915162512"><a name="b7180915162512"></a><a name="b7180915162512"></a>Description</strong></p> 373</td> 374</tr> 375<tr id="row189119352393"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p1391113513917"><a name="p1391113513917"></a><a name="p1391113513917"></a>0</p> 376</td> 377<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p149111435143911"><a name="p149111435143911"></a><a name="p149111435143911"></a>The watchdog is fed.</p> 378</td> 379</tr> 380<tr id="row5911123520392"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p49111335143920"><a name="p49111335143920"></a><a name="p49111335143920"></a>Negative value</p> 381</td> 382<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p1891216356391"><a name="p1891216356391"></a><a name="p1891216356391"></a>Failed to feed the watchdog.</p> 383</td> 384</tr> 385</tbody> 386</table> 387 388``` 389int32_t ret; 390/* Feed the watchdog. */ 391ret = WatchdogFeed(handle); 392if (ret != 0) { 393 HDF_LOGE("WatchdogFeed: failed, ret %d\n", ret); 394 return; 395} 396``` 397 398### Stopping a Watchdog<a name="section944595841217"></a> 399 400int32\_t WatchdogStop\(DevHandle handle\); 401 402**Table 8** Description of WatchdogStop 403 404<a name="table1286810515254"></a> 405<table><thead align="left"><tr id="row28687517259"><th class="cellrowborder" valign="top" width="44.99%" id="mcps1.2.3.1.1"><p id="p6868185120254"><a name="p6868185120254"></a><a name="p6868185120254"></a><strong id="b204221347152519"><a name="b204221347152519"></a><a name="b204221347152519"></a>Parameter</strong></p> 406</th> 407<th class="cellrowborder" valign="top" width="55.010000000000005%" id="mcps1.2.3.1.2"><p id="p15868185114252"><a name="p15868185114252"></a><a name="p15868185114252"></a><strong id="b8153154862516"><a name="b8153154862516"></a><a name="b8153154862516"></a>Description</strong></p> 408</th> 409</tr> 410</thead> 411<tbody><tr id="row1868165114256"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p6869105115256"><a name="p6869105115256"></a><a name="p6869105115256"></a>handle</p> 412</td> 413<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p19869951202513"><a name="p19869951202513"></a><a name="p19869951202513"></a>Watchdog handle.</p> 414</td> 415</tr> 416<tr id="row68696510259"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p2869165114256"><a name="p2869165114256"></a><a name="p2869165114256"></a><strong id="b2116105210253"><a name="b2116105210253"></a><a name="b2116105210253"></a>Return Value</strong></p> 417</td> 418<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p158691551142517"><a name="p158691551142517"></a><a name="p158691551142517"></a><strong id="b8888115292518"><a name="b8888115292518"></a><a name="b8888115292518"></a>Description</strong></p> 419</td> 420</tr> 421<tr id="row9869851192516"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p686916516252"><a name="p686916516252"></a><a name="p686916516252"></a>0</p> 422</td> 423<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p7869115192510"><a name="p7869115192510"></a><a name="p7869115192510"></a>The watchdog is stopped.</p> 424</td> 425</tr> 426<tr id="row15869951122519"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p486925112518"><a name="p486925112518"></a><a name="p486925112518"></a>Negative value</p> 427</td> 428<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p086945172518"><a name="p086945172518"></a><a name="p086945172518"></a>Stopping the watchdog failed.</p> 429</td> 430</tr> 431</tbody> 432</table> 433 434``` 435int32_t ret; 436/* Stop the watchdog. */ 437ret = WatchdogStop(handle); 438if (ret != 0) { 439 HDF_LOGE("WatchdogStop: failed, ret %d\n", ret); 440 return; 441} 442``` 443 444### Closing a Watchdog<a name="section96561824121311"></a> 445 446If the watchdog is no longer required, call **WatchdogClose** to close the watchdog handle. 447 448void WatchdogClose\(DevHandle handle\); 449 450**Table 9** Description of WatchdogClose 451 452<a name="table1017315185320"></a> 453<table><thead align="left"><tr id="row417314182327"><th class="cellrowborder" valign="top" width="44.99%" id="mcps1.2.3.1.1"><p id="p117310184320"><a name="p117310184320"></a><a name="p117310184320"></a><strong id="b97421847122514"><a name="b97421847122514"></a><a name="b97421847122514"></a>Parameter</strong></p> 454</th> 455<th class="cellrowborder" valign="top" width="55.010000000000005%" id="mcps1.2.3.1.2"><p id="p7173191812324"><a name="p7173191812324"></a><a name="p7173191812324"></a><strong id="b14503748122514"><a name="b14503748122514"></a><a name="b14503748122514"></a>Description</strong></p> 456</th> 457</tr> 458</thead> 459<tbody><tr id="row1617331823211"><td class="cellrowborder" valign="top" width="44.99%" headers="mcps1.2.3.1.1 "><p id="p17173191811326"><a name="p17173191811326"></a><a name="p17173191811326"></a>handle</p> 460</td> 461<td class="cellrowborder" valign="top" width="55.010000000000005%" headers="mcps1.2.3.1.2 "><p id="p538814308323"><a name="p538814308323"></a><a name="p538814308323"></a>Watchdog handle.</p> 462</td> 463</tr> 464</tbody> 465</table> 466 467``` 468/* Close the watchdog. */ 469ret = WatchdogClose(handle); 470``` 471 472## Usage Example<a name="section1724514523135"></a> 473 474This example provides a complete process for using a watchdog. 475 476In this example, open a watchdog, set the timeout duration, and start the watchdog. 477 478- Feed the watchdog periodically to ensure that the system is not reset due to timer expiry. 479- Stop feeding the watchdog and check whether the system is reset after the timer expires. 480 481Example: 482 483``` 484#include "watchdog_if.h" 485#include "hdf_log.h" 486#include "osal_irq.h" 487#include "osal_time.h" 488 489#define WATCHDOG_TEST_TIMEOUT 2 490#define WATCHDOG_TEST_FEED_TIME 6 491 492static int32_t TestCaseWatchdog(void) 493{ 494 int32_t i; 495 int32_t ret; 496 uint32_t timeout; 497 DevHandle handle = NULL; 498 499 /* Open watchdog 0. */ 500 handle = WatchdogOpen(0); 501 if (handle == NULL) { 502 HDF_LOGE("Open watchdog fail!"); 503 return -1; 504 } 505 506 /* Set the timeout duration. */ 507 ret = WatchdogSetTimeout(handle, WATCHDOG_TEST_TIMEOUT); 508 if (ret != HDF_SUCCESS) { 509 HDF_LOGE("%s: set timeout fail! ret:%d\n", __func__, ret); 510 WatchdogClose(handle); 511 return ret; 512 } 513 514 /* Obtain the configured timeout duration. */ 515 ret = WatchdogGetTimeout(handle, &timeout); 516 if (ret != HDF_SUCCESS) { 517 HDF_LOGE("%s: get timeout fail! ret:%d\n", __func__, ret); 518 WatchdogClose(handle); 519 return ret; 520 } 521 HDF_LOGI("%s: read timeout back:%u\n", __func__, timeout); 522 523 /* Start the watchdog. The timer starts. */ 524 ret = WatchdogStart(handle); 525 if (ret != HDF_SUCCESS) { 526 HDF_LOGE("%s: satrt fail! ret:%d\n", __func__, ret); 527 WatchdogClose(handle); 528 return ret; 529 } 530 531 /* Feed the watchdog every 1s. */ 532 for (i = 0; i < WATCHDOG_TEST_FEED_TIME; i++) { 533 HDF_LOGE("%s: feeding watchdog %d times... \n", __func__, i); 534 ret = WatchdogFeed(handle); 535 if (ret != HDF_SUCCESS) { 536 HDF_LOGE("%s: feed dog fail! ret:%d\n", __func__, ret); 537 WatchdogClose(handle); 538 return ret; 539 } 540 OsalSleep(1); 541 } 542 /* Because the interval for feeding the watchdog is shorter than the timeout duration, the system does not reset, and logs can be printed normally. */ 543 HDF_LOGE("%s: no reset ... feeding test OK!!!\n", __func__); 544 545 /* Enable the timer to expire by stopping feeding the watchdog. */ 546 for (i = 0; i < WATCHDOG_TEST_FEED_TIME; i++) { 547 HDF_LOGE("%s: watiting dog buck %d times... \n", __func__, i); 548 OsalSleep(1); 549 } 550 551 /* The system resets when the timer expires. If the code is correct, the log below is not displayed. */ 552 HDF_LOGE("%s: dog has't buck!!! \n", __func__, i); 553 WatchdogClose(handle); 554 return -1; 555} 556``` 557 558