From 2a8e7fefd3243a8950b4fbfe06e38a5f670ccdc2 Mon Sep 17 00:00:00 2001 From: mstroh76 Date: Fri, 31 May 2024 17:49:59 +0200 Subject: [PATCH] #250 --- wiringPi/pcf8574.c | 9 +- wiringPi/test/Makefile | 27 ++++- wiringPi/test/wiringpi_i2c_test1_pcf8574.c | 112 +++++++++++++++++++++ 3 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 wiringPi/test/wiringpi_i2c_test1_pcf8574.c diff --git a/wiringPi/pcf8574.c b/wiringPi/pcf8574.c index 402257e..61b4cc7 100644 --- a/wiringPi/pcf8574.c +++ b/wiringPi/pcf8574.c @@ -1,7 +1,7 @@ /* * pcf8574.c: * Extend wiringPi with the PCF8574 I2C GPIO expander chip - * Copyright (c) 2013 Gordon Henderson + * Copyright (c) 2013-2024 Gordon Henderson and contributors *********************************************************************** * This file is part of wiringPi: * https://github.com/WiringPi/WiringPi/ @@ -33,8 +33,9 @@ /* * myPinMode: - * The PCF8574 is an odd chip - the pins are effectively bi-directional, - * however the pins should be drven high when used as an input pin... + * The PCF8574 is a 8-Bit I/O Expander with Open-drain output. + * The pins are effectively bi-directional, + * however the pins should be driven high when used as an input pin... * So, we're effectively copying digitalWrite... ********************************************************************************* */ @@ -102,7 +103,7 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) * pcf8574Setup: * Create a new instance of a PCF8574 I2C GPIO interface. We know it * has 8 pins, so all we need to know here is the I2C address and the - * user-defined pin base. + * user-defined pin base. Default address (A0-A3 low) is 0x20. ********************************************************************************* */ diff --git a/wiringPi/test/Makefile b/wiringPi/test/Makefile index d144baf..cbcd3d8 100644 --- a/wiringPi/test/Makefile +++ b/wiringPi/test/Makefile @@ -6,7 +6,9 @@ tests = wiringpi_test1_sysfs wiringpi_test2_sysfs wiringpi_test3_device_wpi wiri xotests = wiringpi_xotest_test1_spi -all: $(tests) $(xotests) +i2ctests = wiringpi_i2c_test1_pcf8574 + +all: $(tests) $(xotests) $(i2ctests) wiringpi_test1_sysfs: ${CC} ${CFLAGS} wiringpi_test1_sysfs.c -o wiringpi_test1_sysfs -lwiringPi @@ -32,6 +34,9 @@ wiringpi_test7_version: wiringpi_xotest_test1_spi: ${CC} ${CFLAGS} wiringpi_xotest_test1_spi.c -o wiringpi_xotest_test1_spi -lwiringPi +wiringpi_i2c_test1_pcf8574: + ${CC} ${CFLAGS} wiringpi_i2c_test1_pcf8574.c -o wiringpi_i2c_test1_pcf8574 -lwiringPi + test: @error_state=false ; \ for t in $(tests) ; do \ @@ -51,7 +56,7 @@ test: xotest: @error_state=false ; \ for t in $(tests) $(xotests) ; do \ - echo === unit test: $${t} === ; \ + echo === XO unit test: $${t} === ; \ time ./$${t} ; \ if [ $$? -ne 0 ]; then \ error_state=true ; \ @@ -64,7 +69,23 @@ xotest: echo "\n\e[5mSTD/XO TEST SUCCESS\e[0m\n"; \ fi +i2ctest: + @error_state=false ; \ + for t in $(tests) $(i2ctests) ; do \ + echo === I2C unit test: $${t} === ; \ + time ./$${t} ; \ + if [ $$? -ne 0 ]; then \ + error_state=true ; \ + fi ; \ + echo ; echo ; \ + done + if [ "$$error_state" = true ]; then \ + echo "\n\e[5mSTD/I2C TEST FAILED\e[0m\n"; \ + else \ + echo "\n\e[5mSTD/I2C TEST SUCCESS\e[0m\n"; \ + fi + clean: - for t in $(tests) $(xotests) ; do \ + for t in $(tests) $(xotests) $(i2ctests) ; do \ rm -fv $${t} ; \ done diff --git a/wiringPi/test/wiringpi_i2c_test1_pcf8574.c b/wiringPi/test/wiringpi_i2c_test1_pcf8574.c new file mode 100644 index 0000000..19a699f --- /dev/null +++ b/wiringPi/test/wiringpi_i2c_test1_pcf8574.c @@ -0,0 +1,112 @@ +// WiringPi test program: I2C functions (need PCF8574) +// Compile: gcc -Wall wiringpi_i2c_test1.c -o wiringpi_i2c_test1 -lwiringPi + +#include "wpi_test.h" +#include "pcf8574.h" +#include "wiringPiI2C.h" + +const int pinBase = 1020; +const int i2cAdress = 0x20; + +int ShowAll() { + int in; + int value = 0; + + printf("pin: 0 1 2 3 4 5 6 7\nval: "); + for (int pin=0; pin<=7; ++pin) { + in = digitalRead(pinBase + pin); + printf("%d ", in); + if(in==HIGH) { value |= (0x01<