1# Pin 2 3## Overview 4 5### Function 6 7The pin module, also called pin controller, manages pin resources of the system on a chip (SoC) and implements the pin multiplexing function. 8 9The pin module provides a set of APIs for pin management, including: 10 11- Obtaining or releasing a pin description handle 12- Setting or obtaining the pull type (pull-up, pull-down, or floating) of a pin 13- Setting or obtaining the pull strength of a pin 14- Setting or obtaining the function of a pin to implement pin multiplexing 15 16### Basic Concepts 17 18Pin is a software concept designed to uniformly manage SoC pins, implement pin multiplexing, and set electrical features of pins. 19 20- SoC 21 22 An SoC is a chip that integrates microprocessors, analog IP cores, digital IP cores, and memory for specific purposes. 23 24- Pin multiplexing 25 26 When the number of pins of a chip cannot handle the increasing connection requests, you can set software registers to make the pins to work in different states. 27 28### Working Principles 29 30In the Hardware Driver Foundation (HDF), the pin module uses the unified service mode for API adaptation. In this mode, a device service is used as the pin manager to handle access requests from the devices of the same type in a unified manner. The unified service mode applies to the scenario where there are many device objects of the same type. If the independent service mode is used in this case, more device nodes need to be configured and more memory resources will be consumed. 31 32In the unified service mode, the core layer manages all controllers in a unified manner and publishes a service for the interface layer. That is, the driver does not need to publish a service for each controller. 33 34The pin module is divided into the following layers: 35 36- Interface layer: provides APIs for obtaining a pin, setting or obtaining the pull type, pull strength, and function of a pin, and releasing a pin. 37- Core layer: provides the capabilities of matching pin resources, adding and removing a pin controller, and managing pins. The core layer interacts with the adaptation layer through hook functions. 38- Adaptation layer: instantiates the hook functions to implement specific features. 39 40**Figure 1** Unified service mode 41![Unified service mode](figures/unified-service-mode.png) 42 43### Constraints 44 45The pin module supports only the LiteOS-A kernel of the small system. 46 47## Usage Guidelines 48 49### When to Use 50 51The pin module is a software concept used to manage pin resources. You can set the function, pull type, and pull strength of pins to implement pin multiplexing. 52 53### Available APIs 54 55The following table describes the APIs of the pin module. 56 57**Table 1** Pin driver APIs 58 59| **API** | **Description** | 60| ------------------------------------------------------------ | ---------------- | 61| DevHandle PinGet(const char *pinName) | Obtains a pin description handle.| 62| void PinPut(DevHandle handle) | Releases the pin description handle.| 63| int32_t PinSetPull(DevHandle handle, enum PinPullType pullType) | Sets the pull type of a pin.| 64| int32_t PinGetPull(DevHandle handle, enum PinPullType *pullType) | Obtains the pull type of a pin.| 65| int32_t PinSetStrength(DevHandle handle, uint32_t strength) | Sets the pull strength of a pin.| 66| int32_t PinGetStrength(DevHandle handle, uint32_t *strength) | Obtains the pull strength of a pin.| 67| int32_t PinSetFunc(DevHandle handle, const char *funcName) | Sets the pin function.| 68| int32_t PinGetFunc(DevHandle handle, const char **funcName) | Obtains the pin function. | 69 70>![](../public_sys-resources/icon-note.gif) **NOTE** 71> 72>All the pin APIs described in this document can be used in kernel mode and user mode. 73 74### How to Develop 75 76The following figure illustrates how to use pin driver APIs. 77 78 **Figure 2** Using pin driver APIs<br> 79![](figures/using-pin-process.png "Process of using the pin module") 80 81#### Obtaining a Pin Description Handle 82 83Before performing an operation on a pin, use **PinGet()** to obtain the pin description handle based on the pin name. 84 85```c 86DevHandle PinGet(const char *pinName); 87``` 88 89**Table 2** Description of PinGet 90 91| Parameter | Description | 92| ---------- | ----------------------- | 93| pinName | Pointer to the pin name. | 94| **Return Value**| **Description** | 95| NULL | The operation fails.| 96| handle | The operation is successful. The pin description handle obtained is returned. | 97 98Example: Obtain the pin description handle of P18. 99 100```c 101DevHandle handle = NULL; // Pin description handle. 102 103char pinName = "P18"; // Pin name. 104handle = PinGet(pinName); 105if (handle == NULL) { 106 HDF_LOGE("PinGet: get handle failed!\n"); 107 return; 108} 109``` 110 111#### Setting the Pull Type of a Pin 112 113Use **PinSetPull()** to set the pull type of a pin. 114 115```c 116int32_t PinSetPull(DevHandle handle, enum PinPullType pullType); 117``` 118 119**Table 3** Description of PinSetPull 120 121| Parameter | Description | 122| ---------- | ----------------------- | 123| handle | Pin description handle. | 124| pullType | Pull type to set. | 125| **Return Value**| **Description** | 126| 0 | The operation is successful.| 127| Negative value | The operation fails.| 128 129Example: Set the pull type to pull-up. 130 131```c 132int32_t ret; 133enum PinPullType pullTypeNum; 134 135/* Set the pull type of a pin. */ 136pullTypeNum = 1; 137ret = PinSetPull(handle, pullTypeNum); 138if (ret != HDF_SUCCESS) { 139 HDF_LOGE("PinSetPull: failed, ret %d\n", ret); 140 return ret; 141} 142``` 143 144#### Obtaining the Pull Type of a Pin 145 146Use **PinGetPull()** to obtain the pull type of a pin. 147 148```c 149int32_t PinGetPull(DevHandle handle, enum PinPullType *pullType); 150``` 151 152**Table 4** Description of PinGetPull 153 154| Parameter | Description | 155| ---------- | ------------------------- | 156| handle | Pin description handle. | 157| pullType | Pointer to the pull type obtained.| 158| **Return Value**| **Description** | 159| 0 | The operation is successful. | 160| Negative value | The operation fails. | 161 162Example: Obtain the pull type of a pin. 163 164```c 165int32_t ret; 166enum PinPullType pullTypeNum; 167 168/* Obtain the pull type of a pin. */ 169ret = PinGetPull(handle, &pullTypeNum); 170if (ret != HDF_SUCCESS) { 171 HDF_LOGE("PinGetPull: failed, ret %d\n", ret); 172 return ret; 173} 174``` 175 176#### Setting the Pull Strength of a Pin 177 178Use **PinSetStrength()** to set the pull type of a pin. 179 180```c 181int32_t PinSetStrength(DevHandle handle, uint32_t strength); 182``` 183 184**Table 5** Description of PinSetStrength 185 186| Parameter | Description | 187| ---------- | ----------------------- | 188| handle | Pin description handle. | 189| strength | Pull strength to set. | 190| **Return Value**| **Description** | 191| 0 | The operation is successful.| 192| Negative value | The operation fails.| 193 194Example: Set the pull strength of the pin to 2. 195 196```c 197int32_t ret; 198uint32_t strengthNum; 199/* Set the pull strength of the pin. */ 200strengthNum = 2; 201ret = PinSetStrength(handle, strengthNum); 202if (ret != HDF_SUCCESS) { 203 HDF_LOGE("PinSetStrength: failed, ret %d\n", ret); 204 return ret; 205} 206``` 207 208#### Obtaining the Pull Strength of a Pin 209 210Use **PinGetStrength()** to obtain the pull strength of a pin. 211 212```c 213int32_t PinGetStrength(DevHandle handle, uint32_t *strength); 214``` 215 216**Table 6** Description of PinGetStrength 217 218| Parameter | Description | 219| ---------- | ------------------------- | 220| handle | Pin description handle. | 221| strength | Pointer to the pull strength obtained.| 222| **Return Value**| **Description** | 223| 0 | The operation is successful. | 224| Negative value | The operation fails. | 225 226Example: Obtain the pull strength of a pin. 227 228```c 229int32_t ret; 230uint32_t strengthNum; 231 232/* Obtain the pull strength of the pin. */ 233ret = PinGetStrength(handle, &strengthNum); 234if (ret != HDF_SUCCESS) { 235 HDF_LOGE("PinGetStrength: failed, ret %d\n", ret); 236 return ret; 237} 238``` 239 240#### Setting the Pin Function 241 242The pin function refers to the multiplexed pin function. The function of each pin is different. For details about the pin function name, see **//device/soc/hisilicon/hi3516dv300/sdk_liteos/hdf_config/pin/pin_config.hcs**. 243 244Use **PinSetFunc()** to set the pin function. 245 246```c 247int32_t PinSetFunc(DevHandle handle, const char *funcName); 248``` 249 250**Table 7** Description of PinSetFunc 251 252| Parameter | Description | 253| ---------- | ------------------- | 254| handle | Pin description handle. | 255| funcName | Pointer to the pin function to set. | 256| **Return Value**| **Description** | 257| 0 | The operation is successful.| 258| Negative value | The operation fails.| 259 260Example: Set the pin function to LSADC_CH1 (ADC channel 1). 261 262```c 263int32_t ret; 264char funcName = "LSADC_CH1"; 265 266/* Sets the pin function. */ 267ret = PinSetFunc(handle, funcName); 268if (ret != HDF_SUCCESS) { 269 HDF_LOGE("PinSetFunc: failed, ret %d\n", ret); 270 return ret; 271} 272``` 273 274#### Obtaining the Pin Function 275 276Use **PinGetFunc()** to obtain the pin function. 277 278```c 279int32_t PinGetFunc(DevHandle handle, const char **funcName); 280``` 281 282**Table 8** Description of PinGetFunc 283 284| Parameter | Description | 285| ---------- | --------------------- | 286| handle | Pin description handle. | 287| funcName | Pointer to the function name obtained.| 288| **Return Value**| **Description** | 289| 0 | The operation is successful. | 290| Negative value | The operation fails. | 291 292Example: Obtain the pin function. 293 294```c 295int32_t ret; 296char *funcName = NULL; 297 298/* Obtain the pin function. */ 299ret = PinGetFunc(handle, &funcName); 300if (ret != HDF_SUCCESS) { 301 HDF_LOGE("PinGetFunc: failed, ret %d\n", ret); 302 return ret; 303} 304``` 305 306#### Releasing a Pin Description Handle 307 308After the operations on a pin are complete, use **PinPut()** to release the pin description handle. 309 310```c 311void PinPut(DevHandle handle); 312``` 313 314**Table 9** Description of PinPut 315 316| Parameter | Description | 317| ---------- | -------------- | 318| handle | Pin description handle. | 319| **Return Value**| **Description**| 320| NA | No value is returned. | 321 322Example: Release a pin description handle. 323 324```c 325PinPut(handle); 326``` 327 328## Example 329 330The following uses the Hi3516D V300 development board as an example. The procedure is as follows: 331 3321. Pass in the pin name to obtain the pin description handle. 3332. Set the pull type of the pin. If the operation fails, release the pin description handle. 3343. Obtain the pull type of the pin. If the operation fails, release the pin description handle. 3354. Set the pull strength of the pin. If the operation fails, release the pin description handle. 3365. Obtain the pin pull strength. If the operation fails, release the pin description handle. 3375. Set the pin function. If the operation fails, release the pin description handle. 3386. Obtain the pin function. If the operation fails, release the pin description handle. 3397. Release the pin description handle if no operation needs to be performed on the pin. 340 341```c 342#include "hdf_log.h" /* Header file of the HDF log APIs. */ 343#include "pin_if.h" /* Header file of standard pin APIs. */ 344 345int32_t PinTestSample(void) 346{ 347 int32_t ret; 348 uint32_t strengthNum; 349 enum PinPullType pullTypeNum; 350 char pinName; 351 char *funName; 352 DevHandle handle = NULL; 353 354 /* Pin name. Set it to the actual pin name. */ 355 pinName = "P18"; 356 /* Obtain the pin description handle. */ 357 handle = PinGet(pinName); 358 if (handle == NULL) { 359 HDF_LOGE("PinGet: pin get failed!\n"); 360 return; 361 } 362 /* Set the pull type to pull-up for the pin. */ 363 pullTypeNum = 1; 364 ret = PinSetPull(handle, pullTypeNum); 365 if (ret != HDF_SUCCESS) { 366 HDF_LOGE("PinSetPull: failed, ret %d\n", ret); 367 goto ERR; 368 } 369 /* Obtain the pull type of the pin. */ 370 ret = PinGetPull(handle, &pullTypeNum); 371 if (ret != HDF_SUCCESS) { 372 HDF_LOGE("PinGetPull: failed, ret %d\n", ret); 373 goto ERR; 374 } 375 /* Set the pull strength of the pin to 2. */ 376 strengthNum = 2; 377 ret = PinSetStrength(handle, strengthNum); 378 if (ret != HDF_SUCCESS) { 379 HDF_LOGE("PinSetStrength: failed, ret %d\n", ret); 380 goto ERR; 381 } 382 /* Obtain the pull strength of the pin. */ 383 ret = PinGetStrength(handle, &strengthNum); 384 if (ret != HDF_SUCCESS) { 385 HDF_LOGE("PinGetStrength: failed, ret %d\n", ret); 386 goto ERR; 387 } 388 /* Set the pin function to LSADC_CH1. */ 389 funName = "LSADC_CH1"; 390 ret = PinSetFunc(handle, funName); 391 if (ret != HDF_SUCCESS) { 392 HDF_LOGE("PinSetFunc: failed, ret %d\n", ret); 393 goto ERR; 394 } 395 /* Obtain the pin function. */ 396 ret = PinGetFunc(handle, &funcName); 397 if (ret != HDF_SUCCESS) { 398 HDF_LOGE("PinGetFunc: failed, ret %d\n", ret); 399 goto ERR; 400 } 401ERR: 402 /* Release the pin description handle. */ 403 PinPut(handle); 404 return ret; 405} 406``` 407