logo

qmk_firmware

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

matrix.c (3303B)


  1. /**
  2. * matrix.c
  3. *
  4. * Copyright 2020 astro <yuleiz@gmail.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "matrix.h"
  20. #include "tca6424.h"
  21. #include "abelx.h"
  22. static const uint16_t col_pins[MATRIX_COLS] = CUSTOM_MATRIX_COL_PINS;
  23. void matrix_init_custom(void)
  24. {
  25. tca6424_init();
  26. // set port0
  27. tca6424_write_config(TCA6424_PORT0, 0);
  28. // set port1
  29. tca6424_write_config(TCA6424_PORT1, 0);
  30. // set port2
  31. tca6424_write_config(TCA6424_PORT2, 0xF5);
  32. // clear output
  33. tca6424_write_port(TCA6424_PORT0, 0);
  34. tca6424_write_port(TCA6424_PORT1, 0);
  35. tca6424_write_port(TCA6424_PORT2, 0);
  36. }
  37. static uint8_t row_mask[] = {ROW1_MASK,ROW2_MASK,ROW3_MASK,ROW4_MASK,ROW5_MASK,ROW6_MASK};
  38. static uint8_t col_mask[] = {COL1_MASK, COL2_MASK, COL3_MASK, COL4_MASK, COL5_MASK, COL6_MASK, COL7_MASK, COL8_MASK, COL9_MASK, COL10_MASK, COL11_MASK, COL12_MASK, COL13_MASK, COL14_MASK, COL15_MASK, COL16_MASK};
  39. bool matrix_scan_custom(matrix_row_t current_matrix[])
  40. {
  41. bool changed = false;
  42. uint8_t p0_data = tca6424_read_port(TCA6424_PORT0);
  43. for (int col = 0; col < MATRIX_COLS; col++) {
  44. // Select col and wait for col selecton to stabilize
  45. switch(col) {
  46. case 0:
  47. set_pin(col_pins[col]);
  48. break;
  49. case 1 ... 8:
  50. tca6424_write_port(TCA6424_PORT1, col_mask[col]);
  51. break;
  52. default:
  53. tca6424_write_port(TCA6424_PORT0, col_mask[col]|(p0_data&0x01));
  54. break;
  55. }
  56. matrix_io_delay();
  57. // read row port for all rows
  58. uint8_t row_value = tca6424_read_port(ROW_PORT);
  59. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  60. uint8_t tmp = row;
  61. // Store last value of row prior to reading
  62. matrix_row_t last_row_value = current_matrix[tmp];
  63. // Check row pin state
  64. if (row_value & row_mask[row]) {
  65. // Pin HI, set col bit
  66. current_matrix[tmp] |= (1 << col);
  67. } else {
  68. // Pin LOW, clear col bit
  69. current_matrix[tmp] &= ~(1 << col);
  70. }
  71. // Determine if the matrix changed state
  72. if ((last_row_value != current_matrix[tmp]) && !(changed)) {
  73. changed = true;
  74. }
  75. }
  76. // Unselect col
  77. switch(col) {
  78. case 0:
  79. clear_pin(col_pins[col]);
  80. break;
  81. case 8:
  82. tca6424_write_port(TCA6424_PORT1, 0);
  83. break;
  84. case 15:
  85. tca6424_write_port(TCA6424_PORT0, p0_data&0x01);
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. return changed;
  92. }