matrix.c 12 KB

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