logo

qmk_firmware

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

matrix.c (10310B)


  1. /*
  2. Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "atomic_util.h"
  15. #include "util.h"
  16. #include "wait.h"
  17. #include "matrix.h"
  18. #include "debounce.h"
  19. #ifndef readPort
  20. # include "gpio_extr.h"
  21. #endif
  22. #ifndef MATRIX_DEBUG_PIN
  23. # define MATRIX_DEBUG_PIN_INIT()
  24. # define MATRIX_DEBUG_SCAN_START()
  25. # define MATRIX_DEBUG_SCAN_END()
  26. # define MATRIX_DEBUG_DELAY_START()
  27. # define MATRIX_DEBUG_DELAY_END()
  28. # define MATRIX_DEBUG_GAP()
  29. #else
  30. # define MATRIX_DEBUG_GAP() asm volatile("nop \n nop" ::: "memory")
  31. #endif
  32. #ifndef MATRIX_IO_DELAY_ALWAYS
  33. # define MATRIX_IO_DELAY_ALWAYS 0
  34. #endif
  35. #ifdef DIRECT_PINS
  36. static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
  37. #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  38. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  39. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  40. # ifdef MATRIX_MUL_SELECT
  41. static const pin_t col_sel[MATRIX_COLS] = MATRIX_MUL_SEL;
  42. # endif
  43. #endif
  44. #ifdef MATRIX_IO_DELAY_PORTS
  45. static const pin_t delay_ports[] = {MATRIX_IO_DELAY_PORTS};
  46. static const port_data_t delay_masks[] = {MATRIX_IO_DELAY_MASKS};
  47. # ifdef MATRIX_IO_DELAY_MULSEL
  48. static const uint8_t delay_sel[] = {MATRIX_IO_DELAY_MULSEL};
  49. # endif
  50. #endif
  51. /* matrix state(1:on, 0:off) */
  52. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  53. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  54. static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
  55. ATOMIC_BLOCK_FORCEON {
  56. gpio_set_pin_output(pin);
  57. gpio_write_pin_low(pin);
  58. }
  59. }
  60. static inline void gpio_atomic_set_pin_input_high(pin_t pin) {
  61. ATOMIC_BLOCK_FORCEON { gpio_set_pin_input_high(pin); }
  62. }
  63. // matrix code
  64. #ifdef DIRECT_PINS
  65. static void init_pins(void) {
  66. for (int row = 0; row < MATRIX_ROWS; row++) {
  67. for (int col = 0; col < MATRIX_COLS; col++) {
  68. pin_t pin = direct_pins[row][col];
  69. if (pin != NO_PIN) {
  70. gpio_set_pin_input_high(pin);
  71. }
  72. }
  73. }
  74. }
  75. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  76. // Start with a clear matrix row
  77. matrix_row_t current_row_value = 0;
  78. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  79. pin_t pin = direct_pins[current_row][col_index];
  80. if (pin != NO_PIN) {
  81. current_row_value |= gpio_read_pin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  82. }
  83. }
  84. // If the row has changed, store the row and return the changed flag.
  85. if (current_matrix[current_row] != current_row_value) {
  86. current_matrix[current_row] = current_row_value;
  87. return true;
  88. }
  89. return false;
  90. }
  91. #elif defined(DIODE_DIRECTION)
  92. # if (DIODE_DIRECTION == COL2ROW)
  93. static void select_row(uint8_t row) { gpio_atomic_set_pin_output_low(row_pins[row]); }
  94. static void unselect_row(uint8_t row) { gpio_atomic_set_pin_input_high(row_pins[row]); }
  95. static void unselect_rows(void) {
  96. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  97. gpio_atomic_set_pin_input_high(row_pins[x]);
  98. }
  99. }
  100. static void init_pins(void) {
  101. # ifdef MATRIX_MUL_SELECT
  102. gpio_set_pin_output(MATRIX_MUL_SELECT);
  103. gpio_write_pin_low(MATRIX_MUL_SELECT);
  104. # endif
  105. unselect_rows();
  106. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  107. gpio_atomic_set_pin_input_high(col_pins[x]);
  108. }
  109. }
  110. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  111. // Start with a clear matrix row
  112. matrix_row_t current_row_value = 0;
  113. // Select row
  114. select_row(current_row);
  115. matrix_output_select_delay();
  116. // For each col...
  117. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  118. // Select the col pin to read (active low)
  119. # ifdef MATRIX_MUL_SELECT
  120. gpio_write_pin(MATRIX_MUL_SELECT, col_sel[col_index]);
  121. waitInputPinDelay();
  122. # endif
  123. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  124. // Populate the matrix row with the state of the col pin
  125. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  126. }
  127. // Unselect row
  128. unselect_row(current_row);
  129. # ifdef MATRIX_IO_DELAY_PORTS
  130. if (current_row_value) { // wait for col signal to go HIGH
  131. bool is_pressed;
  132. do {
  133. MATRIX_DEBUG_DELAY_START();
  134. is_pressed = false;
  135. for (uint8_t i = 0; i < ARRAY_SIZE(delay_ports); i++) {
  136. # ifdef MATRIX_IO_DELAY_MULSEL
  137. gpio_write_pin(MATRIX_MUL_SELECT, delay_sel[i]);
  138. waitInputPinDelay();
  139. # endif
  140. is_pressed |= ((readPort(delay_ports[i]) & delay_masks[i]) != delay_masks[i]);
  141. }
  142. MATRIX_DEBUG_DELAY_END();
  143. } while (is_pressed);
  144. }
  145. # endif
  146. # ifdef MATRIX_IO_DELAY_ADAPTIVE
  147. if (current_row_value) { // wait for col signal to go HIGH
  148. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  149. MATRIX_DEBUG_DELAY_START();
  150. # ifdef MATRIX_MUL_SELECT
  151. gpio_write_pin(MATRIX_MUL_SELECT, col_sel[col_index]);
  152. waitInputPinDelay();
  153. # endif
  154. while (gpio_read_pin(col_pins[col_index]) == 0) {
  155. }
  156. MATRIX_DEBUG_DELAY_END();
  157. }
  158. }
  159. # endif
  160. # ifdef MATRIX_IO_DELAY_ADAPTIVE2
  161. if (current_row_value) { // wait for col signal to go HIGH
  162. pin_t state;
  163. do {
  164. MATRIX_DEBUG_DELAY_START();
  165. state = 0;
  166. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  167. MATRIX_DEBUG_DELAY_END();
  168. MATRIX_DEBUG_DELAY_START();
  169. # ifdef MATRIX_MUL_SELECT
  170. gpio_write_pin(MATRIX_MUL_SELECT, col_sel[col_index]);
  171. waitInputPinDelay();
  172. # endif
  173. state |= (gpio_read_pin(col_pins[col_index]) == 0);
  174. }
  175. MATRIX_DEBUG_DELAY_END();
  176. } while (state);
  177. }
  178. # endif
  179. if (MATRIX_IO_DELAY_ALWAYS || current_row + 1 < MATRIX_ROWS) {
  180. MATRIX_DEBUG_DELAY_START();
  181. matrix_output_unselect_delay(current_row, current_row_value != 0); // wait for col signal to go HIGH
  182. MATRIX_DEBUG_DELAY_END();
  183. }
  184. // If the row has changed, store the row and return the changed flag.
  185. if (current_matrix[current_row] != current_row_value) {
  186. current_matrix[current_row] = current_row_value;
  187. return true;
  188. }
  189. return false;
  190. }
  191. # elif (DIODE_DIRECTION == ROW2COL)
  192. static void select_col(uint8_t col) { gpio_atomic_set_pin_output_low(col_pins[col]); }
  193. static void unselect_col(uint8_t col) { gpio_atomic_set_pin_input_high(col_pins[col]); }
  194. static void unselect_cols(void) {
  195. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  196. gpio_atomic_set_pin_input_high(col_pins[x]);
  197. }
  198. }
  199. static void init_pins(void) {
  200. unselect_cols();
  201. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  202. gpio_atomic_set_pin_input_high(row_pins[x]);
  203. }
  204. }
  205. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  206. bool matrix_changed = false;
  207. bool key_pressed = false;
  208. // Select col
  209. select_col(current_col);
  210. matrix_output_select_delay();
  211. // For each row...
  212. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  213. // Store last value of row prior to reading
  214. matrix_row_t last_row_value = current_matrix[row_index];
  215. matrix_row_t current_row_value = last_row_value;
  216. // Check row pin state
  217. if (gpio_read_pin(row_pins[row_index]) == 0) {
  218. // Pin LO, set col bit
  219. current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
  220. key_pressed = true;
  221. } else {
  222. // Pin HI, clear col bit
  223. current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
  224. }
  225. // Determine if the matrix changed state
  226. if ((last_row_value != current_row_value)) {
  227. matrix_changed |= true;
  228. current_matrix[row_index] = current_row_value;
  229. }
  230. }
  231. // Unselect col
  232. unselect_col(current_col);
  233. if (MATRIX_IO_DELAY_ALWAYS || current_col + 1 < MATRIX_COLS) {
  234. matrix_output_unselect_delay(current_col, key_pressed); // wait for col signal to go HIGH
  235. }
  236. return matrix_changed;
  237. }
  238. # else
  239. # error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
  240. # endif
  241. #else
  242. # error DIODE_DIRECTION is not defined!
  243. #endif
  244. void matrix_init(void) {
  245. // initialize key pins
  246. init_pins();
  247. // initialize matrix state: all keys off
  248. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  249. raw_matrix[i] = 0;
  250. matrix[i] = 0;
  251. }
  252. debounce_init(MATRIX_ROWS);
  253. matrix_init_kb();
  254. }
  255. uint8_t matrix_scan(void) {
  256. bool changed = false;
  257. MATRIX_DEBUG_PIN_INIT();
  258. MATRIX_DEBUG_SCAN_START();
  259. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  260. // Set row, read cols
  261. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  262. changed |= read_cols_on_row(raw_matrix, current_row);
  263. }
  264. #elif (DIODE_DIRECTION == ROW2COL)
  265. // Set col, read rows
  266. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  267. changed |= read_rows_on_col(raw_matrix, current_col);
  268. }
  269. #endif
  270. MATRIX_DEBUG_SCAN_END();
  271. MATRIX_DEBUG_GAP();
  272. MATRIX_DEBUG_SCAN_START();
  273. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  274. MATRIX_DEBUG_SCAN_END();
  275. MATRIX_DEBUG_GAP();
  276. MATRIX_DEBUG_SCAN_START();
  277. matrix_scan_kb();
  278. MATRIX_DEBUG_SCAN_END();
  279. return (uint8_t)changed;
  280. }