X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fdrv%2Fpcf8574.c;h=a94b2b7d432015cd8163d1faa6b1f95a269f021c;hb=39184f9b190fdcfec2f17aad6f0de8b2edf04a02;hp=5572e40ce3ce29803315fa3bfc3b45d541d7f8ed;hpb=a9d4f6bb2eb2066c32404cb517d84dadd2161b24;p=bertos.git diff --git a/bertos/drv/pcf8574.c b/bertos/drv/pcf8574.c index 5572e40c..a94b2b7d 100644 --- a/bertos/drv/pcf8574.c +++ b/bertos/drv/pcf8574.c @@ -44,33 +44,44 @@ */ #include "pcf8574.h" + +#include "cfg/cfg_i2c.h" + #include + #include -/** - * Read PCF8574 \a pcf bit status. - * \return the pins status or EOF on errors. - */ -int pcf8574_get(Pcf8574 *pcf) +#if !CONFIG_I2C_DISABLE_OLD_API + +INLINE int pcf8574_get_priv(Pcf8574 *pcf) { if (!i2c_start_r(PCF8574ID | ((pcf->addr << 1) & 0xF7))) return EOF; - int data; - + int data; + if (!i2c_recv(&data, 1)) data = EOF; - + i2c_stop(); - + return data; } +/** + * Read PCF8574 \a pcf bit status. + * \return the pins status or EOF on errors. + */ +int pcf8574_get_1(Pcf8574 *pcf) +{ + return pcf8574_get_priv(pcf); +} + /** * Write to PCF8574 \a pcf port \a data. * \return true if ok, false on errors. */ -bool pcf8574_put(Pcf8574 *pcf, uint8_t data) +bool pcf8574_put_2(Pcf8574 *pcf, uint8_t data) { bool res = i2c_start_w(PCF8574ID | ((pcf->addr << 1) & 0xF7)) && i2c_put(data); i2c_stop(); @@ -81,9 +92,59 @@ bool pcf8574_put(Pcf8574 *pcf, uint8_t data) * Init a PCF8574 on the bus with addr \a addr. * \return true if device is found, false otherwise. */ -bool pcf8574_init(Pcf8574 *pcf, pcf8574_addr addr) +bool pcf8574_init_2(Pcf8574 *pcf, pcf8574_addr addr) { MOD_CHECK(i2c); pcf->addr = addr; - return pcf8574_get(pcf) != EOF; + return pcf8574_get_priv(pcf) != EOF; +} +#endif /* !CONFIG_I2C_DISABLE_OLD_API */ + + +/* + * New API + */ + +/** + * Read PCF8574 \a pcf bit status. + * \return the pins status or EOF on errors. + */ +int pcf8574_get_2(I2c *i2c, Pcf8574 *pcf) +{ + i2c_start_r(i2c, PCF8574ID | ((pcf->addr << 1) & 0xF7), 1, I2C_STOP); + + int data = i2c_getc(i2c); + + if (i2c_error(i2c)) + data = EOF; + + return data; +} + +/** + * Write to PCF8574 \a pcf port \a data. + * \return true if ok, false on errors. + */ +bool pcf8574_put_3(I2c *i2c, Pcf8574 *pcf, uint8_t data) +{ + i2c_start_w(i2c, PCF8574ID | ((pcf->addr << 1) & 0xF7), 1, I2C_STOP); + i2c_putc(i2c, data); + + if (i2c_error(i2c)) + return false; + + return true; +} + +/** + * Init a PCF8574 on the bus with addr \a addr. + * \return true if device is found, false otherwise. + */ +bool pcf8574_init_3(I2c *i2c, Pcf8574 *pcf, pcf8574_addr addr) +{ + ASSERT(i2c); + pcf->addr = addr; + + return (pcf8574_get(i2c, pcf) != EOF); } +