Skip to content
php-baremetal REGISTRY
Publish a package

php-baremetal/io-ch422g

bm-lib MIT

CH422G I/O expander driver for ext-i2c (Baremetal\I2c\Driver\Ch422g): eight bidirectional and four output pins, also named by other drivers in the board file ("exio2").

bus: i2c

A driver for the CH422G I/O expander (WCH) on I2C: eight pins IO7 to IO0 (exio7 to exio0), used as outputs, and four outputs OC3 to OC0 (oc3 to oc0). It registers into ext-i2c's i2c_drivers registry, so a firmware built with it has the class Baremetal\I2c\Driver\Ch422g, and into ext-gpio's pin_providers registry, so other drivers can take one of its pins from the board file by name wherever they take a GPIO number (a reset line, an enable, a chip select, a backlight): "rst": "exio1" in any block drives that line through the expander.

Install

bash
composer require php-baremetal/io-ch422g

ext-i2c, ext-gpio and bm-common come with it.

Example

php
<?php
use Baremetal\I2c\Driver\Ch422g;

$x = Ch422g::onboard();        // the board's 'expander' block, on the bus of its 'i2c' block
$x->set('exio2', false);       // drive IO2 low
$x->set('oc0', true);          // drive OC0 high
echo $x->get('exio2') ? 'high' : 'low', "\n";   // low

Without a board block: new Ch422g(new Bus(sda: 8, scl: 9), 'exio0,exio5').

How it works

The chip has no register map: each command is the first byte of a transfer, so it answers at 0x24 (system parameters), 0x38 (IO outputs), 0x23 (OC outputs) and 0x26 (IO read). The driver writes the outputs (all high except the board's inputs) and then sets IO7 to IO0 as outputs. Every write is a whole register, so the driver keeps a copy: set() changes one bit of it and writes the register again, and get() returns the level from the copy. The class and the pin provider share one copy per chip.

Inputs of the board

While IO7 to IO0 are outputs the chip cannot read them. Pins wired to signals coming in are listed in the board's expander.inputs (or the constructor's $inputs): they stay low, set() throws \LogicException, and so does get(), instead of returning a level it cannot know. For example, on the Waveshare ESP32-S3-Touch-LCD-5B these are exio0 and exio5 (the board's digital inputs DI0 and DI1).

Pins in the board file

A board describes the chip with "expander": { "type": "ch422g", "inputs": "exio0,exio5" } and its other blocks name its pins where they would put a GPIO number, as "rst": "exio1". The first pin used by another driver starts the chip (the bus of the i2c block). The pins are used from the PHP task only.

Documentation

License

MIT.

Read the full documentation