1e41f4b71Sopenharmony_ci# RTC
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci## Overview
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ciThe 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.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Available APIs
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci  **Table 1** RTC APIs
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci| Category| Description|
14e41f4b71Sopenharmony_ci| -------- | -------- |
15e41f4b71Sopenharmony_ci| RTC handle| **RtcOpen**: opens the RTC device to obtain its handle.<br>**RtcClose**: closes the RTC device handle.|
16e41f4b71Sopenharmony_ci| 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.|
17e41f4b71Sopenharmony_ci| 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.|
18e41f4b71Sopenharmony_ci| 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.|
19e41f4b71Sopenharmony_ci| Custom register| **RtcReadReg**: reads a custom register.<br>**RtcWriteReg**: writes a custom register.|
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
22e41f4b71Sopenharmony_ci> All APIs described in this document can be called only in kernel mode.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci## Usage Guidelines
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci### How to Use
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ciDuring the OS startup, the HDF loads the RTC driver based on the configuration file. The RTC driver detects the RTC device and initializes the driver.
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ciThe figure below illustrates the general development process of an RTC device.
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci  **Figure 1** Using the RTC APIs
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci  ![image](figures/using-RTC-process.png "Using RTC APIs")
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci### Opening the RTC Device Handle
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ciAfter the RTC driver is loaded, the API provided by the HDF is called to invoke the APIs of the RTC driver.
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
44e41f4b71Sopenharmony_ci> Only one RTC device is supported in the OS.
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci```c
47e41f4b71Sopenharmony_ciDevHandle RtcOpen(void);
48e41f4b71Sopenharmony_ci```
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci  **Table 2** Description of RtcOpen
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci| **Parameter**| **Description**|
53e41f4b71Sopenharmony_ci| -------- | -------- |
54e41f4b71Sopenharmony_ci| void | NA |
55e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
56e41f4b71Sopenharmony_ci| handle | The operation is successful.|
57e41f4b71Sopenharmony_ci| NULL | The operation failed.|
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci```
61e41f4b71Sopenharmony_ciDevHandle  handle = NULL;
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci/* Open the RTC device handle. */
64e41f4b71Sopenharmony_cihandle = RtcOpen();
65e41f4b71Sopenharmony_ciif (handle  == NULL) {
66e41f4b71Sopenharmony_ci    /* Error handling. */
67e41f4b71Sopenharmony_ci}
68e41f4b71Sopenharmony_ci```
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci### Closing the RTC Device Handle
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ciCall **RtcClose()** to release the resources used by the RTC device.
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci```c
76e41f4b71Sopenharmony_civoid RtcClose(DevHandle handle);
77e41f4b71Sopenharmony_ci```
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci  **Table 3** Description of RtcClose
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci| **Parameter**| **Description**|
82e41f4b71Sopenharmony_ci| -------- | -------- |
83e41f4b71Sopenharmony_ci| handle | RTC device handle to close.|
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci```
87e41f4b71Sopenharmony_ci/* Close the RTC device handle. */
88e41f4b71Sopenharmony_ciRtcClose(handle);
89e41f4b71Sopenharmony_ci```
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci### Registering an Alarm Callback
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ciCall **RtcRegisterAlarmCallback** to register a callback, which will be invoked when the specified alarm is not generated at the specified time.
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci```c
97e41f4b71Sopenharmony_ciint32_t RtcRegisterAlarmCallback(DevHandle handle, enum RtcAlarmIndex alarmIndex, RtcAlarmCallback cb);
98e41f4b71Sopenharmony_ci```
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci  **Table 4** Description of RtcRegisterAlarmCallback
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci| **Parameter**| **Description**| 
103e41f4b71Sopenharmony_ci| -------- | -------- |
104e41f4b71Sopenharmony_ci| handle | RTC device handle.| 
105e41f4b71Sopenharmony_ci| alarmIndex | Alarm index.| 
106e41f4b71Sopenharmony_ci| cb | Callback to be invoked when the alarm is not generated at the specified time.| 
107e41f4b71Sopenharmony_ci| **Return Value**| **Description**| 
108e41f4b71Sopenharmony_ci| 0 | The operation is successful.| 
109e41f4b71Sopenharmony_ci| Negative value| The operation failed.| 
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci  The following is an example of registering a callback for processing alarm RTC_ALARM_INDEX_A:
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci```
114e41f4b71Sopenharmony_ci/* Register a callback for alarm RTC_ALARM_INDEX_A. */
115e41f4b71Sopenharmony_ciint32_t RtcAlarmACallback(enum RtcAlarmIndex alarmIndex)
116e41f4b71Sopenharmony_ci{
117e41f4b71Sopenharmony_ci    if (alarmIndex == RTC_ALARM_INDEX_A) {
118e41f4b71Sopenharmony_ci        /* Processing of alarm A. */
119e41f4b71Sopenharmony_ci    } else if (alarmIndex == RTC_ALARM_INDEX_B) {
120e41f4b71Sopenharmony_ci        /* Processing of alarm B. */
121e41f4b71Sopenharmony_ci    } else {
122e41f4b71Sopenharmony_ci        /* Error handling. */
123e41f4b71Sopenharmony_ci    }
124e41f4b71Sopenharmony_ci    return 0;
125e41f4b71Sopenharmony_ci}
126e41f4b71Sopenharmony_ciint32_t ret;
127e41f4b71Sopenharmony_ci/* Register a callback to be invoked when alarm A is not generated at the specified time. */
128e41f4b71Sopenharmony_ciret = RtcRegisterAlarmCallback(handle, RTC_ALARM_INDEX_A, RtcAlarmACallback);
129e41f4b71Sopenharmony_ciif (ret != 0) {
130e41f4b71Sopenharmony_ci    /* Error handling. */
131e41f4b71Sopenharmony_ci}
132e41f4b71Sopenharmony_ci```
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci### Performing RTC-related Operations
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci- Reading the RTC time
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ciCall **RtcReadTime()** to obtain the RTC time, which includes the year, month, day, day of week, hour, minute, second, and millisecond.
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci```c
142e41f4b71Sopenharmony_ciint32_t RtcReadTime(DevHandle handle, struct RtcTime *time);
143e41f4b71Sopenharmony_ci```
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci  **Table 5** Description of RtcReadTime
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci| **Parameter**| **Description**|
148e41f4b71Sopenharmony_ci| -------- | -------- |
149e41f4b71Sopenharmony_ci| handle     | RTC device handle.                                            |
150e41f4b71Sopenharmony_ci| time       | RTC time information, which includes the year, month, day, day of the week, hour, minute, second, and millisecond.|
151e41f4b71Sopenharmony_ci| **Return Value**| **Description**                                         |
152e41f4b71Sopenharmony_ci| 0          | The operation is successful.                                               |
153e41f4b71Sopenharmony_ci| Negative value      | The operation failed.                                               |
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci```
157e41f4b71Sopenharmony_ciint32_t ret;
158e41f4b71Sopenharmony_cistruct RtcTime tm;
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci/* Read RTC time from the RTC driver. */
161e41f4b71Sopenharmony_ciret = RtcReadTime(handle, &tm);
162e41f4b71Sopenharmony_ciif (ret != 0) {
163e41f4b71Sopenharmony_ci    /* Error handling. */
164e41f4b71Sopenharmony_ci}
165e41f4b71Sopenharmony_ci```
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci- Setting the RTC time
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ciCall **RtcWriteTime()** to set the RTC time.
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci```c
172e41f4b71Sopenharmony_ciint32_t RtcWriteTime(DevHandle handle, struct RtcTime *time);
173e41f4b71Sopenharmony_ci```
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci  **Table 6** Description of RtcWriteTime
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci| **Parameter**| **Description**|
178e41f4b71Sopenharmony_ci| -------- | -------- |
179e41f4b71Sopenharmony_ci| handle | RTC device handle.|
180e41f4b71Sopenharmony_ci| time | RTC time to set, which includes the year, month, day, day of the week, hour, minute, second, and millisecond.|
181e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
182e41f4b71Sopenharmony_ci| 0 | The operation is successful.|
183e41f4b71Sopenharmony_ci| Negative value| The operation failed.|
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ci> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
186e41f4b71Sopenharmony_ci> 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.
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci```
190e41f4b71Sopenharmony_ciint32_t ret;
191e41f4b71Sopenharmony_cistruct RtcTime tm;
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci/* Set the RTC time to UTC 2020/01/01 00:59:00 .000. */
194e41f4b71Sopenharmony_citm.year = 2020;
195e41f4b71Sopenharmony_citm.month = 01;
196e41f4b71Sopenharmony_citm.day = 01;
197e41f4b71Sopenharmony_citm.hour= 00;
198e41f4b71Sopenharmony_citm.minute = 59;
199e41f4b71Sopenharmony_citm.second = 00;
200e41f4b71Sopenharmony_citm.millisecond = 0;
201e41f4b71Sopenharmony_ci/* Write the RTC time. */
202e41f4b71Sopenharmony_ciret = RtcWriteTime(handle, &tm);
203e41f4b71Sopenharmony_ciif (ret != 0) {
204e41f4b71Sopenharmony_ci    /* Error handling. */
205e41f4b71Sopenharmony_ci}
206e41f4b71Sopenharmony_ci```
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci- Reading the RTC alarm time
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ciCall **RtcReadAlarm()** to obtain the RTC alarm time.
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci```c
213e41f4b71Sopenharmony_ciint32_t RtcReadAlarm(DevHandle handle, enum RtcAlarmIndex alarmIndex, struct RtcTime *time);
214e41f4b71Sopenharmony_ci```
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ci  **Table 7** Description of RtcReadAlarm
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci| **Parameter**| **Description**|
219e41f4b71Sopenharmony_ci| -------- | -------- |
220e41f4b71Sopenharmony_ci| handle | RTC device handle.|
221e41f4b71Sopenharmony_ci| alarmIndex | Alarm index.|
222e41f4b71Sopenharmony_ci| time | RTC alarm time information, which includes the year, month, day, day of the week, hour, minute, second, and millisecond.|
223e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
224e41f4b71Sopenharmony_ci| 0 | The operation is successful.|
225e41f4b71Sopenharmony_ci| Negative value      | The operation failed.|
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci```
229e41f4b71Sopenharmony_ciint32_t ret;
230e41f4b71Sopenharmony_cistruct RtcTime alarmTime;
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci/* Read the RTC alarm time of alarm RTC_ALARM_INDEX_A. */
233e41f4b71Sopenharmony_ciret = RtcReadAlarm(handle, RTC_ALARM_INDEX_A, &alarmTime);
234e41f4b71Sopenharmony_ciif (ret != 0) {
235e41f4b71Sopenharmony_ci    /* Error handling. */
236e41f4b71Sopenharmony_ci}
237e41f4b71Sopenharmony_ci```
238e41f4b71Sopenharmony_ci
239e41f4b71Sopenharmony_ci- Setting the RTC alarm time
240e41f4b71Sopenharmony_ci
241e41f4b71Sopenharmony_ciCall **RtcWriteAlarm()** to set the RTC alarm time based on the alarm index.
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ci```c
244e41f4b71Sopenharmony_ciint32_t RtcWriteAlarm(DevHandle handle, enum RtcAlarmIndex  alarmIndex, struct RtcTime *time);
245e41f4b71Sopenharmony_ci```
246e41f4b71Sopenharmony_ci
247e41f4b71Sopenharmony_ci  **Table 8** Description of RtcWriteAlarm
248e41f4b71Sopenharmony_ci
249e41f4b71Sopenharmony_ci| **Parameter**| **Description**|
250e41f4b71Sopenharmony_ci| -------- | -------- |
251e41f4b71Sopenharmony_ci| handle | RTC device handle.|
252e41f4b71Sopenharmony_ci| alarmIndex | Alarm index.|
253e41f4b71Sopenharmony_ci| time | RTC alarm time to set, which includes the year, month, day, day of the week, hour, minute, second, and millisecond.|
254e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
255e41f4b71Sopenharmony_ci| 0 | The operation is successful.|
256e41f4b71Sopenharmony_ci| Negative value| The operation failed.|
257e41f4b71Sopenharmony_ci
258e41f4b71Sopenharmony_ci> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
259e41f4b71Sopenharmony_ci> 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.
260e41f4b71Sopenharmony_ci
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ci```
263e41f4b71Sopenharmony_ciint32_t ret;
264e41f4b71Sopenharmony_cistruct RtcTime alarmTime;
265e41f4b71Sopenharmony_ci
266e41f4b71Sopenharmony_ci/* Set the RTC alarm time to 2020/01/01 00:59:59 .000. */
267e41f4b71Sopenharmony_cialarmTime.year = 2020;
268e41f4b71Sopenharmony_cialarmTime.month = 01;
269e41f4b71Sopenharmony_cialarmTime.day = 01;
270e41f4b71Sopenharmony_cialarmTime.hour = 00;
271e41f4b71Sopenharmony_cialarmTime.minute = 59;
272e41f4b71Sopenharmony_cialarmTime.second = 59;
273e41f4b71Sopenharmony_cialarmTime.millisecond = 0;
274e41f4b71Sopenharmony_ci/* Set the alarm time of alarm RTC_ALARM_INDEX_A. */
275e41f4b71Sopenharmony_ciret = RtcWriteAlarm(handle, RTC_ALARM_INDEX_A, &alarmTime);
276e41f4b71Sopenharmony_ciif (ret != 0) {
277e41f4b71Sopenharmony_ci    /* Error handling. */
278e41f4b71Sopenharmony_ci}
279e41f4b71Sopenharmony_ci```
280e41f4b71Sopenharmony_ci
281e41f4b71Sopenharmony_ci- Enabling or disabling alarm interrupts
282e41f4b71Sopenharmony_ci
283e41f4b71Sopenharmony_ciCall **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.
284e41f4b71Sopenharmony_ci
285e41f4b71Sopenharmony_ci```c
286e41f4b71Sopenharmony_ciint32_t RtcAlarmInterruptEnable(DevHandle handle, enum RtcAlarmIndex alarmIndex, uint8_t enable);
287e41f4b71Sopenharmony_ci```
288e41f4b71Sopenharmony_ci
289e41f4b71Sopenharmony_ci  **Table 9** Description of RtcAlarmInterruptEnable
290e41f4b71Sopenharmony_ci
291e41f4b71Sopenharmony_ci| **Parameter**| **Description**|
292e41f4b71Sopenharmony_ci| -------- | -------- |
293e41f4b71Sopenharmony_ci| handle     | RTC device handle.|
294e41f4b71Sopenharmony_ci| alarmIndex | Alarm index.|
295e41f4b71Sopenharmony_ci| enable     | Whether to enable RTC alarm interrupts. The value **1** means to enable interrupts and **0** means to disable interrupts.|
296e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
297e41f4b71Sopenharmony_ci| 0 | The operation is successful.|
298e41f4b71Sopenharmony_ci| Negative value| The operation failed.|
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ci
301e41f4b71Sopenharmony_ci```
302e41f4b71Sopenharmony_ciint32_t ret;
303e41f4b71Sopenharmony_ci
304e41f4b71Sopenharmony_ci/* Enable interrupts for an RTC alarm. */
305e41f4b71Sopenharmony_ciret = RtcAlarmInterruptEnable(handle, RTC_ALARM_INDEX_A, 1);
306e41f4b71Sopenharmony_ciif (ret != 0) {
307e41f4b71Sopenharmony_ci    /* Error handling. */
308e41f4b71Sopenharmony_ci}
309e41f4b71Sopenharmony_ci```
310e41f4b71Sopenharmony_ci
311e41f4b71Sopenharmony_ci- Obtaining the RTC external frequency
312e41f4b71Sopenharmony_ci
313e41f4b71Sopenharmony_ciCall **RtcGetFreq()** to obtain the frequency of the external crystal oscillator connected to the RTC driver.
314e41f4b71Sopenharmony_ci
315e41f4b71Sopenharmony_ci```c
316e41f4b71Sopenharmony_ciint32_t RtcGetFreq(DevHandle handle, uint32_t *freq);
317e41f4b71Sopenharmony_ci```
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ci  **Table 10** Description of RtcGetFreq
320e41f4b71Sopenharmony_ci
321e41f4b71Sopenharmony_ci| **Parameter**| **Description**|
322e41f4b71Sopenharmony_ci| -------- | -------- |
323e41f4b71Sopenharmony_ci| handle | RTC device handle.|
324e41f4b71Sopenharmony_ci| freq | Pointer to the frequency of the external crystal oscillator, in Hz.|
325e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
326e41f4b71Sopenharmony_ci| 0 | The operation is successful.|
327e41f4b71Sopenharmony_ci| Negative value| The operation failed.|
328e41f4b71Sopenharmony_ci
329e41f4b71Sopenharmony_ci
330e41f4b71Sopenharmony_ci```
331e41f4b71Sopenharmony_ciint32_t ret;
332e41f4b71Sopenharmony_ciuint32_t freq = 0;
333e41f4b71Sopenharmony_ci
334e41f4b71Sopenharmony_ci/* Obtain the frequency of the external crystal oscillator connected to the RTC driver. */
335e41f4b71Sopenharmony_ciret = RtcGetFreq(handle, &freq);
336e41f4b71Sopenharmony_ciif (ret != 0) {
337e41f4b71Sopenharmony_ci    /* Error handling. */
338e41f4b71Sopenharmony_ci}
339e41f4b71Sopenharmony_ci```
340e41f4b71Sopenharmony_ci
341e41f4b71Sopenharmony_ci- Setting the RTC external frequency
342e41f4b71Sopenharmony_ci
343e41f4b71Sopenharmony_ciCall **RtcSetFreq()** to set the frequency of the external crystal oscillator connected to the RTC driver.
344e41f4b71Sopenharmony_ci
345e41f4b71Sopenharmony_ci```c
346e41f4b71Sopenharmony_ciint32_t RtcSetFreq(DevHandle handle, uint32_t freq);
347e41f4b71Sopenharmony_ci```
348e41f4b71Sopenharmony_ci
349e41f4b71Sopenharmony_ci  **Table 11** Description of RtcSetFreq
350e41f4b71Sopenharmony_ci
351e41f4b71Sopenharmony_ci| **Parameter**| **Description**|
352e41f4b71Sopenharmony_ci| -------- | -------- |
353e41f4b71Sopenharmony_ci| handle | RTC device handle.|
354e41f4b71Sopenharmony_ci| freq | Pointer to the frequency to set for the external crystal oscillator, in Hz.|
355e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
356e41f4b71Sopenharmony_ci| 0 | The operation is successful.|
357e41f4b71Sopenharmony_ci| Negative value| The operation failed.|
358e41f4b71Sopenharmony_ci
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci```
361e41f4b71Sopenharmony_ciint32_t ret;
362e41f4b71Sopenharmony_ciuint32_t freq = 32768; /* 32768 Hz */
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ci/* Set the frequency of the external crystal oscillator based on the requirements specified in the manual of the device you use. */
365e41f4b71Sopenharmony_ciret = RtcSetFreq(handle, freq);
366e41f4b71Sopenharmony_ciif (ret != 0) {
367e41f4b71Sopenharmony_ci    /* Error handling. */
368e41f4b71Sopenharmony_ci}
369e41f4b71Sopenharmony_ci```
370e41f4b71Sopenharmony_ci
371e41f4b71Sopenharmony_ci- Resetting the RTC
372e41f4b71Sopenharmony_ci
373e41f4b71Sopenharmony_ciCall **RtcReset()** to reset the RTC. After the reset, the registers are restored to default values.
374e41f4b71Sopenharmony_ci
375e41f4b71Sopenharmony_ci```c
376e41f4b71Sopenharmony_ciint32_t RtcReset(DevHandle handle);
377e41f4b71Sopenharmony_ci```
378e41f4b71Sopenharmony_ci
379e41f4b71Sopenharmony_ci  **Table 12** Description of RtcReset
380e41f4b71Sopenharmony_ci
381e41f4b71Sopenharmony_ci| **Parameter**| **Description**|
382e41f4b71Sopenharmony_ci| -------- | -------- |
383e41f4b71Sopenharmony_ci| handle     | RTC device handle.|
384e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
385e41f4b71Sopenharmony_ci| 0 | The operation is successful.|
386e41f4b71Sopenharmony_ci| Negative value| The operation failed.|
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ci
389e41f4b71Sopenharmony_ci```
390e41f4b71Sopenharmony_ciint32_t ret;
391e41f4b71Sopenharmony_ci
392e41f4b71Sopenharmony_ci/* Reset the RTC to restore default values of registers. */
393e41f4b71Sopenharmony_ciret = RtcReset(handle);
394e41f4b71Sopenharmony_ciif (ret != 0) {
395e41f4b71Sopenharmony_ci    /* Error handling. */
396e41f4b71Sopenharmony_ci}
397e41f4b71Sopenharmony_ci```
398e41f4b71Sopenharmony_ci
399e41f4b71Sopenharmony_ci- Reading a custom RTC register
400e41f4b71Sopenharmony_ci
401e41f4b71Sopenharmony_ciCall **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.
402e41f4b71Sopenharmony_ci
403e41f4b71Sopenharmony_ci```c
404e41f4b71Sopenharmony_ciint32_t RtcReadReg(DevHandle handle, uint8_t usrDefIndex, uint8_t *value);
405e41f4b71Sopenharmony_ci```
406e41f4b71Sopenharmony_ci
407e41f4b71Sopenharmony_ci  **Table 13** Description of RtcReadReg
408e41f4b71Sopenharmony_ci
409e41f4b71Sopenharmony_ci| **Parameter**| **Description**|
410e41f4b71Sopenharmony_ci| -------- | -------- |
411e41f4b71Sopenharmony_ci| handle      | RTC device handle.|
412e41f4b71Sopenharmony_ci| usrDefIndex | Index of the custom register.|
413e41f4b71Sopenharmony_ci| value       | Register value.|
414e41f4b71Sopenharmony_ci| **Return Value** | **Description**|
415e41f4b71Sopenharmony_ci| 0 | The operation is successful.|
416e41f4b71Sopenharmony_ci| Negative value| The operation failed.|
417e41f4b71Sopenharmony_ci
418e41f4b71Sopenharmony_ci
419e41f4b71Sopenharmony_ci```
420e41f4b71Sopenharmony_ciint32_t ret;
421e41f4b71Sopenharmony_ciuint8_t usrDefIndex = 0; /* Define index 0 for the first custom register. */
422e41f4b71Sopenharmony_ciuint8_t value = 0;
423e41f4b71Sopenharmony_ci
424e41f4b71Sopenharmony_ci/* Read the value of custom RTC register 0. One index corresponds to one byte of the register value. */
425e41f4b71Sopenharmony_ciret = RtcReadReg(handle, usrDefIndex, &value);
426e41f4b71Sopenharmony_ciif (ret != 0) {
427e41f4b71Sopenharmony_ci    /* Error handling. */
428e41f4b71Sopenharmony_ci}
429e41f4b71Sopenharmony_ci```
430e41f4b71Sopenharmony_ci
431e41f4b71Sopenharmony_ci- Setting a custom RTC register
432e41f4b71Sopenharmony_ci
433e41f4b71Sopenharmony_ciCall **RtcWriteReg()** to set a register based on the specified register index. One index corresponds to one byte of the register value.
434e41f4b71Sopenharmony_ci
435e41f4b71Sopenharmony_ci```c
436e41f4b71Sopenharmony_ciint32_t RtcWriteReg(DevHandle handle, uint8_t usrDefIndex, uint8_t value);
437e41f4b71Sopenharmony_ci```
438e41f4b71Sopenharmony_ci
439e41f4b71Sopenharmony_ci  **Table 14** Description of RtcWriteReg
440e41f4b71Sopenharmony_ci
441e41f4b71Sopenharmony_ci| **Parameter**| **Description**|
442e41f4b71Sopenharmony_ci| -------- | -------- |
443e41f4b71Sopenharmony_ci| handle | RTC device handle.|
444e41f4b71Sopenharmony_ci| usrDefIndex | Index of the custom register.|
445e41f4b71Sopenharmony_ci| value | Register value.|
446e41f4b71Sopenharmony_ci| **Return Value** | **Description**|
447e41f4b71Sopenharmony_ci| 0 | The operation is successful.|
448e41f4b71Sopenharmony_ci| Negative value| The operation failed.|
449e41f4b71Sopenharmony_ci
450e41f4b71Sopenharmony_ci
451e41f4b71Sopenharmony_ci```
452e41f4b71Sopenharmony_ciint32_t ret;
453e41f4b71Sopenharmony_ciuint8_t usrDefIndex = 0; /* Define index 0 for the first custom register. */
454e41f4b71Sopenharmony_ciuint8_t value = 0x10;
455e41f4b71Sopenharmony_ci
456e41f4b71Sopenharmony_ci/* Set a value for register 0. One index corresponds to one byte of the configuration value. */
457e41f4b71Sopenharmony_ciret = RtcWriteReg(handle, usrDefIndex, value);
458e41f4b71Sopenharmony_ciif (ret != 0) {
459e41f4b71Sopenharmony_ci    /* Error handling. */
460e41f4b71Sopenharmony_ci}
461e41f4b71Sopenharmony_ci```
462e41f4b71Sopenharmony_ci
463e41f4b71Sopenharmony_ci
464e41f4b71Sopenharmony_ci## Example
465e41f4b71Sopenharmony_ci
466e41f4b71Sopenharmony_ciThe procedure is as follows:
467e41f4b71Sopenharmony_ci
468e41f4b71Sopenharmony_ci1. During the OS startup, the HDF identifies the RTC device in the system.
469e41f4b71Sopenharmony_ci
470e41f4b71Sopenharmony_ci2. The HDF initializes and creates the RTC device.
471e41f4b71Sopenharmony_ci
472e41f4b71Sopenharmony_ci3. Call APIs to perform operations on the RTC device.
473e41f4b71Sopenharmony_ci
474e41f4b71Sopenharmony_ci4. Close the RTC device handle to release resources.
475e41f4b71Sopenharmony_ci
476e41f4b71Sopenharmony_ciThe sample code is as follows:
477e41f4b71Sopenharmony_ci
478e41f4b71Sopenharmony_ci
479e41f4b71Sopenharmony_ci```
480e41f4b71Sopenharmony_ci#include "rtc_if.h"
481e41f4b71Sopenharmony_ciint32_t RtcAlarmACallback(enum RtcAlarmIndex alarmIndex)
482e41f4b71Sopenharmony_ci{
483e41f4b71Sopenharmony_ci    if (alarmIndex == RTC_ALARM_INDEX_A) {
484e41f4b71Sopenharmony_ci        /* Processing of alarm A. */
485e41f4b71Sopenharmony_ci        printf("RTC Alarm A callback function\n\r");
486e41f4b71Sopenharmony_ci    } else if (alarmIndex == RTC_ALARM_INDEX_B) {
487e41f4b71Sopenharmony_ci        /* Processing of alarm B. */
488e41f4b71Sopenharmony_ci        printf("RTC Alarm B callback function\n\r");
489e41f4b71Sopenharmony_ci    } else {
490e41f4b71Sopenharmony_ci        /* Error handling. */
491e41f4b71Sopenharmony_ci    }
492e41f4b71Sopenharmony_ci    return 0;
493e41f4b71Sopenharmony_ci}
494e41f4b71Sopenharmony_ci
495e41f4b71Sopenharmony_civoid RtcTestSample(void)
496e41f4b71Sopenharmony_ci{
497e41f4b71Sopenharmony_ci    int32_t ret;
498e41f4b71Sopenharmony_ci    struct RtcTime tm;
499e41f4b71Sopenharmony_ci    struct RtcTime alarmTime;
500e41f4b71Sopenharmony_ci    uint32_t freq;
501e41f4b71Sopenharmony_ci    DevHandle handle = NULL;
502e41f4b71Sopenharmony_ci
503e41f4b71Sopenharmony_ci    /* Open the RTC device handle. */
504e41f4b71Sopenharmony_ci    handle = RtcOpen();
505e41f4b71Sopenharmony_ci    if (handle == NULL) {
506e41f4b71Sopenharmony_ci        /* Error handling. */
507e41f4b71Sopenharmony_ci    }
508e41f4b71Sopenharmony_ci    /* Register a callback for alarm A if it is not generated at the specified time. */
509e41f4b71Sopenharmony_ci    ret = RtcRegisterAlarmCallback(handle, RTC_ALARM_INDEX_A, RtcAlarmACallback);
510e41f4b71Sopenharmony_ci    if (ret != 0) {
511e41f4b71Sopenharmony_ci        /* Error handling. */
512e41f4b71Sopenharmony_ci    }
513e41f4b71Sopenharmony_ci    /* Set the frequency of the external crystal oscillator based on the requirements specified in the manual of the device you use. */
514e41f4b71Sopenharmony_ci    freq = 32768; /* 32768 Hz */
515e41f4b71Sopenharmony_ci    ret = RtcSetFreq(handle, freq);
516e41f4b71Sopenharmony_ci    if (ret != 0) {
517e41f4b71Sopenharmony_ci        /* Error handling. */
518e41f4b71Sopenharmony_ci    }
519e41f4b71Sopenharmony_ci    /* Enable interrupts for RTC alarm A. */
520e41f4b71Sopenharmony_ci    ret = RtcAlarmInterruptEnable(handle, RTC_ALARM_INDEX_A, 1);
521e41f4b71Sopenharmony_ci    if (ret != 0) {
522e41f4b71Sopenharmony_ci        /* Error handling. */
523e41f4b71Sopenharmony_ci    }
524e41f4b71Sopenharmony_ci    /* Set the RTC time to 2020/01/01 00:00:10 .990. */
525e41f4b71Sopenharmony_ci    tm.year = 2020;
526e41f4b71Sopenharmony_ci    tm.month = 01;
527e41f4b71Sopenharmony_ci    tm.day = 01;
528e41f4b71Sopenharmony_ci    tm.hour= 0;
529e41f4b71Sopenharmony_ci    tm.minute = 0;
530e41f4b71Sopenharmony_ci    tm.second = 10;
531e41f4b71Sopenharmony_ci    tm.millisecond = 990;
532e41f4b71Sopenharmony_ci    /* Write the RTC time. */
533e41f4b71Sopenharmony_ci    ret = RtcWriteTime(handle, &tm);
534e41f4b71Sopenharmony_ci    if (ret != 0) {
535e41f4b71Sopenharmony_ci        /* Error handling. */
536e41f4b71Sopenharmony_ci    }
537e41f4b71Sopenharmony_ci    /* Set the RTC alarm time to 2020/01/01 00:00:30 .100. */
538e41f4b71Sopenharmony_ci    alarmTime.year = 2020;
539e41f4b71Sopenharmony_ci    alarmTime.month = 01;
540e41f4b71Sopenharmony_ci    alarmTime.day = 01;
541e41f4b71Sopenharmony_ci    alarmTime.hour = 0;
542e41f4b71Sopenharmony_ci    alarmTime.minute = 0;
543e41f4b71Sopenharmony_ci    alarmTime.second = 30;
544e41f4b71Sopenharmony_ci    alarmTime.millisecond = 100;
545e41f4b71Sopenharmony_ci    /* Set the alarm time for RTC_ALARM_INDEX_A. When the specified time is reached, "RTC Alarm A callback function" will be printed. */
546e41f4b71Sopenharmony_ci    ret = RtcWriteAlarm(handle, RTC_ALARM_INDEX_A, &alarmTime);
547e41f4b71Sopenharmony_ci    if (ret != 0) {
548e41f4b71Sopenharmony_ci        /* Error handling. */
549e41f4b71Sopenharmony_ci    }
550e41f4b71Sopenharmony_ci
551e41f4b71Sopenharmony_ci    /* Read the RTC time. */
552e41f4b71Sopenharmony_ci    ret = RtcReadTime(handle, &tm);
553e41f4b71Sopenharmony_ci    if (ret != 0) {
554e41f4b71Sopenharmony_ci        /* Error handling. */
555e41f4b71Sopenharmony_ci    }
556e41f4b71Sopenharmony_ci    sleep(5)
557e41f4b71Sopenharmony_ci    printf("RTC read time:\n\r");
558e41f4b71Sopenharmony_ci    printf("year-month-date-weekday hour:minute:second .millisecond %04u-%02u-%02u-%u %02u:%02u:%02u .%03u",
559e41f4b71Sopenharmony_ci        tm.year, tm.month, tm.day, tm.weekday, tm.hour, tm.minute, tm.second, tm.millisecond);
560e41f4b71Sopenharmony_ci    /* Close the RTC device handle. */
561e41f4b71Sopenharmony_ci    RtcClose(handle);
562e41f4b71Sopenharmony_ci}
563e41f4b71Sopenharmony_ci```
564