Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PCF8574 #1

Open
wants to merge 1 commit into
base: PinExt3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions FluidNC/src/Extenders/I2CExtender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Extenders {
EnumItem i2cDevice[] = { { int(I2CExtenderDevice::PCA9539), "pca9539" },
{ int(I2CExtenderDevice::PCA9555), "pca9555" },
{ int(I2CExtenderDevice::PCF8574), "pcf8574" },
EnumItem(int(I2CExtenderDevice::Unknown)) };

I2CExtender::I2CExtender() : _i2cBus(nullptr), _usedIORegisters(0), _dirtyWriteBuffer(0), _dirtyWrite(0), _status(0) {}
Expand Down Expand Up @@ -353,6 +354,15 @@ namespace Extenders {
_invertReg = 4;
_operationReg = 6;
break;

case I2CExtenderDevice::PCF8574:
_address = 0x20 + _deviceId;
_ports = 8;
_inputReg = 0;
_outputReg = 2;
_invertReg = 4;
_operationReg = 6;
break;

default:
Assert(false, "Pin extender device is not supported!");
Expand Down
2 changes: 2 additions & 0 deletions FluidNC/src/Extenders/I2CExtender.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
namespace Pins {
class PCA9539PinDetail;
class PCA9555PinDetail;
class PCF8574PinDetail;
}

namespace Extenders {
enum class I2CExtenderDevice {
Unknown,
PCA9539,
PCA9555,
PCF8574,
};

// Pin extenders...
Expand Down
271 changes: 271 additions & 0 deletions FluidNC/src/Extenders/PCF8574.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
// Copyright (c) 2021 - Stefan de Bruijn
// Use of this source code is governed by a GPLv3 license that can be found in the LICENSE file.

#include "Extenders.h"
#include "PCF8574.h"
#include "../Logging.h"

#include <esp32-hal-gpio.h>
#include <freertos/FreeRTOS.h>

namespace Extenders {
void PCF8574::claim(pinnum_t index) {
Assert(index >= 0 && index < 64, "PCF8574 IO index should be [0-63]; %d is out of range", index);

uint64_t mask = uint64_t(1) << index;
Assert((_claimed & mask) == 0, "PCF8574 IO port %d is already used.", index);

_claimed |= mask;
}

void PCF8574::free(pinnum_t index) {
uint64_t mask = uint64_t(1) << index;
_claimed &= ~mask;
}

uint8_t PCF8574::I2CGetValue(Machine::I2CBus* bus, uint8_t address, uint8_t reg) {
auto err = bus->write(address, &reg, 1);

if (err) {
// log_info("Error writing to i2c bus. Code: " << err);
return 0;
}

uint8_t inputData;
if (bus->read(address, &inputData, 1) != 1) {
// log_info("Error reading from i2c bus.");
}

return inputData;
}

void PCF8574::I2CSetValue(Machine::I2CBus* bus, uint8_t address, uint8_t reg, uint8_t value) {
uint8_t data[2];
data[0] = reg;
data[1] = uint8_t(value);
auto err = bus->write(address, data, 2);

if (err) {
log_error("Error writing to i2c bus; PCF8574 failed. Code: " << err);
}
}

void PCF8574::validate() const {
auto i2c = config->_i2c;
Assert(i2c != nullptr, "PCF8574 works through I2C, but I2C is not configured.");
}

void PCF8574::group(Configuration::HandlerBase& handler) {
handler.item("interrupt0", _isrData[0]._pin);
handler.item("interrupt1", _isrData[1]._pin);
handler.item("interrupt2", _isrData[2]._pin);
handler.item("interrupt3", _isrData[3]._pin);
}

void PCF8574::isrTaskLoop(void* arg) {
auto inst = static_cast<PCF8574*>(arg);
while (true) {
void* ptr;
if (xQueueReceive(inst->_isrQueue, &ptr, portMAX_DELAY)) {
ISRData* valuePtr = static_cast<ISRData*>(ptr);
// log_info("PCA state change ISR");
valuePtr->updateValueFromDevice();
}
}
}

void PCF8574::init() {
this->_i2cBus = config->_i2c;

_isrQueue = xQueueCreate(16, sizeof(void*));
xTaskCreatePinnedToCore(isrTaskLoop, // task
"isr_handler", // name for task
configMINIMAL_STACK_SIZE + 256, // size of task stack
this, // parameters
1, // priority
&_isrHandler,
SUPPORT_TASK_CORE // core
);

for (int i = 0; i < 8; ++i) {
auto& data = _isrData[i];

data._address = uint8_t(0x20 + i);
data._container = this;
data._valueBase = reinterpret_cast<volatile uint16_t*>(&_value) + i;

// Update the value first by reading it:
data.updateValueFromDevice();

if (!data._pin.undefined()) {
data._pin.setAttr(Pin::Attr::ISR | Pin::Attr::Input);

// The interrupt pin is 'active low'. So if it falls, we're interested in the new value.
data._pin.attachInterrupt(updatePCAState, FALLING, &data);
} else {
// Reset valueBase so we know it's not bound to an ISR:
data._valueBase = nullptr;
}
}
}

void PCF8574::ISRData::updateValueFromDevice() {
const uint8_t InputReg = 0;
auto i2cBus = _container->_i2cBus;

auto r1 = I2CGetValue(i2cBus, _address, InputReg);
auto r2 = I2CGetValue(i2cBus, _address, InputReg + 1);
uint16_t oldValue = *_valueBase;
uint16_t value = (uint16_t(r2) << 8) | uint16_t(r1);
*_valueBase = value;

if (_hasISR) {
for (int i = 0; i < 16; ++i) {
uint16_t mask = uint16_t(1) << i;

if (_isrCallback[i] != nullptr && (oldValue & mask) != (value & mask)) {
// log_info("State change pin " << i);
switch (_isrMode[i]) {
case RISING:
if ((value & mask) == mask) {
_isrCallback[i](_isrArgument);
}
break;
case FALLING:
if ((value & mask) == 0) {
_isrCallback[i](_isrArgument);
}
break;
case CHANGE:
_isrCallback[i](_isrArgument);
break;
}
}
}
}
}

void PCF8574::updatePCAState(void* ptr) {
ISRData* valuePtr = static_cast<ISRData*>(ptr);

BaseType_t xHigherPriorityTaskWoken = false;
xQueueSendFromISR(valuePtr->_container->_isrQueue, &valuePtr, &xHigherPriorityTaskWoken);
}

void PCF8574::setupPin(pinnum_t index, Pins::PinAttributes attr) {
bool activeLow = attr.has(Pins::PinAttributes::ActiveLow);
bool output = attr.has(Pins::PinAttributes::Output);

uint64_t mask = uint64_t(1) << index;
_invert = (_invert & ~mask) | (activeLow ? mask : 0);
_configuration = (_configuration & ~mask) | (output ? 0 : mask);

const uint8_t deviceId = index / 16;

const uint8_t ConfigReg = 6;
uint8_t address = 0x20 + deviceId;

uint8_t value = uint8_t(_configuration >> (8 * (index / 8)));
uint8_t reg = ConfigReg + ((index / 8) & 1);

// log_info("Setup reg " << int(reg) << " with value " << int(value));

I2CSetValue(_i2cBus, address, reg, value);
}

void PCF8574::writePin(pinnum_t index, bool high) {
uint64_t mask = uint64_t(1) << index;
uint64_t oldVal = _value;
uint64_t newVal = high ? mask : uint64_t(0);
_value = (_value & ~mask) | newVal;

_dirtyRegisters |= ((_value != oldVal) ? 1 : 0) << (index / 8);
}

bool PCF8574::readPin(pinnum_t index) {
uint8_t reg = uint8_t(index / 8);
uint8_t deviceId = reg / 2;

// If it's handled by the ISR, we don't need to read anything from the device.
// Otherwise, we do. Check:
if (_isrData[deviceId]._valueBase == nullptr) {
const uint8_t InputReg = 0;
uint8_t address = 0x20 + deviceId;

auto readReg = InputReg + (reg & 1);
auto value = I2CGetValue(_i2cBus, address, readReg);
uint64_t newValue = uint64_t(value) << (int(reg) * 8);
uint64_t mask = uint64_t(0xff) << (int(reg) * 8);

_value = ((newValue ^ _invert) & mask) | (_value & ~mask);

// log_info("Read reg " << int(readReg) << " <- value " << int(newValue) << " gives " << int(_value));
}
// else {
// log_info("No read, value is " << int(_value));
// }

return (_value & (1ull << index)) != 0;
}

void PCF8574::flushWrites() {
uint64_t write = _value ^ _invert;
for (int i = 0; i < 8; ++i) {
if ((_dirtyRegisters & (1 << i)) != 0) {
const uint8_t OutputReg = 2;
uint8_t address = 0x20 + (i / 2);

uint8_t val = uint8_t(write >> (8 * i));
uint8_t reg = OutputReg + (i & 1);
I2CSetValue(_i2cBus, address, reg, val);
}
}

_dirtyRegisters = 0;
}

// ISR's:
void PCF8574::attachInterrupt(pinnum_t index, void (*callback)(void*), void* arg, int mode) {
int device = index / 16;
int pinNumber = index % 16;

Assert(_isrData[device]._isrCallback[pinNumber] == nullptr, "You can only set a single ISR for pin %d", index);

_isrData[device]._isrCallback[pinNumber] = callback;
_isrData[device]._isrArgument[pinNumber] = arg;
_isrData[device]._isrMode[pinNumber] = mode;
_isrData[device]._hasISR = true;
}

void PCF8574::detachInterrupt(pinnum_t index) {
int device = index / 16;
int pinNumber = index % 16;

_isrData[device]._isrCallback[pinNumber] = nullptr;
_isrData[device]._isrArgument[pinNumber] = nullptr;
_isrData[device]._isrMode[pinNumber] = 0;

bool hasISR = false;
for (int i = 0; i < 16; ++i) {
hasISR |= (_isrData[device]._isrArgument[i] != nullptr);
}
_isrData[device]._hasISR = hasISR;
}

const char* PCF8574::name() const { return "PCF8574"; }

PCF8574 ::~PCF8574() {
for (int i = 0; i < 4; ++i) {
auto& data = _isrData[i];

if (!data._pin.undefined()) {
data._pin.detachInterrupt();
}
}
}

// Register extender:
namespace {
PinExtenderFactory::InstanceBuilder<PCF8574> registration("PCF8574");
}
}
Loading