logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix_extension_74hc15x.c (2653B)


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