matrix.c 8.9 KB

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