1# RTC<a name="EN-US_TOPIC_0000001206373007"></a> 2 3## Overview<a name="section104842041574"></a> 4 5The 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. 6 7## Available APIs<a name="section20331159102519"></a> 8 9**Table 1** APIs provided by the RTC driver 10 11<a name="table1731550155318"></a> 12<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> 13</th> 14<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> 15</th> 16<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> 17</th> 18</tr> 19</thead> 20<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> 21</td> 22<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> 23</td> 24<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> 25</td> 26</tr> 27<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> 28</td> 29<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> 30</td> 31</tr> 32<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> 33</td> 34<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> 35</td> 36<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> 37</td> 38</tr> 39<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> 40</td> 41<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> 42</td> 43</tr> 44<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> 45</td> 46<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> 47</td> 48<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> 49</td> 50</tr> 51<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> 52</td> 53<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> 54</td> 55</tr> 56<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> 57</td> 58<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> 59</td> 60</tr> 61<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> 62</td> 63<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> 64</td> 65</tr> 66<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> 67</td> 68<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> 69</td> 70<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> 71</td> 72</tr> 73<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> 74</td> 75<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> 76</td> 77</tr> 78<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> 79</td> 80<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> 81</td> 82</tr> 83<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> 84</td> 85<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> 86</td> 87<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> 88</td> 89</tr> 90<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> 91</td> 92<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> 93</td> 94</tr> 95</tbody> 96</table> 97 98> **NOTE**<br> 99>All functions provided in this document can be called only in kernel mode. 100 101## Usage Guidelines<a name="section20636145604113"></a> 102 103### How to Use<a name="section16919828134215"></a> 104 105During 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. 106 107The figure below illustrates how to use the APIs. 108 109**Figure 1** Process of using an RTC device 110 111 112### Opening the RTC Device Handle<a name="section1131212144310"></a> 113 114After the RTC driver is loaded, you can use the API provided by the HDF and call APIs of the RTC driver. 115 116> **NOTE**<br> 117>Currently, only one RTC device is supported in the OS. 118 119DevHandle RtcOpen\(void\); 120 121**Table 2** Description of RtcOpen 122 123<a name="table1380712985611"></a> 124<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> 125</td> 126<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> 127</td> 128</tr> 129<tr id="row17807112935611"><td class="cellrowborder" valign="top" width="21.45%"><p id="p13807132915565"><a name="p13807132915565"></a><a name="p13807132915565"></a>void</p> 130</td> 131<td class="cellrowborder" valign="top" width="78.55%"><p id="p15807429185612"><a name="p15807429185612"></a><a name="p15807429185612"></a>NA</p> 132</td> 133</tr> 134<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> 135</td> 136<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> 137</td> 138</tr> 139<tr id="row2808192935615"><td class="cellrowborder" valign="top" width="21.45%"><p id="p380852915567"><a name="p380852915567"></a><a name="p380852915567"></a>handle</p> 140</td> 141<td class="cellrowborder" valign="top" width="78.55%"><p id="p26881319114110"><a name="p26881319114110"></a><a name="p26881319114110"></a>The operation is successful.</p> 142</td> 143</tr> 144<tr id="row4808142945615"><td class="cellrowborder" valign="top" width="21.45%"><p id="p188084291561"><a name="p188084291561"></a><a name="p188084291561"></a>NULL</p> 145</td> 146<td class="cellrowborder" valign="top" width="78.55%"><p id="p780852912566"><a name="p780852912566"></a><a name="p780852912566"></a>The operation failed.</p> 147</td> 148</tr> 149</tbody> 150</table> 151 152 153``` 154DevHandle handle = NULL; 155 156/* Obtain the RTC device handle. */ 157handle = RtcOpen(); 158if (handle == NULL) { 159 /* Process the error. */ 160} 161``` 162 163### Releasing the RTC Device Handle<a name="section10744117144314"></a> 164 165You can call **RtcClose()** function to release the RTC device handle, thereby releasing resources of the device. 166 167void RtcClose\(DevHandle handle\); 168 169**Table 3** Description of RtcClose 170 171<a name="table37525421510"></a> 172<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> 173</td> 174<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> 175</td> 176</tr> 177<tr id="row57531442914"><td class="cellrowborder" valign="top" width="21.45%"><p id="p8753164210119"><a name="p8753164210119"></a><a name="p8753164210119"></a>handle</p> 178</td> 179<td class="cellrowborder" valign="top" width="78.55%"><p id="p17533425113"><a name="p17533425113"></a><a name="p17533425113"></a>RTC device handle.</p> 180</td> 181</tr> 182</tbody> 183</table> 184 185``` 186/* Release the RTC device handle. */ 187RtcClose(handle); 188``` 189 190### Registering RtcAlarmCallback 191 192After the OS is started, call the following function to register **RtcAlarmCallback**, which will be invoked when an alarm is generated at the specified time: 193 194int32\_t RtcRegisterAlarmCallback\(DevHandle handle, enum RtcAlarmIndex alarmIndex, RtcAlarmCallback cb\); 195 196**Table 4** Description of RtcRegisterAlarmCallback 197 198<a name="table7603619123820"></a> 199<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> 200</th> 201<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> 202</th> 203</tr> 204</thead> 205<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> 206</td> 207<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> 208</td> 209</tr> 210<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> 211</td> 212<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> 213</td> 214</tr> 215<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> 216</td> 217<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> 218</td> 219</tr> 220<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> 221</td> 222<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> 223</td> 224</tr> 225<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> 226</td> 227<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> 228</td> 229</tr> 230<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> 231</td> 232<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 failed.</p> 233</td> 234</tr> 235</tbody> 236</table> 237 238The following is an example of registering **RtcAlarmCallback** for processing alarm **RTC\_ALARM\_INDEX\_A**: 239 240``` 241/* Register an RTC alarm callback. */ 242int32_t RtcAlarmACallback(enum RtcAlarmIndex alarmIndex) 243{ 244 if (alarmIndex == RTC_ALARM_INDEX_A) { 245 /* Process alarm A. */ 246 } else if (alarmIndex == RTC_ALARM_INDEX_B) { 247 /* Process alarm B. */ 248 } else { 249 /* Process the error. */ 250 } 251 return 0; 252} 253int32_t ret; 254/* Register RtcAlarmCallback for alarm A. */ 255ret = RtcRegisterAlarmCallback(handle, RTC_ALARM_INDEX_A, RtcAlarmACallback); 256if (ret != 0) { 257 /* Process the error. */ 258} 259``` 260 261### Performing RTC-related Operations 262 263- Reading RTC time 264 265Call 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. 266 267int32\_t RtcReadTime\(DevHandle handle, struct RtcTime \*time\); 268 269**Table 5** Description of RtcReadTime 270 271<a name="table1018490043"></a> 272<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> 273</td> 274<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> 275</td> 276</tr> 277<tr id="row1246615200297"><td class="cellrowborder" valign="top" width="21.45%"><p id="p188871821142917"><a name="p188871821142917"></a><a name="p188871821142917"></a>handle</p> 278</td> 279<td class="cellrowborder" valign="top" width="78.55%"><p id="p1788742182910"><a name="p1788742182910"></a><a name="p1788742182910"></a>RTC device handle.</p> 280</td> 281</tr> 282<tr id="row10184701945"><td class="cellrowborder" valign="top" width="21.45%"><p id="p104891871157"><a name="p104891871157"></a><a name="p104891871157"></a>time</p> 283</td> 284<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> 285</td> 286</tr> 287<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> 288</td> 289<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> 290</td> 291</tr> 292<tr id="row339324593215"><td class="cellrowborder" valign="top" width="21.45%"><p id="p139599615287"><a name="p139599615287"></a><a name="p139599615287"></a>0</p> 293</td> 294<td class="cellrowborder" valign="top" width="78.55%"><p id="p1895911611284"><a name="p1895911611284"></a><a name="p1895911611284"></a>The operation is successful.</p> 295</td> 296</tr> 297<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> 298</td> 299<td class="cellrowborder" valign="top" width="78.55%"><p id="p1035216186309"><a name="p1035216186309"></a><a name="p1035216186309"></a>The operation failed.</p> 300</td> 301</tr> 302</tbody> 303</table> 304 305``` 306int32_t ret; 307struct RtcTime tm; 308 309/* Read time information from the RTC driver. */ 310ret = RtcReadTime(handle, &tm); 311if (ret != 0) { 312 /* Process the error. */ 313} 314``` 315 316- Setting RTC time 317 318Call the **RtcWriteTime()** function to set the RTC time. 319 320int32\_t RtcWriteTime\(DevHandle handle, struct RtcTime \*time\); 321 322**Table 6** Description of RtcWriteTime 323 324<a name="table223910318361"></a> 325<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> 326</td> 327<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> 328</td> 329</tr> 330<tr id="row148011248153018"><td class="cellrowborder" valign="top" width="21.54%"><p id="p189641849113018"><a name="p189641849113018"></a><a name="p189641849113018"></a>handle</p> 331</td> 332<td class="cellrowborder" valign="top" width="78.46%"><p id="p20964249123013"><a name="p20964249123013"></a><a name="p20964249123013"></a>RTC device handle.</p> 333</td> 334</tr> 335<tr id="row024043193619"><td class="cellrowborder" valign="top" width="21.54%"><p id="p157679281384"><a name="p157679281384"></a><a name="p157679281384"></a>time</p> 336</td> 337<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> 338</td> 339</tr> 340<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> 341</td> 342<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> 343</td> 344</tr> 345<tr id="row18241531153610"><td class="cellrowborder" valign="top" width="21.54%"><p id="p8550174182810"><a name="p8550174182810"></a><a name="p8550174182810"></a>0</p> 346</td> 347<td class="cellrowborder" valign="top" width="78.46%"><p id="p145503417284"><a name="p145503417284"></a><a name="p145503417284"></a>The operation is successful.</p> 348</td> 349</tr> 350<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> 351</td> 352<td class="cellrowborder" valign="top" width="78.46%"><p id="p12602131643015"><a name="p12602131643015"></a><a name="p12602131643015"></a>The operation failed.</p> 353</td> 354</tr> 355</tbody> 356</table> 357 358> **NOTE**<br> 359>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. 360 361``` 362int32_t ret; 363struct RtcTime tm; 364 365/* Set the RTC time to UTC 2020/01/01 00:59:00 .000. */ 366tm.year = 2020; 367tm.month = 01; 368tm.day = 01; 369tm.hour= 00; 370tm.minute = 59; 371tm.second = 00; 372tm.millisecond = 0; 373/* Write the RTC time information. */ 374ret = RtcWriteTime(handle, &tm); 375if (ret != 0) { 376 /* Process the error. */ 377} 378``` 379 380- Reading the RTC alarm time 381 382Call the **RtcReadAlarm()** function to read the alarm time. 383 384int32\_t RtcReadAlarm\(DevHandle handle, enum RtcAlarmIndex alarmIndex, struct RtcTime \*time\); 385 386**Table 7** Description of RtcReadAlarm 387 388<a name="table11342203111420"></a> 389<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> 390</td> 391<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> 392</td> 393</tr> 394<tr id="row13835647114113"><td class="cellrowborder" valign="top" width="21.54%"><p id="p1183011486412"><a name="p1183011486412"></a><a name="p1183011486412"></a>handle</p> 395</td> 396<td class="cellrowborder" valign="top" width="78.46%"><p id="p1583074815413"><a name="p1583074815413"></a><a name="p1583074815413"></a>RTC device handle.</p> 397</td> 398</tr> 399<tr id="row113439391410"><td class="cellrowborder" valign="top" width="21.54%"><p id="p132281120194318"><a name="p132281120194318"></a><a name="p132281120194318"></a>alarmIndex</p> 400</td> 401<td class="cellrowborder" valign="top" width="78.46%"><p id="p1022852034315"><a name="p1022852034315"></a><a name="p1022852034315"></a>Alarm index.</p> 402</td> 403</tr> 404<tr id="row193431836147"><td class="cellrowborder" valign="top" width="21.54%"><p id="p1926195164011"><a name="p1926195164011"></a><a name="p1926195164011"></a>time</p> 405</td> 406<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> 407</td> 408</tr> 409<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> 410</td> 411<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> 412</td> 413</tr> 414<tr id="row17169595467"><td class="cellrowborder" valign="top" width="21.54%"><p id="p107751111283"><a name="p107751111283"></a><a name="p107751111283"></a>0</p> 415</td> 416<td class="cellrowborder" valign="top" width="78.46%"><p id="p87751917289"><a name="p87751917289"></a><a name="p87751917289"></a>The operation is successful.</p> 417</td> 418</tr> 419<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> 420</td> 421<td class="cellrowborder" valign="top" width="78.46%"><p id="p168341213143015"><a name="p168341213143015"></a><a name="p168341213143015"></a>The operation failed.</p> 422</td> 423</tr> 424</tbody> 425</table> 426 427``` 428int32_t ret; 429struct RtcTime alarmTime; 430 431/* Read the RTC alarm time information of alarm RTC_ALARM_INDEX_A. */ 432ret = RtcReadAlarm(handle, RTC_ALARM_INDEX_A, &alarmTime); 433if (ret != 0) { 434 /* Process the error. */ 435} 436``` 437 438- Setting RTC alarm time 439 440Call the **RtcWriteAlarm()** function to set the RTC alarm time based on the alarm index. 441 442int32\_t RtcWriteAlarm\(DevHandle handle, enum RtcAlarmIndex alarmIndex, struct RtcTime \*time\); 443 444**Table 8** Description of RtcWriteAlarm 445 446<a name="table107922162179"></a> 447<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> 448</td> 449<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> 450</td> 451</tr> 452<tr id="row18419611133117"><td class="cellrowborder" valign="top" width="21.62%"><p id="p075881210314"><a name="p075881210314"></a><a name="p075881210314"></a>handle</p> 453</td> 454<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p975811215317"><a name="p975811215317"></a><a name="p975811215317"></a>RTC device handle.</p> 455</td> 456</tr> 457<tr id="row11793151613176"><td class="cellrowborder" valign="top" width="21.62%"><p id="p562522145215"><a name="p562522145215"></a><a name="p562522145215"></a>alarmIndex</p> 458</td> 459<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p126257215217"><a name="p126257215217"></a><a name="p126257215217"></a>Alarm index.</p> 460</td> 461</tr> 462<tr id="row37932016201720"><td class="cellrowborder" valign="top" width="21.62%"><p id="p26269212527"><a name="p26269212527"></a><a name="p26269212527"></a>time</p> 463</td> 464<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> 465</td> 466</tr> 467<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> 468</td> 469<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> 470</td> 471</tr> 472<tr id="row19869122210483"><td class="cellrowborder" valign="top" width="21.62%"><p id="p10798105812717"><a name="p10798105812717"></a><a name="p10798105812717"></a>0</p> 473</td> 474<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p1179817586276"><a name="p1179817586276"></a><a name="p1179817586276"></a>The operation is successful.</p> 475</td> 476</tr> 477<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> 478</td> 479<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p3246111019309"><a name="p3246111019309"></a><a name="p3246111019309"></a>The operation failed.</p> 480</td> 481</tr> 482</tbody> 483</table> 484 485> **NOTE**<br> 486>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. 487 488``` 489int32_t ret; 490struct RtcTime alarmTime; 491 492/* Set the RTC alarm time to 2020/01/01 00:59:59 .000. */ 493alarmTime.year = 2020; 494alarmTime.month = 01; 495alarmTime.day = 01; 496alarmTime.hour = 00; 497alarmTime.minute = 59; 498alarmTime.second = 59; 499alarmTime.millisecond = 0; 500/* Set the alarm time of alarm RTC_ALARM_INDEX_A. */ 501ret = RtcWriteAlarm(handle, RTC_ALARM_INDEX_A, &alarmTime); 502if (ret != 0) { 503 /* Process the error. */ 504} 505``` 506 507- Enabling or disabling alarm interrupts 508 509Before 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. 510 511int32\_t RtcAlarmInterruptEnable\(DevHandle handle, enum RtcAlarmIndex alarmIndex, uint8\_t enable\); 512 513**Table 9** Description of RtcAlarmInterruptEnable 514 515<a name="table1934615314159"></a> 516<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> 517</td> 518<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> 519</td> 520</tr> 521<tr id="row2125652144213"><td class="cellrowborder" valign="top" width="21.36%"><p id="p3150105313422"><a name="p3150105313422"></a><a name="p3150105313422"></a>handle</p> 522</td> 523<td class="cellrowborder" valign="top" width="78.64%"><p id="p9151653144212"><a name="p9151653144212"></a><a name="p9151653144212"></a>RTC device handle.</p> 524</td> 525</tr> 526<tr id="row153794518293"><td class="cellrowborder" valign="top" width="21.36%"><p id="p4259165518294"><a name="p4259165518294"></a><a name="p4259165518294"></a>alarmIndex</p> 527</td> 528<td class="cellrowborder" valign="top" width="78.64%"><p id="p725985522917"><a name="p725985522917"></a><a name="p725985522917"></a>Alarm index.</p> 529</td> 530</tr> 531<tr id="row19346653141518"><td class="cellrowborder" valign="top" width="21.36%"><p id="p6346253101516"><a name="p6346253101516"></a><a name="p6346253101516"></a>enable</p> 532</td> 533<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> 534</td> 535</tr> 536<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> 537</td> 538<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> 539</td> 540</tr> 541<tr id="row1234685314151"><td class="cellrowborder" valign="top" width="21.36%"><p id="p8947195310279"><a name="p8947195310279"></a><a name="p8947195310279"></a>0</p> 542</td> 543<td class="cellrowborder" valign="top" width="78.64%"><p id="p99471953152712"><a name="p99471953152712"></a><a name="p99471953152712"></a>The operation is successful.</p> 544</td> 545</tr> 546<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> 547</td> 548<td class="cellrowborder" valign="top" width="78.64%"><p id="p7248857302"><a name="p7248857302"></a><a name="p7248857302"></a>The operation failed.</p> 549</td> 550</tr> 551</tbody> 552</table> 553 554``` 555int32_t ret; 556 557/* Enable the RTC alarm interrupts. */ 558ret = RtcAlarmInterruptEnable(handle, RTC_ALARM_INDEX_A, 1); 559if (ret != 0) { 560 /* Process the error. */ 561} 562``` 563 564- Reading RTC external frequency 565 566Call the **RtcGetFreq()** function to read the frequency of the external crystal oscillator connected to the RTC driver. 567 568int32\_t RtcGetFreq\(DevHandle handle, uint32\_t \*freq\); 569 570**Table 10** Description of RtcGetFreq 571 572<a name="table125881625185"></a> 573<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> 574</td> 575<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> 576</td> 577</tr> 578<tr id="row831259124219"><td class="cellrowborder" valign="top" width="21.36%"><p id="p171541407431"><a name="p171541407431"></a><a name="p171541407431"></a>handle</p> 579</td> 580<td class="cellrowborder" valign="top" width="78.64%"><p id="p715413094314"><a name="p715413094314"></a><a name="p715413094314"></a>RTC device handle.</p> 581</td> 582</tr> 583<tr id="row1358842171820"><td class="cellrowborder" valign="top" width="21.36%"><p id="p258814210188"><a name="p258814210188"></a><a name="p258814210188"></a>freq</p> 584</td> 585<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> 586</td> 587</tr> 588<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> 589</td> 590<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> 591</td> 592</tr> 593<tr id="row35883213183"><td class="cellrowborder" valign="top" width="21.36%"><p id="p8133145211272"><a name="p8133145211272"></a><a name="p8133145211272"></a>0</p> 594</td> 595<td class="cellrowborder" valign="top" width="78.64%"><p id="p13133352202719"><a name="p13133352202719"></a><a name="p13133352202719"></a>The operation is successful.</p> 596</td> 597</tr> 598<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> 599</td> 600<td class="cellrowborder" valign="top" width="78.64%"><p id="p327015313294"><a name="p327015313294"></a><a name="p327015313294"></a>The operation failed.</p> 601</td> 602</tr> 603</tbody> 604</table> 605 606``` 607int32_t ret; 608uint32_t freq = 0; 609 610/* Read frequency of the external crystal oscillator connected to the RTC driver */ 611ret = RtcGetFreq(handle, &freq); 612if (ret != 0) { 613 /* Process the error. */ 614} 615``` 616 617- Setting the frequency of the external crystal oscillator connected to the RTC driver 618 619Call the **RtcSetFreq()** function to set the frequency of the external crystal oscillator connected to the RTC driver. 620 621int32\_t RtcSetFreq\(DevHandle handle, uint32\_t freq\); 622 623**Table 11** Description of RtcSetFreq 624 625<a name="table1170124316209"></a> 626<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> 627</td> 628<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> 629</td> 630</tr> 631<tr id="row36067554319"><td class="cellrowborder" valign="top" width="21.36%"><p id="p881511716433"><a name="p881511716433"></a><a name="p881511716433"></a>handle</p> 632</td> 633<td class="cellrowborder" valign="top" width="78.64%"><p id="p188157714432"><a name="p188157714432"></a><a name="p188157714432"></a>RTC device handle.</p> 634</td> 635</tr> 636<tr id="row127019437204"><td class="cellrowborder" valign="top" width="21.36%"><p id="p27019438207"><a name="p27019438207"></a><a name="p27019438207"></a>freq</p> 637</td> 638<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> 639</td> 640</tr> 641<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> 642</td> 643<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> 644</td> 645</tr> 646<tr id="row1670212432206"><td class="cellrowborder" valign="top" width="21.36%"><p id="p77021543152017"><a name="p77021543152017"></a><a name="p77021543152017"></a>0</p> 647</td> 648<td class="cellrowborder" valign="top" width="78.64%"><p id="p770214310209"><a name="p770214310209"></a><a name="p770214310209"></a>The operation is successful.</p> 649</td> 650</tr> 651<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> 652</td> 653<td class="cellrowborder" valign="top" width="78.64%"><p id="p651815219302"><a name="p651815219302"></a><a name="p651815219302"></a>The operation failed.</p> 654</td> 655</tr> 656</tbody> 657</table> 658 659``` 660int32_t ret; 661uint32_t freq = 32768; /* 32768 Hz */ 662 663/* 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. */ 664ret = RtcSetFreq(handle, freq); 665if (ret != 0) { 666 /* Process the error. */ 667} 668``` 669 670- Resetting the RTC driver 671 672Call the **RtcReset()** function to perform a reset on the RTC driver \(after the reset, the registers are restored to the default values\). 673 674int32\_t RtcReset\(DevHandle handle\); 675 676**Table 12** Description of RtcReset 677 678<a name="table398973152517"></a> 679<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> 680</td> 681<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> 682</td> 683</tr> 684<tr id="row14989113118257"><td class="cellrowborder" valign="top" width="21.36%"><p id="p129018136436"><a name="p129018136436"></a><a name="p129018136436"></a>handle</p> 685</td> 686<td class="cellrowborder" valign="top" width="78.64%"><p id="p290111304310"><a name="p290111304310"></a><a name="p290111304310"></a>RTC device handle.</p> 687</td> 688</tr> 689<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> 690</td> 691<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> 692</td> 693</tr> 694<tr id="row9990193142513"><td class="cellrowborder" valign="top" width="21.36%"><p id="p1999043111257"><a name="p1999043111257"></a><a name="p1999043111257"></a>0</p> 695</td> 696<td class="cellrowborder" valign="top" width="78.64%"><p id="p1799093182516"><a name="p1799093182516"></a><a name="p1799093182516"></a>The operation is successful.</p> 697</td> 698</tr> 699<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> 700</td> 701<td class="cellrowborder" valign="top" width="78.64%"><p id="p1153623503014"><a name="p1153623503014"></a><a name="p1153623503014"></a>The operation failed.</p> 702</td> 703</tr> 704</tbody> 705</table> 706 707``` 708int32_t ret; 709 710/* Reset the RTC driver. After the reset, the configuration registers are restored to the default values. */ 711ret = RtcReset(handle); 712if (ret != 0) { 713 /* Process the error. */ 714} 715``` 716 717- Reading the configuration of a custom RTC register 718 719Call 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\): 720 721int32\_t RtcReadReg\(DevHandle handle, uint8\_t usrDefIndex, uint8\_t \*value\); 722 723**Table 13** Description of RtcReadReg 724 725<a name="table1624674153319"></a> 726<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> 727</td> 728<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> 729</td> 730</tr> 731<tr id="row171318200434"><td class="cellrowborder" valign="top" width="21.62%"><p id="p9895321184320"><a name="p9895321184320"></a><a name="p9895321184320"></a>handle</p> 732</td> 733<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p68951214436"><a name="p68951214436"></a><a name="p68951214436"></a>RTC device handle.</p> 734</td> 735</tr> 736<tr id="row324614143314"><td class="cellrowborder" valign="top" width="21.62%"><p id="p82466433320"><a name="p82466433320"></a><a name="p82466433320"></a>usrDefIndex</p> 737</td> 738<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> 739</td> 740</tr> 741<tr id="row524716420334"><td class="cellrowborder" valign="top" width="21.62%"><p id="p8247844333"><a name="p8247844333"></a><a name="p8247844333"></a>value</p> 742</td> 743<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> 744</td> 745</tr> 746<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> 747</td> 748<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> 749</td> 750</tr> 751<tr id="row9247194183314"><td class="cellrowborder" valign="top" width="21.62%"><p id="p62476410334"><a name="p62476410334"></a><a name="p62476410334"></a>0</p> 752</td> 753<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p152471433317"><a name="p152471433317"></a><a name="p152471433317"></a>The operation is successful.</p> 754</td> 755</tr> 756<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> 757</td> 758<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p7247547338"><a name="p7247547338"></a><a name="p7247547338"></a>The operation failed.</p> 759</td> 760</tr> 761</tbody> 762</table> 763 764``` 765int32_t ret; 766uint8_t usrDefIndex = 0; /* Define index 0 for the first custom register. */ 767uint8_t value = 0; 768 769/* Read the configuration of a custom RTC register based on the register index. One index corresponds to one byte of the configuration value. */ 770ret = RtcReadReg(handle, usrDefIndex, &value); 771if (ret != 0) { 772 /* Process the error. */ 773} 774``` 775 776- Setting the configuration of a custom RTC register 777 778Call the **RtcWriteReg()** function to configure a register based on the specified register index \(one index corresponds to one byte of the configuration value\). 779 780int32\_t RtcWriteReg\(DevHandle handle, uint8\_t usrDefIndex, uint8\_t value\); 781 782**Table 14** Description of RtcWriteReg 783 784<a name="table1072216482360"></a> 785<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> 786</td> 787<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> 788</td> 789</tr> 790<tr id="row1675092612435"><td class="cellrowborder" valign="top" width="21.62%"><p id="p7783927144312"><a name="p7783927144312"></a><a name="p7783927144312"></a>handle</p> 791</td> 792<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p197831427134310"><a name="p197831427134310"></a><a name="p197831427134310"></a>RTC device handle.</p> 793</td> 794</tr> 795<tr id="row1723174815367"><td class="cellrowborder" valign="top" width="21.62%"><p id="p1972364814366"><a name="p1972364814366"></a><a name="p1972364814366"></a>usrDefIndex</p> 796</td> 797<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> 798</td> 799</tr> 800<tr id="row2723548163611"><td class="cellrowborder" valign="top" width="21.62%"><p id="p1772364893610"><a name="p1772364893610"></a><a name="p1772364893610"></a>value</p> 801</td> 802<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p1772324803619"><a name="p1772324803619"></a><a name="p1772324803619"></a>Register value</p> 803</td> 804</tr> 805<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> 806</td> 807<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> 808</td> 809</tr> 810<tr id="row20723148173617"><td class="cellrowborder" valign="top" width="21.62%"><p id="p12723348133615"><a name="p12723348133615"></a><a name="p12723348133615"></a>0</p> 811</td> 812<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p472324873620"><a name="p472324873620"></a><a name="p472324873620"></a>The operation is successful.</p> 813</td> 814</tr> 815<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> 816</td> 817<td class="cellrowborder" valign="top" width="78.38000000000001%"><p id="p16723134823618"><a name="p16723134823618"></a><a name="p16723134823618"></a>The operation failed.</p> 818</td> 819</tr> 820</tbody> 821</table> 822 823``` 824int32_t ret; 825uint8_t usrDefIndex = 0; /* Define index 0 for the first custom register. */ 826uint8_t value = 0x10; 827 828/* Configure a register based on the specified register index. One index corresponds to one byte of the configuration value. */ 829ret = RtcWriteReg(handle, usrDefIndex, value); 830if (ret != 0) { 831 /* Process the error. */ 832} 833``` 834 835## Usage Example<a name="section1186111020456"></a> 836 837This section describes the process of using RTC APIs: 838 8391. During the OS startup, the HDF identifies the RTC component in the system. 8402. The HDF initializes and creates the RTC device. 8413. You can perform operations on the RTC device by calling different APIs. 8424. Call the **RtcClose** function to release the device handle and device resources. 843 844Example: 845 846``` 847#include "rtc_if.h" 848int32_t RtcAlarmACallback(enum RtcAlarmIndex alarmIndex) 849{ 850 if (alarmIndex == RTC_ALARM_INDEX_A) { 851 /* Process alarm A. */ 852 printf("RTC Alarm A callback function\n\r"); 853 } else if (alarmIndex == RTC_ALARM_INDEX_B) { 854 /* Process alarm B. */ 855 printf("RTC Alarm B callback function\n\r"); 856 } else { 857 /* Process the error. */ 858 } 859 return 0; 860} 861 862void RtcTestSample(void) 863{ 864 int32_t ret; 865 struct RtcTime tm; 866 struct RtcTime alarmTime; 867 uint32_t freq; 868 DevHandle handle = NULL; 869 870 /* Obtain the RTC device handle. */ 871 handle = RtcOpen(); 872 if (handle == NULL) { 873 /* Process the error. */ 874 } 875 /* Register RtcAlarmCallback for alarm A. */ 876 ret = RtcRegisterAlarmCallback(handle, RTC_ALARM_INDEX_A, RtcAlarmACallback); 877 if (ret != 0) { 878 /* Process the error. */ 879 } 880 /* 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. */ 881 freq = 32768; /* 32768 Hz */ 882 ret = RtcSetFreq(handle, freq); 883 if (ret != 0) { 884 /* Process the error. */ 885 } 886 /* Enable the RTC alarm interrupts. */ 887 ret = RtcAlarmInterruptEnable(handle, RTC_ALARM_INDEX_A, 1); 888 if (ret != 0) { 889 /* Process the error. */ 890 } 891 /* Set the RTC time to 2020/01/01 00:00:10 .990. */ 892 tm.year = 2020; 893 tm.month = 01; 894 tm.day = 01; 895 tm.hour= 0; 896 tm.minute = 0; 897 tm.second = 10; 898 tm.millisecond = 990; 899 /* Write the RTC time information. */ 900 ret = RtcWriteTime(handle, &tm); 901 if (ret != 0) { 902 /* Process the error. */ 903 } 904 /* Set the RTC alarm time to 2020/01/01 00:00:30 .100. */ 905 alarmTime.year = 2020; 906 alarmTime.month = 01; 907 alarmTime.day = 01; 908 alarmTime.hour = 0; 909 alarmTime.minute = 0; 910 alarmTime.second = 30; 911 alarmTime.millisecond = 100; 912 /* Set the alarm time information for RTC_ALARM_INDEX_A. When the specified time is reached, "RTC Alarm A callback function" is printed. */ 913 ret = RtcWriteAlarm(handle, RTC_ALARM_INDEX_A, &alarmTime); 914 if (ret != 0) { 915 /* Process the error. */ 916 } 917 918 /* Read the RTC real time. */ 919 ret = RtcReadTime(handle, &tm); 920 if (ret != 0) { 921 /* Process the error. */ 922 } 923 sleep(5) 924 printf("RTC read time:\n\r"); 925 printf("year-month-date-weekday hour:minute:second .millisecond %04u-%02u-%02u-%u %02u:%02u:%02u .%03u", 926 tm.year, tm.month, tm.day, tm.weekday, tm.hour, tm.minute, tm.second, tm.millisecond); 927 /* Release the RTC device handle. */ 928 RtcClose(handle); 929} 930```