matrix.c 8.6 KB

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