• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# RTC<a name="EN-US_TOPIC_0000001051778226"></a>
2
3-   [Overview](#section104842041574)
4    -   [Available APIs](#section3373340142215)
5
6-   [Usage Guidelines](#section20636145604113)
7    -   [How to Use](#section16919828134215)
8    -   [Creating an RTC Device Handle](#section1131212144310)
9    -   [Releasing the RTC Device Handle](#section10744117144314)
10    -   [Registering RtcAlarmCallback](#section14839440184320)
11    -   [Performing RTC-related Operations](#section161927578433)
12
13-   [Usage Example](#section1186111020456)
14
15## Overview<a name="section104842041574"></a>
16
17The 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.
18
19### Available APIs<a name="section3373340142215"></a>
20
21**Table  1**  APIs provided by the RTC driver
22
23<a name="table1731550155318"></a>
24<table><thead align="left"><tr id="row4419501537"><th class="cellrowborder" valign="top" width="21.902190219021904%" id="mcps1.2.4.1.1"><p id="p641050105320"><a name="p641050105320"></a><a name="p641050105320"></a><strong id="b17744818255"><a name="b17744818255"></a><a name="b17744818255"></a>Capability</strong></p>
25</th>
26<th class="cellrowborder" valign="top" width="24.98249824982498%" id="mcps1.2.4.1.2"><p id="p54150165315"><a name="p54150165315"></a><a name="p54150165315"></a><strong id="b257315114254"><a name="b257315114254"></a><a name="b257315114254"></a>Function</strong></p>
27</th>
28<th class="cellrowborder" valign="top" width="53.11531153115312%" id="mcps1.2.4.1.3"><p id="p941150145313"><a name="p941150145313"></a><a name="p941150145313"></a><strong id="b384185962317"><a name="b384185962317"></a><a name="b384185962317"></a>Description</strong></p>
29</th>
30</tr>
31</thead>
32<tbody><tr id="row17550163418501"><td class="cellrowborder" rowspan="2" valign="top" width="21.902190219021904%" headers="mcps1.2.4.1.1 "><p id="p11670643205012"><a name="p11670643205012"></a><a name="p11670643205012"></a>RTC handle</p>
33</td>
34<td class="cellrowborder" valign="top" width="24.98249824982498%" headers="mcps1.2.4.1.2 "><p id="p16550143465015"><a name="p16550143465015"></a><a name="p16550143465015"></a>RtcOpen</p>
35</td>
36<td class="cellrowborder" valign="top" width="53.11531153115312%" headers="mcps1.2.4.1.3 "><p id="p9550103415015"><a name="p9550103415015"></a><a name="p9550103415015"></a>Opens the RTC device to obtain its handle.</p>
37</td>
38</tr>
39<tr id="row1879052755020"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1479062716506"><a name="p1479062716506"></a><a name="p1479062716506"></a>RtcClose</p>
40</td>
41<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1579142745012"><a name="p1579142745012"></a><a name="p1579142745012"></a>Releases a specified handle of the RTC device.</p>
42</td>
43</tr>
44<tr id="row34145016535"><td class="cellrowborder" rowspan="2" valign="top" width="21.902190219021904%" headers="mcps1.2.4.1.1 "><p id="p229610227124"><a name="p229610227124"></a><a name="p229610227124"></a>RTC time</p>
45</td>
46<td class="cellrowborder" valign="top" width="24.98249824982498%" headers="mcps1.2.4.1.2 "><p id="p19389143041518"><a name="p19389143041518"></a><a name="p19389143041518"></a>RtcReadTime</p>
47</td>
48<td class="cellrowborder" valign="top" width="53.11531153115312%" headers="mcps1.2.4.1.3 "><p id="p8738101941716"><a name="p8738101941716"></a><a name="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>
49</td>
50</tr>
51<tr id="row5632152611414"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p143890309153"><a name="p143890309153"></a><a name="p143890309153"></a>RtcWriteTime</p>
52</td>
53<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p573815197171"><a name="p573815197171"></a><a name="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>
54</td>
55</tr>
56<tr id="row1468016592416"><td class="cellrowborder" rowspan="4" valign="top" width="21.902190219021904%" headers="mcps1.2.4.1.1 "><p id="p36817591648"><a name="p36817591648"></a><a name="p36817591648"></a>RTC alarm</p>
57</td>
58<td class="cellrowborder" valign="top" width="24.98249824982498%" headers="mcps1.2.4.1.2 "><p id="p968116598418"><a name="p968116598418"></a><a name="p968116598418"></a>RtcReadAlarm</p>
59</td>
60<td class="cellrowborder" valign="top" width="53.11531153115312%" headers="mcps1.2.4.1.3 "><p id="p768110592416"><a name="p768110592416"></a><a name="p768110592416"></a>Reads the RTC alarm time that was set last time.</p>
61</td>
62</tr>
63<tr id="row19313155514"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p73141951155"><a name="p73141951155"></a><a name="p73141951155"></a>RtcWriteAlarm</p>
64</td>
65<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p15314656511"><a name="p15314656511"></a><a name="p15314656511"></a>Writes the RTC alarm time based on the alarm index.</p>
66</td>
67</tr>
68<tr id="row138283422555"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p2829142165513"><a name="p2829142165513"></a><a name="p2829142165513"></a>RtcRegisterAlarmCallback</p>
69</td>
70<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p118291242155518"><a name="p118291242155518"></a><a name="p118291242155518"></a>Registers <strong id="b1648234343210"><a name="b1648234343210"></a><a name="b1648234343210"></a>RtcAlarmCallback</strong> that will be invoked when an alarm is generated at the specified time.</p>
71</td>
72</tr>
73<tr id="row13344113914568"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p2344103915616"><a name="p2344103915616"></a><a name="p2344103915616"></a>RtcAlarmInterruptEnable</p>
74</td>
75<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p034415397565"><a name="p034415397565"></a><a name="p034415397565"></a>Enables or disables RTC alarm interrupts.</p>
76</td>
77</tr>
78<tr id="row11801203517565"><td class="cellrowborder" rowspan="3" valign="top" width="21.902190219021904%" headers="mcps1.2.4.1.1 "><p id="p17261458942"><a name="p17261458942"></a><a name="p17261458942"></a>RTC configuration</p>
79</td>
80<td class="cellrowborder" valign="top" width="24.98249824982498%" headers="mcps1.2.4.1.2 "><p id="p15801153515561"><a name="p15801153515561"></a><a name="p15801153515561"></a>RtcGetFreq</p>
81</td>
82<td class="cellrowborder" valign="top" width="53.11531153115312%" headers="mcps1.2.4.1.3 "><p id="p980133515566"><a name="p980133515566"></a><a name="p980133515566"></a>Reads the frequency of the external crystal oscillator connected to the RTC driver.</p>
83</td>
84</tr>
85<tr id="row111502322563"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1615073225618"><a name="p1615073225618"></a><a name="p1615073225618"></a>RtcSetFreq</p>
86</td>
87<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p10150832165613"><a name="p10150832165613"></a><a name="p10150832165613"></a>Sets the frequency of the external crystal oscillator connected to the RTC driver.</p>
88</td>
89</tr>
90<tr id="row21771259145618"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1717725916562"><a name="p1717725916562"></a><a name="p1717725916562"></a>RtcReset</p>
91</td>
92<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p517712598569"><a name="p517712598569"></a><a name="p517712598569"></a>Resets the RTC.</p>
93</td>
94</tr>
95<tr id="row917116425716"><td class="cellrowborder" rowspan="2" valign="top" width="21.902190219021904%" headers="mcps1.2.4.1.1 "><p id="p144765201754"><a name="p144765201754"></a><a name="p144765201754"></a>Custom register</p>
96</td>
97<td class="cellrowborder" valign="top" width="24.98249824982498%" headers="mcps1.2.4.1.2 "><p id="p1117110418570"><a name="p1117110418570"></a><a name="p1117110418570"></a>RtcReadReg</p>
98</td>
99<td class="cellrowborder" valign="top" width="53.11531153115312%" headers="mcps1.2.4.1.3 "><p id="p1517114410578"><a name="p1517114410578"></a><a name="p1517114410578"></a>Reads the configuration of a custom RTC register based on the register index.</p>
100</td>
101</tr>
102<tr id="row46738190576"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1567331910573"><a name="p1567331910573"></a><a name="p1567331910573"></a>RtcWriteReg</p>
103</td>
104<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1967391913576"><a name="p1967391913576"></a><a name="p1967391913576"></a>Writes the configuration of a custom RTC register based on the register index.</p>
105</td>
106</tr>
107</tbody>
108</table>
109
110>![](../public_sys-resources/icon-note.gif) **NOTE:**
111>All functions provided in this document can be called only in kernel mode.
112
113## Usage Guidelines<a name="section20636145604113"></a>
114
115### How to Use<a name="section16919828134215"></a>
116
117During 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.
118
119[Figure 1](#fig166181128151112)  illustrates the process of using an RTC device.
120
121**Figure  1**  Process of using an RTC device<a name="fig166181128151112"></a>
122
123
124![](figure/en-us_image_0000001123675706.png)
125
126### Creating an RTC Device Handle<a name="section1131212144310"></a>
127
128After the RTC driver is loaded, you can use the API provided by the HDF and call APIs of the RTC driver.
129
130>![](../public_sys-resources/icon-note.gif) **NOTE:**
131>Currently, only one RTC device is supported in the OS.
132
133DevHandle RtcOpen\(void\);
134
135**Table  2**  Description of RtcOpen
136
137<a name="table1380712985611"></a>
138<table><tbody><tr id="row580722985616"><td class="cellrowborder" valign="top" width="21.45%"><p id="p1280722911565"><a name="p1280722911565"></a><a name="p1280722911565"></a><strong id="b748651620573"><a name="b748651620573"></a><a name="b748651620573"></a>Parameter</strong></p>
139</td>
140<td class="cellrowborder" valign="top" width="78.55%"><p id="p5807112965618"><a name="p5807112965618"></a><a name="p5807112965618"></a><strong id="b08682171578"><a name="b08682171578"></a><a name="b08682171578"></a>Description</strong></p>
141</td>
142</tr>
143<tr id="row17807112935611"><td class="cellrowborder" valign="top" width="21.45%"><p id="p13807132915565"><a name="p13807132915565"></a><a name="p13807132915565"></a>void</p>
144</td>
145<td class="cellrowborder" valign="top" width="78.55%"><p id="p15807429185612"><a name="p15807429185612"></a><a name="p15807429185612"></a>NA</p>
146</td>
147</tr>
148<tr id="row1980782911567"><td class="cellrowborder" valign="top" width="21.45%"><p id="p0807529165613"><a name="p0807529165613"></a><a name="p0807529165613"></a><strong id="b086691895718"><a name="b086691895718"></a><a name="b086691895718"></a>Return Value</strong></p>
149</td>
150<td class="cellrowborder" valign="top" width="78.55%"><p id="p9808162935612"><a name="p9808162935612"></a><a name="p9808162935612"></a><strong id="b6676151975710"><a name="b6676151975710"></a><a name="b6676151975710"></a>Description</strong></p>
151</td>
152</tr>
153<tr id="row2808192935615"><td class="cellrowborder" valign="top" width="21.45%"><p id="p380852915567"><a name="p380852915567"></a><a name="p380852915567"></a>handle</p>
154</td>
155<td class="cellrowborder" valign="top" width="78.55%"><p id="p26881319114110"><a name="p26881319114110"></a><a name="p26881319114110"></a>Returns the  if the operation is successful.</p>
156</td>
157</tr>
158<tr id="row4808142945615"><td class="cellrowborder" valign="top" width="21.45%"><p id="p188084291561"><a name="p188084291561"></a><a name="p188084291561"></a>NULL</p>
159</td>
160<td class="cellrowborder" valign="top" width="78.55%"><p id="p780852912566"><a name="p780852912566"></a><a name="p780852912566"></a>The operation fails.</p>
161</td>
162</tr>
163</tbody>
164</table>
165
166```
167DevHandle  handle = NULL;
168
169/* Obtain the RTC device handle. */
170handle = RtcOpen();
171if (handle  == NULL) {
172    /* Process the error. */
173}
174```
175
176### Releasing the RTC Device Handle<a name="section10744117144314"></a>
177
178You can call the following function to release the RTC device handle, thereby releasing resources of the device:
179
180void RtcClose\(DevHandle handle\);
181
182**Table  3**  Description of RtcClose
183
184<a name="table37525421510"></a>
185<table><tbody><tr id="row10752134216114"><td class="cellrowborder" valign="top" width="21.45%"><p id="p1075217421019"><a name="p1075217421019"></a><a name="p1075217421019"></a><strong id="b229461765710"><a name="b229461765710"></a><a name="b229461765710"></a>Parameter</strong></p>
186</td>
187<td class="cellrowborder" valign="top" width="78.55%"><p id="p117531421411"><a name="p117531421411"></a><a name="p117531421411"></a><strong id="b2038421875712"><a name="b2038421875712"></a><a name="b2038421875712"></a>Description</strong></p>
188</td>
189</tr>
190<tr id="row57531442914"><td class="cellrowborder" valign="top" width="21.45%"><p id="p8753164210119"><a name="p8753164210119"></a><a name="p8753164210119"></a>handle</p>
191</td>
192<td class="cellrowborder" valign="top" width="78.55%"><p id="p17533425113"><a name="p17533425113"></a><a name="p17533425113"></a>RTC device handle.</p>
193</td>
194</tr>
195</tbody>
196</table>
197
198```
199/* Release the RTC device handle. */
200RtcClose(handle);
201```
202
203### Registering RtcAlarmCallback<a name="section14839440184320"></a>
204
205After the OS is started, call the following function to register  **RtcAlarmCallback**, which will be invoked when an alarm is generated at the specified time:
206
207int32\_t RtcRegisterAlarmCallback\(DevHandle handle, enum RtcAlarmIndex alarmIndex, RtcAlarmCallback cb\);
208
209**Table  4**  Description of RtcRegisterAlarmCallback
210
211<a name="table7603619123820"></a>
212<table><thead align="left"><tr id="row1060351914386"><th class="cellrowborder" valign="top" width="21.36%" id="mcps1.2.3.1.1"><p id="p14603181917382"><a name="p14603181917382"></a><a name="p14603181917382"></a><strong id="b4295161718571"><a name="b4295161718571"></a><a name="b4295161718571"></a>Parameter</strong></p>
213</th>
214<th class="cellrowborder" valign="top" width="78.64%" id="mcps1.2.3.1.2"><p id="p36031519183819"><a name="p36031519183819"></a><a name="p36031519183819"></a><strong id="b43851218195716"><a name="b43851218195716"></a><a name="b43851218195716"></a>Description</strong></p>
215</th>
216</tr>
217</thead>
218<tbody><tr id="row1181618191115"><td class="cellrowborder" valign="top" width="21.36%" headers="mcps1.2.3.1.1 "><p id="p131811218131116"><a name="p131811218131116"></a><a name="p131811218131116"></a>handle</p>
219</td>
220<td class="cellrowborder" valign="top" width="78.64%" headers="mcps1.2.3.1.2 "><p id="p6181191851111"><a name="p6181191851111"></a><a name="p6181191851111"></a>RTC device handle.</p>
221</td>
222</tr>
223<tr id="row960361918383"><td class="cellrowborder" valign="top" width="21.36%" headers="mcps1.2.3.1.1 "><p id="p1775535165418"><a name="p1775535165418"></a><a name="p1775535165418"></a>alarmIndex</p>
224</td>
225<td class="cellrowborder" valign="top" width="78.64%" headers="mcps1.2.3.1.2 "><p id="p137551851185412"><a name="p137551851185412"></a><a name="p137551851185412"></a>Alarm index.</p>
226</td>
227</tr>
228<tr id="row1960431983813"><td class="cellrowborder" valign="top" width="21.36%" headers="mcps1.2.3.1.1 "><p id="p3604719123817"><a name="p3604719123817"></a><a name="p3604719123817"></a>cb</p>
229</td>
230<td class="cellrowborder" valign="top" width="78.64%" headers="mcps1.2.3.1.2 "><p id="p1560441923818"><a name="p1560441923818"></a><a name="p1560441923818"></a>Callback that will be invoked when an alarm is generated at the specified time.</p>
231</td>
232</tr>
233<tr id="row11410612183019"><td class="cellrowborder" valign="top" width="21.36%" headers="mcps1.2.3.1.1 "><p id="p460381915385"><a name="p460381915385"></a><a name="p460381915385"></a><strong id="b132291719185715"><a name="b132291719185715"></a><a name="b132291719185715"></a>Return Value</strong></p>
234</td>
235<td class="cellrowborder" valign="top" width="78.64%" headers="mcps1.2.3.1.2 "><p id="p96031619153812"><a name="p96031619153812"></a><a name="p96031619153812"></a><strong id="b5130182016576"><a name="b5130182016576"></a><a name="b5130182016576"></a>Description</strong></p>
236</td>
237</tr>
238<tr id="row15410111273017"><td class="cellrowborder" valign="top" width="21.36%" headers="mcps1.2.3.1.1 "><p id="p133081510112813"><a name="p133081510112813"></a><a name="p133081510112813"></a>0</p>
239</td>
240<td class="cellrowborder" valign="top" width="78.64%" headers="mcps1.2.3.1.2 "><p id="p530813107289"><a name="p530813107289"></a><a name="p530813107289"></a>The operation is successful.</p>
241</td>
242</tr>
243<tr id="row1241081213303"><td class="cellrowborder" valign="top" width="21.36%" headers="mcps1.2.3.1.1 "><p id="p1123362173010"><a name="p1123362173010"></a><a name="p1123362173010"></a>Negative value</p>
244</td>
245<td class="cellrowborder" valign="top" width="78.64%" headers="mcps1.2.3.1.2 "><p id="p1723362153010"><a name="p1723362153010"></a><a name="p1723362153010"></a>The operation fails.</p>
246</td>
247</tr>
248</tbody>
249</table>
250
251The following is an example of registering  **RtcAlarmCallback**  for processing alarm  **RTC\_ALARM\_INDEX\_A**:
252
253```
254/* Register an RTC alarm callback. */
255int32_t RtcAlarmACallback(enum RtcAlarmIndex alarmIndex)
256{
257    if (alarmIndex == RTC_ALARM_INDEX_A) {
258        /* Process alarm A. */
259    } else if (alarmIndex == RTC_ALARM_INDEX_B) {
260        /* Process alarm B. */
261    } else {
262        /* Process the error. */
263    }
264    return 0;
265}
266int32_t ret;
267/* Register RtcAlarmCallback for alarm A. */
268ret = RtcRegisterAlarmCallback(handle, RTC_ALARM_INDEX_A, RtcAlarmACallback);
269if (ret != 0) {
270    /* Process the error. */
271}
272```
273
274### Performing RTC-related Operations<a name="section161927578433"></a>
275
276-   Reading RTC time
277
278Call the following function to read time information from the RTC driver, including the year, month, the day of the week, day, hour, minute, second, and millisecond:
279
280int32\_t RtcReadTime\(DevHandle handle, struct RtcTime \*time\);
281
282**Table  5**  Description of RtcReadTime
283
284<a name="table1018490043"></a>
285<table><tbody><tr id="row31848013417"><td class="cellrowborder" valign="top" width="21.45%"><p id="p1415816132411"><a name="p1415816132411"></a><a name="p1415816132411"></a><strong id="b10296717125710"><a name="b10296717125710"></a><a name="b10296717125710"></a>Parameter</strong></p>
286</td>
287<td class="cellrowborder" valign="top" width="78.55%"><p id="p11158111316410"><a name="p11158111316410"></a><a name="p11158111316410"></a><strong id="b33873181576"><a name="b33873181576"></a><a name="b33873181576"></a>Description</strong></p>
288</td>
289</tr>
290<tr id="row1246615200297"><td class="cellrowborder" valign="top" width="21.45%"><p id="p188871821142917"><a name="p188871821142917"></a><a name="p188871821142917"></a>handle</p>
291</td>
292<td class="cellrowborder" valign="top" width="78.55%"><p id="p1788742182910"><a name="p1788742182910"></a><a name="p1788742182910"></a>RTC device handle.</p>
293</td>
294</tr>
295<tr id="row10184701945"><td class="cellrowborder" valign="top" width="21.45%"><p id="p104891871157"><a name="p104891871157"></a><a name="p104891871157"></a>time</p>
296</td>
297<td class="cellrowborder" valign="top" width="78.55%"><p id="p204891671156"><a name="p204891671156"></a><a name="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>
298</td>
299</tr>
300<tr id="row17393154515328"><td class="cellrowborder" valign="top" width="21.45%"><p id="p8158313248"><a name="p8158313248"></a><a name="p8158313248"></a><strong id="b162312198575"><a name="b162312198575"></a><a name="b162312198575"></a>Return Value</strong></p>
301</td>
302<td class="cellrowborder" valign="top" width="78.55%"><p id="p161591413741"><a name="p161591413741"></a><a name="p161591413741"></a><strong id="b3131220165711"><a name="b3131220165711"></a><a name="b3131220165711"></a>Description</strong></p>
303</td>
304</tr>
305<tr id="row339324593215"><td class="cellrowborder" valign="top" width="21.45%"><p id="p139599615287"><a name="p139599615287"></a><a name="p139599615287"></a>0</p>
306</td>
307<td class="cellrowborder" valign="top" width="78.55%"><p id="p1895911611284"><a name="p1895911611284"></a><a name="p1895911611284"></a>The operation is successful.</p>
308</td>
309</tr>
310<tr id="row15393184519323"><td class="cellrowborder" valign="top" width="21.45%"><p id="p13521182309"><a name="p13521182309"></a><a name="p13521182309"></a>Negative value</p>
311</td>
312<td class="cellrowborder" valign="top" width="78.55%"><p id="p1035216186309"><a name="p1035216186309"></a><a name="p1035216186309"></a>The operation fails.</p>
313</td>
314</tr>
315</tbody>
316</table>
317
318```
319int32_t ret;
320struct RtcTime tm;
321
322/* Read time information from the RTC driver. */
323ret = RtcReadTime(handle, &tm);
324if (ret != 0) {
325    /* Process the error. */
326}
327```
328
329-   Setting RTC time
330
331Call the following function to set the RTC time:
332
333int32\_t RtcWriteTime\(DevHandle handle, struct RtcTime \*time\);
334
335**Table  6**  Description of RtcWriteTime
336
337<a name="table223910318361"></a>
338<table><tbody><tr id="row924033173613"><td class="cellrowborder" valign="top" width="21.54%"><p id="p16240143143611"><a name="p16240143143611"></a><a name="p16240143143611"></a><strong id="b17297111720576"><a name="b17297111720576"></a><a name="b17297111720576"></a>Parameter</strong></p>
339</td>
340<td class="cellrowborder" valign="top" width="78.46%"><p id="p32401031113610"><a name="p32401031113610"></a><a name="p32401031113610"></a><strong id="b11388191825715"><a name="b11388191825715"></a><a name="b11388191825715"></a>Description</strong></p>
341</td>
342</tr>
343<tr id="row148011248153018"><td class="cellrowborder" valign="top" width="21.54%"><p id="p189641849113018"><a name="p189641849113018"></a><a name="p189641849113018"></a>handle</p>
344</td>
345<td class="cellrowborder" valign="top" width="78.46%"><p id="p20964249123013"><a name="p20964249123013"></a><a name="p20964249123013"></a>RTC device handle.</p>
346</td>
347</tr>
348<tr id="row024043193619"><td class="cellrowborder" valign="top" width="21.54%"><p id="p157679281384"><a name="p157679281384"></a><a name="p157679281384"></a>time</p>
349</td>
350<td class="cellrowborder" valign="top" width="78.46%"><p id="p167675286381"><a name="p167675286381"></a><a name="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>
351</td>
352</tr>
353<tr id="row424093120369"><td class="cellrowborder" valign="top" width="21.54%"><p id="p1240143114366"><a name="p1240143114366"></a><a name="p1240143114366"></a><strong id="b1023212194579"><a name="b1023212194579"></a><a name="b1023212194579"></a>Return Value</strong></p>
354</td>
355<td class="cellrowborder" valign="top" width="78.46%"><p id="p18241173133619"><a name="p18241173133619"></a><a name="p18241173133619"></a><strong id="b18132142035712"><a name="b18132142035712"></a><a name="b18132142035712"></a>Description</strong></p>
356</td>
357</tr>
358<tr id="row18241531153610"><td class="cellrowborder" valign="top" width="21.54%"><p id="p8550174182810"><a name="p8550174182810"></a><a name="p8550174182810"></a>0</p>
359</td>
360<td class="cellrowborder" valign="top" width="78.46%"><p id="p145503417284"><a name="p145503417284"></a><a name="p145503417284"></a>The operation is successful.</p>
361</td>
362</tr>
363<tr id="row024153123616"><td class="cellrowborder" valign="top" width="21.54%"><p id="p5602191619300"><a name="p5602191619300"></a><a name="p5602191619300"></a>Negative value</p>
364</td>
365<td class="cellrowborder" valign="top" width="78.46%"><p id="p12602131643015"><a name="p12602131643015"></a><a name="p12602131643015"></a>The operation fails.</p>
366</td>
367</tr>
368</tbody>
369</table>
370
371>![](../public_sys-resources/icon-note.gif) **NOTE:**
372>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.
373
374```
375int32_t ret;
376struct RtcTime tm;
377
378/* Set the RTC time to UTC 2020/01/01 00:59:00 .000. */
379tm.year = 2020;
380tm.month = 01;
381tm.day = 01;
382tm.hour= 00;
383tm.minute = 59;
384tm.second = 00;
385tm.millisecond = 0;
386/* Write the RTC time information. */
387ret = RtcWriteTime(handle, &tm);
388if (ret != 0) {
389    /* Process the error. */
390}
391```
392
393-   Reading the RTC alarm time
394
395Call the following function to read the alarm time:
396
397int32\_t RtcReadAlarm\(DevHandle handle, enum RtcAlarmIndex alarmIndex, struct RtcTime \*time\);
398
399**Table  7**  Description of RtcReadAlarm
400
401<a name="table11342203111420"></a>
402<table><tbody><tr id="row133429310140"><td class="cellrowborder" valign="top" width="21.54%"><p id="p9886411201416"><a name="p9886411201416"></a><a name="p9886411201416"></a><strong id="b152971517185720"><a name="b152971517185720"></a><a name="b152971517185720"></a>Parameter</strong></p>
403</td>
404<td class="cellrowborder" valign="top" width="78.46%"><p id="p2886411171417"><a name="p2886411171417"></a><a name="p2886411171417"></a><strong id="b4389418135718"><a name="b4389418135718"></a><a name="b4389418135718"></a>Description</strong></p>
405</td>
406</tr>
407<tr id="row13835647114113"><td class="cellrowborder" valign="top" width="21.54%"><p id="p1183011486412"><a name="p1183011486412"></a><a name="p1183011486412"></a>handle</p>
408</td>
409<td class="cellrowborder" valign="top" width="78.46%"><p id="p1583074815413"><a name="p1583074815413"></a><a name="p1583074815413"></a>RTC device handle.</p>
410</td>
411</tr>
412<tr id="row113439391410"><td class="cellrowborder" valign="top" width="21.54%"><p id="p132281120194318"><a name="p132281120194318"></a><a name="p132281120194318"></a>alarmIndex</p>
413</td>
414<td class="cellrowborder" valign="top" width="78.46%"><p id="p1022852034315"><a name="p1022852034315"></a><a name="p1022852034315"></a>Alarm index.</p>
415</td>
416</tr>
417<tr id="row193431836147"><td class="cellrowborder" valign="top" width="21.54%"><p id="p1926195164011"><a name="p1926195164011"></a><a name="p1926195164011"></a>time</p>
418</td>
419<td class="cellrowborder" valign="top" width="78.46%"><p id="p112695144013"><a name="p112695144013"></a><a name="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>
420</td>
421</tr>
422<tr id="row43438361419"><td class="cellrowborder" valign="top" width="21.54%"><p id="p388715117143"><a name="p388715117143"></a><a name="p388715117143"></a><strong id="b14234111985715"><a name="b14234111985715"></a><a name="b14234111985715"></a>Return Value</strong></p>
423</td>
424<td class="cellrowborder" valign="top" width="78.46%"><p id="p088719117143"><a name="p088719117143"></a><a name="p088719117143"></a><strong id="b413222011570"><a name="b413222011570"></a><a name="b413222011570"></a>Description</strong></p>
425</td>
426</tr>
427<tr id="row17169595467"><td class="cellrowborder" valign="top" width="21.54%"><p id="p107751111283"><a name="p107751111283"></a><a name="p107751111283"></a>0</p>
428</td>
429<td class="cellrowborder" valign="top" width="78.46%"><p id="p87751917289"><a name="p87751917289"></a><a name="p87751917289"></a>The operation is successful.</p>
430</td>
431</tr>
432<tr id="row016911915461"><td class="cellrowborder" valign="top" width="21.54%"><p id="p6833213133013"><a name="p6833213133013"></a><a name="p6833213133013"></a>Negative value</p>
433</td>
434<td class="cellrowborder" valign="top" width="78.46%"><p id="p168341213143015"><a name="p168341213143015"></a><a name="p168341213143015"></a>The operation fails.</p>
435</td>
436</tr>
437</tbody>
438</table>
439
440```
441int32_t ret;
442struct RtcTime alarmTime;
443
444/* Read the RTC alarm time information of alarm RTC_ALARM_INDEX_A. */
445ret = RtcReadAlarm(handle, RTC_ALARM_INDEX_A, &alarmTime);
446if (ret != 0) {
447    /* Process the error. */
448}
449```
450
451-   Setting RTC alarm time
452
453Call the following function to set the RTC alarm time based on the alarm index:
454
455int32\_t RtcWriteAlarm\(DevHandle handle, enum RtcAlarmIndex  alarmIndex, struct RtcTime \*time\);
456
457**Table  8**  Description of RtcWriteAlarm
458
459<a name="table107922162179"></a>
460<table><tbody><tr id="row14793316131710"><td class="cellrowborder" valign="top" width="21.62%"><p id="p1891718412183"><a name="p1891718412183"></a><a name="p1891718412183"></a><strong id="b029811725716"><a name="b029811725716"></a><a name="b029811725716"></a>Parameter</strong></p>
461</td>
462<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p891712461814"><a name="p891712461814"></a><a name="p891712461814"></a><strong id="b1339017181576"><a name="b1339017181576"></a><a name="b1339017181576"></a>Description</strong></p>
463</td>
464</tr>
465<tr id="row18419611133117"><td class="cellrowborder" valign="top" width="21.62%"><p id="p075881210314"><a name="p075881210314"></a><a name="p075881210314"></a>handle</p>
466</td>
467<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p975811215317"><a name="p975811215317"></a><a name="p975811215317"></a>RTC device handle.</p>
468</td>
469</tr>
470<tr id="row11793151613176"><td class="cellrowborder" valign="top" width="21.62%"><p id="p562522145215"><a name="p562522145215"></a><a name="p562522145215"></a>alarmIndex</p>
471</td>
472<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p126257215217"><a name="p126257215217"></a><a name="p126257215217"></a>Alarm index.</p>
473</td>
474</tr>
475<tr id="row37932016201720"><td class="cellrowborder" valign="top" width="21.62%"><p id="p26269212527"><a name="p26269212527"></a><a name="p26269212527"></a>time</p>
476</td>
477<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p462602125211"><a name="p462602125211"></a><a name="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>
478</td>
479</tr>
480<tr id="row586915225485"><td class="cellrowborder" valign="top" width="21.62%"><p id="p591712441810"><a name="p591712441810"></a><a name="p591712441810"></a><strong id="b1923501915574"><a name="b1923501915574"></a><a name="b1923501915574"></a>Return Value</strong></p>
481</td>
482<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p1591720461810"><a name="p1591720461810"></a><a name="p1591720461810"></a><strong id="b8133152014571"><a name="b8133152014571"></a><a name="b8133152014571"></a>Description</strong></p>
483</td>
484</tr>
485<tr id="row19869122210483"><td class="cellrowborder" valign="top" width="21.62%"><p id="p10798105812717"><a name="p10798105812717"></a><a name="p10798105812717"></a>0</p>
486</td>
487<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p1179817586276"><a name="p1179817586276"></a><a name="p1179817586276"></a>The operation is successful.</p>
488</td>
489</tr>
490<tr id="row1686918225483"><td class="cellrowborder" valign="top" width="21.62%"><p id="p16246181033012"><a name="p16246181033012"></a><a name="p16246181033012"></a>Negative value</p>
491</td>
492<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p3246111019309"><a name="p3246111019309"></a><a name="p3246111019309"></a>The operation fails.</p>
493</td>
494</tr>
495</tbody>
496</table>
497
498>![](../public_sys-resources/icon-note.gif) **NOTE:**
499>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.
500
501```
502int32_t ret;
503struct RtcTime alarmTime;
504
505/* Set the RTC alarm time to 2020/01/01 00:59:59 .000. */
506alarmTime.year = 2020;
507alarmTime.month = 01;
508alarmTime.day = 01;
509alarmTime.hour = 00;
510alarmTime.minute = 59;
511alarmTime.second = 59;
512alarmTime.millisecond = 0;
513/* Set the alarm time of alarm RTC_ALARM_INDEX_A. */
514ret = RtcWriteAlarm(handle, RTC_ALARM_INDEX_A, &alarmTime);
515if (ret != 0) {
516    /* Process the error. */
517}
518```
519
520-   Enabling or disabling alarm interrupts
521
522Before performing alarm operations, use the following function to enable alarm interrupts, so that  **RtcAlarmCallback**  will be called when the alarm is not generated upon a timeout:
523
524int32\_t RtcAlarmInterruptEnable\(DevHandle handle, enum RtcAlarmIndex alarmIndex, uint8\_t enable\);
525
526**Table  9**  Description of RtcAlarmInterruptEnable
527
528<a name="table1934615314159"></a>
529<table><tbody><tr id="row5346853171519"><td class="cellrowborder" valign="top" width="21.36%"><p id="p143464533153"><a name="p143464533153"></a><a name="p143464533153"></a><strong id="b829913171576"><a name="b829913171576"></a><a name="b829913171576"></a>Parameter</strong></p>
530</td>
531<td class="cellrowborder" valign="top" width="78.64%"><p id="p113461853171514"><a name="p113461853171514"></a><a name="p113461853171514"></a><strong id="b9391318185714"><a name="b9391318185714"></a><a name="b9391318185714"></a>Description</strong></p>
532</td>
533</tr>
534<tr id="row2125652144213"><td class="cellrowborder" valign="top" width="21.36%"><p id="p3150105313422"><a name="p3150105313422"></a><a name="p3150105313422"></a>handle</p>
535</td>
536<td class="cellrowborder" valign="top" width="78.64%"><p id="p9151653144212"><a name="p9151653144212"></a><a name="p9151653144212"></a>RTC device handle.</p>
537</td>
538</tr>
539<tr id="row153794518293"><td class="cellrowborder" valign="top" width="21.36%"><p id="p4259165518294"><a name="p4259165518294"></a><a name="p4259165518294"></a>alarmIndex</p>
540</td>
541<td class="cellrowborder" valign="top" width="78.64%"><p id="p725985522917"><a name="p725985522917"></a><a name="p725985522917"></a>Alarm index.</p>
542</td>
543</tr>
544<tr id="row19346653141518"><td class="cellrowborder" valign="top" width="21.36%"><p id="p6346253101516"><a name="p6346253101516"></a><a name="p6346253101516"></a>enable</p>
545</td>
546<td class="cellrowborder" valign="top" width="78.64%"><p id="p234655341511"><a name="p234655341511"></a><a name="p234655341511"></a>Whether to enable RTC alarm interrupts. The value <strong id="b879021119010"><a name="b879021119010"></a><a name="b879021119010"></a>1</strong> means to enable alarm interrupts and <strong id="b779617111204"><a name="b779617111204"></a><a name="b779617111204"></a>0</strong> means to disable alarm interrupts.</p>
547</td>
548</tr>
549<tr id="row18346953111513"><td class="cellrowborder" valign="top" width="21.36%"><p id="p53460537156"><a name="p53460537156"></a><a name="p53460537156"></a><strong id="b1023631919578"><a name="b1023631919578"></a><a name="b1023631919578"></a>Return Value</strong></p>
550</td>
551<td class="cellrowborder" valign="top" width="78.64%"><p id="p4346153171510"><a name="p4346153171510"></a><a name="p4346153171510"></a><strong id="b7134132075718"><a name="b7134132075718"></a><a name="b7134132075718"></a>Description</strong></p>
552</td>
553</tr>
554<tr id="row1234685314151"><td class="cellrowborder" valign="top" width="21.36%"><p id="p8947195310279"><a name="p8947195310279"></a><a name="p8947195310279"></a>0</p>
555</td>
556<td class="cellrowborder" valign="top" width="78.64%"><p id="p99471953152712"><a name="p99471953152712"></a><a name="p99471953152712"></a>The operation is successful.</p>
557</td>
558</tr>
559<tr id="row2347115321514"><td class="cellrowborder" valign="top" width="21.36%"><p id="p324855163018"><a name="p324855163018"></a><a name="p324855163018"></a>Negative value</p>
560</td>
561<td class="cellrowborder" valign="top" width="78.64%"><p id="p7248857302"><a name="p7248857302"></a><a name="p7248857302"></a>The operation fails.</p>
562</td>
563</tr>
564</tbody>
565</table>
566
567```
568int32_t ret;
569
570/* Enable the RTC alarm interrupts. */
571ret = RtcAlarmInterruptEnable(handle, RTC_ALARM_INDEX_A, 1);
572if (ret != 0) {
573    /* Process the error. */
574}
575```
576
577-   Reading RTC external frequency
578
579Call the following function to read the frequency of the external crystal oscillator connected to the RTC driver:
580
581int32\_t RtcGetFreq\(DevHandle handle, uint32\_t \*freq\);
582
583**Table  10**  Description of RtcGetFreq
584
585<a name="table125881625185"></a>
586<table><tbody><tr id="row1458811241816"><td class="cellrowborder" valign="top" width="21.36%"><p id="p658820241813"><a name="p658820241813"></a><a name="p658820241813"></a><strong id="b3300217175712"><a name="b3300217175712"></a><a name="b3300217175712"></a>Parameter</strong></p>
587</td>
588<td class="cellrowborder" valign="top" width="78.64%"><p id="p758812261820"><a name="p758812261820"></a><a name="p758812261820"></a><strong id="b33921718195713"><a name="b33921718195713"></a><a name="b33921718195713"></a>Description</strong></p>
589</td>
590</tr>
591<tr id="row831259124219"><td class="cellrowborder" valign="top" width="21.36%"><p id="p171541407431"><a name="p171541407431"></a><a name="p171541407431"></a>handle</p>
592</td>
593<td class="cellrowborder" valign="top" width="78.64%"><p id="p715413094314"><a name="p715413094314"></a><a name="p715413094314"></a>RTC device handle.</p>
594</td>
595</tr>
596<tr id="row1358842171820"><td class="cellrowborder" valign="top" width="21.36%"><p id="p258814210188"><a name="p258814210188"></a><a name="p258814210188"></a>freq</p>
597</td>
598<td class="cellrowborder" valign="top" width="78.64%"><p id="p165888211810"><a name="p165888211810"></a><a name="p165888211810"></a>Pointer to the frequency to set for the external crystal oscillator, in Hz.</p>
599</td>
600</tr>
601<tr id="row2058818281817"><td class="cellrowborder" valign="top" width="21.36%"><p id="p458872151810"><a name="p458872151810"></a><a name="p458872151810"></a><strong id="b223851915718"><a name="b223851915718"></a><a name="b223851915718"></a>Return Value</strong></p>
602</td>
603<td class="cellrowborder" valign="top" width="78.64%"><p id="p155880251819"><a name="p155880251819"></a><a name="p155880251819"></a><strong id="b0135182045715"><a name="b0135182045715"></a><a name="b0135182045715"></a>Description</strong></p>
604</td>
605</tr>
606<tr id="row35883213183"><td class="cellrowborder" valign="top" width="21.36%"><p id="p8133145211272"><a name="p8133145211272"></a><a name="p8133145211272"></a>0</p>
607</td>
608<td class="cellrowborder" valign="top" width="78.64%"><p id="p13133352202719"><a name="p13133352202719"></a><a name="p13133352202719"></a>The operation is successful.</p>
609</td>
610</tr>
611<tr id="row135892261811"><td class="cellrowborder" valign="top" width="21.36%"><p id="p152692538292"><a name="p152692538292"></a><a name="p152692538292"></a>Negative value</p>
612</td>
613<td class="cellrowborder" valign="top" width="78.64%"><p id="p327015313294"><a name="p327015313294"></a><a name="p327015313294"></a>The operation fails.</p>
614</td>
615</tr>
616</tbody>
617</table>
618
619```
620int32_t ret;
621uint32_t freq = 0;
622
623/* Read frequency of the external crystal oscillator connected to the RTC driver */
624ret = RtcGetFreq(handle, &freq);
625if (ret != 0) {
626    /* Process the error. */
627}
628```
629
630-   Setting the frequency of the external crystal oscillator connected to the RTC driver
631
632Call the following function to set the frequency of the external crystal oscillator connected to the RTC driver:
633
634int32\_t RtcSetFreq\(DevHandle handle, uint32\_t freq\);
635
636**Table  11**  Description of RtcSetFreq
637
638<a name="table1170124316209"></a>
639<table><tbody><tr id="row270119432202"><td class="cellrowborder" valign="top" width="21.36%"><p id="p127011343132010"><a name="p127011343132010"></a><a name="p127011343132010"></a><strong id="b130031711578"><a name="b130031711578"></a><a name="b130031711578"></a>Parameter</strong></p>
640</td>
641<td class="cellrowborder" valign="top" width="78.64%"><p id="p7701184372013"><a name="p7701184372013"></a><a name="p7701184372013"></a><strong id="b153942183570"><a name="b153942183570"></a><a name="b153942183570"></a>Description</strong></p>
642</td>
643</tr>
644<tr id="row36067554319"><td class="cellrowborder" valign="top" width="21.36%"><p id="p881511716433"><a name="p881511716433"></a><a name="p881511716433"></a>handle</p>
645</td>
646<td class="cellrowborder" valign="top" width="78.64%"><p id="p188157714432"><a name="p188157714432"></a><a name="p188157714432"></a>RTC device handle.</p>
647</td>
648</tr>
649<tr id="row127019437204"><td class="cellrowborder" valign="top" width="21.36%"><p id="p27019438207"><a name="p27019438207"></a><a name="p27019438207"></a>freq</p>
650</td>
651<td class="cellrowborder" valign="top" width="78.64%"><p id="p167021043182011"><a name="p167021043182011"></a><a name="p167021043182011"></a>Frequency to set for the external crystal oscillator, in Hz</p>
652</td>
653</tr>
654<tr id="row97022434205"><td class="cellrowborder" valign="top" width="21.36%"><p id="p1770218431208"><a name="p1770218431208"></a><a name="p1770218431208"></a><strong id="b18239121916575"><a name="b18239121916575"></a><a name="b18239121916575"></a>Return Value</strong></p>
655</td>
656<td class="cellrowborder" valign="top" width="78.64%"><p id="p1170217432201"><a name="p1170217432201"></a><a name="p1170217432201"></a><strong id="b161361720205713"><a name="b161361720205713"></a><a name="b161361720205713"></a>Description</strong></p>
657</td>
658</tr>
659<tr id="row1670212432206"><td class="cellrowborder" valign="top" width="21.36%"><p id="p77021543152017"><a name="p77021543152017"></a><a name="p77021543152017"></a>0</p>
660</td>
661<td class="cellrowborder" valign="top" width="78.64%"><p id="p770214310209"><a name="p770214310209"></a><a name="p770214310209"></a>The operation is successful.</p>
662</td>
663</tr>
664<tr id="row10702194313201"><td class="cellrowborder" valign="top" width="21.36%"><p id="p165182216306"><a name="p165182216306"></a><a name="p165182216306"></a>Negative value</p>
665</td>
666<td class="cellrowborder" valign="top" width="78.64%"><p id="p651815219302"><a name="p651815219302"></a><a name="p651815219302"></a>The operation fails.</p>
667</td>
668</tr>
669</tbody>
670</table>
671
672```
673int32_t ret;
674uint32_t freq = 32768; /* 32768 Hz */
675
676/* 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. */
677ret = RtcSetFreq(handle, freq);
678if (ret != 0) {
679    /* Process the error. */
680}
681```
682
683-   Resetting the RTC driver
684
685Call the following function to perform a reset on the RTC driver \(after the reset, the registers are restored to the default values\):
686
687int32\_t RtcReset\(DevHandle handle\);
688
689**Table  12**  Description of RtcReset
690
691<a name="table398973152517"></a>
692<table><tbody><tr id="row179899311254"><td class="cellrowborder" valign="top" width="21.36%"><p id="p199899314257"><a name="p199899314257"></a><a name="p199899314257"></a><strong id="b1330101719577"><a name="b1330101719577"></a><a name="b1330101719577"></a>Parameter</strong></p>
693</td>
694<td class="cellrowborder" valign="top" width="78.64%"><p id="p179897315257"><a name="p179897315257"></a><a name="p179897315257"></a><strong id="b9395318115714"><a name="b9395318115714"></a><a name="b9395318115714"></a>Description</strong></p>
695</td>
696</tr>
697<tr id="row14989113118257"><td class="cellrowborder" valign="top" width="21.36%"><p id="p129018136436"><a name="p129018136436"></a><a name="p129018136436"></a>handle</p>
698</td>
699<td class="cellrowborder" valign="top" width="78.64%"><p id="p290111304310"><a name="p290111304310"></a><a name="p290111304310"></a>RTC device handle.</p>
700</td>
701</tr>
702<tr id="row7989123111254"><td class="cellrowborder" valign="top" width="21.36%"><p id="p129894313251"><a name="p129894313251"></a><a name="p129894313251"></a><strong id="b424019198572"><a name="b424019198572"></a><a name="b424019198572"></a>Return Value</strong></p>
703</td>
704<td class="cellrowborder" valign="top" width="78.64%"><p id="p799013112256"><a name="p799013112256"></a><a name="p799013112256"></a><strong id="b913613204578"><a name="b913613204578"></a><a name="b913613204578"></a>Description</strong></p>
705</td>
706</tr>
707<tr id="row9990193142513"><td class="cellrowborder" valign="top" width="21.36%"><p id="p1999043111257"><a name="p1999043111257"></a><a name="p1999043111257"></a>0</p>
708</td>
709<td class="cellrowborder" valign="top" width="78.64%"><p id="p1799093182516"><a name="p1799093182516"></a><a name="p1799093182516"></a>The operation is successful.</p>
710</td>
711</tr>
712<tr id="row16990133152516"><td class="cellrowborder" valign="top" width="21.36%"><p id="p17536173573015"><a name="p17536173573015"></a><a name="p17536173573015"></a>Negative value</p>
713</td>
714<td class="cellrowborder" valign="top" width="78.64%"><p id="p1153623503014"><a name="p1153623503014"></a><a name="p1153623503014"></a>The operation fails.</p>
715</td>
716</tr>
717</tbody>
718</table>
719
720```
721int32_t ret;
722
723/* Reset the RTC driver. After the reset, the configuration registers are restored to the default values. */
724ret = RtcReset(handle);
725if (ret != 0) {
726    /* Process the error. */
727}
728```
729
730-   Reading the configuration of a custom RTC register
731
732Call the following 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\):
733
734int32\_t RtcReadReg\(DevHandle handle, uint8\_t usrDefIndex, uint8\_t \*value\);
735
736**Table  13**  Description of RtcReadReg
737
738<a name="table1624674153319"></a>
739<table><tbody><tr id="row92469423320"><td class="cellrowborder" valign="top" width="21.62%"><p id="p102461548331"><a name="p102461548331"></a><a name="p102461548331"></a><strong id="b73024170576"><a name="b73024170576"></a><a name="b73024170576"></a>Parameter</strong></p>
740</td>
741<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p15246847331"><a name="p15246847331"></a><a name="p15246847331"></a><strong id="b1439611818570"><a name="b1439611818570"></a><a name="b1439611818570"></a>Description</strong></p>
742</td>
743</tr>
744<tr id="row171318200434"><td class="cellrowborder" valign="top" width="21.62%"><p id="p9895321184320"><a name="p9895321184320"></a><a name="p9895321184320"></a>handle</p>
745</td>
746<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p68951214436"><a name="p68951214436"></a><a name="p68951214436"></a>RTC device handle.</p>
747</td>
748</tr>
749<tr id="row324614143314"><td class="cellrowborder" valign="top" width="21.62%"><p id="p82466433320"><a name="p82466433320"></a><a name="p82466433320"></a>usrDefIndex</p>
750</td>
751<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p181671825470"><a name="p181671825470"></a><a name="p181671825470"></a>Index of the custom register</p>
752</td>
753</tr>
754<tr id="row524716420334"><td class="cellrowborder" valign="top" width="21.62%"><p id="p8247844333"><a name="p8247844333"></a><a name="p8247844333"></a>value</p>
755</td>
756<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p524711413319"><a name="p524711413319"></a><a name="p524711413319"></a>Pointer to the register value</p>
757</td>
758</tr>
759<tr id="row112471143334"><td class="cellrowborder" valign="top" width="21.62%"><p id="p1124716483312"><a name="p1124716483312"></a><a name="p1124716483312"></a><strong id="b102421419175714"><a name="b102421419175714"></a><a name="b102421419175714"></a>Return Value</strong></p>
760</td>
761<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p1524716443313"><a name="p1524716443313"></a><a name="p1524716443313"></a><strong id="b11376206578"><a name="b11376206578"></a><a name="b11376206578"></a>Description</strong></p>
762</td>
763</tr>
764<tr id="row9247194183314"><td class="cellrowborder" valign="top" width="21.62%"><p id="p62476410334"><a name="p62476410334"></a><a name="p62476410334"></a>0</p>
765</td>
766<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p152471433317"><a name="p152471433317"></a><a name="p152471433317"></a>The operation is successful.</p>
767</td>
768</tr>
769<tr id="row1424719410333"><td class="cellrowborder" valign="top" width="21.62%"><p id="p112477417335"><a name="p112477417335"></a><a name="p112477417335"></a>Negative value</p>
770</td>
771<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p7247547338"><a name="p7247547338"></a><a name="p7247547338"></a>The operation fails.</p>
772</td>
773</tr>
774</tbody>
775</table>
776
777```
778int32_t ret;
779uint8_t usrDefIndex = 0; /* Define index 0 for the first custom register. */
780uint8_t value = 0;
781
782/* Read the configuration of a custom RTC register based on the register index. One index corresponds to one byte of the configuration value. */
783ret = RtcReadReg(handle, usrDefIndex, &value);
784if (ret != 0) {
785    /* Process the error. */
786}
787```
788
789-   Setting the configuration of a custom RTC register
790
791Call the following function to configure a register based on the specified register index \(one index corresponds to one byte of the configuration value\):
792
793int32\_t RtcWriteReg\(DevHandle handle, uint8\_t usrDefIndex, uint8\_t value\);
794
795**Table  14**  Description of RtcWriteReg
796
797<a name="table1072216482360"></a>
798<table><tbody><tr id="row187221648133611"><td class="cellrowborder" valign="top" width="21.62%"><p id="p2722184823617"><a name="p2722184823617"></a><a name="p2722184823617"></a><strong id="b530321795712"><a name="b530321795712"></a><a name="b530321795712"></a>Parameter</strong></p>
799</td>
800<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p1372374810367"><a name="p1372374810367"></a><a name="p1372374810367"></a><strong id="b1339761865714"><a name="b1339761865714"></a><a name="b1339761865714"></a>Description</strong></p>
801</td>
802</tr>
803<tr id="row1675092612435"><td class="cellrowborder" valign="top" width="21.62%"><p id="p7783927144312"><a name="p7783927144312"></a><a name="p7783927144312"></a>handle</p>
804</td>
805<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p197831427134310"><a name="p197831427134310"></a><a name="p197831427134310"></a>RTC device handle.</p>
806</td>
807</tr>
808<tr id="row1723174815367"><td class="cellrowborder" valign="top" width="21.62%"><p id="p1972364814366"><a name="p1972364814366"></a><a name="p1972364814366"></a>usrDefIndex</p>
809</td>
810<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p18723184819365"><a name="p18723184819365"></a><a name="p18723184819365"></a>Index of the custom register</p>
811</td>
812</tr>
813<tr id="row2723548163611"><td class="cellrowborder" valign="top" width="21.62%"><p id="p1772364893610"><a name="p1772364893610"></a><a name="p1772364893610"></a>value</p>
814</td>
815<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p1772324803619"><a name="p1772324803619"></a><a name="p1772324803619"></a>Register value</p>
816</td>
817</tr>
818<tr id="row1072314482361"><td class="cellrowborder" valign="top" width="21.62%"><p id="p6723194853618"><a name="p6723194853618"></a><a name="p6723194853618"></a><strong id="b172432193575"><a name="b172432193575"></a><a name="b172432193575"></a>Return Value</strong></p>
819</td>
820<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p1872314893614"><a name="p1872314893614"></a><a name="p1872314893614"></a><strong id="b131382020165711"><a name="b131382020165711"></a><a name="b131382020165711"></a>Description</strong></p>
821</td>
822</tr>
823<tr id="row20723148173617"><td class="cellrowborder" valign="top" width="21.62%"><p id="p12723348133615"><a name="p12723348133615"></a><a name="p12723348133615"></a>0</p>
824</td>
825<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p472324873620"><a name="p472324873620"></a><a name="p472324873620"></a>The operation is successful.</p>
826</td>
827</tr>
828<tr id="row127231848123615"><td class="cellrowborder" valign="top" width="21.62%"><p id="p197231148173613"><a name="p197231148173613"></a><a name="p197231148173613"></a>Negative value</p>
829</td>
830<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p16723134823618"><a name="p16723134823618"></a><a name="p16723134823618"></a>The operation fails.</p>
831</td>
832</tr>
833</tbody>
834</table>
835
836```
837int32_t ret;
838uint8_t usrDefIndex = 0; /* Define index 0 for the first custom register. */
839uint8_t value = 0x10;
840
841/* Configure a register based on the specified register index. One index corresponds to one byte of the configuration value. */
842ret = RtcWriteReg(handle, usrDefIndex, value);
843if (ret != 0) {
844    /* Process the error. */
845}
846```
847
848## Usage Example<a name="section1186111020456"></a>
849
850This section describes the process of using RTC APIs:
851
8521.  During the OS startup, the HDF identifies the RTC component in the system.
8532.  The HDF initializes and creates the RTC device.
8543.  You can perform operations on the RTC device by calling different APIs.
8554.  Call the  **RtcClose**  function to release the device handle and device resources.
856
857Example:
858
859```
860#include "rtc_if.h"
861int32_t RtcAlarmACallback(enum RtcAlarmIndex alarmIndex)
862{
863    if (alarmIndex == RTC_ALARM_INDEX_A) {
864        /* Process alarm A. */
865        printf("RTC Alarm A callback function\n\r");
866    } else if (alarmIndex == RTC_ALARM_INDEX_B) {
867        /* Process alarm B. */
868        printf("RTC Alarm B callback function\n\r");
869    } else {
870        /* Process the error. */
871    }
872    return 0;
873}
874
875void RtcTestSample(void)
876{
877    int32_t ret;
878    struct RtcTime tm;
879    struct RtcTime alarmTime;
880    uint32_t freq;
881    DevHandle handle = NULL;
882
883    /* Obtain the RTC device handle. */
884    handle = RtcOpen();
885    if (handle == NULL) {
886        /* Process the error. */
887    }
888    /* Register RtcAlarmCallback for alarm A. */
889    ret = RtcRegisterAlarmCallback(handle, RTC_ALARM_INDEX_A, RtcAlarmACallback);
890    if (ret != 0) {
891        /* Process the error. */
892    }
893    /* 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. */
894    freq = 32768; /* 32768 Hz */
895    ret = RtcSetFreq(handle, freq);
896    if (ret != 0) {
897        /* Process the error. */
898    }
899    /* Enable the RTC alarm interrupts. */
900    ret = RtcAlarmInterruptEnable(handle, RTC_ALARM_INDEX_A, 1);
901    if (ret != 0) {
902        /* Process the error. */
903    }
904    /* Set the RTC time to 2020/01/01 00:00:10 .990. */
905    tm.year = 2020;
906    tm.month = 01;
907    tm.day = 01;
908    tm.hour= 0;
909    tm.minute = 0;
910    tm.second = 10;
911    tm.millisecond = 990;
912    /* Write the RTC time information. */
913    ret = RtcWriteTime(handle, &tm);
914    if (ret != 0) {
915        /* Process the error. */
916    }
917    /* Set the RTC alarm time to 2020/01/01 00:00:30 .100. */
918    alarmTime.year = 2020;
919    alarmTime.month = 01;
920    alarmTime.day = 01;
921    alarmTime.hour = 0;
922    alarmTime.minute = 0;
923    alarmTime.second = 30;
924    alarmTime.millisecond = 100;
925    /* Set the alarm time information for RTC_ALARM_INDEX_A. When the specified time is reached, "RTC Alarm A callback function" is printed. */
926    ret = RtcWriteAlarm(handle, RTC_ALARM_INDEX_A, &alarmTime);
927    if (ret != 0) {
928        /* Process the error. */
929    }
930
931    /* Read the RTC real time. */
932    ret = RtcReadTime(handle, &tm);
933    if (ret != 0) {
934        /* Process the error. */
935    }
936    sleep(5)
937    printf("RTC read time:\n\r");
938    printf("year-month-date-weekday hour:minute:second .millisecond %04u-%02u-%02u-%u %02u:%02u:%02u .%03u",
939        tm.year, tm.month, tm.day, tm.weekday, tm.hour, tm.minute, tm.second, tm.millisecond);
940    /* Release the RTC device handle. */
941    RtcClose(handle);
942}
943```
944
945