matrix.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #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. //#include "pwm.c"
  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 uint8_t matrix_rows(void){
  37. return MATRIX_ROWS;
  38. }
  39. inline uint8_t matrix_cols(void){
  40. return MATRIX_COLS;
  41. }
  42. /* generic STM32F103C8T6 board */
  43. #ifdef BOARD_GENERIC_STM32_F103
  44. // This could be removed, only used now in matrix_init()
  45. #define LED_ON() do { palClearPad(GPIOA, 1) ;} while (0)
  46. #define LED_OFF() do { palSetPad(GPIOA, 1); } while (0)
  47. #endif
  48. __attribute__ ((weak))
  49. void matrix_init_kb(void) {
  50. matrix_init_user();
  51. }
  52. __attribute__ ((weak))
  53. void matrix_scan_kb(void) {
  54. matrix_scan_user();
  55. }
  56. __attribute__ ((weak))
  57. void matrix_init_user(void) {
  58. }
  59. __attribute__ ((weak))
  60. void matrix_scan_user(void) {
  61. }
  62. void matrix_init(void)
  63. {
  64. // initialize row and col
  65. unselect_rows();
  66. init_cols();
  67. // initialize matrix state: all keys off
  68. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  69. matrix[i] = 0;
  70. matrix_debouncing[i] = 0;
  71. }
  72. //debug
  73. debug_matrix = true;
  74. LED_ON();
  75. wait_ms(500);
  76. LED_OFF();
  77. matrix_init_quantum();
  78. }
  79. uint8_t matrix_scan(void){
  80. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  81. select_row(i);
  82. wait_us(30); // without this wait read unstable value.
  83. matrix_row_t cols = read_cols();
  84. if (matrix_debouncing[i] != cols) {
  85. matrix_debouncing[i] = cols;
  86. if (debouncing) {
  87. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  88. }
  89. debouncing = DEBOUNCE;
  90. }
  91. unselect_rows();
  92. }
  93. if (debouncing) {
  94. if (--debouncing) {
  95. wait_ms(1);
  96. } else {
  97. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  98. matrix[i] = matrix_debouncing[i];
  99. }
  100. }
  101. }
  102. matrix_scan_quantum();
  103. return 1;
  104. }
  105. inline bool matrix_is_on(uint8_t row, uint8_t col){
  106. return (matrix[row] & ((matrix_row_t)1<<col));
  107. }
  108. inline matrix_row_t matrix_get_row(uint8_t row){
  109. return matrix[row];
  110. }
  111. void matrix_print(void){
  112. print("\nr/c 0123456789ABCDEF\n");
  113. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  114. phex(row); print(": ");
  115. pbin_reverse16(matrix_get_row(row));
  116. print("\n");
  117. }
  118. }
  119. /* Column pin configuration
  120. */
  121. // Modified by Xydane
  122. static void init_cols(void){
  123. palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_PULLUP);
  124. palSetPadMode(GPIOA, 15, PAL_MODE_INPUT_PULLUP);
  125. palSetPadMode(GPIOA, 10, PAL_MODE_INPUT_PULLUP);
  126. palSetPadMode(GPIOA, 9, PAL_MODE_INPUT_PULLUP);
  127. palSetPadMode(GPIOA, 8, PAL_MODE_INPUT_PULLUP);
  128. palSetPadMode(GPIOB, 15, PAL_MODE_INPUT_PULLUP);
  129. palSetPadMode(GPIOB, 14, PAL_MODE_INPUT_PULLUP);
  130. palSetPadMode(GPIOB, 13, PAL_MODE_INPUT_PULLUP);
  131. palSetPadMode(GPIOB, 12, PAL_MODE_INPUT_PULLUP);
  132. palSetPadMode(GPIOB, 11, PAL_MODE_INPUT_PULLUP);
  133. palSetPadMode(GPIOB, 10, PAL_MODE_INPUT_PULLUP);
  134. palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_PULLUP);
  135. palSetPadMode(GPIOB, 0, PAL_MODE_INPUT_PULLUP);
  136. palSetPadMode(GPIOA, 7, PAL_MODE_INPUT_PULLUP);
  137. palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLUP);
  138. }
  139. /* Returns status of switches(1:on, 0:off) */
  140. // Modified by Xydane
  141. static matrix_row_t read_cols(void){
  142. return ((palReadPad(GPIOA, 5)==PAL_HIGH) ? 0 : (1<<0))
  143. | ((palReadPad(GPIOA, 15)==PAL_HIGH) ? 0 : (1<<1))
  144. | ((palReadPad(GPIOA, 10)==PAL_HIGH) ? 0 : (1<<2))
  145. | ((palReadPad(GPIOA, 9)==PAL_HIGH) ? 0 : (1<<3))
  146. | ((palReadPad(GPIOA, 8)==PAL_HIGH) ? 0 : (1<<4))
  147. | ((palReadPad(GPIOB, 15)==PAL_HIGH) ? 0 : (1<<5))
  148. | ((palReadPad(GPIOB, 14)==PAL_HIGH) ? 0 : (1<<6))
  149. | ((palReadPad(GPIOB, 13)==PAL_HIGH) ? 0 : (1<<7))
  150. | ((palReadPad(GPIOB, 12)==PAL_HIGH) ? 0 : (1<<8))
  151. | ((palReadPad(GPIOB, 11)==PAL_HIGH) ? 0 : (1<<9))
  152. | ((palReadPad(GPIOB, 10)==PAL_HIGH) ? 0 : (1<<10))
  153. | ((palReadPad(GPIOB, 1)==PAL_HIGH) ? 0 : (1<<11))
  154. | ((palReadPad(GPIOB, 0)==PAL_HIGH) ? 0 : (1<<12))
  155. | ((palReadPad(GPIOA, 7)==PAL_HIGH) ? 0 : (1<<13))
  156. | ((palReadPad(GPIOA, 6)==PAL_HIGH) ? 0 : (1<<14));
  157. }
  158. /* Row pin configuration
  159. */
  160. // Modified by Xydane
  161. static void unselect_rows(void){
  162. palSetPadMode(GPIOB, 9, PAL_MODE_INPUT);
  163. palSetPadMode(GPIOB, 8, PAL_MODE_INPUT);
  164. palSetPadMode(GPIOB, 7, PAL_MODE_INPUT);
  165. palSetPadMode(GPIOB, 6, PAL_MODE_INPUT);
  166. palSetPadMode(GPIOB, 5, PAL_MODE_INPUT);
  167. palSetPadMode(GPIOA, 4, PAL_MODE_INPUT);
  168. }
  169. // Modified by Xydane
  170. static void select_row(uint8_t row){
  171. (void)row;
  172. switch (row) {
  173. case 0:
  174. palSetPadMode(GPIOB, 9, PAL_MODE_OUTPUT_PUSHPULL);
  175. palClearPad(GPIOB, 9);
  176. break;
  177. case 1:
  178. palSetPadMode(GPIOB, 8, PAL_MODE_OUTPUT_PUSHPULL);
  179. palClearPad(GPIOB, 8);
  180. break;
  181. case 2:
  182. palSetPadMode(GPIOB, 7, PAL_MODE_OUTPUT_PUSHPULL);
  183. palClearPad(GPIOB, 7);
  184. break;
  185. case 3:
  186. palSetPadMode(GPIOB, 6, PAL_MODE_OUTPUT_PUSHPULL);
  187. palClearPad(GPIOB, 6);
  188. break;
  189. case 4:
  190. palSetPadMode(GPIOB, 5, PAL_MODE_OUTPUT_PUSHPULL);
  191. palClearPad(GPIOB, 5);
  192. break;
  193. case 5:
  194. palSetPadMode(GPIOA, 4, PAL_MODE_OUTPUT_PUSHPULL);
  195. palClearPad(GPIOA, 4);
  196. break;
  197. }
  198. }