matrix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 "util.h"
  27. #include "matrix.h"
  28. #include "timer.h"
  29. #include "i2c_master.h"
  30. #define SLAVE_I2C_ADDRESS_RIGHT 0x19
  31. #define SLAVE_I2C_ADDRESS_NUMPAD 0x21
  32. #define SLAVE_I2C_ADDRESS_ARROW 0x23
  33. #define ERROR_DISCONNECT_COUNT 5
  34. static uint8_t error_count_right = 0;
  35. static uint8_t error_count_numpad = 0;
  36. static uint8_t error_count_arrow = 0;
  37. /* Set 0 if debouncing isn't needed */
  38. #ifndef DEBOUNCING_DELAY
  39. # define DEBOUNCING_DELAY 5
  40. #endif
  41. #if (DEBOUNCING_DELAY > 0)
  42. static uint16_t debouncing_time;
  43. static bool debouncing = false;
  44. #endif
  45. #if (MATRIX_COLS <= 8)
  46. # define print_matrix_header() print("\nr/c 01234567\n")
  47. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  48. # define matrix_bitpop(i) bitpop(matrix[i])
  49. # define ROW_SHIFTER ((uint8_t)1)
  50. #elif (MATRIX_COLS <= 16)
  51. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  52. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  53. # define matrix_bitpop(i) bitpop16(matrix[i])
  54. # define ROW_SHIFTER ((uint16_t)1)
  55. #elif (MATRIX_COLS <= 32)
  56. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  57. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  58. # define matrix_bitpop(i) bitpop32(matrix[i])
  59. # define ROW_SHIFTER ((uint32_t)1)
  60. #endif
  61. #ifdef MATRIX_MASKED
  62. extern const matrix_row_t matrix_mask[];
  63. #endif
  64. #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  65. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  66. static const uint8_t col_pins[MATRIX_COLS_SCANNED] = MATRIX_COL_PINS;
  67. #endif
  68. /* matrix state(1:on, 0:off) */
  69. static matrix_row_t matrix[MATRIX_ROWS];
  70. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  71. #if (DIODE_DIRECTION == COL2ROW)
  72. static void init_cols(void);
  73. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  74. static void unselect_rows(void);
  75. static void select_row(uint8_t row);
  76. static void unselect_row(uint8_t row);
  77. #elif (DIODE_DIRECTION == ROW2COL)
  78. static void init_rows(void);
  79. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  80. static void unselect_cols(void);
  81. static void unselect_col(uint8_t col);
  82. static void select_col(uint8_t col);
  83. #endif
  84. __attribute__ ((weak))
  85. void matrix_init_quantum(void) {
  86. matrix_init_kb();
  87. }
  88. __attribute__ ((weak))
  89. void matrix_scan_quantum(void) {
  90. matrix_scan_kb();
  91. }
  92. __attribute__ ((weak))
  93. void matrix_init_kb(void) {
  94. matrix_init_user();
  95. }
  96. __attribute__ ((weak))
  97. void matrix_scan_kb(void) {
  98. matrix_scan_user();
  99. }
  100. __attribute__ ((weak))
  101. void matrix_init_user(void) {
  102. }
  103. __attribute__ ((weak))
  104. void matrix_scan_user(void) {
  105. }
  106. inline
  107. uint8_t matrix_rows(void) {
  108. return MATRIX_ROWS;
  109. }
  110. inline
  111. uint8_t matrix_cols(void) {
  112. return MATRIX_COLS;
  113. }
  114. i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset);
  115. //uint8_t i2c_transaction_numpad(void);
  116. //uint8_t i2c_transaction_arrow(void);
  117. //this replases tmk code
  118. void matrix_setup(void){
  119. i2c_init();
  120. }
  121. void matrix_init(void) {
  122. // initialize row and col
  123. #if (DIODE_DIRECTION == COL2ROW)
  124. unselect_rows();
  125. init_cols();
  126. #elif (DIODE_DIRECTION == ROW2COL)
  127. unselect_cols();
  128. init_rows();
  129. #endif
  130. // initialize matrix state: all keys off
  131. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  132. matrix[i] = 0;
  133. matrix_debouncing[i] = 0;
  134. }
  135. matrix_init_quantum();
  136. }
  137. uint8_t matrix_scan(void)
  138. {
  139. #if (DIODE_DIRECTION == COL2ROW)
  140. // Set row, read cols
  141. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  142. # if (DEBOUNCING_DELAY > 0)
  143. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  144. if (matrix_changed) {
  145. debouncing = true;
  146. debouncing_time = timer_read();
  147. }
  148. # else
  149. read_cols_on_row(matrix, current_row);
  150. # endif
  151. }
  152. #elif (DIODE_DIRECTION == ROW2COL)
  153. // Set col, read rows
  154. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  155. # if (DEBOUNCING_DELAY > 0)
  156. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  157. if (matrix_changed) {
  158. debouncing = true;
  159. debouncing_time = timer_read();
  160. }
  161. # else
  162. read_rows_on_col(matrix, current_col);
  163. # endif
  164. }
  165. #endif
  166. # if (DEBOUNCING_DELAY > 0)
  167. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
  168. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  169. matrix[i] = matrix_debouncing[i];
  170. }
  171. debouncing = false;
  172. }
  173. # endif
  174. if (i2c_transaction(SLAVE_I2C_ADDRESS_RIGHT, 0x3F, 0)){ //error has occured for main right half
  175. error_count_right++;
  176. if (error_count_right > ERROR_DISCONNECT_COUNT){ //disconnect half
  177. for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
  178. matrix[i] &= 0x3F; //mask bits to keep
  179. }
  180. }
  181. }else{ //no error
  182. error_count_right = 0;
  183. }
  184. if (i2c_transaction(SLAVE_I2C_ADDRESS_ARROW, 0X3FFF, 8)){ //error has occured for arrow cluster
  185. error_count_arrow++;
  186. if (error_count_arrow > ERROR_DISCONNECT_COUNT){ //disconnect arrow cluster
  187. for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
  188. matrix[i] &= 0x3FFF; //mask bits to keep
  189. }
  190. }
  191. }else{ //no error
  192. error_count_arrow = 0;
  193. }
  194. if (i2c_transaction(SLAVE_I2C_ADDRESS_NUMPAD, 0x1FFFF, 11)){ //error has occured for numpad
  195. error_count_numpad++;
  196. if (error_count_numpad > ERROR_DISCONNECT_COUNT){ //disconnect numpad
  197. for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
  198. matrix[i] &= 0x1FFFF; //mask bits to keep
  199. }
  200. }
  201. }else{ //no error
  202. error_count_numpad = 0;
  203. }
  204. matrix_scan_quantum();
  205. return 1;
  206. }
  207. bool matrix_is_modified(void)
  208. {
  209. #if (DEBOUNCING_DELAY > 0)
  210. if (debouncing) return false;
  211. #endif
  212. return true;
  213. }
  214. inline
  215. bool matrix_is_on(uint8_t row, uint8_t col)
  216. {
  217. return (matrix[row] & ((matrix_row_t)1<col));
  218. }
  219. inline
  220. matrix_row_t matrix_get_row(uint8_t row)
  221. {
  222. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  223. // switch blocker installed and the switch is always pressed.
  224. #ifdef MATRIX_MASKED
  225. return matrix[row] & matrix_mask[row];
  226. #else
  227. return matrix[row];
  228. #endif
  229. }
  230. void matrix_print(void)
  231. {
  232. print_matrix_header();
  233. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  234. phex(row); print(": ");
  235. print_matrix_row(row);
  236. print("\n");
  237. }
  238. }
  239. uint8_t matrix_key_count(void)
  240. {
  241. uint8_t count = 0;
  242. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  243. count += matrix_bitpop(i);
  244. }
  245. return count;
  246. }
  247. #if (DIODE_DIRECTION == COL2ROW)
  248. static void init_cols(void)
  249. {
  250. for(uint8_t x = 0; x < MATRIX_COLS_SCANNED; x++) {
  251. uint8_t pin = col_pins[x];
  252. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  253. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  254. }
  255. }
  256. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  257. {
  258. // Store last value of row prior to reading
  259. matrix_row_t last_row_value = current_matrix[current_row];
  260. // Clear data in matrix row
  261. current_matrix[current_row] = 0;
  262. // Select row and wait for row selecton to stabilize
  263. select_row(current_row);
  264. wait_us(30);
  265. // For each col...
  266. for(uint8_t col_index = 0; col_index < MATRIX_COLS_SCANNED; col_index++) {
  267. // Select the col pin to read (active low)
  268. uint8_t pin = col_pins[col_index];
  269. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  270. // Populate the matrix row with the state of the col pin
  271. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  272. }
  273. // Unselect row
  274. unselect_row(current_row);
  275. return (last_row_value != current_matrix[current_row]);
  276. }
  277. static void select_row(uint8_t row)
  278. {
  279. uint8_t pin = row_pins[row];
  280. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  281. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  282. }
  283. static void unselect_row(uint8_t row)
  284. {
  285. uint8_t pin = row_pins[row];
  286. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  287. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  288. }
  289. static void unselect_rows(void)
  290. {
  291. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  292. uint8_t pin = row_pins[x];
  293. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  294. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  295. }
  296. }
  297. #elif (DIODE_DIRECTION == ROW2COL)
  298. static void init_rows(void)
  299. {
  300. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  301. uint8_t pin = row_pins[x];
  302. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  303. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  304. }
  305. }
  306. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  307. {
  308. bool matrix_changed = false;
  309. // Select col and wait for col selecton to stabilize
  310. select_col(current_col);
  311. wait_us(30);
  312. // For each row...
  313. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  314. {
  315. // Store last value of row prior to reading
  316. matrix_row_t last_row_value = current_matrix[row_index];
  317. // Check row pin state
  318. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  319. {
  320. // Pin LO, set col bit
  321. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  322. }
  323. else
  324. {
  325. // Pin HI, clear col bit
  326. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  327. }
  328. // Determine if the matrix changed state
  329. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  330. {
  331. matrix_changed = true;
  332. }
  333. }
  334. // Unselect col
  335. unselect_col(current_col);
  336. return matrix_changed;
  337. }
  338. static void select_col(uint8_t col)
  339. {
  340. uint8_t pin = col_pins[col];
  341. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  342. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  343. }
  344. static void unselect_col(uint8_t col)
  345. {
  346. uint8_t pin = col_pins[col];
  347. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  348. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  349. }
  350. static void unselect_cols(void)
  351. {
  352. for(uint8_t x = 0; x < MATRIX_COLS_SCANNED; x++) {
  353. uint8_t pin = col_pins[x];
  354. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  355. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  356. }
  357. }
  358. #endif
  359. // Complete rows from other modules over i2c
  360. i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset) {
  361. i2c_status_t err = i2c_start((address << 1) | I2C_WRITE, 10);
  362. if (err) return err;
  363. i2c_write(0x01, 10);
  364. if (err) return err;
  365. i2c_start((address << 1) | I2C_READ, 10);
  366. if (err) return err;
  367. err = i2c_read_ack(10);
  368. if (err == 0x55) { //synchronization byte
  369. for (uint8_t i = 0; i < MATRIX_ROWS-1 ; i++) { //assemble slave matrix in main matrix
  370. matrix[i] &= mask; //mask bits to keep
  371. err = i2c_read_ack(10);
  372. if (err >= 0) {
  373. matrix[i] |= ((uint32_t)err << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
  374. } else {
  375. return err;
  376. }
  377. }
  378. //last read request must be followed by a NACK
  379. matrix[MATRIX_ROWS - 1] &= mask; //mask bits to keep
  380. err = i2c_read_nack(10);
  381. if (err >= 0) {
  382. matrix[MATRIX_ROWS - 1] |= ((uint32_t)err << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
  383. } else {
  384. return err;
  385. }
  386. } else {
  387. i2c_stop(10);
  388. return 1;
  389. }
  390. i2c_stop(10);
  391. if (err) return err;
  392. return 0;
  393. }