logo

qmk_firmware

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

matrix.c (13706B)


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