matrix.c 9.9 KB

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