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