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.

463 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 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 DEBOUNCE
  39. # define DEBOUNCE 5
  40. #endif
  41. #if (DEBOUNCE > 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. //this replases tmk code
  116. void matrix_setup(void){
  117. i2c_init();
  118. }
  119. void matrix_init(void) {
  120. // initialize row and col
  121. #if (DIODE_DIRECTION == COL2ROW)
  122. unselect_rows();
  123. init_cols();
  124. #elif (DIODE_DIRECTION == ROW2COL)
  125. unselect_cols();
  126. init_rows();
  127. #endif
  128. // initialize matrix state: all keys off
  129. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  130. matrix[i] = 0;
  131. matrix_debouncing[i] = 0;
  132. }
  133. matrix_init_quantum();
  134. }
  135. uint8_t matrix_scan(void)
  136. {
  137. #if (DIODE_DIRECTION == COL2ROW)
  138. // Set row, read cols
  139. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  140. # if (DEBOUNCE > 0)
  141. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  142. if (matrix_changed) {
  143. debouncing = true;
  144. debouncing_time = timer_read();
  145. }
  146. # else
  147. read_cols_on_row(matrix, current_row);
  148. # endif
  149. }
  150. #elif (DIODE_DIRECTION == ROW2COL)
  151. // Set col, read rows
  152. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  153. # if (DEBOUNCE > 0)
  154. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  155. if (matrix_changed) {
  156. debouncing = true;
  157. debouncing_time = timer_read();
  158. }
  159. # else
  160. read_rows_on_col(matrix, current_col);
  161. # endif
  162. }
  163. #endif
  164. # if (DEBOUNCE > 0)
  165. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  166. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  167. matrix[i] = matrix_debouncing[i];
  168. }
  169. debouncing = false;
  170. }
  171. # endif
  172. if (i2c_transaction(SLAVE_I2C_ADDRESS_RIGHT, 0x3F, 0)){ //error has occured for main right half
  173. error_count_right++;
  174. if (error_count_right > ERROR_DISCONNECT_COUNT){ //disconnect half
  175. for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
  176. matrix[i] &= 0x3F; //mask bits to keep
  177. }
  178. }
  179. }else{ //no error
  180. error_count_right = 0;
  181. }
  182. if (i2c_transaction(SLAVE_I2C_ADDRESS_ARROW, 0X3FFF, 8)){ //error has occured for arrow cluster
  183. error_count_arrow++;
  184. if (error_count_arrow > ERROR_DISCONNECT_COUNT){ //disconnect arrow cluster
  185. for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
  186. matrix[i] &= 0x3FFF; //mask bits to keep
  187. }
  188. }
  189. }else{ //no error
  190. error_count_arrow = 0;
  191. }
  192. if (i2c_transaction(SLAVE_I2C_ADDRESS_NUMPAD, 0x1FFFF, 11)){ //error has occured for numpad
  193. error_count_numpad++;
  194. if (error_count_numpad > ERROR_DISCONNECT_COUNT){ //disconnect numpad
  195. for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
  196. matrix[i] &= 0x1FFFF; //mask bits to keep
  197. }
  198. }
  199. }else{ //no error
  200. error_count_numpad = 0;
  201. }
  202. matrix_scan_quantum();
  203. return 1;
  204. }
  205. bool matrix_is_modified(void)
  206. {
  207. #if (DEBOUNCE > 0)
  208. if (debouncing) return false;
  209. #endif
  210. return true;
  211. }
  212. inline
  213. bool matrix_is_on(uint8_t row, uint8_t col)
  214. {
  215. return (matrix[row] & ((matrix_row_t)1<<col));
  216. }
  217. inline
  218. matrix_row_t matrix_get_row(uint8_t row)
  219. {
  220. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  221. // switch blocker installed and the switch is always pressed.
  222. #ifdef MATRIX_MASKED
  223. return matrix[row] & matrix_mask[row];
  224. #else
  225. return matrix[row];
  226. #endif
  227. }
  228. void matrix_print(void)
  229. {
  230. print_matrix_header();
  231. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  232. print_hex8(row); print(": ");
  233. print_matrix_row(row);
  234. print("\n");
  235. }
  236. }
  237. uint8_t matrix_key_count(void)
  238. {
  239. uint8_t count = 0;
  240. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  241. count += matrix_bitpop(i);
  242. }
  243. return count;
  244. }
  245. #if (DIODE_DIRECTION == COL2ROW)
  246. static void init_cols(void)
  247. {
  248. for(uint8_t x = 0; x < MATRIX_COLS_SCANNED; x++) {
  249. uint8_t pin = col_pins[x];
  250. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  251. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  252. }
  253. }
  254. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  255. {
  256. // Store last value of row prior to reading
  257. matrix_row_t last_row_value = current_matrix[current_row];
  258. // Clear data in matrix row
  259. current_matrix[current_row] = 0;
  260. // Select row and wait for row selecton to stabilize
  261. select_row(current_row);
  262. wait_us(30);
  263. // For each col...
  264. for(uint8_t col_index = 0; col_index < MATRIX_COLS_SCANNED; col_index++) {
  265. // Select the col pin to read (active low)
  266. uint8_t pin = col_pins[col_index];
  267. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  268. // Populate the matrix row with the state of the col pin
  269. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  270. }
  271. // Unselect row
  272. unselect_row(current_row);
  273. return (last_row_value != current_matrix[current_row]);
  274. }
  275. static void select_row(uint8_t row)
  276. {
  277. uint8_t pin = row_pins[row];
  278. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  279. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  280. }
  281. static void unselect_row(uint8_t row)
  282. {
  283. uint8_t pin = row_pins[row];
  284. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  285. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  286. }
  287. static void unselect_rows(void)
  288. {
  289. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  290. uint8_t pin = row_pins[x];
  291. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  292. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  293. }
  294. }
  295. #elif (DIODE_DIRECTION == ROW2COL)
  296. static void init_rows(void)
  297. {
  298. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  299. uint8_t pin = row_pins[x];
  300. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  301. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  302. }
  303. }
  304. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  305. {
  306. bool matrix_changed = false;
  307. // Select col and wait for col selecton to stabilize
  308. select_col(current_col);
  309. wait_us(30);
  310. // For each row...
  311. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  312. {
  313. // Store last value of row prior to reading
  314. matrix_row_t last_row_value = current_matrix[row_index];
  315. // Check row pin state
  316. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  317. {
  318. // Pin LO, set col bit
  319. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  320. }
  321. else
  322. {
  323. // Pin HI, clear col bit
  324. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  325. }
  326. // Determine if the matrix changed state
  327. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  328. {
  329. matrix_changed = true;
  330. }
  331. }
  332. // Unselect col
  333. unselect_col(current_col);
  334. return matrix_changed;
  335. }
  336. static void select_col(uint8_t col)
  337. {
  338. uint8_t pin = col_pins[col];
  339. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  340. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  341. }
  342. static void unselect_col(uint8_t col)
  343. {
  344. uint8_t pin = col_pins[col];
  345. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  346. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  347. }
  348. static void unselect_cols(void)
  349. {
  350. for(uint8_t x = 0; x < MATRIX_COLS_SCANNED; x++) {
  351. uint8_t pin = col_pins[x];
  352. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  353. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  354. }
  355. }
  356. #endif
  357. // Complete rows from other modules over i2c
  358. i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset) {
  359. i2c_status_t err = i2c_start((address << 1) | I2C_WRITE, 10);
  360. i2c_write(0x01, 10); //request data in address 1
  361. i2c_start((address << 1) | I2C_READ, 5);
  362. err = i2c_read_ack(10);
  363. if (err == 0x55) { //synchronization byte
  364. for (uint8_t i = 0; i < MATRIX_ROWS-1 ; i++) { //assemble slave matrix in main matrix
  365. matrix[i] &= mask; //mask bits to keep
  366. err = i2c_read_ack(10);
  367. matrix[i] |= ((uint32_t)err << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
  368. }
  369. //last read request must be followed by a NACK
  370. matrix[MATRIX_ROWS - 1] &= mask; //mask bits to keep
  371. err = i2c_read_nack(10);
  372. matrix[MATRIX_ROWS - 1] |= ((uint32_t)err << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
  373. } else {
  374. i2c_stop();
  375. return 1;
  376. }
  377. i2c_stop();
  378. return 0;
  379. }