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.

matrix.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "ch.h"
  15. #include "hal.h"
  16. /*
  17. * scan matrix
  18. */
  19. #include "print.h"
  20. #include "debug.h"
  21. #include "util.h"
  22. #include "matrix.h"
  23. #include "wait.h"
  24. #ifndef DEBOUNCE
  25. # define DEBOUNCE 5
  26. #endif
  27. static uint8_t debouncing = DEBOUNCE;
  28. /* matrix state(1:on, 0:off) */
  29. static matrix_row_t matrix[MATRIX_ROWS];
  30. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  31. static matrix_row_t read_cols(void);
  32. static void init_cols(void);
  33. static void unselect_rows(void);
  34. static void select_row(uint8_t row);
  35. inline
  36. uint8_t matrix_rows(void)
  37. {
  38. return MATRIX_ROWS;
  39. }
  40. inline
  41. uint8_t matrix_cols(void)
  42. {
  43. return MATRIX_COLS;
  44. }
  45. /* generic STM32F103C8T6 board */
  46. #ifdef BOARD_GENERIC_STM32_F103
  47. #define LED_ON() do { palClearPad(GPIOC, GPIOC_LED) ;} while (0)
  48. #define LED_OFF() do { palSetPad(GPIOC, GPIOC_LED); } while (0)
  49. #define LED_TGL() do { palTogglePad(GPIOC, GPIOC_LED); } while (0)
  50. #endif
  51. /* Maple Mini */
  52. #ifdef BOARD_MAPLEMINI_STM32_F103
  53. #define LED_ON() do { palSetPad(GPIOB, 1) ;} while (0)
  54. #define LED_OFF() do { palClearPad(GPIOB, 1); } while (0)
  55. #define LED_TGL() do { palTogglePad(GPIOB, 1); } while (0)
  56. #endif
  57. void matrix_init(void)
  58. {
  59. // initialize row and col
  60. unselect_rows();
  61. init_cols();
  62. // initialize matrix state: all keys off
  63. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  64. matrix[i] = 0;
  65. matrix_debouncing[i] = 0;
  66. }
  67. //debug
  68. debug_matrix = true;
  69. LED_ON();
  70. wait_ms(500);
  71. LED_OFF();
  72. }
  73. uint8_t matrix_scan(void)
  74. {
  75. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  76. select_row(i);
  77. wait_us(30); // without this wait read unstable value.
  78. matrix_row_t cols = read_cols();
  79. if (matrix_debouncing[i] != cols) {
  80. matrix_debouncing[i] = cols;
  81. if (debouncing) {
  82. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  83. }
  84. debouncing = DEBOUNCE;
  85. }
  86. unselect_rows();
  87. }
  88. if (debouncing) {
  89. if (--debouncing) {
  90. wait_ms(1);
  91. } else {
  92. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  93. matrix[i] = matrix_debouncing[i];
  94. }
  95. }
  96. }
  97. return 1;
  98. }
  99. inline
  100. bool matrix_is_on(uint8_t row, uint8_t col)
  101. {
  102. return (matrix[row] & ((matrix_row_t)1<<col));
  103. }
  104. inline
  105. matrix_row_t matrix_get_row(uint8_t row)
  106. {
  107. return matrix[row];
  108. }
  109. void matrix_print(void)
  110. {
  111. print("\nr/c 0123456789ABCDEF\n");
  112. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  113. phex(row); print(": ");
  114. pbin_reverse16(matrix_get_row(row));
  115. print("\n");
  116. }
  117. }
  118. /* Column pin configuration
  119. */
  120. static void init_cols(void)
  121. {
  122. palSetPadMode(GPIOC, 14, PAL_MODE_INPUT_PULLUP);
  123. palSetPadMode(GPIOC, 15, PAL_MODE_INPUT_PULLUP);
  124. palSetPadMode(GPIOA, 0, PAL_MODE_INPUT_PULLUP);
  125. palSetPadMode(GPIOA, 1, PAL_MODE_INPUT_PULLUP);
  126. palSetPadMode(GPIOA, 2, PAL_MODE_INPUT_PULLUP);
  127. palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_PULLUP);
  128. palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_PULLUP);
  129. palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_PULLUP);
  130. palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLUP);
  131. palSetPadMode(GPIOA, 7, PAL_MODE_INPUT_PULLUP);
  132. palSetPadMode(GPIOB, 0, PAL_MODE_INPUT_PULLUP);
  133. palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_PULLUP);
  134. }
  135. /* Returns status of switches(1:on, 0:off) */
  136. static matrix_row_t read_cols(void)
  137. {
  138. return ((palReadPad(GPIOC, 14)==PAL_HIGH) ? 0 : (1<<0))
  139. | ((palReadPad(GPIOC, 15)==PAL_HIGH) ? 0 : (1<<1))
  140. | ((palReadPad(GPIOA, 0)==PAL_HIGH) ? 0 : (1<<2))
  141. | ((palReadPad(GPIOA, 1)==PAL_HIGH) ? 0 : (1<<3))
  142. | ((palReadPad(GPIOA, 2)==PAL_HIGH) ? 0 : (1<<4))
  143. | ((palReadPad(GPIOA, 3)==PAL_HIGH) ? 0 : (1<<5))
  144. | ((palReadPad(GPIOA, 4)==PAL_HIGH) ? 0 : (1<<6))
  145. | ((palReadPad(GPIOA, 5)==PAL_HIGH) ? 0 : (1<<7))
  146. | ((palReadPad(GPIOA, 6)==PAL_HIGH) ? 0 : (1<<8))
  147. | ((palReadPad(GPIOA, 7)==PAL_HIGH) ? 0 : (1<<9))
  148. | ((palReadPad(GPIOB, 0)==PAL_HIGH) ? 0 : (1<<10))
  149. | ((palReadPad(GPIOB, 1)==PAL_HIGH) ? 0 : (1<<11));
  150. }
  151. /* Row pin configuration
  152. */
  153. static void unselect_rows(void)
  154. {
  155. palSetPadMode(GPIOB, 13, PAL_MODE_INPUT); // hi-Z
  156. palSetPadMode(GPIOB, 14, PAL_MODE_INPUT); // hi-Z
  157. palSetPadMode(GPIOB, 15, PAL_MODE_INPUT); // hi-Z
  158. palSetPadMode(GPIOA, 8, PAL_MODE_INPUT); // hi-Z
  159. }
  160. static void select_row(uint8_t row)
  161. {
  162. (void)row;
  163. // Output low to select
  164. switch (row) {
  165. case 0:
  166. palSetPadMode(GPIOB, 13, PAL_MODE_OUTPUT_PUSHPULL);
  167. palClearPad(GPIOB, 13);
  168. break;
  169. case 1:
  170. palSetPadMode(GPIOB, 14, PAL_MODE_OUTPUT_PUSHPULL);
  171. palClearPad(GPIOB, 14);
  172. break;
  173. case 2:
  174. palSetPadMode(GPIOB, 15, PAL_MODE_OUTPUT_PUSHPULL);
  175. palClearPad(GPIOB, 15);
  176. break;
  177. case 3:
  178. palSetPadMode(GPIOA, 8, PAL_MODE_OUTPUT_PUSHPULL);
  179. palClearPad(GPIOA, 8);
  180. break;
  181. }
  182. }