matrix.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 "ergodox_ez.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. static uint8_t debouncing = DEBOUNCE;
  49. /* matrix state(1:on, 0:off) */
  50. static matrix_row_t matrix[MATRIX_ROWS];
  51. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  52. static matrix_row_t read_cols(uint8_t row);
  53. static void init_cols(void);
  54. static void unselect_rows(void);
  55. static void select_row(uint8_t row);
  56. static uint8_t mcp23018_reset_loop;
  57. #ifdef DEBUG_MATRIX_SCAN_RATE
  58. uint32_t matrix_timer;
  59. uint32_t matrix_scan_count;
  60. #endif
  61. __attribute__ ((weak))
  62. void matrix_init_user(void) {}
  63. __attribute__ ((weak))
  64. void matrix_scan_user(void) {}
  65. __attribute__ ((weak))
  66. void matrix_init_kb(void) {
  67. matrix_init_user();
  68. }
  69. __attribute__ ((weak))
  70. void matrix_scan_kb(void) {
  71. matrix_scan_user();
  72. }
  73. inline
  74. uint8_t matrix_rows(void)
  75. {
  76. return MATRIX_ROWS;
  77. }
  78. inline
  79. uint8_t matrix_cols(void)
  80. {
  81. return MATRIX_COLS;
  82. }
  83. void matrix_init(void)
  84. {
  85. // initialize row and col
  86. mcp23018_status = init_mcp23018();
  87. unselect_rows();
  88. init_cols();
  89. // initialize matrix state: all keys off
  90. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  91. matrix[i] = 0;
  92. matrix_debouncing[i] = 0;
  93. }
  94. #ifdef DEBUG_MATRIX_SCAN_RATE
  95. matrix_timer = timer_read32();
  96. matrix_scan_count = 0;
  97. #endif
  98. matrix_init_kb();
  99. }
  100. void matrix_power_up(void) {
  101. mcp23018_status = init_mcp23018();
  102. unselect_rows();
  103. init_cols();
  104. // initialize matrix state: all keys off
  105. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  106. matrix[i] = 0;
  107. matrix_debouncing[i] = 0;
  108. }
  109. #ifdef DEBUG_MATRIX_SCAN_RATE
  110. matrix_timer = timer_read32();
  111. matrix_scan_count = 0;
  112. #endif
  113. }
  114. uint8_t matrix_scan(void)
  115. {
  116. if (mcp23018_status) { // if there was an error
  117. if (++mcp23018_reset_loop == 0) {
  118. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  119. // this will be approx bit more frequent than once per second
  120. print("trying to reset mcp23018\n");
  121. mcp23018_status = init_mcp23018();
  122. if (mcp23018_status) {
  123. print("left side not responding\n");
  124. } else {
  125. print("left side attached\n");
  126. ergodox_blink_all_leds();
  127. }
  128. }
  129. }
  130. #ifdef DEBUG_MATRIX_SCAN_RATE
  131. matrix_scan_count++;
  132. uint32_t timer_now = timer_read32();
  133. if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
  134. print("matrix scan frequency: ");
  135. pdec(matrix_scan_count);
  136. print("\n");
  137. matrix_timer = timer_now;
  138. matrix_scan_count = 0;
  139. }
  140. #endif
  141. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  142. select_row(i);
  143. wait_us(30); // without this wait read unstable value.
  144. matrix_row_t cols = read_cols(i);
  145. if (matrix_debouncing[i] != cols) {
  146. matrix_debouncing[i] = cols;
  147. if (debouncing) {
  148. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  149. }
  150. debouncing = DEBOUNCE;
  151. }
  152. unselect_rows();
  153. }
  154. if (debouncing) {
  155. if (--debouncing) {
  156. wait_us(1);
  157. // this should be wait_ms(1) but has been left as-is at EZ's request
  158. } else {
  159. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  160. matrix[i] = matrix_debouncing[i];
  161. }
  162. }
  163. }
  164. matrix_scan_quantum();
  165. return 1;
  166. }
  167. bool matrix_is_modified(void)
  168. {
  169. if (debouncing) return false;
  170. return true;
  171. }
  172. inline
  173. bool matrix_is_on(uint8_t row, uint8_t col)
  174. {
  175. return (matrix[row] & ((matrix_row_t)1<<col));
  176. }
  177. inline
  178. matrix_row_t matrix_get_row(uint8_t row)
  179. {
  180. return matrix[row];
  181. }
  182. void matrix_print(void)
  183. {
  184. print("\nr/c 0123456789ABCDEF\n");
  185. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  186. phex(row); print(": ");
  187. pbin_reverse16(matrix_get_row(row));
  188. print("\n");
  189. }
  190. }
  191. uint8_t matrix_key_count(void)
  192. {
  193. uint8_t count = 0;
  194. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  195. count += bitpop16(matrix[i]);
  196. }
  197. return count;
  198. }
  199. /* Column pin configuration
  200. *
  201. * Teensy
  202. * col: 0 1 2 3 4 5
  203. * pin: F0 F1 F4 F5 F6 F7
  204. *
  205. * MCP23018
  206. * col: 0 1 2 3 4 5
  207. * pin: B5 B4 B3 B2 B1 B0
  208. */
  209. static void init_cols(void)
  210. {
  211. // init on mcp23018
  212. // not needed, already done as part of init_mcp23018()
  213. // init on teensy
  214. // Input with pull-up(DDR:0, PORT:1)
  215. DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  216. PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  217. }
  218. static matrix_row_t read_cols(uint8_t row)
  219. {
  220. if (row < 7) {
  221. if (mcp23018_status) { // if there was an error
  222. return 0;
  223. } else {
  224. uint8_t data = 0;
  225. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  226. mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
  227. mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
  228. data = i2c_readNak();
  229. data = ~data;
  230. out:
  231. i2c_stop();
  232. return data;
  233. }
  234. } else {
  235. // read from teensy
  236. return
  237. (PINF&(1<<0) ? 0 : (1<<0)) |
  238. (PINF&(1<<1) ? 0 : (1<<1)) |
  239. (PINF&(1<<4) ? 0 : (1<<2)) |
  240. (PINF&(1<<5) ? 0 : (1<<3)) |
  241. (PINF&(1<<6) ? 0 : (1<<4)) |
  242. (PINF&(1<<7) ? 0 : (1<<5)) ;
  243. }
  244. }
  245. /* Row pin configuration
  246. *
  247. * Teensy
  248. * row: 7 8 9 10 11 12 13
  249. * pin: B0 B1 B2 B3 D2 D3 C6
  250. *
  251. * MCP23018
  252. * row: 0 1 2 3 4 5 6
  253. * pin: A0 A1 A2 A3 A4 A5 A6
  254. */
  255. static void unselect_rows(void)
  256. {
  257. // unselect on mcp23018
  258. if (mcp23018_status) { // if there was an error
  259. // do nothing
  260. } else {
  261. // set all rows hi-Z : 1
  262. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  263. mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
  264. mcp23018_status = i2c_write( 0xFF
  265. & ~(0<<7)
  266. ); if (mcp23018_status) goto out;
  267. out:
  268. i2c_stop();
  269. }
  270. // unselect on teensy
  271. // Hi-Z(DDR:0, PORT:0) to unselect
  272. DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
  273. PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
  274. DDRD &= ~(1<<2 | 1<<3);
  275. PORTD &= ~(1<<2 | 1<<3);
  276. DDRC &= ~(1<<6);
  277. PORTC &= ~(1<<6);
  278. }
  279. static void select_row(uint8_t row)
  280. {
  281. if (row < 7) {
  282. // select on mcp23018
  283. if (mcp23018_status) { // if there was an error
  284. // do nothing
  285. } else {
  286. // set active row low : 0
  287. // set other rows hi-Z : 1
  288. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  289. mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
  290. mcp23018_status = i2c_write( 0xFF & ~(1<<row)
  291. & ~(0<<7)
  292. ); if (mcp23018_status) goto out;
  293. out:
  294. i2c_stop();
  295. }
  296. } else {
  297. // select on teensy
  298. // Output low(DDR:1, PORT:0) to select
  299. switch (row) {
  300. case 7:
  301. DDRB |= (1<<0);
  302. PORTB &= ~(1<<0);
  303. break;
  304. case 8:
  305. DDRB |= (1<<1);
  306. PORTB &= ~(1<<1);
  307. break;
  308. case 9:
  309. DDRB |= (1<<2);
  310. PORTB &= ~(1<<2);
  311. break;
  312. case 10:
  313. DDRB |= (1<<3);
  314. PORTB &= ~(1<<3);
  315. break;
  316. case 11:
  317. DDRD |= (1<<2);
  318. PORTD &= ~(1<<3);
  319. break;
  320. case 12:
  321. DDRD |= (1<<3);
  322. PORTD &= ~(1<<3);
  323. break;
  324. case 13:
  325. DDRC |= (1<<6);
  326. PORTC &= ~(1<<6);
  327. break;
  328. }
  329. }
  330. }