matrix.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 {
  62. gpio_set_pin_input_high(pin);
  63. }
  64. }
  65. // matrix code
  66. #ifdef DIRECT_PINS
  67. static void init_pins(void) {
  68. for (int row = 0; row < MATRIX_ROWS; row++) {
  69. for (int col = 0; col < MATRIX_COLS; col++) {
  70. pin_t pin = direct_pins[row][col];
  71. if (pin != NO_PIN) {
  72. gpio_set_pin_input_high(pin);
  73. }
  74. }
  75. }
  76. }
  77. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  78. // Start with a clear matrix row
  79. matrix_row_t current_row_value = 0;
  80. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  81. pin_t pin = direct_pins[current_row][col_index];
  82. if (pin != NO_PIN) {
  83. current_row_value |= gpio_read_pin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  84. }
  85. }
  86. // If the row has changed, store the row and return the changed flag.
  87. if (current_matrix[current_row] != current_row_value) {
  88. current_matrix[current_row] = current_row_value;
  89. return true;
  90. }
  91. return false;
  92. }
  93. #elif defined(DIODE_DIRECTION)
  94. # if (DIODE_DIRECTION == COL2ROW)
  95. static void select_row(uint8_t row) {
  96. gpio_atomic_set_pin_output_low(row_pins[row]);
  97. }
  98. static void unselect_row(uint8_t row) {
  99. gpio_atomic_set_pin_input_high(row_pins[row]);
  100. }
  101. static void unselect_rows(void) {
  102. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  103. gpio_atomic_set_pin_input_high(row_pins[x]);
  104. }
  105. }
  106. static void init_pins(void) {
  107. # ifdef MATRIX_MUL_SELECT
  108. gpio_set_pin_output(MATRIX_MUL_SELECT);
  109. gpio_write_pin_low(MATRIX_MUL_SELECT);
  110. # endif
  111. unselect_rows();
  112. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  113. gpio_atomic_set_pin_input_high(col_pins[x]);
  114. }
  115. }
  116. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  117. // Start with a clear matrix row
  118. matrix_row_t current_row_value = 0;
  119. // Select row
  120. select_row(current_row);
  121. matrix_output_select_delay();
  122. // For each col...
  123. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  124. // Select the col pin to read (active low)
  125. # ifdef MATRIX_MUL_SELECT
  126. gpio_write_pin(MATRIX_MUL_SELECT, col_sel[col_index]);
  127. waitInputPinDelay();
  128. # endif
  129. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  130. // Populate the matrix row with the state of the col pin
  131. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  132. }
  133. // Unselect row
  134. unselect_row(current_row);
  135. # ifdef MATRIX_IO_DELAY_PORTS
  136. if (current_row_value) { // wait for col signal to go HIGH
  137. bool is_pressed;
  138. do {
  139. MATRIX_DEBUG_DELAY_START();
  140. is_pressed = false;
  141. for (uint8_t i = 0; i < ARRAY_SIZE(delay_ports); i++) {
  142. # ifdef MATRIX_IO_DELAY_MULSEL
  143. gpio_write_pin(MATRIX_MUL_SELECT, delay_sel[i]);
  144. waitInputPinDelay();
  145. # endif
  146. is_pressed |= ((readPort(delay_ports[i]) & delay_masks[i]) != delay_masks[i]);
  147. }
  148. MATRIX_DEBUG_DELAY_END();
  149. } while (is_pressed);
  150. }
  151. # endif
  152. # ifdef MATRIX_IO_DELAY_ADAPTIVE
  153. if (current_row_value) { // wait for col signal to go HIGH
  154. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  155. MATRIX_DEBUG_DELAY_START();
  156. # ifdef MATRIX_MUL_SELECT
  157. gpio_write_pin(MATRIX_MUL_SELECT, col_sel[col_index]);
  158. waitInputPinDelay();
  159. # endif
  160. while (gpio_read_pin(col_pins[col_index]) == 0) {
  161. }
  162. MATRIX_DEBUG_DELAY_END();
  163. }
  164. }
  165. # endif
  166. # ifdef MATRIX_IO_DELAY_ADAPTIVE2
  167. if (current_row_value) { // wait for col signal to go HIGH
  168. pin_t state;
  169. do {
  170. MATRIX_DEBUG_DELAY_START();
  171. state = 0;
  172. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  173. MATRIX_DEBUG_DELAY_END();
  174. MATRIX_DEBUG_DELAY_START();
  175. # ifdef MATRIX_MUL_SELECT
  176. gpio_write_pin(MATRIX_MUL_SELECT, col_sel[col_index]);
  177. waitInputPinDelay();
  178. # endif
  179. state |= (gpio_read_pin(col_pins[col_index]) == 0);
  180. }
  181. MATRIX_DEBUG_DELAY_END();
  182. } while (state);
  183. }
  184. # endif
  185. if (MATRIX_IO_DELAY_ALWAYS || current_row + 1 < MATRIX_ROWS) {
  186. MATRIX_DEBUG_DELAY_START();
  187. matrix_output_unselect_delay(current_row, current_row_value != 0); // wait for col signal to go HIGH
  188. MATRIX_DEBUG_DELAY_END();
  189. }
  190. // If the row has changed, store the row and return the changed flag.
  191. if (current_matrix[current_row] != current_row_value) {
  192. current_matrix[current_row] = current_row_value;
  193. return true;
  194. }
  195. return false;
  196. }
  197. # elif (DIODE_DIRECTION == ROW2COL)
  198. static void select_col(uint8_t col) {
  199. gpio_atomic_set_pin_output_low(col_pins[col]);
  200. }
  201. static void unselect_col(uint8_t col) {
  202. gpio_atomic_set_pin_input_high(col_pins[col]);
  203. }
  204. static void unselect_cols(void) {
  205. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  206. gpio_atomic_set_pin_input_high(col_pins[x]);
  207. }
  208. }
  209. static void init_pins(void) {
  210. unselect_cols();
  211. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  212. gpio_atomic_set_pin_input_high(row_pins[x]);
  213. }
  214. }
  215. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  216. bool matrix_changed = false;
  217. bool key_pressed = false;
  218. // Select col
  219. select_col(current_col);
  220. matrix_output_select_delay();
  221. // For each row...
  222. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  223. // Store last value of row prior to reading
  224. matrix_row_t last_row_value = current_matrix[row_index];
  225. matrix_row_t current_row_value = last_row_value;
  226. // Check row pin state
  227. if (gpio_read_pin(row_pins[row_index]) == 0) {
  228. // Pin LO, set col bit
  229. current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
  230. key_pressed = true;
  231. } else {
  232. // Pin HI, clear col bit
  233. current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
  234. }
  235. // Determine if the matrix changed state
  236. if ((last_row_value != current_row_value)) {
  237. matrix_changed |= true;
  238. current_matrix[row_index] = current_row_value;
  239. }
  240. }
  241. // Unselect col
  242. unselect_col(current_col);
  243. if (MATRIX_IO_DELAY_ALWAYS || current_col + 1 < MATRIX_COLS) {
  244. matrix_output_unselect_delay(current_col, key_pressed); // wait for col signal to go HIGH
  245. }
  246. return matrix_changed;
  247. }
  248. # else
  249. # error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
  250. # endif
  251. #else
  252. # error DIODE_DIRECTION is not defined!
  253. #endif
  254. void matrix_init(void) {
  255. // initialize key pins
  256. init_pins();
  257. // initialize matrix state: all keys off
  258. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  259. raw_matrix[i] = 0;
  260. matrix[i] = 0;
  261. }
  262. debounce_init();
  263. matrix_init_kb();
  264. }
  265. uint8_t matrix_scan(void) {
  266. bool changed = false;
  267. MATRIX_DEBUG_PIN_INIT();
  268. MATRIX_DEBUG_SCAN_START();
  269. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  270. // Set row, read cols
  271. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  272. changed |= read_cols_on_row(raw_matrix, current_row);
  273. }
  274. #elif (DIODE_DIRECTION == ROW2COL)
  275. // Set col, read rows
  276. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  277. changed |= read_rows_on_col(raw_matrix, current_col);
  278. }
  279. #endif
  280. MATRIX_DEBUG_SCAN_END();
  281. MATRIX_DEBUG_GAP();
  282. MATRIX_DEBUG_SCAN_START();
  283. debounce(raw_matrix, matrix, changed);
  284. MATRIX_DEBUG_SCAN_END();
  285. MATRIX_DEBUG_GAP();
  286. MATRIX_DEBUG_SCAN_START();
  287. matrix_scan_kb();
  288. MATRIX_DEBUG_SCAN_END();
  289. return (uint8_t)changed;
  290. }