diff options
| author | Drashna Jaelre <drashna@live.com> | 2019-01-28 00:00:42 -0800 |
|---|---|---|
| committer | skullydazed <skullydazed@users.noreply.github.com> | 2019-04-05 14:58:42 -0700 |
| commit | a615a2ae93107c5f4ddcb447a562af1a721a00d9 (patch) | |
| tree | 36dd6222dc8d702af3a6b3364493e37052bbc124 | |
| parent | ced2bbdac8018b08f229460b289ff5a7fce5dd43 (diff) | |
| download | qmk_firmware-a615a2ae93107c5f4ddcb447a562af1a721a00d9.tar.gz qmk_firmware-a615a2ae93107c5f4ddcb447a562af1a721a00d9.zip | |
Convert Orthodox to Split Common code
| -rw-r--r-- | keyboards/orthodox/config.h | 27 | ||||
| -rw-r--r-- | keyboards/orthodox/i2c.c | 162 | ||||
| -rw-r--r-- | keyboards/orthodox/i2c.h | 50 | ||||
| -rw-r--r-- | keyboards/orthodox/matrix.c | 342 | ||||
| -rw-r--r-- | keyboards/orthodox/rev1/config.h | 7 | ||||
| -rw-r--r-- | keyboards/orthodox/rev3/config.h | 7 | ||||
| -rw-r--r-- | keyboards/orthodox/rev3_teensy/config.h | 7 | ||||
| -rw-r--r-- | keyboards/orthodox/rules.mk | 13 | ||||
| -rw-r--r-- | keyboards/orthodox/serial.c | 230 | ||||
| -rw-r--r-- | keyboards/orthodox/serial.h | 27 | ||||
| -rw-r--r-- | keyboards/orthodox/split_util.c | 84 | ||||
| -rw-r--r-- | keyboards/orthodox/split_util.h | 21 |
12 files changed, 12 insertions, 965 deletions
diff --git a/keyboards/orthodox/config.h b/keyboards/orthodox/config.h deleted file mode 100644 index 821cd7e29..000000000 --- a/keyboards/orthodox/config.h +++ /dev/null | |||
| @@ -1,27 +0,0 @@ | |||
| 1 | /* | ||
| 2 | This is the c configuration file for the keyboard | ||
| 3 | |||
| 4 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
| 5 | Copyright 2017 Jack Humbert | ||
| 6 | Copyright 2017 Art Ortenburger | ||
| 7 | |||
| 8 | This program is free software: you can redistribute it and/or modify | ||
| 9 | it under the terms of the GNU General Public License as published by | ||
| 10 | the Free Software Foundation, either version 2 of the License, or | ||
| 11 | (at your option) any later version. | ||
| 12 | |||
| 13 | This program is distributed in the hope that it will be useful, | ||
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | GNU General Public License for more details. | ||
| 17 | |||
| 18 | You should have received a copy of the GNU General Public License | ||
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 20 | */ | ||
| 21 | |||
| 22 | #ifndef CONFIG_H | ||
| 23 | #define CONFIG_H | ||
| 24 | |||
| 25 | #include "config_common.h" | ||
| 26 | |||
| 27 | #endif | ||
diff --git a/keyboards/orthodox/i2c.c b/keyboards/orthodox/i2c.c deleted file mode 100644 index 084c890c4..000000000 --- a/keyboards/orthodox/i2c.c +++ /dev/null | |||
| @@ -1,162 +0,0 @@ | |||
| 1 | #include <util/twi.h> | ||
| 2 | #include <avr/io.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <avr/interrupt.h> | ||
| 5 | #include <util/twi.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | #include "i2c.h" | ||
| 8 | |||
| 9 | #ifdef USE_I2C | ||
| 10 | |||
| 11 | // Limits the amount of we wait for any one i2c transaction. | ||
| 12 | // Since were running SCL line 100kHz (=> 10μs/bit), and each transactions is | ||
| 13 | // 9 bits, a single transaction will take around 90μs to complete. | ||
| 14 | // | ||
| 15 | // (F_CPU/SCL_CLOCK) => # of μC cycles to transfer a bit | ||
| 16 | // poll loop takes at least 8 clock cycles to execute | ||
| 17 | #define I2C_LOOP_TIMEOUT (9+1)*(F_CPU/SCL_CLOCK)/8 | ||
| 18 | |||
| 19 | #define BUFFER_POS_INC() (slave_buffer_pos = (slave_buffer_pos+1)%SLAVE_BUFFER_SIZE) | ||
| 20 | |||
| 21 | volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE]; | ||
| 22 | |||
| 23 | static volatile uint8_t slave_buffer_pos; | ||
| 24 | static volatile bool slave_has_register_set = false; | ||
| 25 | |||
| 26 | // Wait for an i2c operation to finish | ||
| 27 | inline static | ||
| 28 | void i2c_delay(void) { | ||
| 29 | uint16_t lim = 0; | ||
| 30 | while(!(TWCR & (1<<TWINT)) && lim < I2C_LOOP_TIMEOUT) | ||
| 31 | lim++; | ||
| 32 | |||
| 33 | // easier way, but will wait slightly longer | ||
| 34 | // _delay_us(100); | ||
| 35 | } | ||
| 36 | |||
| 37 | // Setup twi to run at 100kHz | ||
| 38 | void i2c_master_init(void) { | ||
| 39 | // no prescaler | ||
| 40 | TWSR = 0; | ||
| 41 | // Set TWI clock frequency to SCL_CLOCK. Need TWBR>10. | ||
| 42 | // Check datasheets for more info. | ||
| 43 | TWBR = ((F_CPU/SCL_CLOCK)-16)/2; | ||
| 44 | } | ||
| 45 | |||
| 46 | // Start a transaction with the given i2c slave address. The direction of the | ||
| 47 | // transfer is set with I2C_READ and I2C_WRITE. | ||
| 48 | // returns: 0 => success | ||
| 49 | // 1 => error | ||
| 50 | uint8_t i2c_master_start(uint8_t address) { | ||
| 51 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTA); | ||
| 52 | |||
| 53 | i2c_delay(); | ||
| 54 | |||
| 55 | // check that we started successfully | ||
| 56 | if ( (TW_STATUS != TW_START) && (TW_STATUS != TW_REP_START)) | ||
| 57 | return 1; | ||
| 58 | |||
| 59 | TWDR = address; | ||
| 60 | TWCR = (1<<TWINT) | (1<<TWEN); | ||
| 61 | |||
| 62 | i2c_delay(); | ||
| 63 | |||
| 64 | if ( (TW_STATUS != TW_MT_SLA_ACK) && (TW_STATUS != TW_MR_SLA_ACK) ) | ||
| 65 | return 1; // slave did not acknowledge | ||
| 66 | else | ||
| 67 | return 0; // success | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | // Finish the i2c transaction. | ||
| 72 | void i2c_master_stop(void) { | ||
| 73 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); | ||
| 74 | |||
| 75 | uint16_t lim = 0; | ||
| 76 | while(!(TWCR & (1<<TWSTO)) && lim < I2C_LOOP_TIMEOUT) | ||
| 77 | lim++; | ||
| 78 | } | ||
| 79 | |||
| 80 | // Write one byte to the i2c slave. | ||
| 81 | // returns 0 => slave ACK | ||
| 82 | // 1 => slave NACK | ||
| 83 | uint8_t i2c_master_write(uint8_t data) { | ||
| 84 | TWDR = data; | ||
| 85 | TWCR = (1<<TWINT) | (1<<TWEN); | ||
| 86 | |||
| 87 | i2c_delay(); | ||
| 88 | |||
| 89 | // check if the slave acknowledged us | ||
| 90 | return (TW_STATUS == TW_MT_DATA_ACK) ? 0 : 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | // Read one byte from the i2c slave. If ack=1 the slave is acknowledged, | ||
| 94 | // if ack=0 the acknowledge bit is not set. | ||
| 95 | // returns: byte read from i2c device | ||
| 96 | uint8_t i2c_master_read(int ack) { | ||
| 97 | TWCR = (1<<TWINT) | (1<<TWEN) | (ack<<TWEA); | ||
| 98 | |||
| 99 | i2c_delay(); | ||
| 100 | return TWDR; | ||
| 101 | } | ||
| 102 | |||
| 103 | void i2c_reset_state(void) { | ||
| 104 | TWCR = 0; | ||
| 105 | } | ||
| 106 | |||
| 107 | void i2c_slave_init(uint8_t address) { | ||
| 108 | TWAR = address << 0; // slave i2c address | ||
| 109 | // TWEN - twi enable | ||
| 110 | // TWEA - enable address acknowledgement | ||
| 111 | // TWINT - twi interrupt flag | ||
| 112 | // TWIE - enable the twi interrupt | ||
| 113 | TWCR = (1<<TWIE) | (1<<TWEA) | (1<<TWINT) | (1<<TWEN); | ||
| 114 | } | ||
| 115 | |||
| 116 | ISR(TWI_vect); | ||
| 117 | |||
| 118 | ISR(TWI_vect) { | ||
| 119 | uint8_t ack = 1; | ||
| 120 | switch(TW_STATUS) { | ||
| 121 | case TW_SR_SLA_ACK: | ||
| 122 | // this device has been addressed as a slave receiver | ||
| 123 | slave_has_register_set = false; | ||
| 124 | break; | ||
| 125 | |||
| 126 | case TW_SR_DATA_ACK: | ||
| 127 | // this device has received data as a slave receiver | ||
| 128 | // The first byte that we receive in this transaction sets the location | ||
| 129 | // of the read/write location of the slaves memory that it exposes over | ||
| 130 | // i2c. After that, bytes will be written at slave_buffer_pos, incrementing | ||
| 131 | // slave_buffer_pos after each write. | ||
| 132 | if(!slave_has_register_set) { | ||
| 133 | slave_buffer_pos = TWDR; | ||
| 134 | // don't acknowledge the master if this memory loctaion is out of bounds | ||
| 135 | if ( slave_buffer_pos >= SLAVE_BUFFER_SIZE ) { | ||
| 136 | ack = 0; | ||
| 137 | slave_buffer_pos = 0; | ||
| 138 | } | ||
| 139 | slave_has_register_set = true; | ||
| 140 | } else { | ||
| 141 | i2c_slave_buffer[slave_buffer_pos] = TWDR; | ||
| 142 | BUFFER_POS_INC(); | ||
| 143 | } | ||
| 144 | break; | ||
| 145 | |||
| 146 | case TW_ST_SLA_ACK: | ||
| 147 | case TW_ST_DATA_ACK: | ||
| 148 | // master has addressed this device as a slave transmitter and is | ||
| 149 | // requesting data. | ||
| 150 | TWDR = i2c_slave_buffer[slave_buffer_pos]; | ||
| 151 | BUFFER_POS_INC(); | ||
| 152 | break; | ||
| 153 | |||
| 154 | case TW_BUS_ERROR: // something went wrong, reset twi state | ||
| 155 | TWCR = 0; | ||
| 156 | default: | ||
| 157 | break; | ||
| 158 | } | ||
| 159 | // Reset everything, so we are ready for the next TWI interrupt | ||
| 160 | TWCR |= (1<<TWIE) | (1<<TWINT) | (ack<<TWEA) | (1<<TWEN); | ||
| 161 | } | ||
| 162 | #endif | ||
diff --git a/keyboards/orthodox/i2c.h b/keyboards/orthodox/i2c.h deleted file mode 100644 index 2af843ff6..000000000 --- a/keyboards/orthodox/i2c.h +++ /dev/null | |||
| @@ -1,50 +0,0 @@ | |||
| 1 | #ifndef I2C_H | ||
| 2 | #define I2C_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | #include "matrix.h" | ||
| 6 | |||
| 7 | #ifndef F_CPU | ||
| 8 | #define F_CPU 16000000UL | ||
| 9 | #endif | ||
| 10 | |||
| 11 | #define I2C_READ 1 | ||
| 12 | #define I2C_WRITE 0 | ||
| 13 | |||
| 14 | #define I2C_ACK 1 | ||
| 15 | #define I2C_NACK 0 | ||
| 16 | |||
| 17 | #define SLAVE_BUFFER_SIZE 0x10 | ||
| 18 | |||
| 19 | // i2c SCL clock frequency | ||
| 20 | #define SCL_CLOCK 100000UL | ||
| 21 | |||
| 22 | extern volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE]; | ||
| 23 | |||
| 24 | void i2c_master_init(void); | ||
| 25 | uint8_t i2c_master_start(uint8_t address); | ||
| 26 | void i2c_master_stop(void); | ||
| 27 | uint8_t i2c_master_write(uint8_t data); | ||
| 28 | uint8_t i2c_master_read(int); | ||
| 29 | void i2c_reset_state(void); | ||
| 30 | void i2c_slave_init(uint8_t address); | ||
| 31 | |||
| 32 | |||
| 33 | static inline unsigned char i2c_start_read(unsigned char addr) { | ||
| 34 | return i2c_master_start((addr << 1) | I2C_READ); | ||
| 35 | } | ||
| 36 | |||
| 37 | static inline unsigned char i2c_start_write(unsigned char addr) { | ||
| 38 | return i2c_master_start((addr << 1) | I2C_WRITE); | ||
| 39 | } | ||
| 40 | |||
| 41 | // from SSD1306 scrips | ||
| 42 | extern unsigned char i2c_rep_start(unsigned char addr); | ||
| 43 | extern void i2c_start_wait(unsigned char addr); | ||
| 44 | extern unsigned char i2c_readAck(void); | ||
| 45 | extern unsigned char i2c_readNak(void); | ||
| 46 | extern unsigned char i2c_read(unsigned char ack); | ||
| 47 | |||
| 48 | #define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak(); | ||
| 49 | |||
| 50 | #endif | ||
diff --git a/keyboards/orthodox/matrix.c b/keyboards/orthodox/matrix.c deleted file mode 100644 index 2ca5f4d87..000000000 --- a/keyboards/orthodox/matrix.c +++ /dev/null | |||
| @@ -1,342 +0,0 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
| 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 | |||
| 18 | /* | ||
| 19 | * scan matrix | ||
| 20 | */ | ||
| 21 | #include <stdint.h> | ||
| 22 | #include <stdbool.h> | ||
| 23 | #ifdef USE_I2C | ||
| 24 | // provides memcpy for copying TWI slave buffer | ||
| 25 | // #include <string.h> | ||
| 26 | #endif | ||
| 27 | #include <avr/io.h> | ||
| 28 | #include <avr/wdt.h> | ||
| 29 | #include <avr/interrupt.h> | ||
| 30 | #include <util/delay.h> | ||
| 31 | #include "print.h" | ||
| 32 | #include "debug.h" | ||
| 33 | #include "util.h" | ||
| 34 | #include "matrix.h" | ||
| 35 | #include "split_util.h" | ||
| 36 | #include "pro_micro.h" | ||
| 37 | #include "config.h" | ||
| 38 | |||
| 39 | #ifdef USE_I2C | ||
| 40 | # include "i2c.h" | ||
| 41 | #else // USE_SERIAL | ||
| 42 | # include "serial.h" | ||
| 43 | #endif | ||
| 44 | |||
| 45 | #ifndef DEBOUNCE | ||
| 46 | # define DEBOUNCE 5 | ||
| 47 | #endif | ||
| 48 | |||
| 49 | #define ERROR_DISCONNECT_COUNT 5 | ||
| 50 | |||
| 51 | static uint8_t debouncing = DEBOUNCE; | ||
| 52 | static const int ROWS_PER_HAND = MATRIX_ROWS/2; | ||
| 53 | static uint8_t error_count = 0; | ||
| 54 | |||
| 55 | static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
| 56 | static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
| 57 | |||
| 58 | /* matrix state(1:on, 0:off) */ | ||
| 59 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
| 60 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
| 61 | |||
| 62 | static matrix_row_t read_cols(void); | ||
| 63 | static void init_cols(void); | ||
| 64 | static void unselect_rows(void); | ||
| 65 | static void select_row(uint8_t row); | ||
| 66 | |||
| 67 | |||
| 68 | __attribute__ ((weak)) | ||
| 69 | void matrix_init_kb(void) { | ||
| 70 | matrix_init_user(); | ||
| 71 | } | ||
| 72 | |||
| 73 | __attribute__ ((weak)) | ||
| 74 | void matrix_scan_kb(void) { | ||
| 75 | matrix_scan_user(); | ||
| 76 | } | ||
| 77 | |||
| 78 | __attribute__ ((weak)) | ||
| 79 | void matrix_init_user(void) { | ||
| 80 | } | ||
| 81 | |||
| 82 | __attribute__ ((weak)) | ||
| 83 | void matrix_scan_user(void) { | ||
| 84 | } | ||
| 85 | |||
| 86 | inline | ||
| 87 | uint8_t matrix_rows(void) | ||
| 88 | { | ||
| 89 | return MATRIX_ROWS; | ||
| 90 | } | ||
| 91 | |||
| 92 | inline | ||
| 93 | uint8_t matrix_cols(void) | ||
| 94 | { | ||
| 95 | return MATRIX_COLS; | ||
| 96 | } | ||
| 97 | |||
| 98 | void matrix_init(void) | ||
| 99 | { | ||
| 100 | debug_enable = true; | ||
| 101 | debug_matrix = true; | ||
| 102 | debug_mouse = true; | ||
| 103 | // initialize row and col | ||
| 104 | unselect_rows(); | ||
| 105 | init_cols(); | ||
| 106 | |||
| 107 | TX_RX_LED_INIT; | ||
| 108 | |||
| 109 | // initialize matrix state: all keys off | ||
| 110 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
| 111 | matrix[i] = 0; | ||
| 112 | matrix_debouncing[i] = 0; | ||
| 113 | } | ||
| 114 | |||
| 115 | matrix_init_quantum(); | ||
| 116 | } | ||
| 117 | |||
| 118 | uint8_t _matrix_scan(void) | ||
| 119 | { | ||
| 120 | // Right hand is stored after the left in the matrix so, we need to offset it | ||
| 121 | int offset = isLeftHand ? 0 : (ROWS_PER_HAND); | ||
| 122 | |||
| 123 | for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | ||
| 124 | select_row(i); | ||
| 125 | _delay_us(30); // without this wait read unstable value. | ||
| 126 | matrix_row_t cols = read_cols(); | ||
| 127 | if (matrix_debouncing[i+offset] != cols) { | ||
| 128 | matrix_debouncing[i+offset] = cols; | ||
| 129 | debouncing = DEBOUNCE; | ||
| 130 | } | ||
| 131 | unselect_rows(); | ||
| 132 | } | ||
| 133 | |||
| 134 | if (debouncing) { | ||
| 135 | if (--debouncing) { | ||
| 136 | _delay_ms(1); | ||
| 137 | } else { | ||
| 138 | for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | ||
| 139 | matrix[i+offset] = matrix_debouncing[i+offset]; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | return 1; | ||
| 145 | } | ||
| 146 | |||
| 147 | #ifdef USE_I2C | ||
| 148 | |||
| 149 | // Get rows from other half over i2c | ||
| 150 | int i2c_transaction(void) { | ||
| 151 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
| 152 | |||
| 153 | int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE); | ||
| 154 | if (err) goto i2c_error; | ||
| 155 | |||
| 156 | // start of matrix stored at 0x00 | ||
| 157 | err = i2c_master_write(0x00); | ||
| 158 | if (err) goto i2c_error; | ||
| 159 | |||
| 160 | // Start read | ||
| 161 | err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ); | ||
| 162 | if (err) goto i2c_error; | ||
| 163 | |||
| 164 | if (!err) { | ||
| 165 | /* | ||
| 166 | // read from TWI byte-by-byte into matrix_row_t memory space | ||
| 167 | size_t i; | ||
| 168 | for (i = 0; i < SLAVE_BUFFER_SIZE-1; ++i) { | ||
| 169 | *((uint8_t*)&matrix[slaveOffset]+i) = i2c_master_read(I2C_ACK); | ||
| 170 | } | ||
| 171 | // last byte to be read / end of chunk | ||
| 172 | *((uint8_t*)&matrix[slaveOffset]+i) = i2c_master_read(I2C_NACK); | ||
| 173 | */ | ||
| 174 | |||
| 175 | // kludge for column #9: unpack bits for keys (2,9) and (3,9) from (1,7) and (1,8) | ||
| 176 | // i2c_master_read(I2C_ACK); | ||
| 177 | matrix[slaveOffset+0] = i2c_master_read(I2C_ACK); | ||
| 178 | // i2c_master_read(I2C_ACK); | ||
| 179 | matrix[slaveOffset+1] = (matrix_row_t)i2c_master_read(I2C_ACK)\ | ||
| 180 | | (matrix[slaveOffset+0]&0x40U)<<2; | ||
| 181 | // i2c_master_read(I2C_ACK); | ||
| 182 | matrix[slaveOffset+2] = (matrix_row_t)i2c_master_read(I2C_NACK)\ | ||
| 183 | | (matrix[slaveOffset+0]&0x80U)<<1; | ||
| 184 | // clear highest two bits on row 1, where the col9 bits were transported | ||
| 185 | matrix[slaveOffset+0] &= 0x3F; | ||
| 186 | |||
| 187 | i2c_master_stop(); | ||
| 188 | } else { | ||
| 189 | i2c_error: // the cable is disconnected, or something else went wrong | ||
| 190 | i2c_reset_state(); | ||
| 191 | return err; | ||
| 192 | } | ||
| 193 | |||
| 194 | return 0; | ||
| 195 | } | ||
| 196 | |||
| 197 | #else // USE_SERIAL | ||
| 198 | |||
| 199 | int serial_transaction(void) { | ||
| 200 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
| 201 | |||
| 202 | if (serial_update_buffers()) { | ||
| 203 | return 1; | ||
| 204 | } | ||
| 205 | |||
| 206 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 207 | matrix[slaveOffset+i] = serial_slave_buffer[i]; | ||
| 208 | } | ||
| 209 | return 0; | ||
| 210 | } | ||
| 211 | #endif | ||
| 212 | |||
| 213 | uint8_t matrix_scan(void) | ||
| 214 | { | ||
| 215 | int ret = _matrix_scan(); | ||
| 216 | |||
| 217 | |||
| 218 | |||
| 219 | #ifdef USE_I2C | ||
| 220 | if( i2c_transaction() ) { | ||
| 221 | #else // USE_SERIAL | ||
| 222 | if( serial_transaction() ) { | ||
| 223 | #endif | ||
| 224 | // turn on the indicator led when halves are disconnected | ||
| 225 | TXLED1; | ||
| 226 | |||
| 227 | error_count++; | ||
| 228 | |||
| 229 | if (error_count > ERROR_DISCONNECT_COUNT) { | ||
| 230 | // reset other half if disconnected | ||
| 231 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
| 232 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 233 | matrix[slaveOffset+i] = 0; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } else { | ||
| 237 | // turn off the indicator led on no error | ||
| 238 | TXLED0; | ||
| 239 | error_count = 0; | ||
| 240 | } | ||
| 241 | matrix_scan_quantum(); | ||
| 242 | return ret; | ||
| 243 | } | ||
| 244 | |||
| 245 | void matrix_slave_scan(void) { | ||
| 246 | _matrix_scan(); | ||
| 247 | |||
| 248 | int offset = (isLeftHand) ? 0 : ROWS_PER_HAND; | ||
| 249 | |||
| 250 | #ifdef USE_I2C | ||
| 251 | // SLAVE_BUFFER_SIZE is from i2c.h | ||
| 252 | // (MATRIX_ROWS/2*sizeof(matrix_row_t)) | ||
| 253 | // memcpy((void*)i2c_slave_buffer, (const void*)&matrix[offset], (ROWS_PER_HAND*sizeof(matrix_row_t))); | ||
| 254 | |||
| 255 | // kludge for column #9: put bits for keys (2,9) and (3,9) into (1,7) and (1,8) | ||
| 256 | i2c_slave_buffer[0] = (uint8_t)(matrix[offset+0])\ | ||
| 257 | | (matrix[offset+1]&0x100U)>>2\ | ||
| 258 | | (matrix[offset+2]&0x100U)>>1; | ||
| 259 | i2c_slave_buffer[1] = (uint8_t)(matrix[offset+1]); | ||
| 260 | i2c_slave_buffer[2] = (uint8_t)(matrix[offset+2]); | ||
| 261 | // note: looks like a possible operator-precedence bug here, in last version? | ||
| 262 | /* | ||
| 263 | i2c_slave_buffer[1] = (uint8_t)matrix[offset+0]; | ||
| 264 | i2c_slave_buffer[2] = (uint8_t)(matrix[offset+1]>>8); | ||
| 265 | i2c_slave_buffer[3] = (uint8_t)(matrix[offset+1]>>8); | ||
| 266 | i2c_slave_buffer[4] = (uint8_t)(matrix[offset+2]>>8); | ||
| 267 | i2c_slave_buffer[5] = (uint8_t)matrix[offset+2]; | ||
| 268 | */ | ||
| 269 | #else // USE_SERIAL | ||
| 270 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 271 | serial_slave_buffer[i] = matrix[offset+i]; | ||
| 272 | } | ||
| 273 | #endif | ||
| 274 | } | ||
| 275 | |||
| 276 | bool matrix_is_modified(void) | ||
| 277 | { | ||
| 278 | if (debouncing) return false; | ||
| 279 | return true; | ||
| 280 | } | ||
| 281 | |||
| 282 | inline | ||
| 283 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 284 | { | ||
| 285 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
| 286 | } | ||
| 287 | |||
| 288 | inline | ||
| 289 | matrix_row_t matrix_get_row(uint8_t row) | ||
| 290 | { | ||
| 291 | return matrix[row]; | ||
| 292 | } | ||
| 293 | |||
| 294 | void matrix_print(void) | ||
| 295 | { | ||
| 296 | print("\nr/c 0123456789ABCDEF\n"); | ||
| 297 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 298 | phex(row); print(": "); | ||
| 299 | pbin_reverse16(matrix_get_row(row)); | ||
| 300 | print("\n"); | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | uint8_t matrix_key_count(void) | ||
| 305 | { | ||
| 306 | uint8_t count = 0; | ||
| 307 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 308 | count += bitpop16(matrix[i]); | ||
| 309 | } | ||
| 310 | return count; | ||
| 311 | } | ||
| 312 | |||
| 313 | static void init_cols(void) | ||
| 314 | { | ||
| 315 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
| 316 | _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF); | ||
| 317 | _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF); | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | static matrix_row_t read_cols(void) | ||
| 322 | { | ||
| 323 | matrix_row_t result = 0; | ||
| 324 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
| 325 | result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x); | ||
| 326 | } | ||
| 327 | return result; | ||
| 328 | } | ||
| 329 | |||
| 330 | static void unselect_rows(void) | ||
| 331 | { | ||
| 332 | for(int x = 0; x < ROWS_PER_HAND; x++) { | ||
| 333 | _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF); | ||
| 334 | _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF); | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | static void select_row(uint8_t row) | ||
| 339 | { | ||
| 340 | _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF); | ||
| 341 | _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF); | ||
| 342 | } | ||
diff --git a/keyboards/orthodox/rev1/config.h b/keyboards/orthodox/rev1/config.h index f7497c09c..198fff5ac 100644 --- a/keyboards/orthodox/rev1/config.h +++ b/keyboards/orthodox/rev1/config.h | |||
| @@ -19,8 +19,7 @@ You should have received a copy of the GNU General Public License | |||
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ | 20 | */ |
| 21 | 21 | ||
| 22 | #ifndef REV1_CONFIG_H | 22 | #pragma once |
| 23 | #define REV1_CONFIG_H | ||
| 24 | 23 | ||
| 25 | #include "config_common.h" | 24 | #include "config_common.h" |
| 26 | 25 | ||
| @@ -49,6 +48,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 49 | /* COL2ROW or ROW2COL */ | 48 | /* COL2ROW or ROW2COL */ |
| 50 | #define DIODE_DIRECTION COL2ROW | 49 | #define DIODE_DIRECTION COL2ROW |
| 51 | 50 | ||
| 51 | #define SOFT_SERIAL_PIN D0 | ||
| 52 | |||
| 52 | /* define if matrix has ghost */ | 53 | /* define if matrix has ghost */ |
| 53 | //#define MATRIX_HAS_GHOST | 54 | //#define MATRIX_HAS_GHOST |
| 54 | 55 | ||
| @@ -89,5 +90,3 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 89 | //#define NO_ACTION_ONESHOT | 90 | //#define NO_ACTION_ONESHOT |
| 90 | //#define NO_ACTION_MACRO | 91 | //#define NO_ACTION_MACRO |
| 91 | //#define NO_ACTION_FUNCTION | 92 | //#define NO_ACTION_FUNCTION |
| 92 | |||
| 93 | #endif | ||
diff --git a/keyboards/orthodox/rev3/config.h b/keyboards/orthodox/rev3/config.h index d727ae674..53e5b15ae 100644 --- a/keyboards/orthodox/rev3/config.h +++ b/keyboards/orthodox/rev3/config.h | |||
| @@ -19,8 +19,7 @@ You should have received a copy of the GNU General Public License | |||
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ | 20 | */ |
| 21 | 21 | ||
| 22 | #ifndef REV3_CONFIG_H | 22 | #pragma once |
| 23 | #define REV3_CONFIG_H | ||
| 24 | 23 | ||
| 25 | #include "config_common.h" | 24 | #include "config_common.h" |
| 26 | 25 | ||
| @@ -54,6 +53,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 54 | /* COL2ROW or ROW2COL */ | 53 | /* COL2ROW or ROW2COL */ |
| 55 | #define DIODE_DIRECTION COL2ROW | 54 | #define DIODE_DIRECTION COL2ROW |
| 56 | 55 | ||
| 56 | #define SOFT_SERIAL_PIN D0 | ||
| 57 | |||
| 57 | /* define if matrix has ghost */ | 58 | /* define if matrix has ghost */ |
| 58 | //#define MATRIX_HAS_GHOST | 59 | //#define MATRIX_HAS_GHOST |
| 59 | 60 | ||
| @@ -95,5 +96,3 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 95 | //#define NO_ACTION_ONESHOT | 96 | //#define NO_ACTION_ONESHOT |
| 96 | //#define NO_ACTION_MACRO | 97 | //#define NO_ACTION_MACRO |
| 97 | //#define NO_ACTION_FUNCTION | 98 | //#define NO_ACTION_FUNCTION |
| 98 | |||
| 99 | #endif | ||
diff --git a/keyboards/orthodox/rev3_teensy/config.h b/keyboards/orthodox/rev3_teensy/config.h index 591827fb9..54cbb823e 100644 --- a/keyboards/orthodox/rev3_teensy/config.h +++ b/keyboards/orthodox/rev3_teensy/config.h | |||
| @@ -19,8 +19,7 @@ You should have received a copy of the GNU General Public License | |||
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ | 20 | */ |
| 21 | 21 | ||
| 22 | #ifndef REV3_TEENSY_CONFIG_H | 22 | #pragma once |
| 23 | #define REV3_TEENSY_CONFIG_H | ||
| 24 | 23 | ||
| 25 | #include "config_common.h" | 24 | #include "config_common.h" |
| 26 | 25 | ||
| @@ -46,6 +45,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 46 | /* COL2ROW or ROW2COL */ | 45 | /* COL2ROW or ROW2COL */ |
| 47 | #define DIODE_DIRECTION COL2ROW | 46 | #define DIODE_DIRECTION COL2ROW |
| 48 | 47 | ||
| 48 | #define SOFT_SERIAL_PIN D0 | ||
| 49 | |||
| 49 | /* define if matrix has ghost */ | 50 | /* define if matrix has ghost */ |
| 50 | //#define MATRIX_HAS_GHOST | 51 | //#define MATRIX_HAS_GHOST |
| 51 | 52 | ||
| @@ -87,5 +88,3 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 87 | //#define NO_ACTION_ONESHOT | 88 | //#define NO_ACTION_ONESHOT |
| 88 | //#define NO_ACTION_MACRO | 89 | //#define NO_ACTION_MACRO |
| 89 | //#define NO_ACTION_FUNCTION | 90 | //#define NO_ACTION_FUNCTION |
| 90 | |||
| 91 | #endif | ||
diff --git a/keyboards/orthodox/rules.mk b/keyboards/orthodox/rules.mk index 715f25c3a..61878ea83 100644 --- a/keyboards/orthodox/rules.mk +++ b/keyboards/orthodox/rules.mk | |||
| @@ -1,8 +1,3 @@ | |||
| 1 | SRC += matrix.c \ | ||
| 2 | i2c.c \ | ||
| 3 | split_util.c \ | ||
| 4 | serial.c | ||
| 5 | |||
| 6 | # MCU name | 1 | # MCU name |
| 7 | MCU = atmega32u4 | 2 | MCU = atmega32u4 |
| 8 | 3 | ||
| @@ -40,7 +35,7 @@ F_USB = $(F_CPU) | |||
| 40 | 35 | ||
| 41 | # Bootloader | 36 | # Bootloader |
| 42 | # This definition is optional, and if your keyboard supports multiple bootloaders of | 37 | # This definition is optional, and if your keyboard supports multiple bootloaders of |
| 43 | # different sizes, comment this out, and the correct address will be loaded | 38 | # different sizes, comment this out, and the correct address will be loaded |
| 44 | # automatically (+60). See bootloader.mk for all options. | 39 | # automatically (+60). See bootloader.mk for all options. |
| 45 | 40 | ||
| 46 | # Interrupt driven control endpoint task(+60) | 41 | # Interrupt driven control endpoint task(+60) |
| @@ -61,12 +56,10 @@ MIDI_ENABLE = no # MIDI controls | |||
| 61 | AUDIO_ENABLE = no # Audio output on port C6 | 56 | AUDIO_ENABLE = no # Audio output on port C6 |
| 62 | UNICODE_ENABLE = no # Unicode | 57 | UNICODE_ENABLE = no # Unicode |
| 63 | BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID | 58 | BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID |
| 64 | RGBLIGHT_ENABLE = no # Enable WS2812 RGB underlight. | 59 | RGBLIGHT_ENABLE = no # Enable WS2812 RGB underlight. |
| 65 | SUBPROJECT_rev1 = yes | ||
| 66 | USE_I2C = yes | ||
| 67 | # Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE | 60 | # Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE |
| 68 | SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend | 61 | SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend |
| 69 | 62 | ||
| 70 | CUSTOM_MATRIX = yes | 63 | SPLIT_KEYBOARD = yes |
| 71 | 64 | ||
| 72 | DEFAULT_FOLDER = orthodox/rev3 | 65 | DEFAULT_FOLDER = orthodox/rev3 |
diff --git a/keyboards/orthodox/serial.c b/keyboards/orthodox/serial.c deleted file mode 100644 index fea57b651..000000000 --- a/keyboards/orthodox/serial.c +++ /dev/null | |||
| @@ -1,230 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * WARNING: be careful changing this code, it is very timing dependent | ||
| 3 | */ | ||
| 4 | |||
| 5 | #ifndef F_CPU | ||
| 6 | #define F_CPU 16000000 | ||
| 7 | #endif | ||
| 8 | |||
| 9 | #include <avr/io.h> | ||
| 10 | #include <avr/interrupt.h> | ||
| 11 | #include <util/delay.h> | ||
| 12 | #include <stdbool.h> | ||
| 13 | #include "serial.h" | ||
| 14 | |||
| 15 | #ifndef USE_I2C | ||
| 16 | |||
| 17 | // Serial pulse period in microseconds. Its probably a bad idea to lower this | ||
| 18 | // value. | ||
| 19 | #define SERIAL_DELAY 24 | ||
| 20 | |||
| 21 | matrix_row_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0}; | ||
| 22 | matrix_row_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0}; | ||
| 23 | |||
| 24 | #define ROW_MASK (((matrix_row_t)0-1)>>(8*sizeof(matrix_row_t)-MATRIX_COLS)) | ||
| 25 | |||
| 26 | #define SLAVE_DATA_CORRUPT (1<<0) | ||
| 27 | volatile uint8_t status = 0; | ||
| 28 | |||
| 29 | inline static | ||
| 30 | void serial_delay(void) { | ||
| 31 | _delay_us(SERIAL_DELAY); | ||
| 32 | } | ||
| 33 | |||
| 34 | inline static | ||
| 35 | void serial_output(void) { | ||
| 36 | SERIAL_PIN_DDR |= SERIAL_PIN_MASK; | ||
| 37 | } | ||
| 38 | |||
| 39 | // make the serial pin an input with pull-up resistor | ||
| 40 | inline static | ||
| 41 | void serial_input(void) { | ||
| 42 | SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK; | ||
| 43 | SERIAL_PIN_PORT |= SERIAL_PIN_MASK; | ||
| 44 | } | ||
| 45 | |||
| 46 | inline static | ||
| 47 | matrix_row_t serial_read_pin(void) { | ||
| 48 | return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK); | ||
| 49 | } | ||
| 50 | |||
| 51 | inline static | ||
| 52 | void serial_low(void) { | ||
| 53 | SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK; | ||
| 54 | } | ||
| 55 | |||
| 56 | inline static | ||
| 57 | void serial_high(void) { | ||
| 58 | SERIAL_PIN_PORT |= SERIAL_PIN_MASK; | ||
| 59 | } | ||
| 60 | |||
| 61 | void serial_master_init(void) { | ||
| 62 | serial_output(); | ||
| 63 | serial_high(); | ||
| 64 | } | ||
| 65 | |||
| 66 | void serial_slave_init(void) { | ||
| 67 | serial_input(); | ||
| 68 | |||
| 69 | // Enable INT0 | ||
| 70 | EIMSK |= _BV(INT0); | ||
| 71 | // Trigger on falling edge of INT0 | ||
| 72 | EICRA &= ~(_BV(ISC00) | _BV(ISC01)); | ||
| 73 | } | ||
| 74 | |||
| 75 | // Used by the master to synchronize timing with the slave. | ||
| 76 | static | ||
| 77 | void sync_recv(void) { | ||
| 78 | serial_input(); | ||
| 79 | // This shouldn't hang if the slave disconnects because the | ||
| 80 | // serial line will float to high if the slave does disconnect. | ||
| 81 | while (!serial_read_pin()); | ||
| 82 | serial_delay(); | ||
| 83 | } | ||
| 84 | |||
| 85 | // Used by the slave to send a synchronization signal to the master. | ||
| 86 | static | ||
| 87 | void sync_send(void) { | ||
| 88 | serial_output(); | ||
| 89 | |||
| 90 | serial_low(); | ||
| 91 | serial_delay(); | ||
| 92 | |||
| 93 | serial_high(); | ||
| 94 | } | ||
| 95 | |||
| 96 | // Reads a byte from the serial line | ||
| 97 | static | ||
| 98 | matrix_row_t serial_read_byte(void) { | ||
| 99 | matrix_row_t byte = 0; | ||
| 100 | serial_input(); | ||
| 101 | for ( uint8_t i = 0; i < MATRIX_COLS; ++i) { | ||
| 102 | byte = (byte << 1) | serial_read_pin(); | ||
| 103 | serial_delay(); | ||
| 104 | _delay_us(1); | ||
| 105 | } | ||
| 106 | |||
| 107 | return byte; | ||
| 108 | } | ||
| 109 | |||
| 110 | // Sends a byte with MSB ordering | ||
| 111 | static | ||
| 112 | void serial_write_byte(matrix_row_t data) { | ||
| 113 | matrix_row_t b = MATRIX_COLS; | ||
| 114 | serial_output(); | ||
| 115 | while( b-- ) { | ||
| 116 | if(data & (1UL << b)) { | ||
| 117 | serial_high(); | ||
| 118 | } else { | ||
| 119 | serial_low(); | ||
| 120 | } | ||
| 121 | serial_delay(); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | // interrupt handle to be used by the slave device | ||
| 126 | ISR(SERIAL_PIN_INTERRUPT) { | ||
| 127 | sync_send(); | ||
| 128 | |||
| 129 | matrix_row_t checksum = 0; | ||
| 130 | for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { | ||
| 131 | serial_write_byte(serial_slave_buffer[i]); | ||
| 132 | sync_send(); | ||
| 133 | checksum += ROW_MASK & serial_slave_buffer[i]; | ||
| 134 | } | ||
| 135 | serial_write_byte(checksum); | ||
| 136 | sync_send(); | ||
| 137 | |||
| 138 | // wait for the sync to finish sending | ||
| 139 | serial_delay(); | ||
| 140 | |||
| 141 | // read the middle of pulses | ||
| 142 | _delay_us(SERIAL_DELAY/2); | ||
| 143 | |||
| 144 | matrix_row_t checksum_computed = 0; | ||
| 145 | for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { | ||
| 146 | serial_master_buffer[i] = serial_read_byte(); | ||
| 147 | sync_send(); | ||
| 148 | checksum_computed += ROW_MASK & serial_master_buffer[i]; | ||
| 149 | } | ||
| 150 | matrix_row_t checksum_received = serial_read_byte(); | ||
| 151 | sync_send(); | ||
| 152 | |||
| 153 | serial_input(); // end transaction | ||
| 154 | |||
| 155 | if ( checksum_computed != checksum_received ) { | ||
| 156 | status |= SLAVE_DATA_CORRUPT; | ||
| 157 | } else { | ||
| 158 | status &= ~SLAVE_DATA_CORRUPT; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | inline | ||
| 163 | bool serial_slave_DATA_CORRUPT(void) { | ||
| 164 | return status & SLAVE_DATA_CORRUPT; | ||
| 165 | } | ||
| 166 | |||
| 167 | // Copies the serial_slave_buffer to the master and sends the | ||
| 168 | // serial_master_buffer to the slave. | ||
| 169 | // | ||
| 170 | // Returns: | ||
| 171 | // 0 => no error | ||
| 172 | // 1 => slave did not respond | ||
| 173 | int serial_update_buffers(void) { | ||
| 174 | // this code is very time dependent, so we need to disable interrupts | ||
| 175 | cli(); | ||
| 176 | |||
| 177 | // signal to the slave that we want to start a transaction | ||
| 178 | serial_output(); | ||
| 179 | serial_low(); | ||
| 180 | _delay_us(1); | ||
| 181 | |||
| 182 | // wait for the slaves response | ||
| 183 | serial_input(); | ||
| 184 | serial_high(); | ||
| 185 | _delay_us(SERIAL_DELAY); | ||
| 186 | |||
| 187 | // check if the slave is present | ||
| 188 | if (serial_read_pin()) { | ||
| 189 | // slave failed to pull the line low, assume not present | ||
| 190 | sei(); | ||
| 191 | return 1; | ||
| 192 | } | ||
| 193 | |||
| 194 | // if the slave is present syncronize with it | ||
| 195 | sync_recv(); | ||
| 196 | |||
| 197 | matrix_row_t checksum_computed = 0; | ||
| 198 | // receive data from the slave | ||
| 199 | for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { | ||
| 200 | serial_slave_buffer[i] = serial_read_byte(); | ||
| 201 | sync_recv(); | ||
| 202 | checksum_computed += ROW_MASK & serial_slave_buffer[i]; | ||
| 203 | } | ||
| 204 | matrix_row_t checksum_received = serial_read_byte(); | ||
| 205 | sync_recv(); | ||
| 206 | |||
| 207 | if (checksum_computed != checksum_received) { | ||
| 208 | sei(); | ||
| 209 | return 1; | ||
| 210 | } | ||
| 211 | |||
| 212 | matrix_row_t checksum = 0; | ||
| 213 | // send data to the slave | ||
| 214 | for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { | ||
| 215 | serial_write_byte(serial_master_buffer[i]); | ||
| 216 | sync_recv(); | ||
| 217 | checksum += ROW_MASK & serial_master_buffer[i]; | ||
| 218 | } | ||
| 219 | serial_write_byte(checksum); | ||
| 220 | sync_recv(); | ||
| 221 | |||
| 222 | // always, release the line when not in use | ||
| 223 | serial_output(); | ||
| 224 | serial_high(); | ||
| 225 | |||
| 226 | sei(); | ||
| 227 | return 0; | ||
| 228 | } | ||
| 229 | |||
| 230 | #endif | ||
diff --git a/keyboards/orthodox/serial.h b/keyboards/orthodox/serial.h deleted file mode 100644 index a46a98c94..000000000 --- a/keyboards/orthodox/serial.h +++ /dev/null | |||
| @@ -1,27 +0,0 @@ | |||
| 1 | #ifndef MY_SERIAL_H | ||
| 2 | #define MY_SERIAL_H | ||
| 3 | |||
| 4 | #include "config.h" | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include "matrix.h" | ||
| 7 | |||
| 8 | /* TODO: some defines for interrupt setup */ | ||
| 9 | #define SERIAL_PIN_DDR DDRD | ||
| 10 | #define SERIAL_PIN_PORT PORTD | ||
| 11 | #define SERIAL_PIN_INPUT PIND | ||
| 12 | #define SERIAL_PIN_MASK _BV(PD0) | ||
| 13 | #define SERIAL_PIN_INTERRUPT INT0_vect | ||
| 14 | |||
| 15 | #define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2 | ||
| 16 | #define SERIAL_MASTER_BUFFER_LENGTH 1 | ||
| 17 | |||
| 18 | // Buffers for master - slave communication | ||
| 19 | extern volatile matrix_row_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH]; | ||
| 20 | extern volatile matrix_row_t serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH]; | ||
| 21 | |||
| 22 | void serial_master_init(void); | ||
| 23 | void serial_slave_init(void); | ||
| 24 | int serial_update_buffers(void); | ||
| 25 | bool serial_slave_data_corrupt(void); | ||
| 26 | |||
| 27 | #endif | ||
diff --git a/keyboards/orthodox/split_util.c b/keyboards/orthodox/split_util.c deleted file mode 100644 index 39639c3b4..000000000 --- a/keyboards/orthodox/split_util.c +++ /dev/null | |||
| @@ -1,84 +0,0 @@ | |||
| 1 | #include <avr/io.h> | ||
| 2 | #include <avr/wdt.h> | ||
| 3 | #include <avr/power.h> | ||
| 4 | #include <avr/interrupt.h> | ||
| 5 | #include <util/delay.h> | ||
| 6 | #include <avr/eeprom.h> | ||
| 7 | #include "split_util.h" | ||
| 8 | #include "matrix.h" | ||
| 9 | #include "keyboard.h" | ||
| 10 | #include "config.h" | ||
| 11 | |||
| 12 | #ifdef USE_I2C | ||
| 13 | # include "i2c.h" | ||
| 14 | #else | ||
| 15 | # include "serial.h" | ||
| 16 | #endif | ||
| 17 | |||
| 18 | volatile bool isLeftHand = true; | ||
| 19 | |||
| 20 | static void setup_handedness(void) { | ||
| 21 | #ifdef EE_HANDS | ||
| 22 | isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS); | ||
| 23 | #else | ||
| 24 | // I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c | ||
| 25 | #if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT) | ||
| 26 | isLeftHand = !has_usb(); | ||
| 27 | #else | ||
| 28 | isLeftHand = has_usb(); | ||
| 29 | #endif | ||
| 30 | #endif | ||
| 31 | } | ||
| 32 | |||
| 33 | static void keyboard_master_setup(void) { | ||
| 34 | #ifdef USE_I2C | ||
| 35 | i2c_master_init(); | ||
| 36 | #ifdef SSD1306OLED | ||
| 37 | matrix_master_OLED_init (); | ||
| 38 | #endif | ||
| 39 | #else | ||
| 40 | serial_master_init(); | ||
| 41 | #endif | ||
| 42 | } | ||
| 43 | |||
| 44 | static void keyboard_slave_setup(void) { | ||
| 45 | #ifdef USE_I2C | ||
| 46 | i2c_slave_init(SLAVE_I2C_ADDRESS); | ||
| 47 | #else | ||
| 48 | serial_slave_init(); | ||
| 49 | #endif | ||
| 50 | } | ||
| 51 | |||
| 52 | bool has_usb(void) { | ||
| 53 | USBCON |= (1 << OTGPADE); //enables VBUS pad | ||
| 54 | _delay_us(5); | ||
| 55 | return (USBSTA & (1<<VBUS)); //checks state of VBUS | ||
| 56 | } | ||
| 57 | |||
| 58 | void split_keyboard_setup(void) { | ||
| 59 | setup_handedness(); | ||
| 60 | |||
| 61 | if (has_usb()) { | ||
| 62 | keyboard_master_setup(); | ||
| 63 | } else { | ||
| 64 | keyboard_slave_setup(); | ||
| 65 | } | ||
| 66 | sei(); | ||
| 67 | } | ||
| 68 | |||
| 69 | void keyboard_slave_loop(void) { | ||
| 70 | matrix_init(); | ||
| 71 | |||
| 72 | while (1) { | ||
| 73 | matrix_slave_scan(); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | // this code runs before the usb and keyboard is initialized | ||
| 78 | void matrix_setup(void) { | ||
| 79 | split_keyboard_setup(); | ||
| 80 | |||
| 81 | if (!has_usb()) { | ||
| 82 | keyboard_slave_loop(); | ||
| 83 | } | ||
| 84 | } | ||
diff --git a/keyboards/orthodox/split_util.h b/keyboards/orthodox/split_util.h deleted file mode 100644 index a0a8dd3bf..000000000 --- a/keyboards/orthodox/split_util.h +++ /dev/null | |||
| @@ -1,21 +0,0 @@ | |||
| 1 | #ifndef SPLIT_KEYBOARD_UTIL_H | ||
| 2 | #define SPLIT_KEYBOARD_UTIL_H | ||
| 3 | |||
| 4 | #include <stdbool.h> | ||
| 5 | #include "eeconfig.h" | ||
| 6 | |||
| 7 | |||
| 8 | #define SLAVE_I2C_ADDRESS 0x32 | ||
| 9 | |||
| 10 | extern volatile bool isLeftHand; | ||
| 11 | |||
| 12 | // slave version of matix scan, defined in matrix.c | ||
| 13 | void matrix_slave_scan(void); | ||
| 14 | |||
| 15 | void split_keyboard_setup(void); | ||
| 16 | bool has_usb(void); | ||
| 17 | void keyboard_slave_loop(void); | ||
| 18 | |||
| 19 | void matrix_master_OLED_init (void); | ||
| 20 | |||
| 21 | #endif | ||
