qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

matrix_extension_74hc15x.c (2653B)


      1 /*
      2 Copyright 2021 mtei
      3 
      4 This program is free software: you can redistribute it and/or modify
      5 it under the terms of the GNU General Public License as published by
      6 the Free Software Foundation, either version 2 of the License, or
      7 (at your option) any later version.
      8 
      9 This program is distributed in the hope that it will be useful,
     10 but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 GNU General Public License for more details.
     13 
     14 You should have received a copy of the GNU General Public License
     15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 */
     17 // clang-format off
     18 
     19 #include "atomic_util.h"
     20 #include "gpio.h"
     21 #include "wait.h"
     22 
     23 #if defined(MATRIX_EXTENSION_74HC157)
     24 #    define MATRIX_DEVICES MCU_GPIOa, MCU_GPIOb
     25 #    define IS_74HC15x(dev) ((dev)==MCU_GPIOa || (dev)==MCU_GPIOb)
     26 #    define MATRIX_EXT_74HC15x MATRIX_EXTENSION_74HC157
     27 #elif defined(MATRIX_EXTENSION_74HC153)
     28 #    define MATRIX_DEVICES MCU_GPIOa, MCU_GPIOb, MCU_GPIOc, MCU_GPIOd
     29 #    define IS_74HC15x(dev) ((dev)==MCU_GPIOa || (dev)==MCU_GPIOb || (dev)==MCU_GPIOc || (dev)==MCU_GPIOd)
     30 #    define MATRIX_EXT_74HC15x MATRIX_EXTENSION_74HC153
     31 #endif
     32 
     33 static const pin_t sel_pins[] = { MATRIX_EXT_74HC15x };
     34 
     35 #ifdef MATRIX_GPIO_NEED_SEPARATE_ATOMIC
     36 #    define setMatrixInputHigh(dev, port, bit) \
     37     do {                                            \
     38         if ((dev) == MCU_GPIO || IS_74HC15x(dev)) { \
     39             setPortBitInputHigh_atomic(port, bit);  \
     40         }
     41     } while(0)
     42 #else
     43 #    define setMatrixInputHigh(dev, port, bit) \
     44     do {                                            \
     45         if ((dev) == MCU_GPIO || IS_74HC15x(dev)) { \
     46             setPortBitInputHigh(port, bit);         \
     47         }                                           \
     48     } while(0)
     49 #endif
     50 
     51 LOCAL_FUNC ALWAYS_INLINE void select74HC15x(uint8_t devid);
     52 LOCAL_FUNC
     53 void select74HC15x(uint8_t devid) {
     54     gpio_write_pin(sel_pins[0], devid&1);
     55 #if defined(MATRIX_EXTENSION_74HC153)
     56     gpio_write_pin(sel_pins[1], devid&2);
     57 #endif
     58 }
     59 
     60 LOCAL_FUNC ALWAYS_INLINE port_width_t readPortMultiplexer(uint8_t devid, pin_t port);
     61 LOCAL_FUNC port_width_t readPortMultiplexer(uint8_t devid, pin_t port) {
     62     select74HC15x(devid);
     63     waitInputPinDelay();
     64     return readPort(port);
     65 }
     66 
     67 #define readMatrixPort(dev, port) \
     68     ((dev) == MCU_GPIO)? readPort(port): (IS_74HC15x(dev))? readPortMultiplexer((dev)-MCU_GPIOa, port):0
     69 
     70 #define INIT_74HC15X(x) gpio_set_pin_output(x); gpio_write_pin_low(x);
     71 LOCAL_FUNC
     72 void init_74hc15x(void) {
     73     MAP(INIT_74HC15X, MATRIX_EXT_74HC15x)
     74 }
     75 #define init_extension() init_74hc15x()
     76