summaryrefslogtreecommitdiff
path: root/keyboards/custommk/evo70_r2/matrix.c
blob: 99a23a4542e4f4aa6c938f6545dd293717a909c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2023 David Hoelscher (@customMK)
// SPDX-License-Identifier: GPL-2.0-or-later
#include "quantum.h"

// Pin definitions
static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;

void matrix_wait_for_pin(pin_t pin, uint8_t target_state) {
    rtcnt_t start = chSysGetRealtimeCounterX();
    rtcnt_t end   = start + 5000;
    while (chSysIsCounterWithinX(chSysGetRealtimeCounterX(), start, end)) {
        if (readPin(pin) == target_state) {
            break;
        }
    }
}

void matrix_wait_for_port(stm32_gpio_t *port, uint32_t target_bitmask) {
    rtcnt_t start = chSysGetRealtimeCounterX();
    rtcnt_t end   = start + 5000;
    while (chSysIsCounterWithinX(chSysGetRealtimeCounterX(), start, end)) {
        if ((palReadPort(port) & target_bitmask) == target_bitmask) {
            break;
        }
    }
}

void shift_pulse_clock(void) {
        writePinHigh(COL_SHIFT_CLK_PIN);
        matrix_wait_for_pin(COL_SHIFT_CLK_PIN, 1);
        writePinLow(COL_SHIFT_CLK_PIN);
}

void matrix_init_custom(void) {
    //set all row pins as input with pullups
    for (int i = 0; i < MATRIX_ROWS; ++i) {
        writePinHigh(row_pins[i]);
        setPinInputHigh(row_pins[i]);
    }

    //set all column pins high in ROW2COL matrix
    setPinOutput(COL_SHIFT_IN_PIN);
    setPinOutput(COL_SHIFT_CLK_PIN);
    writePinHigh(COL_SHIFT_IN_PIN);
    matrix_wait_for_pin(COL_SHIFT_IN_PIN, 1);

    for (int i = 0; i < MATRIX_COLS; ++i) {
        shift_pulse_clock();
    }

}

bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    static matrix_row_t temp_matrix[MATRIX_ROWS] = {0};

    writePinLow(COL_SHIFT_IN_PIN);
    matrix_wait_for_pin(COL_SHIFT_IN_PIN, 0);

    // Setup the output column pin
    shift_pulse_clock();

    writePinHigh(COL_SHIFT_IN_PIN);
    for (int current_col = 0; current_col < MATRIX_COLS; ++current_col) {

        // Read the column ports
        uint32_t gpio_a = palReadPort(GPIOA);
        uint32_t gpio_b = palReadPort(GPIOB);
        
        // row 0, pin A8
        int current_row = 0;
        if ((gpio_a & (1 << 8)) >> 8) {
            temp_matrix[current_row] &= ~(1ul << current_col);
        } else {
            temp_matrix[current_row] |= (1ul << current_col);
        }
        // row 1, pin A1
        current_row = 1;
        if ((gpio_a & (1 << 1)) >> 1) {
            temp_matrix[current_row] &= ~(1ul << current_col);
        } else {
            temp_matrix[current_row] |= (1ul << current_col);
        }
        // row 2, pin A2
        current_row = 2;
        if ((gpio_a & (1 << 2)) >> 2) {
            temp_matrix[current_row] &= ~(1ul << current_col);
        } else {
            temp_matrix[current_row] |= (1ul << current_col);
        }
        // row 3, pin B3
        current_row = 3;
        if ((gpio_b & (1 << 1)) >> 1) {
            temp_matrix[current_row] &= ~(1ul << current_col);
        } else {
            temp_matrix[current_row] |= (1ul << current_col);
        }
        // row 4, pin A7
        current_row = 4;
        if ((gpio_a & (1 << 7)) >> 7) {
            temp_matrix[current_row] &= ~(1ul << current_col);
        } else {
            temp_matrix[current_row] |= (1ul << current_col);
        }
        
        // Setup the output column pin
        shift_pulse_clock();
    
    }

    // Check if matrix has changed, return the last-read data
    bool changed = memcmp(current_matrix, temp_matrix, sizeof(temp_matrix)) != 0;
    if (changed) {
        memcpy(current_matrix, temp_matrix, sizeof(temp_matrix));
    }
    shift_pulse_clock();
    return changed;
}