You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

351 lines
9.2 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  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. /*
  33. * This constant define not debouncing time in msecs, but amount of matrix
  34. * scan loops which should be made to get stable debounced results.
  35. *
  36. * On Ergodox matrix scan rate is relatively low, because of slow I2C.
  37. * Now it's only 317 scans/second, or about 3.15 msec/scan.
  38. * According to Cherry specs, debouncing time is 5 msec.
  39. *
  40. * And so, there is no sense to have DEBOUNCE higher than 2.
  41. */
  42. #ifndef DEBOUNCE
  43. # define DEBOUNCE 5
  44. #endif
  45. static uint8_t debouncing = DEBOUNCE;
  46. /* matrix state(1:on, 0:off) */
  47. static matrix_row_t matrix[MATRIX_ROWS];
  48. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  49. static matrix_row_t read_cols(uint8_t row);
  50. static void init_cols(void);
  51. static void unselect_rows(void);
  52. static void select_row(uint8_t row);
  53. static uint8_t mcp23018_reset_loop;
  54. __attribute__ ((weak))
  55. void matrix_init_user(void) {}
  56. __attribute__ ((weak))
  57. void matrix_scan_user(void) {}
  58. __attribute__ ((weak))
  59. void matrix_init_kb(void) {
  60. matrix_init_user();
  61. }
  62. __attribute__ ((weak))
  63. void matrix_scan_kb(void) {
  64. matrix_scan_user();
  65. }
  66. inline
  67. uint8_t matrix_rows(void)
  68. {
  69. return MATRIX_ROWS;
  70. }
  71. inline
  72. uint8_t matrix_cols(void)
  73. {
  74. return MATRIX_COLS;
  75. }
  76. void matrix_init(void)
  77. {
  78. // initialize row and col
  79. debug_enable = true;
  80. debug_matrix = true;
  81. debug_keyboard = true;
  82. debug_mouse = true;
  83. mcp23018_status = init_mcp23018();
  84. unselect_rows();
  85. init_cols();
  86. // initialize matrix state: all keys off
  87. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  88. matrix[i] = 0;
  89. matrix_debouncing[i] = 0;
  90. }
  91. matrix_init_quantum();
  92. }
  93. void matrix_power_up(void) {
  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. }
  103. uint8_t matrix_scan(void)
  104. {
  105. if (mcp23018_status) { // if there was an error
  106. if (++mcp23018_reset_loop == 0) {
  107. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  108. // this will be approx bit more frequent than once per second
  109. print("trying to reset mcp23018\n");
  110. mcp23018_status = init_mcp23018();
  111. if (mcp23018_status) {
  112. print("left side not responding\n");
  113. } else {
  114. print("left side attached\n");
  115. frenchdev_blink_all_leds();
  116. }
  117. }
  118. }
  119. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  120. select_row(i);
  121. wait_us(30); // without this wait read unstable value.
  122. matrix_row_t cols = read_cols(i);
  123. if (matrix_debouncing[i] != cols) {
  124. matrix_debouncing[i] = cols;
  125. if (debouncing) {
  126. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  127. }
  128. debouncing = DEBOUNCE;
  129. }
  130. unselect_rows();
  131. }
  132. if (debouncing) {
  133. if (--debouncing) {
  134. wait_us(1);
  135. // this should be wait_ms(1) but has been left as-is at EZ's request
  136. } else {
  137. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  138. matrix[i] = matrix_debouncing[i];
  139. }
  140. }
  141. }
  142. matrix_scan_quantum();
  143. return 1;
  144. }
  145. bool matrix_is_modified(void)
  146. {
  147. if (debouncing) return false;
  148. return true;
  149. }
  150. inline
  151. bool matrix_is_on(uint8_t row, uint8_t col)
  152. {
  153. return (matrix[row] & ((matrix_row_t)1<<col));
  154. }
  155. inline
  156. matrix_row_t matrix_get_row(uint8_t row)
  157. {
  158. return matrix[row];
  159. }
  160. void matrix_print(void)
  161. {
  162. print("\nr/c 0123456789ABCDEF\n");
  163. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  164. print_hex8(row); print(": ");
  165. print_bin_reverse16(matrix_get_row(row));
  166. print("\n");
  167. }
  168. }
  169. uint8_t matrix_key_count(void)
  170. {
  171. uint8_t count = 0;
  172. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  173. count += bitpop16(matrix[i]);
  174. }
  175. return count;
  176. }
  177. /* Column pin configuration
  178. *
  179. * Teensy
  180. * col: 0 1 2 3 4 5
  181. * pin: F0 F1 F4 F5 F6 F7
  182. *
  183. * MCP23018
  184. * col: 0 1 2 3 4 5
  185. * pin: B5 B4 B3 B2 B1 B0
  186. */
  187. static void init_cols(void)
  188. {
  189. // init on mcp23018
  190. // not needed, already done as part of init_mcp23018()
  191. // init on teensy
  192. // Input with pull-up(DDR:0, PORT:1)
  193. DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  194. PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  195. }
  196. static matrix_row_t read_cols(uint8_t row)
  197. {
  198. if (row < 8) {
  199. if (mcp23018_status) { // if there was an error
  200. return 0;
  201. } else {
  202. uint8_t data = 0;
  203. mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
  204. mcp23018_status = i2c_write(GPIOB, I2C_TIMEOUT); if (mcp23018_status) goto out;
  205. mcp23018_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (mcp23018_status) goto out;
  206. data = i2c_read_nack(I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
  207. data = ~((uint8_t)mcp23018_status);
  208. mcp23018_status = I2C_STATUS_SUCCESS;
  209. out:
  210. i2c_stop();
  211. return data;
  212. }
  213. } else {
  214. // read from teensy
  215. return
  216. (PINF&(1<<0) ? 0 : (1<<0)) |
  217. (PINF&(1<<1) ? 0 : (1<<1)) |
  218. (PINF&(1<<4) ? 0 : (1<<2)) |
  219. (PINF&(1<<5) ? 0 : (1<<3)) |
  220. (PINF&(1<<6) ? 0 : (1<<4)) |
  221. (PINF&(1<<7) ? 0 : (1<<5)) ;
  222. }
  223. }
  224. /* Row pin configuration
  225. *
  226. * Teensy
  227. * row: 7 8 9 10 11 12 13
  228. * pin: B0 B1 B2 B3 D2 D3 C6
  229. *
  230. * MCP23018
  231. * row: 0 1 2 3 4 5 6
  232. * pin: A0 A1 A2 A3 A4 A5 A6
  233. */
  234. static void unselect_rows(void)
  235. {
  236. // unselect on mcp23018
  237. if (mcp23018_status) { // if there was an error
  238. // do nothing
  239. } else {
  240. // set all rows hi-Z : 1
  241. mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
  242. mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT); if (mcp23018_status) goto out;
  243. mcp23018_status = i2c_write( 0xFF & ~(0<<8), I2C_TIMEOUT); if (mcp23018_status) goto out;
  244. out:
  245. i2c_stop();
  246. }
  247. // unselect on teensy
  248. // Hi-Z(DDR:0, PORT:0) to unselect
  249. DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
  250. PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
  251. DDRD &= ~(1<<2 | 1<<3);
  252. PORTD &= ~(1<<2 | 1<<3);
  253. DDRC &= ~(1<<6 | 1<<7);
  254. PORTC &= ~(1<<6 | 1<<7);
  255. }
  256. static void select_row(uint8_t row)
  257. {
  258. if (row < 8) {
  259. // select on mcp23018
  260. if (mcp23018_status) { // if there was an error
  261. // do nothing
  262. } else {
  263. // set active row low : 0
  264. // set other rows hi-Z : 1
  265. mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
  266. mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT); if (mcp23018_status) goto out;
  267. mcp23018_status = i2c_write( 0xFF & ~(1<<row) & ~(0<<8), I2C_TIMEOUT); if (mcp23018_status) goto out;
  268. out:
  269. i2c_stop();
  270. }
  271. } else {
  272. // select on teensy
  273. // Output low(DDR:1, PORT:0) to select
  274. switch (row) {
  275. case 8:
  276. DDRB |= (1<<0);
  277. PORTB &= ~(1<<0);
  278. break;
  279. case 9:
  280. DDRB |= (1<<1);
  281. PORTB &= ~(1<<1);
  282. break;
  283. case 10:
  284. DDRB |= (1<<2);
  285. PORTB &= ~(1<<2);
  286. break;
  287. case 11:
  288. DDRB |= (1<<3);
  289. PORTB &= ~(1<<3);
  290. break;
  291. case 12:
  292. DDRD |= (1<<2);
  293. PORTD &= ~(1<<3);
  294. break;
  295. case 13:
  296. DDRD |= (1<<3);
  297. PORTD &= ~(1<<3);
  298. break;
  299. case 14:
  300. DDRC |= (1<<6);
  301. PORTC &= ~(1<<6);
  302. break;
  303. case 15:
  304. DDRC |= (1<<7);
  305. PORTC &= ~(1<<7);
  306. break;
  307. }
  308. }
  309. }