# HowTo: GPIO access, I2C, Kernel Building

**URL:** <https://forum.clockworkpi.com/t/howto-gpio-access-i2c-kernel-building/5036>\
**Category:** Developers\
**Created:** [December 27, 2019, 3:49pm UTC](https://forum.clockworkpi.com/t/howto-gpio-access-i2c-kernel-building/5036 "2019-12-27T15:49:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![p3dt](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@p3dt](https://forum.clockworkpi.com/u/p3dt)\
**Post date:** [December 27, 2019, 3:49pm UTC](https://forum.clockworkpi.com/t/howto-gpio-access-i2c-kernel-building/5036/1 "2019-12-27T15:49:11Z")

</div>

Hi, it’s Paul again 😉

## Intro

I found some time over the last days to play with the GPIOs and I2C. And got all of them to work, as well as I2C support:

 ![00-i2c-setup](https://canada1.discourse-cdn.com/flex029/uploads/clockworkpi/original/2X/d/dee4401af5a78dde156fbe75285edc42a63995dc.jpeg)

```auto
    WARNING! This program can confuse your I2C bus, cause data loss and worse!
    I will probe file /dev/i2c-0.
    I will probe address range 0x03-0x77.
    Continue? [Y/n] 
        0 1 2 3 4 5 6 7 8 9 a b c d e f
    00: -- -- -- -- -- -- -- -- -- -- -- -- -- 
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- -- 
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    70: -- -- -- -- -- -- 76 --    

```

The GPIOs worked out of the box. I2C was a bit tricky (especially if you know nothing about linux kernel development, kind of like me)

## GPIOs

This section describes how to use the pins via the sysfs interface.

Create the pin’s interface:

```
echo 32 > /sys/class/gpio/export # PB0, green cable

```

which creates the interface’s pin directory:

```
cd /sys/class/gpio/gpio32/ 

```

Which contains the following files

```
active_low device direction edge power subsystem uevent value

```

To use this pin as an output pint you can configure it

```
echo out > /sys/class/gpio/gpio32/direction

```

And enable it

```
echo 1 > /sys/class/gpio/gpio32/value

```

Or disable it

```
echo 0 > /sys/class/gpio/gpio32/value

```

## Short version for I2C (Setup)

If you `mount /dev/mmcblk0p1 /mnt/boot/` you will find a file `sun8i-r16-clockworkpi-cpi3.dtb` which is missing the entry (in it’s original source form)

```auto
&i2c1 {
	status = "okay";
};

```

Which I rebuilt using the kernel sources version 5.2-rc4.  
I replaced the file (and the Kernel with my rebuilt kernel). And then got the device `/dev/i2c-0` after a reboot.

## Details for I2C

I wrote the long version down while figuring things out on my blog (it’s a bit much for a forum post):

[p3dt.net/post/2019/12/26/gameshell-gpios.html](https://p3dt.net/post/2019/12/26/gameshell-gpios.html)

Let me know if this helps or if you have any questions 🙂

## Resulting Kernel/DeviceTree files for I2C

If you don’t want to build everything yourself you can download my kernel and the accompanying files here:

[https://p3dt.net/assets/downloads/cpi-0.4-kernel-5.2-rc4-i2c.tar.gz](https://p3dt.net/assets/downloads/cpi-0.4-kernel-5.2-rc4-i2c.tar.gz)  
Using these files is at your own risk, if you run into issues, I will do my best to answer your questions in this thread.

You need to replace these (make backups first!) on the boot partition.  
**Note:** It might be the case that replacing the `.dtb` file is sufficient if you have a different kernel, but I haven’t tested this. If you are up for some tests, try this first and let us know, thank you.

Have fun,  
Paul

---

<div class="post-metadata">

**Author:** ![p3dt](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@p3dt](https://forum.clockworkpi.com/u/p3dt)\
**Post date:** [December 27, 2019, 3:51pm UTC](https://forum.clockworkpi.com/t/howto-gpio-access-i2c-kernel-building/5036/2 "2019-12-27T15:51:17Z")

</div>

The GPIO Table is as follows:

| Type | Pin # (ext.) | Pin # (Package) | Function 1 | Function 2 | Pin # (sysfs) | Color |
| --- | --- | --- | --- | --- | --- | --- |
| 3V0 | 1 | | | | | _blue_ |
| GPIO | 2 | PB0 | UART_0/2_\_TX | PB-EINT0 | 32 | _green_ |
| GPIO | 3 | PB1 | UART_0/2_\_RX | PB-EINT1 | 33 | _yellow_ |
| GND | 4 | | | | | _white_ |
| GPIO | 5 | PH **5** | I2C1-SDA | | 22 **9** | _red_ |
| GPIO | 6 | PH **4** | I2C1-SCL | | 22 **8** | _brown_ |
| GND | 7 | | | | | _black_ |
| GPIO | 8 | PH6 | UART3-TX | SPI0-CS | 230 | _blue_ |
| GPIO | 9 | PH7 | UART3-RX | SPI0-CLK | 231 | _green_ |
| GPIO | 10 | PH **9** | UART3-CTS | SPI0-MISO | 23 **3** | _yellow_ |
| GPIO | 11 | PH **8** | UART3-RTS | SPI0-MOSI | 23 **2** | _white_ |
| GND | 12 | | | | | _red_ |
| 5V0 | 13 | | | | | _brown_ |
| 5V0 | 14 | | | | | _black_ |

This should give you an orientation:  
 ![03-on.sm](https://canada1.discourse-cdn.com/flex029/uploads/clockworkpi/original/2X/3/39f4adade2ebb0909ae46968716b35ab464be487.jpeg)

---

<div class="post-metadata">

**Author:** ![0x0c](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/0x0c/32/3515_2.png) [@0x0c](https://forum.clockworkpi.com/u/0x0c)\
**Post date:** [December 27, 2019, 4:34pm UTC](https://forum.clockworkpi.com/t/howto-gpio-access-i2c-kernel-building/5036/3 "2019-12-27T16:34:34Z")

</div>

OMG you made it!!!

I am being occupied by other projects hence no bandwidth for me to follow up on this topic…

_ **just a quick question: What did you changed in kernel for enable I2C support?** _ - Pardon me, just found it recorded in your blog.

I may not able to use your kernel because of mine is customized as well, so I need to figure out what has been changed.

* * *

My blueprint is integrate CPI with a CO2 sensor / PM2.5 sensor, seems we got a chance to make it works by some effort.

---

<div class="post-metadata">

**Author:** ![p3dt](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@p3dt](https://forum.clockworkpi.com/u/p3dt)\
**Post date:** [December 27, 2019, 5:01pm UTC](https://forum.clockworkpi.com/t/howto-gpio-access-i2c-kernel-building/5036/4 "2019-12-27T17:01:54Z")

</div>

try with exchanging only the dtb file, leave your kernel as is. in theory this should work to. as long as the i2c support is already built in which should be
