matrix.c 8.8 KB

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