matrix.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <stdint.h>
  20. #include <stdbool.h>
  21. #include <string.h>
  22. #include "quantum.h"
  23. #include "matrix.h"
  24. #include "tca6424.h"
  25. #include "abelx.h"
  26. static const uint16_t col_pins[MATRIX_COLS] = CUSTOM_MATRIX_COL_PINS;
  27. void matrix_init_custom(void)
  28. {
  29. tca6424_init();
  30. // set port0
  31. tca6424_write_config(TCA6424_PORT0, 0);
  32. // set port1
  33. tca6424_write_config(TCA6424_PORT1, 0);
  34. // set port2
  35. tca6424_write_config(TCA6424_PORT2, 0xF5);
  36. // clear output
  37. tca6424_write_port(TCA6424_PORT0, 0);
  38. tca6424_write_port(TCA6424_PORT1, 0);
  39. tca6424_write_port(TCA6424_PORT2, 0);
  40. }
  41. static uint8_t row_mask[] = {ROW1_MASK,ROW2_MASK,ROW3_MASK,ROW4_MASK,ROW5_MASK,ROW6_MASK};
  42. 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};
  43. bool matrix_scan_custom(matrix_row_t current_matrix[])
  44. {
  45. bool changed = false;
  46. uint8_t p0_data = tca6424_read_port(TCA6424_PORT0);
  47. for (int col = 0; col < MATRIX_COLS; col++) {
  48. // Select col and wait for col selecton to stabilize
  49. switch(col) {
  50. case 0:
  51. set_pin(col_pins[col]);
  52. break;
  53. case 1 ... 8:
  54. tca6424_write_port(TCA6424_PORT1, col_mask[col]);
  55. break;
  56. default:
  57. tca6424_write_port(TCA6424_PORT0, col_mask[col]|(p0_data&0x01));
  58. break;
  59. }
  60. matrix_io_delay();
  61. // read row port for all rows
  62. uint8_t row_value = tca6424_read_port(ROW_PORT);
  63. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  64. uint8_t tmp = row;
  65. // Store last value of row prior to reading
  66. matrix_row_t last_row_value = current_matrix[tmp];
  67. // Check row pin state
  68. if (row_value & row_mask[row]) {
  69. // Pin HI, set col bit
  70. current_matrix[tmp] |= (1 << col);
  71. } else {
  72. // Pin LOW, clear col bit
  73. current_matrix[tmp] &= ~(1 << col);
  74. }
  75. // Determine if the matrix changed state
  76. if ((last_row_value != current_matrix[tmp]) && !(changed)) {
  77. changed = true;
  78. }
  79. }
  80. // Unselect col
  81. switch(col) {
  82. case 0:
  83. clear_pin(col_pins[col]);
  84. break;
  85. case 8:
  86. tca6424_write_port(TCA6424_PORT1, 0);
  87. break;
  88. case 15:
  89. tca6424_write_port(TCA6424_PORT0, p0_data&0x01);
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. return changed;
  96. }