Keyboard firmwares for Atmel AVR and Cortex-M
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.

пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <avr/io.h>
  17. #include <util/delay.h>
  18. #include "print.h"
  19. #include "debug.h"
  20. #include "util.h"
  21. #include "matrix.h"
  22. /*
  23. * Happy Buckling Keyboard(IBM Model M mod)
  24. *
  25. * Pin usage:
  26. * COL: PD0-7
  27. * ROW: PB0-7, PF4-7
  28. */
  29. #ifndef DEBOUNCE
  30. # define DEBOUNCE 10
  31. #endif
  32. static uint8_t debouncing = DEBOUNCE;
  33. /* matrix state(1:on, 0:off) */
  34. static matrix_row_t matrix[MATRIX_ROWS];
  35. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  36. static matrix_row_t read_cols(void);
  37. static void unselect_rows(void);
  38. static void select_row(uint8_t row);
  39. void matrix_init(void)
  40. {
  41. // JTAG disable for PORT F. write JTD bit twice within four cycles.
  42. MCUCR |= (1<<JTD);
  43. MCUCR |= (1<<JTD);
  44. // initialize rows
  45. unselect_rows();
  46. // initialize columns to input with pull-up(DDR:0, PORT:1)
  47. DDRD = 0x00;
  48. PORTD = 0xFF;
  49. // initialize matrix state: all keys off
  50. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  51. matrix[i] = 0;
  52. matrix_debouncing[i] = 0;
  53. }
  54. }
  55. uint8_t matrix_scan(void)
  56. {
  57. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  58. select_row(i);
  59. _delay_us(30); // without this wait read unstable value.
  60. matrix_row_t cols = read_cols();
  61. if (matrix_debouncing[i] != cols) {
  62. matrix_debouncing[i] = cols;
  63. if (debouncing) {
  64. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  65. }
  66. debouncing = DEBOUNCE;
  67. }
  68. unselect_rows();
  69. }
  70. if (debouncing) {
  71. if (--debouncing) {
  72. _delay_ms(1);
  73. } else {
  74. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  75. matrix[i] = matrix_debouncing[i];
  76. }
  77. }
  78. }
  79. return 1;
  80. }
  81. inline
  82. matrix_row_t matrix_get_row(uint8_t row)
  83. {
  84. return matrix[row];
  85. }
  86. inline
  87. static matrix_row_t read_cols(void)
  88. {
  89. return ~PIND;
  90. }
  91. inline
  92. static void unselect_rows(void)
  93. {
  94. // Hi-Z(DDR:0, PORT:0) to unselect
  95. DDRB &= ~0b11111111;
  96. PORTB &= ~0b11111111;
  97. DDRF &= ~0b11110000;
  98. PORTF &= ~0b11110000;
  99. }
  100. inline
  101. static void select_row(uint8_t row)
  102. {
  103. // Output low(DDR:1, PORT:0) to select
  104. switch (row) {
  105. case 0:
  106. case 1:
  107. case 2:
  108. case 3:
  109. case 4:
  110. case 5:
  111. case 6:
  112. case 7:
  113. DDRB |= (1<<row);
  114. PORTB &= ~(1<<row);
  115. break;
  116. case 8:
  117. DDRF |= (1<<4);
  118. PORTF &= ~(1<<4);
  119. break;
  120. case 9:
  121. case 10:
  122. case 11:
  123. DDRF |= (1<<(row-4));
  124. PORTF &= ~(1<<(row-4));
  125. break;
  126. }
  127. }