1# Niobe407开发板OpenHarmony基于HDF驱动框架编程开发——I2C 2 3I2C是一种串行,同步,半双工通信协议,允许在同一总线上同时存在多个主机和从机。I2C总线由两条线组成:串行数据线(SDA)和串行时钟(SCL)。两条线都需要上拉电阻。I2C组件为设备提供读写等操作。 4 5本示例将演示如何在Niobe407开发板上通过HDF驱动框架,使用I2C3接口对RJGT102芯片进行读写芯片UUID操作。 6 7## 接口说明 8 9``` 101. I2C open打开I2C函数:DevHandle I2cOpen(int16_t number); 11 参数说明: 12 number: I2C控制器ID 13 return: 返回NULL,表示初始化成功 14 152. I2C close打开I2C函数:void I2cClose(DevHandle handle); 16 173. I2C transfer启动到I2C设备的自定义传输函数:int32_t I2cTransfer(DevHandle handle, struct I2cMsg *msgs, int16_t count); 18 参数说明: 19 handle: 指向通过{@link I2cOpen} 获得的I2C 控制器的设备句柄的指针 20 msgs: 指示指向 I2C 传输消息结构体数组的指针 21 count: 表示消息结构数组的长度 22 return: 返回传输的消息结构数,表示成功,返回负数,表示失败 23``` 24 25## 编译调试 26- 进入//kernel/liteos_m目录, 在menuconfig配置中进入如下选项: 27 28 `(Top) → Platform → Board Selection → select board niobe407 → use talkweb niobe407 application → niobe407 application choose` 29 30- 选择 `204_hdf_i2c` 31 32- 在menuconfig的`(Top) → Driver`选项中使能如下配置: 33 34``` 35 [*] Enable Driver 36 [*] HDF driver framework support 37 [*] Enable HDF platform driver 38 [*] Enable HDF platform uart driver 39``` 40- 回到sdk根目录,执行`hb build -f`脚本进行编译。 41 42### 运行结果 43 44示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志 45``` 46Entering scheduler 47[HDF:E/HDF_LOG_TAG]DeviceManagerStart in 48[HDF:I/devmgr_service]start svcmgr result 0 49[HDF:I/hcs_blob_if]CheckHcsBlobLength: the blobLength: 1248, byteAlign: 1 50[HDF:D/device_node]node HDF_PLATFORM_I2C_MANAGER property empty, match attr: 51[HDF:I/device_node]launch devnode HDF_PLATFORM_I2C_MANAGER 52[HDF:I/i2c_core]I2cManagerBind: Enter! 53[HDF:I/device_node]launch devnode HDF_PLATFORM_I2C_3 54[HDF:I/i2c_core]I2cCntlrAdd: use default lock methods! 55OHOS # hiview init success. 56 57<--------------- OHOS Application Start Here ---------------> 58[HDF:E/HDF_LOG_TAG]into HdfI2cTestEntry! 59 60write uuid success: [11, 22, 33, 44, aa, bb, cc, dd] 61read uuid success: [11, 22, 33, 44, aa, bb, cc, dd] 62``` 63## I2C HDF HCS配置文件解析 64- device_i2c_info.hcs文件位于/device/board/talkweb/niobe407/sdk/hdf_config/device_i2c_info.hcs,本例子使用的是i2c3 65``` 66 67root { 68 module = "talkweb,stm32f407"; 69 device_info { 70 match_attr = "hdf_manager"; 71 template host { 72 hostName = ""; 73 priority = 100; 74 template device { 75 template deviceNode { 76 policy = 0; 77 priority = 100; 78 preload = 0; 79 permission = 0664; 80 moduleName = ""; 81 serviceName = ""; 82 deviceMatchAttr = ""; 83 } 84 } 85 } 86 platform :: host { 87 hostName = "platform_host"; 88 priority = 0; 89 90 device_i2c :: device { 91 i2c_manager :: deviceNode { 92 policy = 2; 93 priority = 50; 94 moduleName = "HDF_PLATFORM_I2C_MANAGER"; 95 serviceName = "HDF_PLATFORM_I2C_MANAGER"; 96 } 97 98 i2c3 :: deviceNode { 99 policy = 0; 100 priority = 100; 101 preload = 0; 102 permission = 0664; 103 moduleName = "HDF_I2C"; 104 serviceName = "HDF_PLATFORM_I2C_3"; 105 deviceMatchAttr = "i2c3_config"; 106 } 107 } 108 } 109 } 110} 111``` 112 113- hdf_i2c.hcs文件位于/device/board/talkweb/niobe407/sdk/hdf_config/hdf_i2c.hcs,在此文件中配置串口对应的GPIO引脚信息,I2C配置信息 114``` 115#include "device_i2c_info.hcs" 116root { 117 platform { 118 i2c_config { 119 i2c3 { 120 gpio{ 121 // 要配置的引脚个数,接下来的引脚名必须定义成gpio_num_1, gpio_num_2, gpio_num_3... 122 gpio_num_max = 2; //配置的GPIO个数 123 124 // port, pin, mode, speed, outputType, pull, alternate 125 gpio_num_1 = [7, 7, 2, 3, 1, 0, 4]; //GPIO配置1 126 gpio_num_2 = [7, 8, 2, 3, 1, 0, 4]; //GPIO配置2 127 } 128 129 driver : gpio { 130 match_attr = "i2c3_config"; 131 port = 3; // I2C端口号(1~3) 132 dev_mode = 0; //0 = master, 1= slave 133 dev_addr = 0; //I2C 本机地址 134 speed = 100000; //I2C速率 135 } 136 } 137 } 138 } 139} 140``` 141 142## 关于I2C通讯测试芯片RJGT102 143 144 RJGT102芯片为一款标准I2C通讯的加密芯片。 145 146 这里将此芯片当成普通EEPROM使用,通过读写芯片内部UUID区域,以测试HDF I2C的基本收发功能。 147 148 具体芯片读写操作时序及寄存器信息,请自行参考数据手册 149