logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (13877B)


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