From 3b9b0e6a2cd670bedcb82414b4943d0cb12214e3 Mon Sep 17 00:00:00 2001 From: lady ada Date: Thu, 24 Jun 2021 13:24:04 -0400 Subject: [PATCH 1/3] possible implementation for MCP23S --- Adafruit_BusIO_Register.cpp | 21 +++++++++++++++++++++ Adafruit_BusIO_Register.h | 10 +++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Adafruit_BusIO_Register.cpp b/Adafruit_BusIO_Register.cpp index fb4735a..88fea36 100644 --- a/Adafruit_BusIO_Register.cpp +++ b/Adafruit_BusIO_Register.cpp @@ -101,6 +101,17 @@ bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) { return _i2cdevice->write(buffer, len, true, addrbuffer, _addrwidth); } if (_spidevice) { + if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) { + // very special case! + + // pass the special opcode address which we set as the high byte of the regaddr + addrbuffer[0] = (uint8_t)(_address >> 8) & ~0x01; // set bottom bit low to write + // the 'actual' reg addr is the second byte then + addrbuffer[1] = (uint8_t)(_address & 0xFF); + // the address appears to be a byte longer + return _spidevice->write(buffer, len, addrbuffer, _addrwidth+1); + } + if (_spiregtype == ADDRBIT8_HIGH_TOREAD) { addrbuffer[0] &= ~0x80; } @@ -190,6 +201,16 @@ bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) { return _i2cdevice->write_then_read(addrbuffer, _addrwidth, buffer, len); } if (_spidevice) { + if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) { + // very special case! + + // pass the special opcode address which we set as the high byte of the regaddr + addrbuffer[0] = (uint8_t)(_address >> 8) | 0x01; // set bottom bit high to read + // the 'actual' reg addr is the second byte then + addrbuffer[1] = (uint8_t)(_address & 0xFF); + // the address appears to be a byte longer + return _spidevice->write(buffer, len, addrbuffer, _addrwidth+1); + } if (_spiregtype == ADDRBIT8_HIGH_TOREAD) { addrbuffer[0] |= 0x80; } diff --git a/Adafruit_BusIO_Register.h b/Adafruit_BusIO_Register.h index bc27e5f..a55cfaf 100644 --- a/Adafruit_BusIO_Register.h +++ b/Adafruit_BusIO_Register.h @@ -15,13 +15,21 @@ typedef enum _Adafruit_BusIO_SPIRegType { */ AD8_HIGH_TOREAD_AD7_HIGH_TOINC = 1, - ADDRBIT8_HIGH_TOWRITE = 2, /*!< * ADDRBIT8_HIGH_TOWRITE * When writing to a register you must actually send the value 0x80 + * the register address to the device. e.g. To write to the register 0x19 the * register value 0x99 is sent and to read 0x19 is sent. */ + ADDRBIT8_HIGH_TOWRITE = 2, + + /*!< + * ADDRESSED_OPCODE_LOWBIT_TO_WRITE + * Used by the MCP23S series, we send 0x40 |'rd with the opcode + * Then set the lowest bit to write + */ + ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE = 3, + } Adafruit_BusIO_SPIRegType; /*! From e6712a5dfe142b390d070b0bded88da09097ab15 Mon Sep 17 00:00:00 2001 From: caternuson Date: Fri, 25 Jun 2021 09:12:04 -0700 Subject: [PATCH 2/3] fix read --- Adafruit_BusIO_Register.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Adafruit_BusIO_Register.cpp b/Adafruit_BusIO_Register.cpp index 88fea36..a1f5d91 100644 --- a/Adafruit_BusIO_Register.cpp +++ b/Adafruit_BusIO_Register.cpp @@ -209,7 +209,7 @@ bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) { // the 'actual' reg addr is the second byte then addrbuffer[1] = (uint8_t)(_address & 0xFF); // the address appears to be a byte longer - return _spidevice->write(buffer, len, addrbuffer, _addrwidth+1); + return _spidevice->write_then_read(addrbuffer, _addrwidth+1, buffer, len); } if (_spiregtype == ADDRBIT8_HIGH_TOREAD) { addrbuffer[0] |= 0x80; From f1db9d5956d705669662d23f18545276cd5424ae Mon Sep 17 00:00:00 2001 From: caternuson Date: Fri, 25 Jun 2021 09:20:30 -0700 Subject: [PATCH 3/3] clang --- Adafruit_BusIO_Register.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Adafruit_BusIO_Register.cpp b/Adafruit_BusIO_Register.cpp index a1f5d91..1f88d9e 100644 --- a/Adafruit_BusIO_Register.cpp +++ b/Adafruit_BusIO_Register.cpp @@ -104,12 +104,14 @@ bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) { if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) { // very special case! - // pass the special opcode address which we set as the high byte of the regaddr - addrbuffer[0] = (uint8_t)(_address >> 8) & ~0x01; // set bottom bit low to write + // pass the special opcode address which we set as the high byte of the + // regaddr + addrbuffer[0] = + (uint8_t)(_address >> 8) & ~0x01; // set bottom bit low to write // the 'actual' reg addr is the second byte then addrbuffer[1] = (uint8_t)(_address & 0xFF); // the address appears to be a byte longer - return _spidevice->write(buffer, len, addrbuffer, _addrwidth+1); + return _spidevice->write(buffer, len, addrbuffer, _addrwidth + 1); } if (_spiregtype == ADDRBIT8_HIGH_TOREAD) { @@ -204,12 +206,15 @@ bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) { if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) { // very special case! - // pass the special opcode address which we set as the high byte of the regaddr - addrbuffer[0] = (uint8_t)(_address >> 8) | 0x01; // set bottom bit high to read + // pass the special opcode address which we set as the high byte of the + // regaddr + addrbuffer[0] = + (uint8_t)(_address >> 8) | 0x01; // set bottom bit high to read // the 'actual' reg addr is the second byte then addrbuffer[1] = (uint8_t)(_address & 0xFF); // the address appears to be a byte longer - return _spidevice->write_then_read(addrbuffer, _addrwidth+1, buffer, len); + return _spidevice->write_then_read(addrbuffer, _addrwidth + 1, buffer, + len); } if (_spiregtype == ADDRBIT8_HIGH_TOREAD) { addrbuffer[0] |= 0x80;