diff options
| author | Joel Challis <git@zvecr.com> | 2021-08-05 00:59:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-05 00:59:50 +0100 |
| commit | bdaea259afa98503e6ba30ad3bd2c95872a289bd (patch) | |
| tree | c0974d2ffba94264202e67232007ff13d6dac047 /keyboards/rgbkb/mun/matrix.c | |
| parent | cb4d91f85fa254ab1ce0d068af335735a6265925 (diff) | |
| download | qmk_firmware-bdaea259afa98503e6ba30ad3bd2c95872a289bd.tar.gz qmk_firmware-bdaea259afa98503e6ba30ad3bd2c95872a289bd.zip | |
Tidy up rgbkb/mun (#13801)
* Tidy up rgbkb/mun
Diffstat (limited to 'keyboards/rgbkb/mun/matrix.c')
| -rw-r--r-- | keyboards/rgbkb/mun/matrix.c | 190 |
1 files changed, 48 insertions, 142 deletions
diff --git a/keyboards/rgbkb/mun/matrix.c b/keyboards/rgbkb/mun/matrix.c index 497ab041f..b859847f1 100644 --- a/keyboards/rgbkb/mun/matrix.c +++ b/keyboards/rgbkb/mun/matrix.c | |||
| @@ -7,150 +7,56 @@ | |||
| 7 | * ---------------------------------------------------------------------------- | 7 | * ---------------------------------------------------------------------------- |
| 8 | */ | 8 | */ |
| 9 | 9 | ||
| 10 | #include <stdint.h> | ||
| 11 | #include <stdbool.h> | ||
| 12 | #include <string.h> | ||
| 13 | #include "util.h" | ||
| 14 | #include "matrix.h" | 10 | #include "matrix.h" |
| 15 | #include "debounce.h" | 11 | #include "atomic_util.h" |
| 16 | #include "quantum.h" | 12 | #include "gpio.h" |
| 17 | #include "split_util.h" | 13 | |
| 18 | #include "config.h" | 14 | static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; |
| 19 | #include "transactions.h" | 15 | |
| 20 | 16 | void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { | |
| 21 | #define ERROR_DISCONNECT_COUNT 5 | 17 | /* Drive row pin low. */ |
| 22 | #define ROWS_PER_HAND (MATRIX_ROWS / 2) | 18 | ATOMIC_BLOCK_FORCEON { writePinLow(row_pins[current_row]); } |
| 23 | 19 | matrix_output_select_delay(); | |
| 24 | static const pin_t row_pins[ROWS_PER_HAND] = MATRIX_ROW_PINS; | 20 | |
| 25 | static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | 21 | /* Read all columns in one go, aka port scanning. */ |
| 26 | 22 | uint16_t porta = palReadPort(GPIOA); | |
| 27 | /* matrix state(1:on, 0:off) */ | 23 | uint16_t portb = palReadPort(GPIOB); |
| 28 | extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values | 24 | |
| 29 | extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values | 25 | /* Order of pins on the mun is: A0, B11, B0, B10, B12, B2, A8 |
| 30 | 26 | Pin is active low, therefore we have to invert the result. */ | |
| 31 | // row offsets for each hand | 27 | matrix_row_t cols = ~(((porta & (0x1 << 0)) >> 0) // A0 (0) |
| 32 | uint8_t thisHand, thatHand; | 28 | | ((portb & (0x1 << 11)) >> 10) // B11 (1) |
| 33 | 29 | | ((portb & (0x1 << 0)) << 2) // B0 (2) | |
| 34 | // user-defined overridable functions | 30 | | ((portb & (0x1 << 10)) >> 7) // B10 (3) |
| 35 | __attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); } | 31 | | ((portb & (0x1 << 12)) >> 8) // B12 (4) |
| 36 | __attribute__((weak)) void matrix_slave_scan_user(void) {} | 32 | | ((portb & (0x1 << 2)) << 3) // B2 (5) |
| 37 | 33 | | ((porta & (0x1 << 8)) >> 2)); // A8 (6) | |
| 38 | static void init_pins(void) { | 34 | |
| 39 | for (size_t i = 0; i < MATRIX_COLS; i++) { | 35 | /* Reverse the order of columns for left hand as the board is flipped. */ |
| 40 | setPinInputHigh(col_pins[i]); | 36 | // if (isLeftHand) { |
| 41 | } | 37 | // #if defined(__arm__) |
| 42 | for (size_t i = 0; i < ROWS_PER_HAND; i++) { | 38 | // /* rbit assembly reverses bit order of 32bit registers. */ |
| 43 | setPinOutput(row_pins[i]); | 39 | // uint32_t temp = cols; |
| 44 | writePinHigh(row_pins[i]); | 40 | // __asm__("rbit %0, %1" : "=r"(temp) : "r"(temp)); |
| 45 | } | 41 | // cols = temp >> 24; |
| 46 | } | 42 | // #else |
| 47 | 43 | // /* RISC-V bit manipulation extension not present. Use bit-hack. | |
| 48 | void matrix_init(void) { | 44 | // https://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits */ |
| 49 | split_pre_init(); | 45 | // cols = (matrix_row_t)(((cols * 0x0802LU & 0x22110LU) | (cols * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16); |
| 50 | 46 | // #endif | |
| 51 | thisHand = isLeftHand ? 0 : (ROWS_PER_HAND); | 47 | // } |
| 52 | thatHand = ROWS_PER_HAND - thisHand; | 48 | |
| 53 | 49 | current_matrix[current_row] = cols; | |
| 54 | // initialize key pins | 50 | |
| 55 | init_pins(); | 51 | /* Drive row pin high again. */ |
| 56 | 52 | ATOMIC_BLOCK_FORCEON { writePinHigh(row_pins[current_row]); } | |
| 57 | // initialize matrix state: all keys off | 53 | matrix_output_unselect_delay(current_row, row_pins[current_row] != 0); |
| 58 | memset(raw_matrix, 0, sizeof(raw_matrix)); | ||
| 59 | memset(matrix, 0, sizeof(matrix)); | ||
| 60 | |||
| 61 | debounce_init(ROWS_PER_HAND); | ||
| 62 | |||
| 63 | matrix_init_quantum(); | ||
| 64 | |||
| 65 | split_post_init(); | ||
| 66 | } | ||
| 67 | |||
| 68 | bool matrix_post_scan(void) { | ||
| 69 | bool changed = false; | ||
| 70 | if (is_keyboard_master()) { | ||
| 71 | static uint8_t error_count; | ||
| 72 | |||
| 73 | matrix_row_t slave_matrix[ROWS_PER_HAND] = {0}; | ||
| 74 | if (!transport_master(matrix + thisHand, slave_matrix)) { | ||
| 75 | error_count++; | ||
| 76 | |||
| 77 | if (error_count > ERROR_DISCONNECT_COUNT) { | ||
| 78 | // reset other half if disconnected | ||
| 79 | memset(&matrix[thatHand], 0, sizeof(slave_matrix)); | ||
| 80 | memset(slave_matrix, 0, sizeof(slave_matrix)); | ||
| 81 | |||
| 82 | changed = true; | ||
| 83 | } | ||
| 84 | } else { | ||
| 85 | error_count = 0; | ||
| 86 | |||
| 87 | if (memcmp(&matrix[thatHand], slave_matrix, sizeof(slave_matrix)) != 0) { | ||
| 88 | memcpy(&matrix[thatHand], slave_matrix, sizeof(slave_matrix)); | ||
| 89 | changed = true; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | matrix_scan_quantum(); | ||
| 94 | } else { | ||
| 95 | transport_slave(matrix + thatHand, matrix + thisHand); | ||
| 96 | |||
| 97 | matrix_slave_scan_kb(); | ||
| 98 | } | ||
| 99 | |||
| 100 | return changed; | ||
| 101 | } | 54 | } |
| 102 | 55 | ||
| 103 | uint8_t matrix_scan(void) { | 56 | #if defined(BUSY_WAIT) |
| 104 | bool local_changed = false; | 57 | void matrix_output_unselect_delay(uint8_t line, bool key_pressed) { |
| 105 | matrix_row_t current_matrix[ROWS_PER_HAND]; | 58 | for (int32_t i = 0; i < BUSY_WAIT_INSTRUCTIONS; i++) { |
| 106 | 59 | __asm__ volatile("nop" ::: "memory"); | |
| 107 | for (size_t row_idx = 0; row_idx < ROWS_PER_HAND; row_idx++) { | ||
| 108 | /* Drive row pin low. */ | ||
| 109 | ATOMIC_BLOCK_FORCEON { writePinLow(row_pins[row_idx]); } | ||
| 110 | matrix_output_select_delay(); | ||
| 111 | |||
| 112 | /* Read all columns in one go, aka port scanning. */ | ||
| 113 | uint16_t porta = palReadPort(GPIOA); | ||
| 114 | uint16_t portb = palReadPort(GPIOB); | ||
| 115 | |||
| 116 | /* Order of pins on the mun is: A0, B11, B0, B10, B12, B2, A8 | ||
| 117 | Pin is active low, therefore we have to invert the result. */ | ||
| 118 | matrix_row_t cols = ~(((porta & (0x1 << 0)) >> 0) // A0 (0) | ||
| 119 | | ((portb & (0x1 << 11)) >> 10) // B11 (1) | ||
| 120 | | ((portb & (0x1 << 0)) << 2) // B0 (2) | ||
| 121 | | ((portb & (0x1 << 10)) >> 7) // B10 (3) | ||
| 122 | | ((portb & (0x1 << 12)) >> 8) // B12 (4) | ||
| 123 | | ((portb & (0x1 << 2)) << 3) // B2 (5) | ||
| 124 | | ((porta & (0x1 << 8)) >> 2)); // A8 (6) | ||
| 125 | |||
| 126 | /* Reverse the order of columns for left hand as the board is flipped. */ | ||
| 127 | // if (isLeftHand) { | ||
| 128 | // #if defined(__arm__) | ||
| 129 | // /* rbit assembly reverses bit order of 32bit registers. */ | ||
| 130 | // uint32_t temp = cols; | ||
| 131 | // __asm__("rbit %0, %1" : "=r"(temp) : "r"(temp)); | ||
| 132 | // cols = temp >> 24; | ||
| 133 | // #else | ||
| 134 | // /* RISC-V bit manipulation extension not present. Use bit-hack. | ||
| 135 | // https://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits */ | ||
| 136 | // cols = (matrix_row_t)(((cols * 0x0802LU & 0x22110LU) | (cols * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16); | ||
| 137 | // #endif | ||
| 138 | // } | ||
| 139 | |||
| 140 | current_matrix[row_idx] = cols; | ||
| 141 | |||
| 142 | /* Drive row pin high again. */ | ||
| 143 | ATOMIC_BLOCK_FORCEON { writePinHigh(row_pins[row_idx]); } | ||
| 144 | matrix_output_unselect_delay(row_idx, row_pins[row_idx] != 0); | ||
| 145 | } | 60 | } |
| 146 | |||
| 147 | if (memcmp(raw_matrix, current_matrix, sizeof(current_matrix)) != 0) { | ||
| 148 | memcpy(raw_matrix, current_matrix, sizeof(current_matrix)); | ||
| 149 | local_changed = true; | ||
| 150 | } | ||
| 151 | |||
| 152 | debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, local_changed); | ||
| 153 | |||
| 154 | bool remote_changed = matrix_post_scan(); | ||
| 155 | return (uint8_t)(local_changed || remote_changed); | ||
| 156 | } | 61 | } |
| 62 | #endif | ||
