driver-platform-rtc-develop.md 15.6 KB
Newer Older
A
Annie_wang 已提交
1
# RTC
D
duangavin123 已提交
2 3


A
Annie_wang 已提交
4
## Overview
D
duangavin123 已提交
5

A
Annie_wang 已提交
6
The real-time clock (RTC) is a real-time clock device in the operating system. In the Hardware Driver Foundation (HDF), the RTC uses the independent service mode for API adaptation. In this mode, each device independently publishes a service to process external access requests. When receiving an access request, the HDF DeviceManager extracts parameters from the request to call the internal APIs of the target device. In the independent service mode, the HDF DeviceManager provides service management capabilities. However, you need to configure a node for each device, which increases memory usage.
D
duangavin123 已提交
7

A
Annie_wang 已提交
8
  **Figure 1** Independent service mode
D
duangavin123 已提交
9

A
Annie_wang 已提交
10
  ![image](figures/independent-service-mode.png "RTC independent service mode")
D
duangavin123 已提交
11

A
Annie_wang 已提交
12 13 14 15 16

## Available APIs

**RtcMethod**:

A
Annie_wang 已提交
17
  
D
duangavin123 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
```
struct RtcMethod {
    int32_t (*ReadTime)(struct RtcHost *host, struct RtcTime *time);
    int32_t (*WriteTime)(struct RtcHost *host, const struct RtcTime *time);
    int32_t (*ReadAlarm)(struct RtcHost *host, enum RtcAlarmIndex alarmIndex, struct RtcTime *time);
    int32_t (*WriteAlarm)(struct RtcHost *host, enum RtcAlarmIndex alarmIndex, const struct RtcTime *time);
    int32_t (*RegisterAlarmCallback)(struct RtcHost *host, enum RtcAlarmIndex alarmIndex, RtcAlarmCallback cb);
    int32_t (*AlarmInterruptEnable)(struct RtcHost *host, enum RtcAlarmIndex alarmIndex, uint8_t enable);
    int32_t (*GetFreq)(struct RtcHost *host, uint32_t *freq);
    int32_t (*SetFreq)(struct RtcHost *host, uint32_t freq);
    int32_t (*Reset)(struct RtcHost *host);
    int32_t (*ReadReg)(struct RtcHost *host, uint8_t usrDefIndex, uint8_t *value);
    int32_t (*WriteReg)(struct RtcHost *host, uint8_t usrDefIndex, uint8_t value);
};
```

A
Annie_wang 已提交
34
  **Table 1** Description of the callback functions in RtcMethod
A
annie_wangli 已提交
35

A
Annie_wang 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
| Function| Input Parameter| Output Parameter| Return Value| Description| 
| -------- | -------- | -------- | -------- | -------- |
| ReadTime | **host**: structure pointer to the RTC controller at the core layer.| **time**: structure pointer to the time read.| HDF_STATUS| Reads the RTC time.| 
| WriteTime | **host**: structure pointer to the RTC controller at the core layer.<br>**time**: structure pointer to the time to write.| –| HDF_STATUS| Writes the RTC time (including the year, month, day, day of week, hour, minute, second, and millisecond).| 
| ReadAlarm | **host**: structure pointer to the RTC controller at the core layer.<br>**alarmIndex**: RTC alarm index, which is an enumerated value.| **time**: structure pointer to the time read.| HDF_STATUS| Reads the RTC alarm time.| 
| WriteAlarm | **host**: structure pointer to the RTC controller at the core layer.<br>**alarmIndex**: RTC alarm index, which is an enumerated value.<br>**time**: structure pointer to the time to write.| –| HDF_STATUS| Writes the RTC alarm time.| 
| RegisterAlarmCallback | **host**: structure pointer to the RTC controller at the core layer.<br>**alarmIndex**: RTC alarm index, which is an enumerated value.<br>**cb**: pointer to the callback to register.| –| HDF_STATUS| Registers a callback to be invoked when an alarm is not generated at the specified time.| 
| AlarmInterruptEnable | **host**: structure pointer to the RTC controller at the core layer.<br>**alarmIndex**: RTC alarm index, which is an enumerated value.<br>**enable**: whether to enable interrupts for an RTC alarm. | –| HDF_STATUS| Enables or disables interrupts for an RTC alarm.| 
| GetFreq | **host**: structure pointer to the RTC controller at the core layer.| **freq**: pointer to the frequency obtained, which is of the uint32_t type.| HDF_STATUS| Obtains the frequency of the external crystal oscillator connected to the RTC driver.| 
| SetFreq | **host**: structure pointer to the RTC controller at the core layer.<br>**freq**: frequency to set, which is of the uint32_t type.| –| HDF_STATUS| Sets the frequency of the external crystal oscillator connected to the RTC driver.| 
| Reset | **host**: structure pointer to the RTC controller at the core layer.| –| HDF_STATUS| Resets the RTC.| 
| ReadReg | **host**: structure pointer to the RTC controller at the core layer.<br>**usrDefIndex**: index of a custom register.| **value**: pointer to the register value obtained, which is of the uint8_t type.| HDF_STATUS| Reads a custom RTC register. A register index corresponds to one byte of the register value.| 
| WriteReg | **host**: structure pointer to the RTC controller at the core layer.<br>**usrDefIndex**: index of a custom register.<br>**value**: register value to write, which is of the uint8_t type.| –| HDF_STATUS| Writes a custom RTC register. A register index corresponds to one byte of the register value.| 
A
annie_wangli 已提交
49 50


A
Annie_wang 已提交
51
## How to Develop
A
annie_wangli 已提交
52

A
Annie_wang 已提交
53
The RTC module adaptation involves the following steps:
A
annie_wangli 已提交
54

A
Annie_wang 已提交
55 56 57 58 59 60 61 62 63 64 65 66
1. Instantiate the driver entry.
   - Instantiate the **HdfDriverEntry** structure.
   - Call **HDF_INIT** to register the **HdfDriverEntry** instance with the HDF.

2. Configure attribute files.
   - Add the **deviceNode** information to the **device_info.hcs** file.
   - (Optional) Add the **rtc_config.hcs** file.

3. Instantiate the RTC controller object.
   - Initialize **RtcHost**.
   - Instantiate **RtcMethod** in the **RtcHost** object.
      > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
A
Annie_wang 已提交
67
      > For details about the functions in **RtcMethod**, see [Available APIs](#available-apis).
A
Annie_wang 已提交
68 69 70 71 72 73 74

4. Debug the driver.
   (Optional) For new drivers, verify the basic functions, such as the RTC status control and response to interrupts.


## Development Example

A
Annie_wang 已提交
75
The following uses **rtc_hi35xx.c** as an example to present the information required for implementing device functions.
A
Annie_wang 已提交
76 77 78 79 80

1. Instantiate the driver entry.<br/>The driver entry must be a global variable of the **HdfDriverEntry** type (defined in **hdf_device_desc.h**), and the value of **moduleName** must be the same as that in **device_info.hcs**. In the HDF framework, the start address of each **HdfDriverEntry** object of all loaded drivers is collected to form a segment address space similar to an array for the upper layer to invoke.
   Generally, the HDF calls the **Bind** function and then the **Init** function to load a driver. If **Init** fails to be called, the HDF calls **Release** to release driver resources and exit.

     RTC driver entry example:
A
Annie_wang 已提交
81
     
A
Annie_wang 已提交
82 83 84 85 86 87 88 89 90 91 92 93
   ```
   struct HdfDriverEntry g_rtcDriverEntry = {
     .moduleVersion = 1,
     .Bind = HiRtcBind,                // See the Bind function.
     .Init = HiRtcInit,              // See the Init function.
     .Release = HiRtcRelease,         //See the Release function.
     .moduleName = "HDF_PLATFORM_RTC", // (Mandatory) The value must be the same as that in the .hcs file.
   };
   // Call HDF_INIT to register the driver entry with the HDF.
   HDF_INIT(g_rtcDriverEntry);
   ```

A
Annie_wang 已提交
94 95 96 97 98
2. Add the **deviceNode** information to the **device_info.hcs** file and configure the device attributes in the **rtc_config.hcs** file. 
     
   The **deviceNode** information is related to registration of the driver entry. The device attribute values are closely related to the default values or value ranges of the **RtcHost** members at the core layer.
   In this example, there is only one RTC controller. If there are multiple RTC controllers, you need to add the **deviceNode** information to the **device_info** file and add the corresponding device attributes to the **rtc_config** file for each controller.
   
A
Annie_wang 已提交
99 100
   - **device_info.hcs** configuration example
   
A
Annie_wang 已提交
101
       
A
Annie_wang 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
     ```
     root {
       device_info {
         platform :: host {
           device_rtc :: device {
             device0 :: deviceNode {
               policy = 1;                              // The driver publishes services for kernel-mode processes only.
               priority = 30;                           // A smaller value indicates a higher priority.
               permission = 0644;                  // Permission for the driver to create a device node
               moduleName = "HDF_PLATFORM_RTC";         // (Mandatory) Driver name, which must be the same as moduleName in the driver entry.
               serviceName = "HDF_PLATFORM_RTC";         // (Mandatory) Unique name of the service published by the driver.
               deviceMatchAttr = "hisilicon_hi35xx_rtc";// The value must be the same as that of match_attr in the .hcs file.
             }
           }
         }
       }
     }
     ```
   
   - **rtc_config.hcs** configuration example
   
A
Annie_wang 已提交
123
       
A
Annie_wang 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
     ```
     root {
       platform {
         rtc_config {
           controller_0x12080000 {
             match_attr = "hisilicon_hi35xx_rtc";// (Mandatory) The value must be the same as that of deviceMatchAttr in device_info.hcs.
             rtcSpiBaseAddr = 0x12080000;        // Used for address mapping.
             regAddrLength = 0x100;              // Used for address mapping.
             irq = 37;                           // Interrupt number.
             supportAnaCtrl = false;
             supportLock = false;
             anaCtrlAddr = 0xff;
             lock0Addr = 0xff;
             lock1Addr = 0xff;
             lock2Addr = 0xff;
             lock3Addr = 0xff;
           }
         }
       }
A
Annie_wang 已提交
143 144 145
     }
     ```

A
Annie_wang 已提交
146 147 148 149
3. Initialize the **RtcHost** object at the core layer, including defining a custom structure (to pass parameters and data) and implementing the **HdfDriverEntry** member functions (**Bind**, **Init** and **Release**) to instantiate **RtcMethod** in **RtcHost** (so that the underlying driver functions can be called).
   - Defining a custom structure

      To the driver, the custom structure holds parameters and data. The **DeviceResourceIface** method provided by the HDF reads the values in the **rtc_config.hcs** file to initialize the members in the custom structure.
A
annie_wangli 已提交
150

A
Annie_wang 已提交
151
        
A
Annie_wang 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
      ```
      struct RtcConfigInfo {
        uint32_t           spiBaseAddr; // Used for address mapping.
        volatile void      *remapBaseAddr;  // Used for address mapping.
        uint16_t           regAddrLength; // Used for address mapping.
        uint8_t            supportAnaCtrl; // Whether ANACTRL is supported.
        uint8_t            supportLock;    // Whether lock is supported.
        uint8_t            irq;            // Interrupt number.
        uint8_t            alarmIndex;    // RTC alarm index.
        uint8_t            anaCtrlAddr; // ANACTRL address.
        struct RtcLockAddr lockAddr;      // Lock address.
        RtcAlarmCallback   cb;             // Callback.
        struct OsalMutex   mutex;          // Mutex.
      };
      
      // RtcHost is the controller structure at the core layer. The Init function assigns values to the members of RtcHost.
      struct RtcHost {
          struct IDeviceIoService service;
          struct HdfDeviceObject *device;
          struct RtcMethod *method;
          void *data;
      };
      ```
   - Instantiating **RtcMethod** in **RtcHost** (other members are initialized by **Init**)
A
annie_wangli 已提交
176

A
Annie_wang 已提交
177
        
A
Annie_wang 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
      ```
      // Example in rtc_hi35xx.c: instantiate the hook.
      static struct RtcMethod g_method = {
          .ReadTime   = HiRtcReadTime,
          .WriteTime  = HiRtcWriteTime,
          .ReadAlarm  = HiReadAlarm,
          .WriteAlarm = HiWriteAlarm,
          .RegisterAlarmCallback  = HiRegisterAlarmCallback,
          .AlarmInterruptEnable   = HiAlarmInterruptEnable,
          .GetFreq  = HiGetFreq,
          .SetFreq  = HiSetFreq,
          .Reset       = HiReset,
          .ReadReg  = HiReadReg,
          .WriteReg = HiWriteReg,
      };
      ```

A
Annie_wang 已提交
195
   - **Bind** function
A
Annie_wang 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

      Input parameter:

      **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.

      Return value:

      HDF_STATUS<br/>The table below describes some status. For more information, see **HDF_STATUS** in the **/drivers/framework/include/utils/hdf_base.h** file.

        **Table 2** HDF_STATUS
      
      | Status| Description| 
      | -------- | -------- |
      | HDF_ERR_INVALID_OBJECT | Invalid controller object.| 
      | HDF_ERR_MALLOC_FAIL | Failed to allocate memory.| 
      | HDF_ERR_INVALID_PARAM | Invalid parameter.| 
      | HDF_ERR_IO | I/O error.| 
      | HDF_SUCCESS | Initialization successful.| 
      | HDF_FAILURE | Initialization failed.| 

      Function description:

      Binds the **HdfDeviceObject** object and **RtcHost**.
D
duangavin123 已提交
219

A
Annie_wang 已提交
220
        
A
Annie_wang 已提交
221 222 223 224 225 226 227 228
      ```
      static int32_t HiRtcBind(struct HdfDeviceObject *device)
      {
        struct RtcHost *host = NULL;  
        host = RtcHostCreate(device); // Allocate memory and attach the device host and the device.
                                         // Prerequisite for conversion between HdfDeviceObject and RtcHost.
        ...
        device->service = &host->service; // Prerequisite for conversion between HdfDeviceObject and RtcHost.
A
Annie_wang 已提交
229
                                         // It helps implement the global host by calling RtcHostFromDevice.
A
Annie_wang 已提交
230 231 232
        return HDF_SUCCESS;
      }
      ```
D
duangavin123 已提交
233

A
Annie_wang 已提交
234
   - **Init** function
D
duangavin123 已提交
235

A
Annie_wang 已提交
236
      Input parameter:
D
duangavin123 已提交
237

A
Annie_wang 已提交
238
      **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
D
duangavin123 已提交
239

A
Annie_wang 已提交
240
      Return value:
D
duangavin123 已提交
241

A
Annie_wang 已提交
242
      HDF_STATUS
D
duangavin123 已提交
243

A
Annie_wang 已提交
244
      Function description:
D
duangavin123 已提交
245

A
Annie_wang 已提交
246
      Initializes the custom structure object and **RtcHost**.
D
duangavin123 已提交
247

A
Annie_wang 已提交
248
        
A
Annie_wang 已提交
249 250 251 252 253 254
      ```
      static int32_t HiRtcInit(struct HdfDeviceObject *device)
      {
        struct RtcHost *host = NULL;
        struct RtcConfigInfo *rtcInfo = NULL;
        ...
A
Annie_wang 已提交
255
        host = RtcHostFromDevice(device);// Forcibly convert HdfDeviceObject to RtcHost.
A
Annie_wang 已提交
256 257 258
        rtcInfo = OsalMemCalloc(sizeof(*rtcInfo));
        ...
        // HiRtcConfigData reads attributes from the device configuration tree and fills the values in supportAnaCtrl, supportLock, spiBaseAddr, regAddrLength, and irq in rtcInfo.
A
Annie_wang 已提交
259
        // Provide parameters for HiRtcSwInit and HiRtcSwInit. When HiRtcSwInit and HiRtcSwInit fail to be executed internally, Release() can be called to release memory.
A
Annie_wang 已提交
260 261
        if (HiRtcConfigData(rtcInfo, device->property) != 0) {
          ...
D
duangavin123 已提交
262
        }
A
Annie_wang 已提交
263 264
        if (HiRtcSwInit(rtcInfo)! = 0) {// Address mapping and interrupt registration.
          ...
D
duangavin123 已提交
265
        }
A
Annie_wang 已提交
266
        if (HiRtcHwInit(rtcInfo)! = 0) {// Initialize ANACTRL and lockAddr.
D
duangavin123 已提交
267 268
          ...
        }
A
Annie_wang 已提交
269 270 271 272 273 274 275 276
      
        host->method = &g_method; // Attach the RtcMethod instance.
        host->data = rtcInfo; // Prerequisites for conversion between RtcConfigInfo and RtcHost.
        HDF_LOGI("Hdf dev service:%s init success!", HdfDeviceGetServiceName(device));
        return HDF_SUCCESS;
      }
      ```
   - **Release** function
D
duangavin123 已提交
277

A
Annie_wang 已提交
278
      Input parameter:
D
duangavin123 已提交
279

A
Annie_wang 已提交
280
      **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
D
duangavin123 已提交
281

A
Annie_wang 已提交
282
      Return value:
D
duangavin123 已提交
283

A
Annie_wang 已提交
284
      No value is returned.
D
duangavin123 已提交
285

A
Annie_wang 已提交
286
      Function description:
D
duangavin123 已提交
287

A
Annie_wang 已提交
288
      Releases the memory and deletes the controller. This function assigns values to the **Release** function in the driver entry structure. If the HDF fails to call the **Init** function to initialize the driver, the **Release** function can be called to release driver resources. All forced conversion operations for obtaining the corresponding object can be successful only when the **Init** or **Bind** function has the corresponding value assignment operations.
D
duangavin123 已提交
289

A
Annie_wang 已提交
290
        
A
Annie_wang 已提交
291 292 293
      ```
      static void HiRtcRelease(struct HdfDeviceObject *device)
      {
D
duangavin123 已提交
294 295 296
          struct RtcHost *host = NULL;
          struct RtcConfigInfo *rtcInfo = NULL;
          ...
A
Annie_wang 已提交
297 298
          host = RtcHostFromDevice(device);            // Forcibly convert HdfDeviceObject to RtcHost.
          rtcInfo = (struct RtcConfigInfo *)host->data;// Forcibly convert RtcHost to RtcConfigInfo.
A
Annie_wang 已提交
299 300 301 302
          if (rtcInfo != NULL) {
              HiRtcSwExit(rtcInfo);
              OsalMemFree(rtcInfo);                    // Release RtcConfigInfo.
              host->data = NULL;
D
duangavin123 已提交
303
          }
A
Annie_wang 已提交
304 305 306
          RtcHostDestroy(host);                        // Release RtcHost.
      }
      ```