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