driver-platform-rtc-develop.md 15.9 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
| Function| Input Parameter| Output Parameter| Return Value| Description|
A
Annie_wang 已提交
37
| -------- | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
38 39 40 41 42 43 44 45 46 47 48
| 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

4. Debug the driver.
A
Annie_wang 已提交
70

A
Annie_wang 已提交
71 72 73 74 75
   (Optional) For new drivers, verify the basic functions, such as the RTC status control and response to interrupts.


## Development Example

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

A
Annie_wang 已提交
78 79 80 81 82 83
1. Instantiate the driver entry.

   The driver entry must be a global variable of the **HdfDriverEntry** type (defined in **hdf_device_desc.h**), and the value of **moduleName** must be the same as that in **device_info.hcs**.

   In the HDF, the start address of each **HdfDriverEntry** object of all loaded drivers is collected to form a segment address space similar to an array for the upper layer to invoke.

A
Annie_wang 已提交
84 85
   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.

A
Annie_wang 已提交
86 87
   RTC driver entry example:
   
A
Annie_wang 已提交
88 89 90 91
   ```
   struct HdfDriverEntry g_rtcDriverEntry = {
     .moduleVersion = 1,
     .Bind = HiRtcBind,                // See the Bind function.
A
Annie_wang 已提交
92 93
     .Init = HiRtcInit,                // See the Init function.
     .Release = HiRtcRelease,          //See the Release function.
A
Annie_wang 已提交
94 95 96 97 98 99
     .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 已提交
100 101
2. Add the **deviceNode** information to the **device_info.hcs** file and configure the device attributes in the **rtc_config.hcs** file.

A
Annie_wang 已提交
102
   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.
A
Annie_wang 已提交
103

A
Annie_wang 已提交
104
   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 已提交
105

A
Annie_wang 已提交
106 107
   - **device_info.hcs** configuration example
   
A
Annie_wang 已提交
108
     
A
Annie_wang 已提交
109 110 111 112 113 114 115 116
     ```
     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.
A
Annie_wang 已提交
117
               permission = 0644;                       // Permission for the driver to create a device node
A
Annie_wang 已提交
118
               moduleName = "HDF_PLATFORM_RTC";         // (Mandatory) Driver name, which must be the same as moduleName in the driver entry.
A
Annie_wang 已提交
119
               serviceName = "HDF_PLATFORM_RTC";        // (Mandatory) Unique name of the service published by the driver.
A
Annie_wang 已提交
120 121 122 123 124 125 126 127 128 129
               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 已提交
130
     
A
Annie_wang 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
     ```
     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 已提交
150 151 152
     }
     ```

A
Annie_wang 已提交
153 154
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).

A
Annie_wang 已提交
155 156 157
   - 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 已提交
158

A
Annie_wang 已提交
159 160
      ```
      struct RtcConfigInfo {
A
Annie_wang 已提交
161
        uint32_t           spiBaseAddr;     // Used for address mapping.
A
Annie_wang 已提交
162
        volatile void      *remapBaseAddr;  // Used for address mapping.
A
Annie_wang 已提交
163 164 165 166 167 168 169 170 171
        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.
A
Annie_wang 已提交
172 173 174 175 176 177 178 179 180 181
      };
      
      // 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;
      };
      ```
A
Annie_wang 已提交
182

A
Annie_wang 已提交
183
   - Instantiating **RtcMethod** in **RtcHost** (other members are initialized by **Init**)
A
annie_wangli 已提交
184

A
Annie_wang 已提交
185
      ```
A
Annie_wang 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199
       // 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 已提交
200 201
      ```

A
Annie_wang 已提交
202
   - **Bind** function
A
Annie_wang 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
   
     **Input parameter**:
     
     **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
     
     **Return value**:
     
     **HDF_STATUS**
     
     The table below describes some status. For more information, see **HDF_STATUS** in the **/drivers/framework/include/utils/hdf_base.h** file.
     
      **Table 2** Description of 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**:
     
A
Annie_wang 已提交
227
      Binds the **HdfDeviceObject** object and **RtcHost**.
A
Annie_wang 已提交
228 229 230 231 232 233 234
     
     ```
     static int32_t HiRtcBind(struct HdfDeviceObject *device)
        {
          struct RtcHost *host = NULL;  
          host = RtcHostCreate(device);    // Allocate memory and attach the device host with the device.
                                           // Prerequisite for conversion between HdfDeviceObject and RtcHost.
D
duangavin123 已提交
235
          ...
A
Annie_wang 已提交
236 237 238
          device->service = &host->service; // Prerequisite for conversion between HdfDeviceObject and RtcHost.
                                           // It helps implement a global host by calling RtcHostFromDevice.
       return HDF_SUCCESS;
D
duangavin123 已提交
239
        }
A
Annie_wang 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
     ```
     
   - **Init** function
     
     **Input parameter**:
     
     **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
     
     **Return value**:
     
     **HDF_STATUS**
     
      **Function description**:
     
     Initializes the custom structure object and **RtcHost**.
     
     ```
     static int32_t HiRtcInit(struct HdfDeviceObject *device)
         {
           struct RtcHost *host = NULL;
           struct RtcConfigInfo *rtcInfo = NULL;
           ...
           host = RtcHostFromDevice(device);// Forcibly convert HdfDeviceObject to RtcHost.
           rtcInfo = OsalMemCalloc(sizeof(*rtcInfo));
           ...
           // HiRtcConfigData reads attributes from the device configuration tree and fills in supportAnaCtrl, supportLock, spiBaseAddr, regAddrLength and irq of RTCInfo.
           // Provide parameters for HiRtcSwInit and HiRtcSwInit. When HiRtcSwInit and HiRtcSwInit fail to be executed internally, Release() can be called to release memory.
           if (HiRtcConfigData(rtcInfo, device->property) != 0) {
             ...
           }
           if (HiRtcSwInit(rtcInfo)! = 0) {// Address mapping and interrupt registration.
             ...
           }
           if (HiRtcHwInit(rtcInfo)! = 0) {// Initialize ANACTRL and lockAddr.
             ...
           }
         
           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;
		 }
     ```
     
A
Annie_wang 已提交
284
   - **Release** function
A
Annie_wang 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
     
     **Input parameter**:
     
     **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
     
     **Return value**:
     
     No value is returned.
     
     **Function description**:
     
     Releases the memory and deletes the controller. This function assigns values to the **Release** function in the driver entry structure. If the HDF fails to call the **Init** function to initialize the driver, the **Release** function can be called to release driver resources. 
	
	
     > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
	 > 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.
   
   
     ```
     static void HiRtcRelease(struct HdfDeviceObject *device)
         {
D
duangavin123 已提交
306 307 308
          struct RtcHost *host = NULL;
          struct RtcConfigInfo *rtcInfo = NULL;
          ...
A
Annie_wang 已提交
309 310
          host = RtcHostFromDevice(device);            // Forcibly convert HdfDeviceObject to RtcHost.
          rtcInfo = (struct RtcConfigInfo *)host->data;// Forcibly convert RtcHost to RtcConfigInfo.
A
Annie_wang 已提交
311 312 313 314
          if (rtcInfo != NULL) {
              HiRtcSwExit(rtcInfo);
              OsalMemFree(rtcInfo);                    // Release RtcConfigInfo.
              host->data = NULL;
D
duangavin123 已提交
315
          }
A
Annie_wang 已提交
316
          RtcHostDestroy(host);                        // Release RtcHost.
A
Annie_wang 已提交
317 318
         }
     ```