You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

453 lines
12 KiB

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