aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--keyboards/minidox/config.h9
-rw-r--r--keyboards/minidox/eeprom-lefthand.eep2
-rw-r--r--keyboards/minidox/eeprom-righthand.eep2
-rw-r--r--keyboards/minidox/i2c.c162
-rw-r--r--keyboards/minidox/i2c.h49
-rw-r--r--keyboards/minidox/matrix.c307
-rw-r--r--keyboards/minidox/minidox.h5
-rw-r--r--keyboards/minidox/readme.md66
-rw-r--r--keyboards/minidox/rev1/config.h12
-rw-r--r--keyboards/minidox/rev1/rev1.h7
-rw-r--r--keyboards/minidox/rules.mk38
-rw-r--r--keyboards/minidox/serial.c228
-rw-r--r--keyboards/minidox/serial.h26
-rw-r--r--keyboards/minidox/split_util.c84
-rw-r--r--keyboards/minidox/split_util.h20
15 files changed, 32 insertions, 985 deletions
diff --git a/keyboards/minidox/config.h b/keyboards/minidox/config.h
index 7502983b6..b3d375656 100644
--- a/keyboards/minidox/config.h
+++ b/keyboards/minidox/config.h
@@ -15,8 +15,7 @@ You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>. 15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#ifndef CONFIG_H 18#pragma once
19#define CONFIG_H
20 19
21#include "config_common.h" 20#include "config_common.h"
22 21
@@ -61,9 +60,3 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
61//#define NO_ACTION_ONESHOT 60//#define NO_ACTION_ONESHOT
62//#define NO_ACTION_MACRO 61//#define NO_ACTION_MACRO
63//#define NO_ACTION_FUNCTION 62//#define NO_ACTION_FUNCTION
64
65#ifdef SUBPROJECT_rev1
66 #include "rev1/config.h"
67#endif
68
69#endif
diff --git a/keyboards/minidox/eeprom-lefthand.eep b/keyboards/minidox/eeprom-lefthand.eep
deleted file mode 100644
index e5a7bc1e5..000000000
--- a/keyboards/minidox/eeprom-lefthand.eep
+++ /dev/null
@@ -1,2 +0,0 @@
1:0F000000000000000000000000000000000001F0
2:00000001FF
diff --git a/keyboards/minidox/eeprom-righthand.eep b/keyboards/minidox/eeprom-righthand.eep
deleted file mode 100644
index 7ea44f52e..000000000
--- a/keyboards/minidox/eeprom-righthand.eep
+++ /dev/null
@@ -1,2 +0,0 @@
1:0F000000000000000000000000000000000000F1
2:00000001FF
diff --git a/keyboards/minidox/i2c.c b/keyboards/minidox/i2c.c
deleted file mode 100644
index 084c890c4..000000000
--- a/keyboards/minidox/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/minidox/i2c.h b/keyboards/minidox/i2c.h
deleted file mode 100644
index c15b6bc50..000000000
--- a/keyboards/minidox/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/minidox/matrix.c b/keyboards/minidox/matrix.c
deleted file mode 100644
index 27a38dca0..000000000
--- a/keyboards/minidox/matrix.c
+++ /dev/null
@@ -1,307 +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 <avr/wdt.h>
25#include <avr/interrupt.h>
26#include <util/delay.h>
27#include "print.h"
28#include "debug.h"
29#include "util.h"
30#include "matrix.h"
31#include "split_util.h"
32#include "pro_micro.h"
33#include "config.h"
34
35#ifdef USE_I2C
36# include "i2c.h"
37#else // USE_SERIAL
38# include "serial.h"
39#endif
40
41#ifndef DEBOUNCE
42# define DEBOUNCE 5
43#endif
44
45#define ERROR_DISCONNECT_COUNT 5
46
47static uint8_t debouncing = DEBOUNCE;
48static const int ROWS_PER_HAND = MATRIX_ROWS/2;
49static uint8_t error_count = 0;
50
51static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
52static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
53
54/* matrix state(1:on, 0:off) */
55static matrix_row_t matrix[MATRIX_ROWS];
56static matrix_row_t matrix_debouncing[MATRIX_ROWS];
57
58static matrix_row_t read_cols(void);
59static void init_cols(void);
60static void unselect_rows(void);
61static void select_row(uint8_t row);
62
63
64__attribute__ ((weak))
65void matrix_init_kb(void) {
66 matrix_init_user();
67}
68
69__attribute__ ((weak))
70void matrix_scan_kb(void) {
71 matrix_scan_user();
72}
73
74__attribute__ ((weak))
75void matrix_init_user(void) {
76}
77
78__attribute__ ((weak))
79void matrix_scan_user(void) {
80}
81
82inline
83uint8_t matrix_rows(void)
84{
85 return MATRIX_ROWS;
86}
87
88inline
89uint8_t matrix_cols(void)
90{
91 return MATRIX_COLS;
92}
93
94void matrix_init(void)
95{
96 debug_enable = true;
97 debug_matrix = true;
98 debug_mouse = true;
99 // initialize row and col
100 unselect_rows();
101 init_cols();
102
103 TX_RX_LED_INIT;
104
105 // initialize matrix state: all keys off
106 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
107 matrix[i] = 0;
108 matrix_debouncing[i] = 0;
109 }
110
111 matrix_init_quantum();
112}
113
114uint8_t _matrix_scan(void)
115{
116 // Right hand is stored after the left in the matirx so, we need to offset it
117 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
118
119 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
120 select_row(i);
121 _delay_us(30); // without this wait read unstable value.
122 matrix_row_t cols = read_cols();
123 if (matrix_debouncing[i+offset] != cols) {
124 matrix_debouncing[i+offset] = cols;
125 debouncing = DEBOUNCE;
126 }
127 unselect_rows();
128 }
129
130 if (debouncing) {
131 if (--debouncing) {
132 _delay_ms(1);
133 } else {
134 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
135 matrix[i+offset] = matrix_debouncing[i+offset];
136 }
137 }
138 }
139
140 return 1;
141}
142
143#ifdef USE_I2C
144
145// Get rows from other half over i2c
146int i2c_transaction(void) {
147 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
148
149 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
150 if (err) goto i2c_error;
151
152 // start of matrix stored at 0x00
153 err = i2c_master_write(0x00);
154 if (err) goto i2c_error;
155
156 // Start read
157 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
158 if (err) goto i2c_error;
159
160 if (!err) {
161 int i;
162 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
163 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
164 }
165 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
166 i2c_master_stop();
167 } else {
168i2c_error: // the cable is disconnceted, or something else went wrong
169 i2c_reset_state();
170 return err;
171 }
172
173 return 0;
174}
175
176#else // USE_SERIAL
177
178int serial_transaction(void) {
179 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
180
181 if (serial_update_buffers()) {
182 return 1;
183 }
184
185 for (int i = 0; i < ROWS_PER_HAND; ++i) {
186 matrix[slaveOffset+i] = serial_slave_buffer[i];
187 }
188 return 0;
189}
190#endif
191
192uint8_t matrix_scan(void)
193{
194 int ret = _matrix_scan();
195
196
197
198#ifdef USE_I2C
199 if( i2c_transaction() ) {
200#else // USE_SERIAL
201 if( serial_transaction() ) {
202#endif
203 // turn on the indicator led when halves are disconnected
204 TXLED1;
205
206 error_count++;
207
208 if (error_count > ERROR_DISCONNECT_COUNT) {
209 // reset other half if disconnected
210 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
211 for (int i = 0; i < ROWS_PER_HAND; ++i) {
212 matrix[slaveOffset+i] = 0;
213 }
214 }
215 } else {
216 // turn off the indicator led on no error
217 TXLED0;
218 error_count = 0;
219 }
220 matrix_scan_quantum();
221 return ret;
222}
223
224void matrix_slave_scan(void) {
225 _matrix_scan();
226
227 int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2);
228
229#ifdef USE_I2C
230 for (int i = 0; i < ROWS_PER_HAND; ++i) {
231 /* i2c_slave_buffer[i] = matrix[offset+i]; */
232 i2c_slave_buffer[i] = matrix[offset+i];
233 }
234#else // USE_SERIAL
235 for (int i = 0; i < ROWS_PER_HAND; ++i) {
236 serial_slave_buffer[i] = matrix[offset+i];
237 }
238#endif
239}
240
241bool matrix_is_modified(void)
242{
243 if (debouncing) return false;
244 return true;
245}
246
247inline
248bool matrix_is_on(uint8_t row, uint8_t col)
249{
250 return (matrix[row] & ((matrix_row_t)1<<col));
251}
252
253inline
254matrix_row_t matrix_get_row(uint8_t row)
255{
256 return matrix[row];
257}
258
259void matrix_print(void)
260{
261 print("\nr/c 0123456789ABCDEF\n");
262 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
263 phex(row); print(": ");
264 pbin_reverse16(matrix_get_row(row));
265 print("\n");
266 }
267}
268
269uint8_t matrix_key_count(void)
270{
271 uint8_t count = 0;
272 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
273 count += bitpop16(matrix[i]);
274 }
275 return count;
276}
277
278static void init_cols(void)
279{
280 for(int x = 0; x < MATRIX_COLS; x++) {
281 _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
282 _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
283 }
284}
285
286static matrix_row_t read_cols(void)
287{
288 matrix_row_t result = 0;
289 for(int x = 0; x < MATRIX_COLS; x++) {
290 result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
291 }
292 return result;
293}
294
295static void unselect_rows(void)
296{
297 for(int x = 0; x < ROWS_PER_HAND; x++) {
298 _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
299 _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
300 }
301}
302
303static void select_row(uint8_t row)
304{
305 _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
306 _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
307}
diff --git a/keyboards/minidox/minidox.h b/keyboards/minidox/minidox.h
index 6e8add206..5d5ca9f50 100644
--- a/keyboards/minidox/minidox.h
+++ b/keyboards/minidox/minidox.h
@@ -1,10 +1,7 @@
1#ifndef MINIDOX_H 1#pragma once
2#define MINIDOX_H
3 2
4#ifdef KEYBOARD_minidox_rev1 3#ifdef KEYBOARD_minidox_rev1
5 #include "rev1.h" 4 #include "rev1.h"
6#endif 5#endif
7 6
8#include "quantum.h" 7#include "quantum.h"
9
10#endif \ No newline at end of file
diff --git a/keyboards/minidox/readme.md b/keyboards/minidox/readme.md
index 74afedc18..ec52e1f75 100644
--- a/keyboards/minidox/readme.md
+++ b/keyboards/minidox/readme.md
@@ -1,75 +1,27 @@
1MiniDox 1# MiniDox
2=====
3 2
4![MiniDox](http://i.imgur.com/iWb3yO0.jpg) 3![MiniDox](http://i.imgur.com/iWb3yO0.jpg)
5 4
6A compact version of the ErgoDox 5A compact version of the ErgoDox
7 6
8Keyboard Maintainer: That-Canadian 7* Keyboard Maintainer: That-Canadian
9Hardware Supported: MiniDox PCB rev1 Pro Micro 8* Hardware Supported: MiniDox PCB rev1 Pro Micro
10 9
11Make example for this keyboard (after setting up your build environment): 10Make example for this keyboard (after setting up your build environment):
12 11
13 make minidox/rev1:default 12 make minidox/rev1:default
14 13
14Flashing example for this keyboard ([using the command line](https://docs.qmk.fm/#/newbs_flashing?id=flash-your-keyboard-from-the-command-line)):
15
16 make minidox/rev1:default:flash
17
15See [build environment setup](https://docs.qmk.fm/#/getting_started_build_tools) then the [make instructions](https://docs.qmk.fm/#/getting_started_make_guide) for more information. 18See [build environment setup](https://docs.qmk.fm/#/getting_started_build_tools) then the [make instructions](https://docs.qmk.fm/#/getting_started_make_guide) for more information.
16 19
17## Build Guide 20## Build Guide
18 21
19A build guide for putting together the MiniDox v1 can be found here: [MiniDox Build Log / Guide](http://imgur.com/a/vImo6) 22A build guide for putting together the MiniDox v1 can be found here: [MiniDox Build Log / Guide](http://imgur.com/a/vImo6)
20 23
21Flashing
22-------
23Note: Most of this is copied from the Let's Split readme, because it is awesome
24
25From the root directory run `make PROJECT:KEYMAP:avrdude` for automatic serial port resolution and flashing.
26Example: `make minidox/rev1:default:avrdude`
27
28Choosing which board to plug the USB cable into (choosing Master)
29--------
30Because the two boards are identical, the firmware has logic to differentiate the left and right board.
31
32It uses two strategies to figure things out: look at the EEPROM (memory on the chip) or looks if the current board has the usb cable.
33
34The EEPROM approach requires additional setup (flashing the eeeprom) but allows you to swap the usb cable to either side.
35
36The USB cable approach is easier to setup and if you just want the usb cable on the left board, you do not need to do anything extra.
37
38### Setting the left hand as master
39If you always plug the usb cable into the left board, nothing extra is needed as this is the default. Comment out `EE_HANDS` and comment out `I2C_MASTER_RIGHT` or `MASTER_RIGHT` if for some reason it was set.
40
41### Setting the right hand as master
42If you always plug the usb cable into the right board, add an extra flag to your `config.h`
43```
44 #define MASTER_RIGHT
45```
46
47### Setting EE_hands to use either hands as master
48If you define `EE_HANDS` in your `config.h`, you will need to set the
49EEPROM for the left and right halves.
50
51The EEPROM is used to store whether the
52half is left handed or right handed. This makes it so that the same firmware
53file will run on both hands instead of having to flash left and right handed
54versions of the firmware to each half. To flash the EEPROM file for the left
55half run:
56```
57avrdude -p atmega32u4 -P $(COM_PORT) -c avr109 -U eeprom:w:"./quantum/split_common/eeprom-lefthand.eep"
58// or the equivalent in dfu-programmer
59
60```
61and similarly for right half
62```
63avrdude -p atmega32u4 -P $(COM_PORT) -c avr109 -U eeprom:w:"./quantum/split_common/eeprom-righthand.eep"
64// or the equivalent in dfu-programmer
65```
66
67NOTE: replace `$(COM_PORT)` with the port of your device (e.g. `/dev/ttyACM0`)
68 24
69After you have flashed the EEPROM, you then need to set `EE_HANDS` in your config.h, rebuild the hex files and reflash. 25## Choosing which board to plug the USB cable into (choosing Master)
70 26
71Note that you need to program both halves, but you have the option of using 27Because the two boards are identical, the firmware has logic to differentiate the left and right board. It uses two strategies to figure things out, [EE_HANDS](https://docs.qmk.fm/#/feature_split_keyboard?id=handedness-by-eeprom) or [by define](https://docs.qmk.fm/#/feature_split_keyboard?id=handedness-by-define). See [setting-handedness](https://docs.qmk.fm/#/config_options?id=setting-handedness) for more information.
72different keymaps for each half. You could program the left half with a QWERTY
73layout and the right half with a Colemak layout using bootmagic's default layout option.
74Then if you connect the left half to a computer by USB the keyboard will use QWERTY and Colemak when the
75right half is connected.
diff --git a/keyboards/minidox/rev1/config.h b/keyboards/minidox/rev1/config.h
index b7272bdb0..734ca562f 100644
--- a/keyboards/minidox/rev1/config.h
+++ b/keyboards/minidox/rev1/config.h
@@ -15,10 +15,7 @@ You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>. 15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#ifndef REV1_CONFIG_H 18#pragma once
19#define REV1_CONFIG_H
20
21#include "../config.h"
22 19
23#define DEVICE_VER 0x0001 20#define DEVICE_VER 0x0001
24 21
@@ -26,11 +23,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
26#define MATRIX_ROW_PINS { B2, B6, B4, B5 } 23#define MATRIX_ROW_PINS { B2, B6, B4, B5 }
27#define MATRIX_COL_PINS { F4, D3, D2, D1, D4 } 24#define MATRIX_COL_PINS { F4, D3, D2, D1, D4 }
28 25
26#define SOFT_SERIAL_PIN D0
27
29#define USE_SERIAL 28#define USE_SERIAL
30 29
31//#define EE_HANDS 30//#define EE_HANDS
32
33#define I2C_MASTER_LEFT
34//#define I2C_MASTER_RIGHT
35
36#endif
diff --git a/keyboards/minidox/rev1/rev1.h b/keyboards/minidox/rev1/rev1.h
index 5d32b05d4..bfa0e5fcc 100644
--- a/keyboards/minidox/rev1/rev1.h
+++ b/keyboards/minidox/rev1/rev1.h
@@ -1,7 +1,4 @@
1#ifndef REV1_H 1#pragma once
2#define REV1_H
3
4#include "../minidox.h"
5 2
6#include "quantum.h" 3#include "quantum.h"
7 4
@@ -24,5 +21,3 @@
24 { k61, k62, k63, k64, k65 }, \ 21 { k61, k62, k63, k64, k65 }, \
25 { ___, ___, k73, k74, k75 } \ 22 { ___, ___, k73, k74, k75 } \
26 } 23 }
27
28#endif
diff --git a/keyboards/minidox/rules.mk b/keyboards/minidox/rules.mk
index dd2affb3f..1446c5144 100644
--- a/keyboards/minidox/rules.mk
+++ b/keyboards/minidox/rules.mk
@@ -12,29 +12,25 @@ MCU = atmega32u4
12BOOTLOADER = caterina 12BOOTLOADER = caterina
13 13
14# Build Options 14# Build Options
15# change to "no" to disable the options, or define them in the Makefile in 15# change yes to no to disable
16# the appropriate keymap folder that will get included automatically
17# 16#
18BOOTMAGIC_ENABLE ?= no # Virtual DIP switch configuration(+1000) 17BOOTMAGIC_ENABLE = no # Virtual DIP switch configuration
19MOUSEKEY_ENABLE ?= yes # Mouse keys(+4700) 18MOUSEKEY_ENABLE = yes # Mouse keys
20EXTRAKEY_ENABLE ?= no # Audio control and System control(+450) 19EXTRAKEY_ENABLE = yes # Audio control and System control
21CONSOLE_ENABLE ?= no # Console for debug(+400) 20CONSOLE_ENABLE = no # Console for debug
22COMMAND_ENABLE ?= yes # Commands for debug and configuration 21COMMAND_ENABLE = yes # Commands for debug and configuration
23NKRO_ENABLE ?= no # Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
24BACKLIGHT_ENABLE ?= no # Enable keyboard backlight functionality
25MIDI_ENABLE ?= no # MIDI controls
26AUDIO_ENABLE ?= no # Audio output on port C6
27UNICODE_ENABLE ?= no # Unicode
28BLUETOOTH_ENABLE ?= no # Enable Bluetooth with the Adafruit EZ-Key HID
29RGBLIGHT_ENABLE ?= no # Enable WS2812 RGB underlight.
30USE_I2C ?= no
31# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE 22# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
32SLEEP_LED_ENABLE ?= no # Breathing sleep LED during USB suspend 23SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
24# if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
25NKRO_ENABLE = no # USB Nkey Rollover
26BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality
27RGBLIGHT_ENABLE = no # Enable keyboard RGB underglow
28MIDI_ENABLE = no # MIDI support
29BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
30AUDIO_ENABLE = no # Audio output on port C6
31FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches
32HD44780_ENABLE = no # Enable support for HD44780 based LCDs
33 33
34CUSTOM_MATRIX = yes 34SPLIT_KEYBOARD = yes
35SRC += matrix.c \
36 i2c.c \
37 split_util.c \
38 serial.c
39 35
40DEFAULT_FOLDER = minidox/rev1 36DEFAULT_FOLDER = minidox/rev1
diff --git a/keyboards/minidox/serial.c b/keyboards/minidox/serial.c
deleted file mode 100644
index 74bcbb6bf..000000000
--- a/keyboards/minidox/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/minidox/serial.h b/keyboards/minidox/serial.h
deleted file mode 100644
index 15fe4db7b..000000000
--- a/keyboards/minidox/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/minidox/split_util.c b/keyboards/minidox/split_util.c
deleted file mode 100644
index 39639c3b4..000000000
--- a/keyboards/minidox/split_util.c
+++ /dev/null
@@ -1,84 +0,0 @@
1#include <avr/io.h>
2#include <avr/wdt.h>
3#include <avr/power.h>
4#include <avr/interrupt.h>
5#include <util/delay.h>
6#include <avr/eeprom.h>
7#include "split_util.h"
8#include "matrix.h"
9#include "keyboard.h"
10#include "config.h"
11
12#ifdef USE_I2C
13# include "i2c.h"
14#else
15# include "serial.h"
16#endif
17
18volatile bool isLeftHand = true;
19
20static void setup_handedness(void) {
21 #ifdef EE_HANDS
22 isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
23 #else
24 // I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c
25 #if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT)
26 isLeftHand = !has_usb();
27 #else
28 isLeftHand = has_usb();
29 #endif
30 #endif
31}
32
33static void keyboard_master_setup(void) {
34#ifdef USE_I2C
35 i2c_master_init();
36#ifdef SSD1306OLED
37 matrix_master_OLED_init ();
38#endif
39#else
40 serial_master_init();
41#endif
42}
43
44static void keyboard_slave_setup(void) {
45#ifdef USE_I2C
46 i2c_slave_init(SLAVE_I2C_ADDRESS);
47#else
48 serial_slave_init();
49#endif
50}
51
52bool has_usb(void) {
53 USBCON |= (1 << OTGPADE); //enables VBUS pad
54 _delay_us(5);
55 return (USBSTA & (1<<VBUS)); //checks state of VBUS
56}
57
58void split_keyboard_setup(void) {
59 setup_handedness();
60
61 if (has_usb()) {
62 keyboard_master_setup();
63 } else {
64 keyboard_slave_setup();
65 }
66 sei();
67}
68
69void keyboard_slave_loop(void) {
70 matrix_init();
71
72 while (1) {
73 matrix_slave_scan();
74 }
75}
76
77// this code runs before the usb and keyboard is initialized
78void matrix_setup(void) {
79 split_keyboard_setup();
80
81 if (!has_usb()) {
82 keyboard_slave_loop();
83 }
84}
diff --git a/keyboards/minidox/split_util.h b/keyboards/minidox/split_util.h
deleted file mode 100644
index 595a0659e..000000000
--- a/keyboards/minidox/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