aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzvecr <git@zvecr.com>2018-12-14 22:25:32 +0000
committerDrashna Jaelre <drashna@live.com>2018-12-14 14:25:32 -0800
commitbb5262de0790ed3f4f91de20fad6e13fc0ffddc5 (patch)
tree48a73db53aabbb2b6b4e8b4c055cd292a6f7966d
parentcb149650efd07cbdc3cacb1fe2eb58f17eee5965 (diff)
downloadqmk_firmware-bb5262de0790ed3f4f91de20fad6e13fc0ffddc5.tar.gz
qmk_firmware-bb5262de0790ed3f4f91de20fad6e13fc0ffddc5.zip
Keyboard: Refactor fourier to use split common code (#4582)
* Refactor fourier to use split common code * Refactor fourier to use split common code - fix handedness when not using EE_HANDS or MASTER_LEFT * add SOFT_SERIAL_PIN config
-rw-r--r--keyboards/fourier/config.h9
-rw-r--r--keyboards/fourier/fourier.h8
-rw-r--r--keyboards/fourier/i2c.c162
-rw-r--r--keyboards/fourier/i2c.h49
-rw-r--r--keyboards/fourier/keymaps/default/config.h8
-rw-r--r--keyboards/fourier/keymaps/default/keymap.c4
-rw-r--r--keyboards/fourier/keymaps/default/rules.mk3
-rw-r--r--keyboards/fourier/matrix.c467
-rw-r--r--keyboards/fourier/rev1/config.h14
-rw-r--r--keyboards/fourier/rev1/rev1.c2
-rw-r--r--keyboards/fourier/rev1/rev1.h17
-rw-r--r--keyboards/fourier/rules.mk14
-rw-r--r--keyboards/fourier/serial.c228
-rw-r--r--keyboards/fourier/serial.h26
-rw-r--r--keyboards/fourier/split_util.c80
-rw-r--r--keyboards/fourier/split_util.h20
16 files changed, 17 insertions, 1094 deletions
diff --git a/keyboards/fourier/config.h b/keyboards/fourier/config.h
index 8f0524f97..cfb6bf4ff 100644
--- a/keyboards/fourier/config.h
+++ b/keyboards/fourier/config.h
@@ -16,13 +16,6 @@ You should have received a copy of the GNU General Public License
16along with this program. If not, see <http://www.gnu.org/licenses/>. 16along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/ 17*/
18 18
19#ifndef CONFIG_H 19#pragma once
20#define CONFIG_H
21 20
22#include "config_common.h" 21#include "config_common.h"
23
24#ifdef SUBPROJECT_rev1
25 #include "rev1/config.h"
26#endif
27
28#endif
diff --git a/keyboards/fourier/fourier.h b/keyboards/fourier/fourier.h
index 4e9797b35..1b4585c82 100644
--- a/keyboards/fourier/fourier.h
+++ b/keyboards/fourier/fourier.h
@@ -1,12 +1,11 @@
1#ifndef FOURIER_H 1#pragma once
2#define FOURIER_H
3
4#include "quantum.h"
5 2
6#ifdef KEYBOARD_fourier_rev1 3#ifdef KEYBOARD_fourier_rev1
7 #include "rev1.h" 4 #include "rev1.h"
8#endif 5#endif
9 6
7#include "quantum.h"
8
10// Used to create a keymap using only KC_ prefixed keys 9// Used to create a keymap using only KC_ prefixed keys
11#define LAYOUT_kc( \ 10#define LAYOUT_kc( \
12 LA1, LA2, LA3, LA4, LA5, LA6, RA1, RA2, RA3, RA4, RA5, RA6, RA7, \ 11 LA1, LA2, LA3, LA4, LA5, LA6, RA1, RA2, RA3, RA4, RA5, RA6, RA7, \
@@ -20,4 +19,3 @@
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, \ 19 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 \ 20 KC_##LD1, KC_##LD2, KC_##LD3, KC_##LD4, KC_##LD5, KC_##RD1, KC_##RD4, KC_##RD5, KC_##RD6, KC_##RD7 \
22 ) 21 )
23#endif
diff --git a/keyboards/fourier/i2c.c b/keyboards/fourier/i2c.c
deleted file mode 100644
index 084c890c4..000000000
--- a/keyboards/fourier/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
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/fourier/i2c.h b/keyboards/fourier/i2c.h
deleted file mode 100644
index c15b6bc50..000000000
--- a/keyboards/fourier/i2c.h
+++ /dev/null
@@ -1,49 +0,0 @@
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/fourier/keymaps/default/config.h b/keyboards/fourier/keymaps/default/config.h
index 20e49c421..18f4c1f7a 100644
--- a/keyboards/fourier/keymaps/default/config.h
+++ b/keyboards/fourier/keymaps/default/config.h
@@ -18,14 +18,8 @@ You should have received a copy of the GNU General Public License
18along with this program. If not, see <http://www.gnu.org/licenses/>. 18along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/ 19*/
20 20
21#ifndef CONFIG_USER_H 21#pragma once
22#define CONFIG_USER_H
23
24#include "config_common.h"
25 22
26/* Use I2C or Serial, not both */ 23/* Use I2C or Serial, not both */
27
28#define USE_SERIAL 24#define USE_SERIAL
29// #define USE_I2C 25// #define USE_I2C
30
31#endif
diff --git a/keyboards/fourier/keymaps/default/keymap.c b/keyboards/fourier/keymaps/default/keymap.c
index 9c2013776..a08f27b7c 100644
--- a/keyboards/fourier/keymaps/default/keymap.c
+++ b/keyboards/fourier/keymaps/default/keymap.c
@@ -1,7 +1,5 @@
1#include QMK_KEYBOARD_H 1#include QMK_KEYBOARD_H
2 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. 3// 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. 4// 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 5// Layer names don't all need to be of the same length, obviously, and you can also skip them
@@ -14,8 +12,6 @@ enum custom_keycodes {
14 QWERTY = SAFE_RANGE, 12 QWERTY = SAFE_RANGE,
15}; 13};
16 14
17#define _______ KC_TRNS
18#define XXXXXXX KC_NO
19#define KC_FN1 MO(_FN1) 15#define KC_FN1 MO(_FN1)
20#define KC_FN2 MO(_FN2) 16#define KC_FN2 MO(_FN2)
21#define KC_SPFN1 LT(_FN1, KC_SPACE) 17#define KC_SPFN1 LT(_FN1, KC_SPACE)
diff --git a/keyboards/fourier/keymaps/default/rules.mk b/keyboards/fourier/keymaps/default/rules.mk
index 457a3d01d..e69de29bb 100644
--- a/keyboards/fourier/keymaps/default/rules.mk
+++ b/keyboards/fourier/keymaps/default/rules.mk
@@ -1,3 +0,0 @@
1ifndef QUANTUM_DIR
2 include ../../../../Makefile
3endif
diff --git a/keyboards/fourier/matrix.c b/keyboards/fourier/matrix.c
deleted file mode 100644
index fdeead7dc..000000000
--- a/keyboards/fourier/matrix.c
+++ /dev/null
@@ -1,467 +0,0 @@
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/fourier/rev1/config.h b/keyboards/fourier/rev1/config.h
index 3bd67228c..d9913d7de 100644
--- a/keyboards/fourier/rev1/config.h
+++ b/keyboards/fourier/rev1/config.h
@@ -16,10 +16,7 @@ You should have received a copy of the GNU General Public License
16along with this program. If not, see <http://www.gnu.org/licenses/>. 16along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/ 17*/
18 18
19#ifndef REV1_CONFIG_H 19#pragma once
20#define REV1_CONFIG_H
21
22#include "../config.h"
23 20
24/* USB Device descriptor parameter */ 21/* USB Device descriptor parameter */
25#define VENDOR_ID 0xCB10 22#define VENDOR_ID 0xCB10
@@ -38,6 +35,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
38#define MATRIX_ROW_PINS { F4, D7, E6, B4 } 35#define MATRIX_ROW_PINS { F4, D7, E6, B4 }
39#define MATRIX_COL_PINS { F5, F6, F7, B1, B3, B2, B6 } 36#define MATRIX_COL_PINS { F5, F6, F7, B1, B3, B2, B6 }
40 37
38/* Split Defines */
39#define SPLIT_HAND_PIN D2
40/* serial.c configuration for split keyboard */
41#define SOFT_SERIAL_PIN D0
42
41/* define if matrix has ghost */ 43/* define if matrix has ghost */
42//#define MATRIX_HAS_GHOST 44//#define MATRIX_HAS_GHOST
43 45
@@ -56,7 +58,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
56 58
57/* ws2812 RGB LED */ 59/* ws2812 RGB LED */
58#define RGB_DI_PIN D3 60#define RGB_DI_PIN D3
59
60#define RGBLIGHT_ANIMATIONS 61#define RGBLIGHT_ANIMATIONS
61#define RGBLED_NUM 14 // Number of LEDs 62#define RGBLED_NUM 14 // Number of LEDs
62 63
@@ -77,6 +78,3 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
77//#define NO_ACTION_ONESHOT 78//#define NO_ACTION_ONESHOT
78//#define NO_ACTION_MACRO 79//#define NO_ACTION_MACRO
79//#define NO_ACTION_FUNCTION 80//#define NO_ACTION_FUNCTION
80
81
82#endif
diff --git a/keyboards/fourier/rev1/rev1.c b/keyboards/fourier/rev1/rev1.c
index 61d7139d6..1e662e7b8 100644
--- a/keyboards/fourier/rev1/rev1.c
+++ b/keyboards/fourier/rev1/rev1.c
@@ -1,7 +1,5 @@
1#include "fourier.h" 1#include "fourier.h"
2 2
3
4void matrix_init_kb(void) { 3void matrix_init_kb(void) {
5 matrix_init_user(); 4 matrix_init_user();
6}; 5};
7
diff --git a/keyboards/fourier/rev1/rev1.h b/keyboards/fourier/rev1/rev1.h
index 9ef9b9233..ac889462b 100644
--- a/keyboards/fourier/rev1/rev1.h
+++ b/keyboards/fourier/rev1/rev1.h
@@ -1,19 +1,8 @@
1#ifndef REV1_H 1#pragma once
2#define REV1_H
3
4#include "../fourier.h"
5 2
3#include "fourier.h"
6#include "quantum.h" 4#include "quantum.h"
7 5
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( \ 6#define LAYOUT( \
18 LA1, LA2, LA3, LA4, LA5, LA6, RA1, RA2, RA3, RA4, RA5, RA6, RA7, \ 7 LA1, LA2, LA3, LA4, LA5, LA6, RA1, RA2, RA3, RA4, RA5, RA6, RA7, \
19 LB1, LB2, LB3, LB4, LB5, LB6, RB1, RB2, RB3, RB4, RB5, RB7, \ 8 LB1, LB2, LB3, LB4, LB5, LB6, RB1, RB2, RB3, RB4, RB5, RB7, \
@@ -30,5 +19,3 @@
30 { RC1, KC_NO, RC3, RC4, RC5, RC6, RC7}, \ 19 { RC1, KC_NO, RC3, RC4, RC5, RC6, RC7}, \
31 { RD1, KC_NO, KC_NO, RD4, RD5, RD6, RD7} \ 20 { RD1, KC_NO, KC_NO, RD4, RD5, RD6, RD7} \
32 } 21 }
33
34#endif
diff --git a/keyboards/fourier/rules.mk b/keyboards/fourier/rules.mk
index 9ec05e874..93935e02e 100644
--- a/keyboards/fourier/rules.mk
+++ b/keyboards/fourier/rules.mk
@@ -1,10 +1,4 @@
1SRC += matrix.c \
2 i2c.c \
3 split_util.c \
4 serial.c
5
6# MCU name 1# MCU name
7#MCU = at90usb1287
8MCU = atmega32u4 2MCU = atmega32u4
9 3
10# Processor frequency. 4# Processor frequency.
@@ -41,7 +35,7 @@ F_USB = $(F_CPU)
41 35
42# Bootloader 36# Bootloader
43# 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
44# 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
45# automatically (+60). See bootloader.mk for all options. 39# automatically (+60). See bootloader.mk for all options.
46BOOTLOADER = caterina 40BOOTLOADER = caterina
47 41
@@ -63,12 +57,12 @@ MIDI_ENABLE = no # MIDI controls
63AUDIO_ENABLE = no # Audio output on port C6 57AUDIO_ENABLE = no # Audio output on port C6
64UNICODE_ENABLE = no # Unicode 58UNICODE_ENABLE = no # Unicode
65BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID 59BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
66RGBLIGHT_ENABLE = yes # Enable WS2812 RGB underlight. 60RGBLIGHT_ENABLE = yes # Enable WS2812 RGB underlight.
67SUBPROJECT_rev1 = yes 61SUBPROJECT_rev1 = yes
68USE_I2C = yes 62
69# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE 63# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
70SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend 64SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
71 65
72CUSTOM_MATRIX = yes 66SPLIT_KEYBOARD = yes
73 67
74DEFAULT_FOLDER = fourier/rev1 68DEFAULT_FOLDER = fourier/rev1
diff --git a/keyboards/fourier/serial.c b/keyboards/fourier/serial.c
deleted file mode 100644
index 74bcbb6bf..000000000
--- a/keyboards/fourier/serial.c
+++ /dev/null
@@ -1,228 +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
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/fourier/serial.h b/keyboards/fourier/serial.h
deleted file mode 100644
index 15fe4db7b..000000000
--- a/keyboards/fourier/serial.h
+++ /dev/null
@@ -1,26 +0,0 @@
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/fourier/split_util.c b/keyboards/fourier/split_util.c
deleted file mode 100644
index 2704e30e0..000000000
--- a/keyboards/fourier/split_util.c
+++ /dev/null
@@ -1,80 +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#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/fourier/split_util.h b/keyboards/fourier/split_util.h
deleted file mode 100644
index 595a0659e..000000000
--- a/keyboards/fourier/split_util.h
+++ /dev/null
@@ -1,20 +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#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