aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDrashna Jaelre <drashna@live.com>2019-04-08 12:43:03 -0700
committerMechMerlin <30334081+mechmerlin@users.noreply.github.com>2019-04-08 12:43:03 -0700
commitf8d365a47847f8e49fde5096aa065dbee08cf728 (patch)
treeb3e865779abd7d2aaa49eb1e73a70f60a60f4f26
parentda9bb590551e4e40552f301852074dffccd2c29d (diff)
downloadqmk_firmware-f8d365a47847f8e49fde5096aa065dbee08cf728.tar.gz
qmk_firmware-f8d365a47847f8e49fde5096aa065dbee08cf728.zip
Convert BFO9000 to Split Keyboard code (#5568)
-rw-r--r--keyboards/keebio/bfo9000/bfo9000.h13
-rw-r--r--keyboards/keebio/bfo9000/config.h3
-rw-r--r--keyboards/keebio/bfo9000/i2c.c162
-rw-r--r--keyboards/keebio/bfo9000/i2c.h49
-rw-r--r--keyboards/keebio/bfo9000/matrix.c342
-rw-r--r--keyboards/keebio/bfo9000/rules.mk11
-rw-r--r--keyboards/keebio/bfo9000/serial.c230
-rw-r--r--keyboards/keebio/bfo9000/serial.h27
-rw-r--r--keyboards/keebio/bfo9000/split_util.c86
-rw-r--r--keyboards/keebio/bfo9000/split_util.h20
10 files changed, 7 insertions, 936 deletions
diff --git a/keyboards/keebio/bfo9000/bfo9000.h b/keyboards/keebio/bfo9000/bfo9000.h
index c3bd2236c..985c7f67f 100644
--- a/keyboards/keebio/bfo9000/bfo9000.h
+++ b/keyboards/keebio/bfo9000/bfo9000.h
@@ -1,16 +1,7 @@
1#ifndef BFO9000_H 1#pragma once
2#define BFO9000_H
3 2
4#include "quantum.h" 3#include "quantum.h"
5 4
6#ifdef USE_I2C
7#include <stddef.h>
8#ifdef __AVR__
9 #include <avr/io.h>
10 #include <avr/interrupt.h>
11#endif
12#endif
13
14#define LAYOUT( \ 5#define LAYOUT( \
15 L00, L01, L02, L03, L04, L05, L06, L07, L08, R00, R01, R02, R03, R04, R05, R06, R07, R08, \ 6 L00, L01, L02, L03, L04, L05, L06, L07, L08, R00, R01, R02, R03, R04, R05, R06, R07, R08, \
16 L10, L11, L12, L13, L14, L15, L16, L17, L18, R10, R11, R12, R13, R14, R15, R16, R17, R18, \ 7 L10, L11, L12, L13, L14, L15, L16, L17, L18, R10, R11, R12, R13, R14, R15, R16, R17, R18, \
@@ -33,5 +24,3 @@
33 { R40, R41, R42, R43, R44, R45, R46, R47, R48 }, \ 24 { R40, R41, R42, R43, R44, R45, R46, R47, R48 }, \
34 { R50, R51, R52, R53, R54, R55, R56, R57, R58 } \ 25 { R50, R51, R52, R53, R54, R55, R56, R57, R58 } \
35 } 26 }
36
37#endif
diff --git a/keyboards/keebio/bfo9000/config.h b/keyboards/keebio/bfo9000/config.h
index 5dc2bd434..c5afb265f 100644
--- a/keyboards/keebio/bfo9000/config.h
+++ b/keyboards/keebio/bfo9000/config.h
@@ -45,6 +45,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
45/* Locking resynchronize hack */ 45/* Locking resynchronize hack */
46#define LOCKING_RESYNC_ENABLE 46#define LOCKING_RESYNC_ENABLE
47 47
48/* serial.c configuration for split keyboard */
49#define SOFT_SERIAL_PIN D0
50
48/* ws2812 RGB LED */ 51/* ws2812 RGB LED */
49#define RGB_DI_PIN B4 52#define RGB_DI_PIN B4
50#define RGBLED_NUM 20 // Number of LEDs 53#define RGBLED_NUM 20 // Number of LEDs
diff --git a/keyboards/keebio/bfo9000/i2c.c b/keyboards/keebio/bfo9000/i2c.c
deleted file mode 100644
index 084c890c4..000000000
--- a/keyboards/keebio/bfo9000/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/keebio/bfo9000/i2c.h b/keyboards/keebio/bfo9000/i2c.h
deleted file mode 100644
index c15b6bc50..000000000
--- a/keyboards/keebio/bfo9000/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/keebio/bfo9000/matrix.c b/keyboards/keebio/bfo9000/matrix.c
deleted file mode 100644
index 2ca5f4d87..000000000
--- a/keyboards/keebio/bfo9000/matrix.c
+++ /dev/null
@@ -1,342 +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#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
51static uint8_t debouncing = DEBOUNCE;
52static const int ROWS_PER_HAND = MATRIX_ROWS/2;
53static uint8_t error_count = 0;
54
55static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
56static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
57
58/* matrix state(1:on, 0:off) */
59static matrix_row_t matrix[MATRIX_ROWS];
60static matrix_row_t matrix_debouncing[MATRIX_ROWS];
61
62static matrix_row_t read_cols(void);
63static void init_cols(void);
64static void unselect_rows(void);
65static void select_row(uint8_t row);
66
67
68__attribute__ ((weak))
69void matrix_init_kb(void) {
70 matrix_init_user();
71}
72
73__attribute__ ((weak))
74void matrix_scan_kb(void) {
75 matrix_scan_user();
76}
77
78__attribute__ ((weak))
79void matrix_init_user(void) {
80}
81
82__attribute__ ((weak))
83void matrix_scan_user(void) {
84}
85
86inline
87uint8_t matrix_rows(void)
88{
89 return MATRIX_ROWS;
90}
91
92inline
93uint8_t matrix_cols(void)
94{
95 return MATRIX_COLS;
96}
97
98void 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
118uint8_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
150int 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 {
189i2c_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
199int 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
213uint8_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
245void 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
276bool matrix_is_modified(void)
277{
278 if (debouncing) return false;
279 return true;
280}
281
282inline
283bool matrix_is_on(uint8_t row, uint8_t col)
284{
285 return (matrix[row] & ((matrix_row_t)1<<col));
286}
287
288inline
289matrix_row_t matrix_get_row(uint8_t row)
290{
291 return matrix[row];
292}
293
294void 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
304uint8_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
313static 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
321static 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
330static 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
338static 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/keebio/bfo9000/rules.mk b/keyboards/keebio/bfo9000/rules.mk
index d11f9a54e..3ebe39a90 100644
--- a/keyboards/keebio/bfo9000/rules.mk
+++ b/keyboards/keebio/bfo9000/rules.mk
@@ -1,8 +1,3 @@
1SRC += matrix.c \
2 i2c.c \
3 split_util.c \
4 serial.c
5
6# MCU name 1# MCU name
7#MCU = at90usb1287 2#MCU = at90usb1287
8MCU = atmega32u4 3MCU = atmega32u4
@@ -41,7 +36,7 @@ F_USB = $(F_CPU)
41 36
42# Bootloader 37# Bootloader
43# This definition is optional, and if your keyboard supports multiple bootloaders of 38# 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 39# different sizes, comment this out, and the correct address will be loaded
45# automatically (+60). See bootloader.mk for all options. 40# automatically (+60). See bootloader.mk for all options.
46BOOTLOADER = caterina 41BOOTLOADER = caterina
47 42
@@ -63,8 +58,8 @@ MIDI_ENABLE = no # MIDI controls
63AUDIO_ENABLE = no # Audio output on port C6 58AUDIO_ENABLE = no # Audio output on port C6
64UNICODE_ENABLE = no # Unicode 59UNICODE_ENABLE = no # Unicode
65BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID 60BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
66RGBLIGHT_ENABLE = no # Enable WS2812 RGB underlight. 61RGBLIGHT_ENABLE = no # Enable WS2812 RGB underlight.
67# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE 62# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
68SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend 63SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
69 64
70CUSTOM_MATRIX = yes 65SPLIT_KEYBOARD = yes
diff --git a/keyboards/keebio/bfo9000/serial.c b/keyboards/keebio/bfo9000/serial.c
deleted file mode 100644
index fea57b651..000000000
--- a/keyboards/keebio/bfo9000/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
21matrix_row_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
22matrix_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)
27volatile uint8_t status = 0;
28
29inline static
30void serial_delay(void) {
31 _delay_us(SERIAL_DELAY);
32}
33
34inline static
35void serial_output(void) {
36 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
37}
38
39// make the serial pin an input with pull-up resistor
40inline static
41void serial_input(void) {
42 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
43 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
44}
45
46inline static
47matrix_row_t serial_read_pin(void) {
48 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
49}
50
51inline static
52void serial_low(void) {
53 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
54}
55
56inline static
57void serial_high(void) {
58 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
59}
60
61void serial_master_init(void) {
62 serial_output();
63 serial_high();
64}
65
66void 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.
76static
77void 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.
86static
87void 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
97static
98matrix_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
111static
112void 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
126ISR(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
162inline
163bool 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
173int 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/keebio/bfo9000/serial.h b/keyboards/keebio/bfo9000/serial.h
deleted file mode 100644
index 627619457..000000000
--- a/keyboards/keebio/bfo9000/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 "matrix.h"
6#include <stdbool.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
19extern volatile matrix_row_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH];
20extern volatile matrix_row_t serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH];
21
22void serial_master_init(void);
23void serial_slave_init(void);
24int serial_update_buffers(void);
25bool serial_slave_data_corrupt(void);
26
27#endif
diff --git a/keyboards/keebio/bfo9000/split_util.c b/keyboards/keebio/bfo9000/split_util.c
deleted file mode 100644
index 7f200e6c9..000000000
--- a/keyboards/keebio/bfo9000/split_util.c
+++ /dev/null
@@ -1,86 +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
13#ifdef USE_I2C
14# include "i2c.h"
15#else
16# include "serial.h"
17#endif
18
19volatile bool isLeftHand = true;
20
21static void setup_handedness(void) {
22 #ifdef EE_HANDS
23 isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
24 #else
25 // I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c
26 #if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT)
27 isLeftHand = !has_usb();
28 #else
29 isLeftHand = has_usb();
30 #endif
31 #endif
32}
33
34static void keyboard_master_setup(void) {
35#ifdef USE_I2C
36 i2c_master_init();
37#ifdef SSD1306OLED
38 matrix_master_OLED_init();
39#endif
40#else
41 serial_master_init();
42#endif
43}
44
45static void keyboard_slave_setup(void) {
46 timer_init();
47#ifdef USE_I2C
48 i2c_slave_init(SLAVE_I2C_ADDRESS);
49#else
50 serial_slave_init();
51#endif
52}
53
54bool has_usb(void) {
55 USBCON |= (1 << OTGPADE); //enables VBUS pad
56 _delay_us(5);
57 return (USBSTA & (1<<VBUS)); //checks state of VBUS
58}
59
60void split_keyboard_setup(void) {
61 setup_handedness();
62
63 if (has_usb()) {
64 keyboard_master_setup();
65 } else {
66 keyboard_slave_setup();
67 }
68 sei();
69}
70
71void keyboard_slave_loop(void) {
72 matrix_init();
73
74 while (1) {
75 matrix_slave_scan();
76 }
77}
78
79// this code runs before the usb and keyboard is initialized
80void matrix_setup(void) {
81 split_keyboard_setup();
82
83 if (!has_usb()) {
84 keyboard_slave_loop();
85 }
86}
diff --git a/keyboards/keebio/bfo9000/split_util.h b/keyboards/keebio/bfo9000/split_util.h
deleted file mode 100644
index 595a0659e..000000000
--- a/keyboards/keebio/bfo9000/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