1e41f4b71Sopenharmony_ci# CLOCK<a name="1"></a> 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## 概述<a name="section1"></a> 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci### 功能简介<a name="section2"></a> 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciCLOCK,时钟是系统各个部件运行的基础,以CPU时钟举例,CPU 时钟是指 CPU 内部的时钟发生器,它以频率的形式工作,用来同步和控制 CPU 内部的各个操作。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciCLOCK接口定义了完成CLOCK操作的通用方法集合,包括: 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci- CLOCK设备管理:打开或关闭时钟设备。 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci- CLOCK速率控制:读取或设置时钟速率。 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci- CLOCK门控:使能或去使能时钟。 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci- CLOCK父时钟管理:获取或者设置父时钟。 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci### 基本概念<a name="section3"></a> 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci设备的时钟信号是指在电子设备中用于同步和控制各个模块或组件操作的信号。它是设备内部的一个基本信号源,用于确保设备的正常运行和数据传输的准确性。 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci### 运作机制<a name="section4"></a> 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci在HDF框架中,同类型设备对象较多时(可能同时存在十几个同类型配置器),如果采用独立服务模式则需要配置更多的设备节点,且相关服务会占据更多的内存资源。相反,采用统一服务模式可以使用一个设备服务作为管理器,统一处理所有同类型对象的外部访问(这会在配置文件中有所体现),实现便捷管理和节约资源的目的。CLOCK模块接口适配模式采用统一服务模式。 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci## 使用指导<a name="section5"></a> 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci### 场景介绍<a name="section6"></a> 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciCLOCK提供芯片级别的时钟管理:时钟功能可用于控制芯片内部的时钟分频、时钟倍频、时钟源选择和时钟门控等操作。通过合理的时钟管理,可以提高芯片的能效,并确保各个功能部件的正确协调和协同工作。 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci### 接口说明<a name="section7"></a> 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ciCLOCK模块提供的主要接口如表1所示,具体API详见//drivers/hdf_core/framework/include/platform/clock_if.h。 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**表 1** CLOCK驱动API接口功能介绍 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci<a name="table1"></a> 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci| **接口名** | **接口描述** | 返回值描述 | 特殊情况说明 | 42e41f4b71Sopenharmony_ci| ---------------------------------------------------------- | ------------- | ----------------------------------- | --------------------------------------- | 43e41f4b71Sopenharmony_ci| DevHandle ClockOpen(uint32_t number); | 打开Clock设备 | NULL:获取失败,设备句柄:获取成功 | | 44e41f4b71Sopenharmony_ci| int32_t ClockClose(DevHandle handle); | 关闭Clock设备 | 零:关闭成功,非零:关闭失败 | | 45e41f4b71Sopenharmony_ci| int32_t ClockEnable(DevHandle handle); | 使能Clock | 零:使能成功,非零:使能失败 | | 46e41f4b71Sopenharmony_ci| int32_t ClockDisable(DevHandle handle); | 去使能Clock | 零:去使能成功,非零:去使能失败 | | 47e41f4b71Sopenharmony_ci| int32_t ClockSetRate(DevHandle handle, uint32_t rate); | 设置时钟速率 | 零:设置时钟速率成功,非零:设置失败 | 失败请检查传入的rate对应的Clock是否支持 | 48e41f4b71Sopenharmony_ci| int32_t ClockGetRate(DevHandle handle, uint32_t *rate); | 获取时钟速率 | 零:获取成功,非零:获取失败 | | 49e41f4b71Sopenharmony_ci| int32_t ClockSetParent(DevHandle child, DevHandle parent); | 设置父时钟 | 零:设置父成功,非零:设置父失败 | 父时钟重复设置时不报错,直接返回成功 | 50e41f4b71Sopenharmony_ci| DevHandle ClockGetParent(DevHandle handle); | 获取父时钟 | 零:获取父成功,非零:获取父失败 | | 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci### 开发步骤<a name="section8"></a> 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci使用CLOCK设备的一般流程如图2所示。 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci**图 2** CLOCK使用流程图<a name="fig2"></a> 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci### 使用实例<a name="section9"></a> 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci本例拟对RK3568开发板上CLOCK设备进行简单的读取操作,基本硬件信息如下: 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci- SOC:RK3568。 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci本例程对各个接口进行调用测试 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci示例如下: 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci```c 70e41f4b71Sopenharmony_ci#include "clock_if.h" // CLOCK标准接口头文件 71e41f4b71Sopenharmony_ci#include "hdf_log.h" // 标准日志打印头文件 72e41f4b71Sopenharmony_ci#define CLOCK_NUM 1 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_cistatic int32_t TestCaseClock(void) 75e41f4b71Sopenharmony_ci{ 76e41f4b71Sopenharmony_ci int ret = 0; 77e41f4b71Sopenharmony_ci DevHandle handle = NULL; 78e41f4b71Sopenharmony_ci DevHandle parent = NULL; 79e41f4b71Sopenharmony_ci uint32_t rate = 0; 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci handle = ClockOpen(CLOCK_NUM); 82e41f4b71Sopenharmony_ci if (handle == NULL) { 83e41f4b71Sopenharmony_ci HDF_LOGE("Failed to open CLOCK_NUM %d \n", CLOCK_NUM); 84e41f4b71Sopenharmony_ci return HDF_FAILURE; 85e41f4b71Sopenharmony_ci } 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci ret = ClockEnable(handle); 88e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 89e41f4b71Sopenharmony_ci HDF_LOGE("Failed to ClockEnable ret = %d \n",ret); 90e41f4b71Sopenharmony_ci return ret; 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci ret = ClockGetRate(handle, &rate); 95e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 96e41f4b71Sopenharmony_ci HDF_LOGE("Failed to ClockGetRate ret = %d \n",ret); 97e41f4b71Sopenharmony_ci return ret; 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci ret = ClockSetRate(handle, set_rate); 101e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 102e41f4b71Sopenharmony_ci HDF_LOGE("Failed to ClockSetRate ret = %d \n",ret); 103e41f4b71Sopenharmony_ci return ret; 104e41f4b71Sopenharmony_ci } 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci ret = ClockDisable(handle); 107e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 108e41f4b71Sopenharmony_ci HDF_LOGE("Failed to ClockDisable ret = %d \n",ret); 109e41f4b71Sopenharmony_ci return ret; 110e41f4b71Sopenharmony_ci } 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci parent = ClockGetParent(handle); 113e41f4b71Sopenharmony_ci if (parent != NULL) { 114e41f4b71Sopenharmony_ci ret = ClockSetParent(handle, parent); 115e41f4b71Sopenharmony_ci ClockClose(parent); 116e41f4b71Sopenharmony_ci } else { 117e41f4b71Sopenharmony_ci HDF_LOGE("Failed to ClockGetParent ret = %d \n",ret); 118e41f4b71Sopenharmony_ci } 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci ret = ClockClose(handle); 121e41f4b71Sopenharmony_ci return ret; 122e41f4b71Sopenharmony_ci} 123e41f4b71Sopenharmony_ci```