feat: generic I2C and PCA95xx driver (#5233)
Co-authored-by: Peter Feerick <peter.feerick@gmail.com>
This commit is contained in:
co-authored by
Peter Feerick
parent
08c90dc39c
commit
b71d2a0228
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTx
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "pca95xx.h"
|
||||
|
||||
#define REG_INPUT 0x00
|
||||
#define REG_OUTPUT 0x02
|
||||
#define REG_POLARITY 0x04
|
||||
#define REG_DIRECTION 0x06
|
||||
|
||||
static int pca95xx_i2c_write16(pca95xx_t* dev, uint16_t reg, uint16_t value)
|
||||
{
|
||||
if (!dev->addr) return -1;
|
||||
|
||||
if (i2c_write(dev->bus, dev->addr, reg, 1, (uint8_t*)&value, sizeof(value)) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pca95xx_init(pca95xx_t* dev, etx_i2c_bus_t bus, uint16_t addr)
|
||||
{
|
||||
dev->bus = 0;
|
||||
dev->addr = 0;
|
||||
dev->polarity = 0;
|
||||
dev->direction = 0xFFFF;
|
||||
dev->output = 0;
|
||||
|
||||
if (i2c_init(bus) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (i2c_dev_ready(bus, addr) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
dev->bus = bus;
|
||||
dev->addr = addr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pca95xx_set_direction(pca95xx_t* dev, uint16_t mask, uint16_t dir)
|
||||
{
|
||||
uint16_t tmp = dev->direction;
|
||||
tmp &= ~mask;
|
||||
tmp |= dir & mask;
|
||||
|
||||
if (pca95xx_i2c_write16(dev, REG_DIRECTION, tmp) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
dev->direction = tmp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pca95xx_set_polarity(pca95xx_t* dev, uint16_t mask, uint16_t dir)
|
||||
{
|
||||
uint16_t tmp = dev->polarity;
|
||||
tmp &= ~mask;
|
||||
tmp |= dir & mask;
|
||||
|
||||
if (pca95xx_i2c_write16(dev, REG_POLARITY, tmp) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
dev->polarity = tmp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pca95xx_write(pca95xx_t* dev, uint16_t mask, uint16_t value)
|
||||
{
|
||||
uint16_t tmp = dev->output;
|
||||
tmp &= ~mask;
|
||||
tmp |= value & mask;
|
||||
|
||||
if (pca95xx_i2c_write16(dev, REG_OUTPUT, tmp) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
dev->output = tmp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pca95xx_read(pca95xx_t* dev, uint16_t mask, uint16_t* value)
|
||||
{
|
||||
if (!dev->addr) return -1;
|
||||
|
||||
uint16_t tmp;
|
||||
if (i2c_read(dev->bus, dev->addr, REG_INPUT, 1, (uint8_t*)&tmp, 2) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
*value = tmp & mask;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTx
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hal/i2c_driver.h"
|
||||
|
||||
#define PCA95XX_PIN_0 (1 << 0)
|
||||
#define PCA95XX_PIN_1 (1 << 1)
|
||||
#define PCA95XX_PIN_2 (1 << 2)
|
||||
#define PCA95XX_PIN_3 (1 << 3)
|
||||
#define PCA95XX_PIN_4 (1 << 4)
|
||||
#define PCA95XX_PIN_5 (1 << 5)
|
||||
#define PCA95XX_PIN_6 (1 << 6)
|
||||
#define PCA95XX_PIN_7 (1 << 7)
|
||||
#define PCA95XX_PIN_8 (1 << 8)
|
||||
#define PCA95XX_PIN_9 (1 << 9)
|
||||
#define PCA95XX_PIN_10 (1 << 10)
|
||||
#define PCA95XX_PIN_11 (1 << 11)
|
||||
#define PCA95XX_PIN_12 (1 << 12)
|
||||
#define PCA95XX_PIN_13 (1 << 13)
|
||||
#define PCA95XX_PIN_14 (1 << 14)
|
||||
#define PCA95XX_PIN_15 (1 << 15)
|
||||
|
||||
typedef struct {
|
||||
etx_i2c_bus_t bus;
|
||||
uint16_t addr;
|
||||
uint16_t polarity;
|
||||
uint16_t direction;
|
||||
uint16_t output;
|
||||
} pca95xx_t;
|
||||
|
||||
int pca95xx_init(pca95xx_t* dev, etx_i2c_bus_t bus, uint16_t addr);
|
||||
int pca95xx_set_direction(pca95xx_t* dev, uint16_t mask, uint16_t dir);
|
||||
int pca95xx_set_polarity(pca95xx_t* dev, uint16_t mask, uint16_t dir);
|
||||
int pca95xx_write(pca95xx_t* dev, uint16_t mask, uint16_t value);
|
||||
int pca95xx_read(pca95xx_t* dev, uint16_t mask, uint16_t* value);
|
||||
Reference in New Issue
Block a user