aboutsummaryrefslogtreecommitdiff
path: root/keyboards/mschwingen/modelm/matrix.c
diff options
context:
space:
mode:
authorMichael Schwingen <spam-github@discworld.dascon.de>2020-08-23 01:02:16 +0200
committerGitHub <noreply@github.com>2020-08-22 16:02:16 -0700
commit42eeb315a5424fc576239b7e57061affc2ffa8ab (patch)
treeeb04ba06dfe91fea8c530c056e068e50bce2accf /keyboards/mschwingen/modelm/matrix.c
parentac3dfa742a92de541f86846b94269aab87d55d3c (diff)
downloadqmk_firmware-42eeb315a5424fc576239b7e57061affc2ffa8ab.tar.gz
qmk_firmware-42eeb315a5424fc576239b7e57061affc2ffa8ab.zip
[Keyboard] add support for ModelM USB board (#9846)
* add support for ModelM USB board * EMI improvement: remove unnecessary toggling of MOSI pin * address review comments * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update keyboards/mschwingen/modelm/config.h Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update keyboards/mschwingen/modelm/config.h Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: Drashna Jaelre <drashna@live.com> * Update keyboards/mschwingen/modelm/keymaps/default/keymap.c Co-authored-by: Drashna Jaelre <drashna@live.com> * update printf usage * add comment * EMI improvement: remove unnecessary toggling of MOSI signal * remove trailing space * use shorter macros as suggested in review by noroadsleft, re-format table to line up columns * Update keyboards/mschwingen/modelm/config.h Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/README.md Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/README.md Co-authored-by: Ryan <fauxpark@gmail.com> * Apply suggestions from code review use spi_read from core insteads of our own copy Co-authored-by: Ryan <fauxpark@gmail.com> * include spi_master.c to use spi_read() * Update keyboards/mschwingen/modelm/README.md Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Apply suggestions from code review: correct indenting in keymap Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Apply suggestions from code review use automatic variant defines from makefile instead of defining our own Co-authored-by: Drashna Jaelre <drashna@live.com> * Update keyboards/mschwingen/modelm/rules.mk: use QUANTUM_LIB_SRC for uart.c Co-authored-by: Drashna Jaelre <drashna@live.com> Co-authored-by: Michael Schwingen <michael@schwingen.org> Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Drashna Jaelre <drashna@live.com> Co-authored-by: Ryan <fauxpark@gmail.com>
Diffstat (limited to 'keyboards/mschwingen/modelm/matrix.c')
-rw-r--r--keyboards/mschwingen/modelm/matrix.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/keyboards/mschwingen/modelm/matrix.c b/keyboards/mschwingen/modelm/matrix.c
new file mode 100644
index 000000000..ef725a61e
--- /dev/null
+++ b/keyboards/mschwingen/modelm/matrix.c
@@ -0,0 +1,117 @@
1/*
2 * Copyright 2020 Michael Schwingen
3
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17#include <stdint.h>
18#include <stdbool.h>
19#include "util.h"
20#include "matrix.h"
21#include "debounce.h"
22#include "quantum.h"
23#include "spi_master.h"
24#include "print.h"
25#include "modelm.h"
26
27#define DEBUG 0
28
29#define SPI_TIMEOUT 100
30
31/* Keyboard Matrix Assignments */
32static uint16_t row_bits[MATRIX_ROWS] = {
33 0x4000, 0x8000, 0x2000, 0x1000, 0x0800, 0x0400, 0x0100, 0x0200,
34 0x0040, 0x0080, 0x0020, 0x0010, 0x0008, 0x0004, 0x0001, 0x0002};
35
36static const pin_t col_pins[MATRIX_COLS] = {D1, D4, D7, B4, F7, F6, F5, F4};
37
38static void select_col(uint8_t col) {
39 setPinOutput(col_pins[col]);
40 writePinLow(col_pins[col]);
41}
42
43static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
44
45static void unselect_cols(void) {
46 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
47 setPinInputHigh(col_pins[x]);
48 }
49}
50
51static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
52 uint16_t row_data;
53 bool matrix_changed = false;
54
55 // Select col and wait for col selecton to stabilize
56 select_col(current_col);
57 matrix_io_delay();
58
59 writePinLow(SR_LOAD_PIN);
60 writePinHigh(SR_LOAD_PIN);
61
62 row_data = spi_read() << 8;
63 row_data |= spi_read();
64
65#if DEBUG
66 phex(~row_data);
67 uprint(" ");
68#endif
69 // For each row...
70 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
71 // Store last value of row prior to reading
72 matrix_row_t last_row_value = current_matrix[row_index];
73 matrix_row_t current_row_value = last_row_value;
74
75 // Check row pin state
76 if ((row_data & row_bits[row_index]) == 0) {
77 // Pin LO, set col bit
78 current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
79 } else {
80 // Pin HI, clear col bit
81 current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
82 }
83
84 // Determine if the matrix changed state
85 if ((last_row_value != current_row_value)) {
86 matrix_changed = true;
87 current_matrix[row_index] = current_row_value;
88 }
89 }
90
91 // Unselect col
92 unselect_col(current_col);
93
94 return matrix_changed;
95}
96
97void matrix_init_custom(void) {
98 unselect_cols();
99
100 // set 4MHz SPI clock
101 SPSR = 0;
102 SPCR = _BV(SPE) | _BV(MSTR) | _BV(CPOL);
103}
104
105bool matrix_scan_custom(matrix_row_t current_matrix[]) {
106 bool changed = false;
107
108#if DEBUG
109 uprint("\r\nScan: ");
110#endif
111 // Set col, read rows
112 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
113 changed |= read_rows_on_col(current_matrix, current_col);
114 }
115 update_layer_leds();
116 return changed;
117}