# Using gpiolcd on a 16x2 LCD



## Phishfry (Nov 20, 2022)

I am attempting to use Andriy's driver on a i2c controlled 16 Character LED with 2 Lines.
The driver source is one file and I have copied it to a BeagleBone where I have a working i2c bus on /dev/iic1









						GitHub - avg-I/gpiolcd: Utility for driving HD44780 character LCD via GPIO on FreeBSD
					

Utility for driving HD44780 character LCD via GPIO on FreeBSD - GitHub - avg-I/gpiolcd: Utility for driving HD44780 character LCD via GPIO on FreeBSD




					github.com
				




I copied this source file into /usr/src/sys/dev/gpio/ on a Beaglebone with FreeBSD 13.1-RELEASE on eMMC.
Still need to compile it.

I have enabled pfc8574(4) per AVG's instructions:








						Recon Sentinel is Rock in a box.
					

Got my Rock64 in the mail today. eMMC and flasher left China too from Pine store. This Recon Sentinel is a nice implementation. Box, manual, AC Cord. Retail package. $40 new off ebay. https://hackaday.com/2020/11/16/teardown-recon-sentinel/  Flash uboot to SPI and FreeBSD to eMMC.  Figure out...




					forums.freebsd.org
				




I created an overlay for it after using `i2c -s -f /dev/iic1` to determine the i2c address in use by my module.

```
/ {

         ...
         pcf8574@27    {
             compatible    = "nxp,pcf8574";
             reg = <0x27>;
         };
     };
```

So what now?

Do I need to create an overlay for the 16x2 display?

This seems like they created a combined dts with pfc8574 i2c controller and hd44780 display.








						pcf8574-hd44780-overlay/pcf8574-hd44780.dts at master · oniongarlic/pcf8574-hd44780-overlay
					

Overlay for Hitachi HD44780 LCDs connected trough PCF8574 I2C GPIO chip - pcf8574-hd44780-overlay/pcf8574-hd44780.dts at master · oniongarlic/pcf8574-hd44780-overlay




					github.com
				




Any thoughts? Do I need the overlay for hd44780 or will gpioled suffice?

What about I2C bus speed. I might enable another bus. I am using ds1307 now for test.
Should I use the fast bus overlay for the pfc8574? 400K or 100K clock freq?








						bb.org-overlays/BB-I2C1-FAST-00A0.dts at master · beagleboard/bb.org-overlays
					

Device Tree Overlays for bb.org boards. Contribute to beagleboard/bb.org-overlays development by creating an account on GitHub.




					github.com


----------



## Andriy (Nov 22, 2022)

Just a note, gpiolcd is not a driver, it's a very simple command line utility (although with not much of documentation). It works with gpio. You should have gotten a new gpio device if pcf8574 attached correctly.
Hope that helps.


----------



## Phishfry (Nov 23, 2022)

Thank You so Much for this hint:


Andriy said:


> You should have gotten a new gpio device if pcf8574 attached correctly.



I now have something showing on the Raspberry Pi3.
DS3231 RTC on iic0 (pins gpio2 and gpio3) and pcf8574 on iic1 (pins gpio28 SDA0 and gpio29 SDL0)

```
gpio2: <PCF8574 I/O expander> at addr 0x4e on iicbus1
```

New gpioc2 bus showed up:

```
# gpioctl -f /dev/gpioc2 -lv
pin 00: -1      <>, caps:<>
pin 01: -1      <>, caps:<>
pin 02: -1      <>, caps:<>
pin 03: -1      <>, caps:<>
pin 04: -1      <>, caps:<>
pin 05: -1      <>, caps:<>
pin 06: -1      <>, caps:<>
pin 07: -1      <>, caps:<>
```

Recon Sentinal // Rock64 LCD is showing up as 3f and using an pcf8574A.
That threw me for a loop.

My pcf8574.dts:

```
/dts-v1/;
/plugin/;

&i2c1 {
        status = "okay";

        pcf8574@27 {
                compatible = "nxp,pcf8574";
                reg = <0x27>;
                clock-frequency = <400000>;
                };
};
```
`dtc -O dtb -o pcf8574.dtbo -b 0 -@ pcf8574.dts`

The Pi uses sysutils/rpi-firmware port for pre-compiled binaries for overlays.
Binary overlays go in /boot/msdos/overlays/
Configured with config.txt

```
dtparam=i2c_arm=on,i2c0=on,i2c1=on,i2c_arm_baudrate=400000,spi=on
dtoverlay=i2c0
```


----------



## ice051 (Dec 11, 2022)

Thanks Phishfry and Andriy for the tips.

My hardware is RPi 4B.

`uname -a`

```
FreeBSD generic 14.0-CURRENT FreeBSD 14.0-CURRENT #0 main-n259495-d1f3abc89250: Thu Dec  1 10:53:21 UTC 2022     root@releng1.nyi.freebsd.org:/usr/obj/usr/src/arm64.aarch64/sys/GENERIC arm64
```

I want to display text in 1602A.

Add pcf8574_load="YES" to loader.conf.

`dev`

```
nexus0
  ofwbus0
    psci0
    simplebus0
      bcm2835_clkman0
      mbox0
      gpio0
        gpiobus0
          owc0
            ow0
              ow_temp0
        gpioc0
      uart0
      spi0
        spibus0
      iichb0
        iicbus0
          iic0
      gic0
      simple_mfd0
      bcm_dma0
      bcmwd0
      bcmrng0
      bcm2835_firmware0
        ofw_clkbus1
        gpio1
          gpiobus1
          gpioc1
      sdhci_bcm0
        mmc0
      fb0
        fbd0
    ofw_clkbus0
      clk_fixed0
      clk_fixed1
    usb_nop_xceiv0
    clk_fixed2
    clk_fixed3
    simplebus1
      sdhci_bcm1
        mmc1
          mmcsd0
    pmu0
    generic_timer0
    cpulist0
      cpu0
        bcm2835_cpufreq0
        cpufreq0
      cpu1
      cpu2
      cpu3
    simplebus2
      pcib0
        pci0
          pcib1
            pci1
              bcm_xhci0
                usbus0
                  uhub0
                    uhub1
      genet0
        miibus0
          brgphy0
    gpioled0
    gpioregulator0
    regfix0
    regfix1
    regfix2
    simplebus3
  cryptosoft0
```

`dmesg | grep "gpio"`

```
gpio0: <BCM2708/2835 GPIO controller> mem 0x7e200000-0x7e2000b3 irq 14,15 on simplebus0
gpiobus0: <OFW GPIO bus> on gpio0
gpio1: <Raspberry Pi Firmware GPIO controller> on bcm2835_firmware0
gpiobus1: <GPIO bus> on gpio1
gpioregulator0: <GPIO controlled regulator> on ofwbus0
owc0: <GPIO one-wire bus> at pin 4 on gpiobus0
gpioc0: <GPIO controller> on gpio0
gpioc1: <GPIO controller> on gpio1
gpioled0: <GPIO LEDs> on ofwbus0
```

`gpioctl -f /dev/gpioc1 -vl`

```
pin 00: 1       BT_ON<IN>, caps:<IN,OUT>
pin 01: 1       WL_ON<IN>, caps:<IN,OUT>
pin 02: 0       PWR_LED_OFF<OUT>, caps:<IN,OUT>
pin 03: 1       GLOBAL_RESET<IN>, caps:<IN,OUT>
pin 04: 1       VDD_SD_IO_SEL<OUT>, caps:<IN,OUT>
pin 05: 1       CAM_GPIO<IN>, caps:<IN,OUT>
pin 06: 0       SD_PWR_ON<IN>, caps:<IN,OUT>
pin 07: 1       SD_OC_N<IN>, caps:<IN,OUT>
```

Use and compile 








						GitHub - avg-I/gpiolcd: Utility for driving HD44780 character LCD via GPIO on FreeBSD
					

Utility for driving HD44780 character LCD via GPIO on FreeBSD - GitHub - avg-I/gpiolcd: Utility for driving HD44780 character LCD via GPIO on FreeBSD




					github.com
				




`./a.out -f /dev/gpioc1 "sdfk"`

 There's no change at 1602A.
After a while it will show:

```
Connection to 192.168.0.104 closed by remote host.
```

When you log in again, the following message is displayed:

```
ssh: connect to host 192.168.0.104 port 22: Connection refused
```

You can log in through ssh only after the restart.

Next
`pkg install sysutils/rpi-firmware
pkg install rpi-firmware`

`cp -pr /usr/local/share/rpi-firmware/* /boot/msdos/
cp /boot/msdos/config_rpi4.txt /boot/msdos/config.txt`

Add the following to config.txt:

```
dtparam=i2c_arm=on,i2c0=on,i2c1=on,i2c_arm_baudrate=400000,spi=on
dtoverlay=i2c0
```
 
`./a.out -f /dev/gpioc1 "sdfk"`

There's no change at 1602A.
 However, ssh login is not automatically logged out

 What should I do


----------



## Phishfry (Dec 11, 2022)

ice051 said:


> i2c_arm=on,i2c0=on,i2c1=on,


You need to fire up sysutils/i2c-tools.
Use that to figure out what is where by scanning the buses.

You should see PFC8574 in dmesg when you have the lcd on the right bus.

I have found i2c naming very inconstant. Please consider turning on all i2c buses for your platform.

I never figured out this program. I got it up and running but couldn't figure out how to send text to display with arguments.

There is a possibility you need an overlay for the display too like the hd44780 overlay. I don't know for sure.


----------



## Phishfry (Dec 11, 2022)

ice051 said:


> iichb0 iicbus0 iic0


It looks like only one i2c bus is active.

HDMI requires i2c so make sure you use a different bus than that uses or disable HDMI if unused.

A scan with `i2c -s` should make things more apparent.

You are looking for 0x27 or 0x3f i2c address for the gpio extender to lcd controller.


----------



## Phishfry (Dec 11, 2022)

ice051 said:


> gpioctl -f /dev/gpioc1 -vl


Take a look at that output you posted.
Does that look like the output from a GPIO expander(PCF8574) ?
No that is the Pi's second bank of GPIO pins.
The PCF8574 will be the next GPIO bus once it shows up in dmesg.


----------



## Phishfry (Dec 11, 2022)

Just for clarity are you using Pin3=SDA and Pin5=SCL for your i2c?


----------



## Phishfry (Dec 11, 2022)

Be advised the above dts overlay I posted is using i2c1. You may need to adjust that for your board.
Pin27 and Pin28 on Pi are special and need muxing for i2c.


----------



## Phishfry (Dec 12, 2022)

I have found that decompiling the dtb to see what resides where is a very useful learning experience.

I am on BananaPi-M1 now and see two i2c busses with `ls /dev/iic*`
I have no idea where the pins are for this board.
`dtc -I dtb -O dts bananapi.dtb > bananapi.dts`

After decompiling the dtb I see that iic0 is the boards Power Management Unit.
iic1 is on Pin3 and Pin5 for user gadgets.

 # i2c -s
Scanning I2C devices on /dev/iic0: 34

 # i2c -f /dev/iic1 -s
Scanning I2C devices on /dev/iic1: 3f

I can see from the decompiled dtb that the 34 address is the PMU


----------



## Phishfry (Dec 12, 2022)

I still have not figured out how to display things with gpiolcd
I would assume I need to clear the screen and send text then end line.

`./gpiolcd -d -h 2 -w 16 -I 4 "\nTesting\r"`
hd44780: reset to 4-bit interface, 2 lines, 5x8 font, no cursor


----------



## Phishfry (Dec 12, 2022)

Well I just realized you must set the gpio bus in the program source.
Once I did that it shows errors now:

```
./gpiolcd -d -w 16 "testing"
hd44780: reset to 4-bit interface, 2 lines, 5x8 font, no cursor
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
hd44780_set_pin: error 92
```

From this I can see the device has changed:

```
# gpioctl -f /dev/gpioc2 -lv
pin 00: 1       P0<OUT>, caps:<IN,OUT>
pin 01: 0       P1<OUT>, caps:<IN,OUT>
pin 02: 0       P2<OUT>, caps:<IN,OUT>
pin 03: 0       P3<IN>, caps:<IN,OUT>
pin 04: 1       P4<OUT>, caps:<IN,OUT>
pin 05: 1       P5<OUT>, caps:<IN,OUT>
pin 06: 1       P6<OUT>, caps:<IN,OUT>
pin 07: 0       P7<OUT>, caps:<IN,OUT>
```


----------



## Phishfry (Dec 12, 2022)

Well I have characters coming up on Line 1 of the LCD now. They are not the correct characters but a start.


----------



## ice051 (Dec 12, 2022)

The i2c-tools pkg package does not contain `i2cdetect`. An error occurred when I compiled.

When I scanned it with `i2c-s`, I didn't find anything.

I am using Pin3=SDA and Pin5=SCL for i2c.

Yes, you're right.
gpioc1 is the Pi's second bank of GPIO pins。
When I reboot after unplugging from SDA and SCL, the output is the same:
`gpioctl -f /dev/gpioc1 -vl`

```
pin 00: 1       BT_ON<IN>, caps:<IN,OUT>
pin 01: 1       WL_ON<IN>, caps:<IN,OUT>
pin 02: 0       PWR_LED_OFF<OUT>, caps:<IN,OUT>
pin 03: 1       GLOBAL_RESET<IN>, caps:<IN,OUT>
pin 04: 1       VDD_SD_IO_SEL<OUT>, caps:<IN,OUT>
pin 05: 1       CAM_GPIO<IN>, caps:<IN,OUT>
pin 06: 0       SD_PWR_ON<IN>, caps:<IN,OUT>
pin 07: 1       SD_OC_N<IN>, caps:<IN,OUT>
```

I'll try the others later.

Lack of understanding of the overall architecture of embedded hardware, where can I find this information. I think I can learn from the linux literature and it will be easier to grasp the whole thing.


----------



## Andriy (Dec 12, 2022)

Phishfry said:


> Well I just realized you must set the gpio bus in the program source.


Wouldn't -f option work for that as well?


----------



## Phishfry (Dec 12, 2022)

Indeed it would. I caught that late..


----------



## Phishfry (Dec 12, 2022)

Are the ASCII control characters and escape sequences needed for command prompt arguments?
I am a bit lost by arguments.
Is quoted text OK as shown?


----------



## Andriy (Dec 12, 2022)

Phishfry said:


> Well I have characters coming up on Line 1 of the LCD now. They are not the correct characters but a start.


Have you checked the connections between the I2C port expander and the LCD proper?
Do you they match what gpiolcd expects? See https://github.com/avg-I/gpiolcd/blob/master/gpiolcd.c#L150


----------



## Phishfry (Dec 12, 2022)

ice051 said:


> When I scanned it with `i2c-s`, I didn't find anything.


Same as above. You need the -i2c -f flag with iic bus except for default iic0


----------



## Andriy (Dec 12, 2022)

Phishfry said:


> Are the ASCII control characters and escape sequences needed for command prompt arguments?
> I am a bit lost by arguments.
> Is quoted text OK as shown?


Yes, it's okay.


----------



## Phishfry (Dec 12, 2022)

I do have to figure out my devices 'lines' to pcf8574 . I will trace it out.


----------

