Skip to main content

Peripherals and Interfaces

1.1 Introduction

  • GPIO stands for General-purpose input/output.

  • Core3566 pin classification:

    • Power pins: 5V, 3.3V, GND (Ground)
    • General GPIO control pins: These pins can be controlled by writing programs to set their high or low logic levels.
    • Special GPIO communication pins: SPI communication, I2C communication, TXD/RXD serial communication.
  • Core3566 has a 40-pin expansion header.

1.2 GPIO Numbering Calculation

  • GPIO has 5 banks, GPIO0 to GPIO4, and each bank has 32 pins, named as follows:

    GPIO0_A0 ~ A7 
    GPIO0_B0 ~ B7
    GPIO0_C0 ~ C7
    GPIO0_D0 ~ D7

    GPIO1_A0 ~ A7
    ....
    GPIO1_D0 ~ D7
    ....
    GPIO4_D0 ~ D7
  • For Linux 4.19 kernel, the GPIO number can be calculated as follows, using GPIO4_C6 (PIN40 on the 40-pin GPIO) as an example:

    GPIO4_C6 = 4*32 + 2*8 + 6 = 150
    (A=0, B=1, C=2, D=3)

1.3 Controlling I/O Using GPIO Sysfs Interface

  • Setting GPIO4_C6 as an output:

    sudo su
    cd /sys/class/gpio
    echo 150 > export
    cd gpio150
    echo out > direction
    echo 1 > value # 输出高
    echo 0 > value # 输出低

1.4 Controlling I/O Using Python Program

  1. Controlling an LED to blink alternately.

    from periphery import GPIO
    import time

    LED_Pin = 150 #Physical Pin-40 is GPIO 150
    # Open GPIO /sys/class/gpio/gpio150 with output direction
    LED_GPIO = GPIO(LED_Pin, "out")

    while True:
    try:
    LED_GPIO.write(True)
    time.sleep(0.5)
    LED_GPIO.write(False)
    time.sleep(0.5)
    except KeyboardInterrupt:
    LED_GPIO.write(False)
    break
    except IOError:
    print ("Error")

    LED_GPIO.close()
  2. Run the program.

    linaro@linaro-alip:~$ sudo python3 test.py 

1.5 Controlling I/O Using C Program

  1. Download and unzip the C program file, and upload the files to Core3566 (you can refer to Samba file sharing).

  2. Enter the project folder, where you will find four files: sysfs_gpio.c, sysfs_gpio.h, main.c, and Makefile.

    linaro@linaro-alip:~$ cd Luckfox_GPIO_C && ls
    main.c Makefile sysfs_gpio.c sysfs_gpio.h
    linaro@linaro-alip:~/Luckfox_GPIO_C$
  3. In the sysfs_gpio.h file, the physical encoding of Core3566 pins is defined, and it is called in the main function in main.c. You can modify it according to your needs.

    #sysfs_gpio.h 
    ....
    #define GPIO7 111 // 7,111
    #define UART2_TX 25 // 8,25
    #define UART2_RX 24 // 10,24
    #define GPIO11 104 //11,104
    #define GPIO12 150 //12,150
    #define GPIO13 116 //13,116
    #define GPIO15 117 //15,117
    #define GPIO16 97 //16,97
    #define GPIO18 98 //18,98
    #define SPI1_MOSI 113 // 19,113
    #define SPI1_MISO 114 // 21,114
    #define GPIO22 99 //22,99
    #define SPI1_CLK 115 //23,115
    #define SPI1_CS0 100 //24,100
    #define SPI1_CS1 101 //26,101
    #define GPIO29 112 //29,112
    #define GPIO31 148 //31,148
    #define GPIO32 106 //32,106
    #define GPIO33 105 //33,105
    #define GPIO35 149 //35,149
    #define GPIO36 102 //36,102
    #define GPIO37 103 //37,103
    #define GPIO38 147 //38,147
    #define GPIO40 146 //40,146
    ....

    #main.c
    ....
    int x[25] = {GPIO7, UART2_TX , UART2_RX , GPIO11, GPIO12, GPIO13, GPIO15, GPIO16, \
    GPIO18, SPI1_MOSI, SPI1_MISO, GPIO22, SPI1_CLK , SPI1_CS0, SPI1_CS1, \
    GPIO29 ,GPIO31 ,GPIO32 ,GPIO33 ,GPIO35 ,GPIO36 ,GPIO37 ,GPIO38 ,GPIO40};
    ....
  4. Compile the program.

    linaro@linaro-alip:~$ sudo make
    cc -c sysfs_gpio.c -o sysfs_gpio.o
    cc -c main.c -o main.o
    cc sysfs_gpio.o main.o -o sysfs_gpio
  5. Run the program to view GPIO information on the terminal and turn on/off the LED one by one.

    linaro@linaro-alip:~/Luckfox_GPIO_C$ sudo ./sysfs_gpio 
    Debug: Export: Pin111
    Debug: Pin111:Output
    Debug: Export: Pin25
    Debug: Pin25:Output
    Debug: Export: Pin24
    Debug: Pin24:Output
    Debug: Export: Pin104
    Debug: Pin104:Output
    ....