matrix.c 8.4 KB

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