matrix.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. Note for ErgoDox EZ customizers: Here be dragons!
  3. This is not a file you want to be messing with.
  4. All of the interesting stuff for you is under keymaps/ :)
  5. Love, Erez
  6. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * scan matrix
  20. */
  21. #include <stdint.h>
  22. #include <stdbool.h>
  23. #include <avr/io.h>
  24. #include "wait.h"
  25. #include "action_layer.h"
  26. #include "print.h"
  27. #include "debug.h"
  28. #include "util.h"
  29. #include "matrix.h"
  30. #include QMK_KEYBOARD_H
  31. #include "i2cmaster.h"
  32. #ifdef DEBUG_MATRIX_SCAN_RATE
  33. #include "timer.h"
  34. #endif
  35. /*
  36. * This constant define not debouncing time in msecs, but amount of matrix
  37. * scan loops which should be made to get stable debounced results.
  38. *
  39. * On Ergodox matrix scan rate is relatively low, because of slow I2C.
  40. * Now it's only 317 scans/second, or about 3.15 msec/scan.
  41. * According to Cherry specs, debouncing time is 5 msec.
  42. *
  43. * And so, there is no sense to have DEBOUNCE higher than 2.
  44. */
  45. #ifndef DEBOUNCE
  46. # define DEBOUNCE 5
  47. #endif
  48. /* matrix state(1:on, 0:off) */
  49. static matrix_row_t matrix[MATRIX_ROWS];
  50. // Debouncing: store for each key the number of scans until it's eligible to
  51. // change. When scanning the matrix, ignore any changes in keys that have
  52. // already changed in the last DEBOUNCE scans.
  53. static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
  54. static matrix_row_t read_cols(uint8_t row);
  55. static void init_cols(void);
  56. static void unselect_rows(void);
  57. static void select_row(uint8_t row);
  58. static uint8_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. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  95. debounce_matrix[i * MATRIX_COLS + j] = 0;
  96. }
  97. }
  98. #ifdef DEBUG_MATRIX_SCAN_RATE
  99. matrix_timer = timer_read32();
  100. matrix_scan_count = 0;
  101. #endif
  102. matrix_init_quantum();
  103. }
  104. void matrix_power_up(void) {
  105. mcp23018_status = init_mcp23018();
  106. unselect_rows();
  107. init_cols();
  108. // initialize matrix state: all keys off
  109. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  110. matrix[i] = 0;
  111. }
  112. #ifdef DEBUG_MATRIX_SCAN_RATE
  113. matrix_timer = timer_read32();
  114. matrix_scan_count = 0;
  115. #endif
  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(uint8_t row) {
  120. matrix_row_t result = 0;
  121. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  122. if (debounce_matrix[row * MATRIX_COLS + j]) {
  123. --debounce_matrix[row * MATRIX_COLS + j];
  124. } else {
  125. result |= (1 << j);
  126. }
  127. }
  128. return result;
  129. }
  130. // Report changed keys in the given row. Resets the debounce countdowns
  131. // corresponding to each set bit in 'change' to DEBOUNCE.
  132. void debounce_report(matrix_row_t change, uint8_t row) {
  133. for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
  134. if (change & (1 << i)) {
  135. debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
  136. }
  137. }
  138. }
  139. uint8_t matrix_scan(void)
  140. {
  141. if (mcp23018_status) { // if there was an error
  142. if (++mcp23018_reset_loop == 0) {
  143. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  144. // this will be approx bit more frequent than once per second
  145. print("trying to reset mcp23018\n");
  146. mcp23018_status = init_mcp23018();
  147. if (mcp23018_status) {
  148. print("left side not responding\n");
  149. } else {
  150. print("left side attached\n");
  151. ergodox_blink_all_leds();
  152. }
  153. }
  154. }
  155. #ifdef DEBUG_MATRIX_SCAN_RATE
  156. matrix_scan_count++;
  157. uint32_t timer_now = timer_read32();
  158. if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
  159. print("matrix scan frequency: ");
  160. pdec(matrix_scan_count);
  161. print("\n");
  162. matrix_timer = timer_now;
  163. matrix_scan_count = 0;
  164. }
  165. #endif
  166. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  167. select_row(i);
  168. wait_us(30); // without this wait read unstable value.
  169. matrix_row_t mask = debounce_mask(i);
  170. matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
  171. debounce_report(cols ^ matrix[i], i);
  172. matrix[i] = cols;
  173. unselect_rows();
  174. }
  175. matrix_scan_quantum();
  176. return 1;
  177. }
  178. bool matrix_is_modified(void) // deprecated and evidently not called.
  179. {
  180. return true;
  181. }
  182. inline
  183. bool matrix_is_on(uint8_t row, uint8_t col)
  184. {
  185. return (matrix[row] & ((matrix_row_t)1<<col));
  186. }
  187. inline
  188. matrix_row_t matrix_get_row(uint8_t row)
  189. {
  190. return matrix[row];
  191. }
  192. void matrix_print(void)
  193. {
  194. print("\nr/c 0123456789ABCDEF\n");
  195. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  196. phex(row); print(": ");
  197. pbin_reverse16(matrix_get_row(row));
  198. print("\n");
  199. }
  200. }
  201. uint8_t matrix_key_count(void)
  202. {
  203. uint8_t count = 0;
  204. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  205. count += bitpop16(matrix[i]);
  206. }
  207. return count;
  208. }
  209. /* Column pin configuration
  210. *
  211. * Teensy
  212. * col: 0 1 2 3 4 5
  213. * pin: F0 F1 F4 F5 F6 F7
  214. *
  215. * MCP23018
  216. * col: 0 1 2 3 4 5
  217. * pin: B5 B4 B3 B2 B1 B0
  218. */
  219. static void init_cols(void)
  220. {
  221. // init on mcp23018
  222. // not needed, already done as part of init_mcp23018()
  223. // init on teensy
  224. // Input with pull-up(DDR:0, PORT:1)
  225. DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  226. PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  227. }
  228. static matrix_row_t read_cols(uint8_t row)
  229. {
  230. if (row < 7) {
  231. if (mcp23018_status) { // if there was an error
  232. return 0;
  233. } else {
  234. uint8_t data = 0;
  235. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  236. mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
  237. mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
  238. data = i2c_readNak();
  239. data = ~data;
  240. out:
  241. i2c_stop();
  242. return data;
  243. }
  244. } else {
  245. // read from teensy
  246. return
  247. (PINF&(1<<0) ? 0 : (1<<0)) |
  248. (PINF&(1<<1) ? 0 : (1<<1)) |
  249. (PINF&(1<<4) ? 0 : (1<<2)) |
  250. (PINF&(1<<5) ? 0 : (1<<3)) |
  251. (PINF&(1<<6) ? 0 : (1<<4)) |
  252. (PINF&(1<<7) ? 0 : (1<<5)) ;
  253. }
  254. }
  255. /* Row pin configuration
  256. *
  257. * Teensy
  258. * row: 7 8 9 10 11 12 13
  259. * pin: B0 B1 B2 B3 D2 D3 C6
  260. *
  261. * MCP23018
  262. * row: 0 1 2 3 4 5 6
  263. * pin: A0 A1 A2 A3 A4 A5 A6
  264. */
  265. static void unselect_rows(void)
  266. {
  267. // unselect on mcp23018
  268. if (mcp23018_status) { // if there was an error
  269. // do nothing
  270. } else {
  271. // set all rows hi-Z : 1
  272. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  273. mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
  274. mcp23018_status = i2c_write( 0xFF
  275. & ~(0<<7)
  276. ); if (mcp23018_status) goto out;
  277. out:
  278. i2c_stop();
  279. }
  280. // unselect on teensy
  281. // Hi-Z(DDR:0, PORT:0) to unselect
  282. DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
  283. PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
  284. DDRD &= ~(1<<2 | 1<<3);
  285. PORTD &= ~(1<<2 | 1<<3);
  286. DDRC &= ~(1<<6);
  287. PORTC &= ~(1<<6);
  288. }
  289. static void select_row(uint8_t row)
  290. {
  291. if (row < 7) {
  292. // select on mcp23018
  293. if (mcp23018_status) { // if there was an error
  294. // do nothing
  295. } else {
  296. // set active row low : 0
  297. // set other rows hi-Z : 1
  298. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  299. mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
  300. mcp23018_status = i2c_write( 0xFF & ~(1<<row)
  301. & ~(0<<7)
  302. ); if (mcp23018_status) goto out;
  303. out:
  304. i2c_stop();
  305. }
  306. } else {
  307. // select on teensy
  308. // Output low(DDR:1, PORT:0) to select
  309. switch (row) {
  310. case 7:
  311. DDRB |= (1<<0);
  312. PORTB &= ~(1<<0);
  313. break;
  314. case 8:
  315. DDRB |= (1<<1);
  316. PORTB &= ~(1<<1);
  317. break;
  318. case 9:
  319. DDRB |= (1<<2);
  320. PORTB &= ~(1<<2);
  321. break;
  322. case 10:
  323. DDRB |= (1<<3);
  324. PORTB &= ~(1<<3);
  325. break;
  326. case 11:
  327. DDRD |= (1<<2);
  328. PORTD &= ~(1<<3);
  329. break;
  330. case 12:
  331. DDRD |= (1<<3);
  332. PORTD &= ~(1<<3);
  333. break;
  334. case 13:
  335. DDRC |= (1<<6);
  336. PORTC &= ~(1<<6);
  337. break;
  338. }
  339. }
  340. }