logo

qmk_firmware

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

matrix.c (8461B)


  1. /*
  2. Note to self: adapted from ergodox EZ matrix
  3. The "column" and "row" in here actually refers to the opposite on the keyboard
  4. see definition of KEYMAP in v1.h, the grid is transposed so that a "row" in here is actually a "column" on the physical keyboard
  5. Nicolas
  6. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  7. Copyright 2013 Nicolas Poirey <nicolas.poirey@gmail.com>
  8. This program is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation, either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * scan matrix
  21. */
  22. #include <stdint.h>
  23. #include <stdbool.h>
  24. #include <avr/io.h>
  25. #include "wait.h"
  26. #include "action_layer.h"
  27. #include "print.h"
  28. #include "debug.h"
  29. #include "util.h"
  30. #include "matrix.h"
  31. #include "frenchdev.h"
  32. /*
  33. * This constant define not debouncing time in msecs, but amount of matrix
  34. * scan loops which should be made to get stable debounced results.
  35. *
  36. * On Ergodox matrix scan rate is relatively low, because of slow I2C.
  37. * Now it's only 317 scans/second, or about 3.15 msec/scan.
  38. * According to Cherry specs, debouncing time is 5 msec.
  39. *
  40. * And so, there is no sense to have DEBOUNCE higher than 2.
  41. */
  42. #ifndef DEBOUNCE
  43. # define DEBOUNCE 5
  44. #endif
  45. static uint8_t debouncing = DEBOUNCE;
  46. /* matrix state(1:on, 0:off) */
  47. static matrix_row_t matrix[MATRIX_ROWS];
  48. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  49. static matrix_row_t read_cols(uint8_t row);
  50. static void init_cols(void);
  51. static void unselect_rows(void);
  52. static void select_row(uint8_t row);
  53. static uint8_t mcp23018_reset_loop;
  54. __attribute__ ((weak))
  55. void matrix_init_user(void) {}
  56. __attribute__ ((weak))
  57. void matrix_scan_user(void) {}
  58. __attribute__ ((weak))
  59. void matrix_init_kb(void) {
  60. matrix_init_user();
  61. }
  62. __attribute__ ((weak))
  63. void matrix_scan_kb(void) {
  64. matrix_scan_user();
  65. }
  66. inline
  67. uint8_t matrix_rows(void)
  68. {
  69. return MATRIX_ROWS;
  70. }
  71. inline
  72. uint8_t matrix_cols(void)
  73. {
  74. return MATRIX_COLS;
  75. }
  76. void matrix_init(void)
  77. {
  78. // initialize row and col
  79. debug_enable = true;
  80. debug_matrix = true;
  81. debug_keyboard = true;
  82. debug_mouse = true;
  83. mcp23018_status = init_mcp23018();
  84. unselect_rows();
  85. init_cols();
  86. // initialize matrix state: all keys off
  87. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  88. matrix[i] = 0;
  89. matrix_debouncing[i] = 0;
  90. }
  91. matrix_init_kb();
  92. }
  93. void matrix_power_up(void) {
  94. mcp23018_status = init_mcp23018();
  95. unselect_rows();
  96. init_cols();
  97. // initialize matrix state: all keys off
  98. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  99. matrix[i] = 0;
  100. matrix_debouncing[i] = 0;
  101. }
  102. }
  103. uint8_t matrix_scan(void)
  104. {
  105. if (mcp23018_status) { // if there was an error
  106. if (++mcp23018_reset_loop == 0) {
  107. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  108. // this will be approx bit more frequent than once per second
  109. print("trying to reset mcp23018\n");
  110. mcp23018_status = init_mcp23018();
  111. if (mcp23018_status) {
  112. print("left side not responding\n");
  113. } else {
  114. print("left side attached\n");
  115. frenchdev_blink_all_leds();
  116. }
  117. }
  118. }
  119. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  120. select_row(i);
  121. wait_us(30); // without this wait read unstable value.
  122. matrix_row_t cols = read_cols(i);
  123. if (matrix_debouncing[i] != cols) {
  124. matrix_debouncing[i] = cols;
  125. if (debouncing) {
  126. dprintf("bounce!: %02X\n", debouncing);
  127. }
  128. debouncing = DEBOUNCE;
  129. }
  130. unselect_rows();
  131. }
  132. if (debouncing) {
  133. if (--debouncing) {
  134. wait_us(1);
  135. // this should be wait_ms(1) but has been left as-is at EZ's request
  136. } else {
  137. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  138. matrix[i] = matrix_debouncing[i];
  139. }
  140. }
  141. }
  142. matrix_scan_kb();
  143. return 1;
  144. }
  145. inline
  146. bool matrix_is_on(uint8_t row, uint8_t col)
  147. {
  148. return (matrix[row] & ((matrix_row_t)1<<col));
  149. }
  150. inline
  151. matrix_row_t matrix_get_row(uint8_t row)
  152. {
  153. return matrix[row];
  154. }
  155. void matrix_print(void)
  156. {
  157. print("\nr/c 0123456789ABCDEF\n");
  158. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  159. print_hex8(row); print(": ");
  160. print_bin_reverse16(matrix_get_row(row));
  161. print("\n");
  162. }
  163. }
  164. /* Column pin configuration
  165. *
  166. * Teensy
  167. * col: 0 1 2 3 4 5
  168. * pin: F0 F1 F4 F5 F6 F7
  169. *
  170. * MCP23018
  171. * col: 0 1 2 3 4 5
  172. * pin: B5 B4 B3 B2 B1 B0
  173. */
  174. static void init_cols(void)
  175. {
  176. // init on mcp23018
  177. // not needed, already done as part of init_mcp23018()
  178. // init on teensy
  179. // Input with pull-up(DDR:0, PORT:1)
  180. gpio_set_pin_input_high(F0);
  181. gpio_set_pin_input_high(F1);
  182. gpio_set_pin_input_high(F4);
  183. gpio_set_pin_input_high(F5);
  184. gpio_set_pin_input_high(F6);
  185. gpio_set_pin_input_high(F7);
  186. }
  187. static matrix_row_t read_cols(uint8_t row)
  188. {
  189. if (row < 8) {
  190. if (mcp23018_status) { // if there was an error
  191. return 0;
  192. } else {
  193. uint8_t data = 0;
  194. mcp23018_status = i2c_read_register(I2C_ADDR, GPIOB, &data, 1, I2C_TIMEOUT);
  195. return ~data;
  196. }
  197. } else {
  198. // read from teensy
  199. return
  200. (PINF&(1<<0) ? 0 : (1<<0)) |
  201. (PINF&(1<<1) ? 0 : (1<<1)) |
  202. (PINF&(1<<4) ? 0 : (1<<2)) |
  203. (PINF&(1<<5) ? 0 : (1<<3)) |
  204. (PINF&(1<<6) ? 0 : (1<<4)) |
  205. (PINF&(1<<7) ? 0 : (1<<5)) ;
  206. }
  207. }
  208. /* Row pin configuration
  209. *
  210. * Teensy
  211. * row: 7 8 9 10 11 12 13
  212. * pin: B0 B1 B2 B3 D2 D3 C6
  213. *
  214. * MCP23018
  215. * row: 0 1 2 3 4 5 6
  216. * pin: A0 A1 A2 A3 A4 A5 A6
  217. */
  218. static void unselect_rows(void)
  219. {
  220. // unselect on mcp23018
  221. if (mcp23018_status) { // if there was an error
  222. // do nothing
  223. } else {
  224. // set all rows hi-Z : 1
  225. uint8_t data;
  226. data = 0xFF & ~(0<<8);
  227. mcp23018_status = i2c_write_register(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
  228. }
  229. // unselect on teensy
  230. // Hi-Z(DDR:0, PORT:0) to unselect
  231. gpio_set_pin_input(B0);
  232. gpio_set_pin_input(B1);
  233. gpio_set_pin_input(B2);
  234. gpio_set_pin_input(B3);
  235. gpio_set_pin_input(D2);
  236. gpio_set_pin_input(D3);
  237. gpio_set_pin_input(C6);
  238. gpio_set_pin_input(C7);
  239. }
  240. static void select_row(uint8_t row)
  241. {
  242. if (row < 8) {
  243. // select on mcp23018
  244. if (mcp23018_status) { // if there was an error
  245. // do nothing
  246. } else {
  247. // set active row low : 0
  248. // set other rows hi-Z : 1
  249. uint8_t data = 0xFF & ~(1<<row) & ~(0<<8);
  250. mcp23018_status = i2c_write_register(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
  251. }
  252. } else {
  253. // select on teensy
  254. // Output low(DDR:1, PORT:0) to select
  255. switch (row) {
  256. case 8:
  257. gpio_set_pin_output(B0);
  258. gpio_write_pin_low(B0);
  259. break;
  260. case 9:
  261. gpio_set_pin_output(B1);
  262. gpio_write_pin_low(B1);
  263. break;
  264. case 10:
  265. gpio_set_pin_output(B2);
  266. gpio_write_pin_low(B2);
  267. break;
  268. case 11:
  269. gpio_set_pin_output(B3);
  270. gpio_write_pin_low(B3);
  271. break;
  272. case 12:
  273. gpio_set_pin_output(D2);
  274. gpio_write_pin_low(D2);
  275. break;
  276. case 13:
  277. gpio_set_pin_output(D3);
  278. gpio_write_pin_low(D3);
  279. break;
  280. case 14:
  281. gpio_set_pin_output(C6);
  282. gpio_write_pin_low(C6);
  283. break;
  284. case 15:
  285. gpio_set_pin_output(C7);
  286. gpio_write_pin_low(C7);
  287. break;
  288. }
  289. }
  290. }