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.

362 lines
9.3 KiB

  1. /*
  2. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  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 "matrix.h"
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <avr/io.h>
  18. #include "wait.h"
  19. #include "action_layer.h"
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include QMK_KEYBOARD_H
  24. #ifdef DEBUG_MATRIX_SCAN_RATE
  25. #include "timer.h"
  26. #endif
  27. #ifndef DEBOUNCE
  28. # define DEBOUNCE 5
  29. #endif
  30. // ATmega pin defs
  31. #define ROW1 (1<<5)
  32. #define COL6 (1<<0)
  33. #define COL7 (1<<1)
  34. #define COL8 (1<<2)
  35. #define COL9 (1<<3)
  36. #define COL10 (1<<2)
  37. #define COL11 (1<<3)
  38. // bit masks
  39. #define BMASK (COL7 | COL8 | COL9 | COL6)
  40. #define DMASK (COL10 | COL11)
  41. #define FMASK (ROW1)
  42. /* matrix state(1:on, 0:off) */
  43. static matrix_row_t matrix[MATRIX_ROWS];
  44. /*
  45. * matrix state(1:on, 0:off)
  46. * contains the raw values without debounce filtering of the last read cycle.
  47. */
  48. static matrix_row_t raw_matrix[MATRIX_ROWS];
  49. // Debouncing: store for each key the number of scans until it's eligible to
  50. // change. When scanning the matrix, ignore any changes in keys that have
  51. // already changed in the last DEBOUNCE scans.
  52. static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
  53. static matrix_row_t read_cols(uint8_t row);
  54. static void init_cols(void);
  55. static void unselect_rows(void);
  56. static void select_row(uint8_t row);
  57. static uint8_t mcp23018_reset_loop;
  58. // static uint16_t mcp23018_reset_loop;
  59. #ifdef DEBUG_MATRIX_SCAN_RATE
  60. uint32_t matrix_timer;
  61. uint32_t matrix_scan_count;
  62. #endif
  63. __attribute__ ((weak))
  64. void matrix_init_user(void) {}
  65. __attribute__ ((weak))
  66. void matrix_scan_user(void) {}
  67. __attribute__ ((weak))
  68. void matrix_init_kb(void) {
  69. matrix_init_user();
  70. }
  71. __attribute__ ((weak))
  72. void matrix_scan_kb(void) {
  73. matrix_scan_user();
  74. }
  75. inline
  76. uint8_t matrix_rows(void)
  77. {
  78. return MATRIX_ROWS;
  79. }
  80. inline
  81. uint8_t matrix_cols(void)
  82. {
  83. return MATRIX_COLS;
  84. }
  85. void matrix_init(void)
  86. {
  87. // initialize row and col
  88. mcp23018_status = init_mcp23018();
  89. unselect_rows();
  90. init_cols();
  91. // initialize matrix state: all keys off
  92. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  93. matrix[i] = 0;
  94. raw_matrix[i] = 0;
  95. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  96. debounce_matrix[i * MATRIX_COLS + j] = 0;
  97. }
  98. }
  99. #ifdef DEBUG_MATRIX_SCAN_RATE
  100. matrix_timer = timer_read32();
  101. matrix_scan_count = 0;
  102. #endif
  103. matrix_init_quantum();
  104. }
  105. void matrix_power_up(void) {
  106. mcp23018_status = init_mcp23018();
  107. unselect_rows();
  108. init_cols();
  109. // initialize matrix state: all keys off
  110. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  111. matrix[i] = 0;
  112. }
  113. #ifdef DEBUG_MATRIX_SCAN_RATE
  114. matrix_timer = timer_read32();
  115. matrix_scan_count = 0;
  116. #endif
  117. }
  118. // Returns a matrix_row_t whose bits are set if the corresponding key should be
  119. // eligible to change in this scan.
  120. matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
  121. matrix_row_t result = 0;
  122. matrix_row_t change = rawcols ^ raw_matrix[row];
  123. raw_matrix[row] = rawcols;
  124. for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
  125. if (debounce_matrix[row * MATRIX_COLS + i]) {
  126. --debounce_matrix[row * MATRIX_COLS + i];
  127. } else {
  128. result |= (1 << i);
  129. }
  130. if (change & (1 << i)) {
  131. debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
  132. }
  133. }
  134. return result;
  135. }
  136. matrix_row_t debounce_read_cols(uint8_t row) {
  137. // Read the row without debouncing filtering and store it for later usage.
  138. matrix_row_t cols = read_cols(row);
  139. // Get the Debounce mask.
  140. matrix_row_t mask = debounce_mask(cols, row);
  141. // debounce the row and return the result.
  142. return (cols & mask) | (matrix[row] & ~mask);;
  143. }
  144. uint8_t matrix_scan(void)
  145. {
  146. // Then the keyboard
  147. if (mcp23018_status) { // if there was an error
  148. if (++mcp23018_reset_loop == 0) {
  149. // if (++mcp23018_reset_loop >= 1300) {
  150. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  151. // this will be approx bit more frequent than once per second
  152. print("trying to reset mcp23018\n");
  153. mcp23018_status = init_mcp23018();
  154. if (mcp23018_status) {
  155. print("left side not responding\n");
  156. } else {
  157. print("left side attached\n");
  158. }
  159. }
  160. }
  161. #ifdef DEBUG_MATRIX_SCAN_RATE
  162. matrix_scan_count++;
  163. uint32_t timer_now = timer_read32();
  164. if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
  165. print("matrix scan frequency: ");
  166. pdec(matrix_scan_count);
  167. print("\n");
  168. matrix_timer = timer_now;
  169. matrix_scan_count = 0;
  170. }
  171. #endif
  172. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  173. select_row(i);
  174. // and select on left hand
  175. select_row(i + MATRIX_ROWS_PER_SIDE);
  176. // we don't need a 30us delay anymore, because selecting a
  177. // left-hand row requires more than 30us for i2c.
  178. // grab cols from left hand
  179. matrix[i] = debounce_read_cols(i);
  180. // grab cols from right hand
  181. matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
  182. unselect_rows();
  183. }
  184. matrix_scan_quantum();
  185. #ifdef DEBUG_MATRIX
  186. for (uint8_t c = 0; c < MATRIX_COLS; c++)
  187. for (uint8_t r = 0; r < MATRIX_ROWS; r++)
  188. if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
  189. #endif
  190. return 1;
  191. }
  192. bool matrix_is_modified(void) // deprecated and evidently not called.
  193. {
  194. return true;
  195. }
  196. inline
  197. bool matrix_is_on(uint8_t row, uint8_t col)
  198. {
  199. return (matrix[row] & ((matrix_row_t)1<<col));
  200. }
  201. inline
  202. matrix_row_t matrix_get_row(uint8_t row)
  203. {
  204. return matrix[row];
  205. }
  206. void matrix_print(void)
  207. {
  208. print("\nr/c 0123456789ABCDEF\n");
  209. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  210. phex(row); print(": ");
  211. pbin_reverse16(matrix_get_row(row));
  212. print("\n");
  213. }
  214. }
  215. uint8_t matrix_key_count(void)
  216. {
  217. uint8_t count = 0;
  218. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  219. count += bitpop16(matrix[i]);
  220. }
  221. return count;
  222. }
  223. // Remember this means ROWS
  224. static void init_cols(void)
  225. {
  226. // init on mcp23018
  227. // not needed, already done as part of init_mcp23018()
  228. // Input with pull-up(DDR:0, PORT:1)
  229. DDRF &= ~FMASK;
  230. PORTF |= FMASK;
  231. }
  232. static matrix_row_t read_cols(uint8_t row)
  233. {
  234. if (row < 6) {
  235. if (mcp23018_status) { // if there was an error
  236. return 0;
  237. } else {
  238. uint8_t data = 0;
  239. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  240. mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  241. mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  242. mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
  243. data = (~((uint8_t)mcp23018_status) >> 2) & 0x01 ;
  244. mcp23018_status = I2C_STATUS_SUCCESS;
  245. out:
  246. i2c_stop();
  247. #ifdef DEBUG_MATRIX
  248. if (data != 0x00) xprintf("I2C: %d\n", data);
  249. #endif
  250. return data;
  251. }
  252. } else {
  253. // Read using bitmask
  254. return ~((((PINF & ROW1) >> 5)) & 0x1);
  255. }
  256. }
  257. // Row pin configuration
  258. static void unselect_rows(void)
  259. {
  260. // no need to unselect on mcp23018, because the select step sets all
  261. // the other row bits high, and it's not changing to a different
  262. // direction
  263. // Hi-Z(DDR:0, PORT:0) to unselect
  264. DDRB &= ~BMASK;
  265. PORTB &= ~BMASK;
  266. DDRD &= ~DMASK;
  267. PORTD &= ~DMASK;
  268. }
  269. static void select_row(uint8_t row)
  270. {
  271. if (row < 6) {
  272. // select on mcp23018
  273. if (mcp23018_status) { // do nothing on error
  274. // Read using bitmask
  275. } else { // set active row low : 0 // set other rows hi-Z : 1
  276. mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  277. mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  278. mcp23018_status = i2c_write(~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
  279. out:
  280. i2c_stop();
  281. }
  282. } else {
  283. // Output low(DDR:1, PORT:0) to select
  284. switch (row) {
  285. case 6:
  286. DDRB |= COL6;
  287. PORTB &= ~COL6;
  288. break;
  289. case 7:
  290. DDRB |= COL7;
  291. PORTB &= ~COL7;
  292. break;
  293. case 8:
  294. DDRB |= COL8;
  295. PORTB &= ~COL8;
  296. break;
  297. case 9:
  298. DDRB |= COL9;
  299. PORTB &= ~COL9;
  300. break;
  301. case 10:
  302. DDRD |= COL10;
  303. PORTD &= ~COL10;
  304. break;
  305. case 11:
  306. DDRD |= COL11;
  307. PORTD &= ~COL11;
  308. break;
  309. }
  310. }
  311. }