Merge pull request #107 from UncleRus/extras/pcf8574

Driver for PCF8574 (8-bit I2C GPIO expander)
This commit is contained in:
Alex Stewart 2016-03-07 19:06:09 -08:00
commit 6c50e968f5
3 changed files with 131 additions and 0 deletions

View file

@ -0,0 +1,6 @@
# Component makefile for extras/pcf8574
INC_DIRS += $(pcf8574_ROOT)..
pcf8574_SRC_DIR = $(pcf8574_ROOT)
$(eval $(call component_compile_rules,pcf8574))

54
extras/pcf8574/pcf8574.c Normal file
View file

@ -0,0 +1,54 @@
#include "pcf8574.h"
#include <i2c/i2c.h>
uint8_t pcf8574_port_read(uint8_t addr)
{
i2c_start();
uint8_t res = i2c_write((addr << 1) | 1) ? i2c_read(1) : 0;
i2c_stop();
return res;
}
size_t pcf8574_port_read_buf(uint8_t addr, void *buf, size_t len)
{
if (!len || !buf) return 0;
uint8_t *_buf = (uint8_t *)buf;
i2c_start();
if (!i2c_write((addr << 1) | 1)) return 0;
for (size_t i = 0; i < len; i++)
*_buf++ = i2c_read(i == len - 1);
i2c_stop();
return len;
}
size_t pcf8574_port_write_buf(uint8_t addr, void *buf, size_t len)
{
if (!len || !buf) return 0;
uint8_t *_buf = (uint8_t *)buf;
i2c_start();
if (!i2c_write(addr << 1)) return 0;
for (size_t i = 0; i < len; i++)
i2c_write(*_buf++);
return len;
}
void pcf8574_port_write(uint8_t addr, uint8_t value)
{
i2c_start();
if (i2c_write(addr << 1)) i2c_write(value);
i2c_stop();
}
bool pcf8574_gpio_read(uint8_t addr, uint8_t num)
{
return (bool)((pcf8574_port_read(addr) >> num) & 1);
}
void pcf8574_gpio_write(uint8_t addr, uint8_t num, bool value)
{
uint8_t bit = (uint8_t)value << num;
uint8_t mask = ~(1 << num);
pcf8574_port_write (addr, (pcf8574_port_read(addr) & mask) | bit);
}

71
extras/pcf8574/pcf8574.h Normal file
View file

@ -0,0 +1,71 @@
/**
* \file Driver for PCF8574 compartible remote 8-bit I/O expanders for I2C-bus
* \author Ruslan V. Uss
*/
#ifndef PCF8574_PCF8574_H_
#define PCF8574_PCF8574_H_
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
/**
* \brief Read GPIO port value
* \param addr I2C register address (0b0100<A2><A1><A0> for PCF8574)
* \return 8-bit GPIO port value
*/
uint8_t pcf8574_port_read(uint8_t addr);
/**
* \brief Continiously read GPIO port values to buffer
* @param addr I2C register address (0b0100<A2><A1><A0> for PCF8574)
* @param buf Target buffer
* @param len Buffer length
* @return Number of bytes read
*/
size_t pcf8574_port_read_buf(uint8_t addr, void *buf, size_t len);
/**
* \brief Write value to GPIO port
* \param addr I2C register address (0b0100<A2><A1><A0> for PCF8574)
* \param value GPIO port value
*/
void pcf8574_port_write(uint8_t addr, uint8_t value);
/**
* \brief Continiously write GPIO values to GPIO port
* \param addr I2C register address (0b0100<A2><A1><A0> for PCF8574)
* @param buf Buffer with values
* @param len Buffer length
* @return Number of bytes written
*/
size_t pcf8574_port_write_buf(uint8_t addr, void *buf, size_t len);
/**
* \brief Read input value of a GPIO pin
* \param addr I2C register address (0b0100<A2><A1><A0> for PCF8574)
* \param num pin number (0..7)
* \return GPIO pin value
*/
bool pcf8574_gpio_read(uint8_t addr, uint8_t num);
/**
* \brief Set GPIO pin output
* Note this is READ - MODIFY - WRITE operation! Please read PCF8574
* datasheet first.
* \param addr I2C register address (0b0100<A2><A1><A0> for PCF8574)
* \param num pin number (0..7)
* \param value true for high level
*/
void pcf8574_gpio_write(uint8_t addr, uint8_t num, bool value);
#ifdef __cplusplus
}
#endif
#endif /* PCF8574_PCF8574_H_ */