logo

qmk_firmware

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

matrix.c (3663B)


  1. // Copyright 2023 David Hoelscher (@customMK)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "matrix.h"
  4. #include <string.h>
  5. // Pin definitions
  6. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  7. void matrix_wait_for_pin(pin_t pin, uint8_t target_state) {
  8. rtcnt_t start = chSysGetRealtimeCounterX();
  9. rtcnt_t end = start + 5000;
  10. while (chSysIsCounterWithinX(chSysGetRealtimeCounterX(), start, end)) {
  11. if (gpio_read_pin(pin) == target_state) {
  12. break;
  13. }
  14. }
  15. }
  16. void matrix_wait_for_port(stm32_gpio_t *port, uint32_t target_bitmask) {
  17. rtcnt_t start = chSysGetRealtimeCounterX();
  18. rtcnt_t end = start + 5000;
  19. while (chSysIsCounterWithinX(chSysGetRealtimeCounterX(), start, end)) {
  20. if ((palReadPort(port) & target_bitmask) == target_bitmask) {
  21. break;
  22. }
  23. }
  24. }
  25. void shift_pulse_clock(void) {
  26. gpio_write_pin_high(COL_SHIFT_CLK_PIN);
  27. matrix_wait_for_pin(COL_SHIFT_CLK_PIN, 1);
  28. gpio_write_pin_low(COL_SHIFT_CLK_PIN);
  29. }
  30. void matrix_init_custom(void) {
  31. //set all row pins as input with pullups
  32. for (int i = 0; i < MATRIX_ROWS; ++i) {
  33. gpio_write_pin_high(row_pins[i]);
  34. gpio_set_pin_input_high(row_pins[i]);
  35. }
  36. //set all column pins high in ROW2COL matrix
  37. gpio_set_pin_output(COL_SHIFT_IN_PIN);
  38. gpio_set_pin_output(COL_SHIFT_CLK_PIN);
  39. gpio_write_pin_high(COL_SHIFT_IN_PIN);
  40. matrix_wait_for_pin(COL_SHIFT_IN_PIN, 1);
  41. for (int i = 0; i < MATRIX_COLS; ++i) {
  42. shift_pulse_clock();
  43. }
  44. }
  45. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  46. static matrix_row_t temp_matrix[MATRIX_ROWS] = {0};
  47. gpio_write_pin_low(COL_SHIFT_IN_PIN);
  48. matrix_wait_for_pin(COL_SHIFT_IN_PIN, 0);
  49. // Setup the output column pin
  50. shift_pulse_clock();
  51. gpio_write_pin_high(COL_SHIFT_IN_PIN);
  52. for (int current_col = 0; current_col < MATRIX_COLS; ++current_col) {
  53. // Read the column ports
  54. uint32_t gpio_a = palReadPort(GPIOA);
  55. uint32_t gpio_b = palReadPort(GPIOB);
  56. // row 0, pin A8
  57. int current_row = 0;
  58. if ((gpio_a & (1 << 8)) >> 8) {
  59. temp_matrix[current_row] &= ~(1ul << current_col);
  60. } else {
  61. temp_matrix[current_row] |= (1ul << current_col);
  62. }
  63. // row 1, pin A1
  64. current_row = 1;
  65. if ((gpio_a & (1 << 1)) >> 1) {
  66. temp_matrix[current_row] &= ~(1ul << current_col);
  67. } else {
  68. temp_matrix[current_row] |= (1ul << current_col);
  69. }
  70. // row 2, pin A2
  71. current_row = 2;
  72. if ((gpio_a & (1 << 2)) >> 2) {
  73. temp_matrix[current_row] &= ~(1ul << current_col);
  74. } else {
  75. temp_matrix[current_row] |= (1ul << current_col);
  76. }
  77. // row 3, pin B3
  78. current_row = 3;
  79. if ((gpio_b & (1 << 1)) >> 1) {
  80. temp_matrix[current_row] &= ~(1ul << current_col);
  81. } else {
  82. temp_matrix[current_row] |= (1ul << current_col);
  83. }
  84. // row 4, pin A7
  85. current_row = 4;
  86. if ((gpio_a & (1 << 7)) >> 7) {
  87. temp_matrix[current_row] &= ~(1ul << current_col);
  88. } else {
  89. temp_matrix[current_row] |= (1ul << current_col);
  90. }
  91. // Setup the output column pin
  92. shift_pulse_clock();
  93. }
  94. // Check if matrix has changed, return the last-read data
  95. bool changed = memcmp(current_matrix, temp_matrix, sizeof(temp_matrix)) != 0;
  96. if (changed) {
  97. memcpy(current_matrix, temp_matrix, sizeof(temp_matrix));
  98. }
  99. shift_pulse_clock();
  100. return changed;
  101. }