matrix.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. Copyright 2018 Jack Humbert <jack.humb@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 <stdint.h>
  15. #include <stdbool.h>
  16. #include <string.h>
  17. #include "hal.h"
  18. #include "timer.h"
  19. #include "wait.h"
  20. #include "printf.h"
  21. #include "backlight.h"
  22. #include "matrix.h"
  23. #include "action.h"
  24. #include "keycode.h"
  25. #include <string.h>
  26. #include "moonlander.h"
  27. #include "i2c_master.h"
  28. /*
  29. #define MATRIX_ROW_PINS { B10, B11, B12, B13, B14, B15 } outputs
  30. #define MATRIX_COL_PINS { A0, A1, A2, A3, A4, A5, A6 } inputs
  31. */
  32. /* matrix state(1:on, 0:off) */
  33. static matrix_row_t matrix[MATRIX_ROWS];
  34. static matrix_row_t matrix_debouncing[6];
  35. static matrix_row_t matrix_debouncing_right[MATRIX_COLS];
  36. static bool debouncing = false;
  37. static uint16_t debouncing_time = 0;
  38. static bool debouncing_right = false;
  39. static uint16_t debouncing_time_right = 0;
  40. extern bool mcp23018_leds[3];
  41. __attribute__ ((weak))
  42. void matrix_init_user(void) {}
  43. __attribute__ ((weak))
  44. void matrix_scan_user(void) {}
  45. __attribute__ ((weak))
  46. void matrix_init_kb(void) {
  47. matrix_init_user();
  48. }
  49. __attribute__ ((weak))
  50. void matrix_scan_kb(void) {
  51. matrix_scan_user();
  52. }
  53. bool mcp23018_initd = false;
  54. uint8_t mcp23018_tx[3];
  55. uint8_t mcp23018_rx[1];
  56. void mcp23018_init(void) {
  57. i2c_init();
  58. i2c_start(MCP23018_DEFAULT_ADDRESS << 1);
  59. // #define MCP23_ROW_PINS { GPB5, GBP4, GBP3, GBP2, GBP1, GBP0 } outputs
  60. // #define MCP23_COL_PINS { GPA0, GBA1, GBA2, GBA3, GBA4, GBA5, GBA6 } inputs
  61. mcp23018_tx[0] = 0x00; // IODIRA
  62. mcp23018_tx[1] = 0b00000000; // A is output
  63. mcp23018_tx[2] = 0b00111111; // B is inputs
  64. if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1,
  65. mcp23018_tx, 3, 100
  66. )) {
  67. printf("error hori\n");
  68. } else {
  69. mcp23018_tx[0] = 0x0C; // GPPUA
  70. mcp23018_tx[1] = 0b10000000; // A is not pulled-up
  71. mcp23018_tx[2] = 0b11111111; // B is pulled-up
  72. if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1,
  73. mcp23018_tx, 3, 100
  74. )) {
  75. printf("error hori\n");
  76. } else {
  77. mcp23018_initd = true;
  78. }
  79. }
  80. }
  81. void matrix_init(void) {
  82. printf("matrix init\n");
  83. //debug_matrix = true;
  84. // outputs
  85. palSetPadMode(GPIOB, 10, PAL_MODE_OUTPUT_PUSHPULL);
  86. palSetPadMode(GPIOB, 11, PAL_MODE_OUTPUT_PUSHPULL);
  87. palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL);
  88. palSetPadMode(GPIOB, 13, PAL_MODE_OUTPUT_PUSHPULL);
  89. palSetPadMode(GPIOB, 14, PAL_MODE_OUTPUT_PUSHPULL);
  90. palSetPadMode(GPIOB, 15, PAL_MODE_OUTPUT_PUSHPULL);
  91. // inputs
  92. palSetPadMode(GPIOA, 0, PAL_MODE_INPUT_PULLDOWN);
  93. palSetPadMode(GPIOA, 1, PAL_MODE_INPUT_PULLDOWN);
  94. palSetPadMode(GPIOA, 2, PAL_MODE_INPUT_PULLDOWN);
  95. palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_PULLDOWN);
  96. palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN);
  97. palSetPadMode(GPIOA, 7, PAL_MODE_INPUT_PULLDOWN);
  98. palSetPadMode(GPIOB, 0, PAL_MODE_INPUT_PULLDOWN);
  99. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  100. memset(matrix_debouncing, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  101. mcp23018_init();
  102. matrix_init_quantum();
  103. }
  104. uint8_t matrix_scan(void) {
  105. matrix_row_t data = 0;
  106. // actual matrix
  107. for (int row = 0; row < 6; row++) {
  108. // strobe row
  109. switch (row) {
  110. case 0: palSetPad(GPIOB, 10); break;
  111. case 1: palSetPad(GPIOB, 11); break;
  112. case 2: palSetPad(GPIOB, 12); break;
  113. case 3: palSetPad(GPIOB, 13); break;
  114. case 4: palSetPad(GPIOB, 14); break;
  115. case 5: palSetPad(GPIOB, 15); break;
  116. }
  117. // need wait to settle pin state
  118. wait_us(20);
  119. // read col data
  120. data = (
  121. (palReadPad(GPIOA, 0) << 0 ) |
  122. (palReadPad(GPIOA, 1) << 1 ) |
  123. (palReadPad(GPIOA, 2) << 2 ) |
  124. (palReadPad(GPIOA, 3) << 3 ) |
  125. (palReadPad(GPIOA, 6) << 4 ) |
  126. (palReadPad(GPIOA, 7) << 5 ) |
  127. (palReadPad(GPIOB, 0) << 6 )
  128. );
  129. // unstrobe row
  130. switch (row) {
  131. case 0: palClearPad(GPIOB, 10); break;
  132. case 1: palClearPad(GPIOB, 11); break;
  133. case 2: palClearPad(GPIOB, 12); break;
  134. case 3: palClearPad(GPIOB, 13); break;
  135. case 4: palClearPad(GPIOB, 14); break;
  136. case 5: palClearPad(GPIOB, 15); break;
  137. }
  138. if (matrix_debouncing[row] != data) {
  139. matrix_debouncing[row] = data;
  140. debouncing = true;
  141. debouncing_time = timer_read();
  142. }
  143. }
  144. for (int row = 0; row < 7; row++) {
  145. // right side
  146. if (!mcp23018_initd) {
  147. printf("trying to init right\n");
  148. mcp23018_init();
  149. }
  150. // #define MCP23_ROW_PINS { GPB5, GBP4, GBP3, GBP2, GBP1, GBP0 } outputs
  151. // #define MCP23_COL_PINS { GPA0, GBA1, GBA2, GBA3, GBA4, GBA5, GBA6 } inputs
  152. // select row
  153. mcp23018_tx[0] = 0x12; // GPIOA
  154. mcp23018_tx[1] = (0b01111111 & ~(1<<(row))) | ((uint8_t)!mcp23018_leds[2] << 7); // activate row
  155. mcp23018_tx[2] = ((uint8_t)!mcp23018_leds[1] << 6) | ((uint8_t)!mcp23018_leds[0] << 7); // activate row
  156. if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1,
  157. mcp23018_tx, 3, 100
  158. )) {
  159. printf("error hori\n");
  160. }
  161. // read col
  162. mcp23018_tx[0] = 0x13; // GPIOB
  163. if (MSG_OK != i2c_transmit_receive(MCP23018_DEFAULT_ADDRESS << 1,
  164. mcp23018_tx, 1,
  165. mcp23018_rx, 1
  166. )) {
  167. printf("error vert\n");
  168. }
  169. data = ~(mcp23018_rx[0] & 0b00111111);
  170. // data = 0x01;
  171. if (matrix_debouncing_right[row] != data) {
  172. matrix_debouncing_right[row] = data;
  173. debouncing_right = true;
  174. debouncing_time_right = timer_read();
  175. }
  176. }
  177. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCING_DELAY) {
  178. for (int row = 0; row < 6; row++) {
  179. matrix[row] = matrix_debouncing[row];
  180. }
  181. debouncing = false;
  182. }
  183. if (debouncing_right && timer_elapsed(debouncing_time_right) > DEBOUNCING_DELAY) {
  184. for (int row = 0; row < 6; row++) {
  185. matrix[11 - row] = 0;
  186. for (int col = 0; col < MATRIX_COLS; col++) {
  187. matrix[11 - row] |= ((matrix_debouncing_right[6 - col] & (1 << row) ? 1 : 0) << col);
  188. }
  189. }
  190. debouncing_right = false;
  191. }
  192. matrix_scan_quantum();
  193. return 1;
  194. }
  195. bool matrix_is_on(uint8_t row, uint8_t col) {
  196. return (matrix[row] & (1<<col));
  197. }
  198. matrix_row_t matrix_get_row(uint8_t row) {
  199. return matrix[row];
  200. }
  201. void matrix_print(void) {
  202. printf("\nr/c 01234567\n");
  203. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  204. printf("%X0: ", row);
  205. matrix_row_t data = matrix_get_row(row);
  206. for (int col = 0; col < MATRIX_COLS; col++) {
  207. if (data & (1<<col))
  208. printf("1");
  209. else
  210. printf("0");
  211. }
  212. printf("\n");
  213. }
  214. }