aboutsummaryrefslogtreecommitdiff
path: root/quantum/split_common
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/split_common')
-rw-r--r--quantum/split_common/matrix.c41
-rw-r--r--quantum/split_common/split_util.c65
2 files changed, 69 insertions, 37 deletions
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c
index 95ee2433a..5bad9db08 100644
--- a/quantum/split_common/matrix.c
+++ b/quantum/split_common/matrix.c
@@ -61,17 +61,22 @@ static void init_pins(void) {
61} 61}
62 62
63static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 63static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
64 matrix_row_t last_row_value = current_matrix[current_row]; 64 // Start with a clear matrix row
65 current_matrix[current_row] = 0; 65 matrix_row_t current_row_value = 0;
66 66
67 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 67 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
68 pin_t pin = direct_pins[current_row][col_index]; 68 pin_t pin = direct_pins[current_row][col_index];
69 if (pin != NO_PIN) { 69 if (pin != NO_PIN) {
70 current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); 70 current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
71 } 71 }
72 } 72 }
73 73
74 return (last_row_value != current_matrix[current_row]); 74 // If the row has changed, store the row and return the changed flag.
75 if (current_matrix[current_row] != current_row_value) {
76 current_matrix[current_row] = current_row_value;
77 return true;
78 }
79 return false;
75} 80}
76 81
77#elif defined(DIODE_DIRECTION) 82#elif defined(DIODE_DIRECTION)
@@ -98,11 +103,8 @@ static void init_pins(void) {
98} 103}
99 104
100static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 105static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
101 // Store last value of row prior to reading 106 // Start with a clear matrix row
102 matrix_row_t last_row_value = current_matrix[current_row]; 107 matrix_row_t current_row_value = 0;
103
104 // Clear data in matrix row
105 current_matrix[current_row] = 0;
106 108
107 // Select row and wait for row selecton to stabilize 109 // Select row and wait for row selecton to stabilize
108 select_row(current_row); 110 select_row(current_row);
@@ -114,13 +116,18 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
114 uint8_t pin_state = readPin(col_pins[col_index]); 116 uint8_t pin_state = readPin(col_pins[col_index]);
115 117
116 // Populate the matrix row with the state of the col pin 118 // Populate the matrix row with the state of the col pin
117 current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); 119 current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
118 } 120 }
119 121
120 // Unselect row 122 // Unselect row
121 unselect_row(current_row); 123 unselect_row(current_row);
122 124
123 return (last_row_value != current_matrix[current_row]); 125 // If the row has changed, store the row and return the changed flag.
126 if (current_matrix[current_row] != current_row_value) {
127 current_matrix[current_row] = current_row_value;
128 return true;
129 }
130 return false;
124} 131}
125 132
126# elif (DIODE_DIRECTION == ROW2COL) 133# elif (DIODE_DIRECTION == ROW2COL)
@@ -155,20 +162,22 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
155 // For each row... 162 // For each row...
156 for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) { 163 for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
157 // Store last value of row prior to reading 164 // Store last value of row prior to reading
158 matrix_row_t last_row_value = current_matrix[row_index]; 165 matrix_row_t last_row_value = current_matrix[row_index];
166 matrix_row_t current_row_value = last_row_value;
159 167
160 // Check row pin state 168 // Check row pin state
161 if (readPin(row_pins[row_index]) == 0) { 169 if (readPin(row_pins[row_index]) == 0) {
162 // Pin LO, set col bit 170 // Pin LO, set col bit
163 current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); 171 current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
164 } else { 172 } else {
165 // Pin HI, clear col bit 173 // Pin HI, clear col bit
166 current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); 174 current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
167 } 175 }
168 176
169 // Determine if the matrix changed state 177 // Determine if the matrix changed state
170 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) { 178 if ((last_row_value != current_row_value)) {
171 matrix_changed = true; 179 matrix_changed |= true;
180 current_matrix[row_index] = current_row_value;
172 } 181 }
173 } 182 }
174 183
diff --git a/quantum/split_common/split_util.c b/quantum/split_common/split_util.c
index fb6a3b85a..dfd06f5f9 100644
--- a/quantum/split_common/split_util.c
+++ b/quantum/split_common/split_util.c
@@ -6,6 +6,14 @@
6#include "transport.h" 6#include "transport.h"
7#include "quantum.h" 7#include "quantum.h"
8 8
9#ifdef PROTOCOL_LUFA
10# include <LUFA/Drivers/USB/USB.h>
11#endif
12
13#ifdef PROTOCOL_VUSB
14# include "usbdrv.h"
15#endif
16
9#ifdef EE_HANDS 17#ifdef EE_HANDS
10# include "eeconfig.h" 18# include "eeconfig.h"
11#endif 19#endif
@@ -22,30 +30,54 @@
22# define SPLIT_USB_TIMEOUT_POLL 10 30# define SPLIT_USB_TIMEOUT_POLL 10
23#endif 31#endif
24 32
33#ifdef PROTOCOL_CHIBIOS
34# define SPLIT_USB_DETECT // Force this on for now
35#endif
36
25volatile bool isLeftHand = true; 37volatile bool isLeftHand = true;
26 38
27bool waitForUsb(void) { 39#if defined(SPLIT_USB_DETECT)
40# if defined(PROTOCOL_LUFA)
41static inline bool usbHasActiveConnection(void) { return USB_Device_IsAddressSet(); }
42static inline void usbDisable(void) { USB_Disable(); }
43# elif defined(PROTOCOL_CHIBIOS)
44static inline bool usbHasActiveConnection(void) { return usbGetDriverStateI(&USBD1) == USB_ACTIVE; }
45static inline void usbDisable(void) { usbStop(&USBD1); }
46# elif defined(PROTOCOL_VUSB)
47static inline bool usbHasActiveConnection(void) {
48 usbPoll();
49 return usbConfiguration;
50}
51static inline void usbDisable(void) { usbDeviceDisconnect(); }
52# else
53static inline bool usbHasActiveConnection(void) { return true; }
54static inline void usbDisable(void) {}
55# endif
56
57bool usbIsActive(void) {
28 for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL); i++) { 58 for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL); i++) {
29 // This will return true if a USB connection has been established 59 // This will return true if a USB connection has been established
30#if defined(__AVR__) 60 if (usbHasActiveConnection()) {
31 if (UDADDR & _BV(ADDEN)) {
32#else
33 if (usbGetDriverStateI(&USBD1) == USB_ACTIVE) {
34#endif
35 return true; 61 return true;
36 } 62 }
37 wait_ms(SPLIT_USB_TIMEOUT_POLL); 63 wait_ms(SPLIT_USB_TIMEOUT_POLL);
38 } 64 }
39 65
40 // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow 66 // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
41#if defined(__AVR__) 67 usbDisable();
42 (USBCON &= ~(_BV(USBE) | _BV(OTGPADE)));
43#else
44 usbStop(&USBD1);
45#endif
46 68
47 return false; 69 return false;
48} 70}
71#elif defined(PROTOCOL_LUFA)
72static inline bool usbIsActive(void) {
73 USB_OTGPAD_On(); // enables VBUS pad
74 wait_us(5);
75
76 return USB_VBUS_GetStatus(); // checks state of VBUS
77}
78#else
79static inline bool usbIsActive(void) { return true; }
80#endif
49 81
50__attribute__((weak)) bool is_keyboard_left(void) { 82__attribute__((weak)) bool is_keyboard_left(void) {
51#if defined(SPLIT_HAND_PIN) 83#if defined(SPLIT_HAND_PIN)
@@ -66,16 +98,7 @@ __attribute__((weak)) bool is_keyboard_master(void) {
66 98
67 // only check once, as this is called often 99 // only check once, as this is called often
68 if (usbstate == UNKNOWN) { 100 if (usbstate == UNKNOWN) {
69#if defined(SPLIT_USB_DETECT) || defined(PROTOCOL_CHIBIOS) 101 usbstate = usbIsActive() ? MASTER : SLAVE;
70 usbstate = waitForUsb() ? MASTER : SLAVE;
71#elif defined(__AVR__)
72 USBCON |= (1 << OTGPADE); // enables VBUS pad
73 wait_us(5);
74
75 usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS
76#else
77 usbstate = MASTER;
78#endif
79 } 102 }
80 103
81 return (usbstate == MASTER); 104 return (usbstate == MASTER);