logo

qmk_firmware

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

matrix.c (8803B)


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