aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny <nooges@users.noreply.github.com>2018-07-12 13:26:50 -0400
committerDrashna Jaelre <drashna@live.com>2018-07-12 10:26:49 -0700
commit9a1613dd1a86e8db65d68756cea48d9d9ba075d8 (patch)
tree247db8b9b7bb42ae1ef2c1c4fc9da7d49d4e90dd
parentf30d6dd7858dfa3a0cf214113e8b1d45b30d74ce (diff)
downloadqmk_firmware-9a1613dd1a86e8db65d68756cea48d9d9ba075d8.tar.gz
qmk_firmware-9a1613dd1a86e8db65d68756cea48d9d9ba075d8.zip
Add Quefrency Keyboard (#3376)
* Fork Fourier keyboard to Quefrency * Set Quefrency pinouts and initial keymap * Set configurator layout
-rw-r--r--keyboards/quefrency/config.h28
-rw-r--r--keyboards/quefrency/i2c.c162
-rw-r--r--keyboards/quefrency/i2c.h49
-rw-r--r--keyboards/quefrency/info.json86
-rw-r--r--keyboards/quefrency/keymaps/default/config.h31
-rw-r--r--keyboards/quefrency/keymaps/default/keymap.c35
-rw-r--r--keyboards/quefrency/keymaps/default/rules.mk0
-rw-r--r--keyboards/quefrency/matrix.c467
-rw-r--r--keyboards/quefrency/quefrency.c1
-rw-r--r--keyboards/quefrency/quefrency.h23
-rw-r--r--keyboards/quefrency/readme.md20
-rw-r--r--keyboards/quefrency/rev1/config.h84
-rw-r--r--keyboards/quefrency/rev1/rev1.c7
-rw-r--r--keyboards/quefrency/rev1/rev1.h37
-rw-r--r--keyboards/quefrency/rev1/rules.mk0
-rw-r--r--keyboards/quefrency/rules.mk74
-rw-r--r--keyboards/quefrency/serial.c228
-rw-r--r--keyboards/quefrency/serial.h26
-rw-r--r--keyboards/quefrency/split_util.c80
-rw-r--r--keyboards/quefrency/split_util.h20
20 files changed, 1458 insertions, 0 deletions
diff --git a/keyboards/quefrency/config.h b/keyboards/quefrency/config.h
new file mode 100644
index 000000000..8f0524f97
--- /dev/null
+++ b/keyboards/quefrency/config.h
@@ -0,0 +1,28 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3Copyright 2015 Jack Humbert
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#ifndef CONFIG_H
20#define CONFIG_H
21
22#include "config_common.h"
23
24#ifdef SUBPROJECT_rev1
25 #include "rev1/config.h"
26#endif
27
28#endif
diff --git a/keyboards/quefrency/i2c.c b/keyboards/quefrency/i2c.c
new file mode 100644
index 000000000..084c890c4
--- /dev/null
+++ b/keyboards/quefrency/i2c.c
@@ -0,0 +1,162 @@
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
21volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
22
23static volatile uint8_t slave_buffer_pos;
24static volatile bool slave_has_register_set = false;
25
26// Wait for an i2c operation to finish
27inline static
28void 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
38void 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
50uint8_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.
72void 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
83uint8_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
96uint8_t i2c_master_read(int ack) {
97 TWCR = (1<<TWINT) | (1<<TWEN) | (ack<<TWEA);
98
99 i2c_delay();
100 return TWDR;
101}
102
103void i2c_reset_state(void) {
104 TWCR = 0;
105}
106
107void 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
116ISR(TWI_vect);
117
118ISR(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/quefrency/i2c.h b/keyboards/quefrency/i2c.h
new file mode 100644
index 000000000..c15b6bc50
--- /dev/null
+++ b/keyboards/quefrency/i2c.h
@@ -0,0 +1,49 @@
1#ifndef I2C_H
2#define I2C_H
3
4#include <stdint.h>
5
6#ifndef F_CPU
7#define F_CPU 16000000UL
8#endif
9
10#define I2C_READ 1
11#define I2C_WRITE 0
12
13#define I2C_ACK 1
14#define I2C_NACK 0
15
16#define SLAVE_BUFFER_SIZE 0x10
17
18// i2c SCL clock frequency
19#define SCL_CLOCK 400000L
20
21extern volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
22
23void i2c_master_init(void);
24uint8_t i2c_master_start(uint8_t address);
25void i2c_master_stop(void);
26uint8_t i2c_master_write(uint8_t data);
27uint8_t i2c_master_read(int);
28void i2c_reset_state(void);
29void i2c_slave_init(uint8_t address);
30
31
32static inline unsigned char i2c_start_read(unsigned char addr) {
33 return i2c_master_start((addr << 1) | I2C_READ);
34}
35
36static inline unsigned char i2c_start_write(unsigned char addr) {
37 return i2c_master_start((addr << 1) | I2C_WRITE);
38}
39
40// from SSD1306 scrips
41extern unsigned char i2c_rep_start(unsigned char addr);
42extern void i2c_start_wait(unsigned char addr);
43extern unsigned char i2c_readAck(void);
44extern unsigned char i2c_readNak(void);
45extern unsigned char i2c_read(unsigned char ack);
46
47#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();
48
49#endif
diff --git a/keyboards/quefrency/info.json b/keyboards/quefrency/info.json
new file mode 100644
index 000000000..cfb72e9e0
--- /dev/null
+++ b/keyboards/quefrency/info.json
@@ -0,0 +1,86 @@
1{
2 "keyboard_name": "Quefrency",
3 "url": "https://keeb.io",
4 "maintainer": "nooges",
5 "width": 16,
6 "height": 5,
7 "layouts": {
8 "LAYOUT": {
9 "layout": [
10 {"label":"Esc", "x":0, "y":0},
11 {"label":"1", "x":1, "y":0},
12 {"label":"2", "x":2, "y":0},
13 {"label":"3", "x":3, "y":0},
14 {"label":"4", "x":4, "y":0},
15 {"label":"5", "x":5, "y":0},
16 {"label":"6", "x":6, "y":0},
17
18 {"label":"7", "x":8, "y":0},
19 {"label":"8", "x":9, "y":0},
20 {"label":"9", "x":10, "y":0},
21 {"label":"0", "x":11, "y":0},
22 {"label":"-", "x":12, "y":0},
23 {"label":"=", "x":13, "y":0},
24 {"label":"Backspace", "x":14, "y":0, "w":2},
25
26 {"label":"Tab", "x":0, "y":1, "w":1.5},
27 {"label":"Q", "x":1.5, "y":1},
28 {"label":"W", "x":2.5, "y":1},
29 {"label":"E", "x":3.5, "y":1},
30 {"label":"R", "x":4.5, "y":1},
31 {"label":"T", "x":5.5, "y":1},
32
33 {"label":"Y", "x":7.5, "y":1},
34 {"label":"U", "x":8.5, "y":1},
35 {"label":"I", "x":9.5, "y":1},
36 {"label":"O", "x":10.5, "y":1},
37 {"label":"P", "x":11.5, "y":1},
38 {"label":"[", "x":12.5, "y":1},
39 {"label":"]", "x":13.5, "y":1},
40 {"label":"Backslash", "x":14.5, "y":1, "w":1.5},
41
42 {"label":"Caps", "x":0, "y":2, "w":1.75},
43 {"label":"A", "x":1.75, "y":2},
44 {"label":"S", "x":2.75, "y":2},
45 {"label":"D", "x":3.75, "y":2},
46 {"label":"F", "x":4.75, "y":2},
47 {"label":"G", "x":5.75, "y":2},
48
49 {"label":"H", "x":7.75, "y":2},
50 {"label":"J", "x":8.75, "y":2},
51 {"label":"K", "x":9.75, "y":2},
52 {"label":"L", "x":10.75, "y":2},
53 {"label":";", "x":11.75, "y":2},
54 {"label":"'", "x":12.75, "y":2},
55 {"label":"Enter", "x":13.75, "y":2, "w":2.25},
56
57 {"label":"Shift", "x":0, "y":3, "w":2.25},
58 {"label":"Z", "x":2.25, "y":3},
59 {"label":"X", "x":3.25, "y":3},
60 {"label":"C", "x":4.25, "y":3},
61 {"label":"V", "x":5.25, "y":3},
62 {"label":"B", "x":6.25, "y":3},
63
64 {"label":"N", "x":8.25, "y":3},
65 {"label":"M", "x":9.25, "y":3},
66 {"label":"&lt;", "x":10.25, "y":3},
67 {"label":"&gt;", "x":11.25, "y":3},
68 {"label":"?", "x":12.25, "y":3},
69 {"label":"Shift", "x":13.25, "y":3, "w":1.75},
70 {"label":"Up", "x":15, "y":3},
71
72 {"label":"Ctrl", "x":0, "y":4, "w":1.25},
73 {"label":"Alt", "x":1.25, "y":4, "w":1.25},
74 {"label":"Gui", "x":2.5, "y":4, "w":1.25},
75 {"label":"Fn1", "x":3.75, "y":4, "w":1.25},
76 {"label":"Space", "x":5, "y":4, "w":2.25},
77
78 {"label":"Backspace", "x":8.25, "y":4, "w":2.75},
79 {"label":"Gui", "x":11, "y":4, "w":1.25},
80 {"label":"Left", "x":12.25, "y":4, "w":1.25},
81 {"label":"Down", "x":13.5, "y":4, "w":1.25},
82 {"label":"Right", "x":14.75, "y":4, "w":1.25}
83 ]
84 }
85 }
86}
diff --git a/keyboards/quefrency/keymaps/default/config.h b/keyboards/quefrency/keymaps/default/config.h
new file mode 100644
index 000000000..20e49c421
--- /dev/null
+++ b/keyboards/quefrency/keymaps/default/config.h
@@ -0,0 +1,31 @@
1/*
2This is the c configuration file for the keymap
3
4Copyright 2012 Jun Wako <wakojun@gmail.com>
5Copyright 2015 Jack Humbert
6
7This program is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#ifndef CONFIG_USER_H
22#define CONFIG_USER_H
23
24#include "config_common.h"
25
26/* Use I2C or Serial, not both */
27
28#define USE_SERIAL
29// #define USE_I2C
30
31#endif
diff --git a/keyboards/quefrency/keymaps/default/keymap.c b/keyboards/quefrency/keymaps/default/keymap.c
new file mode 100644
index 000000000..e6aba1a27
--- /dev/null
+++ b/keyboards/quefrency/keymaps/default/keymap.c
@@ -0,0 +1,35 @@
1#include QMK_KEYBOARD_H
2
3extern keymap_config_t keymap_config;
4
5// Each layer gets a name for readability, which is then used in the keymap matrix below.
6// The underscores don't mean anything - you can have a layer called STUFF or any other name.
7// Layer names don't all need to be of the same length, obviously, and you can also skip them
8// entirely and just use numbers.
9#define _BASE 0
10#define _FN1 1
11
12enum custom_keycodes {
13 QWERTY = SAFE_RANGE,
14};
15
16#define _______ KC_TRNS
17#define XXXXXXX KC_NO
18
19const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
20 [_BASE] = LAYOUT(
21 KC_GESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_DEL, KC_BSPC, \
22 KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, \
23 KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, \
24 KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, \
25 KC_LCTL, KC_LALT, KC_LGUI, MO(_FN1),KC_SPC, KC_BSPC, KC_RALT, KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT
26 ),
27
28 [_FN1] = LAYOUT(
29 KC_GESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_DEL, KC_BSPC, \
30 RGB_TOG, RGB_MOD, _______, KC_UP, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \
31 _______, _______, KC_LEFT, KC_DOWN, KC_RGHT, _______, _______, _______, _______, _______, _______, _______, _______,\
32 _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \
33 KC_TILD, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
34 )
35};
diff --git a/keyboards/quefrency/keymaps/default/rules.mk b/keyboards/quefrency/keymaps/default/rules.mk
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/keyboards/quefrency/keymaps/default/rules.mk
diff --git a/keyboards/quefrency/matrix.c b/keyboards/quefrency/matrix.c
new file mode 100644
index 000000000..fdeead7dc
--- /dev/null
+++ b/keyboards/quefrency/matrix.c
@@ -0,0 +1,467 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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#include <avr/io.h>
24#include "wait.h"
25#include "print.h"
26#include "debug.h"
27#include "util.h"
28#include "matrix.h"
29#include "split_util.h"
30#include "pro_micro.h"
31#include "config.h"
32#include "timer.h"
33#include "backlight.h"
34
35#ifdef USE_I2C
36# include "i2c.h"
37#else // USE_SERIAL
38# include "serial.h"
39#endif
40
41#ifndef DEBOUNCING_DELAY
42# define DEBOUNCING_DELAY 5
43#endif
44
45#if (DEBOUNCING_DELAY > 0)
46 static uint16_t debouncing_time;
47 static bool debouncing = false;
48#endif
49
50#if (MATRIX_COLS <= 8)
51# define print_matrix_header() print("\nr/c 01234567\n")
52# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
53# define matrix_bitpop(i) bitpop(matrix[i])
54# define ROW_SHIFTER ((uint8_t)1)
55#else
56# error "Currently only supports 8 COLS"
57#endif
58static matrix_row_t matrix_debouncing[MATRIX_ROWS];
59
60#define ERROR_DISCONNECT_COUNT 5
61
62#define SERIAL_LED_ADDR 0x00
63
64#define ROWS_PER_HAND (MATRIX_ROWS/2)
65
66static uint8_t error_count = 0;
67
68static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
69static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
70
71/* matrix state(1:on, 0:off) */
72static matrix_row_t matrix[MATRIX_ROWS];
73static matrix_row_t matrix_debouncing[MATRIX_ROWS];
74
75#if (DIODE_DIRECTION == COL2ROW)
76 static void init_cols(void);
77 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
78 static void unselect_rows(void);
79 static void select_row(uint8_t row);
80 static void unselect_row(uint8_t row);
81#elif (DIODE_DIRECTION == ROW2COL)
82 static void init_rows(void);
83 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
84 static void unselect_cols(void);
85 static void unselect_col(uint8_t col);
86 static void select_col(uint8_t col);
87#endif
88
89__attribute__ ((weak))
90void matrix_init_kb(void) {
91 matrix_init_user();
92}
93
94__attribute__ ((weak))
95void matrix_scan_kb(void) {
96 matrix_scan_user();
97}
98
99__attribute__ ((weak))
100void matrix_init_user(void) {
101}
102
103__attribute__ ((weak))
104void matrix_scan_user(void) {
105}
106
107inline
108uint8_t matrix_rows(void)
109{
110 return MATRIX_ROWS;
111}
112
113inline
114uint8_t matrix_cols(void)
115{
116 return MATRIX_COLS;
117}
118
119void matrix_init(void)
120{
121 debug_enable = true;
122 debug_matrix = true;
123 debug_mouse = true;
124 // initialize row and col
125 unselect_rows();
126 init_cols();
127
128 TX_RX_LED_INIT;
129
130 // initialize matrix state: all keys off
131 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
132 matrix[i] = 0;
133 matrix_debouncing[i] = 0;
134 }
135
136 matrix_init_quantum();
137
138}
139
140uint8_t _matrix_scan(void)
141{
142 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
143#if (DIODE_DIRECTION == COL2ROW)
144 // Set row, read cols
145 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
146# if (DEBOUNCING_DELAY > 0)
147 bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
148
149 if (matrix_changed) {
150 debouncing = true;
151 debouncing_time = timer_read();
152 }
153
154# else
155 read_cols_on_row(matrix+offset, current_row);
156# endif
157
158 }
159
160#elif (DIODE_DIRECTION == ROW2COL)
161 // Set col, read rows
162 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
163# if (DEBOUNCING_DELAY > 0)
164 bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
165 if (matrix_changed) {
166 debouncing = true;
167 debouncing_time = timer_read();
168 }
169# else
170 read_rows_on_col(matrix+offset, current_col);
171# endif
172
173 }
174#endif
175
176# if (DEBOUNCING_DELAY > 0)
177 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
178 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
179 matrix[i+offset] = matrix_debouncing[i+offset];
180 }
181 debouncing = false;
182 }
183# endif
184
185 return 1;
186}
187
188#ifdef USE_I2C
189
190// Get rows from other half over i2c
191int i2c_transaction(void) {
192 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
193
194 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
195 if (err) goto i2c_error;
196
197 // start of matrix stored at 0x00
198 err = i2c_master_write(0x00);
199 if (err) goto i2c_error;
200
201 // Start read
202 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
203 if (err) goto i2c_error;
204
205 if (!err) {
206 int i;
207 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
208 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
209 }
210 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
211 i2c_master_stop();
212 } else {
213i2c_error: // the cable is disconnceted, or something else went wrong
214 i2c_reset_state();
215 return err;
216 }
217
218 return 0;
219}
220
221#else // USE_SERIAL
222
223int serial_transaction(void) {
224 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
225
226 if (serial_update_buffers()) {
227 return 1;
228 }
229
230 for (int i = 0; i < ROWS_PER_HAND; ++i) {
231 matrix[slaveOffset+i] = serial_slave_buffer[i];
232 }
233
234#ifdef BACKLIGHT_ENABLE
235 // Write backlight level for slave to read
236 serial_master_buffer[SERIAL_LED_ADDR] = get_backlight_level();
237#endif
238 return 0;
239}
240#endif
241
242uint8_t matrix_scan(void)
243{
244 uint8_t ret = _matrix_scan();
245
246#ifdef USE_I2C
247 if( i2c_transaction() ) {
248#else // USE_SERIAL
249 if( serial_transaction() ) {
250#endif
251 // turn on the indicator led when halves are disconnected
252 TXLED1;
253
254 error_count++;
255
256 if (error_count > ERROR_DISCONNECT_COUNT) {
257 // reset other half if disconnected
258 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
259 for (int i = 0; i < ROWS_PER_HAND; ++i) {
260 matrix[slaveOffset+i] = 0;
261 }
262 }
263 } else {
264 // turn off the indicator led on no error
265 TXLED0;
266 error_count = 0;
267 }
268 matrix_scan_quantum();
269 return ret;
270}
271
272void matrix_slave_scan(void) {
273 _matrix_scan();
274
275 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
276
277#ifdef USE_I2C
278 for (int i = 0; i < ROWS_PER_HAND; ++i) {
279 i2c_slave_buffer[i] = matrix[offset+i];
280 }
281#else // USE_SERIAL
282 for (int i = 0; i < ROWS_PER_HAND; ++i) {
283 serial_slave_buffer[i] = matrix[offset+i];
284 }
285
286#ifdef BACKLIGHT_ENABLE
287 // Read backlight level sent from master and update level on slave
288 backlight_set(serial_master_buffer[SERIAL_LED_ADDR]);
289#endif
290#endif
291}
292
293bool matrix_is_modified(void)
294{
295 if (debouncing) return false;
296 return true;
297}
298
299inline
300bool matrix_is_on(uint8_t row, uint8_t col)
301{
302 return (matrix[row] & ((matrix_row_t)1<<col));
303}
304
305inline
306matrix_row_t matrix_get_row(uint8_t row)
307{
308 return matrix[row];
309}
310
311void matrix_print(void)
312{
313 print("\nr/c 0123456789ABCDEF\n");
314 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
315 phex(row); print(": ");
316 pbin_reverse16(matrix_get_row(row));
317 print("\n");
318 }
319}
320
321uint8_t matrix_key_count(void)
322{
323 uint8_t count = 0;
324 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
325 count += bitpop16(matrix[i]);
326 }
327 return count;
328}
329
330#if (DIODE_DIRECTION == COL2ROW)
331
332static void init_cols(void)
333{
334 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
335 uint8_t pin = col_pins[x];
336 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
337 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
338 }
339}
340
341static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
342{
343 // Store last value of row prior to reading
344 matrix_row_t last_row_value = current_matrix[current_row];
345
346 // Clear data in matrix row
347 current_matrix[current_row] = 0;
348
349 // Select row and wait for row selecton to stabilize
350 select_row(current_row);
351 wait_us(30);
352
353 // For each col...
354 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
355
356 // Select the col pin to read (active low)
357 uint8_t pin = col_pins[col_index];
358 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
359
360 // Populate the matrix row with the state of the col pin
361 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
362 }
363
364 // Unselect row
365 unselect_row(current_row);
366
367 return (last_row_value != current_matrix[current_row]);
368}
369
370static void select_row(uint8_t row)
371{
372 uint8_t pin = row_pins[row];
373 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
374 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
375}
376
377static void unselect_row(uint8_t row)
378{
379 uint8_t pin = row_pins[row];
380 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
381 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
382}
383
384static void unselect_rows(void)
385{
386 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
387 uint8_t pin = row_pins[x];
388 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
389 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
390 }
391}
392
393#elif (DIODE_DIRECTION == ROW2COL)
394
395static void init_rows(void)
396{
397 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
398 uint8_t pin = row_pins[x];
399 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
400 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
401 }
402}
403
404static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
405{
406 bool matrix_changed = false;
407
408 // Select col and wait for col selecton to stabilize
409 select_col(current_col);
410 wait_us(30);
411
412 // For each row...
413 for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
414 {
415
416 // Store last value of row prior to reading
417 matrix_row_t last_row_value = current_matrix[row_index];
418
419 // Check row pin state
420 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
421 {
422 // Pin LO, set col bit
423 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
424 }
425 else
426 {
427 // Pin HI, clear col bit
428 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
429 }
430
431 // Determine if the matrix changed state
432 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
433 {
434 matrix_changed = true;
435 }
436 }
437
438 // Unselect col
439 unselect_col(current_col);
440
441 return matrix_changed;
442}
443
444static void select_col(uint8_t col)
445{
446 uint8_t pin = col_pins[col];
447 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
448 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
449}
450
451static void unselect_col(uint8_t col)
452{
453 uint8_t pin = col_pins[col];
454 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
455 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
456}
457
458static void unselect_cols(void)
459{
460 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
461 uint8_t pin = col_pins[x];
462 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
463 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
464 }
465}
466
467#endif
diff --git a/keyboards/quefrency/quefrency.c b/keyboards/quefrency/quefrency.c
new file mode 100644
index 000000000..d713a0ff3
--- /dev/null
+++ b/keyboards/quefrency/quefrency.c
@@ -0,0 +1 @@
#include "quefrency.h"
diff --git a/keyboards/quefrency/quefrency.h b/keyboards/quefrency/quefrency.h
new file mode 100644
index 000000000..7a791230d
--- /dev/null
+++ b/keyboards/quefrency/quefrency.h
@@ -0,0 +1,23 @@
1#ifndef QUEFRENCY_H
2#define QUEFRENCY_H
3
4#include "quantum.h"
5
6#ifdef KEYBOARD_quefrency_rev1
7 #include "rev1.h"
8#endif
9
10// Used to create a keymap using only KC_ prefixed keys
11#define LAYOUT_kc( \
12 LA1, LA2, LA3, LA4, LA5, LA6, RA1, RA2, RA3, RA4, RA5, RA6, RA7, \
13 LB1, LB2, LB3, LB4, LB5, LB6, RB1, RB2, RB3, RB4, RB5, RB7, \
14 LC1, LC2, LC3, LC4, LC5, LC6, RC1, RC3, RC4, RC5, RC6, RC7, \
15 LD1, LD2, LD3, LD4, LD5, RD1, RD4, RD5, RD6, RD7 \
16 ) \
17 LAYOUT( \
18 KC_##LA1, KC_##LA2, KC_##LA3, KC_##LA4, KC_##LA5, KC_##LA6, KC_##RA1, KC_##RA2, KC_##RA3, KC_##RA4, KC_##RA5, KC_##RA6, KC_##RA7, \
19 KC_##LB1, KC_##LB2, KC_##LB3, KC_##LB4, KC_##LB5, KC_##LB6, KC_##RB1, KC_##RB2, KC_##RB3, KC_##RB4, KC_##RB5, KC_##RB7, \
20 KC_##LC1, KC_##LC2, KC_##LC3, KC_##LC4, KC_##LC5, KC_##LC6, KC_##RC1, KC_##RC3, KC_##RC4, KC_##RC5, KC_##RC6, KC_##RC7, \
21 KC_##LD1, KC_##LD2, KC_##LD3, KC_##LD4, KC_##LD5, KC_##RD1, KC_##RD4, KC_##RD5, KC_##RD6, KC_##RD7 \
22 )
23#endif
diff --git a/keyboards/quefrency/readme.md b/keyboards/quefrency/readme.md
new file mode 100644
index 000000000..33681c1a7
--- /dev/null
+++ b/keyboards/quefrency/readme.md
@@ -0,0 +1,20 @@
1Quefrency
2=========
3
4A split 60% staggered keyboard made and sold by Keebio. [More info at Keebio](https://keeb.io).
5
6Keyboard Maintainer: [Bakingpy/nooges](https://github.com/nooges)
7Hardware Supported: Pro Micro
8Hardware Availability: [Keebio](https://keeb.io/)
9
10Make example for this keyboard (after setting up your build environment):
11
12 make quefrency/rev1:default
13
14Example of flashing this keyboard:
15
16 make quefrency/rev1:default:avrdude
17
18See [build environment setup](https://docs.qmk.fm/build_environment_setup.html) then the [make instructions](https://docs.qmk.fm/make_instructions.html) for more information.
19
20A build guide for this keyboard can be found here: [Keebio Build Guides](https://docs.keeb.io)
diff --git a/keyboards/quefrency/rev1/config.h b/keyboards/quefrency/rev1/config.h
new file mode 100644
index 000000000..c24cf5ab3
--- /dev/null
+++ b/keyboards/quefrency/rev1/config.h
@@ -0,0 +1,84 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3Copyright 2015 Jack Humbert
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#ifndef REV1_CONFIG_H
20#define REV1_CONFIG_H
21
22#include "../config.h"
23
24/* USB Device descriptor parameter */
25#define VENDOR_ID 0xCB10
26#define PRODUCT_ID 0x1257
27#define DEVICE_VER 0x0100
28#define MANUFACTURER Keebio
29#define PRODUCT Quefrency
30#define DESCRIPTION Split 60 percent staggered keyboard
31
32/* key matrix size */
33// Rows are doubled-up
34#define MATRIX_ROWS 10
35#define MATRIX_COLS 8
36
37// wiring of each half
38#define MATRIX_ROW_PINS { F4, D4, D7, E6, B4 }
39#define MATRIX_COL_PINS { F5, F6, F7, B1, B3, B2, B6, C6 }
40
41/* define if matrix has ghost */
42//#define MATRIX_HAS_GHOST
43
44/* Set 0 if debouncing isn't needed */
45#define DEBOUNCING_DELAY 5
46
47/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
48#define LOCKING_SUPPORT_ENABLE
49/* Locking resynchronize hack */
50#define LOCKING_RESYNC_ENABLE
51
52/* key combination for command */
53#define IS_COMMAND() ( \
54 keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)) \
55)
56
57/* ws2812 RGB LED */
58#define RGB_DI_PIN D3
59#define RGBLIGHT_TIMER
60#define RGBLIGHT_ANIMATIONS
61#define RGBLED_NUM 16 // Number of LEDs
62#define ws2812_PORTREG PORTD
63#define ws2812_DDRREG DDRD
64
65/*
66 * Feature disable options
67 * These options are also useful to firmware size reduction.
68 */
69
70/* disable debug print */
71// #define NO_DEBUG
72
73/* disable print */
74// #define NO_PRINT
75
76/* disable action features */
77//#define NO_ACTION_LAYER
78//#define NO_ACTION_TAPPING
79//#define NO_ACTION_ONESHOT
80//#define NO_ACTION_MACRO
81//#define NO_ACTION_FUNCTION
82
83
84#endif
diff --git a/keyboards/quefrency/rev1/rev1.c b/keyboards/quefrency/rev1/rev1.c
new file mode 100644
index 000000000..06fbfa25e
--- /dev/null
+++ b/keyboards/quefrency/rev1/rev1.c
@@ -0,0 +1,7 @@
1#include "quefrency.h"
2
3
4void matrix_init_kb(void) {
5 matrix_init_user();
6};
7
diff --git a/keyboards/quefrency/rev1/rev1.h b/keyboards/quefrency/rev1/rev1.h
new file mode 100644
index 000000000..44476354b
--- /dev/null
+++ b/keyboards/quefrency/rev1/rev1.h
@@ -0,0 +1,37 @@
1#ifndef REV1_H
2#define REV1_H
3
4#include "../quefrency.h"
5
6#include "quantum.h"
7
8
9#ifdef USE_I2C
10#include <stddef.h>
11#ifdef __AVR__
12 #include <avr/io.h>
13 #include <avr/interrupt.h>
14#endif
15#endif
16
17#define LAYOUT( \
18 LA1, LA2, LA3, LA4, LA5, LA6, LA7, RA1, RA2, RA3, RA4, RA5, RA6, RA7, RA8, \
19 LB1, LB2, LB3, LB4, LB5, LB6, RB1, RB2, RB3, RB4, RB5, RB6, RB7, RB8, \
20 LC1, LC2, LC3, LC4, LC5, LC6, RC1, RC2, RC3, RC4, RC5, RC6, RC8, \
21 LD1, LD3, LD4, LD5, LD6, LD7, RD1, RD2, RD3, RD4, RD6, RD7, RD8, \
22 LE1, LE2, LE3, LE5, LE7, RE1, RE4, RE5, RE6, RE7, RE8 \
23 ) \
24 { \
25 { LA1, LA2, LA3, LA4, LA5, LA6, LA7, KC_NO }, \
26 { LB1, LB2, LB3, LB4, LB5, LB6, KC_NO, KC_NO }, \
27 { LC1, LC2, LC3, LC4, LC5, LC6, KC_NO, KC_NO }, \
28 { LD1, KC_NO, LD3, LD4, LD5, LD6, LD7, KC_NO }, \
29 { LE1, LE2, LE3, KC_NO, LE5, KC_NO, LE7, KC_NO }, \
30 { RA1, RA2, RA3, RA4, RA5, RA6, RA7, RA8 }, \
31 { RB1, RB2, RB3, RB4, RB5, RB6, RB7, RB8 }, \
32 { RC1, RC2, RC3, RC4, RC5, RC6, KC_NO, RC8 }, \
33 { RD1, RD2, RD3, RD4, KC_NO, RD6, RD7, RD8 }, \
34 { RE1, KC_NO, KC_NO, RE4, RE5, RE6, RE7, RE8 } \
35 }
36
37#endif
diff --git a/keyboards/quefrency/rev1/rules.mk b/keyboards/quefrency/rev1/rules.mk
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/keyboards/quefrency/rev1/rules.mk
diff --git a/keyboards/quefrency/rules.mk b/keyboards/quefrency/rules.mk
new file mode 100644
index 000000000..93ccd2b05
--- /dev/null
+++ b/keyboards/quefrency/rules.mk
@@ -0,0 +1,74 @@
1SRC += matrix.c \
2 i2c.c \
3 split_util.c \
4 serial.c
5
6# MCU name
7#MCU = at90usb1287
8MCU = atmega32u4
9
10# Processor frequency.
11# This will define a symbol, F_CPU, in all source code files equal to the
12# processor frequency in Hz. You can then use this symbol in your source code to
13# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
14# automatically to create a 32-bit value in your source code.
15#
16# This will be an integer division of F_USB below, as it is sourced by
17# F_USB after it has run through any CPU prescalers. Note that this value
18# does not *change* the processor frequency - it should merely be updated to
19# reflect the processor speed set externally so that the code can use accurate
20# software delays.
21F_CPU = 16000000
22
23#
24# LUFA specific
25#
26# Target architecture (see library "Board Types" documentation).
27ARCH = AVR8
28
29# Input clock frequency.
30# This will define a symbol, F_USB, in all source code files equal to the
31# input clock frequency (before any prescaling is performed) in Hz. This value may
32# differ from F_CPU if prescaling is used on the latter, and is required as the
33# raw input clock is fed directly to the PLL sections of the AVR for high speed
34# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
35# at the end, this will be done automatically to create a 32-bit value in your
36# source code.
37#
38# If no clock division is performed on the input clock inside the AVR (via the
39# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
40F_USB = $(F_CPU)
41
42# Bootloader
43# This definition is optional, and if your keyboard supports multiple bootloaders of
44# different sizes, comment this out, and the correct address will be loaded
45# automatically (+60). See bootloader.mk for all options.
46BOOTLOADER = caterina
47
48# Interrupt driven control endpoint task(+60)
49OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT
50
51# Build Options
52# change to "no" to disable the options, or define them in the Makefile in
53# the appropriate keymap folder that will get included automatically
54#
55BOOTMAGIC_ENABLE = no # Virtual DIP switch configuration(+1000)
56MOUSEKEY_ENABLE = no # Mouse keys(+4700)
57EXTRAKEY_ENABLE = yes # Audio control and System control(+450)
58CONSOLE_ENABLE = no # Console for debug(+400)
59COMMAND_ENABLE = yes # Commands for debug and configuration
60NKRO_ENABLE = no # Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
61BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality
62MIDI_ENABLE = no # MIDI controls
63AUDIO_ENABLE = no # Audio output on port C6
64UNICODE_ENABLE = no # Unicode
65BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
66RGBLIGHT_ENABLE = yes # Enable WS2812 RGB underlight. Do not enable this with audio at the same time.
67SUBPROJECT_rev1 = yes
68USE_I2C = yes
69# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
70SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
71
72CUSTOM_MATRIX = yes
73
74DEFAULT_FOLDER = quefrency/rev1
diff --git a/keyboards/quefrency/serial.c b/keyboards/quefrency/serial.c
new file mode 100644
index 000000000..74bcbb6bf
--- /dev/null
+++ b/keyboards/quefrency/serial.c
@@ -0,0 +1,228 @@
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
21uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
22uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
23
24#define SLAVE_DATA_CORRUPT (1<<0)
25volatile uint8_t status = 0;
26
27inline static
28void serial_delay(void) {
29 _delay_us(SERIAL_DELAY);
30}
31
32inline static
33void serial_output(void) {
34 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
35}
36
37// make the serial pin an input with pull-up resistor
38inline static
39void serial_input(void) {
40 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
41 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
42}
43
44inline static
45uint8_t serial_read_pin(void) {
46 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
47}
48
49inline static
50void serial_low(void) {
51 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
52}
53
54inline static
55void serial_high(void) {
56 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
57}
58
59void serial_master_init(void) {
60 serial_output();
61 serial_high();
62}
63
64void serial_slave_init(void) {
65 serial_input();
66
67 // Enable INT0
68 EIMSK |= _BV(INT0);
69 // Trigger on falling edge of INT0
70 EICRA &= ~(_BV(ISC00) | _BV(ISC01));
71}
72
73// Used by the master to synchronize timing with the slave.
74static
75void sync_recv(void) {
76 serial_input();
77 // This shouldn't hang if the slave disconnects because the
78 // serial line will float to high if the slave does disconnect.
79 while (!serial_read_pin());
80 serial_delay();
81}
82
83// Used by the slave to send a synchronization signal to the master.
84static
85void sync_send(void) {
86 serial_output();
87
88 serial_low();
89 serial_delay();
90
91 serial_high();
92}
93
94// Reads a byte from the serial line
95static
96uint8_t serial_read_byte(void) {
97 uint8_t byte = 0;
98 serial_input();
99 for ( uint8_t i = 0; i < 8; ++i) {
100 byte = (byte << 1) | serial_read_pin();
101 serial_delay();
102 _delay_us(1);
103 }
104
105 return byte;
106}
107
108// Sends a byte with MSB ordering
109static
110void serial_write_byte(uint8_t data) {
111 uint8_t b = 8;
112 serial_output();
113 while( b-- ) {
114 if(data & (1 << b)) {
115 serial_high();
116 } else {
117 serial_low();
118 }
119 serial_delay();
120 }
121}
122
123// interrupt handle to be used by the slave device
124ISR(SERIAL_PIN_INTERRUPT) {
125 sync_send();
126
127 uint8_t checksum = 0;
128 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
129 serial_write_byte(serial_slave_buffer[i]);
130 sync_send();
131 checksum += serial_slave_buffer[i];
132 }
133 serial_write_byte(checksum);
134 sync_send();
135
136 // wait for the sync to finish sending
137 serial_delay();
138
139 // read the middle of pulses
140 _delay_us(SERIAL_DELAY/2);
141
142 uint8_t checksum_computed = 0;
143 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
144 serial_master_buffer[i] = serial_read_byte();
145 sync_send();
146 checksum_computed += serial_master_buffer[i];
147 }
148 uint8_t checksum_received = serial_read_byte();
149 sync_send();
150
151 serial_input(); // end transaction
152
153 if ( checksum_computed != checksum_received ) {
154 status |= SLAVE_DATA_CORRUPT;
155 } else {
156 status &= ~SLAVE_DATA_CORRUPT;
157 }
158}
159
160inline
161bool serial_slave_DATA_CORRUPT(void) {
162 return status & SLAVE_DATA_CORRUPT;
163}
164
165// Copies the serial_slave_buffer to the master and sends the
166// serial_master_buffer to the slave.
167//
168// Returns:
169// 0 => no error
170// 1 => slave did not respond
171int serial_update_buffers(void) {
172 // this code is very time dependent, so we need to disable interrupts
173 cli();
174
175 // signal to the slave that we want to start a transaction
176 serial_output();
177 serial_low();
178 _delay_us(1);
179
180 // wait for the slaves response
181 serial_input();
182 serial_high();
183 _delay_us(SERIAL_DELAY);
184
185 // check if the slave is present
186 if (serial_read_pin()) {
187 // slave failed to pull the line low, assume not present
188 sei();
189 return 1;
190 }
191
192 // if the slave is present syncronize with it
193 sync_recv();
194
195 uint8_t checksum_computed = 0;
196 // receive data from the slave
197 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
198 serial_slave_buffer[i] = serial_read_byte();
199 sync_recv();
200 checksum_computed += serial_slave_buffer[i];
201 }
202 uint8_t checksum_received = serial_read_byte();
203 sync_recv();
204
205 if (checksum_computed != checksum_received) {
206 sei();
207 return 1;
208 }
209
210 uint8_t checksum = 0;
211 // send data to the slave
212 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
213 serial_write_byte(serial_master_buffer[i]);
214 sync_recv();
215 checksum += serial_master_buffer[i];
216 }
217 serial_write_byte(checksum);
218 sync_recv();
219
220 // always, release the line when not in use
221 serial_output();
222 serial_high();
223
224 sei();
225 return 0;
226}
227
228#endif
diff --git a/keyboards/quefrency/serial.h b/keyboards/quefrency/serial.h
new file mode 100644
index 000000000..15fe4db7b
--- /dev/null
+++ b/keyboards/quefrency/serial.h
@@ -0,0 +1,26 @@
1#ifndef MY_SERIAL_H
2#define MY_SERIAL_H
3
4#include "config.h"
5#include <stdbool.h>
6
7/* TODO: some defines for interrupt setup */
8#define SERIAL_PIN_DDR DDRD
9#define SERIAL_PIN_PORT PORTD
10#define SERIAL_PIN_INPUT PIND
11#define SERIAL_PIN_MASK _BV(PD0)
12#define SERIAL_PIN_INTERRUPT INT0_vect
13
14#define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2
15#define SERIAL_MASTER_BUFFER_LENGTH 1
16
17// Buffers for master - slave communication
18extern volatile uint8_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH];
19extern volatile uint8_t serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH];
20
21void serial_master_init(void);
22void serial_slave_init(void);
23int serial_update_buffers(void);
24bool serial_slave_data_corrupt(void);
25
26#endif
diff --git a/keyboards/quefrency/split_util.c b/keyboards/quefrency/split_util.c
new file mode 100644
index 000000000..2704e30e0
--- /dev/null
+++ b/keyboards/quefrency/split_util.c
@@ -0,0 +1,80 @@
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#include "timer.h"
12#include "pincontrol.h"
13
14#ifdef USE_I2C
15# include "i2c.h"
16#else
17# include "serial.h"
18#endif
19
20volatile bool isLeftHand = true;
21
22static void setup_handedness(void) {
23 // Test D2 pin for handedness, if D2 is grounded, it's the right hand
24 pinMode(D2, PinDirectionInput);
25 isLeftHand = digitalRead(D2);
26}
27
28static void keyboard_master_setup(void) {
29#ifdef USE_I2C
30 i2c_master_init();
31#ifdef SSD1306OLED
32 matrix_master_OLED_init();
33#endif
34#else
35 serial_master_init();
36#endif
37}
38
39static void keyboard_slave_setup(void) {
40 timer_init();
41#ifdef USE_I2C
42 i2c_slave_init(SLAVE_I2C_ADDRESS);
43#else
44 serial_slave_init();
45#endif
46}
47
48bool has_usb(void) {
49 USBCON |= (1 << OTGPADE); //enables VBUS pad
50 _delay_us(5);
51 return (USBSTA & (1<<VBUS)); //checks state of VBUS
52}
53
54void split_keyboard_setup(void) {
55 setup_handedness();
56
57 if (has_usb()) {
58 keyboard_master_setup();
59 } else {
60 keyboard_slave_setup();
61 }
62 sei();
63}
64
65void keyboard_slave_loop(void) {
66 matrix_init();
67
68 while (1) {
69 matrix_slave_scan();
70 }
71}
72
73// this code runs before the usb and keyboard is initialized
74void matrix_setup(void) {
75 split_keyboard_setup();
76
77 if (!has_usb()) {
78 keyboard_slave_loop();
79 }
80}
diff --git a/keyboards/quefrency/split_util.h b/keyboards/quefrency/split_util.h
new file mode 100644
index 000000000..595a0659e
--- /dev/null
+++ b/keyboards/quefrency/split_util.h
@@ -0,0 +1,20 @@
1#ifndef SPLIT_KEYBOARD_UTIL_H
2#define SPLIT_KEYBOARD_UTIL_H
3
4#include <stdbool.h>
5#include "eeconfig.h"
6
7#define SLAVE_I2C_ADDRESS 0x32
8
9extern volatile bool isLeftHand;
10
11// slave version of matix scan, defined in matrix.c
12void matrix_slave_scan(void);
13
14void split_keyboard_setup(void);
15bool has_usb(void);
16void keyboard_slave_loop(void);
17
18void matrix_master_OLED_init (void);
19
20#endif