You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.6 KiB

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