HiKey(LeMaker version):How to control the GPIO on the board

From BananaPro/Pi
Jump to: navigation, search

Calculation GPIO value

The GPIO pins can be accessed from user space using sysfs. By default, the mapping gpio_operation from the physical GPIO to the sys file system is enabled in the kernels. Before exporting or unexporting, you have to obtain the correct numbering index from the pin name, the calculation is as below for Hikey:

GPIO0:  GPIO0*NUMBER* =  504 + *NUMBER*, for example, GPIO0_7  = 504 + 7 = 511
GPIO1:  GPIO1*NUMBER* =  496 + *NUMBER*, for example, GPIO1_5  = 496 + 5 = 501
GPIO2:  GPIO2*NUMBER* =  488 + *NUMBER*, for example, GPIO2_4  = 488 + 4 = 492
GPIO3:  GPIO3*NUMBER* =  480 + *NUMBER*, for example, GPIO3_6  = 480 + 6 = 486
GPIO4:  GPIO4*NUMBER* =  472 + *NUMBER*, for example, GPIO4_6  = 472 + 6 = 478
GPIO5:  GPIO5*NUMBER* =  464 + *NUMBER*, for example, GPIO5_3  = 464 + 3 = 467
GPIO6:  GPIO6*NUMBER* =  456 + *NUMBER*, for example, GPIO6_2  = 456 + 2 = 458
GPIO7:  GPIO7*NUMBER* =  448 + *NUMBER*, for example, GPIO7_1  = 448 + 1 = 449
GPIO8:  GPIO8*NUMBER* =  440 + *NUMBER*, for example, GPIO8_5  = 440 + 5 = 445
GPIO9:  GPIO9*NUMBER* =  432 + *NUMBER*, for example, GPIO9_6  = 432 + 6 = 438
GPIO10: GPIO10*NUMBER* = 424 + *NUMBER*, for example, GPIO10_5  = 424 + 5 = 429
GPIO11: GPIO11*NUMBER* = 416 + *NUMBER*, for example, GPIO11_3  = 416 + 3 = 419
GPIO12: GPIO12*NUMBER* = 408 + *NUMBER*, for example, GPIO12_1  = 408 + 1 = 409
GPIO13: GPIO13*NUMBER* = 400 + *NUMBER*, for example, GPIO13_1  = 400 + 1 = 401
GPIO14: GPIO14*NUMBER* = 392 + *NUMBER*, for example, GPIO14_4  = 392 + 4 = 396
GPIO15: GPIO15*NUMBER* = 384 + *NUMBER*, for example, GPIO15_5  = 384 + 5 = 389
GPIO16: GPIO16*NUMBER* = 376 + *NUMBER*, for example, GPIO16_2  = 376 + 2 = 378
GPIO17: GPIO17*NUMBER* = 368 + *NUMBER*, for example, GPIO17_1  = 368 + 1 = 369
GPIO18: GPIO18*NUMBER* = 360 + *NUMBER*, for example, GPIO18_7  = 360 + 7 = 367
GPIO19: GPIO19*NUMBER* = 352 + *NUMBER*, for example, GPIO19_6  = 352 + 6 = 358

Use sysfs to control the GPIO

At the same time, you can simply check the under #Appendix: 40 Pins GPIO Mapping Table for 96boards HiKey to obtain the correct sys Pin number. To access a GPIO pin such as GPIO13_6(400+6 = 406) you first need to export it with

echo XX > /sys/class/gpio/export

With XX being the numbering index of the desired pin, here XX = 406. If succeed, you would find the sys file system node

ls
#! /sys/class/gpio/gpioXX is generated

To set a GPIO pin as output you have to change the input/output direction with

echo out > /sys/class/gpio/gpioXX/direction

To set a GPIO pin as input you have to change the input/output direction with

echo in > /sys/class/gpio/gpioXX/direction

You can read a GPIO pin with /sys/class/gpio/gpioXX/value using

cat /sys/class/gpio/gpioXX/value

When the direction is set to out, you can write 1 or 0 to a GPIO pin with /sys/class/gpio/gpioXX/value using

echo 1 > /sys/class/gpio/gpioXX/value

Appendix: 40 Pins GPIO Mapping Table for 96boards HiKey

40 Pins GPIO Mapping Table for 96boards HiKey
Pin Number Pin Definition Pin Index Pin Index Pin Definition Pin Number GPIO CHIP GPIO PORT
- DGND 1 2 DGND - gpiochip504 GPIO0
406 GPIO_13_6/UART2_CTS_N 3 4 PMU_PWRON_N - gpiochip496 GPIO1
393 GPIO_14_1/UART2_TX 5 6 EXP_RSTOUT_N - gpiochip488 GPIO2
392 GPIO_14_0/UART2_RX 7 8 SPI0_SCLK/GPIO_6_3 459 gpiochip480 GPIO3
407 GPIO_13_7/UART2_RTS_N 9 10 SPI0_DI/GPIO_6_0 456 gpiochip472 GPIO4
467 GPIO_5_3/UART3_TX 11 12 SPI0_DI/GPIO_6_2 458 gpiochip464 GPIO5
466 GPIO_5_2/UART3_RX 13 14 SPI0_DI/GPIO_6_1 457 gpiochip456 GPIO6
494 GPIO_14_2/I2C0_SCL 15 16 MODEM_PCM_XFS/GPIO_5_7 471 gpiochip448 GPIO7
495 GPIO_14_3/I2C0_SDA 17 18 MODEM_PCM_XCLK/GPIO_5_6 470 gpiochip440 GPIO8
496 GPIO_14_4/I2C1_SCL 19 20 MODEM_PCM_XCLK/GPIO_7_5 453 gpiochip432 GPIO9
497 GPIO_14_5/I2C1_SDA 21 22 MODEM_PCM_XCLK/GPIO_7_4 452 gpiochip424 GPIO10
488 GPIO2_0 23 24 GPIO2_1 489 gpiochip416 GPIO11
490 GPIO2_2 25 26 GPIO2_3 491 gpiochip408 GPIO12
492 GPIO2_4 27 28 BL_PWM_GPIO12_7 415 gpiochip400 GPIO13
463 GPIO6_7_DSI_TE0 29 30 GPIO2_7 495 gpiochip392 GPIO14
426 ISP_RSTB0_GPIO10_2 31 32 ISP_PWDN0_GPIO9_1 433 gpiochip384 GPIO15
427 ISP_RSTB0_GPIO10_3 33 34 ISP_PWDN0_GPIO9_2 434 gpiochip376 GPIO16
- LD021_1V8 35 36 SYSDC_IN - gpiochip368 GPIO17
- SYS_5V 37 38 SYSDC_IN - gpiochip360 GPIO18
- DGND 39 40 DGND - gpiochip352 GPIO19