matrix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 "i2c_master.h"
  25. #include "timer.h"
  26. #define I2C_TIMEOUT 100
  27. #define I2C_ADDR (0b0100000<<1)
  28. #define IODIRA 0x00 // i/o direction register
  29. #define IODIRB 0x01
  30. #define GPPUA 0x0C // GPIO pull-up resistor register
  31. #define GPPUB 0x0D
  32. #define GPIOA 0x12 // general purpose i/o port register (write modifies OLAT)
  33. #define GPIOB 0x13
  34. void init_expander(void);
  35. /* Set 0 if debouncing isn't needed */
  36. #ifndef DEBOUNCE
  37. # define DEBOUNCE 5
  38. #endif
  39. #if (DEBOUNCE > 0)
  40. static uint16_t debouncing_time;
  41. static bool debouncing = false;
  42. #endif
  43. #ifdef MATRIX_MASKED
  44. extern const matrix_row_t matrix_mask[];
  45. #endif
  46. #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  47. static const uint8_t onboard_row_pins[MATRIX_ROWS] = MATRIX_ONBOARD_ROW_PINS;
  48. static const uint8_t onboard_col_pins[MATRIX_COLS] = MATRIX_ONBOARD_COL_PINS;
  49. static const bool col_expanded[MATRIX_COLS] = COL_EXPANDED;
  50. #endif
  51. /* matrix state(1:on, 0:off) */
  52. static matrix_row_t matrix[MATRIX_ROWS];
  53. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  54. #if (DIODE_DIRECTION == COL2ROW)
  55. static const uint8_t expander_col_pins[MATRIX_COLS] = MATRIX_EXPANDER_COL_PINS;
  56. static void init_cols(void);
  57. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  58. static void unselect_rows(void);
  59. static void select_row(uint8_t row);
  60. static void unselect_row(uint8_t row);
  61. #elif (DIODE_DIRECTION == ROW2COL)
  62. static const uint8_t expander_row_pins[MATRIX_ROWS] = MATRIX_EXPANDER_ROW_PINS;
  63. static void init_rows(void);
  64. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  65. static void unselect_cols(void);
  66. static void select_col(uint8_t col);
  67. static void unselect_col(uint8_t col);
  68. #endif
  69. static uint8_t expander_reset_loop;
  70. uint8_t expander_status;
  71. uint8_t expander_input_pin_mask;
  72. bool i2c_initialized = false;
  73. __attribute__ ((weak))
  74. void matrix_init_user(void) {}
  75. __attribute__ ((weak))
  76. void matrix_scan_user(void) {}
  77. __attribute__ ((weak))
  78. void matrix_init_kb(void) {
  79. matrix_init_user();
  80. }
  81. __attribute__ ((weak))
  82. void matrix_scan_kb(void) {
  83. matrix_scan_user();
  84. }
  85. inline
  86. uint8_t matrix_rows(void)
  87. {
  88. return MATRIX_ROWS;
  89. }
  90. inline
  91. uint8_t matrix_cols(void)
  92. {
  93. return MATRIX_COLS;
  94. }
  95. void matrix_init(void)
  96. {
  97. init_expander();
  98. #if (DIODE_DIRECTION == COL2ROW)
  99. unselect_rows();
  100. init_cols();
  101. #elif (DIODE_DIRECTION == ROW2COL)
  102. unselect_cols();
  103. init_rows();
  104. #endif
  105. // initialize matrix state: all keys off
  106. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  107. matrix[i] = 0;
  108. matrix_debouncing[i] = 0;
  109. }
  110. matrix_init_kb();
  111. }
  112. void init_expander(void) {
  113. if (! i2c_initialized) {
  114. i2c_init();
  115. wait_ms(1000);
  116. }
  117. if (! expander_input_pin_mask) {
  118. #if (DIODE_DIRECTION == COL2ROW)
  119. for (int col = 0; col < MATRIX_COLS; col++) {
  120. if (col_expanded[col]) {
  121. expander_input_pin_mask |= (1 << expander_col_pins[col]);
  122. }
  123. }
  124. #elif (DIODE_DIRECTION == ROW2COL)
  125. for (int row = 0; row < MATRIX_ROWS; row++) {
  126. expander_input_pin_mask |= (1 << expander_row_pins[row]);
  127. }
  128. #endif
  129. }
  130. /*
  131. Pin direction and pull-up depends on both the diode direction
  132. and on whether the column register is GPIOA or GPIOB
  133. +-------+---------------+---------------+
  134. | | ROW2COL | COL2ROW |
  135. +-------+---------------+---------------+
  136. | GPIOA | input, output | output, input |
  137. +-------+---------------+---------------+
  138. | GPIOB | output, input | input, output |
  139. +-------+---------------+---------------+
  140. */
  141. #if (EXPANDER_COL_REGISTER == GPIOA)
  142. # if (DIODE_DIRECTION == COL2ROW)
  143. uint8_t data[] = { expander_input_pin_mask, 0};
  144. # elif (DIODE_DIRECTION == ROW2COL)
  145. uint8_t data[] = { 0, expander_input_pin_mask};
  146. # endif
  147. #elif (EXPANDER_COL_REGISTER == GPIOB)
  148. # if (DIODE_DIRECTION == COL2ROW)
  149. uint8_t data[] = { 0, expander_input_pin_mask};
  150. # elif (DIODE_DIRECTION == ROW2COL)
  151. uint8_t data[] = { expander_input_pin_mask, 0};
  152. # endif
  153. #endif
  154. expander_status = i2c_write_register(I2C_ADDR, IODIRA, data, sizeof(data), I2C_TIMEOUT);
  155. if (!expander_status) {
  156. // set pull-up
  157. // - unused : off : 0
  158. // - input : on : 1
  159. // - driving : off : 0
  160. expander_status = i2c_write_register(I2C_ADDR, GPPUA, data, sizeof(data), I2C_TIMEOUT);
  161. }
  162. }
  163. uint8_t matrix_scan(void)
  164. {
  165. if (expander_status) { // if there was an error
  166. if (++expander_reset_loop == 0) {
  167. // since expander_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  168. // this will be approx bit more frequent than once per second
  169. print("trying to reset expander\n");
  170. init_expander();
  171. if (expander_status) {
  172. print("left side not responding\n");
  173. } else {
  174. print("left side attached\n");
  175. }
  176. }
  177. }
  178. #if (DIODE_DIRECTION == COL2ROW)
  179. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  180. # if (DEBOUNCE > 0)
  181. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  182. if (matrix_changed) {
  183. debouncing = true;
  184. debouncing_time = timer_read();
  185. }
  186. # else
  187. read_cols_on_row(matrix, current_row);
  188. # endif
  189. }
  190. #elif (DIODE_DIRECTION == ROW2COL)
  191. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  192. # if (DEBOUNCE > 0)
  193. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  194. if (matrix_changed) {
  195. debouncing = true;
  196. debouncing_time = timer_read();
  197. }
  198. # else
  199. read_rows_on_col(matrix, current_col);
  200. # endif
  201. }
  202. #endif
  203. # if (DEBOUNCE > 0)
  204. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  205. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  206. matrix[i] = matrix_debouncing[i];
  207. }
  208. debouncing = false;
  209. }
  210. # endif
  211. matrix_scan_kb();
  212. return 1;
  213. }
  214. inline
  215. bool matrix_is_on(uint8_t row, uint8_t col)
  216. {
  217. return (matrix[row] & (MATRIX_ROW_SHIFTER << col));
  218. }
  219. inline
  220. matrix_row_t matrix_get_row(uint8_t row)
  221. {
  222. #ifdef MATRIX_MASKED
  223. return matrix[row] & matrix_mask[row];
  224. #else
  225. return matrix[row];
  226. #endif
  227. }
  228. void matrix_print(void)
  229. {
  230. print("\nr/c 0123456789ABCDEF\n");
  231. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  232. print_hex8(row); print(": ");
  233. print_bin_reverse16(matrix_get_row(row));
  234. print("\n");
  235. }
  236. }
  237. #if (DIODE_DIRECTION == COL2ROW)
  238. static void init_cols(void) {
  239. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  240. if (! col_expanded[x]) {
  241. uint8_t pin = onboard_col_pins[x];
  242. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  243. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  244. }
  245. }
  246. }
  247. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  248. // Store last value of row prior to reading
  249. matrix_row_t last_row_value = current_matrix[current_row];
  250. // Clear data in matrix row
  251. current_matrix[current_row] = 0;
  252. // Select row and wait for row selection to stabilize
  253. select_row(current_row);
  254. wait_us(30);
  255. // Read columns from expander, unless it's in an error state
  256. if (! expander_status) {
  257. uint8_t data;
  258. i2c_read_register(I2C_ADDR, EXPANDER_COL_REGISTER, &data, 1, I2C_TIMEOUT);
  259. current_matrix[current_row] |= (~data) & expander_input_pin_mask;
  260. }
  261. // Read columns from onboard pins
  262. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  263. if (! col_expanded[col_index]) {
  264. uint8_t pin = onboard_col_pins[col_index];
  265. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  266. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  267. }
  268. }
  269. unselect_row(current_row);
  270. return (last_row_value != current_matrix[current_row]);
  271. }
  272. static void select_row(uint8_t row) {
  273. // select on expander, unless it's in an error state
  274. if (! expander_status) {
  275. // set active row low : 0
  276. // set other rows hi-Z : 1
  277. uint8_t data = 0xFF & ~(1<<row);
  278. i2c_write_register(I2C_ADDR, EXPANDER_ROW_REGISTER, &data, 1, I2C_TIMEOUT);
  279. }
  280. // select on teensy
  281. uint8_t pin = onboard_row_pins[row];
  282. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  283. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  284. }
  285. static void unselect_row(uint8_t row)
  286. {
  287. // No need to explicitly unselect expander pins--their I/O state is
  288. // set simultaneously, with a single bitmask sent to i2c_write. When
  289. // select_row selects a single pin, it implicitly unselects all the
  290. // other ones.
  291. // unselect on teensy
  292. uint8_t pin = onboard_row_pins[row];
  293. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // OUT
  294. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // LOW
  295. }
  296. static void unselect_rows(void) {
  297. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  298. unselect_row(x);
  299. }
  300. }
  301. #elif (DIODE_DIRECTION == ROW2COL)
  302. static void init_rows(void)
  303. {
  304. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  305. uint8_t pin = onboard_row_pins[x];
  306. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  307. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  308. }
  309. }
  310. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  311. {
  312. bool matrix_changed = false;
  313. uint8_t column_state = 0;
  314. //select col and wait for selection to stabilize
  315. select_col(current_col);
  316. wait_us(30);
  317. if (current_col < 6) {
  318. // read rows from expander
  319. if (expander_status) {
  320. // it's already in an error state; nothing we can do
  321. return false;
  322. }
  323. i2c_write_register(I2C_ADDR, EXPANDER_ROW_REGISTER, &column_state, 1, I2C_TIMEOUT);
  324. column_state = ~column_state;
  325. } else {
  326. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  327. if ((_SFR_IO8(onboard_row_pins[current_row] >> 4) & _BV(onboard_row_pins[current_row] & 0xF)) == 0) {
  328. column_state |= (1 << current_row);
  329. }
  330. }
  331. }
  332. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  333. // Store last value of row prior to reading
  334. matrix_row_t last_row_value = current_matrix[current_row];
  335. if (column_state & (1 << current_row)) {
  336. // key closed; set state bit in matrix
  337. current_matrix[current_row] |= (MATRIX_ROW_SHIFTER << current_col);
  338. } else {
  339. // key open; clear state bit in matrix
  340. current_matrix[current_row] &= ~(MATRIX_ROW_SHIFTER << current_col);
  341. }
  342. // Determine whether the matrix changed state
  343. if ((last_row_value != current_matrix[current_row]) && !(matrix_changed))
  344. {
  345. matrix_changed = true;
  346. }
  347. }
  348. unselect_col(current_col);
  349. return matrix_changed;
  350. }
  351. static void select_col(uint8_t col)
  352. {
  353. if (col_expanded[col]) {
  354. // select on expander
  355. if (expander_status) { // if there was an error
  356. // do nothing
  357. } else {
  358. // set active col low : 0
  359. // set other cols hi-Z : 1
  360. expander_status = i2c_start(I2C_ADDR_WRITE); if (expander_status) goto out;
  361. expander_status = i2c_write(EXPANDER_COL_REGISTER); if (expander_status) goto out;
  362. expander_status = i2c_write(0xFF & ~(1<<col)); if (expander_status) goto out;
  363. out:
  364. i2c_stop();
  365. }
  366. } else {
  367. // select on teensy
  368. uint8_t pin = onboard_col_pins[col];
  369. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  370. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  371. }
  372. }
  373. static void unselect_col(uint8_t col)
  374. {
  375. if (col_expanded[col]) {
  376. // No need to explicitly unselect expander pins--their I/O state is
  377. // set simultaneously, with a single bitmask sent to i2c_write. When
  378. // select_col selects a single pin, it implicitly unselects all the
  379. // other ones.
  380. } else {
  381. // unselect on teensy
  382. uint8_t pin = onboard_col_pins[col];
  383. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  384. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  385. }
  386. }
  387. static void unselect_cols(void)
  388. {
  389. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  390. unselect_col(x);
  391. }
  392. }
  393. #endif