matrix.c 10 KB

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