matrix.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #ifdef USE_I2C
  20. // provides memcpy for copying TWI slave buffer
  21. // #include <string.h>
  22. #endif
  23. #include <avr/io.h>
  24. #include <avr/wdt.h>
  25. #include <avr/interrupt.h>
  26. #include <util/delay.h>
  27. #include "print.h"
  28. #include "debug.h"
  29. #include "util.h"
  30. #include "matrix.h"
  31. #include "split_util.h"
  32. #include "pro_micro.h"
  33. #include "config.h"
  34. #ifdef USE_I2C
  35. # include "i2c.h"
  36. #else // USE_SERIAL
  37. # include "serial.h"
  38. #endif
  39. #ifndef DEBOUNCE
  40. # define DEBOUNCE 5
  41. #endif
  42. #define ERROR_DISCONNECT_COUNT 5
  43. static uint8_t debouncing = DEBOUNCE;
  44. static const int ROWS_PER_HAND = MATRIX_ROWS/2;
  45. static uint8_t error_count = 0;
  46. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  47. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  48. /* matrix state(1:on, 0:off) */
  49. static matrix_row_t matrix[MATRIX_ROWS];
  50. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  51. static matrix_row_t read_cols(void);
  52. static void init_cols(void);
  53. static void unselect_rows(void);
  54. static void select_row(uint8_t row);
  55. __attribute__ ((weak))
  56. void matrix_init_kb(void) {
  57. matrix_init_user();
  58. }
  59. __attribute__ ((weak))
  60. void matrix_scan_kb(void) {
  61. matrix_scan_user();
  62. }
  63. __attribute__ ((weak))
  64. void matrix_init_user(void) {
  65. }
  66. __attribute__ ((weak))
  67. void matrix_scan_user(void) {
  68. }
  69. inline
  70. uint8_t matrix_rows(void)
  71. {
  72. return MATRIX_ROWS;
  73. }
  74. inline
  75. uint8_t matrix_cols(void)
  76. {
  77. return MATRIX_COLS;
  78. }
  79. void matrix_init(void)
  80. {
  81. debug_enable = true;
  82. debug_matrix = true;
  83. debug_mouse = true;
  84. // initialize row and col
  85. unselect_rows();
  86. init_cols();
  87. TX_RX_LED_INIT;
  88. // initialize matrix state: all keys off
  89. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  90. matrix[i] = 0;
  91. matrix_debouncing[i] = 0;
  92. }
  93. matrix_init_quantum();
  94. }
  95. uint8_t _matrix_scan(void)
  96. {
  97. // Right hand is stored after the left in the matrix so, we need to offset it
  98. int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
  99. for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
  100. select_row(i);
  101. _delay_us(30); // without this wait read unstable value.
  102. matrix_row_t cols = read_cols();
  103. if (matrix_debouncing[i+offset] != cols) {
  104. matrix_debouncing[i+offset] = cols;
  105. debouncing = DEBOUNCE;
  106. }
  107. unselect_rows();
  108. }
  109. if (debouncing) {
  110. if (--debouncing) {
  111. _delay_ms(1);
  112. } else {
  113. for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
  114. matrix[i+offset] = matrix_debouncing[i+offset];
  115. }
  116. }
  117. }
  118. return 1;
  119. }
  120. #ifdef USE_I2C
  121. // Get rows from other half over i2c
  122. int i2c_transaction(void) {
  123. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  124. int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
  125. if (err) goto i2c_error;
  126. // start of matrix stored at 0x00
  127. err = i2c_master_write(0x00);
  128. if (err) goto i2c_error;
  129. // Start read
  130. err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
  131. if (err) goto i2c_error;
  132. if (!err) {
  133. /*
  134. // read from TWI byte-by-byte into matrix_row_t memory space
  135. size_t i;
  136. for (i = 0; i < SLAVE_BUFFER_SIZE-1; ++i) {
  137. *((uint8_t*)&matrix[slaveOffset]+i) = i2c_master_read(I2C_ACK);
  138. }
  139. // last byte to be read / end of chunk
  140. *((uint8_t*)&matrix[slaveOffset]+i) = i2c_master_read(I2C_NACK);
  141. */
  142. // kludge for column #9: unpack bits for keys (2,9) and (3,9) from (1,7) and (1,8)
  143. // i2c_master_read(I2C_ACK);
  144. matrix[slaveOffset+0] = i2c_master_read(I2C_ACK);
  145. // i2c_master_read(I2C_ACK);
  146. matrix[slaveOffset+1] = (matrix_row_t)i2c_master_read(I2C_ACK)\
  147. | (matrix[slaveOffset+0]&0x40U)<<2;
  148. // i2c_master_read(I2C_ACK);
  149. matrix[slaveOffset+2] = (matrix_row_t)i2c_master_read(I2C_NACK)\
  150. | (matrix[slaveOffset+0]&0x80U)<<1;
  151. // clear highest two bits on row 1, where the col9 bits were transported
  152. matrix[slaveOffset+0] &= 0x3F;
  153. i2c_master_stop();
  154. } else {
  155. i2c_error: // the cable is disconnected, or something else went wrong
  156. i2c_reset_state();
  157. return err;
  158. }
  159. return 0;
  160. }
  161. #else // USE_SERIAL
  162. int serial_transaction(void) {
  163. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  164. if (serial_update_buffers()) {
  165. return 1;
  166. }
  167. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  168. matrix[slaveOffset+i] = serial_slave_buffer[i];
  169. }
  170. return 0;
  171. }
  172. #endif
  173. uint8_t matrix_scan(void)
  174. {
  175. int ret = _matrix_scan();
  176. #ifdef USE_I2C
  177. if( i2c_transaction() ) {
  178. #else // USE_SERIAL
  179. if( serial_transaction() ) {
  180. #endif
  181. // turn on the indicator led when halves are disconnected
  182. TXLED1;
  183. error_count++;
  184. if (error_count > ERROR_DISCONNECT_COUNT) {
  185. // reset other half if disconnected
  186. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  187. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  188. matrix[slaveOffset+i] = 0;
  189. }
  190. }
  191. } else {
  192. // turn off the indicator led on no error
  193. TXLED0;
  194. error_count = 0;
  195. }
  196. matrix_scan_quantum();
  197. return ret;
  198. }
  199. void matrix_slave_scan(void) {
  200. _matrix_scan();
  201. int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
  202. #ifdef USE_I2C
  203. // SLAVE_BUFFER_SIZE is from i2c.h
  204. // (MATRIX_ROWS/2*sizeof(matrix_row_t))
  205. // memcpy((void*)i2c_slave_buffer, (const void*)&matrix[offset], (ROWS_PER_HAND*sizeof(matrix_row_t)));
  206. // kludge for column #9: put bits for keys (2,9) and (3,9) into (1,7) and (1,8)
  207. i2c_slave_buffer[0] = (uint8_t)(matrix[offset+0])\
  208. | (matrix[offset+1]&0x100U)>>2\
  209. | (matrix[offset+2]&0x100U)>>1;
  210. i2c_slave_buffer[1] = (uint8_t)(matrix[offset+1]);
  211. i2c_slave_buffer[2] = (uint8_t)(matrix[offset+2]);
  212. // note: looks like a possible operator-precedence bug here, in last version?
  213. /*
  214. i2c_slave_buffer[1] = (uint8_t)matrix[offset+0];
  215. i2c_slave_buffer[2] = (uint8_t)(matrix[offset+1]>>8);
  216. i2c_slave_buffer[3] = (uint8_t)(matrix[offset+1]>>8);
  217. i2c_slave_buffer[4] = (uint8_t)(matrix[offset+2]>>8);
  218. i2c_slave_buffer[5] = (uint8_t)matrix[offset+2];
  219. */
  220. #else // USE_SERIAL
  221. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  222. serial_slave_buffer[i] = matrix[offset+i];
  223. }
  224. #endif
  225. }
  226. bool matrix_is_modified(void)
  227. {
  228. if (debouncing) return false;
  229. return true;
  230. }
  231. inline
  232. bool matrix_is_on(uint8_t row, uint8_t col)
  233. {
  234. return (matrix[row] & ((matrix_row_t)1<<col));
  235. }
  236. inline
  237. matrix_row_t matrix_get_row(uint8_t row)
  238. {
  239. return matrix[row];
  240. }
  241. void matrix_print(void)
  242. {
  243. print("\nr/c 0123456789ABCDEF\n");
  244. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  245. phex(row); print(": ");
  246. pbin_reverse16(matrix_get_row(row));
  247. print("\n");
  248. }
  249. }
  250. uint8_t matrix_key_count(void)
  251. {
  252. uint8_t count = 0;
  253. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  254. count += bitpop16(matrix[i]);
  255. }
  256. return count;
  257. }
  258. static void init_cols(void)
  259. {
  260. for(int x = 0; x < MATRIX_COLS; x++) {
  261. _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
  262. _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
  263. }
  264. }
  265. static matrix_row_t read_cols(void)
  266. {
  267. matrix_row_t result = 0;
  268. for(int x = 0; x < MATRIX_COLS; x++) {
  269. result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
  270. }
  271. return result;
  272. }
  273. static void unselect_rows(void)
  274. {
  275. for(int x = 0; x < ROWS_PER_HAND; x++) {
  276. _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
  277. _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
  278. }
  279. }
  280. static void select_row(uint8_t row)
  281. {
  282. _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
  283. _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
  284. }