The Inter-Integrated Circuit (I2C) bus is a simple and bidirectional two-wire synchronous serial bus developed by Philips. In the Hardware Driver Foundation (HDF), the I2C module uses the unified service mode for API adaptation. In this mode, a device service is used as the I2C manager to handle external access requests in a unified manner, which is reflected in the configuration file. The unified service mode applies to the scenario where there are many device objects of the same type, for example, when the I2C module has more than 10 controllers. If the independent service mode is used, more device nodes need to be configured and memory resources will be consumed by services.
The Inter-Integrated Circuit \(I2C\) bus is a simple and bidirectional two-wire synchronous serial bus developed by Philips. In the Hardware Driver Foundation (HDF) framework, the I2C module uses the unified service mode for API adaptation. In this mode, a device service is used as the I2C manager to handle external access requests in a unified manner, which is reflected in the configuration file. The unified service mode applies to the scenario where there are many device objects of the same type, for example, when the I2C module has more than 10 controllers. If the independent service mode is used, more device nodes need to be configured and memory resources will be consumed by services.
**Figure 1** Unified service mode
**Figure 1** Unified service mode<aname="fig17640124912440"></a>

| transfer | **cntlr**: structure pointer to the I2C controller at the core layer.<br><br>**msgs**: structure pointer to the messages to transfer.<br>**count**: number of messages. The value is of the uint16_t type.| –| HDF_STATUS| Transfers user messages.|
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.2 "><pid="p9551164011468"><aname="p9551164011468"></a><aname="p9551164011468"></a><strongid="b1413775771219"><aname="b1413775771219"></a><aname="b1413775771219"></a>cntlr</strong>: structure pointer to the I2C controller at the core layer. <strongid="b13955019171313"><aname="b13955019171313"></a><aname="b13955019171313"></a>msgs</strong>: structure pointer to the user message. <strongid="b4678857181319"><aname="b4678857181319"></a><aname="b4678857181319"></a>count</strong>: number of messages, which is of the uint16_t type.</p>
<tdclass="cellrowborder"valign="top"width="20%"headers="mcps1.2.6.1.5 "><pid="p8551174044612"><aname="p8551174044612"></a><aname="p8551174044612"></a>Transfers user messages.</p>
</td>
</tr>
</tbody>
</table>
## How to Develop<a name="section1085786591114257"></a>
The I2C module adaptation involves the following steps:
The I2C module adaptation involves the following steps:
1. Instantiate the driver entry.
1. Instantiate the driver entry.
- Instantiate the **HdfDriverEntry** structure.
- Instantiate the **HdfDriverEntry** structure.
- Call **HDF\_INIT** to register the **HdfDriverEntry** instance with the HDF framework.
- Call **HDF_INIT** to register the **HdfDriverEntry** instance with the HDF.
2. Configure attribute files.
2. Configure attribute files.
- Add the **deviceNode** information to the **device\_info.hcs** file.
- Add the **deviceNode** information to the **device_info.hcs** file.
-\(Optional\) Add the **i2c\_config.hcs** file.
- (Optional) Add the **i2c_config.hcs** file.
3. Instantiate the I2C controller object.
3. Instantiate the I2C controller object.
- Initialize **I2cCntlr**.
- Initialize **I2cCntlr**.
- Instantiate **I2cMethod** and **I2cLockMethod** in **I2cCntlr**.
- Instantiate **I2cMethod** and **I2cLockMethod** in **I2cCntlr**.
For details, see [Available APIs](#available-apis).
>
> For details, see [Available APIs](#available-apis).
4.\(Optional\) Debug the driver.
4. Debug the driver.<br>
For new drivers, verify basic functions, for example, verify the information returned after the connect operation and whether data is successfully transmitted.
(Optional) For new drivers, verify basic functions, for example, check whether data is successfully transmitted and the information returned after the virtual file system (VFS) is mounted.
## Development Example
## Development Example<a name="section1773332551114257"></a>
The following uses **i2c_hi35xx.c** as an example to present the information required for implementing device functions.
The following uses **i2c\_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.<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, 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.
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.
I2C driver entry example:
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.
An I2C controller may be connected with multiple devices. You need to create a manager object in the HDF and publish a manager service to handle external access requests in a unified manner. Before a device is used, the manager service must be obtained first. Then, the manager service locates the target device based on the specified parameters.
- I2C driver entry reference
The driver of the I2C manager is implemented by the core layer. Vendors do not need to care about the implementation of this part. However, the **Init** function must call the **I2cCntlrAdd** function of the core layer to implement corresponding features.
Many devices may be connected to the I2C module. Therefore, in the HDF framework, a manager object is created for the I2C, and a manager service is launched to handle external access requests in a unified manner. When a user wants to open a device, the user obtains the manager service first. Then, the manager service locates the target device based on the parameters specified by the user.
The driver of the I2C manager is implemented by the core layer. Vendors do not need to pay attention to the implementation of this part. However, when they implement the **Init** function, the **I2cCntlrAdd** function of the core layer must be called to implement the corresponding features.
```
struct HdfDriverEntry g_i2cDriverEntry = {
```
.moduleVersion = 1,
struct HdfDriverEntry g_i2cDriverEntry = {
.Init = Hi35xxI2cInit,
.moduleVersion = 1,
.Release = Hi35xxI2cRelease,
.Init = Hi35xxI2cInit,
.moduleName = "hi35xx_i2c_driver",// (Mandatory) The value must be the same as that in the config.hcs file.
.Release = Hi35xxI2cRelease,
};
.moduleName = "hi35xx_i2c_driver",// (Mandatory) The value must be the same as that in the config.hcs file.
HDF_INIT(g_i2cDriverEntry); // Call HDF_INIT to register the driver entry with the HDF.
};
HDF_INIT(g_i2cDriverEntry); // Call HDF_INIT to register the driver entry with the HDF framework.
// Driver entry of the i2c_core.c manager service at the core layer
struct HdfDriverEntry g_i2cManagerEntry = {
// Driver entry of the i2c_core.c manager service at the core layer
.moduleVersion = 1,
struct HdfDriverEntry g_i2cManagerEntry = {
.Bind = I2cManagerBind,
.moduleVersion = 1,
.Init = I2cManagerInit,
.Bind = I2cManagerBind,
.Release = I2cManagerRelease,
.Init = I2cManagerInit,
.moduleName = "HDF_PLATFORM_I2C_MANAGER",// This parameter corresponds to device0 in the device_info file.
.Release = I2cManagerRelease,
};
.moduleName = "HDF_PLATFORM_I2C_MANAGER",// This parameter corresponds to device0 in the device_info file.
HDF_INIT(g_i2cManagerEntry);
};
```
HDF_INIT(g_i2cManagerEntry);
```
2. Add the **deviceNode** information to the **device_info.hcs** file and configure the device attributes in the **i2c_config.hcs** file.
2. Add the **deviceNode** information to the **device\_info.hcs** file and configure the device attributes in the **i2c\_config.hcs** file. The **deviceNode** information is related to registration of the driver entry. The device attribute values are closely related to the driver implementation and the default values or value ranges of the **I2cCntlr** members at the core layer.
The **deviceNode** information is related to registration of the driver entry. The device attribute values are closely related to the driver implementation and the default values or value ranges of the **I2cCntlr** members at the core layer.
In the unified service mode, the first device node in the **device\_info** file must be the I2C manager. [Table 2](#table96651915911) lists settings of its parameters.
In the unified service mode, the first device node in the **device_info** file must be the I2C manager. The table below lists the settings of its parameters.
Configure I2C controller information from the second node. This node specifies a type of I2C controllers rather than a specific I2C controller. The controllers are distinguishes by **busID** and **reg_pbase**, which can be seen in the **i2c_config** file.
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p36717191292"><a name="p36717191292"></a><a name="p36717191292"></a>It has a fixed value of <strong id="b1343012314357"><a name="b1343012314357"></a><a name="b1343012314357"></a>HDF_PLATFORM_I2C_MANAGER</strong>.</p>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p86716195912"><a name="p86716195912"></a><a name="p86716195912"></a>It has a fixed value of <strong id="b107651238143515"><a name="b107651238143515"></a><a name="b107651238143515"></a>HDF_PLATFORM_I2C_MANAGER</strong>.</p>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18677191699"><a name="p18677191699"></a><a name="p18677191699"></a>The value can be <strong id="b13997735183718"><a name="b13997735183718"></a><a name="b13997735183718"></a>1</strong> or <strong id="b165591038103717"><a name="b165591038103717"></a><a name="b165591038103717"></a>2</strong>, depending on whether it is visible to the user mode.</p>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p1567171918915"><a name="p1567171918915"></a><a name="p1567171918915"></a>This parameter is not used.</p>
}
</td>
device1 :: deviceNode {
</tr>
policy = 0; // The value 0 indicates that no service needs to be published.
</tbody>
priority = 55; // Driver startup priority.
</table>
permission = 0644; // Permission for the driver to create a device node.
moduleName = "hi35xx_i2c_driver"; // (Mandatory) Driver name, which must be the same as moduleName in the driver entry.
Configure I2C controller information from the second node. This node specifies a type of I2C controllers rather than an I2C controller. The **busID** and **reg\_pbase** parameters distinguish controllers, which can be seen in the **i2c\_config** file.
serviceName = "HI35XX_I2C_DRIVER"; // (Mandatory) Unique name of the service published by the driver.
deviceMatchAttr = "hisilicon_hi35xx_i2c"; // (Mandatory) Used to configure the private data of the controller. The value must be the same as the controller information in i2c_config.hcs.
- **device\_info.hcs** configuration reference
//The specific controller information is stored in i2c_config.hcs.
}
```
}
root {
}
device_info {
}
match_attr = "hdf_manager";
```
device_i2c :: device {
device0 :: deviceNode {
-**i2c_config.hcs** configuration example
policy = 2;
priority = 50;
permission = 0644;
```
moduleName = "HDF_PLATFORM_I2C_MANAGER";
root {
serviceName = "HDF_PLATFORM_I2C_MANAGER";
platform {
deviceMatchAttr = "hdf_platform_i2c_manager";
i2c_config {
}
match_attr = "hisilicon_hi35xx_i2c"; // (Mandatory) The value must be the same as that of deviceMatchAttr in device_info.hcs.
device1 :: deviceNode {
template i2c_controller { // Template configuration. In the template, you can configure the common parameters shared by service nodes.
policy = 0; // The value 0 indicates that no service needs to be published.
bus = 0; // (Mandatory) I2C identifier.
priority = 55; // Driver startup priority
reg_pbase = 0x120b0000; // (Mandatory) Physical base address.
permission = 0644; // Permission for the driver to create a device node
reg_size = 0xd1; // (Mandatory) Register bit width.
moduleName = "hi35xx_i2c_driver"; // (Mandatory) Driver name, which must be the same as the moduleName in the driver entry.
irq = 0; // (Optional) Configured based on your requirements.
serviceName = "HI35XX_I2C_DRIVER"; // (Mandatory) Unique name of the service published by the driver
freq = 400000; // (Optional) Configured based on your requirements.
deviceMatchAttr = "hisilicon_hi35xx_i2c";// (Mandatory) Used to configure the private data of the controller. The value must be the same as the controller in i2c_config.hcs.
clk = 50000000; // (Optional) Configured based on your requirements.
} // The specific controller information is in i2c_config.hcs.
}
}
controller_0x120b0000 :: i2c_controller {
}
bus = 0;
}
}
}
controller_0x120b1000 :: i2c_controller {
```
bus = 1;
reg_pbase = 0x120b1000;
- **i2c\_config.hcs** configuration reference
}
...
```
}
root {
}
platform {
}
i2c_config {
```
match_attr = "hisilicon_hi35xx_i2c";// (Mandatory) The value must be the same as that of deviceMatchAttr in device_info.hcs.
template i2c_controller { // Template configuration. In the template, you can configure the common parameters shared by service nodes.
3. Initialize the **I2cCntlr** 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 **I2cMethod** in **I2cCntlr** (so that the underlying driver functions can be called).
bus = 0; // (Mandatory) I2C ID
- Defining a custom structure
reg_pbase = 0x120b0000; // (Mandatory) Physical base address
reg_size = 0xd1; // (Mandatory) Register bit width
To the driver, the custom structure holds parameters and data. The **DeviceResourceIface** method provided by the HDF reads the values in the **i2c_config.hcs** file to initialize the members in the custom structure and passes important parameters, such as the device number and bus number, to the **I2cCntlr** object at the core layer.
irq = 0; // (Optional) Configured based on the vendor's requirements.
freq = 400000; // (Optional) Configured based on the vendor's requirements.
clk = 50000000; // (Optional) Configured based on the vendor's requirements.
```
}
// Custom structure
controller_0x120b0000 :: i2c_controller {
struct Hi35xxI2cCntlr {
bus = 0;
struct I2cCntlr cntlr; // (Mandatory) Control object at the core layer. For details, see the following description.
}
OsalSpinlock spin; // (Mandatory) You need to implement lock() and unlock() based on this variable.
controller_0x120b1000 :: i2c_controller {
volatile unsigned char *regBase; // (Mandatory) Register base address.
bus = 1;
uint16_t regSize; // (Mandatory) Register bit width.
reg_pbase = 0x120b1000;
int16_t bus; // (Mandatory) The value can be read from the i2c_config.hcs file.
}
uint32_t clk; // (Optional) Customized.
...
uint32_t freq; // (Optional) Customized.
}
uint32_t irq; // (Optional) Customized.
}
uint32_t regBasePhy // (Mandatory) Physical base address of the register.
}
};
```
// I2cCntlr is a controller structure at the core layer. The Init function assigns values to the members of I2cCntlr.
3. Initialize the **I2cCntlr** object at the core layer, including initializing the vendor custom structure \(transferring parameters and data\), instantiating **I2cMethod**\(used to call underlying functions of the driver\) in **I2cCntlr**, and implementing the **HdfDriverEntry** member functions \(**Bind**, **Init**, and **Release**\).
struct I2cCntlr {
- Custom structure reference
struct OsalMutex lock;
void *owner;
To the driver, the custom structure carries parameters and data. The values in the **i2c\_config.hcs** file are read by HDF, and the structure members are initialized through **DeviceResourceIface**. Some important values, such as the device number and bus number, are also passed to the **I2cCntlr** object at the core layer.
int16_t busId;
void *priv;
```
const struct I2cMethod *ops;
// Vendor custom function structure
const struct I2cLockMethod *lockOps;
struct Hi35xxI2cCntlr {
};
struct I2cCntlr cntlr; // (Mandatory) Control object of the core layer. For details, see the following description.
```
OsalSpinlock spin; // (Mandatory) The vendor needs to implement lock and unlock for I2C operation functions based on this variable.
- Instantiating **I2cMethod** and **I2cLockMethod** (other members are initialized by **Init**)
volatile unsigned char *regBase; // (Mandatory) Base address of the register
uint16_t regSize; // (mandatory) Bit width of the register
int16_t bus; // (Mandatory) The value can be read from the i2c_config.hcs file.
```
uint32_t clk; // (Optional) Customized by the vendor.
// Example in i2c_hi35xx.c
uint32_t freq; // (Optional) Customized by the vendor.
static const struct I2cMethod g_method = {
uint32_t irq; // (Optional) Customized by the vendor.
.transfer = Hi35xxI2cTransfer,
uint32_t regBasePhy; // (Mandatory) Physical base address of the register
};
};
static const struct I2cLockMethod g_lockOps = {
// I2cCntlr is the controller structure at the core layer. Its members are assigned with values by using the Init function.
.lock = Hi35xxI2cLock, // Lock function
struct I2cCntlr {
.unlock = Hi35xxI2cUnlock,// Unlock function
struct OsalMutex lock;
};
void *owner;
```
int16_t busId;
-**Init** function
void *priv;
const struct I2cMethod *ops;
Input parameter:
const struct I2cLockMethod *lockOps;
};
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
```
Return value:
- Instantiate the member callback function structure **I2cMethod** in **I2cCntlr** and the lock callback function structure **I2cLockMethod**. Other members are initialized by using the **Init** function.
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.
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.\)
...
// Traverse and parse all nodes in i2c_config.hcs and call Hi35xxI2cParseAndInit to initialize the devices separately.
**Table 3** Input parameters and return values of the Init function
(void)OsalSpinInit(&hi35xx->spin); // (Mandatory) Initialize the lock.
</td>
ret = I2cCntlrAdd(&hi35xx->cntlr); // (Mandatory) Call this function to set the structure of the core layer. The driver accesses the platform core layer only after a success signal is returned.
ret = Hi35xxI2cParseAndInit(device, childNode);// For details about the function definition, see the following 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.
Hi35xxI2cCntlrInit(hi35xx); // (Mandatory) Initialize the I2C device.
{
...
hi35xx->cntlr.priv = (void *)node; // (Mandatory) Store device attributes.
// (Mandatory) Call the I2cCntlrGet function to obtain the I2cCntlr object based on bus ID of the device, and call the I2cCntlrRemove function to release the I2cCntlr object.
hi35xx->cntlr.busId = hi35xx->bus; // (Mandatory) Initialize busId in I2cCntlr.
cntlr = I2cCntlrGet(bus);
hi35xx->cntlr.ops = &g_method; // (Mandatory) Connect to the I2cMethod instance.
if (cntlr != NULL && cntlr->priv == node) {
hi35xx->cntlr.lockOps = &g_lockOps; // (Mandatory) Connect to the I2cLockMethod instance.
...
(void)OsalSpinInit(&hi35xx->spin); // (Mandatory) Initialize the lock.
I2cCntlrRemove(cntlr);
ret = I2cCntlrAdd(&hi35xx->cntlr); // (Mandatory) Call this function to set the structure of the core layer. The driver accesses the platform core layer only after a success signal is returned.
// (Mandatory) Unmap the addresses and release the lock and memory.
...
hi35xx = (struct Hi35xxI2cCntlr *)cntlr;
#ifdef USER_VFS_SUPPORT
OsalIoUnmap((void *)hi35xx->regBase);
(void)I2cAddVfsById(hi35xx->cntlr.busId);// (Optional) Connect the driver to the user-level virtual file system supported.
(void)OsalSpinDestroy(&hi35xx->spin);
#endif
OsalMemFree(hi35xx);
return HDF_SUCCESS;
}
__ERR__: // If the operation fails, execute the initialization function reversely.
return;
if (hi35xx != NULL) {
}
if (hi35xx->regBase != NULL) {
```
OsalIoUnmap((void *)hi35xx->regBase);
hi35xx->regBase = NULL;
}
OsalMemFree(hi35xx);
hi35xx = NULL;
}
return ret;
}
```
- 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.
// (Mandatory) Call the I2cCntlrGet function to obtain the I2cCntlr object based on busid of the device, and call the I2cCntlrRemove function to release the I2cCntlr object.
cntlr = I2cCntlrGet(bus);
if (cntlr != NULL && cntlr->priv == node) {
...
I2cCntlrRemove(cntlr);
// (Mandatory) Remove the address mapping and release the lock and memory.