diff options
Diffstat (limited to 'keyboards/matrix/m20add/matrix.c')
| -rw-r--r-- | keyboards/matrix/m20add/matrix.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/keyboards/matrix/m20add/matrix.c b/keyboards/matrix/m20add/matrix.c new file mode 100644 index 000000000..b17643fea --- /dev/null +++ b/keyboards/matrix/m20add/matrix.c | |||
| @@ -0,0 +1,93 @@ | |||
| 1 | /** | ||
| 2 | * matrix.c | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include <stdint.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | #include <string.h> | ||
| 8 | #include "quantum.h" | ||
| 9 | #include "matrix.h" | ||
| 10 | #include "tca6424.h" | ||
| 11 | #include "m20add.h" | ||
| 12 | |||
| 13 | static const uint16_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
| 14 | |||
| 15 | void matrix_init_custom(void) | ||
| 16 | { | ||
| 17 | tca6424_init(); | ||
| 18 | // set port0 | ||
| 19 | tca6424_write_config(TCA6424_PORT0, 0); | ||
| 20 | // set port1 | ||
| 21 | tca6424_write_config(TCA6424_PORT1, 0); | ||
| 22 | // set port2 | ||
| 23 | tca6424_write_config(TCA6424_PORT2, 0xF5); | ||
| 24 | |||
| 25 | // clear output | ||
| 26 | tca6424_write_port(TCA6424_PORT0, 0); | ||
| 27 | tca6424_write_port(TCA6424_PORT1, 0); | ||
| 28 | tca6424_write_port(TCA6424_PORT2, 0); | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | static uint8_t row_mask[] = {ROW1_MASK,ROW2_MASK,ROW3_MASK,ROW4_MASK,ROW5_MASK,ROW6_MASK}; | ||
| 33 | static uint8_t col_mask[] = {COL1_MASK, COL2_MASK, COL3_MASK, COL4_MASK, COL5_MASK, COL6_MASK, COL7_MASK, COL8_MASK, COL9_MASK, COL10_MASK, COL11_MASK, COL12_MASK, COL13_MASK, COL14_MASK, COL15_MASK, COL16_MASK}; | ||
| 34 | |||
| 35 | bool matrix_scan_custom(matrix_row_t current_matrix[]) | ||
| 36 | { | ||
| 37 | bool changed = false; | ||
| 38 | uint8_t p0_data = tca6424_read_port(TCA6424_PORT0); | ||
| 39 | |||
| 40 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
| 41 | // Select col and wait for col selecton to stabilize | ||
| 42 | switch(col) { | ||
| 43 | case 0: | ||
| 44 | set_pin(col_pins[col]); | ||
| 45 | break; | ||
| 46 | case 1 ... 8: | ||
| 47 | tca6424_write_port(TCA6424_PORT1, col_mask[col]); | ||
| 48 | break; | ||
| 49 | default: | ||
| 50 | tca6424_write_port(TCA6424_PORT0, col_mask[col]|(p0_data&0x01)); | ||
| 51 | break; | ||
| 52 | } | ||
| 53 | matrix_io_delay(); | ||
| 54 | |||
| 55 | // read row port for all rows | ||
| 56 | uint8_t row_value = tca6424_read_port(ROW_PORT); | ||
| 57 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 58 | uint8_t tmp = row; | ||
| 59 | // Store last value of row prior to reading | ||
| 60 | matrix_row_t last_row_value = current_matrix[tmp]; | ||
| 61 | |||
| 62 | // Check row pin state | ||
| 63 | if (row_value & row_mask[row]) { | ||
| 64 | // Pin HI, set col bit | ||
| 65 | current_matrix[tmp] |= (1 << col); | ||
| 66 | } else { | ||
| 67 | // Pin LOW, clear col bit | ||
| 68 | current_matrix[tmp] &= ~(1 << col); | ||
| 69 | } | ||
| 70 | |||
| 71 | // Determine if the matrix changed state | ||
| 72 | if ((last_row_value != current_matrix[tmp]) && !(changed)) { | ||
| 73 | changed = true; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | // Unselect col | ||
| 77 | switch(col) { | ||
| 78 | case 0: | ||
| 79 | clear_pin(col_pins[col]); | ||
| 80 | break; | ||
| 81 | case 8: | ||
| 82 | tca6424_write_port(TCA6424_PORT1, 0); | ||
| 83 | break; | ||
| 84 | case 15: | ||
| 85 | tca6424_write_port(TCA6424_PORT0, p0_data&0x01); | ||
| 86 | break; | ||
| 87 | default: | ||
| 88 | break; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | return changed; | ||
| 93 | } | ||
