A 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.
## **Overview**
## Available APIs<a name="section1180575010271"></a>
A watchdog, also called a watchdog timer, is a hardware timing device used to facilitate automatic correction of temporary hardware faults or recover from system malfunctions. If an error occurs in the main program of the system and the watchdog timer is not cleared in time, the watchdog timer sends a reset signal to restore the system to the normal state.
<tdclass="cellrowborder"valign="top"width="40.58%"headers="mcps1.2.4.1.3 "><pid="p17370161911710"><aname="p17370161911710"></a><aname="p17370161911710"></a>Opens a watchdog.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p11611715161713"><aname="p11611715161713"></a><aname="p11611715161713"></a>Closes a watchdog.</p>
<tdclass="cellrowborder"valign="top"width="40.58%"headers="mcps1.2.4.1.3 "><pid="p18376517332"><aname="p18376517332"></a><aname="p18376517332"></a>Starts a watchdog.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p19400194633318"><aname="p19400194633318"></a><aname="p19400194633318"></a>Stops a watchdog.</p>
<tdclass="cellrowborder"valign="top"width="40.58%"headers="mcps1.2.4.1.3 "><pid="p331961319210"><aname="p331961319210"></a><aname="p331961319210"></a>Obtains the watchdog status.</p>
<tdclass="cellrowborder"valign="top"width="40.58%"headers="mcps1.2.4.1.3 "><pid="p62815221125"><aname="p62815221125"></a><aname="p62815221125"></a>Feeds a watchdog, or resets a watchdog timer.</p>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p3138192512815"><aname="p3138192512815"></a><aname="p3138192512815"></a>Failed to open the watchdog.</p>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p16138122512817"><aname="p16138122512817"></a><aname="p16138122512817"></a>Pointer to the watchdog handle.</p>
</td>
</tr>
</tbody>
</table>
| **Parameter**| **Description**|
| -------- | -------- |
| wdtId | Watchdog ID.|
| **Return Value**| **Description**|
| NULL | The operation failed.|
| **DevHandle** pointer| The operation is successful. The pointer to the watchdog device handle is returned.|
```
DevHandle handle = NULL;
...
...
@@ -129,46 +65,23 @@ if (handle == NULL) {
}
```
### Obtaining the Watchdog Status<a name="section786624341011"></a>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p7282752412"><aname="p7282752412"></a><aname="p7282752412"></a>Pointer to the watchdog status.</p>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p1231981611712"><aname="p1231981611712"></a><aname="p1231981611712"></a>The watchdog status is obtained.</p>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p93191161174"><aname="p93191161174"></a><aname="p93191161174"></a>Failed to obtain the watchdog status.</p>
</td>
</tr>
</tbody>
</table>
| **Parameter**| **Description**|
| -------- | -------- |
| handle | Watchdog device handle.|
| status | Pointer to the watchdog status obtained.|
| **Return Value**| **Description**|
| 0 | The operation is successful.|
| Negative value| The operation failed.|
```
int32_t ret;
...
...
@@ -181,51 +94,28 @@ if (ret != 0) {
}
```
### Setting the Timeout Duration<a name="section182386137111"></a>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p5160172182214"><aname="p5160172182214"></a><aname="p5160172182214"></a>Timeout duration, in seconds.</p>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p151607218222"><aname="p151607218222"></a><aname="p151607218222"></a>The setting is successful.</p>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p71478484238"><aname="p71478484238"></a><aname="p71478484238"></a>Pointer to the timeout duration, in seconds.</p>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p1314824872310"><aname="p1314824872310"></a><aname="p1314824872310"></a>The watchdog status is obtained.</p>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p1314864822311"><aname="p1314864822311"></a><aname="p1314864822311"></a>Failed to obtain the watchdog status.</p>
</td>
</tr>
</tbody>
</table>
| **Parameter**| **Description**|
| -------- | -------- |
| handle | Pointer to the watchdog device handle.|
| seconds | Pointer to the timeout duration, in seconds.|
| **Return Value**| **Description**|
| 0 | The operation is successful.|
| Negative value| The operation failed.|
```
int32_t ret;
...
...
@@ -285,41 +152,22 @@ if (ret != 0) {
}
```
### Starting a Watchdog<a name="section82501405123"></a>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p630754259"><aname="p630754259"></a><aname="p630754259"></a>The watchdog is started.</p>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p0301559254"><aname="p0301559254"></a><aname="p0301559254"></a>Failed to start the watchdog.</p>
</td>
</tr>
</tbody>
</table>
| **Parameter**| **Description**|
| -------- | -------- |
| handle | Watchdog device handle.|
| **Return Value**| **Description**|
| 0 | The operation is successful.|
| Negative value| The operation failed.|
```
int32_t ret;
...
...
@@ -331,41 +179,22 @@ if (ret != 0) {
}
```
### Feeding a Watchdog<a name="section3547530101211"></a>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p149111435143911"><aname="p149111435143911"></a><aname="p149111435143911"></a>The watchdog is fed.</p>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p1891216356391"><aname="p1891216356391"></a><aname="p1891216356391"></a>Failed to feed the watchdog.</p>
</td>
</tr>
</tbody>
</table>
| **Parameter**| **Description**|
| -------- | -------- |
| handle | Watchdog device handle.|
| **Return Value**| **Description**|
| 0 | The operation is successful.|
| Negative value| The operation failed.|
```
int32_t ret;
...
...
@@ -377,41 +206,22 @@ if (ret != 0) {
}
```
### Stopping a Watchdog<a name="section944595841217"></a>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p7869115192510"><aname="p7869115192510"></a><aname="p7869115192510"></a>The watchdog is stopped.</p>
<tdclass="cellrowborder"valign="top"width="55.010000000000005%"headers="mcps1.2.3.1.2 "><pid="p086945172518"><aname="p086945172518"></a><aname="p086945172518"></a>Stopping the watchdog failed.</p>
</td>
</tr>
</tbody>
</table>
| **Parameter**| **Description**|
| -------- | -------- |
| handle | Watchdog device handle.|
| **Return Value**| **Description**|
| 0 | The operation is successful.|
| Negative value| The operation failed.|
```
int32_t ret;
...
...
@@ -423,44 +233,38 @@ if (ret != 0) {
}
```
### Closing a Watchdog<a name="section96561824121311"></a>
If the watchdog is no longer required, call **WatchdogClose** to close the watchdog handle.
### Closing a Watchdog
void WatchdogClose\(DevHandle handle\);
If a watchdog is no longer required, call **WatchdogClose()** to close it.
In the Hardware Driver Foundation \(HDF\), the Watchdog \(also called Watchdog timer\) module uses the independent service mode for API adaptation. In this mode, each device independently publishes a device service to handle external access requests. After receiving an access request from an API, the device manager extracts the parameters in the request to call the internal method of the target device. In the independent service mode, the service management capabilities of the HDF Device Manager can be directly used. However, you need to configure a device node for each device, which increases the memory usage.
A watchdog, also called a watchdog timer, is a hardware timing device used to facilitate automatic correction of temporary hardware faults or recover from system malfunctions.
**Figure 1** Independent service mode<aname="fig61584136211"></a>
In the Hardware Driver Foundation (HDF), the watchdog uses the independent service mode for API adaptation. In this mode, each device independently publishes a service to process external access requests. When receiving an access request, the HDF DeviceManager extracts parameters from the request to call the internal APIs of the target device. In the independent service mode, the HDF DeviceManager provides service management capabilities. However, you need to configure a node for each device, which increases memory usage.
## Available APIs<a name="section1180575010271"></a>
**Figure 1** Independent service mode

## **Available APIs**
**WatchdogMethod**:
WatchdogMethod
```
struct WatchdogMethod {
...
...
@@ -25,151 +30,85 @@ struct WatchdogMethod {
};
```
**Table 1** Callbacks for the members in the WatchdogMethod structure
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p124968261413"><aname="p124968261413"></a><aname="p124968261413"></a><strongid="b13481135102819"><aname="b13481135102819"></a><aname="b13481135102819"></a>wdt</strong>: structure pointer to the Watchdog controller at the core layer.</p>
</td>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.3 "><pid="p17071515316"><aname="p17071515316"></a><aname="p17071515316"></a><strongid="b142021397417"><aname="b142021397417"></a><aname="b142021397417"></a>status</strong>: int32_t pointer indicating the watchdog status (started or stopped).</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p2707511312"><aname="p2707511312"></a><aname="p2707511312"></a>Obtains the watchdog status.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p117071316317"><aname="p117071316317"></a><aname="p117071316317"></a><strongid="b136901310133110"><aname="b136901310133110"></a><aname="b136901310133110"></a>wdt</strong>: structure pointer to the Watchdog controller at the core layer.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p170811111319"><aname="p170811111319"></a><aname="p170811111319"></a>Starts a watchdog.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p870841138"><aname="p870841138"></a><aname="p870841138"></a><strongid="b14846151912249"><aname="b14846151912249"></a><aname="b14846151912249"></a>wdt</strong>: structure pointer to the Watchdog controller at the core layer.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p3708121738"><aname="p3708121738"></a><aname="p3708121738"></a>Stops a watchdog.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p2021212409407"><aname="p2021212409407"></a><aname="p2021212409407"></a><strongid="b1521224016409"><aname="b1521224016409"></a><aname="b1521224016409"></a>wdt</strong>: structure pointer to the Watchdog controller at the core layer.</p>
<pid="p1870801834"><aname="p1870801834"></a><aname="p1870801834"></a><strongid="b721091763217"><aname="b721091763217"></a><aname="b721091763217"></a>seconds</strong>: input time value, which is of the uint32_t type.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p57091717315"><aname="p57091717315"></a><aname="p57091717315"></a>Sets the timeout period (in seconds) for a watchdog. Ensure that the actual watchdog running time complies with this setting.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p47091711732"><aname="p47091711732"></a><aname="p47091711732"></a><strongid="b88511419192416"><aname="b88511419192416"></a><aname="b88511419192416"></a>wdt</strong>: structure pointer to the Watchdog controller at the core layer.</p>
</td>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.3 "><pid="p57091411632"><aname="p57091411632"></a><aname="p57091411632"></a><strongid="b3906135871316"><aname="b3906135871316"></a><aname="b3906135871316"></a>seconds</strong>: output time value, which is of the uint32_t type.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p17091511139"><aname="p17091511139"></a><aname="p17091511139"></a>Obtains the timeout period of a watchdog.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p570971736"><aname="p570971736"></a><aname="p570971736"></a><strongid="b1854319172417"><aname="b1854319172417"></a><aname="b1854319172417"></a>wdt</strong>: structure pointer to the Watchdog controller at the core layer.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p10709818316"><aname="p10709818316"></a><aname="p10709818316"></a>Feeds a watchdog. </p>
</td>
</tr>
</tbody>
</table>
## How to Develop<a name="section477974542160117"></a>
The Watchdog module adaptation involves the following steps:
**Table 1** Description of the callback functions in WatchdogMethod
| getStatus | **wdt**: structure pointer to the watchdog controller at the core layer.| **status**: int32_t pointer to the watchdog status (started or stopped).| HDF_STATUS| Obtains the watchdog status.|
| start | **wdt**: structure pointer to the watchdog controller at the core layer.| –| HDF_STATUS| Starts a watchdog.|
| stop | **wdt**: structure pointer to the watchdog controller at the core layer.| –| HDF_STATUS | Stops a watchdog.|
| setTimeout | **wdt**: structure pointer to the watchdog controller at the core layer.<br/>**seconds**: Timeout duration to set, in seconds. The value is of the uint32_t type. | – | HDF_STATUS | Sets the timeout duration for a watchdog. |
| getTimeout | **wdt**: structure pointer to the watchdog controller at the core layer.| **seconds**: Pointer to the watchdog timeout duration obtained. The value is of the uint32_t type. | HDF_STATUS| Obtains the timeout duration of a watchdog.|
| feed | **wdt**: structure pointer to the watchdog controller at the core layer.| –| HDF_STATUS| Feeds a watchdog. |
## How to Develop
The watchdog module adaptation involves the following steps:
1. Instantiate the driver entry.
- Instantiate the **HdfDriverEntry** structure.
- Call **HDF\_INIT** to register the **HdfDriverEntry** instance with the HDF.
- Call **HDF_INIT** to register the **HdfDriverEntry** instance with the HDF.
2. Configure attribute files.
- Add the **deviceNode** information to the **device\_info.hcs** file.
-\(Optional\) Add the **watchdog\_config.hcs** file.
- Add the **deviceNode** information to the **device_info.hcs** file.
- (Optional) Add the **watchdog_config.hcs** file.
3. Instantiate the Watchdog controller object.
3.Instantiate the watchdog controller object.
- Initialize **WatchdogCntlr**.
- Instantiate **WatchdogMethod** in the **WatchdogCntlr** object.
For details, see [Available APIs](#available-apis).
> For details about the functions in **WatchdogMethod**, see [Available APIs](#available-apis).
4. Debug the driver.
(Optional) For new drivers, verify basic functions, for example, check the information returned after the driver is attached and whether the watchdog timer is successfully set.
For new drivers, verify basic functions, for example, verify the information returned after the connect operation and whether the watchdog timer is successfully set.
## Development Example
## Development Example<a name="section1832270347160117"></a>
The following uses **watchdog_hi35xx.c** as an example to present the information required for implementing device functions.
The following uses **watchdog\_hi35xx.c** as an example to present the contents that need to be provided by the vendor to implement device functions.
1. Instantiate the driver entry.
1. Instantiate the driver entry. The driver entry must be a global variable of the **HdfDriverEntry** type \(defined in **hdf\_device\_desc.h**\), and the value of **moduleName** must be the same as that in **device\_info.hcs**. In the HDF, the start address of each **HdfDriverEntry** object of all loaded drivers is collected to form a segment address space similar to an array for the upper layer to invoke.
The driver entry must be a global variable of the **HdfDriverEntry** type (defined in **hdf_device_desc.h**), and the value of **moduleName** must be the same as that in **device_info.hcs**. In the HDF, the start address of each **HdfDriverEntry** object of all loaded drivers is collected to form a segment address space similar to an array for the upper layer to invoke.
Generally, HDF calls the **Bind** function and then the **Init** function to load a driver. If **Init** fails to be called, HDF calls **Release** to release driver resources and exit.
Generally, the HDF calls the **Bind** function and then the **Init** function to load a driver. If **Init** fails to be called, the HDF calls **Release** to release driver resources and exit.
- Watchdog driver entry reference
Watchdog driver entry example:
```
struct HdfDriverEntry g_watchdogDriverEntry = {
.moduleVersion = 1,
.Bind = Hi35xxWatchdogBind, // See the Bind function.
.Init = Hi35xxWatchdogInit, // See the Init function.
.Release = Hi35xxWatchdogRelease, //See the Release function.
.Release = Hi35xxWatchdogRelease, // See the Release function.
.moduleName = "HDF_PLATFORM_WATCHDOG",// (Mandatory) The value must be the same as that of moduleName in the .hcs file.
};
HDF_INIT(g_watchdogDriverEntry);// Call HDF_INIT to register the driver entry with the HDF.
```
2. Add the **deviceNode** information to the **device\_info.hcs** file and configure the component attributes in the **watchdog\_config.hcs** file. The **deviceNode** information is related to registration of the driver entry. The device attribute values are closely related to the default values or value ranges of the **WatchdogCntlr** members at the core layer.
2. Add the **deviceNode** information to the **device_info.hcs** file and configure the component attributes in the **watchdog_config.hcs** file.
The **deviceNode** information is related to registration of the driver entry. The device attribute values are closely related to the default values or value ranges of the **WatchdogCntlr** members at the core layer.
In this example, there is only one watchdog controller. If there are multiple watchdog controllers, you need to add the **deviceNode** information to the **device_info** file and add the corresponding device attributes to the **watchdog_config** file for each controller.
In this example, there is only one Watchdog controller. If there are multiple Watchdog controllers, you need to add the **deviceNode** information to the **device\_info** file and add the corresponding device attributes to the **watchdog\_config** file.
-**device_info.hcs** configuration example:
- **device\_info.hcs** configuration reference
```
root {
device_info {
match_attr = "hdf_manager";
device_watchdog :: device {// Device node
device0:: deviceNode {// DeviceNode of the driver
The policy = 1; // The value 1 indicates that the driver publishes kernel-mode services. The value 2 indicates that the driver publishes user-mode services.
priority = 20; // Driver startup priority
permission = 0644; // Permission to create device nodes for the driver
device_watchdog :: device {// Device node.
device0:: deviceNode { // Device node of the driver.
policy = 1; // Policy for the driver to provide services.
priority = 20; // Driver startup priority.
permission = 0644; // Permission to create device nodes for the driver.
moduleName = "HDF_PLATFORM_WATCHDOG";
// (Mandatory) Driver name. The value must be the same as that of moduleName in the driver entry structure.
serviceName = "HDF_PLATFORM_WATCHDOG_0";
...
...
@@ -182,12 +121,13 @@ The following uses **watchdog\_hi35xx.c** as an example to present the contents
template watchdog_controller {// Template configuration. In the template, you can configure the common parameters shared by service nodes.
template watchdog_controller {// (Mandatory) Template configuration. In the template, you can configure the common parameters shared by device nodes.
id = 0;
match_attr = "";
regBase = 0x12050000; // (Mandatory) Used for address mapping.
...
...
@@ -202,10 +142,11 @@ The following uses **watchdog\_hi35xx.c** as an example to present the contents
}
```
3. Initialize the **WatchdogCntlr** object at the core layer, including initializing the vendor custom structure \(passing parameters and data\), instantiating **WatchdogMethod**\(used to call underlying functions of the driver\) in **WatchdogCntlr**, and implementing the **HdfDriverEntry** member functions \(**Bind**, **Init**, and **Release**\).
- Custom structure reference
3. Initialize the **WatchdogCntlr** object at the core layer, including defining a custom structure (to pass parameters and data) and implementing the **HdfDriverEntry** member functions (**Bind**, **Init**, and **Release**) to instantiate **WatchdogMethod** in **WatchdogCntlr** (so that the underlying driver functions can be called).
- Defining a custom structure
To the driver, the custom structure holds parameters and data. The **DeviceResourceIface** method provided by the HDF reads the values in the **watchdog_config.hcs** file to initialize the members in the custom structure and passes important parameters, such as the index and the number of pins, to the **WatchdogCntlr** object at the core layer.
To the driver, the custom structure carries parameters and data. The values in the **watchdog\_config.hcs** file are read by HDF, and the structure members are initialized through **DeviceResourceIface**. Some important values, such as the index and the number of pins, are also passed to the **WatchdogCntlr** object at the core layer.
```
struct Hi35xxWatchdog {
...
...
@@ -215,18 +156,19 @@ The following uses **watchdog\_hi35xx.c** as an example to present the contents
uint32_t phyBase; // (Mandatory) Used for address mapping.
uint32_t regStep; // (Mandatory) Used for address mapping.
};
// WatchdogCntlr is the core layer controller structure. Its members are assigned with values by using the Init function.
// WatchdogCntlr is the core layer controller structure. The Init function assigns values to the members of WatchdogCntlr.
struct WatchdogCntlr {
struct IDeviceIoService service;// Driver service
struct HdfDeviceObject *device; // Drive device
OsalSpinlock lock; // This variable implements the spinlock function.
OsalSpinlock lock; // This variable is called by the HDF core layer to implement the spinlock function.
struct WatchdogMethod *ops; // Callbacks.
int16_t wdtId // ID of the watchdog device.
void *priv; // Pointer to the driver's private data.
};
```
- Instantiate the callback function structure **WatchdogMethod** in **WatchdogCntlr**. Other members are initialized by using the **Init** and **Bind** functions.
- Instantiating **WatchdogMethod** in **WatchdogCntlr** (other members are initialized by **Init** and **Bind**)
```
static struct WatchdogMethod g_method = {
...
...
@@ -239,59 +181,35 @@ The following uses **watchdog\_hi35xx.c** as an example to present the contents
};
```
- Init and Bind functions
Input parameters:
**HdfDeviceObject**: device object created by the HDF for each driver. It stores device-related private data and service APIs.
Return values:
HDF\_STATUS \(The following table lists some status. For details about other status, see **HDF\_STATUS** in the **//drivers/framework/include/utils/hdf\_base.h** file.\)
**Table 2** Input parameters and return values of the Init and Bind functions
**HdfDeviceObject**, a device object created by the HDF for each driver, holds device-related private data and service APIs.
Return value:
HDF_STATUS
The table below lists some status. For more information, see **HDF_STATUS** in the /drivers/framework/include/utils/hdf_base.h file.
**Table 2** HDF_STATUS
| Status| Description|
| -------- | -------- |
| HDF_ERR_INVALID_OBJECT | Failed to locate the watchdog device.|
| HDF_ERR_MALLOC_FAIL | Failed to allocate memory.|
| HDF_ERR_IO | I/O error.|
| HDF_SUCCESS | Initialization successful.|
| HDF_FAILURE | Initialization failed.|
Function description:
Initializes the custom structure object and **WatchdogCntlr**, and calls the **WatchdogCntlrAdd** function at the core layer.
```
// Generally, the Init function initializes the members of the Hi35xxWatchdog structure based on the attribute values of the input parameter (HdfDeviceObject).
// Generally, the Init function initializes the members of the Hi35xxWatchdog structure based on the attribute values in **HdfDeviceObject**.
// In this example, the Bind function initializes the Hi35xxWatchdog structure.
hwdt->wdt.priv = (void *)device->property;// (Optional) Assign the device attribute values to priv. However, priv is not called subsequently.
//If priv needs to be called, instantiate the getPriv and releasePriv member functions in WatchdogMethod.
hwdt->wdt.ops = &g_method; // (Mandatory) Assign the instantiated objects to the ops member so that the top layer can invoke the WatchdogMethod member functions.
//If the priv member is required, instantiate getPriv() and releasePriv() of WatchdogMethod.
hwdt->wdt.ops = &g_method; // (Mandatory) Assign the instantiated objects to the ops members so that the top layer can invoke the WatchdogMethod functions.
hwdt->wdt.device = device; // (Mandatory) Enable conversion between HdfDeviceObject and WatchdogcCntlr.
ret = WatchdogCntlrAdd(&hwdt->wdt); // (Mandatory) Call this function to initialize the structure of the core layer. The driver accesses the platform core layer only after a success signal is returned.
if (ret != HDF_SUCCESS) {// If the operation fails, release the resources used by the Init function.
if (ret != HDF_SUCCESS) { // If the operation fails, release the resources used by Init().
OsalIoUnmap((void *)hwdt->regBase);
OsalMemFree(hwdt);
return ret;
...
...
@@ -322,19 +240,22 @@ The following uses **watchdog\_hi35xx.c** as an example to present the contents
}
```
- Release function
-**Release** function
Input parameters:
Input parameter:
**HdfDeviceObject**: device object created by the HDF for each driver. It stores device-related private data and service APIs.
**HdfDeviceObject**, a device object created by the HDF for each driver, holds device-related private data and service APIs.
Return values:
Return value:
–
No value is returned.
Function description:
Releases the memory and deletes the controller. This function assigns a value to the **Release** API in the driver entry structure. When the HDF fails to call the **Init** function to initialize the driver, the **Release** function can be called to release driver resources. All forced conversion operations for obtaining the corresponding object can be successful only when the **Init** function has the corresponding value assignment operations.
Releases driver resources. This function assigns values to **Release()** in the driver entry structure. When the HDF fails to call the **Init** function to initialize the driver, **Release()** can be called to release driver resources. The **Release()** function must contain the operations for releasing the memory and deleting the controller.
All forced conversion operations for obtaining the corresponding object can be successful only when **Init()** has the corresponding value assignment operations.