Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. Copyright 2012 Jun Wako <[email protected]>
  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. #include <avr/io.h>
  20. #include <util/delay.h>
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #ifndef DEBOUNCE
  26. # define DEBOUNCE 5
  27. #endif
  28. static uint8_t debouncing = DEBOUNCE;
  29. /* matrix state(1:on, 0:off) */
  30. static matrix_row_t matrix[MATRIX_ROWS];
  31. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  32. static matrix_row_t read_cols(void);
  33. static void init_cols(void);
  34. static void unselect_rows(void);
  35. static void select_row(uint8_t row);
  36. inline
  37. uint8_t matrix_rows(void)
  38. {
  39. return MATRIX_ROWS;
  40. }
  41. inline
  42. uint8_t matrix_cols(void)
  43. {
  44. return MATRIX_COLS;
  45. }
  46. void matrix_init(void)
  47. {
  48. // disable JTAG
  49. MCUCR = (1<<JTD);
  50. MCUCR = (1<<JTD);
  51. // initialize row and col
  52. unselect_rows();
  53. init_cols();
  54. // initialize matrix state: all keys off
  55. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  56. matrix[i] = 0;
  57. matrix_debouncing[i] = 0;
  58. }
  59. }
  60. uint8_t matrix_scan(void)
  61. {
  62. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  63. select_row(i);
  64. #ifdef HYBRID_MATRIX
  65. init_cols();
  66. #endif
  67. _delay_us(30); // without this wait read unstable value.
  68. matrix_row_t cols = read_cols();
  69. if (matrix_debouncing[i] != cols) {
  70. matrix_debouncing[i] = cols;
  71. if (debouncing) {
  72. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  73. }
  74. debouncing = DEBOUNCE;
  75. }
  76. unselect_rows();
  77. }
  78. if (debouncing) {
  79. if (--debouncing) {
  80. _delay_ms(1);
  81. } else {
  82. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  83. matrix[i] = matrix_debouncing[i];
  84. }
  85. }
  86. }
  87. return 1;
  88. }
  89. bool matrix_is_modified(void)
  90. {
  91. if (debouncing) return false;
  92. return true;
  93. }
  94. inline
  95. bool matrix_is_on(uint8_t row, uint8_t col)
  96. {
  97. return (matrix[row] & ((matrix_row_t)1<<col));
  98. }
  99. inline
  100. matrix_row_t matrix_get_row(uint8_t row)
  101. {
  102. return matrix[row];
  103. }
  104. void matrix_print(void)
  105. {
  106. print("\nr/c 0123456789ABCDEF\n");
  107. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  108. phex(row); print(": ");
  109. pbin_reverse16(matrix_get_row(row));
  110. print("\n");
  111. }
  112. }
  113. uint8_t matrix_key_count(void)
  114. {
  115. uint8_t count = 0;
  116. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  117. count += bitpop16(matrix[i]);
  118. }
  119. return count;
  120. }
  121. /* Column pin configuration
  122. * pin: F1 F0 E6 D7 D6 D4 C7 C6 B6 B5 B4 B3 B1 B0 (Rev.A)
  123. * pin: F1 F0 E6 D7 D6 D4 C7 C6 B7 B6 B5 B4 B3 B1 (Rev.B)
  124. * pin: F1 F0 E6 D7 D6 D4 C7 C6 B7 B5 B4 B3 B1 B0 (Rev.CHN/CNY)
  125. */
  126. static void init_cols(void)
  127. {
  128. // Input with pull-up(DDR:0, PORT:1)
  129. DDRF &= ~(1<<PF1 | 1<<PF0);
  130. PORTF |= (1<<PF1 | 1<<PF0);
  131. DDRE &= ~(1<<PE6);
  132. PORTE |= (1<<PE6);
  133. DDRD &= ~(1<<PD7 | 1<<PD6 | 1<<PD4);
  134. PORTD |= (1<<PD7 | 1<<PD6 | 1<<PD4);
  135. DDRC &= ~(1<<PC7 | 1<<PC6);
  136. PORTC |= (1<<PC7 | 1<<PC6);
  137. #if defined(GH60_REV_CHN) || defined(GH60_REV_CNY)
  138. DDRB &= ~(1<<PB7 | 1<<PB5 | 1<<PB4 | 1<<PB3 | 1<<PB1 | 1<<PB0);
  139. PORTB |= (1<<PB7 | 1<<PB5 | 1<<PB4 | 1<<PB3 | 1<<PB1 | 1<<PB0);
  140. #else
  141. DDRB &= ~(1<<PB7 | 1<<PB6 | 1<<PB5 | 1<<PB4 | 1<<PB3 | 1<<PB1 | 1<<PB0);
  142. PORTB |= (1<<PB7 | 1<<PB6 | 1<<PB5 | 1<<PB4 | 1<<PB3 | 1<<PB1 | 1<<PB0);
  143. #endif
  144. }
  145. /* Column pin configuration
  146. * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
  147. * pin: F0 F1 E6 C7 C6 B6 D4 B1 B0 B5 B4 D7 D6 B3 (Rev.A)
  148. * pin: F0 F1 E6 C7 C6 B6 D4 B1 B7 B5 B4 D7 D6 B3 (Rev.B)
  149. * pin: F0 F1 E6 C7 C6 B7 D4 B1 B0 B5 B4 D7 D6 B3 (Rev.CHN)
  150. * pin: F0 F1 E6 C7 C6 B7 D4 B0 B1 B5 B4 D7 D6 B3 (Rev.CNY)
  151. */
  152. static matrix_row_t read_cols(void)
  153. {
  154. #if defined(GH60_REV_CHN)
  155. return (PINF&(1<<PF0) ? 0 : (1<<0)) |
  156. (PINF&(1<<PF1) ? 0 : (1<<1)) |
  157. (PINE&(1<<PE6) ? 0 : (1<<2)) |
  158. (PINC&(1<<PC7) ? 0 : (1<<3)) |
  159. (PINC&(1<<PC6) ? 0 : (1<<4)) |
  160. (PINB&(1<<PB7) ? 0 : (1<<5)) |
  161. (PIND&(1<<PD4) ? 0 : (1<<6)) |
  162. (PINB&(1<<PB1) ? 0 : (1<<7)) |
  163. (PINB&(1<<PB0) ? 0 : (1<<8)) |
  164. (PINB&(1<<PB5) ? 0 : (1<<9)) |
  165. (PINB&(1<<PB4) ? 0 : (1<<10)) |
  166. (PIND&(1<<PD7) ? 0 : (1<<11)) |
  167. (PIND&(1<<PD6) ? 0 : (1<<12)) |
  168. (PINB&(1<<PB3) ? 0 : (1<<13));
  169. #elif defined(GH60_REV_CNY)
  170. return (PINF&(1<<PF0) ? 0 : (1<<0)) |
  171. (PINF&(1<<PF1) ? 0 : (1<<1)) |
  172. (PINE&(1<<PE6) ? 0 : (1<<2)) |
  173. (PINC&(1<<PC7) ? 0 : (1<<3)) |
  174. (PINC&(1<<PC6) ? 0 : (1<<4)) |
  175. (PINB&(1<<PB7) ? 0 : (1<<5)) |
  176. (PIND&(1<<PD4) ? 0 : (1<<6)) |
  177. (PINB&(1<<PB0) ? 0 : (1<<7)) |
  178. (PINB&(1<<PB1) ? 0 : (1<<8)) |
  179. (PINB&(1<<PB5) ? 0 : (1<<9)) |
  180. (PINB&(1<<PB4) ? 0 : (1<<10)) |
  181. (PIND&(1<<PD7) ? 0 : (1<<11)) |
  182. (PIND&(1<<PD6) ? 0 : (1<<12)) |
  183. (PINB&(1<<PB3) ? 0 : (1<<13));
  184. #else
  185. return (PINF&(1<<0) ? 0 : (1<<0)) |
  186. (PINF&(1<<1) ? 0 : (1<<1)) |
  187. (PINE&(1<<6) ? 0 : (1<<2)) |
  188. (PINC&(1<<7) ? 0 : (1<<3)) |
  189. (PINC&(1<<6) ? 0 : (1<<4)) |
  190. (PINB&(1<<6) ? 0 : (1<<5)) |
  191. (PIND&(1<<4) ? 0 : (1<<6)) |
  192. (PINB&(1<<1) ? 0 : (1<<7)) |
  193. ((PINB&(1<<0) && PINB&(1<<7)) ? 0 : (1<<8)) | // Rev.A and B
  194. (PINB&(1<<5) ? 0 : (1<<9)) |
  195. (PINB&(1<<4) ? 0 : (1<<10)) |
  196. (PIND&(1<<7) ? 0 : (1<<11)) |
  197. (PIND&(1<<6) ? 0 : (1<<12)) |
  198. (PINB&(1<<3) ? 0 : (1<<13));
  199. #endif
  200. }
  201. /* Row pin configuration
  202. * row: 0 1 2 3 4
  203. * pin: D0 D1 D2 D3 D5
  204. */
  205. static void unselect_rows(void)
  206. {
  207. // Hi-Z(DDR:0, PORT:0) to unselect
  208. DDRD &= ~0b00101111;
  209. PORTD &= ~0b00101111;
  210. }
  211. static void select_row(uint8_t row)
  212. {
  213. // Output low(DDR:1, PORT:0) to select
  214. switch (row) {
  215. case 0:
  216. DDRD |= (1<<0);
  217. PORTD &= ~(1<<0);
  218. break;
  219. case 1:
  220. DDRD |= (1<<1);
  221. PORTD &= ~(1<<1);
  222. break;
  223. case 2:
  224. DDRD |= (1<<2);
  225. PORTD &= ~(1<<2);
  226. break;
  227. case 3:
  228. DDRD |= (1<<3);
  229. PORTD &= ~(1<<3);
  230. break;
  231. case 4:
  232. DDRD |= (1<<5);
  233. PORTD &= ~(1<<5);
  234. break;
  235. }
  236. }