matrix.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  3. Copyright 2017 Erin Call <hello@erincall.com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <avr/io.h>
  18. #include "wait.h"
  19. #include "action_layer.h"
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include "matrix.h"
  24. #include "dactyl.h"
  25. #include "i2c_master.h"
  26. #include "timer.h"
  27. /* Set 0 if debouncing isn't needed */
  28. #ifndef DEBOUNCE
  29. # define DEBOUNCE 5
  30. #endif
  31. #if (DEBOUNCE > 0)
  32. static uint16_t debouncing_time;
  33. static bool debouncing = false;
  34. #endif
  35. #ifdef MATRIX_MASKED
  36. extern const matrix_row_t matrix_mask[];
  37. #endif
  38. #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  39. static const uint8_t onboard_row_pins[MATRIX_ROWS] = MATRIX_ONBOARD_ROW_PINS;
  40. static const uint8_t onboard_col_pins[MATRIX_COLS] = MATRIX_ONBOARD_COL_PINS;
  41. static const bool col_expanded[MATRIX_COLS] = COL_EXPANDED;
  42. #endif
  43. /* matrix state(1:on, 0:off) */
  44. static matrix_row_t matrix[MATRIX_ROWS];
  45. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  46. #if (DIODE_DIRECTION == COL2ROW)
  47. static const uint8_t expander_col_pins[MATRIX_COLS] = MATRIX_EXPANDER_COL_PINS;
  48. static void init_cols(void);
  49. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  50. static void unselect_rows(void);
  51. static void select_row(uint8_t row);
  52. static void unselect_row(uint8_t row);
  53. #elif (DIODE_DIRECTION == ROW2COL)
  54. static const uint8_t expander_row_pins[MATRIX_ROWS] = MATRIX_EXPANDER_ROW_PINS;
  55. static void init_rows(void);
  56. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  57. static void unselect_cols(void);
  58. static void select_col(uint8_t col);
  59. static void unselect_col(uint8_t col);
  60. #endif
  61. static uint8_t expander_reset_loop;
  62. uint8_t expander_status;
  63. uint8_t expander_input_pin_mask;
  64. bool i2c_initialized = false;
  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. init_expander();
  90. #if (DIODE_DIRECTION == COL2ROW)
  91. unselect_rows();
  92. init_cols();
  93. #elif (DIODE_DIRECTION == ROW2COL)
  94. unselect_cols();
  95. init_rows();
  96. #endif
  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. matrix_init_kb();
  103. }
  104. void init_expander(void) {
  105. if (! i2c_initialized) {
  106. i2c_init();
  107. wait_ms(1000);
  108. }
  109. if (! expander_input_pin_mask) {
  110. #if (DIODE_DIRECTION == COL2ROW)
  111. for (int col = 0; col < MATRIX_COLS; col++) {
  112. if (col_expanded[col]) {
  113. expander_input_pin_mask |= (1 << expander_col_pins[col]);
  114. }
  115. }
  116. #elif (DIODE_DIRECTION == ROW2COL)
  117. for (int row = 0; row < MATRIX_ROWS; row++) {
  118. expander_input_pin_mask |= (1 << expander_row_pins[row]);
  119. }
  120. #endif
  121. }
  122. /*
  123. Pin direction and pull-up depends on both the diode direction
  124. and on whether the column register is GPIOA or GPIOB
  125. +-------+---------------+---------------+
  126. | | ROW2COL | COL2ROW |
  127. +-------+---------------+---------------+
  128. | GPIOA | input, output | output, input |
  129. +-------+---------------+---------------+
  130. | GPIOB | output, input | input, output |
  131. +-------+---------------+---------------+
  132. */
  133. #if (EXPANDER_COL_REGISTER == GPIOA)
  134. # if (DIODE_DIRECTION == COL2ROW)
  135. uint8_t direction[2] = {
  136. expander_input_pin_mask,
  137. 0,
  138. };
  139. # elif (DIODE_DIRECTION == ROW2COL)
  140. uint8_t direction[2] = {
  141. 0,
  142. expander_input_pin_mask,
  143. };
  144. # endif
  145. #elif (EXPANDER_COL_REGISTER == GPIOB)
  146. # if (DIODE_DIRECTION == COL2ROW)
  147. uint8_t direction[2] = {
  148. 0,
  149. expander_input_pin_mask,
  150. };
  151. # elif (DIODE_DIRECTION == ROW2COL)
  152. uint8_t direction[2] = {
  153. expander_input_pin_mask,
  154. 0,
  155. };
  156. # endif
  157. #endif
  158. // set pull-up
  159. // - unused : off : 0
  160. // - input : on : 1
  161. // - driving : off : 0
  162. #if (EXPANDER_COL_REGISTER == GPIOA)
  163. # if (DIODE_DIRECTION == COL2ROW)
  164. uint8_t pullup[2] = {
  165. expander_input_pin_mask,
  166. 0,
  167. };
  168. # elif (DIODE_DIRECTION == ROW2COL)
  169. uint8_t pullup[2] = {
  170. 0,
  171. expander_input_pin_mask,
  172. };
  173. # endif
  174. #elif (EXPANDER_COL_REGISTER == GPIOB)
  175. # if (DIODE_DIRECTION == COL2ROW)
  176. uint8_t pullup[2] = {
  177. 0,
  178. expander_input_pin_mask,
  179. };
  180. # elif (DIODE_DIRECTION == ROW2COL)
  181. uint8_t pullup[2] = {
  182. expander_input_pin_mask,
  183. 0,
  184. };
  185. # endif
  186. #endif
  187. expander_status = i2c_write_register(I2C_ADDR, IODIRA, direction, 2, I2C_TIMEOUT);
  188. if (expander_status) return;
  189. expander_status = i2c_write_register(I2C_ADDR, GPPUA, pullup, 2, I2C_TIMEOUT);
  190. }
  191. uint8_t matrix_scan(void)
  192. {
  193. if (expander_status) { // if there was an error
  194. if (++expander_reset_loop == 0) {
  195. // since expander_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  196. // this will be approx bit more frequent than once per second
  197. print("trying to reset expander\n");
  198. init_expander();
  199. if (expander_status) {
  200. print("left side not responding\n");
  201. } else {
  202. print("left side attached\n");
  203. }
  204. }
  205. }
  206. #if (DIODE_DIRECTION == COL2ROW)
  207. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  208. # if (DEBOUNCE > 0)
  209. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  210. if (matrix_changed) {
  211. debouncing = true;
  212. debouncing_time = timer_read();
  213. }
  214. # else
  215. read_cols_on_row(matrix, current_row);
  216. # endif
  217. }
  218. #elif (DIODE_DIRECTION == ROW2COL)
  219. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  220. # if (DEBOUNCE > 0)
  221. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  222. if (matrix_changed) {
  223. debouncing = true;
  224. debouncing_time = timer_read();
  225. }
  226. # else
  227. read_rows_on_col(matrix, current_col);
  228. # endif
  229. }
  230. #endif
  231. # if (DEBOUNCE > 0)
  232. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  233. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  234. matrix[i] = matrix_debouncing[i];
  235. }
  236. debouncing = false;
  237. }
  238. # endif
  239. matrix_scan_kb();
  240. return 1;
  241. }
  242. inline
  243. bool matrix_is_on(uint8_t row, uint8_t col)
  244. {
  245. return (matrix[row] & (MATRIX_ROW_SHIFTER << col));
  246. }
  247. inline
  248. matrix_row_t matrix_get_row(uint8_t row)
  249. {
  250. #ifdef MATRIX_MASKED
  251. return matrix[row] & matrix_mask[row];
  252. #else
  253. return matrix[row];
  254. #endif
  255. }
  256. void matrix_print(void)
  257. {
  258. print("\nr/c 0123456789ABCDEF\n");
  259. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  260. print_hex8(row); print(": ");
  261. print_bin_reverse16(matrix_get_row(row));
  262. print("\n");
  263. }
  264. }
  265. #if (DIODE_DIRECTION == COL2ROW)
  266. static void init_cols(void) {
  267. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  268. if (! col_expanded[x]) {
  269. uint8_t pin = onboard_col_pins[x];
  270. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  271. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  272. }
  273. }
  274. }
  275. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  276. // Store last value of row prior to reading
  277. matrix_row_t last_row_value = current_matrix[current_row];
  278. // Clear data in matrix row
  279. current_matrix[current_row] = 0;
  280. // Select row and wait for row selection to stabilize
  281. select_row(current_row);
  282. wait_us(30);
  283. // Read columns from expander, unless it's in an error state
  284. if (! expander_status) {
  285. uint8_t state = 0;
  286. expander_status = i2c_read_register(I2C_ADDR, EXPANDER_COL_REGISTER, &state, 1, I2C_TIMEOUT);
  287. if (! expander_status) {
  288. current_matrix[current_row] |= (~state) & expander_input_pin_mask;
  289. }
  290. }
  291. // Read columns from onboard pins
  292. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  293. if (! col_expanded[col_index]) {
  294. uint8_t pin = onboard_col_pins[col_index];
  295. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  296. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  297. }
  298. }
  299. unselect_row(current_row);
  300. return (last_row_value != current_matrix[current_row]);
  301. }
  302. static void select_row(uint8_t row) {
  303. // select on expander, unless it's in an error state
  304. if (! expander_status) {
  305. // set active row low : 0
  306. // set other rows hi-Z : 1
  307. uint8_t port = 0xFF & ~(1<<row);
  308. expander_status = i2c_write_register(I2C_ADDR, EXPANDER_ROW_REGISTER, &port, 1, I2C_TIMEOUT);
  309. }
  310. // select on teensy
  311. uint8_t pin = onboard_row_pins[row];
  312. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  313. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  314. }
  315. static void unselect_row(uint8_t row)
  316. {
  317. // No need to explicitly unselect expander pins--their I/O state is
  318. // set simultaneously, with a single bitmask sent to i2c_write. When
  319. // select_row selects a single pin, it implicitly unselects all the
  320. // other ones.
  321. // unselect on teensy
  322. uint8_t pin = onboard_row_pins[row];
  323. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // OUT
  324. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // LOW
  325. }
  326. static void unselect_rows(void) {
  327. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  328. unselect_row(x);
  329. }
  330. }
  331. #elif (DIODE_DIRECTION == ROW2COL)
  332. static void init_rows(void)
  333. {
  334. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  335. uint8_t pin = onboard_row_pins[x];
  336. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  337. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  338. }
  339. }
  340. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  341. {
  342. bool matrix_changed = false;
  343. uint8_t column_state = 0;
  344. //select col and wait for selection to stabilize
  345. select_col(current_col);
  346. wait_us(30);
  347. if (current_col < 6) {
  348. // read rows from expander
  349. if (expander_status) {
  350. // it's already in an error state; nothing we can do
  351. return false;
  352. }
  353. expander_status = i2c_read_register(I2C_ADDR, EXPANDER_ROW_REGISTER, &column_state, 1, I2C_TIMEOUT);
  354. column_state = ~column_state;
  355. } else {
  356. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  357. if ((_SFR_IO8(onboard_row_pins[current_row] >> 4) & _BV(onboard_row_pins[current_row] & 0xF)) == 0) {
  358. column_state |= (1 << current_row);
  359. }
  360. }
  361. }
  362. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  363. // Store last value of row prior to reading
  364. matrix_row_t last_row_value = current_matrix[current_row];
  365. if (column_state & (1 << current_row)) {
  366. // key closed; set state bit in matrix
  367. current_matrix[current_row] |= (MATRIX_ROW_SHIFTER << current_col);
  368. } else {
  369. // key open; clear state bit in matrix
  370. current_matrix[current_row] &= ~(MATRIX_ROW_SHIFTER << current_col);
  371. }
  372. // Determine whether the matrix changed state
  373. if ((last_row_value != current_matrix[current_row]) && !(matrix_changed))
  374. {
  375. matrix_changed = true;
  376. }
  377. }
  378. unselect_col(current_col);
  379. return matrix_changed;
  380. }
  381. static void select_col(uint8_t col)
  382. {
  383. if (col_expanded[col]) {
  384. // select on expander
  385. if (expander_status) { // if there was an error
  386. // do nothing
  387. } else {
  388. // set active col low : 0
  389. // set other cols hi-Z : 1
  390. uint8_t port = 0xFF & ~(1<<col);
  391. expander_status = i2c_write_register(I2C_ADDR, EXPANDER_COL_REGISTER, &port, 1, I2C_TIMEOUT);
  392. }
  393. } else {
  394. // select on teensy
  395. uint8_t pin = onboard_col_pins[col];
  396. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  397. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  398. }
  399. }
  400. static void unselect_col(uint8_t col)
  401. {
  402. if (col_expanded[col]) {
  403. // No need to explicitly unselect expander pins--their I/O state is
  404. // set simultaneously, with a single bitmask sent to i2c_write. When
  405. // select_col selects a single pin, it implicitly unselects all the
  406. // other ones.
  407. } else {
  408. // unselect on teensy
  409. uint8_t pin = onboard_col_pins[col];
  410. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  411. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  412. }
  413. }
  414. static void unselect_cols(void)
  415. {
  416. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  417. unselect_col(x);
  418. }
  419. }
  420. #endif