matrix.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <avr/io.h>
  20. #include <util/delay.h>
  21. #include "action_layer.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #include "ergodox.h"
  27. #include "i2cmaster.h"
  28. #ifdef DEBUG_MATRIX_SCAN_RATE
  29. #include "timer.h"
  30. #endif
  31. #ifndef DEBOUNCE
  32. # define DEBOUNCE 5
  33. #endif
  34. static uint8_t debouncing = DEBOUNCE;
  35. /* matrix state(1:on, 0:off) */
  36. static matrix_row_t matrix[MATRIX_ROWS];
  37. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  38. static matrix_row_t read_cols(uint8_t row);
  39. static void init_cols(void);
  40. static void unselect_rows();
  41. static void select_row(uint8_t row);
  42. static uint8_t mcp23018_reset_loop;
  43. #ifdef DEBUG_MATRIX_SCAN_RATE
  44. uint32_t matrix_timer;
  45. uint32_t matrix_scan_count;
  46. #endif
  47. __attribute__ ((weak))
  48. void * matrix_init_kb(void) {
  49. };
  50. __attribute__ ((weak))
  51. void * matrix_scan_kb(void) {
  52. };
  53. inline
  54. uint8_t matrix_rows(void)
  55. {
  56. return MATRIX_ROWS;
  57. }
  58. inline
  59. uint8_t matrix_cols(void)
  60. {
  61. return MATRIX_COLS;
  62. }
  63. void matrix_init(void)
  64. {
  65. // initialize row and col
  66. mcp23018_status = init_mcp23018();
  67. unselect_rows();
  68. init_cols();
  69. // initialize matrix state: all keys off
  70. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  71. matrix[i] = 0;
  72. matrix_debouncing[i] = 0;
  73. }
  74. #ifdef DEBUG_MATRIX_SCAN_RATE
  75. matrix_timer = timer_read32();
  76. matrix_scan_count = 0;
  77. #endif
  78. if (matrix_init_kb) {
  79. (*matrix_init_kb)();
  80. }
  81. }
  82. uint8_t matrix_scan(void)
  83. {
  84. if (mcp23018_status) { // if there was an error
  85. if (++mcp23018_reset_loop == 0) {
  86. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  87. // this will be approx bit more frequent than once per second
  88. print("trying to reset mcp23018\n");
  89. mcp23018_status = init_mcp23018();
  90. if (mcp23018_status) {
  91. print("left side not responding\n");
  92. } else {
  93. print("left side attached\n");
  94. ergodox_blink_all_leds();
  95. }
  96. }
  97. }
  98. #ifdef DEBUG_MATRIX_SCAN_RATE
  99. matrix_scan_count++;
  100. uint32_t timer_now = timer_read32();
  101. if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
  102. print("matrix scan frequency: ");
  103. pdec(matrix_scan_count);
  104. print("\n");
  105. matrix_timer = timer_now;
  106. matrix_scan_count = 0;
  107. }
  108. #endif
  109. #ifdef KEYMAP_CUB
  110. uint8_t layer = biton32(layer_state);
  111. ergodox_board_led_off();
  112. ergodox_left_led_1_off();
  113. ergodox_left_led_2_off();
  114. ergodox_left_led_3_off();
  115. switch (layer) {
  116. case 1:
  117. // all
  118. ergodox_left_led_1_on();
  119. ergodox_left_led_2_on();
  120. ergodox_left_led_3_on();
  121. break;
  122. case 2:
  123. // blue
  124. ergodox_left_led_2_on();
  125. break;
  126. case 8:
  127. // blue and green
  128. ergodox_left_led_2_on();
  129. // break missed intentionally
  130. case 3:
  131. // green
  132. ergodox_left_led_3_on();
  133. break;
  134. case 6:
  135. ergodox_board_led_on();
  136. // break missed intentionally
  137. case 4:
  138. case 5:
  139. case 7:
  140. // white
  141. ergodox_left_led_1_on();
  142. break;
  143. case 9:
  144. // white+green
  145. ergodox_left_led_1_on();
  146. ergodox_left_led_3_on();
  147. break;
  148. default:
  149. // none
  150. break;
  151. }
  152. mcp23018_status = ergodox_left_leds_update();
  153. #endif
  154. #ifdef KEYMAP_SIMON
  155. uint8_t layer = biton32(layer_state);
  156. ergodox_board_led_off();
  157. switch (layer) {
  158. case 0:
  159. // none
  160. break;
  161. default:
  162. ergodox_board_led_on();
  163. break;
  164. }
  165. #endif
  166. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  167. select_row(i);
  168. matrix_row_t cols = read_cols(i);
  169. if (matrix_debouncing[i] != cols) {
  170. matrix_debouncing[i] = cols;
  171. if (debouncing) {
  172. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  173. }
  174. debouncing = DEBOUNCE;
  175. }
  176. unselect_rows();
  177. }
  178. if (debouncing) {
  179. if (--debouncing) {
  180. _delay_ms(1);
  181. } else {
  182. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  183. matrix[i] = matrix_debouncing[i];
  184. }
  185. }
  186. }
  187. if (matrix_scan_kb) {
  188. (*matrix_scan_kb)();
  189. }
  190. return 1;
  191. }
  192. bool matrix_is_modified(void)
  193. {
  194. if (debouncing) return false;
  195. return true;
  196. }
  197. inline
  198. bool matrix_is_on(uint8_t row, uint8_t col)
  199. {
  200. return (matrix[row] & ((matrix_row_t)1<<col));
  201. }
  202. inline
  203. matrix_row_t matrix_get_row(uint8_t row)
  204. {
  205. return matrix[row];
  206. }
  207. void matrix_print(void)
  208. {
  209. print("\nr/c 0123456789ABCDEF\n");
  210. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  211. phex(row); print(": ");
  212. pbin_reverse16(matrix_get_row(row));
  213. print("\n");
  214. }
  215. }
  216. uint8_t matrix_key_count(void)
  217. {
  218. uint8_t count = 0;
  219. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  220. count += bitpop16(matrix[i]);
  221. }
  222. return count;
  223. }
  224. /* Column pin configuration
  225. *
  226. * Teensy
  227. * col: 0 1 2 3 4 5
  228. * pin: F0 F1 F4 F5 F6 F7
  229. *
  230. * MCP23018
  231. * col: 0 1 2 3 4 5
  232. * pin: B5 B4 B3 B2 B1 B0
  233. */
  234. static void init_cols(void)
  235. {
  236. // init on mcp23018
  237. // not needed, already done as part of init_mcp23018()
  238. // init on teensy
  239. // Input with pull-up(DDR:0, PORT:1)
  240. DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  241. PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  242. }
  243. static matrix_row_t read_cols(uint8_t row)
  244. {
  245. if (row < 7) {
  246. if (mcp23018_status) { // if there was an error
  247. return 0;
  248. } else {
  249. uint8_t data = 0;
  250. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  251. mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
  252. mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
  253. data = i2c_readNak();
  254. data = ~data;
  255. out:
  256. i2c_stop();
  257. return data;
  258. }
  259. } else {
  260. _delay_us(30); // without this wait read unstable value.
  261. // read from teensy
  262. return
  263. (PINF&(1<<0) ? 0 : (1<<0)) |
  264. (PINF&(1<<1) ? 0 : (1<<1)) |
  265. (PINF&(1<<4) ? 0 : (1<<2)) |
  266. (PINF&(1<<5) ? 0 : (1<<3)) |
  267. (PINF&(1<<6) ? 0 : (1<<4)) |
  268. (PINF&(1<<7) ? 0 : (1<<5)) ;
  269. }
  270. }
  271. /* Row pin configuration
  272. *
  273. * Teensy
  274. * row: 7 8 9 10 11 12 13
  275. * pin: B0 B1 B2 B3 D2 D3 C6
  276. *
  277. * MCP23018
  278. * row: 0 1 2 3 4 5 6
  279. * pin: A0 A1 A2 A3 A4 A5 A6
  280. */
  281. static void unselect_rows(void)
  282. {
  283. // unselect on mcp23018
  284. if (mcp23018_status) { // if there was an error
  285. // do nothing
  286. } else {
  287. // set all 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
  291. & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
  292. ); if (mcp23018_status) goto out;
  293. out:
  294. i2c_stop();
  295. }
  296. // unselect on teensy
  297. // Hi-Z(DDR:0, PORT:0) to unselect
  298. DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
  299. PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
  300. DDRD &= ~(1<<2 | 1<<3);
  301. PORTD &= ~(1<<2 | 1<<3);
  302. DDRC &= ~(1<<6);
  303. PORTC &= ~(1<<6);
  304. }
  305. static void select_row(uint8_t row)
  306. {
  307. if (row < 7) {
  308. // select on mcp23018
  309. if (mcp23018_status) { // if there was an error
  310. // do nothing
  311. } else {
  312. // set active row low : 0
  313. // set other rows hi-Z : 1
  314. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  315. mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
  316. mcp23018_status = i2c_write( 0xFF & ~(1<<row)
  317. & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
  318. ); if (mcp23018_status) goto out;
  319. out:
  320. i2c_stop();
  321. }
  322. } else {
  323. // select on teensy
  324. // Output low(DDR:1, PORT:0) to select
  325. switch (row) {
  326. case 7:
  327. DDRB |= (1<<0);
  328. PORTB &= ~(1<<0);
  329. break;
  330. case 8:
  331. DDRB |= (1<<1);
  332. PORTB &= ~(1<<1);
  333. break;
  334. case 9:
  335. DDRB |= (1<<2);
  336. PORTB &= ~(1<<2);
  337. break;
  338. case 10:
  339. DDRB |= (1<<3);
  340. PORTB &= ~(1<<3);
  341. break;
  342. case 11:
  343. DDRD |= (1<<2);
  344. PORTD &= ~(1<<3);
  345. break;
  346. case 12:
  347. DDRD |= (1<<3);
  348. PORTD &= ~(1<<3);
  349. break;
  350. case 13:
  351. DDRC |= (1<<6);
  352. PORTC &= ~(1<<6);
  353. break;
  354. }
  355. }
  356. }