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.

510 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. inline
  243. bool matrix_is_on(uint8_t row, uint8_t col)
  244. {
  245. return (matrix[row] & (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. expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
  286. expander_status = i2c_write(EXPANDER_COL_REGISTER, I2C_TIMEOUT); if (expander_status) goto out;
  287. expander_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (expander_status) goto out;
  288. current_matrix[current_row] |= (~i2c_read_nack(I2C_TIMEOUT)) & expander_input_pin_mask;
  289. out:
  290. i2c_stop();
  291. }
  292. // Read columns from onboard pins
  293. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  294. if (! col_expanded[col_index]) {
  295. uint8_t pin = onboard_col_pins[col_index];
  296. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  297. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  298. }
  299. }
  300. unselect_row(current_row);
  301. return (last_row_value != current_matrix[current_row]);
  302. }
  303. static void select_row(uint8_t row) {
  304. // select on expander, unless it's in an error state
  305. if (! expander_status) {
  306. // set active row low : 0
  307. // set other rows hi-Z : 1
  308. expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
  309. expander_status = i2c_write(EXPANDER_ROW_REGISTER, I2C_TIMEOUT); if (expander_status) goto out;
  310. expander_status = i2c_write(0xFF & ~(1<<row), I2C_TIMEOUT); if (expander_status) goto out;
  311. out:
  312. i2c_stop();
  313. }
  314. // select on teensy
  315. uint8_t pin = onboard_row_pins[row];
  316. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  317. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  318. }
  319. static void unselect_row(uint8_t row)
  320. {
  321. // No need to explicitly unselect expander pins--their I/O state is
  322. // set simultaneously, with a single bitmask sent to i2c_write. When
  323. // select_row selects a single pin, it implicitly unselects all the
  324. // other ones.
  325. // unselect on teensy
  326. uint8_t pin = onboard_row_pins[row];
  327. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // OUT
  328. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // LOW
  329. }
  330. static void unselect_rows(void) {
  331. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  332. unselect_row(x);
  333. }
  334. }
  335. #elif (DIODE_DIRECTION == ROW2COL)
  336. static void init_rows(void)
  337. {
  338. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  339. uint8_t pin = onboard_row_pins[x];
  340. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  341. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  342. }
  343. }
  344. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  345. {
  346. bool matrix_changed = false;
  347. uint8_t column_state = 0;
  348. //select col and wait for selection to stabilize
  349. select_col(current_col);
  350. wait_us(30);
  351. if (current_col < 6) {
  352. // read rows from expander
  353. if (expander_status) {
  354. // it's already in an error state; nothing we can do
  355. return false;
  356. }
  357. expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
  358. expander_status = i2c_write(EXPANDER_ROW_REGISTER, I2C_TIMEOUT); if (expander_status) goto out;
  359. expander_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (expander_status) goto out;
  360. column_state = i2c_read_nack(I2C_TIMEOUT);
  361. out:
  362. i2c_stop();
  363. column_state = ~column_state;
  364. } else {
  365. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  366. if ((_SFR_IO8(onboard_row_pins[current_row] >> 4) & _BV(onboard_row_pins[current_row] & 0xF)) == 0) {
  367. column_state |= (1 << current_row);
  368. }
  369. }
  370. }
  371. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  372. // Store last value of row prior to reading
  373. matrix_row_t last_row_value = current_matrix[current_row];
  374. if (column_state & (1 << current_row)) {
  375. // key closed; set state bit in matrix
  376. current_matrix[current_row] |= (ROW_SHIFTER << current_col);
  377. } else {
  378. // key open; clear state bit in matrix
  379. current_matrix[current_row] &= ~(ROW_SHIFTER << current_col);
  380. }
  381. // Determine whether the matrix changed state
  382. if ((last_row_value != current_matrix[current_row]) && !(matrix_changed))
  383. {
  384. matrix_changed = true;
  385. }
  386. }
  387. unselect_col(current_col);
  388. return matrix_changed;
  389. }
  390. static void select_col(uint8_t col)
  391. {
  392. if (col_expanded[col]) {
  393. // select on expander
  394. if (expander_status) { // if there was an error
  395. // do nothing
  396. } else {
  397. // set active col low : 0
  398. // set other cols hi-Z : 1
  399. expander_status = i2c_start(I2C_ADDR_WRITE); if (expander_status) goto out;
  400. expander_status = i2c_write(EXPANDER_COL_REGISTER); if (expander_status) goto out;
  401. expander_status = i2c_write(0xFF & ~(1<<col)); if (expander_status) goto out;
  402. out:
  403. i2c_stop();
  404. }
  405. } else {
  406. // select on teensy
  407. uint8_t pin = onboard_col_pins[col];
  408. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  409. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  410. }
  411. }
  412. static void unselect_col(uint8_t col)
  413. {
  414. if (col_expanded[col]) {
  415. // No need to explicitly unselect expander pins--their I/O state is
  416. // set simultaneously, with a single bitmask sent to i2c_write. When
  417. // select_col selects a single pin, it implicitly unselects all the
  418. // other ones.
  419. } else {
  420. // unselect on teensy
  421. uint8_t pin = onboard_col_pins[col];
  422. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  423. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  424. }
  425. }
  426. static void unselect_cols(void)
  427. {
  428. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  429. unselect_col(x);
  430. }
  431. }
  432. #endif