13 * Column pins are input with internal pull-down. Row pins are output and strobe with high.
14 * Key is high or 1 when it turns on.
16 * col: { PTA15, PTC10, PTC11, PTC12, PTD2, PTB3, PTB4, PTB5, PTB6, PTB7, PTB8, PTB9, PTA2, PTA3 }
17 * row: { PTB11, PTB10, PTB2, PTB1, PTB0}
19 /* matrix state(1:on, 0:off) */
20 static matrix_row_t matrix[MATRIX_ROWS];
21 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
22 static bool debouncing = false;
23 static uint16_t debouncing_time = 0;
26 void matrix_init(void)
28 //debug_matrix = true;
30 palSetPadMode(GPIOA, 15, PAL_MODE_INPUT_PULLDOWN);
31 palSetPadMode(GPIOC, 10, PAL_MODE_INPUT_PULLDOWN);
32 palSetPadMode(GPIOC, 11, PAL_MODE_INPUT_PULLDOWN);
33 palSetPadMode(GPIOC, 12, PAL_MODE_INPUT_PULLDOWN);
34 palSetPadMode(GPIOD, 2, PAL_MODE_INPUT_PULLDOWN);
35 palSetPadMode(GPIOB, 3, PAL_MODE_INPUT_PULLDOWN);
36 palSetPadMode(GPIOB, 4, PAL_MODE_INPUT_PULLDOWN);
37 palSetPadMode(GPIOB, 5, PAL_MODE_INPUT_PULLDOWN);
38 palSetPadMode(GPIOB, 6, PAL_MODE_INPUT_PULLDOWN);
39 palSetPadMode(GPIOB, 7, PAL_MODE_INPUT_PULLDOWN);
40 palSetPadMode(GPIOB, 8, PAL_MODE_INPUT_PULLDOWN);
41 palSetPadMode(GPIOB, 9, PAL_MODE_INPUT_PULLDOWN);
42 palSetPadMode(GPIOA, 2, PAL_MODE_INPUT_PULLDOWN);
43 palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_PULLDOWN);
46 palSetPadMode(GPIOB, 11, PAL_MODE_OUTPUT_PUSHPULL);
47 palSetPadMode(GPIOB, 10, PAL_MODE_OUTPUT_PUSHPULL);
48 palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL);
49 palSetPadMode(GPIOB, 1, PAL_MODE_OUTPUT_PUSHPULL);
50 palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL);
52 memset(matrix, 0, MATRIX_ROWS);
53 memset(matrix_debouncing, 0, MATRIX_ROWS);
56 uint8_t matrix_scan(void)
58 for (int row = 0; row < MATRIX_ROWS; row++) {
59 matrix_row_t data = 0;
63 case 0: palSetPad(GPIOB, 11); break;
64 case 1: palSetPad(GPIOB, 10); break;
65 case 2: palSetPad(GPIOB, 2); break;
66 case 3: palSetPad(GPIOB, 1); break;
67 case 4: palSetPad(GPIOB, 0); break;
70 wait_us(20); // need wait to settle pin state
72 // read col data: { PTA15, PTC10, PTC11, PTC12, PTD2, PTB3, PTB4, PTB5, PTB6, PTB7, PTB8, PTB9, PTA2, PTA3 }
73 data = ((palReadPort(GPIOA) & 0x8000UL) >> 15) | // 0
74 ((palReadPort(GPIOC) & 0x1C00UL) >> 9) | // 1, 2, 3
75 ((palReadPort(GPIOD) & 0x0004UL) << 2) | // 4
76 ((palReadPort(GPIOB) & 0x03F8UL) << 2) | // 5, 6, 7, 8, 9, 10, 11
77 ((palReadPort(GPIOA) & 0x000CUL) << 10); // 12, 13
81 case 0: palClearPad(GPIOB, 11); break;
82 case 1: palClearPad(GPIOB, 10); break;
83 case 2: palClearPad(GPIOB, 2); break;
84 case 3: palClearPad(GPIOB, 1); break;
85 case 4: palClearPad(GPIOB, 0); break;
88 if (matrix_debouncing[row] != data) {
89 matrix_debouncing[row] = data;
91 debouncing_time = timer_read();
95 if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
96 for (int row = 0; row < MATRIX_ROWS; row++) {
97 matrix[row] = matrix_debouncing[row];
104 bool matrix_is_on(uint8_t row, uint8_t col)
106 return (matrix[row] & (1<<col));
109 matrix_row_t matrix_get_row(uint8_t row)
114 void matrix_print(void)
116 xprintf("\nr/c 01234567\n");
117 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
118 xprintf("%X0: ", row);
119 matrix_row_t data = matrix_get_row(row);
120 for (int col = 0; col < MATRIX_COLS; col++) {