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.

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