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.

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