@@ -63,7 +63,7 @@ The GPIO module adaptation involves the following steps:
- Initialize **GpioCntlr**.
- Instantiate **GpioMethod** in the **GpioCntlr** object.
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> For details about the callbacks in **GpioMethod**, see [Available APIs](#available_apis).
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> For details about the callbacks in **GpioMethod**, see [Available APIs](#available-apis).
The real-time clock \(RTC\) driver provides precise real time for the operating system \(OS\). If the OS is powered off, the RTC driver continues to keep track of the system time using an external battery.
## Available APIs<a name="section20331159102519"></a>
<tdclass="cellrowborder"valign="top"width="53.11531153115312%"headers="mcps1.2.4.1.3 "><pid="p9550103415015"><aname="p9550103415015"></a><aname="p9550103415015"></a>Opens the RTC device to obtain its handle.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1579142745012"><aname="p1579142745012"></a><aname="p1579142745012"></a>Releases a specified handle of the RTC device.</p>
<tdclass="cellrowborder"valign="top"width="53.11531153115312%"headers="mcps1.2.4.1.3 "><pid="p8738101941716"><aname="p8738101941716"></a><aname="p8738101941716"></a>Reads time information from the RTC driver, including the year, month, the day of the week, day, hour, minute, second, and millisecond.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p573815197171"><aname="p573815197171"></a><aname="p573815197171"></a>Writes time information from the RTC driver, including the year, month, the day of the week, day, hour, minute, second, and millisecond.</p>
<tdclass="cellrowborder"valign="top"width="53.11531153115312%"headers="mcps1.2.4.1.3 "><pid="p768110592416"><aname="p768110592416"></a><aname="p768110592416"></a>Reads the RTC alarm time that was set last time.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p15314656511"><aname="p15314656511"></a><aname="p15314656511"></a>Writes the RTC alarm time based on the alarm index.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p118291242155518"><aname="p118291242155518"></a><aname="p118291242155518"></a>Registers <strongid="b1648234343210"><aname="b1648234343210"></a><aname="b1648234343210"></a>RtcAlarmCallback</strong> that will be invoked when an alarm is generated at the specified time.</p>
<tdclass="cellrowborder"valign="top"width="53.11531153115312%"headers="mcps1.2.4.1.3 "><pid="p980133515566"><aname="p980133515566"></a><aname="p980133515566"></a>Reads the frequency of the external crystal oscillator connected to the RTC driver.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p10150832165613"><aname="p10150832165613"></a><aname="p10150832165613"></a>Sets the frequency of the external crystal oscillator connected to the RTC driver.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p517712598569"><aname="p517712598569"></a><aname="p517712598569"></a>Resets the RTC.</p>
<tdclass="cellrowborder"valign="top"width="53.11531153115312%"headers="mcps1.2.4.1.3 "><pid="p1517114410578"><aname="p1517114410578"></a><aname="p1517114410578"></a>Reads the configuration of a custom RTC register based on the register index.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p1967391913576"><aname="p1967391913576"></a><aname="p1967391913576"></a>Writes the configuration of a custom RTC register based on the register index.</p>
### How to Use<a name="section16919828134215"></a>
During the OS startup, the HDF loads the RTC driver based on the configuration file. The RTC driver detects the RTC component and initializes the driver.
<tdclass="cellrowborder"valign="top"width="78.55%"><pid="p26881319114110"><aname="p26881319114110"></a><aname="p26881319114110"></a>The operation is successful.</p>
The real-time clock (RTC) provides precise real time for the operating system (OS) and scheduled alarming function. When the device is powered off, the RTC can accurately keep track of the system time using an auxiliary battery. The RTC regulates time with the use of a crystal oscillator.
## Available APIs
**Table 1** RTC APIs
| Category| Description|
| -------- | -------- |
| RTC handle| **RtcOpen**: Opens the RTC device to obtain its handle.<br>**RtcClose**: Closes the RTC device handle.|
| RTC time| **RtcReadTime**: reads the RTC time information, which includes the year, month, day, day of week, hour, minute, second, and millisecond.<br>**RtcWriteTime**: writes the RTC time, including the year, month, day, day of week, hour, minute, second, and millisecond.|
| RTC alarm| **RtcReadAlarm**: reads the RTC alarm time.<br>**RtcWriteAlarm**: writes the RTC alarm time.<br>**RtcRegisterAlarmCallback**: registers a callback to be invoked when an alarm is not generated at the specified time.<br>**RtcAlarmInterruptEnable**: enables or disables interrupts for an RTC alarm.|
| RTC configuration| **RtcGetFreq**: obtains the frequency of the external crystal oscillator connected to the RTC driver.<br>**RtcSetFreq**: sets the frequency of the external crystal oscillator connected to the RTC driver.<br>**RtcReset**: resets the RTC.|
| Custom register| **RtcReadReg**: reads a custom register.<br>**RtcWriteReg**: writes a custom register.|
After the OS is started, call the following function to register **RtcAlarmCallback**, which will be invoked when an alarm is generated at the specified time:
<tdclass="cellrowborder"valign="top"width="78.64%"headers="mcps1.2.3.1.2 "><pid="p1560441923818"><aname="p1560441923818"></a><aname="p1560441923818"></a>Callback that will be invoked when an alarm is generated at the specified time.</p>
<tdclass="cellrowborder"valign="top"width="78.64%"headers="mcps1.2.3.1.2 "><pid="p530813107289"><aname="p530813107289"></a><aname="p530813107289"></a>The operation is successful.</p>
/* Register a callback to be invoked when alarm A is not generated at the specified time. */
ret = RtcRegisterAlarmCallback(handle, RTC_ALARM_INDEX_A, RtcAlarmACallback);
if (ret != 0) {
/* Process the error. */
/* Error handling. */
}
```
### Performing RTC-related Operations
- Reading RTC time
Call the **RtcReadTime()** function to read time information from the RTC driver, including the year, month, the day of the week, day, hour, minute, second, and millisecond.
<tdclass="cellrowborder"valign="top"width="78.55%"><pid="p204891671156"><aname="p204891671156"></a><aname="p204891671156"></a>Pointer to the time information read from the RTC driver. The time information includes the year, month, the day of the week, day, hour, minute, second, and millisecond.</p>
<tdclass="cellrowborder"valign="top"width="78.55%"><pid="p1895911611284"><aname="p1895911611284"></a><aname="p1895911611284"></a>The operation is successful.</p>
<tdclass="cellrowborder"valign="top"width="78.46%"><pid="p167675286381"><aname="p167675286381"></a><aname="p167675286381"></a>Pointer to the time information written into the RTC driver. The time information includes the year, month, the day of the week, day, hour, minute, second, and millisecond.</p>
<tdclass="cellrowborder"valign="top"width="78.46%"><pid="p145503417284"><aname="p145503417284"></a><aname="p145503417284"></a>The operation is successful.</p>
>The RTC start time is 1970/01/01 Thursday 00:00:00 \(UTC\). The maximum value of **year** must be set based on the requirements specified in the product manual of the in-use component. You do not need to configure the day of the week.
> The RTC start time is 1970/01/01 Thursday 00:00:00 (UTC). The maximum value of **year** must be set based on the requirements specified in the manual of the device you use. You do not need to set the day of week.
```
int32_t ret;
...
...
@@ -370,120 +198,66 @@ tm.hour= 00;
tm.minute = 59;
tm.second = 00;
tm.millisecond = 0;
/* Write the RTC time information. */
/* Write the RTC time. */
ret = RtcWriteTime(handle, &tm);
if (ret != 0) {
/* Process the error. */
/* Error handling. */
}
```
- Reading the RTC alarm time
Call the **RtcReadAlarm()** function to read the alarm time.
<tdclass="cellrowborder"valign="top"width="78.46%"><pid="p112695144013"><aname="p112695144013"></a><aname="p112695144013"></a>Pointer to the RTC alarm time information. The time information includes the year, month, the day of the week, day, hour, minute, second, and millisecond.</p>
<tdclass="cellrowborder"valign="top"width="78.46%"><pid="p87751917289"><aname="p87751917289"></a><aname="p87751917289"></a>The operation is successful.</p>
<tdclass="cellrowborder"valign="top"width="78.38000000000001%"><pid="p462602125211"><aname="p462602125211"></a><aname="p462602125211"></a>Pointer to the RTC alarm time information. The time information includes the year, month, the day of the week, day, hour, minute, second, and millisecond.</p>
<tdclass="cellrowborder"valign="top"width="78.38000000000001%"><pid="p1179817586276"><aname="p1179817586276"></a><aname="p1179817586276"></a>The operation is successful.</p>
>The RTC start time is 1970/01/01 Thursday 00:00:00 \(UTC\). The maximum value of **year** must be set based on the requirements specified in the product manual of the in-use component. You do not need to configure the day of the week.
- Setting the RTC alarm time
Call **RtcWriteAlarm()** to set the RTC alarm time based on the alarm index.
> The RTC start time is 1970/01/01 Thursday 00:00:00 (UTC). The maximum value of **year** must be set based on the requirements specified in the manual of the device you use. You do not need to set the day of week.
/* Set the alarm time of alarm RTC_ALARM_INDEX_A. */
ret = RtcWriteAlarm(handle, RTC_ALARM_INDEX_A, &alarmTime);
if (ret != 0) {
/* Process the error. */
/* Error handling. */
}
```
- Enabling or disabling alarm interrupts
Before performing alarm operations, use the **RtcAlarmInterruptEnable()** function to enable alarm interrupts, so that **RtcAlarmCallback** will be called when the alarm is not generated upon a timeout.
<tdclass="cellrowborder"valign="top"width="78.64%"><pid="p234655341511"><aname="p234655341511"></a><aname="p234655341511"></a>Whether to enable RTC alarm interrupts. The value <strongid="b879021119010"><aname="b879021119010"></a><aname="b879021119010"></a>1</strong> means to enable alarm interrupts and <strongid="b779617111204"><aname="b779617111204"></a><aname="b779617111204"></a>0</strong> means to disable alarm interrupts.</p>
<tdclass="cellrowborder"valign="top"width="78.64%"><pid="p99471953152712"><aname="p99471953152712"></a><aname="p99471953152712"></a>The operation is successful.</p>
Call **RtcAlarmInterruptEnable** to enable interrupts for an RTC alarm so that the registered callback can be invoked when the alarm is not generated at the specified time.
<tdclass="cellrowborder"valign="top"width="78.64%"><pid="p165888211810"><aname="p165888211810"></a><aname="p165888211810"></a>Pointer to the frequency to set for the external crystal oscillator, in Hz.</p>
<tdclass="cellrowborder"valign="top"width="78.64%"><pid="p13133352202719"><aname="p13133352202719"></a><aname="p13133352202719"></a>The operation is successful.</p>
<tdclass="cellrowborder"valign="top"width="78.64%"><pid="p167021043182011"><aname="p167021043182011"></a><aname="p167021043182011"></a>Frequency to set for the external crystal oscillator, in Hz</p>
<tdclass="cellrowborder"valign="top"width="78.64%"><pid="p770214310209"><aname="p770214310209"></a><aname="p770214310209"></a>The operation is successful.</p>
Call **RtcSetFreq()** to set the frequency of the external crystal oscillator connected to the RTC driver.
```c
int32_tRtcSetFreq(DevHandlehandle,uint32_tfreq);
```
**Table 11** Description of RtcSetFreq
| **Parameter**| **Description**|
| -------- | -------- |
| handle | RTC device handle.|
| freq | Pointer to the frequency to set for the external crystal oscillator, in Hz.|
| **Return Value**| **Description**|
| 0 | The operation is successful.|
| Negative value| The operation failed.|
```
int32_t ret;
uint32_t freq = 32768; /* 32768 Hz */
/* Set the frequency of the external crystal oscillator. Note that the frequency must be configured in accordance with the requirements specified in the product manual of the in-use component. */
/* Set the frequency of the external crystal oscillator based on the requirements specified in the manual of the device you use. */
ret = RtcSetFreq(handle, freq);
if (ret != 0) {
/* Process the error. */
/* Error handling. */
}
```
- Resetting the RTC driver
Call the **RtcReset()** function to perform a reset on the RTC driver \(after the reset, the registers are restored to the default values\).
<tdclass="cellrowborder"valign="top"width="78.64%"><pid="p1799093182516"><aname="p1799093182516"></a><aname="p1799093182516"></a>The operation is successful.</p>
Call **RtcReset()** to reset the RTC. After the reset, the registers are restored to default values.
```c
int32_tRtcReset(DevHandlehandle);
```
**Table 12** Description of RtcReset
| **Parameter**| **Description**|
| -------- | -------- |
| handle | RTC device handle.|
| **Return Value**| **Description**|
| 0 | The operation is successful.|
| Negative value| The operation failed.|
```
int32_t ret;
/* Reset the RTC driver. After the reset, the configuration registers are restored to the default values. */
/* Reset the RTC to restore default values of registers. */
ret = RtcReset(handle);
if (ret != 0) {
/* Process the error. */
/* Error handling. */
}
```
- Reading the configuration of a custom RTC register
Call the **RtcReadReg()** function to read the configuration of a custom RTC register based on the register index \(one index corresponds to one byte of the configuration value\):
<tdclass="cellrowborder"valign="top"width="78.38000000000001%"><pid="p181671825470"><aname="p181671825470"></a><aname="p181671825470"></a>Index of the custom register</p>
<tdclass="cellrowborder"valign="top"width="78.38000000000001%"><pid="p524711413319"><aname="p524711413319"></a><aname="p524711413319"></a>Pointer to the register value</p>
<tdclass="cellrowborder"valign="top"width="78.38000000000001%"><pid="p152471433317"><aname="p152471433317"></a><aname="p152471433317"></a>The operation is successful.</p>
Call **RtcReadReg()** to read the configuration of a custom RTC register based on the register index. One index corresponds to one byte of the register value.
uint8_t usrDefIndex = 0; /* Define index 0 for the first custom register. */
uint8_t value = 0;
/* Read the configuration of a custom RTC register based on the register index. One index corresponds to one byte of the configuration value. */
/* Read the value of custom RTC register 0. One index corresponds to one byte of the register value. */
ret = RtcReadReg(handle, usrDefIndex, &value);
if (ret != 0) {
/* Process the error. */
/* Error handling. */
}
```
- Setting the configuration of a custom RTC register
Call the **RtcWriteReg()** function to configure a register based on the specified register index \(one index corresponds to one byte of the configuration value\).
<tdclass="cellrowborder"valign="top"width="78.38000000000001%"><pid="p18723184819365"><aname="p18723184819365"></a><aname="p18723184819365"></a>Index of the custom register</p>
<tdclass="cellrowborder"valign="top"width="78.38000000000001%"><pid="p472324873620"><aname="p472324873620"></a><aname="p472324873620"></a>The operation is successful.</p>
/* Register a callback for alarm A if it is not generated at the specified time. */
ret = RtcRegisterAlarmCallback(handle, RTC_ALARM_INDEX_A, RtcAlarmACallback);
if (ret != 0) {
/* Process the error. */
/* Error handling. */
}
/* Set the RTC external crystal frequency. Note that the frequency must be configured in accordance with the requirements specified in the product manual of the in-use component. */
/* Set the frequency of the external crystal oscillator based on the requirements specified in the manual of the device you use. */
freq = 32768; /* 32768 Hz */
ret = RtcSetFreq(handle, freq);
if (ret != 0) {
/* Process the error. */
/* Error handling. */
}
/* Enable the RTC alarm interrupts. */
/* Enable interrupts for RTC alarm A. */
ret = RtcAlarmInterruptEnable(handle, RTC_ALARM_INDEX_A, 1);
if (ret != 0) {
/* Process the error. */
/* Error handling. */
}
/* Set the RTC time to 2020/01/01 00:00:10 .990. */
tm.year = 2020;
...
...
@@ -896,10 +529,10 @@ void RtcTestSample(void)
tm.minute = 0;
tm.second = 10;
tm.millisecond = 990;
/* Write the RTC time information. */
/* Write the RTC time. */
ret = RtcWriteTime(handle, &tm);
if (ret != 0) {
/* Process the error. */
/* Error handling. */
}
/* Set the RTC alarm time to 2020/01/01 00:00:30 .100. */
alarmTime.year = 2020;
...
...
@@ -909,22 +542,22 @@ void RtcTestSample(void)
alarmTime.minute = 0;
alarmTime.second = 30;
alarmTime.millisecond = 100;
/* Set the alarm time information for RTC_ALARM_INDEX_A. When the specified time is reached, "RTC Alarm A callback function" is printed. */
/* Set the alarm time for RTC_ALARM_INDEX_A. When the specified time is reached, "RTC Alarm A callback function" will be printed. */
ret = RtcWriteAlarm(handle, RTC_ALARM_INDEX_A, &alarmTime);
In the Hardware Driver Foundation \(HDF\) framework, the real-time clock \(RTC\) 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 HDFDeviceManager can be directly used. However, you need to configure a device node for each device, which increases the memory usage.
## Overview
**Figure 1** Independent service mode<aname="fig6742142611299"></a>
The real-time clock (RTC) is a real-time clock device in the operating system. In the HDF, the RTC 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 to increase memory resources.
## Available APIs<a name="section752964871810"></a>
**Figure 1** Independent service mode
RtcMethod
![image](figures/independent-service-mode.png"RTC independent service mode")
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p094112118314"><aname="p094112118314"></a><aname="p094112118314"></a><strongid="b1264214345404"><aname="b1264214345404"></a><aname="b1264214345404"></a>host</strong>: structure pointer to the RTC controller at the core layer.</p>
</td>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.3 "><pid="p19945219318"><aname="p19945219318"></a><aname="p19945219318"></a><strongid="b6902125819406"><aname="b6902125819406"></a><aname="b6902125819406"></a>time</strong>: structure pointer to the output time value.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p094132115317"><aname="p094132115317"></a><aname="p094132115317"></a>Reads the RTC time information.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p10306227052"><aname="p10306227052"></a><aname="p10306227052"></a><strongid="b1330617271556"><aname="b1330617271556"></a><aname="b1330617271556"></a>host</strong>: structure pointer to the RTC controller at the core layer.</p>
<pid="p89418214311"><aname="p89418214311"></a><aname="p89418214311"></a><strongid="b1411813377426"><aname="b1411813377426"></a><aname="b1411813377426"></a>time</strong>: structure pointer to the input time.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p5941521123118"><aname="p5941521123118"></a><aname="p5941521123118"></a>Writes the RTC time information (from milliseconds to years).</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p1362015341251"><aname="p1362015341251"></a><aname="p1362015341251"></a><strongid="b14620934553"><aname="b14620934553"></a><aname="b14620934553"></a>host</strong>: structure pointer to the RTC controller at the core layer.</p>
<pid="p9951721113113"><aname="p9951721113113"></a><aname="p9951721113113"></a><strongid="b477221417458"><aname="b477221417458"></a><aname="b477221417458"></a>alarmIndex</strong>: clock alarm index, which is an enumerated value.</p>
</td>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.3 "><pid="p1995921113112"><aname="p1995921113112"></a><aname="p1995921113112"></a><strongid="b0371144014414"><aname="b0371144014414"></a><aname="b0371144014414"></a>time</strong>: structure pointer to the output time.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p136914402055"><aname="p136914402055"></a><aname="p136914402055"></a><strongid="b176918401258"><aname="b176918401258"></a><aname="b176918401258"></a>host</strong>: structure pointer to the RTC controller at the core layer.</p>
<pid="p11512468517"><aname="p11512468517"></a><aname="p11512468517"></a><strongid="b195114613519"><aname="b195114613519"></a><aname="b195114613519"></a>alarmIndex</strong>: clock alarm index, which is an enumerated value.</p>
<pid="p595152114315"><aname="p595152114315"></a><aname="p595152114315"></a><strongid="b1892302045019"><aname="b1892302045019"></a><aname="b1892302045019"></a>time</strong>: structure pointer to the input time.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p7793145219520"><aname="p7793145219520"></a><aname="p7793145219520"></a><strongid="b37931529513"><aname="b37931529513"></a><aname="b37931529513"></a>host</strong>: structure pointer to the RTC controller at the core layer.</p>
<pid="p109352055359"><aname="p109352055359"></a><aname="p109352055359"></a><strongid="b393518551159"><aname="b393518551159"></a><aname="b393518551159"></a>alarmIndex</strong>: clock alarm index, which is an enumerated value.</p>
<pid="p1295182163112"><aname="p1295182163112"></a><aname="p1295182163112"></a><strongid="b11371112513"><aname="b11371112513"></a><aname="b11371112513"></a>cb</strong>: pointer to the callback.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p39602133113"><aname="p39602133113"></a><aname="p39602133113"></a>Registers <strongid="b176627580513"><aname="b176627580513"></a><aname="b176627580513"></a>RtcAlarmCallback</strong> that will be invoked when an alarm is generated at the specified time.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p796015120619"><aname="p796015120619"></a><aname="p796015120619"></a><strongid="b149601210611"><aname="b149601210611"></a><aname="b149601210611"></a>host</strong>: structure pointer to the RTC controller at the core layer.</p>
<pid="p174501061561"><aname="p174501061561"></a><aname="p174501061561"></a><strongid="b7450562062"><aname="b7450562062"></a><aname="b7450562062"></a>alarmIndex</strong>: clock alarm index, which is an enumerated value.</p>
<pid="p59682173112"><aname="p59682173112"></a><aname="p59682173112"></a><strongid="b6999711105518"><aname="b6999711105518"></a><aname="b6999711105518"></a>enable</strong>: specifies whether to enable the RTC alarm interrupt.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p996182112316"><aname="p996182112316"></a><aname="p996182112316"></a>Enables or disables the RTC alarm interrupt.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p16961021103119"><aname="p16961021103119"></a><aname="p16961021103119"></a><strongid="b17147182285715"><aname="b17147182285715"></a><aname="b17147182285715"></a>host</strong>: structure pointer to the RTC controller at the core layer.</p>
</td>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.3 "><pid="p09620218318"><aname="p09620218318"></a><aname="p09620218318"></a><strongid="b11413184435713"><aname="b11413184435713"></a><aname="b11413184435713"></a>freq</strong>: pointer to the output frequency, which is of the uint32_t type.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p3968214311"><aname="p3968214311"></a><aname="p3968214311"></a>Reads the frequency of the external crystal oscillator connected to the RTC driver.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p38121311169"><aname="p38121311169"></a><aname="p38121311169"></a><strongid="b481218117612"><aname="b481218117612"></a><aname="b481218117612"></a>host</strong>: structure pointer to the RTC controller at the core layer.</p>
<pid="p79792114315"><aname="p79792114315"></a><aname="p79792114315"></a><strongid="b1220510308"><aname="b1220510308"></a><aname="b1220510308"></a>freq</strong>: input frequency, which is of the uint32_t type.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p11971621193115"><aname="p11971621193115"></a><aname="p11971621193115"></a>Sets the frequency of the external crystal oscillator connected to the RTC driver.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p169712123111"><aname="p169712123111"></a><aname="p169712123111"></a><strongid="b13129173210577"><aname="b13129173210577"></a><aname="b13129173210577"></a>host</strong>: structure pointer to the RTC controller at the core layer.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p197112116316"><aname="p197112116316"></a><aname="p197112116316"></a>Resets the RTC.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p12704168619"><aname="p12704168619"></a><aname="p12704168619"></a><strongid="b1570171616616"><aname="b1570171616616"></a><aname="b1570171616616"></a>host</strong>: structure pointer to the RTC controller at the core layer.</p>
<pid="p18975210312"><aname="p18975210312"></a><aname="p18975210312"></a><strongid="b1020311128520"><aname="b1020311128520"></a><aname="b1020311128520"></a>usrDefIndex</strong>: structure defining the index of a custom register.</p>
</td>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.3 "><pid="p14975211314"><aname="p14975211314"></a><aname="p14975211314"></a><strongid="b10580501973"><aname="b10580501973"></a><aname="b10580501973"></a>value</strong>: pointer to the output register value, which is of the uint8_t type.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p1098821143115"><aname="p1098821143115"></a><aname="p1098821143115"></a>Reads the configuration of a custom RTC register based on the register index. One index corresponds to one byte of the configuration value.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p1747714196613"><aname="p1747714196613"></a><aname="p1747714196613"></a><strongid="b6477519469"><aname="b6477519469"></a><aname="b6477519469"></a>host</strong>: structure pointer to the RTC controller at the core layer.</p>
<pid="p1527912317612"><aname="p1527912317612"></a><aname="p1527912317612"></a><strongid="b72791123968"><aname="b72791123968"></a><aname="b72791123968"></a>usrDefIndex</strong>: structure defining the index of a custom register.</p>
<pid="p1498172173115"><aname="p1498172173115"></a><aname="p1498172173115"></a><strongid="b68893701014"><aname="b68893701014"></a><aname="b68893701014"></a>value</strong>: input register value, which is of the uint8_t type.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p13981421203110"><aname="p13981421203110"></a><aname="p13981421203110"></a>Configures the RTC register based on the register index. One index corresponds to one byte of the configuration value.</p>
</td>
</tr>
</tbody>
</table>
## How to Develop<a name="section1784450860142407"></a>
The RTC module adaptation involves the following steps:
**Table 1** Description of the callback functions in RtcMethod
1. Instantiate the driver entry.
- Instantiate the **HdfDriverEntry** structure.
- Call **HDF\_INIT** to register the **HdfDriverEntry** instance with the HDF.
| ReadTime | **host**: structure pointer to the RTC controller at the core layer.| **time**: structure pointer to the time read.| HDF_STATUS| Reads the RTC time.|
| WriteTime | **host**: structure pointer to the RTC controller at the core layer.<br>**time**: structure pointer to the time to write.| –| HDF_STATUS| Writes the RTC time (including the year, month, day, day of week, hour, minute, second, and millisecond).|
| ReadAlarm | **host**: structure pointer to the RTC controller at the core layer.<br>**alarmIndex**: RTC alarm index, which is an enumerated value.| **time**: structure pointer to the time read.| HDF_STATUS| Reads the RTC alarm time.|
| WriteAlarm | **host**: structure pointer to the RTC controller at the core layer.<br>**alarmIndex**: RTC alarm index, which is an enumerated value.<br>**time**: structure pointer to the time to write.| –| HDF_STATUS| Writes the RTC alarm time.|
| RegisterAlarmCallback | **host**: structure pointer to the RTC controller at the core layer.<br>**alarmIndex**: RTC alarm index, which is an enumerated value.<br>**cb**: pointer to the callback to register.| –| HDF_STATUS| Registers a callback to be invoked when an alarm is not generated at the specified time.|
| AlarmInterruptEnable | **host**: structure pointer to the RTC controller at the core layer.<br>**alarmIndex**: RTC alarm index, which is an enumerated value.<br>**enable**: whether to enable interrupts for an RTC alarm. | –| HDF_STATUS| Enables or disables interrupts for an RTC alarm.|
| GetFreq | **host**: structure pointer to the RTC controller at the core layer.| **freq**: pointer to the frequency obtained, which is of the uint32_t type.| HDF_STATUS| Obtains the frequency of the external crystal oscillator connected to the RTC driver.|
| SetFreq | **host**: structure pointer to the RTC controller at the core layer.<br>**freq**: frequency to set, which is of the uint32_t type.| –| HDF_STATUS| Sets the frequency of the external crystal oscillator connected to the RTC driver.|
| Reset | **host**: structure pointer to the RTC controller at the core layer.| –| HDF_STATUS| Resets the RTC.|
| ReadReg | **host**: structure pointer to the RTC controller at the core layer.<br>**usrDefIndex**: index of a custom register.| **value**: pointer to the register value obtained, which is of the uint8_t type.| HDF_STATUS| Reads a custom RTC register. A register index corresponds to one byte of the register value.|
| WriteReg | **host**: structure pointer to the RTC controller at the core layer.<br>**usrDefIndex**: index of a custom register.<br>**value**: register value to write, which is of the uint8_t type.| –| HDF_STATUS| Writes a custom RTC register. A register index corresponds to one byte of the register value.|
2. Configure attribute files.
- Add the **deviceNode** information to the **device\_info.hcs** file.
-\(Optional\) Add the **rtc\_config.hcs** file.
3. Instantiate the RTC controller object.
- Initialize **RtcHost**.
- Instantiate **RtcMethod** in the **RtcHost** object.
## How to Develop
For details, see [Available APIs](#available-apis).
4.\(Optional\) Debug the driver.
The RTC module adaptation involves the following steps:
For new drivers, verify the basic functions, such as the RTC control status and response to interrupts.
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 **rtc_config.hcs** file.
3. Instantiate the RTC controller object.
- Initialize **RtcHost**.
- Instantiate **RtcMethod** in the **RtcHost** object.
> For details about the functions in **RtcMethod**, see [API APIs](#available-apis).
4. Debug the driver.
(Optional) For new drivers, verify the basic functions, such as the RTC status control and response to interrupts.
## Development Example
The following uses **rtc_hi35xx.c** as an example to present the information to be provided by the vendor to implement device functions.
1. Instantiate the driver entry.<br/>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 framework, 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.
RTC driver entry example:
```
struct HdfDriverEntry g_rtcDriverEntry = {
.moduleVersion = 1,
.Bind = HiRtcBind, // See the Bind function.
.Init = HiRtcInit, // See the Init function.
.Release = HiRtcRelease, //See the Release function.
.moduleName = "HDF_PLATFORM_RTC", // (Mandatory) The value must be the same as that in the .hcs file.
};
// Call HDF_INIT to register the driver entry with the HDF.
HDF_INIT(g_rtcDriverEntry);
```
2. Add the **deviceNode** information to the **device_info.hcs** file and configure the device attributes in the **rtc_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 **RtcHost** members at the core layer.
In this example, there is only one RTC controller. If there are multiple RTC controllers, you need to add the **deviceNode** information to the **device_info** file and add the corresponding device attributes to the **rtc_config** file for each controller.
-**device_info.hcs** configuration example
```
root {
device_info {
platform :: host {
device_rtc :: device {
device0 :: deviceNode {
policy = 1; // The driver publishes services for kernel-mode processes only.
priority = 30; // A smaller value indicates a higher priority.
permission = 0644; // Permission for the driver to create a device node
moduleName = "HDF_PLATFORM_RTC"; // (Mandatory) Driver name, which must be the same as moduleName in the driver entry.
serviceName = "HDF_PLATFORM_RTC"; // (Mandatory) Unique name of the service published by the driver.
deviceMatchAttr = "hisilicon_hi35xx_rtc";// The value must be the same as that of match_attr in the .hcs file.
}
}
}
}
}
```
-**rtc_config.hcs** configuration example
```
root {
platform {
rtc_config {
controller_0x12080000 {
match_attr = "hisilicon_hi35xx_rtc";// (Mandatory) The value must be the same as that of deviceMatchAttr in device_info.hcs.
rtcSpiBaseAddr = 0x12080000; // Used for address mapping.
regAddrLength = 0x100; // Used for address mapping.
irq = 37; // Interrupt number.
supportAnaCtrl = false;
supportLock = false;
anaCtrlAddr = 0xff;
lock0Addr = 0xff;
lock1Addr = 0xff;
lock2Addr = 0xff;
lock3Addr = 0xff;
}
}
}
}
```
3. Initialize the **RtcHost** 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 **RtcMethod** in **RtcHost** (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 **rtc_config.hcs** file to initialize the members in the custom structure.
```
struct RtcConfigInfo {
uint32_t spiBaseAddr; // Used for address mapping.
volatile void *remapBaseAddr; // Used for address mapping.
uint16_t regAddrLength; // Used for address mapping.
uint8_t supportAnaCtrl; // Whether ANACTRL is supported.
uint8_t supportLock; // Whether lock is supported.
uint8_t irq; // Interrupt number.
uint8_t alarmIndex; // RTC alarm index.
uint8_t anaCtrlAddr; // ANACTRL address.
struct RtcLockAddr lockAddr; // Lock address.
RtcAlarmCallback cb; // Callback.
struct OsalMutex mutex; // Mutex.
};
// RtcHost is the controller structure at the core layer. The Init function assigns values to the members of RtcHost.
struct RtcHost {
struct IDeviceIoService service;
struct HdfDeviceObject *device;
struct RtcMethod *method;
void *data;
};
```
- Instantiating **RtcMethod** in **RtcHost** (other members are initialized by **Init**)
## Development Example<a name="section1594883301142407"></a>
```
// Example in rtc_hi35xx.c: instantiate the hook.
static struct RtcMethod g_method = {
.ReadTime = HiRtcReadTime,
.WriteTime = HiRtcWriteTime,
.ReadAlarm = HiReadAlarm,
.WriteAlarm = HiWriteAlarm,
.RegisterAlarmCallback = HiRegisterAlarmCallback,
.AlarmInterruptEnable = HiAlarmInterruptEnable,
.GetFreq = HiGetFreq,
.SetFreq = HiSetFreq,
.Reset = HiReset,
.ReadReg = HiReadReg,
.WriteReg = HiWriteReg,
};
```
- Bind function
Input parameter:
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
Return value:
HDF_STATUS<br/>The table below describes some status. For more information, see **HDF_STATUS** in the **/drivers/framework/include/utils/hdf_base.h** file.
host = RtcHostCreate(device); // Allocate memory and attach the device host and the device.
// Prerequisite for conversion between HdfDeviceObject and RtcHost.
...
device->service = &host->service; // Prerequisite for conversion between HdfDeviceObject and RtcHost.
// It allows the global use of host by calling RtcHostFromDevice.
return HDF_SUCCESS;
}
```
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.
-**Init** function
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.
Input parameter:
- RTC driver entry reference
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
```
struct HdfDriverEntry g_rtcDriverEntry = {
.moduleVersion = 1,
.Bind = HiRtcBind, // See the Bind function.
.Init = HiRtcInit, // See the Init function.
.Release = HiRtcRelease, //See the Release function.
.moduleName = "HDF_PLATFORM_RTC", // (Mandatory) This parameter must be the same as that in the .hcs file.
};
// Call HDF_INIT to register the driver entry with the HDF.
HDF_INIT(g_rtcDriverEntry);
```
Return value:
2. Add the **deviceNode** information to the **device\_info.hcs** file and configure the device attributes in the **rtc\_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 **RtcHost** members at the core layer.
HDF_STATUS
In this example, there is only one RTC controller. If there are multiple RTC controllers, you need to add the **deviceNode** information to the **device\_info** file and add the corresponding device attributes to the **rtc\_config** file.
Function description:
- **device\_info.hcs** configuration reference
Initializes the custom structure object and **RtcHost**.
```
root {
device_info {
platform :: host {
device_rtc :: device {
device0 :: deviceNode {
policy = 1; // 2: The driver publishes user-mode services. 1: The driver publishes kernel-mode services. 0: No service needs to be published.
priority = 30; // A smaller value indicates a higher priority.
permission = 0644; // Permission for the driver to create a device node
moduleName = "HDF_PLATFORM_RTC"; // (Mandatory) Driver name, which must be the same as the moduleName in the driver entry.
serviceName = "HDF_PLATFORM_RTC"; // (Mandatory) Unique name of the service published by the driver
deviceMatchAttr = "hisilicon_hi35xx_rtc";// The value must be the same as that of match_attr in the .hcs file.
host = RtcHostFromDevice(device);// A forced conversion from HdfDeviceObject to RtcHost is involved.
rtcInfo = OsalMemCalloc(sizeof(*rtcInfo));
...
// HiRtcConfigData reads attributes from the device configuration tree and fills the values in supportAnaCtrl, supportLock, spiBaseAddr, regAddrLength, and irq in rtcInfo.
// Provide parameters for HiRtcSwInit and HiRtcSwInit, and perform operations such as releasing memory when the function internal processing fails.
if (HiRtcConfigData(rtcInfo, device->property) != 0) {
...
}
```
- **rtc\_config.hcs** configuration reference
```
root {
platform {
rtc_config {
controller_0x12080000 {
match_attr = "hisilicon_hi35xx_rtc";// (Mandatory) The value must be the same as that of deviceMatchAttr in device_info.hcs.
rtcSpiBaseAddr = 0x12080000; // Used for address mapping
regAddrLength = 0x100; // Used for address mapping
irq = 37; // Interruption number
supportAnaCtrl = false;
supportLock = false;
anaCtrlAddr = 0xff;
lock0Addr = 0xff;
lock1Addr = 0xff;
lock2Addr = 0xff;
lock3Addr = 0xff;
}
}
}
if (HiRtcSwInit(rtcInfo)! = 0) {// Address mapping and interrupt registration.
...
}
```
3. Initialize the **RtcHost** object at the core layer, including initializing the vendor custom structure \(transferring parameters and data\), instantiating **RtcMethod**\(used to call underlying functions of the driver\) in **RtcHost**, 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 **rtc\_config.hcs** file are read by HDF, and the structure members are initialized through **DeviceResourceIface**.
```
struct RtcConfigInfo {
uint32_t spiBaseAddr; // Used for address mapping
volatile void *remapBaseAddr; // Used for address mapping
uint16_t regAddrLength; // Used for address mapping
uint8_t supportAnaCtrl; // Indicates whether ANACTRL is supported.
uint8_t supportLock; // Indicates whether lock is supported.
uint8_t irq; // Interrupt number
uint8_t alarmIndex; // Clock alarm index
uint8_t anaCtrlAddr; // ANACTRL address
struct RtcLockAddr lockAddr; // Lock address
RtcAlarmCallback cb; // Callback
struct OsalMutex mutex; // Mutex
};
// RtcHost is the controller structure at the core layer. Its members are assigned with values by using the Init function.
struct RtcHost {
struct IDeviceIoService service;
struct HdfDeviceObject *device;
struct RtcMethod *method;
void *data;
};
```
- Instantiate the callback function structure **RtcMethod** in **RtcHost**. Other members are initialized by using the **Init** function.
```
// Example in rtc_hi35xx.c: instantiate the hook.
static struct RtcMethod g_method = {
.ReadTime = HiRtcReadTime,
.WriteTime = HiRtcWriteTime,
.ReadAlarm = HiReadAlarm,
.WriteAlarm = HiWriteAlarm,
.RegisterAlarmCallback = HiRegisterAlarmCallback,
.AlarmInterruptEnable = HiAlarmInterruptEnable,
.GetFreq = HiGetFreq,
.SetFreq = HiSetFreq,
.Reset = HiReset,
.ReadReg = HiReadReg,
.WriteReg = HiWriteReg,
};
```
- Bind function
Input parameters:
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information.
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 Bind function
host = RtcHostCreate(device); // Apply for memory and connect to the device: host->device = device;
// Enable conversion between HdfDeviceObject and RtcHost.
if (HiRtcHwInit(rtcInfo)! = 0) {// Initialize ANACTRL and lockAddr.
...
device->service = &host->service; // Enable conversion between HdfDeviceObject and RtcHost.
// This setting enables the host to be globally used by calling RtcHostFromDevice.
return HDF_SUCCESS;
}
```
host->method = &g_method; // Attach the RtcMethod instance.
host->data = rtcInfo; // Prerequisites for conversion between RtcConfigInfo and RtcHost.
HDF_LOGI("Hdf dev service:%s init success!", HdfDeviceGetServiceName(device));
return HDF_SUCCESS;
}
```
-**Release** function
- Init function
Input parameter:
Input parameters:
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information.
Return value:
Return values:
No value is returned.
HDF\_STATUS
Function description:
Function description:
Releases the memory and deletes the controller. This function assigns values to the **Release** function in the driver entry structure. If 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** or **Bind** function has the corresponding value assignment operations.
Initializes the custom structure object and **RtcHost**.
host = RtcHostFromDevice(device);// A forced conversion from HdfDeviceObject to RtcHost is involved.
rtcInfo = OsalMemCalloc(sizeof(*rtcInfo));
...
// HiRtcConfigData reads attributes from the device configuration tree and fills in supportAnaCtrl, supportLock, spiBaseAddr, regAddrLength, and irq in rtcInfo.
// Provide parameters for HiRtcSwInit and HiRtcSwInit, and perform operations such as releasing memory when the function internal processing fails.
if (HiRtcConfigData(rtcInfo, device->property) != 0) {
...
}
if (HiRtcSwInit(rtcInfo)! = 0) {// Related to address mapping and interrupt registration
...
host = RtcHostFromDevice(device); // A forced conversion from HdfDeviceObject to RtcHost is involved.
rtcInfo = (struct RtcConfigInfo *)host->data;// A forced conversion from RtcHost to RtcConfigInfo is involved.
if (rtcInfo != NULL) {
HiRtcSwExit(rtcInfo);
OsalMemFree(rtcInfo); // Release RtcConfigInfo.
host->data = NULL;
}
if (HiRtcHwInit(rtcInfo)! = 0) {// Initialize ANACTRL and lockAddr.
...
}
host->method = &g_method; // Connect to the UARTHostMethod instance.
host->data = rtcInfo; // Enable conversion between RtcConfigInfo and RtcHost.
HDF_LOGI("Hdf dev service:%s init success!", HdfDeviceGetServiceName(device));
return HDF_SUCCESS;
}
```
- Release function
Input parameters:
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information.
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** or **Bind** function has the corresponding value assignment operations.