From 830e0e12736975e4da0440cc225ff6a4e98d45ca Mon Sep 17 00:00:00 2001 From: batt Date: Thu, 9 Oct 2008 14:04:34 +0000 Subject: [PATCH] Merged from external project: ********** r22497 | batt | 2008-10-08 19:01:03 +0200 (Wed, 08 Oct 2008) | 1 line Add first skel of PCF8574 driver. ********** git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1887 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/drv/pcf8574.c | 82 ++++++++++++++++++++++++++++++++++++++++++++ bertos/drv/pcf8574.h | 59 +++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 bertos/drv/pcf8574.c create mode 100644 bertos/drv/pcf8574.h diff --git a/bertos/drv/pcf8574.c b/bertos/drv/pcf8574.c new file mode 100644 index 00000000..a9ed659e --- /dev/null +++ b/bertos/drv/pcf8574.c @@ -0,0 +1,82 @@ +/** + * \file + * + * + * \brief PCF8574 i2c port expander driver. + * + * This driver controls the PCF8574. + * The PCF8574 is an 8bit i2c port expander. + * You can read/write 8 pins through an i2c bus. + * The pins are quasi-bidirectionals, this mean that + * without the need of a direction register you can use + * each pin as input or output, see datasheet on how this + * is achieved. + * + * \version $Id: ft245rl.c 22301 2008-09-09 16:53:17Z batt $ + * \author Francesco Sacchi + */ + +#include "pcf8574.h" +#include + +/** + * Read PCF8574 \a pcf bit status. + * \return the pins status or EOF on errors. + */ +int pcf8574_get(Pcf8574 *pcf) +{ + if (!i2c_start_r(PCF8574ID | ((pcf->addr << 1) & 0xF7))) + return EOF; + + int data = i2c_get(false); + i2c_stop(); + return data; +} + +/** + * Write to PCF8574 \a pcf port \a data. + * \return true if ok, false on errors. + */ +bool pcf8574_put(Pcf8574 *pcf, uint8_t data) +{ + return i2c_start_w(PCF8574ID | ((pcf->addr << 1) & 0xF7)) && i2c_put(data) && i2c_stop(); +} + +/** + * 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) +{ + MOD_CHECK(i2c); + pcf->addr = addr; + return pcf8574_get(pcf) != EOF; +} diff --git a/bertos/drv/pcf8574.h b/bertos/drv/pcf8574.h new file mode 100644 index 00000000..7d53d95e --- /dev/null +++ b/bertos/drv/pcf8574.h @@ -0,0 +1,59 @@ +/** + * \file + * + * + * \brief PCF8574 i2c port expander driver (interface). + * + * \version $Id: ft245rl.c 22301 2008-09-09 16:53:17Z batt $ + * \author Francesco Sacchi + */ + +#ifndef DRV_PCF8574_H +#define DRV_PCF8574_H + +#include + +typedef uint8_t pcf8574_addr; + +/** + * Context for accessing a PCF8574. + */ +typedef struct Pcf8574 +{ + pcf8574_addr addr; +} Pcf8574; + + +int pcf8574_get(Pcf8574 *pcf); +bool pcf8574_put(Pcf8574 *pcf, uint8_t data); +bool pcf8574_init(Pcf8574 *pcf, pcf8574_addr addr); + +#endif /* DRV_PCF8574_H */ -- 2.25.1