# RTC - [Overview](#section509989381142407) - [How to Develop](#section1784450860142407) - [RtcMethod](#section13652132473017) - [Development Example](#section1594883301142407) ## Overview In the Hardware Driver Foundation \(HDF\) framework, the real-time clock \(RTC\) uses the independent service mode for API adaptation. In this mode, each device independently publishes a device service to handle external access requests. After receiving an access request from an API, the device manager extracts the parameters in the request to call the internal method of the target device. In the independent service mode, the service management capabilities of the HDFDeviceManager can be directly used. However, you need to configure a device node for each device, which increases the memory usage. **Figure 1** Independent service mode ![](figures/independent-service-mode.png "independent-service-mode-11") ## Available APIs RtcMethod ``` 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); }; ``` **Table 1** Callbacks for the members in the RtcMethod structure

Callback

Input Parameter

Output Parameter

Return Value

Description

ReadTime

host: structure pointer to the RTC controller at the core layer.

time: structure pointer to the output time value.

HDF_STATUS

Reads the RTC time information.

WriteTime

host: structure pointer to the RTC controller at the core layer.

time: structure pointer to the input time.

HDF_STATUS

Writes the RTC time information (from milliseconds to years).

ReadAlarm

host: structure pointer to the RTC controller at the core layer.

alarmIndex: clock alarm index, which is an enumerated value.

time: structure pointer to the output time.

HDF_STATUS

Reads the RTC alarm time.

WriteAlarm

host: structure pointer to the RTC controller at the core layer.

alarmIndex: clock alarm index, which is an enumerated value.

time: structure pointer to the input time.

HDF_STATUS

Writes the RTC alarm time.

RegisterAlarmCallback

host: structure pointer to the RTC controller at the core layer.

alarmIndex: clock alarm index, which is an enumerated value.

cb: pointer to the callback.

HDF_STATUS

Registers RtcAlarmCallback that will be invoked when an alarm is generated at the specified time.

AlarmInterruptEnable

host: structure pointer to the RTC controller at the core layer.

alarmIndex: clock alarm index, which is an enumerated value.

enable: specifies whether to enable the RTC alarm interrupt.

HDF_STATUS

Enables or disables the RTC alarm interrupt.

GetFreq

host: structure pointer to the RTC controller at the core layer.

freq: pointer to the output frequency, which is of the uint32_t type.

HDF_STATUS

Reads the frequency of the external crystal oscillator connected to the RTC driver.

SetFreq

host: structure pointer to the RTC controller at the core layer.

freq: input frequency, 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.

usrDefIndex: structure defining the index of a custom register.

value: pointer to the output register value, which is of the uint8_t type.

HDF_STATUS

Reads the configuration of a custom RTC register based on the register index. One index corresponds to one byte of the configuration value.

WriteReg

host: structure pointer to the RTC controller at the core layer.

usrDefIndex: structure defining the index of a custom register.

value: input register value, which is of the uint8_t type.

HDF_STATUS

Configures the RTC register based on the register index. One index corresponds to one byte of the configuration value.

## How to Develop The RTC module adaptation involves the following steps: 1. Instantiate the driver entry. - Instantiate the **HdfDriverEntry** structure. - Call **HDF\_INIT** to register the **HdfDriverEntry** instance with the HDF framework. 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. >![](../public_sys-resources/icon-note.gif) **NOTE:** >For details, see [RtcMethod](#section13652132473017) and [Table 1](#table12929217311). 4. Debug the driver. - \(Optional\) For new drivers, verify the basic functions, such as the RTC control status and response to interrupts. ## Development Example The following uses **rtc\_hi35xx.c** as an example to present the contents that need to be provided by the vendor to implement device functions. 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 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, HDF calls the **Bind** function and then the **Init** function to load a driver. If **Init** fails to be called, HDF calls **Release** to release driver resources and exit. - RTC driver entry reference ``` 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) This parameter must be the same as that in the .hcs file. }; // Call HDF_INIT to register the driver entry with the HDF framework. HDF_INIT(g_rtcDriverEntry); ``` 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. - **device\_info.hcs** configuration reference ``` root { device_info { platform :: host { device_rtc :: device { device0 :: deviceNode { policy = 1; // 2: The driver publishes user-mode services. 1: The driver publishes kernel-mode services. 0: No service needs to be published. 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 the 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 reference ``` 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; // Interruption number supportAnaCtrl = false; supportLock = false; anaCtrlAddr = 0xff; lock0Addr = 0xff; lock1Addr = 0xff; lock2Addr = 0xff; lock3Addr = 0xff; } } } } ``` 3. Initialize the **RtcHost** object at the core layer, including initializing the vendor custom structure \(transferring parameters and data\), instantiating **RtcMethod** \(used to call underlying functions of the driver\) in **RtcHost**, and implementing the **HdfDriverEntry** member functions \(**Bind**, **Init**, and **Release**\). - Custom structure reference To the driver, the custom structure carries parameters and data. The values in the **rtc\_config.hcs** file are read by HDF, and the structure members are initialized through **DeviceResourceIface**. ``` 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; // Indicates whether ANACTRL is supported. uint8_t supportLock; // Indicates whether lock is supported. uint8_t irq; // Interrupt number uint8_t alarmIndex; // Clock 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. Its members are assigned with values by using the Init function. struct RtcHost { struct IDeviceIoService service; struct HdfDeviceObject *device; struct RtcMethod *method; void *data; }; ``` - Instantiate the callback function structure **RtcMethod** in **RtcHost**. Other members are initialized by using the **Init** function. ``` // 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, }; ``` - Bind function Input parameters: **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information. Return values: HDF\_STATUS \(The following table lists some status. For details about other status, see **HDF\_STATUS** in the **//drivers/framework/include/utils/hdf\_base.h** file.\) **Table 2** Input parameters and return values of the Bind function

Status (Value)

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: Associates the **HdfDeviceObject** object and **RtcHost**. ``` static int32_t HiRtcBind(struct HdfDeviceObject *device) { struct RtcHost *host = NULL; host = RtcHostCreate(device); // Apply for memory and connect to the device: host->device = device; // Enable conversion between HdfDeviceObject and RtcHost. ... device->service = &host->service; // Enable conversion between HdfDeviceObject and RtcHost. // This setting enables the host to be globally used by calling RtcHostFromDevice. return HDF_SUCCESS; } ``` - Init function Input parameters: **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information. Return values: 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);// A forced conversion from HdfDeviceObject to RtcHost is involved. rtcInfo = OsalMemCalloc(sizeof(*rtcInfo)); ... // HiRtcConfigData reads attributes from the device configuration tree and fills in supportAnaCtrl, supportLock, spiBaseAddr, regAddrLength, and irq in rtcInfo. // Provide parameters for HiRtcSwInit and HiRtcSwInit, and perform operations such as releasing memory when the function internal processing fails. if (HiRtcConfigData(rtcInfo, device->property) != 0) { ... } if (HiRtcSwInit(rtcInfo)! = 0) {// Related to address mapping and interrupt registration ... } if (HiRtcHwInit(rtcInfo)! = 0) {// Initialize ANACTRL and lockAddr. ... } host->method = &g_method; // Connect to the UARTHostMethod instance. host->data = rtcInfo; // Enable conversion between RtcConfigInfo and RtcHost. HDF_LOGI("Hdf dev service:%s init success!", HdfDeviceGetServiceName(device)); return HDF_SUCCESS; } ``` - Release function Input parameters: **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information. Return values: – **Function description**: Releases the memory and deletes the controller. This function assigns a value to the **Release** API in the driver entry structure. When the HDF framework 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. ``` static void HiRtcRelease(struct HdfDeviceObject *device) { struct RtcHost *host = NULL; struct RtcConfigInfo *rtcInfo = NULL; ... host = RtcHostFromDevice(device); // A forced conversion from HdfDeviceObject to RtcHost is involved. rtcInfo = (struct RtcConfigInfo *)host->data;// A forced conversion from RtcHost to RtcConfigInfo is involved. if (rtcInfo != NULL) { HiRtcSwExit(rtcInfo); OsalMemFree(rtcInfo); // Release RtcConfigInfo. host->data = NULL; } RtcHostDestroy(host); // Release RtcHost. } ```