matrix.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. Copyright 2012 Jun Wako
  3. Copyright 2014 Jack Humbert
  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. #if defined(__AVR__)
  18. #include <avr/io.h>
  19. #include <avr/wdt.h>
  20. #include <avr/interrupt.h>
  21. #include <util/delay.h>
  22. #endif
  23. #include "wait.h"
  24. #include "print.h"
  25. #include "debug.h"
  26. #include "gpio.h"
  27. #include "util.h"
  28. #include "matrix.h"
  29. #include "timer.h"
  30. #include "i2c_master.h"
  31. #define SLAVE_I2C_ADDRESS_RIGHT 0x32
  32. #define SLAVE_I2C_ADDRESS_NUMPAD 0x36
  33. #define SLAVE_I2C_ADDRESS_ARROW 0x40
  34. #define ERROR_DISCONNECT_COUNT 5
  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. #if (MATRIX_COLS <= 8)
  44. # define print_matrix_header() print("\nr/c 01234567\n")
  45. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  46. #elif (MATRIX_COLS <= 16)
  47. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  48. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  49. #elif (MATRIX_COLS <= 32)
  50. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  51. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  52. #endif
  53. #ifdef MATRIX_MASKED
  54. extern const matrix_row_t matrix_mask[];
  55. #endif
  56. #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  57. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  58. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  59. #endif
  60. /* matrix state(1:on, 0:off) */
  61. static matrix_row_t matrix[MATRIX_ROWS];
  62. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  63. #if (DIODE_DIRECTION == COL2ROW)
  64. static void init_cols(void);
  65. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  66. static void unselect_rows(void);
  67. static void select_row(uint8_t row);
  68. static void unselect_row(uint8_t row);
  69. #elif (DIODE_DIRECTION == ROW2COL)
  70. static void init_rows(void);
  71. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  72. static void unselect_cols(void);
  73. static void unselect_col(uint8_t col);
  74. static void select_col(uint8_t col);
  75. #endif
  76. __attribute__ ((weak))
  77. void matrix_init_kb(void) {
  78. matrix_init_user();
  79. }
  80. __attribute__ ((weak))
  81. void matrix_scan_kb(void) {
  82. matrix_scan_user();
  83. }
  84. __attribute__ ((weak))
  85. void matrix_init_user(void) {
  86. }
  87. __attribute__ ((weak))
  88. void matrix_scan_user(void) {
  89. }
  90. inline
  91. uint8_t matrix_rows(void) {
  92. return MATRIX_ROWS;
  93. }
  94. inline
  95. uint8_t matrix_cols(void) {
  96. return MATRIX_COLS;
  97. }
  98. i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset);
  99. //this replases tmk code
  100. void matrix_setup(void){
  101. i2c_init();
  102. }
  103. void matrix_init(void) {
  104. // initialize row and col
  105. #if (DIODE_DIRECTION == COL2ROW)
  106. unselect_rows();
  107. init_cols();
  108. #elif (DIODE_DIRECTION == ROW2COL)
  109. unselect_cols();
  110. init_rows();
  111. #endif
  112. // initialize matrix state: all keys off
  113. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  114. matrix[i] = 0;
  115. matrix_debouncing[i] = 0;
  116. }
  117. matrix_init_kb();
  118. }
  119. uint8_t matrix_scan(void)
  120. {
  121. #if (DIODE_DIRECTION == COL2ROW)
  122. // Set row, read cols
  123. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  124. # if (DEBOUNCE > 0)
  125. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  126. if (matrix_changed) {
  127. debouncing = true;
  128. debouncing_time = timer_read();
  129. }
  130. # else
  131. read_cols_on_row(matrix, current_row);
  132. # endif
  133. }
  134. #elif (DIODE_DIRECTION == ROW2COL)
  135. // Set col, read rows
  136. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  137. # if (DEBOUNCE > 0)
  138. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  139. if (matrix_changed) {
  140. debouncing = true;
  141. debouncing_time = timer_read();
  142. }
  143. # else
  144. read_rows_on_col(matrix, current_col);
  145. # endif
  146. }
  147. #endif
  148. # if (DEBOUNCE > 0)
  149. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  150. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  151. matrix[i] = matrix_debouncing[i];
  152. }
  153. debouncing = false;
  154. }
  155. # endif
  156. if (i2c_transaction(SLAVE_I2C_ADDRESS_RIGHT, 0x3F, 0)) {
  157. for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
  158. matrix[i] &= 0x3F; //mask bits to keep
  159. }
  160. }
  161. if (i2c_transaction(SLAVE_I2C_ADDRESS_ARROW, 0X3FFF, 8)) {
  162. for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
  163. matrix[i] &= 0x3FFF; //mask bits to keep
  164. }
  165. }
  166. if (i2c_transaction(SLAVE_I2C_ADDRESS_NUMPAD, 0x1FFFF, 11)) {
  167. for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
  168. matrix[i] &= 0x1FFFF; //mask bits to keep
  169. }
  170. }
  171. matrix_scan_kb();
  172. return 1;
  173. }
  174. inline
  175. bool matrix_is_on(uint8_t row, uint8_t col)
  176. {
  177. return (matrix[row] & ((matrix_row_t)1<<col));
  178. }
  179. inline
  180. matrix_row_t matrix_get_row(uint8_t row)
  181. {
  182. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  183. // switch blocker installed and the switch is always pressed.
  184. #ifdef MATRIX_MASKED
  185. return matrix[row] & matrix_mask[row];
  186. #else
  187. return matrix[row];
  188. #endif
  189. }
  190. void matrix_print(void)
  191. {
  192. print_matrix_header();
  193. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  194. print_hex8(row); print(": ");
  195. print_matrix_row(row);
  196. print("\n");
  197. }
  198. }
  199. #if (DIODE_DIRECTION == COL2ROW)
  200. static void init_cols(void)
  201. {
  202. for(uint8_t x = 0; x < MATRIX_COLS_SCANNED; x++) {
  203. uint8_t pin = col_pins[x];
  204. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  205. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  206. }
  207. }
  208. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  209. {
  210. // Store last value of row prior to reading
  211. matrix_row_t last_row_value = current_matrix[current_row];
  212. // Clear data in matrix row
  213. current_matrix[current_row] = 0;
  214. // Select row and wait for row selecton to stabilize
  215. select_row(current_row);
  216. wait_us(30);
  217. // For each col...
  218. for(uint8_t col_index = 0; col_index < MATRIX_COLS_SCANNED; col_index++) {
  219. // Select the col pin to read (active low)
  220. uint8_t pin = col_pins[col_index];
  221. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  222. // Populate the matrix row with the state of the col pin
  223. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  224. }
  225. // Unselect row
  226. unselect_row(current_row);
  227. return (last_row_value != current_matrix[current_row]);
  228. }
  229. static void select_row(uint8_t row)
  230. {
  231. uint8_t pin = row_pins[row];
  232. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  233. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  234. }
  235. static void unselect_row(uint8_t row)
  236. {
  237. uint8_t pin = row_pins[row];
  238. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  239. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  240. }
  241. static void unselect_rows(void)
  242. {
  243. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  244. uint8_t pin = row_pins[x];
  245. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  246. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  247. }
  248. }
  249. #elif (DIODE_DIRECTION == ROW2COL)
  250. static void init_rows(void)
  251. {
  252. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  253. uint8_t pin = row_pins[x];
  254. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  255. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  256. }
  257. }
  258. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  259. {
  260. bool matrix_changed = false;
  261. // Select col and wait for col selecton to stabilize
  262. select_col(current_col);
  263. wait_us(30);
  264. // For each row...
  265. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  266. {
  267. // Store last value of row prior to reading
  268. matrix_row_t last_row_value = current_matrix[row_index];
  269. // Check row pin state
  270. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  271. {
  272. // Pin LO, set col bit
  273. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  274. }
  275. else
  276. {
  277. // Pin HI, clear col bit
  278. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  279. }
  280. // Determine if the matrix changed state
  281. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  282. {
  283. matrix_changed = true;
  284. }
  285. }
  286. // Unselect col
  287. unselect_col(current_col);
  288. return matrix_changed;
  289. }
  290. static void select_col(uint8_t col)
  291. {
  292. uint8_t pin = col_pins[col];
  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_col(uint8_t col)
  297. {
  298. uint8_t pin = col_pins[col];
  299. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  300. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  301. }
  302. static void unselect_cols(void)
  303. {
  304. for(uint8_t x = 0; x < MATRIX_COLS_SCANNED; x++) {
  305. uint8_t pin = col_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. #endif
  311. // Complete rows from other modules over i2c
  312. i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset) {
  313. uint8_t data[MATRIX_ROWS + 1];
  314. i2c_status_t status = i2c_read_register(address, 0x01, data, (MATRIX_ROWS + 1), 5);
  315. for (uint8_t i = 0; i < (MATRIX_ROWS) && status >= 0; i++) { //assemble slave matrix in main matrix
  316. matrix[i] &= mask; //mask bits to keep
  317. matrix[i] |= ((uint32_t)data[i+1] << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
  318. }
  319. return status;
  320. }