diff --git a/en/device-dev/driver/driver-platform-watchdog-des.md b/en/device-dev/driver/driver-platform-watchdog-des.md index deb85133a3dde4f11cfb03762f4f4fd9d656d51b..aa00d54869576d625e05eead5cbe237a81d560e8 100644 --- a/en/device-dev/driver/driver-platform-watchdog-des.md +++ b/en/device-dev/driver/driver-platform-watchdog-des.md @@ -1,124 +1,60 @@ -# Watchdog - -## Overview - -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. - -## Available APIs - -**Table 1** Watchdog APIs - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Capability

-

Function

-

Description

-

Open/Close

-

WatchdogOpen

-

Opens a watchdog.

-

WatchdogClose

-

Closes a watchdog.

-

Start/Stop

-

WatchdogStart

-

Starts a watchdog.

-

WatchdogStop

-

Stops a watchdog.

-

Timeout duration

-

WatchdogSetTimeout

-

Sets the watchdog timeout duration.

-

WatchdogGetTimeout

-

Obtains the watchdog timeout duration.

-

Status

-

WatchdogGetStatus

-

Obtains the watchdog status.

-

Feeding

-

WatchdogFeed

-

Feeds a watchdog, or resets a watchdog timer.

-
- ->![](../public_sys-resources/icon-note.gif) **NOTE**
->All watchdog functions provided in this document can be called only in kernel mode. - -## Usage Guidelines - -### How to Use - -The figure below illustrates how to use the APIs. - -**Figure 1** Using watchdog driver APIs -![](figures/using-watchdog-process.png "process-of-using-a-watchdog") - -### Opening a Watchdog - -Use **WatchdogOpen** to open a watchdog. A system may have multiple watchdogs. You can open the specified watchdog by using the ID. - -int32\_t WatchdogOpen\(int16\_t wdtId\); - -**Table 2** Description of WatchdogOpen - - - - - - - - - - - - - - - - - - - -

Parameter

-

Description

-

wdtId

-

Watchdog ID.

-

Return Value

-

Description

-

NULL

-

Failed to open the watchdog.

-

DevHandle pointer

-

Pointer to the watchdog handle.

-
+# Watchdog + + +## **Overview** + +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. + + +## Available APIs + +**Table 1** Watchdog APIs + +| API| Description| +| -------- | -------- | +| WatchdogOpen | Opens a watchdog.| +| WatchdogClose | Closes a watchdog.| +| WatchdogStart | Starts a watchdog.| +| WatchdogStop | Stops a watchdog.| +| WatchdogSetTimeout | Sets the watchdog timeout duration.| +| WatchdogGetTimeout | Obtains the watchdog timeout duration.| +| WatchdogGetStatus | Obtains the watchdog status.| +| WatchdogFeed | Feeds a watchdog or resets a watchdog timer.| + +> ![](../public_sys-resources/icon-note.gif) **NOTE** +> +> All watchdog APIs provided in this document can be called only in kernel mode. + + +## Usage Guidelines + + +### How to Use + +The figure below shows how to use the watchdog APIs. + +Figure 1 Using watchdog APIs + +![image](figures/using-watchdog-process.png) + + +### Opening a Watchdog + +Use **WatchdogOpen()** to open a watchdog. A system may have multiple watchdogs. You need to specify the ID of the watchdog to open. + +``` +DevHandle WatchdogOpen(int16_t wdtId); +``` + +**Table 2** Description of WatchdogOpen + +| **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 - -int32\_t WatchdogGetStatus\(DevHandle handle, int32\_t \*status\); - -**Table 3** Description of WatchdogGetStatus - - - - - - - - - - - - - - - - - - - - - - -

Parameter

-

Description

-

handle

-

Watchdog handle.

-

status

-

Pointer to the watchdog status.

-

Return Value

-

Description

-

0

-

The watchdog status is obtained.

-

Negative value

-

Failed to obtain the watchdog status.

-
+ +### Obtaining the Watchdog Status + +``` +int32_t WatchdogGetStatus(DevHandle handle, int32_t *status); +``` + +**Table 3** Description of WatchdogGetStatus + +| **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 - -int32\_t WatchdogSetTimeout\(PalHandle \*handle, uint32\_t seconds\); - -**Table 4** Description of WatchdogSetTimeout - - - - - - - - - - - - - - - - - - - - - - -

Parameter

-

Description

-

handle

-

Watchdog handle.

-

seconds

-

Timeout duration, in seconds.

-

Return Value

-

Description

-

0

-

The setting is successful.

-

Negative value

-

Setting failed.

-
+ +### Setting the Timeout Duration + +``` +int32_t WatchdogSetTimeout(DevHandle *handle, uint32_t seconds); +``` + +**Table 4** Description of WatchdogSetTimeout + +| **Parameter**| **Description**| +| -------- | -------- | +| handle | Pointer to the watchdog device handle.| +| seconds | Timeout duration to set, in seconds.| +| **Return Value**| **Description**| +| 0 | The operation is successful.| +| Negative value| The operation failed.| + ``` int32_t ret; uint32_t timeOut = 60; -/* Set the timeout duration, in seconds. */ +/* Set the timeout duration to 60 seconds. */ ret = WatchdogSetTimeout(handle, timeOut); if (ret != 0) { HDF_LOGE("WatchdogSetTimeout: failed, ret %d\n", ret); @@ -233,46 +123,23 @@ if (ret != 0) { } ``` -### Obtaining the Timeout Duration - -int32\_t WatchdogGetTimeout\(PalHandle \*handle, uint32\_t \*seconds\); - -**Table 5** Description of WatchdogGetTimeout - - - - - - - - - - - - - - - - - - - - - - -

Parameter

-

Description

-

handle

-

Watchdog handle.

-

seconds

-

Pointer to the timeout duration, in seconds.

-

Return Value

-

Description

-

0

-

The watchdog status is obtained.

-

Negative value

-

Failed to obtain the watchdog status.

-
+ +### Obtaining the Timeout Duration + +``` +int32_t WatchdogGetTimeout(DevHandle *handle, uint32_t *seconds); +``` + +**Table 5** Description of WatchdogGetTimeout + +| **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 - -int32\_t WatchdogStart\(DevHandle handle\); - -**Table 6** Description of WatchdogStart - - - - - - - - - - - - - - - - - - - -

Parameter

-

Description

-

handle

-

Watchdog handle.

-

Return Value

-

Description

-

0

-

The watchdog is started.

-

Negative value

-

Failed to start the watchdog.

-
+ +### Starting a Watchdog + +``` +int32_t WatchdogStart(DevHandle handle); +``` + +**Table 6** Description of WatchdogStart + +| **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 - -int32\_t WatchdogFeed\(DevHandle handle\); - -**Table 7** Description of WatchdogFeed - - - - - - - - - - - - - - - - - - - -

Parameter

-

Description

-

handle

-

Watchdog handle.

-

Return Value

-

Description

-

0

-

The watchdog is fed.

-

Negative value

-

Failed to feed the watchdog.

-
+ +### Feeding a Watchdog + +``` +int32_t WatchdogFeed(DevHandle handle); +``` + +**Table 7** Description of WatchdogFeed + +| **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 - -int32\_t WatchdogStop\(DevHandle handle\); - -**Table 8** Description of WatchdogStop - - - - - - - - - - - - - - - - - - - -

Parameter

-

Description

-

handle

-

Watchdog handle.

-

Return Value

-

Description

-

0

-

The watchdog is stopped.

-

Negative value

-

Stopping the watchdog failed.

-
+ +### Stopping a Watchdog + +``` +int32_t WatchdogStop(DevHandle handle); +``` + +**Table 8** Description of WatchdogStop + +| **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 -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. -**Table 9** Description of WatchdogClose +``` +void WatchdogClose(DevHandle handle); +``` + +**Table 9** Description of WatchdogClose + +| **Parameter**| **Description**| +| -------- | -------- | +| handle | Watchdog device handle.| - - - - - - - - - -

Parameter

-

Description

-

handle

-

Watchdog handle.

-
``` /* Close the watchdog. */ ret = WatchdogClose(handle); ``` -## Usage Example -This example provides a complete process for using a watchdog. +## Example -In this example, open a watchdog, set the timeout duration, and start the watchdog. +The following example provides the complete development process. -- Feed the watchdog periodically to ensure that the system is not reset due to timer expiry. -- Stop feeding the watchdog and check whether the system is reset after the timer expires. +1. Open a watchdog, set the timeout duration, and start the watchdog. -Example: +2. Feed the watchdog periodically to ensure that the system is not reset due to timer expiry. +3. Stop feeding the watchdog and check whether the system is reset after the timer expires. + +Sample code: ``` #include "watchdog_if.h" @@ -481,7 +285,7 @@ static int32_t TestCaseWatchdog(void) /* Open watchdog 0. */ handle = WatchdogOpen(0); if (handle == NULL) { - HDF_LOGE("Open watchdog fail!"); + HDF_LOGE("Open watchdog failed!"); return -1; } @@ -493,7 +297,7 @@ static int32_t TestCaseWatchdog(void) return ret; } - /* Obtain the configured timeout duration. */ + /* Obtain the timeout duration. */ ret = WatchdogGetTimeout(handle, &timeout); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: get timeout fail! ret:%d\n", __func__, ret); @@ -505,14 +309,14 @@ static int32_t TestCaseWatchdog(void) /* Start the watchdog. The timer starts. */ ret = WatchdogStart(handle); if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: satrt fail! ret:%d\n", __func__, ret); + HDF_LOGE("%s: start fail! ret:%d\n", __func__, ret); WatchdogClose(handle); return ret; } - /* Feed the watchdog every 1s. */ + /* Feed the watchdog every other second. */ for (i = 0; i < WATCHDOG_TEST_FEED_TIME; i++) { - HDF_LOGE("%s: feeding watchdog %d times... \n", __func__, i); + HDF_LOGI("%s: feeding watchdog %d times... \n", __func__, i); ret = WatchdogFeed(handle); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: feed dog fail! ret:%d\n", __func__, ret); @@ -522,17 +326,17 @@ static int32_t TestCaseWatchdog(void) OsalSleep(1); } /* Because the interval for feeding the watchdog is shorter than the timeout duration, the system does not reset, and logs can be printed normally. */ - HDF_LOGE("%s: no reset ... feeding test OK!!!\n", __func__); + HDF_LOGI("%s: no reset ... feeding test OK!!!\n", __func__); - /* Enable the timer to expire by stopping feeding the watchdog. */ + /* Stop feeding the watchdog to make the timer expire. */ for (i = 0; i < WATCHDOG_TEST_FEED_TIME; i++) { - HDF_LOGE("%s: watiting dog buck %d times... \n", __func__, i); + HDF_LOGI("%s: waiting dog buck %d times... \n", __func__, i); OsalSleep(1); } - /* The system resets when the timer expires. If the code is correct, the log below is not displayed. */ - HDF_LOGE("%s: dog has't buck!!! \n", __func__, i); + /* The system resets when the timer expires. Theoretically, this log is not displayed. */ + HDF_LOGI("%s: dog hasn't back!!! \n", __func__, i); WatchdogClose(handle); return -1; } -``` \ No newline at end of file +``` diff --git a/en/device-dev/driver/driver-platform-watchdog-develop.md b/en/device-dev/driver/driver-platform-watchdog-develop.md index 13898cfeaafd8803834c5b62f819e56ecfde96e7..d153a4ae1788127dd08a0cf8c084c3cecc1b73fd 100644 --- a/en/device-dev/driver/driver-platform-watchdog-develop.md +++ b/en/device-dev/driver/driver-platform-watchdog-develop.md @@ -1,16 +1,21 @@ -# Watchdog +# Watchdog -## Overview +## **Overview** -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 -![](figures/independent-service-mode.png "independent-service-mode-15") +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 + **Figure 1** Independent service mode + + ![image](figures/independent-service-mode.png "Watchdog independent service mode") + + +## **Available APIs** + +**WatchdogMethod**: -WatchdogMethod ``` struct WatchdogMethod { @@ -25,334 +30,250 @@ struct WatchdogMethod { }; ``` -**Table 1** Callbacks for the members in the WatchdogMethod structure - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Callback

-

Input Parameter

-

Output Parameter

-

Return Value

-

Description

-

getStatus

-

wdt: structure pointer to the Watchdog controller at the core layer.

-

status: int32_t pointer indicating 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.

-

seconds: input time value, which is of the uint32_t type.

-

-

HDF_STATUS

-

Sets the timeout period (in seconds) for a watchdog. Ensure that the actual watchdog running time complies with this setting.

-

getTimeout

-

wdt: structure pointer to the Watchdog controller at the core layer.

-

seconds: output time value, which is of the uint32_t type.

-

HDF_STATUS

-

Obtains the timeout period 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. - -2. Configure attribute files. - - Add the **deviceNode** information to the **device\_info.hcs** file. - - \(Optional\) Add the **watchdog\_config.hcs** file. - -3. Instantiate the Watchdog controller object. - - Initialize **WatchdogCntlr**. - - Instantiate **WatchdogMethod** in the **WatchdogCntlr** object. - - For details, see [Available APIs](#available-apis). - -4. \(Optional\) Debug the driver. - - 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 - -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. 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. - - - Watchdog driver entry reference - - ``` - struct HdfDriverEntry g_watchdogDriverEntry = { - .moduleVersion = 1, - .Bind = Hi35xxWatchdogBind, // See the Bind function. - .Init = Hi35xxWatchdogInit, // See the Init 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. - - 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 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 - 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"; - // (Mandatory) Unique name of the service published by the driver. - deviceMatchAttr = "hisilicon_hi35xx_watchdog_0"; - // (Mandatory) Keyword matching the private data of the driver. The value must be the same as that of match_attr in the private data configuration table of the driver. - } - } - } - } - ``` - - - **watchdog\_config.hcs** configuration reference - - ``` - root { - platform { - template watchdog_controller {// Template configuration. In the template, you can configure the common parameters shared by service nodes. - id = 0; - match_attr = ""; - regBase = 0x12050000; // (Mandatory) Used for address mapping. - regStep = 0x1000; // (Mandatory) Used for address mapping. - } - controller_0x12050000 :: watchdog_controller {// (Mandatory) Keyword for matching the private data of the device driver. - match_attr = "hisilicon_hi35xx_watchdog_0"; // (Mandatory) The value must be the same as that of deviceMatchAttr in device_info.hcs. - } - // Configure this parameter when there are multiple watchdogs. - ... - } - } - ``` - -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 - - 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 { - struct WatchdogCntlr wdt; // (Mandatory) Carrier that connects the upper and underlying layers. For details, see the following description. - OsalSpinlock lock; - volatile unsigned char *regBase;// [Mandatory] Used for address mapping. - 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. - struct WatchdogCntlr { - struct IDeviceIoService service;// Driver service - struct HdfDeviceObject *device; // Drive device - OsalSpinlock lock; // This variable implements the spinlock function. - struct WatchdogMethod *ops; // Interface callback - int16_t wdtId; // ID of the watchdog device - void *priv; // Save the pointer. - }; - ``` - - - Instantiate the callback function structure **WatchdogMethod** in **WatchdogCntlr**. Other members are initialized by using the **Init** and **Bind** functions. - - ``` - static struct WatchdogMethod g_method = { - .getStatus = Hi35xxWatchdogGetStatus, - .start = Hi35xxWatchdogStart, - .stop = Hi35xxWatchdogStop, - .setTimeout = Hi35xxWatchdogSetTimeout, - .getTimeout = Hi35xxWatchdogGetTimeout, - .feed = Hi35xxWatchdogFeed, - }; - ``` - - - 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 - - - - - - - - - - - - - - - - - - - - - - -

Status (Value)

-

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). - // In this example, the Bind function initializes the Hi35xxWatchdog structure. - static int32_t Hi35xxWatchdogInit(struct HdfDeviceObject *device) - { - (void)device; - return HDF_SUCCESS; - } - - static int32_t Hi35xxWatchdogBind(struct HdfDeviceObject *device) - { - int32_t ret; - struct Hi35xxWatchdog *hwdt = NULL; - ... - hwdt = (struct Hi35xxWatchdog *)OsalMemCalloc(sizeof(*hwdt));// Apply for memory for the Hi35xxWatchdog structure. - ... - hwdt->regBase = OsalIoRemap(hwdt->phyBase, hwdt->regStep); // Address mapping - ... - 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. - 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. - OsalIoUnmap((void *)hwdt->regBase); - OsalMemFree(hwdt); - return ret; - } - return HDF_SUCCESS; - } - ``` - - - Release function - - Input parameters: - - **HdfDeviceObject**: device object created by the HDF for each driver. It stores device-related private data and service APIs. - - Return values: - - – - - 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. - - ``` - static void Hi35xxWatchdogRelease(struct HdfDeviceObject *device) - { - struct WatchdogCntlr *wdt = NULL; - struct Hi35xxWatchdog *hwdt = NULL; - ... - wdt = WatchdogCntlrFromDevice(device);// Convert HdfDeviceObject to WatchdogCntlr by the service member. - //return (device == NULL) ? NULL : (struct WatchdogCntlr *)device->service; - if (wdt == NULL) { - return; - } - WatchdogCntlrRemove(wdt); // Core layer function used to execute wdt->device->service = NULL and release cntlr->lock. - hwdt = (struct Hi35xxWatchdog *)wdt; // Convert WatchdogCntlr to HimciHost. - if (hwdt->regBase != NULL) {// Remove address mapping. - OsalIoUnmap((void *)hwdt->regBase); - hwdt->regBase = NULL; - } - OsalMemFree(hwdt); // Release the memory occupied by the vendor-defined objects. - } - ``` \ No newline at end of file + **Table 1** Description of the callback functions in WatchdogMethod + +| Function| Input Parameter| Output Parameter| Return Value| Description| +| -------- | -------- | -------- | -------- | -------- | +| 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.
**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. + +2. Configure attribute files. + - Add the **deviceNode** information to the **device_info.hcs** file. + - (Optional) Add the **watchdog_config.hcs** file. + +3. Instantiate the watchdog controller object. + - Initialize **WatchdogCntlr**. + - Instantiate **WatchdogMethod** in the **WatchdogCntlr** object. + + + > ![](../public_sys-resources/icon-note.gif) **NOTE** + > + > 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. + + +## Development Example + +The following uses **watchdog_hi35xx.c** as an example to present the information required for implementing device functions. + +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. + + 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 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. + .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. + + 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. + + - **device_info.hcs** configuration example: + + + ``` + root { + device_info { + match_attr = "hdf_manager"; + 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"; + // (Mandatory) Unique name of the service published by the driver. + deviceMatchAttr = "hisilicon_hi35xx_watchdog_0"; + // (Mandatory) Keyword matching the private data of the driver. The value must be the same as that of match_attr in the private data configuration table of the driver. + } + } + } + } + ``` + + - **watchdog_config.hcs** configuration example: + + + ``` + root { + platform { + 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. + regStep = 0x1000; // (Mandatory) Used for address mapping. + } + controller_0x12050000 :: watchdog_controller {// (Mandatory) Keyword for matching the private data of the device driver. + match_attr = "hisilicon_hi35xx_watchdog_0"; // (Mandatory) The value must be the same as that of deviceMatchAttr in device_info.hcs. + } + // Configure this parameter when there are multiple watchdogs. + ... + } + } + ``` + +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. + + + ``` + struct Hi35xxWatchdog { + struct WatchdogCntlr wdt; // (Mandatory) Carrier that connects the upper and underlying layers. For details, see the following description. + OsalSpinlock lock; + volatile unsigned char *regBase;// [Mandatory] Used for address mapping. + uint32_t phyBase; // (Mandatory) Used for address mapping. + uint32_t regStep; // (Mandatory) Used for address mapping. + }; + // 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; // Driver device. + 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. + }; + ``` + + - Instantiating **WatchdogMethod** in **WatchdogCntlr** (other members are initialized by **Init** and **Bind**) + + + ``` + static struct WatchdogMethod g_method = { + .getStatus = Hi35xxWatchdogGetStatus, + .start = Hi35xxWatchdogStart, + .stop = Hi35xxWatchdogStop, + .setTimeout = Hi35xxWatchdogSetTimeout, + .getTimeout = Hi35xxWatchdogGetTimeout, + .feed = Hi35xxWatchdogFeed, + }; + ``` + + - **Init** and **Bind** functions + + Input parameter: + + **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 in **HdfDeviceObject**. + // In this example, the Bind function initializes the Hi35xxWatchdog structure. + static int32_t Hi35xxWatchdogInit(struct HdfDeviceObject *device) + { + (void)device; + return HDF_SUCCESS; + } + + static int32_t Hi35xxWatchdogBind(struct HdfDeviceObject *device) + { + int32_t ret; + struct Hi35xxWatchdog *hwdt = NULL; + ... + hwdt = (struct Hi35xxWatchdog *)OsalMemCalloc(sizeof(*hwdt));// Apply for memory for the Hi35xxWatchdog structure. + ... + hwdt->regBase = OsalIoRemap(hwdt->phyBase, hwdt->regStep); // Address mapping + ... + hwdt->wdt.priv = (void *)device->property;// (Optional) Assign the device attribute values to priv. However, priv is not called subsequently. + //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 Init(). + OsalIoUnmap((void *)hwdt->regBase); + OsalMemFree(hwdt); + return ret; + } + return HDF_SUCCESS; + } + ``` + + - **Release** function + + Input parameter: + + **HdfDeviceObject**, a device object created by the HDF for each driver, holds device-related private data and service APIs. + + Return value: + + No value is returned. + + Function description: + + 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. + + + ``` + static void Hi35xxWatchdogRelease(struct HdfDeviceObject *device) + { + struct WatchdogCntlr *wdt = NULL; + struct Hi35xxWatchdog *hwdt = NULL; + ... + wdt = WatchdogCntlrFromDevice(device);// Use service to convert HdfDeviceObject to WatchdogCntlr. + // return (device == NULL) ? NULL : (struct WatchdogCntlr *)device->service; + if (wdt == NULL) { + return; + } + WatchdogCntlrRemove(wdt); // Core layer function used to execute wdt->device->service = NULL and release cntlr->lock. + hwdt = (struct Hi35xxWatchdog *)wdt; // Convert WatchdogCntlr to HimciHost. + if (hwdt->regBase != NULL) { // Unmap addresses. + OsalIoUnmap((void *)hwdt->regBase); + hwdt->regBase = NULL; + } + OsalMemFree(hwdt); // Release the memory occupied by the vendor-defined objects. + } + ```