From 1d18d5dea5de41e8a51c80a81375b919249aecb6 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sat, 12 Oct 2024 21:41:40 +0200 Subject: [PATCH 1/3] explain RMT variations --- DCCRMT.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/DCCRMT.cpp b/DCCRMT.cpp index afada7b..24a6e00 100644 --- a/DCCRMT.cpp +++ b/DCCRMT.cpp @@ -17,6 +17,25 @@ * along with CommandStation. If not, see . */ +/* + * RMT has "channels" which us FIFO RAM where you place what you want to send + * or receive. Channels can be merged to get more words per channel. + * + * WROOM: 8 channels total of 512 words, 64 words per channel. We use currently + * channel 0+1 for 128 words for DCC MAIN and 2+3 for DCC PROG. + * + * S3: 8 channels total of 384 words. 4 channels dedicated for TX and 4 channels + * dedicated for RX. 48 words per channel. So for TX there are 4 channels and we + * could use them with 96 words for MAIN and PROG if DCC data does fit in there. + * + * C3: 4 channels total of 192 words. As we do not use RX we can use all for TX + * so the situation is the same as for the -S3 + * + * C6, H2: 4 channels total of 192 words. 2 channels dedictaed for TX and + * 2 channels dedicated for RX. Half RMT capacity compared to the C3. + * + */ + #if defined(ARDUINO_ARCH_ESP32) #include "defines.h" #include "DIAG.h" From 535dcabcec6fe4e45a143e02558e431e33697058 Mon Sep 17 00:00:00 2001 From: pmantoine Date: Thu, 24 Oct 2024 13:19:07 +0800 Subject: [PATCH 2/3] Initial working IO_TCA8418 driver --- IO_TCA8418.h | 459 +++++++++++++++++++++++++++------------ Release_Notes/TCA8418.md | 44 ++++ version.h | 3 +- 3 files changed, 369 insertions(+), 137 deletions(-) create mode 100644 Release_Notes/TCA8418.md diff --git a/IO_TCA8418.h b/IO_TCA8418.h index b3302b9..2ea1f76 100644 --- a/IO_TCA8418.h +++ b/IO_TCA8418.h @@ -1,5 +1,5 @@ /* - * © 2023, Paul M. Antoine + * © 2023-2024, Paul M. Antoine * © 2021, Neil McKechnie. All rights reserved. * * This file is part of DCC-EX API @@ -21,163 +21,350 @@ #ifndef io_tca8418_h #define io_tca8418_h -#include "IO_GPIOBase.h" +#include "IODevice.h" +#include "I2CManager.h" +#include "DIAG.h" #include "FSH.h" ///////////////////////////////////////////////////////////////////////////////////////////////////// /* - * IODevice subclass for TCA8418 80-key keypad encoder, which we'll treat as 64 of the possible - * 80 inputs for now, in an 8x8 matrix only, although the datasheet says: + * IODevice subclass for TCA8418 80-key keypad encoder, which we'll treat as 80 available VPINs where + * key down == 1 and key up == 0 by configuring just as an 8x10 keyboard matrix. Users can opt to use + * up to all 80 of the available VPINs for now, allowing memory to be saved if not all events are required. + * + * The datasheet says: * * The TCA8418 can be configured to support many different configurations of keypad setups. * All 18 GPIOs for the rows and columns can be used to support up to 80 keys in an 8x10 key pad * array. Another option is that all 18 GPIOs be used for GPIs to read 18 buttons which are * not connected in an array. Any combination in between is also acceptable (for example, a * 3x4 keypad matrix and using the remaining 11 GPIOs as a combination of inputs and outputs). + * + * With an 8x10 key event matrix, the events are numbered as such: + * + * C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 + * ======================================== + * R0| 0 1 2 3 4 5 6 7 8 9 + * R1| 10 11 12 13 14 15 16 17 18 19 + * R2| 20 21 22 23 24 25 26 27 28 29 + * R3| 30 31 32 33 34 35 36 37 38 39 + * R4| 40 41 42 43 44 45 46 47 48 49 + * R5| 50 51 52 53 54 55 56 57 58 59 + * R6| 60 61 62 63 64 65 66 67 68 69 + * R7| 70 71 72 73 74 75 76 77 78 79 + * + * So if you start with VPIN 300, R0/C0 will be 300, and R7/C9 will be 379. + * + * HAL declaration for myAutomation.h is: + * HAL(TCA8418, firstVpin, numPins, I2CAddress, interruptPin) + * + * Where numPins can be 1-80, and interruptPin can be any spare Arduino pin. + * + * Configure using the following on the main I2C bus: + * HAL(TCA8418, 300, 80, 0x34) + * + * Use something like this on a multiplexor, and with up to 8 of the 8-way multiplexors you could have 64 different TCA8418 boards: + * HAL(TCA8418, 400, 80, {SubBus_1, 0x34}) + * + * And if needing an Interrupt pin to speed up operations: + * HAL(TCA8418, 300, 80, 0x34, D21) + * + * Note that using an interrupt pin speeds up button press acquisition considerably (less than a millisecond vs 10-100), + * but even with interrupts enabled the code presently checks every 100ms in case the interrupt pin becomes disconnected. + * Use any available Arduino pin for interrupt monitoring. */ -class TCA8418 : public GPIOBase { +class TCA8418 : public IODevice { public: - static void create(VPIN vpin, uint8_t nPins, I2CAddress i2cAddress, int interruptPin=-1) { - if (checkNoOverlap(vpin, nPins, i2cAddress)) - // temporarily use the simple 18-pin GPIO mode - we'll switch to 8x8 matrix once this works - new TCA8418(vpin, (nPins = (nPins > 18) ? 18 : nPins), i2cAddress, interruptPin); + + static void create(VPIN firstVpin, uint8_t nPins, I2CAddress i2cAddress, int interruptPin=-1) { + if (checkNoOverlap(firstVpin, nPins, i2cAddress)) + new TCA8418(firstVpin, (nPins = (nPins > 80) ? 80 : nPins), i2cAddress, interruptPin); } private: + + uint8_t* _digitalInputStates = NULL; // Array of pin states + uint8_t _digitalPinBytes = 0; // Number of bytes in pin state array + + uint8_t _numKeyEvents = 0; // Number of outsanding key events waiting for us + + unsigned long _lastEventRead = 0; + unsigned long _eventRefresh = 10000UL; // Delay refreshing events for 10ms + const unsigned long _eventRefreshSlow = 100000UL; // Delay refreshing events for 100ms + bool _gpioInterruptsEnabled = false; + + uint8_t _inputBuffer[1]; + uint8_t _commandBuffer[1]; + I2CRB _i2crb; + + enum {RDS_IDLE, RDS_EVENT, RDS_KEYCODE}; // Read operation states + uint8_t _readState = RDS_IDLE; + // Constructor - TCA8418(VPIN vpin, uint8_t nPins, I2CAddress i2cAddress, int interruptPin=-1) - : GPIOBase((FSH *)F("TCA8418"), vpin, nPins, i2cAddress, interruptPin) - { - uint8_t receiveBuffer[1]; - uint8_t commandBuffer[1]; - uint8_t status; - - commandBuffer[0] = REG_INT_STAT; // Check interrupt status - status = I2CManager.read(_I2CAddress, receiveBuffer, sizeof(receiveBuffer), commandBuffer, sizeof(commandBuffer)); - if (status == I2C_STATUS_OK) { - DIAG(F("TCA8418 Interrupt status was: %x"), receiveBuffer[0]); + TCA8418(VPIN firstVpin, uint8_t nPins, I2CAddress i2cAddress, int interruptPin=-1) { + if (nPins > 0) + { + _firstVpin = firstVpin; + _nPins = nPins; + _I2CAddress = i2cAddress; + _gpioInterruptPin = interruptPin; + addDevice(this); } - else - DIAG(F("TCA8418 Interrupt status failed to read!")); - // requestBlock.setRequestParams(_I2CAddress, inputBuffer, sizeof(inputBuffer), - // outputBuffer, sizeof(outputBuffer)); - // outputBuffer[0] = REG_GPIOA; - } - void _writeGpioPort() override { - // I2CManager.write(_I2CAddress, 3, REG_GPIOA, _portOutputState, _portOutputState>>8); - } - void _writePullups() override { - // Set pullups only for in-use pins. This prevents pullup being set for a pin that - // is intended for use as an output but hasn't been written to yet. - uint32_t temp = _portPullup & _portInUse; - (void)temp; // Chris did this so he could see warnings that mattered - // I2CManager.write(_I2CAddress, 3, REG_GPPUA, temp, temp>>8); - } - void _writePortModes() override { - // Write 0 to each GPIO_DIRn for in-use pins that are inputs, 1 for outputs - uint64_t temp = _portMode & _portInUse; - DIAG(F("TCA8418 writing Port Mode: %x, to GPIO_DIRs"), temp); - DIAG(F("TCA8418 writing Port Mode: %x, to GPIO_DIR1"), (temp&0xFF)); - I2CManager.write(_I2CAddress, 2, REG_GPIO_DIR1, (temp&0xFF)); - DIAG(F("TCA8418 writing Port Mode: %x, to GPIO_DIR2"), ((temp&0xFF00)>>8)); - I2CManager.write(_I2CAddress, 2, REG_GPIO_DIR2, ((temp&0xFF00)>>8)); - DIAG(F("TCA8418 writing Port Mode: %x, to GPIO_DIR3"), (temp&0x30000)>>16); - I2CManager.write(_I2CAddress, 2, REG_GPIO_DIR3, ((temp&0x30000)>>16)); - - // Enable interrupt for in-use pins which are inputs (_portMode=0) - // TCA8418 has interrupt enables per pin, but must be configured for low->high - // or high->low... unlike the MCP23017 - temp = ~_portMode & _portInUse; - DIAG(F("TCA8418 writing interrupt Port Mode: %x, to GPIO_INT_ENs"), temp); - DIAG(F("TCA8418 writing interrupt Port Mode: %x, to GPIO_INT_EN1"), (temp&0xFF)); - I2CManager.write(_I2CAddress, 2, REG_GPIO_INT_EN1, (temp&0xFF)); - DIAG(F("TCA8418 writing interrupt Port Mode: %x, to GPIO_INT_EN2"), ((temp&0xFF00)>>8)); - I2CManager.write(_I2CAddress, 2, REG_GPIO_INT_EN2, ((temp&0xFF00)>>8)); - DIAG(F("TCA8418 writing interrupt Port Mode: %x, to GPIO_INT_EN3"), (temp&0x30000)>>16); - I2CManager.write(_I2CAddress, 2, REG_GPIO_INT_EN3, ((temp&0x30000)>>16)); - // I2CManager.write(_I2CAddress, 3, REG_INTCONA, 0x00, 0x00); - // I2CManager.write(_I2CAddress, 3, REG_GPINTENA, temp, temp>>8); - } - void _readGpioPort(bool immediate) override { - // if (immediate) { - // uint8_t buffer[2]; - // I2CManager.read(_I2CAddress, buffer, 2, 1, REG_GPIOA); - // _portInputState = ((uint16_t)buffer[1]<<8) | buffer[0] | _portMode; - // } else { - // // Queue new request - // requestBlock.wait(); // Wait for preceding operation to complete - // // Issue new request to read GPIO register - // I2CManager.queueRequest(&requestBlock); - // } - } - // This function is invoked when an I/O operation on the requestBlock completes. - void _processCompletion(uint8_t status) override { - // if (status == I2C_STATUS_OK) - // _portInputState = (((uint16_t)inputBuffer[1]<<8) | inputBuffer[0]) | _portMode; - // else - // _portInputState = 0xffff; } - void _setupDevice() override { - DIAG(F("TCA8418 setupDevice() called")); - // IOCON is set MIRROR=1, ODR=1 (open drain shared interrupt pin) - // I2CManager.write(_I2CAddress, 2, REG_IOCON, 0x44); - _writePortModes(); - _writePullups(); - _writeGpioPort(); + void _begin() { + + I2CManager.begin(); + + if (I2CManager.exists(_I2CAddress)) { + // Default all GPIO pins to INPUT + I2CManager.write(_I2CAddress, 2, REG_GPIO_DIR_1, 0x00); + I2CManager.write(_I2CAddress, 2, REG_GPIO_DIR_2, 0x00); + I2CManager.write(_I2CAddress, 2, REG_GPIO_DIR_3, 0x00); + + // Remove all GPIO pins from events + I2CManager.write(_I2CAddress, 2, REG_GPI_EM_1, 0x00); + I2CManager.write(_I2CAddress, 2, REG_GPI_EM_2, 0x00); + I2CManager.write(_I2CAddress, 2, REG_GPI_EM_3, 0x00); + + // Set all pins to FALLING interrupts + I2CManager.write(_I2CAddress, 2, REG_GPIO_INT_LVL_1, 0x00); + I2CManager.write(_I2CAddress, 2, REG_GPIO_INT_LVL_2, 0x00); + I2CManager.write(_I2CAddress, 2, REG_GPIO_INT_LVL_3, 0x00); + + // Remove all GPIO pins from interrupts + I2CManager.write(_I2CAddress, 2, REG_GPIO_INT_EN_1, 0x00); + I2CManager.write(_I2CAddress, 2, REG_GPIO_INT_EN_2, 0x00); + I2CManager.write(_I2CAddress, 2, REG_GPIO_INT_EN_3, 0x00); + + // Set up an 8 x 10 matrix by writing 0xFF to all the row and column configs + // Row config is maximum of 8, and in REG_KP_GPIO_1 + I2CManager.write(_I2CAddress, 2, REG_KP_GPIO_1, 0xFF); + // Column config is maximum of 10, lower 8 bits in REG_KP_GPIO_2, upper in REG_KP_GPIO_3 + // Set first 8 columns + I2CManager.write(_I2CAddress, 2, REG_KP_GPIO_2, 0xFF); + // Turn on cols 9/10 + I2CManager.write(_I2CAddress, 2, REG_KP_GPIO_3, 0x03); + + // // Set all pins to Enable Debounce + I2CManager.write(_I2CAddress, 2, REG_DEBOUNCE_DIS_1, 0x00); + I2CManager.write(_I2CAddress, 2, REG_DEBOUNCE_DIS_2, 0x00); + I2CManager.write(_I2CAddress, 2, REG_DEBOUNCE_DIS_3, 0x00); + + // Let's assume an 8x10 matrix for now, and configure + _digitalPinBytes = (_nPins + 7) / 8; + if ((_digitalInputStates = (byte *)calloc(_digitalPinBytes, 1)) == NULL) { + DIAG(F("TCA8418 I2C: Unable to alloc %d bytes"), _digitalPinBytes); + return; + } + + // Configure pin used for GPIO extender notification of change (if allocated) + // and configure TCA8418 to produce key event interrupts + if (_gpioInterruptPin >= 0) { + DIAG(F("TCA8418 I2C: interrupt pin configured on %d"), _gpioInterruptPin); + _gpioInterruptsEnabled = true; + _eventRefresh = _eventRefreshSlow; // Switch to slower manual refreshes in case the INT pin isn't connected! + pinMode(_gpioInterruptPin, INPUT_PULLUP); + I2CManager.write(_I2CAddress, 2, REG_CFG, REG_CFG_KE_IEN); + // Clear any pending interrupts + I2CManager.write(_I2CAddress, 2, REG_INT_STAT, REG_STAT_K_INT); + } + +#ifdef DIAG_IO + _display(); +#endif + } } - - enum + + int _read(VPIN vpin) override { + if (_deviceState == DEVSTATE_FAILED) + return 0; + int pin = vpin - _firstVpin; + bool result = _digitalInputStates[pin / 8] & (1 << (pin % 8)); + return result; + } + + + // Main loop, collect both digital and analogue pin states continuously (faster sensor/input reads) + void _loop(unsigned long currentMicros) override { + if (_deviceState == DEVSTATE_FAILED) return; // If device failed, return + + // Request block is used for key event reads from the TCA8418, which are performed + // on a cyclic basis. + + if (_readState != RDS_IDLE) { + if (_i2crb.isBusy()) return; // If I2C operation still in progress, return + + uint8_t status = _i2crb.status; + if (status == I2C_STATUS_OK) { // If device request ok, read input data + + // First check if we have any key events waiting + if (_readState == RDS_EVENT) { + if ((_numKeyEvents = (_inputBuffer[0] & 0x0F)) != 0) { + // We could read each key event waiting in a synchronous loop, which may prove preferable + // but for now, schedule an async read of the first key event in the queue + _commandBuffer[0] = REG_KEY_EVENT_A; + I2CManager.read(_I2CAddress, _inputBuffer, 1, _commandBuffer, 1, &_i2crb); // non-blocking read + _readState = RDS_KEYCODE; // Shift to reading key events! + } + else // We found no key events waiting, return to IDLE + _readState = RDS_IDLE; + } + else { + // RDS_KEYCODE + uint8_t key = _inputBuffer[0] & 0x7F; + bool keyDown = _inputBuffer[0] & 0x80; + // Check for just keypad events + key--; // R0/C0 is key #1, so subtract 1 to create an array offset + // We only want to record key events we're configured for, as we have calloc'd an + // appropriately sized _digitalInputStates array! + if (key < _nPins) { + if (keyDown) + _digitalInputStates[key / 8] |= (1 << (key % 8)); + else + _digitalInputStates[key / 8] &= ~(1 << (key % 8)); + } + else + DIAG(F("TCA8418 I2C: key event %d discarded, outside Vpin range"), key); + _numKeyEvents--; // One less key event to get + if (_numKeyEvents != 0) + { + // DIAG(F("TCA8418 I2C: more keys in read event queue, # waiting is: %x"), _numKeyEvents); + // We could read each key event waiting in a synchronous loop, which may prove preferable + // but for now, schedule an async read of the first key event in the queue + _commandBuffer[0] = REG_KEY_EVENT_A; + I2CManager.read(_I2CAddress, _inputBuffer, 1, _commandBuffer, 1, &_i2crb); // non-blocking read + } + else { + // DIAG(F("TCA8418 I2C: no more keys in read event queue")); + // Clear any pending interrupts + I2CManager.write(_I2CAddress, 2, REG_INT_STAT, REG_STAT_K_INT); + _readState = RDS_IDLE; // Shift to IDLE + return; + } + } + } else + reportError(status, false); // report eror but don't go offline. + } + + // If we're not doing anything now, check to see if we have an interrupt pin configured and it is low, + // or if our timer has elapsed and we should check anyway in case the interrupt pin is disconnected. + if (_readState == RDS_IDLE) { + if ((_gpioInterruptsEnabled && !digitalRead(_gpioInterruptPin)) || + ((currentMicros - _lastEventRead) > _eventRefresh)) + { + _commandBuffer[0] = REG_KEY_LCK_EC; + I2CManager.read(_I2CAddress, _inputBuffer, 1, _commandBuffer, 1, &_i2crb); // non-blocking read + _lastEventRead = currentMicros; + _readState = RDS_EVENT; // Shift to looking for key events! + } + } + } + + // Display device information and status + void _display() override { + DIAG(F("TCA8418 I2C:%s Vpins %u-%u%S"), + _I2CAddress.toString(), + _firstVpin, (_firstVpin+_nPins-1), + _deviceState == DEVSTATE_FAILED ? F(" OFFLINE") : F("")); + if (_gpioInterruptsEnabled) + DIAG(F("TCA8418 I2C:Interrupt on pin %d"), _gpioInterruptPin); + } + + // Helper function for error handling + void reportError(uint8_t status, bool fail=true) { + DIAG(F("TCA8418 I2C:%s Error:%d (%S)"), _I2CAddress.toString(), + status, I2CManager.getErrorMessage(status)); + if (fail) + _deviceState = DEVSTATE_FAILED; + } + + enum tca8418_registers { - REG_FIRST_RESERVED = 0x00, - REG_CFG = 0x01, - REG_INT_STAT = 0x02, - REG_KEY_LCK_EC = 0x03, - REG_KEY_EVENT_A = 0x04, - REG_KEY_EVENT_B = 0x05, - REG_KEY_EVENT_C = 0x06, - REG_KEY_EVENT_D = 0x07, - REG_KEY_EVENT_E = 0x08, - REG_KEY_EVENT_F = 0x09, - REG_KEY_EVENT_G = 0x0A, - REG_KEY_EVENT_H = 0x0B, - REG_KEY_EVENT_I = 0x0C, - REG_KEY_EVENT_J = 0x0D, - REG_KP_LCK_TIMER = 0x0E, - REG_UNLOCK1 = 0x0F, - REG_UNLOCK2 = 0x10, - REG_GPIO_INT_STAT1 = 0x11, - REG_GPIO_INT_STAT2 = 0x12, - REG_GPIO_INT_STAT3 = 0x13, - REG_GPIO_DAT_STAT1 = 0x14, - REG_GPIO_DAT_STAT2 = 0x15, - REG_GPIO_DAT_STAT3 = 0x16, - REG_GPIO_DAT_OUT1 = 0x17, - REG_GPIO_DAT_OUT2 = 0x18, - REG_GPIO_DAT_OUT3 = 0x19, - REG_GPIO_INT_EN1 = 0x1A, - REG_GPIO_INT_EN2 = 0x1B, - REG_GPIO_INT_EN3 = 0x1C, - REG_KP_GPIO1 = 0x1D, - REG_KP_GPIO2 = 0x1E, - REG_KP_GPIO3 = 0x1F, - REG_GPI_EM1 = 0x20, - REG_GPI_EM2 = 0x21, - REG_GPI_EM3 = 0x22, - REG_GPIO_DIR1 = 0x23, - REG_GPIO_DIR2 = 0x24, - REG_GPIO_DIR3 = 0x25, - REG_GPIO_INT_LVL1 = 0x26, - REG_GPIO_INT_LVL2 = 0x27, - REG_GPIO_INT_LVL3 = 0x28, - REG_DEBOUNCE_DIS1 = 0x29, - REG_DEBOUNCE_DIS2 = 0x2A, - REG_DEBOUNCE_DIS3 = 0x2B, - REG_GPIO_PULL1 = 0x2C, - REG_GPIO_PULL2 = 0x2D, - REG_GPIO_PULL3 = 0x2E, - REG_LAST_RESERVED = 0x2F, + // REG_RESERVED = 0x00 + REG_CFG = 0x01, // Configuration register + REG_INT_STAT = 0x02, // Interrupt status + REG_KEY_LCK_EC = 0x03, // Key lock and event counter + REG_KEY_EVENT_A = 0x04, // Key event register A + REG_KEY_EVENT_B = 0x05, // Key event register B + REG_KEY_EVENT_C = 0x06, // Key event register C + REG_KEY_EVENT_D = 0x07, // Key event register D + REG_KEY_EVENT_E = 0x08, // Key event register E + REG_KEY_EVENT_F = 0x09, // Key event register F + REG_KEY_EVENT_G = 0x0A, // Key event register G + REG_KEY_EVENT_H = 0x0B, // Key event register H + REG_KEY_EVENT_I = 0x0C, // Key event register I + REG_KEY_EVENT_J = 0x0D, // Key event register J + REG_KP_LCK_TIMER = 0x0E, // Keypad lock1 to lock2 timer + REG_UNLOCK_1 = 0x0F, // Unlock register 1 + REG_UNLOCK_2 = 0x10, // Unlock register 2 + REG_GPIO_INT_STAT_1 = 0x11, // GPIO interrupt status 1 + REG_GPIO_INT_STAT_2 = 0x12, // GPIO interrupt status 2 + REG_GPIO_INT_STAT_3 = 0x13, // GPIO interrupt status 3 + REG_GPIO_DAT_STAT_1 = 0x14, // GPIO data status 1 + REG_GPIO_DAT_STAT_2 = 0x15, // GPIO data status 2 + REG_GPIO_DAT_STAT_3 = 0x16, // GPIO data status 3 + REG_GPIO_DAT_OUT_1 = 0x17, // GPIO data out 1 + REG_GPIO_DAT_OUT_2 = 0x18, // GPIO data out 2 + REG_GPIO_DAT_OUT_3 = 0x19, // GPIO data out 3 + REG_GPIO_INT_EN_1 = 0x1A, // GPIO interrupt enable 1 + REG_GPIO_INT_EN_2 = 0x1B, // GPIO interrupt enable 2 + REG_GPIO_INT_EN_3 = 0x1C, // GPIO interrupt enable 3 + REG_KP_GPIO_1 = 0x1D, // Keypad/GPIO select 1 + REG_KP_GPIO_2 = 0x1E, // Keypad/GPIO select 2 + REG_KP_GPIO_3 = 0x1F, // Keypad/GPIO select 3 + REG_GPI_EM_1 = 0x20, // GPI event mode 1 + REG_GPI_EM_2 = 0x21, // GPI event mode 2 + REG_GPI_EM_3 = 0x22, // GPI event mode 3 + REG_GPIO_DIR_1 = 0x23, // GPIO data direction 1 + REG_GPIO_DIR_2 = 0x24, // GPIO data direction 2 + REG_GPIO_DIR_3 = 0x25, // GPIO data direction 3 + REG_GPIO_INT_LVL_1 = 0x26, // GPIO edge/level detect 1 + REG_GPIO_INT_LVL_2 = 0x27, // GPIO edge/level detect 2 + REG_GPIO_INT_LVL_3 = 0x28, // GPIO edge/level detect 3 + REG_DEBOUNCE_DIS_1 = 0x29, // Debounce disable 1 + REG_DEBOUNCE_DIS_2 = 0x2A, // Debounce disable 2 + REG_DEBOUNCE_DIS_3 = 0x2B, // Debounce disable 3 + REG_GPIO_PULL_1 = 0x2C, // GPIO pull-up disable 1 + REG_GPIO_PULL_2 = 0x2D, // GPIO pull-up disable 2 + REG_GPIO_PULL_3 = 0x2E, // GPIO pull-up disable 3 + // REG_RESERVED = 0x2F + }; + + enum tca8418_config_reg_fields + { + // Config Register #1 fields + REG_CFG_AI = 0x80, // Auto-increment for read/write + REG_CFG_GPI_E_CGF = 0x40, // Event mode config + REG_CFG_OVR_FLOW_M = 0x20, // Overflow mode enable + REG_CFG_INT_CFG = 0x10, // Interrupt config + REG_CFG_OVR_FLOW_IEN = 0x08, // Overflow interrupt enable + REG_CFG_K_LCK_IEN = 0x04, // Keypad lock interrupt enable + REG_CFG_GPI_IEN = 0x02, // GPI interrupt enable + REG_CFG_KE_IEN = 0x01, // Key events interrupt enable + }; + + enum tca8418_int_status_fields + { + // Interrupt Status Register #2 fields + REG_STAT_CAD_INT = 0x10, // Ctrl-alt-del seq status + REG_STAT_OVR_FLOW_INT = 0x08, // Overflow interrupt status + REG_STAT_K_LCK_INT = 0x04, // Key lock interrupt status + REG_STAT_GPI_INT = 0x02, // GPI interrupt status + REG_STAT_K_INT = 0x01, // Key events interrupt status + }; + + enum tca8418_lock_ec_fields + { + // Key Lock Event Count Register #3 + REG_LCK_EC_K_LCK_EN = 0x40, // Key lock enable + REG_LCK_EC_LCK_2 = 0x20, // Keypad lock status 2 + REG_LCK_EC_LCK_1 = 0x10, // Keypad lock status 1 + REG_LCK_EC_KLEC_3 = 0x08, // Key event count bit 3 + REG_LCK_EC_KLEC_2 = 0x04, // Key event count bit 2 + REG_LCK_EC_KLEC_1 = 0x02, // Key event count bit 1 + REG_LCK_EC_KLEC_0 = 0x01, // Key event count bit 0 }; }; diff --git a/Release_Notes/TCA8418.md b/Release_Notes/TCA8418.md new file mode 100644 index 0000000..71fd746 --- /dev/null +++ b/Release_Notes/TCA8418.md @@ -0,0 +1,44 @@ +## TCA8418 ## + +The TCA8418 IC from Texas Instruments is a low cost and very capable GPIO and keyboard scanner. Used as a keyboard scanner, it has 8 rows of 10 columns of IO pins which allow encoding of up to 80 buttons. The IC is available on an Adafruit board with Qwiic I2C interconnect called the "Adafruit TCA8418 Keypad Matrix and GPIO Expander Breakout" and available here for the modest sum of $US6 or so: https://www.adafruit.com/product/4918 + +The great advantage of this IC is that the keyboard scanning is done continuously, and it has a 10-element event queue, so even if you don't get to the interrupt immediately, keypress and release events will be held for you. Since it's I2C its very easy to use with any DCC-EX command station. + +The TCA8418 driver presently configures the IC in the full 8x10 keyboard scanning mode, and then maps each key down/key up event to the state of a single vpin for extremely easy use from within EX-RAIL and JMRI as each key looks like an individual sensor. + +This is ideal for mimic panels where you may need a lot of buttons, but with this board you can use just 18 wires to handle as many as 80 buttons. + +By adding a simple HAL statement to myAutomation.h it creates between 1 and 80 buttons it will report back. + +`HAL(TCA8418, firstVpin, numPins, I2CAddress, interruptPin)` + +For example: + +`HAL(TCA8418, 300, 80, 0x34)` + +Creates VPINs 300-379 which you can monitor with EX-RAIL, JMRI sensors etc. + +With an 8x10 key event matrix, the events are numbered using the Rn row pins and Cn column pins as such: + + C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 + ======================================== + R0| 0 1 2 3 4 5 6 7 8 9 + R1| 10 11 12 13 14 15 16 17 18 19 + R2| 20 21 22 23 24 25 26 27 28 29 + R3| 30 31 32 33 34 35 36 37 38 39 + R4| 40 41 42 43 44 45 46 47 48 49 + R5| 50 51 52 53 54 55 56 57 58 59 + R6| 60 61 62 63 64 65 66 67 68 69 + R7| 70 71 72 73 74 75 76 77 78 79 + +So if you start with the first pin definition being VPIN 300, R0/C0 will be 300 + 0, and R7/C9 will be 300+79 or 379. + +Use something like this on a multiplexor, and with up to 8 of the 8-way multiplexors you could have 64 different TCA8418 boards: + +`HAL(TCA8418, 400, 80, {SubBus_1, 0x34})` + +And if needing an Interrupt pin to speed up operations: +`HAL(TCA8418, 300, 80, 0x34, 21)` + +Note that using an interrupt pin speeds up button press acquisition considerably (less than a millisecond vs 10-100), but even with interrupts enabled the code presently checks every 100ms in case the interrupt pin becomes disconnected. Use any available Arduino pin for interrupt monitoring. + diff --git a/version.h b/version.h index e6d43d3..6224bfe 100644 --- a/version.h +++ b/version.h @@ -3,7 +3,8 @@ #include "StringFormatter.h" -#define VERSION "5.2.85" +#define VERSION "5.2.86" +// 5.2.86 - IO_TCA8418 driver for keypad matrix input now fully functioning, including being able to use an interrupt pin // 5.2.85 - IO_TM1638 driver, SEG7 Exrail macro and _s7 segment pattern generator. // 5.2.84 - Fix TrackManager setDCCSignal and setPROGSignal for STM32 shadowing of PORTG/PORTH - this time it really is correct! // 5.2.83 - Various STM32 related fixes for serial ports, I2C pullups now turned off, and shadowing of PORTG/PORTH for TrackManager now correct From c50f3e016c383a0a1348b5a3bbaa4e169929bf8e Mon Sep 17 00:00:00 2001 From: Asbelos Date: Sun, 27 Oct 2024 13:53:34 +0000 Subject: [PATCH 3/3] No functional change Unused parameter removal & end of file tidying by the Arduino IDE --- CommandDistributor.cpp | 1 - DCCACK.cpp | 1 - DCCTimerAVR.cpp | 2 ++ DCCTimerESP.cpp | 1 - EXRAIL2Parser.cpp | 1 - EXRAILSensor.h | 2 +- I2CManager_NonBlocking.h | 2 +- I2CManager_Wire.h | 2 +- IODevice.cpp | 1 - IO_AnalogueInputs.h | 2 +- IO_DCCAccessory.cpp | 1 - IO_EncoderThrottle.cpp | 1 - IO_ExampleSerial.h | 2 +- IO_HALDisplay.h | 2 +- IO_MCP23008.h | 2 +- IO_MCP23017.h | 2 +- IO_NeoPixel.h | 3 ++- IO_PCA9685pwm.h | 2 +- IO_PCF8574.h | 2 +- IO_PCF8575.h | 2 +- IO_Servo.cpp | 1 - IO_Servo.h | 2 +- IO_TCA8418.h | 2 +- IO_TM1638.cpp | 2 -- IO_TouchKeypad.h | 2 +- IO_duinoNodes.h | 2 +- IO_trainbrains.h | 2 +- KeywordHasher.h | 2 +- LiquidCrystal_I2C.cpp | 2 +- StringBuffer.cpp | 2 -- StringBuffer.h | 2 +- TrackManager.cpp | 1 - Turnouts.cpp | 1 - 33 files changed, 23 insertions(+), 34 deletions(-) diff --git a/CommandDistributor.cpp b/CommandDistributor.cpp index be23577..e889f62 100644 --- a/CommandDistributor.cpp +++ b/CommandDistributor.cpp @@ -377,4 +377,3 @@ void CommandDistributor::setVirtualLCDSerial(Print * stream) { Print* CommandDistributor::virtualLCDSerial=&USB_SERIAL; byte CommandDistributor::virtualLCDClient=0xFF; byte CommandDistributor::rememberVLCDClient=0; - diff --git a/DCCACK.cpp b/DCCACK.cpp index 9d33529..64dafa3 100644 --- a/DCCACK.cpp +++ b/DCCACK.cpp @@ -483,4 +483,3 @@ void DCCACK::checkAck(byte sentResetsSincePacket) { } ackPulseStart=0; // We have detected a too-short or too-long pulse so ignore and wait for next leading edge } - diff --git a/DCCTimerAVR.cpp b/DCCTimerAVR.cpp index 656ba7e..5f828d7 100644 --- a/DCCTimerAVR.cpp +++ b/DCCTimerAVR.cpp @@ -197,6 +197,8 @@ void DCCTimer::DCCEXanalogWriteFrequency(uint8_t pin, uint32_t f) { } void DCCTimer::DCCEXanalogWriteFrequencyInternal(uint8_t pin, uint32_t fbits) { #if defined(ARDUINO_AVR_UNO) + (void)fbits; + (void) pin; // Not worth doin something here as: // If we are on pin 9 or 10 we are on Timer1 and we can not touch Timer1 as that is our DCC source. // If we are on pin 5 or 6 we are on Timer 0 ad we can not touch Timer0 as that is millis() etc. diff --git a/DCCTimerESP.cpp b/DCCTimerESP.cpp index 39824a5..31dc2b1 100644 --- a/DCCTimerESP.cpp +++ b/DCCTimerESP.cpp @@ -324,4 +324,3 @@ void ADCee::begin() { } #endif //ESP32 - diff --git a/EXRAIL2Parser.cpp b/EXRAIL2Parser.cpp index a26d78a..63bace0 100644 --- a/EXRAIL2Parser.cpp +++ b/EXRAIL2Parser.cpp @@ -363,4 +363,3 @@ bool RMFT2::parseSlash(Print * stream, byte & paramCount, int16_t p[]) { return false; } } - diff --git a/EXRAILSensor.h b/EXRAILSensor.h index b5b00c6..c9929f6 100644 --- a/EXRAILSensor.h +++ b/EXRAILSensor.h @@ -47,4 +47,4 @@ class EXRAILSensor { bool onChange; byte latchDelay; }; -#endif \ No newline at end of file +#endif diff --git a/I2CManager_NonBlocking.h b/I2CManager_NonBlocking.h index 59bbcaf..83e1a2f 100644 --- a/I2CManager_NonBlocking.h +++ b/I2CManager_NonBlocking.h @@ -384,4 +384,4 @@ void I2CManagerClass::handleInterrupt() { } } -#endif \ No newline at end of file +#endif diff --git a/I2CManager_Wire.h b/I2CManager_Wire.h index 1fd4d67..8324919 100644 --- a/I2CManager_Wire.h +++ b/I2CManager_Wire.h @@ -231,4 +231,4 @@ void I2CManagerClass::queueRequest(I2CRB *req) { ***************************************************************************/ void I2CManagerClass::loop() {} -#endif \ No newline at end of file +#endif diff --git a/IODevice.cpp b/IODevice.cpp index 5d3397e..2a990bf 100644 --- a/IODevice.cpp +++ b/IODevice.cpp @@ -627,4 +627,3 @@ bool ArduinoPins::fastReadDigital(uint8_t pin) { #endif return result; } - diff --git a/IO_AnalogueInputs.h b/IO_AnalogueInputs.h index a78503f..e9d25a7 100644 --- a/IO_AnalogueInputs.h +++ b/IO_AnalogueInputs.h @@ -166,4 +166,4 @@ private: uint8_t _nextState; }; -#endif // io_analogueinputs_h \ No newline at end of file +#endif // io_analogueinputs_h diff --git a/IO_DCCAccessory.cpp b/IO_DCCAccessory.cpp index 3e60e3b..e138da5 100644 --- a/IO_DCCAccessory.cpp +++ b/IO_DCCAccessory.cpp @@ -65,4 +65,3 @@ void DCCAccessoryDecoder::_display() { DIAG(F("DCCAccessoryDecoder Configured on Vpins:%u-%u Addresses %d/%d-%d/%d)"), _firstVpin, _firstVpin+_nPins-1, ADDRESS(_packedAddress), SUBADDRESS(_packedAddress), ADDRESS(endAddress), SUBADDRESS(endAddress)); } - diff --git a/IO_EncoderThrottle.cpp b/IO_EncoderThrottle.cpp index 76a5f85..e6019a8 100644 --- a/IO_EncoderThrottle.cpp +++ b/IO_EncoderThrottle.cpp @@ -141,4 +141,3 @@ const byte _DIR_MASK = 0x30; void EncoderThrottle::_display() { DIAG(F("DRIVE vpin %d loco %d notch %d"),_firstVpin,_locoid,_notch); } - diff --git a/IO_ExampleSerial.h b/IO_ExampleSerial.h index 5c70eb4..8ecdb83 100644 --- a/IO_ExampleSerial.h +++ b/IO_ExampleSerial.h @@ -162,4 +162,4 @@ protected: }; -#endif // IO_EXAMPLESERIAL_H \ No newline at end of file +#endif // IO_EXAMPLESERIAL_H diff --git a/IO_HALDisplay.h b/IO_HALDisplay.h index 24ffde7..5bf16a1 100644 --- a/IO_HALDisplay.h +++ b/IO_HALDisplay.h @@ -262,4 +262,4 @@ public: }; -#endif // IO_HALDisplay_H \ No newline at end of file +#endif // IO_HALDisplay_H diff --git a/IO_MCP23008.h b/IO_MCP23008.h index 9598a50..277d3ea 100644 --- a/IO_MCP23008.h +++ b/IO_MCP23008.h @@ -98,4 +98,4 @@ private: }; -#endif \ No newline at end of file +#endif diff --git a/IO_MCP23017.h b/IO_MCP23017.h index 7bdc288..b53d3e3 100644 --- a/IO_MCP23017.h +++ b/IO_MCP23017.h @@ -108,4 +108,4 @@ private: }; -#endif \ No newline at end of file +#endif diff --git a/IO_NeoPixel.h b/IO_NeoPixel.h index 09966b5..03d3254 100644 --- a/IO_NeoPixel.h +++ b/IO_NeoPixel.h @@ -206,6 +206,7 @@ private: // loop called by HAL supervisor void _loop(unsigned long currentMicros) override { + (void)currentMicros; if (!_showPendimg) return; byte showBuffer[]={SEESAW_NEOPIXEL_BASE,SEESAW_NEOPIXEL_SHOW}; I2CManager.write(_I2CAddress,showBuffer,sizeof(showBuffer)); @@ -291,7 +292,7 @@ private: } - void transmit(uint16_t pixel, bool show=true) { + void transmit(uint16_t pixel) { byte buffer[]={SEESAW_NEOPIXEL_BASE,SEESAW_NEOPIXEL_BUF,0x00,0x00,0x00,0x00,0x00}; uint16_t offset= pixel * _bytesPerPixel; buffer[2]=(byte)(offset>>8); diff --git a/IO_PCA9685pwm.h b/IO_PCA9685pwm.h index a12cb84..98885b8 100644 --- a/IO_PCA9685pwm.h +++ b/IO_PCA9685pwm.h @@ -167,4 +167,4 @@ private: }; -#endif \ No newline at end of file +#endif diff --git a/IO_PCF8574.h b/IO_PCF8574.h index d71a32a..17241ee 100644 --- a/IO_PCF8574.h +++ b/IO_PCF8574.h @@ -101,4 +101,4 @@ private: uint8_t inputBuffer[1]; }; -#endif \ No newline at end of file +#endif diff --git a/IO_PCF8575.h b/IO_PCF8575.h index 0674617..4d9caa1 100644 --- a/IO_PCF8575.h +++ b/IO_PCF8575.h @@ -106,4 +106,4 @@ private: uint8_t inputBuffer[2]; }; -#endif \ No newline at end of file +#endif diff --git a/IO_Servo.cpp b/IO_Servo.cpp index d59c1de..841fe68 100644 --- a/IO_Servo.cpp +++ b/IO_Servo.cpp @@ -30,4 +30,3 @@ // const uint8_t FLASH Servo::_bounceProfile[30] = {0,2,3,7,13,33,50,83,100,83,75,70,65,60,60,65,74,84,100,83,75,70,70,72,75,80,87,92,97,100}; - diff --git a/IO_Servo.h b/IO_Servo.h index 9e3211e..4e50bf3 100644 --- a/IO_Servo.h +++ b/IO_Servo.h @@ -295,4 +295,4 @@ private: } }; -#endif \ No newline at end of file +#endif diff --git a/IO_TCA8418.h b/IO_TCA8418.h index 2ea1f76..f2ff99c 100644 --- a/IO_TCA8418.h +++ b/IO_TCA8418.h @@ -368,4 +368,4 @@ private: }; }; -#endif \ No newline at end of file +#endif diff --git a/IO_TM1638.cpp b/IO_TM1638.cpp index 45614f6..48d4602 100644 --- a/IO_TM1638.cpp +++ b/IO_TM1638.cpp @@ -213,5 +213,3 @@ void TM1638::test(){ } } - - diff --git a/IO_TouchKeypad.h b/IO_TouchKeypad.h index 48e3b25..0fc7b08 100644 --- a/IO_TouchKeypad.h +++ b/IO_TouchKeypad.h @@ -131,4 +131,4 @@ protected: }; -#endif // IO_TOUCHKEYPAD_H \ No newline at end of file +#endif // IO_TOUCHKEYPAD_H diff --git a/IO_duinoNodes.h b/IO_duinoNodes.h index c764db2..f9cc0fc 100644 --- a/IO_duinoNodes.h +++ b/IO_duinoNodes.h @@ -170,4 +170,4 @@ public: } }; -#endif \ No newline at end of file +#endif diff --git a/IO_trainbrains.h b/IO_trainbrains.h index 058fe02..b762fd1 100644 --- a/IO_trainbrains.h +++ b/IO_trainbrains.h @@ -95,4 +95,4 @@ private: }; -#endif \ No newline at end of file +#endif diff --git a/KeywordHasher.h b/KeywordHasher.h index fe85eb4..16ff927 100644 --- a/KeywordHasher.h +++ b/KeywordHasher.h @@ -93,4 +93,4 @@ constexpr uint32_t operator""_s7(const char * keyword, size_t len) { return CompiletimeSeg7(keyword,0*len,4); } -#endif \ No newline at end of file +#endif diff --git a/LiquidCrystal_I2C.cpp b/LiquidCrystal_I2C.cpp index 1354024..a517f61 100644 --- a/LiquidCrystal_I2C.cpp +++ b/LiquidCrystal_I2C.cpp @@ -221,4 +221,4 @@ void LiquidCrystal_I2C::expanderWrite(uint8_t value) { rb.wait(); outputBuffer[0] = value | _backlightval; I2CManager.write(_Addr, outputBuffer, 1, &rb); // Write command asynchronously -} \ No newline at end of file +} diff --git a/StringBuffer.cpp b/StringBuffer.cpp index dd4e477..014bf87 100644 --- a/StringBuffer.cpp +++ b/StringBuffer.cpp @@ -41,5 +41,3 @@ size_t StringBuffer::write(uint8_t b) { _buffer[_pos_write]='\0'; return 1; } - - diff --git a/StringBuffer.h b/StringBuffer.h index ac42964..8bf3458 100644 --- a/StringBuffer.h +++ b/StringBuffer.h @@ -35,4 +35,4 @@ class StringBuffer : public Print { char _buffer[buffer_max+2]; }; -#endif \ No newline at end of file +#endif diff --git a/TrackManager.cpp b/TrackManager.cpp index 6b37cef..b4863bc 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -697,4 +697,3 @@ TRACK_MODE TrackManager::getMode(byte t) { int16_t TrackManager::returnDCAddr(byte t) { return (trackDCAddr[t]); } - diff --git a/Turnouts.cpp b/Turnouts.cpp index ca5f890..ffc5e93 100644 --- a/Turnouts.cpp +++ b/Turnouts.cpp @@ -527,4 +527,3 @@ StringFormatter::send(stream, F("\n"), _turnoutData.id, !_turnoutData.closed); } -