matrix.c 9.9 KB

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