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.

527 lines
16 KiB

  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 "pterodactyl.h"
  25. #include "i2c_master.h"
  26. #include "timer.h"
  27. #define I2C_TIMEOUT 100
  28. #define I2C_ADDR 0b0100000
  29. #define I2C_ADDR_WRITE ( (I2C_ADDR<<1) | I2C_WRITE )
  30. #define I2C_ADDR_READ ( (I2C_ADDR<<1) | I2C_READ )
  31. #define IODIRA 0x00 // i/o direction register
  32. #define IODIRB 0x01
  33. #define GPPUA 0x0C // GPIO pull-up resistor register
  34. #define GPPUB 0x0D
  35. #define GPIOA 0x12 // general purpose i/o port register (write modifies OLAT)
  36. #define GPIOB 0x13
  37. void init_expander(void);
  38. /* Set 0 if debouncing isn't needed */
  39. #ifndef DEBOUNCE
  40. # define DEBOUNCE 5
  41. #endif
  42. #if (DEBOUNCE > 0)
  43. static uint16_t debouncing_time;
  44. static bool debouncing = false;
  45. #endif
  46. #ifdef MATRIX_MASKED
  47. extern const matrix_row_t matrix_mask[];
  48. #endif
  49. #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  50. static const uint8_t onboard_row_pins[MATRIX_ROWS] = MATRIX_ONBOARD_ROW_PINS;
  51. static const uint8_t onboard_col_pins[MATRIX_COLS] = MATRIX_ONBOARD_COL_PINS;
  52. static const bool col_expanded[MATRIX_COLS] = COL_EXPANDED;
  53. #endif
  54. /* matrix state(1:on, 0:off) */
  55. static matrix_row_t matrix[MATRIX_ROWS];
  56. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  57. #if (DIODE_DIRECTION == COL2ROW)
  58. static const uint8_t expander_col_pins[MATRIX_COLS] = MATRIX_EXPANDER_COL_PINS;
  59. static void init_cols(void);
  60. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  61. static void unselect_rows(void);
  62. static void select_row(uint8_t row);
  63. static void unselect_row(uint8_t row);
  64. #elif (DIODE_DIRECTION == ROW2COL)
  65. static const uint8_t expander_row_pins[MATRIX_ROWS] = MATRIX_EXPANDER_ROW_PINS;
  66. static void init_rows(void);
  67. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  68. static void unselect_cols(void);
  69. static void select_col(uint8_t col);
  70. static void unselect_col(uint8_t col);
  71. #endif
  72. static uint8_t expander_reset_loop;
  73. uint8_t expander_status;
  74. uint8_t expander_input_pin_mask;
  75. bool i2c_initialized = false;
  76. #define ROW_SHIFTER ((matrix_row_t)1)
  77. __attribute__ ((weak))
  78. void matrix_init_user(void) {}
  79. __attribute__ ((weak))
  80. void matrix_scan_user(void) {}
  81. __attribute__ ((weak))
  82. void matrix_init_kb(void) {
  83. matrix_init_user();
  84. }
  85. __attribute__ ((weak))
  86. void matrix_scan_kb(void) {
  87. matrix_scan_user();
  88. }
  89. inline
  90. uint8_t matrix_rows(void)
  91. {
  92. return MATRIX_ROWS;
  93. }
  94. inline
  95. uint8_t matrix_cols(void)
  96. {
  97. return MATRIX_COLS;
  98. }
  99. void matrix_init(void)
  100. {
  101. init_expander();
  102. #if (DIODE_DIRECTION == COL2ROW)
  103. unselect_rows();
  104. init_cols();
  105. #elif (DIODE_DIRECTION == ROW2COL)
  106. unselect_cols();
  107. init_rows();
  108. #endif
  109. // initialize matrix state: all keys off
  110. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  111. matrix[i] = 0;
  112. matrix_debouncing[i] = 0;
  113. }
  114. matrix_init_quantum();
  115. }
  116. void init_expander(void) {
  117. if (! i2c_initialized) {
  118. i2c_init();
  119. wait_ms(1000);
  120. }
  121. if (! expander_input_pin_mask) {
  122. #if (DIODE_DIRECTION == COL2ROW)
  123. for (int col = 0; col < MATRIX_COLS; col++) {
  124. if (col_expanded[col]) {
  125. expander_input_pin_mask |= (1 << expander_col_pins[col]);
  126. }
  127. }
  128. #elif (DIODE_DIRECTION == ROW2COL)
  129. for (int row = 0; row < MATRIX_ROWS; row++) {
  130. expander_input_pin_mask |= (1 << expander_row_pins[row]);
  131. }
  132. #endif
  133. }
  134. expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
  135. expander_status = i2c_write(IODIRA, I2C_TIMEOUT); if (expander_status) goto out;
  136. /*
  137. Pin direction and pull-up depends on both the diode direction
  138. and on whether the column register is GPIOA or GPIOB
  139. +-------+---------------+---------------+
  140. | | ROW2COL | COL2ROW |
  141. +-------+---------------+---------------+
  142. | GPIOA | input, output | output, input |
  143. +-------+---------------+---------------+
  144. | GPIOB | output, input | input, output |
  145. +-------+---------------+---------------+
  146. */
  147. #if (EXPANDER_COL_REGISTER == GPIOA)
  148. # if (DIODE_DIRECTION == COL2ROW)
  149. expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
  150. expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
  151. # elif (DIODE_DIRECTION == ROW2COL)
  152. expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
  153. expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
  154. # endif
  155. #elif (EXPANDER_COL_REGISTER == GPIOB)
  156. # if (DIODE_DIRECTION == COL2ROW)
  157. expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
  158. expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
  159. # elif (DIODE_DIRECTION == ROW2COL)
  160. expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
  161. expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
  162. # endif
  163. #endif
  164. i2c_stop();
  165. // set pull-up
  166. // - unused : off : 0
  167. // - input : on : 1
  168. // - driving : off : 0
  169. expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
  170. expander_status = i2c_write(GPPUA, I2C_TIMEOUT); if (expander_status) goto out;
  171. #if (EXPANDER_COL_REGISTER == GPIOA)
  172. # if (DIODE_DIRECTION == COL2ROW)
  173. expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
  174. expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
  175. # elif (DIODE_DIRECTION == ROW2COL)
  176. expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
  177. expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
  178. # endif
  179. #elif (EXPANDER_COL_REGISTER == GPIOB)
  180. # if (DIODE_DIRECTION == COL2ROW)
  181. expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
  182. expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
  183. # elif (DIODE_DIRECTION == ROW2COL)
  184. expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
  185. expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
  186. # endif
  187. #endif
  188. out:
  189. i2c_stop();
  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_quantum();
  240. return 1;
  241. }
  242. bool matrix_is_modified(void) // deprecated and evidently not called.
  243. {
  244. #if (DEBOUNCE > 0)
  245. if (debouncing) return false;
  246. #endif
  247. return true;
  248. }
  249. inline
  250. bool matrix_is_on(uint8_t row, uint8_t col)
  251. {
  252. return (matrix[row] & (ROW_SHIFTER << col));
  253. }
  254. inline
  255. matrix_row_t matrix_get_row(uint8_t row)
  256. {
  257. #ifdef MATRIX_MASKED
  258. return matrix[row] & matrix_mask[row];
  259. #else
  260. return matrix[row];
  261. #endif
  262. }
  263. void matrix_print(void)
  264. {
  265. print("\nr/c 0123456789ABCDEF\n");
  266. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  267. print_hex8(row); print(": ");
  268. print_bin_reverse16(matrix_get_row(row));
  269. print("\n");
  270. }
  271. }
  272. uint8_t matrix_key_count(void)
  273. {
  274. uint8_t count = 0;
  275. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  276. count += bitpop16(matrix[i]);
  277. }
  278. return count;
  279. }
  280. #if (DIODE_DIRECTION == COL2ROW)
  281. static void init_cols(void) {
  282. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  283. if (! col_expanded[x]) {
  284. uint8_t pin = onboard_col_pins[x];
  285. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  286. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  287. }
  288. }
  289. }
  290. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  291. // Store last value of row prior to reading
  292. matrix_row_t last_row_value = current_matrix[current_row];
  293. // Clear data in matrix row
  294. current_matrix[current_row] = 0;
  295. // Select row and wait for row selection to stabilize
  296. select_row(current_row);
  297. wait_us(30);
  298. // Read columns from expander, unless it's in an error state
  299. if (! expander_status) {
  300. expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
  301. expander_status = i2c_write(EXPANDER_COL_REGISTER, I2C_TIMEOUT); if (expander_status) goto out;
  302. expander_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (expander_status) goto out;
  303. current_matrix[current_row] |= (~i2c_read_nack(I2C_TIMEOUT)) & expander_input_pin_mask;
  304. out:
  305. i2c_stop();
  306. }
  307. // Read columns from onboard pins
  308. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  309. if (! col_expanded[col_index]) {
  310. uint8_t pin = onboard_col_pins[col_index];
  311. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  312. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  313. }
  314. }
  315. unselect_row(current_row);
  316. return (last_row_value != current_matrix[current_row]);
  317. }
  318. static void select_row(uint8_t row) {
  319. // select on expander, unless it's in an error state
  320. if (! expander_status) {
  321. // set active row low : 0
  322. // set other rows hi-Z : 1
  323. expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
  324. expander_status = i2c_write(EXPANDER_ROW_REGISTER, I2C_TIMEOUT); if (expander_status) goto out;
  325. expander_status = i2c_write(0xFF & ~(1<<row), I2C_TIMEOUT); if (expander_status) goto out;
  326. out:
  327. i2c_stop();
  328. }
  329. // select on teensy
  330. uint8_t pin = onboard_row_pins[row];
  331. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  332. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  333. }
  334. static void unselect_row(uint8_t row)
  335. {
  336. // No need to explicitly unselect expander pins--their I/O state is
  337. // set simultaneously, with a single bitmask sent to i2c_write. When
  338. // select_row selects a single pin, it implicitly unselects all the
  339. // other ones.
  340. // unselect on teensy
  341. uint8_t pin = onboard_row_pins[row];
  342. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // OUT
  343. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // LOW
  344. }
  345. static void unselect_rows(void) {
  346. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  347. unselect_row(x);
  348. }
  349. }
  350. #elif (DIODE_DIRECTION == ROW2COL)
  351. static void init_rows(void)
  352. {
  353. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  354. uint8_t pin = onboard_row_pins[x];
  355. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  356. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  357. }
  358. }
  359. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  360. {
  361. bool matrix_changed = false;
  362. uint8_t column_state = 0;
  363. //select col and wait for selection to stabilize
  364. select_col(current_col);
  365. wait_us(30);
  366. if (current_col < 6) {
  367. // read rows from expander
  368. if (expander_status) {
  369. // it's already in an error state; nothing we can do
  370. return false;
  371. }
  372. expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
  373. expander_status = i2c_write(EXPANDER_ROW_REGISTER, I2C_TIMEOUT); if (expander_status) goto out;
  374. expander_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (expander_status) goto out;
  375. column_state = i2c_read_nack(I2C_TIMEOUT);
  376. out:
  377. i2c_stop();
  378. column_state = ~column_state;
  379. } else {
  380. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  381. if ((_SFR_IO8(onboard_row_pins[current_row] >> 4) & _BV(onboard_row_pins[current_row] & 0xF)) == 0) {
  382. column_state |= (1 << current_row);
  383. }
  384. }
  385. }
  386. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  387. // Store last value of row prior to reading
  388. matrix_row_t last_row_value = current_matrix[current_row];
  389. if (column_state & (1 << current_row)) {
  390. // key closed; set state bit in matrix
  391. current_matrix[current_row] |= (ROW_SHIFTER << current_col);
  392. } else {
  393. // key open; clear state bit in matrix
  394. current_matrix[current_row] &= ~(ROW_SHIFTER << current_col);
  395. }
  396. // Determine whether the matrix changed state
  397. if ((last_row_value != current_matrix[current_row]) && !(matrix_changed))
  398. {
  399. matrix_changed = true;
  400. }
  401. }
  402. unselect_col(current_col);
  403. return matrix_changed;
  404. }
  405. static void select_col(uint8_t col)
  406. {
  407. if (col_expanded[col]) {
  408. // select on expander
  409. if (expander_status) { // if there was an error
  410. // do nothing
  411. } else {
  412. // set active col low : 0
  413. // set other cols hi-Z : 1
  414. expander_status = i2c_start(I2C_ADDR_WRITE); if (expander_status) goto out;
  415. expander_status = i2c_write(EXPANDER_COL_REGISTER); if (expander_status) goto out;
  416. expander_status = i2c_write(0xFF & ~(1<<col)); if (expander_status) goto out;
  417. out:
  418. i2c_stop();
  419. }
  420. } else {
  421. // select on teensy
  422. uint8_t pin = onboard_col_pins[col];
  423. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  424. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  425. }
  426. }
  427. static void unselect_col(uint8_t col)
  428. {
  429. if (col_expanded[col]) {
  430. // No need to explicitly unselect expander pins--their I/O state is
  431. // set simultaneously, with a single bitmask sent to i2c_write. When
  432. // select_col selects a single pin, it implicitly unselects all the
  433. // other ones.
  434. } else {
  435. // unselect on teensy
  436. uint8_t pin = onboard_col_pins[col];
  437. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  438. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  439. }
  440. }
  441. static void unselect_cols(void)
  442. {
  443. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  444. unselect_col(x);
  445. }
  446. }
  447. #endif