logo

qmk_firmware

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

matrix.c (2605B)


  1. /**
  2. * matrix.c
  3. */
  4. #include "matrix.h"
  5. #include "tca6424.h"
  6. #include "m20add.h"
  7. static const uint16_t col_pins[MATRIX_COLS] = MATRIX_M20_COL_PINS;
  8. void matrix_init_custom(void)
  9. {
  10. tca6424_init();
  11. // set port0
  12. tca6424_write_config(TCA6424_PORT0, 0);
  13. // set port1
  14. tca6424_write_config(TCA6424_PORT1, 0);
  15. // set port2
  16. tca6424_write_config(TCA6424_PORT2, 0xF5);
  17. // clear output
  18. tca6424_write_port(TCA6424_PORT0, 0);
  19. tca6424_write_port(TCA6424_PORT1, 0);
  20. tca6424_write_port(TCA6424_PORT2, 0);
  21. }
  22. static uint8_t row_mask[] = {ROW1_MASK,ROW2_MASK,ROW3_MASK,ROW4_MASK,ROW5_MASK,ROW6_MASK};
  23. 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};
  24. bool matrix_scan_custom(matrix_row_t current_matrix[])
  25. {
  26. bool changed = false;
  27. uint8_t p0_data = tca6424_read_port(TCA6424_PORT0);
  28. for (int col = 0; col < MATRIX_COLS; col++) {
  29. // Select col and wait for col selecton to stabilize
  30. switch(col) {
  31. case 0:
  32. set_pin(col_pins[col]);
  33. break;
  34. case 1 ... 8:
  35. tca6424_write_port(TCA6424_PORT1, col_mask[col]);
  36. break;
  37. default:
  38. tca6424_write_port(TCA6424_PORT0, col_mask[col]|(p0_data&0x01));
  39. break;
  40. }
  41. matrix_io_delay();
  42. // read row port for all rows
  43. uint8_t row_value = tca6424_read_port(ROW_PORT);
  44. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  45. uint8_t tmp = row;
  46. // Store last value of row prior to reading
  47. matrix_row_t last_row_value = current_matrix[tmp];
  48. // Check row pin state
  49. if (row_value & row_mask[row]) {
  50. // Pin HI, set col bit
  51. current_matrix[tmp] |= (1 << col);
  52. } else {
  53. // Pin LOW, clear col bit
  54. current_matrix[tmp] &= ~(1 << col);
  55. }
  56. // Determine if the matrix changed state
  57. if ((last_row_value != current_matrix[tmp]) && !(changed)) {
  58. changed = true;
  59. }
  60. }
  61. // Unselect col
  62. switch(col) {
  63. case 0:
  64. clear_pin(col_pins[col]);
  65. break;
  66. case 8:
  67. tca6424_write_port(TCA6424_PORT1, 0);
  68. break;
  69. case 15:
  70. tca6424_write_port(TCA6424_PORT0, p0_data&0x01);
  71. break;
  72. default:
  73. break;
  74. }
  75. }
  76. return changed;
  77. }