aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--keyboards/mint60/config.h11
-rw-r--r--keyboards/mint60/i2c.c162
-rw-r--r--keyboards/mint60/i2c.h49
-rw-r--r--keyboards/mint60/keymaps/default/keymap.c28
-rw-r--r--keyboards/mint60/matrix.c343
-rw-r--r--keyboards/mint60/mint60.c27
-rw-r--r--keyboards/mint60/mint60.h19
-rw-r--r--keyboards/mint60/rules.mk19
-rw-r--r--keyboards/mint60/serial.c295
-rw-r--r--keyboards/mint60/serial.h27
-rw-r--r--keyboards/mint60/serial_config.h16
-rw-r--r--keyboards/mint60/split_util.c70
-rw-r--r--keyboards/mint60/split_util.h19
13 files changed, 13 insertions, 1072 deletions
diff --git a/keyboards/mint60/config.h b/keyboards/mint60/config.h
index c98b9c1af..19a17a7c5 100644
--- a/keyboards/mint60/config.h
+++ b/keyboards/mint60/config.h
@@ -18,7 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
18#pragma once 18#pragma once
19 19
20#include "config_common.h" 20#include "config_common.h"
21#include <serial_config.h>
22 21
23/* USB Device descriptor parameter */ 22/* USB Device descriptor parameter */
24#define VENDOR_ID 0xFEED 23#define VENDOR_ID 0xFEED
@@ -28,11 +27,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
28#define PRODUCT Mint60 27#define PRODUCT Mint60
29#define DESCRIPTION A row staggered split keyboard 28#define DESCRIPTION A row staggered split keyboard
30 29
31#define TAPPING_FORCE_HOLD
32#define TAPPING_TERM 100
33
34#define USE_SERIAL
35
36/* key matrix size */ 30/* key matrix size */
37#define MATRIX_ROWS 10 31#define MATRIX_ROWS 10
38#define MATRIX_COLS 8 32#define MATRIX_COLS 8
@@ -54,6 +48,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
54/* COL2ROW, ROW2COL*/ 48/* COL2ROW, ROW2COL*/
55#define DIODE_DIRECTION COL2ROW 49#define DIODE_DIRECTION COL2ROW
56 50
51/*
52 * Split Keyboard specific options, make sure you have 'SPLIT_KEYBOARD = yes' in your rules.mk, and define SOFT_SERIAL_PIN.
53 */
54#define SOFT_SERIAL_PIN D2
55
57// #define BACKLIGHT_PIN B7 56// #define BACKLIGHT_PIN B7
58// #define BACKLIGHT_BREATHING 57// #define BACKLIGHT_BREATHING
59// #define BACKLIGHT_LEVELS 3 58// #define BACKLIGHT_LEVELS 3
diff --git a/keyboards/mint60/i2c.c b/keyboards/mint60/i2c.c
deleted file mode 100644
index 4bee5c639..000000000
--- a/keyboards/mint60/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 or 400kHz (see ./i2c.h SCL_CLOCK)
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/mint60/i2c.h b/keyboards/mint60/i2c.h
deleted file mode 100644
index 47cf6bd1b..000000000
--- a/keyboards/mint60/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 400kHz
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/mint60/keymaps/default/keymap.c b/keyboards/mint60/keymaps/default/keymap.c
index 0c65f73c7..a56bc5a1a 100644
--- a/keyboards/mint60/keymaps/default/keymap.c
+++ b/keyboards/mint60/keymaps/default/keymap.c
@@ -14,22 +14,11 @@
14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */ 15 */
16#include QMK_KEYBOARD_H 16#include QMK_KEYBOARD_H
17#ifdef PROTOCOL_LUFA
18#include "lufa.h"
19#include "split_util.h"
20#endif
21
22
23#ifdef RGBLIGHT_ENABLE
24//Following line allows macro to read current RGB settings
25extern rgblight_config_t rgblight_config;
26#endif
27 17
28enum custom_keycodes { 18enum custom_keycodes {
29 RGBRST = SAFE_RANGE 19 RGBRST = SAFE_RANGE
30}; 20};
31 21
32
33const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { 22const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
34 [0] = LAYOUT( \ 23 [0] = LAYOUT( \
35 KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, \ 24 KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, \
@@ -47,10 +36,6 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
47 ) 36 )
48}; 37};
49 38
50// define variables for reactive RGB
51bool TOG_STATUS = false;
52int RGB_current_mode;
53
54bool process_record_user(uint16_t keycode, keyrecord_t *record) { 39bool process_record_user(uint16_t keycode, keyrecord_t *record) {
55 switch (keycode) { 40 switch (keycode) {
56 case RGBRST: 41 case RGBRST:
@@ -58,22 +43,9 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
58 if (record->event.pressed) { 43 if (record->event.pressed) {
59 eeconfig_update_rgblight_default(); 44 eeconfig_update_rgblight_default();
60 rgblight_enable(); 45 rgblight_enable();
61 RGB_current_mode = rgblight_config.mode;
62 } 46 }
63 #endif 47 #endif
64 break; 48 break;
65 } 49 }
66 return true; 50 return true;
67} 51}
68
69void matrix_init_user(void) {
70
71}
72
73void matrix_scan_user(void) {
74
75}
76
77void led_set_user(uint8_t usb_led) {
78
79}
diff --git a/keyboards/mint60/matrix.c b/keyboards/mint60/matrix.c
deleted file mode 100644
index 30f3c56bc..000000000
--- a/keyboards/mint60/matrix.c
+++ /dev/null
@@ -1,343 +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
34#ifdef USE_I2C
35# include "i2c.h"
36#else // USE_SERIAL
37# include "serial.h"
38#endif
39
40#ifndef DEBOUNCE
41# define DEBOUNCE 5
42#endif
43
44#define ERROR_DISCONNECT_COUNT 5
45
46static uint8_t debouncing = DEBOUNCE;
47static const int ROWS_PER_HAND = MATRIX_ROWS/2;
48static uint8_t error_count = 0;
49uint8_t is_master = 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);
62static uint8_t matrix_master_scan(void);
63
64
65__attribute__ ((weak))
66void matrix_init_kb(void) {
67 matrix_init_user();
68}
69
70__attribute__ ((weak))
71void matrix_scan_kb(void) {
72 matrix_scan_user();
73}
74
75__attribute__ ((weak))
76void matrix_init_user(void) {
77}
78
79__attribute__ ((weak))
80void matrix_scan_user(void) {
81}
82
83inline
84uint8_t matrix_rows(void)
85{
86 return MATRIX_ROWS;
87}
88
89inline
90uint8_t matrix_cols(void)
91{
92 return MATRIX_COLS;
93}
94
95void matrix_init(void)
96{
97 debug_enable = true;
98 debug_matrix = true;
99 debug_mouse = true;
100 // initialize row and col
101 unselect_rows();
102 init_cols();
103
104 TX_RX_LED_INIT;
105
106 // initialize matrix state: all keys off
107 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
108 matrix[i] = 0;
109 matrix_debouncing[i] = 0;
110 }
111
112 is_master = has_usb();
113
114 matrix_init_quantum();
115}
116
117uint8_t _matrix_scan(void)
118{
119 // Right hand is stored after the left in the matirx so, we need to offset it
120 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
121
122 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
123 select_row(i);
124 _delay_us(30); // without this wait read unstable value.
125 matrix_row_t cols = read_cols();
126 if (matrix_debouncing[i+offset] != cols) {
127 matrix_debouncing[i+offset] = cols;
128 debouncing = DEBOUNCE;
129 }
130 unselect_rows();
131 }
132
133 if (debouncing) {
134 if (--debouncing) {
135 _delay_ms(1);
136 } else {
137 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
138 matrix[i+offset] = matrix_debouncing[i+offset];
139 }
140 }
141 }
142
143 return 1;
144}
145
146#ifdef USE_I2C
147
148// Get rows from other half over i2c
149int i2c_transaction(void) {
150 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
151
152 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
153 if (err) goto i2c_error;
154
155 // start of matrix stored at 0x00
156 err = i2c_master_write(0x00);
157 if (err) goto i2c_error;
158
159 // Start read
160 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
161 if (err) goto i2c_error;
162
163 if (!err) {
164 int i;
165 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
166 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
167 }
168 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
169 i2c_master_stop();
170 } else {
171i2c_error: // the cable is disconnceted, or something else went wrong
172 i2c_reset_state();
173 return err;
174 }
175
176 return 0;
177}
178
179#else // USE_SERIAL
180
181int serial_transaction(void) {
182 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
183 int ret=serial_update_buffers();
184 if (ret ) {
185 if(ret==2)RXLED1;
186 return 1;
187 }
188RXLED0;
189 for (int i = 0; i < ROWS_PER_HAND; ++i) {
190 matrix[slaveOffset+i] = serial_slave_buffer[i];
191 }
192 return 0;
193}
194#endif
195
196uint8_t matrix_scan(void)
197{
198 if (is_master) {
199 matrix_master_scan();
200 }else{
201 matrix_slave_scan();
202
203 int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
204
205 for (int i = 0; i < ROWS_PER_HAND; ++i) {
206 matrix[offset+i] = serial_master_buffer[i];
207 }
208
209 matrix_scan_quantum();
210 }
211 return 1;
212}
213
214
215uint8_t matrix_master_scan(void) {
216
217 int ret = _matrix_scan();
218
219#ifndef KEYBOARD_helix_rev1
220 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
221
222#ifdef USE_I2C
223// for (int i = 0; i < ROWS_PER_HAND; ++i) {
224 /* i2c_slave_buffer[i] = matrix[offset+i]; */
225// i2c_slave_buffer[i] = matrix[offset+i];
226// }
227#else // USE_SERIAL
228 for (int i = 0; i < ROWS_PER_HAND; ++i) {
229 serial_master_buffer[i] = matrix[offset+i];
230 }
231#endif
232#endif
233
234#ifdef USE_I2C
235 if( i2c_transaction() ) {
236#else // USE_SERIAL
237 if( serial_transaction() ) {
238#endif
239 // turn on the indicator led when halves are disconnected
240 TXLED1;
241
242 error_count++;
243
244 if (error_count > ERROR_DISCONNECT_COUNT) {
245 // reset other half if disconnected
246 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
247 for (int i = 0; i < ROWS_PER_HAND; ++i) {
248 matrix[slaveOffset+i] = 0;
249 }
250 }
251 } else {
252 // turn off the indicator led on no error
253 TXLED0;
254 error_count = 0;
255 }
256 matrix_scan_quantum();
257 return ret;
258}
259
260void matrix_slave_scan(void) {
261 _matrix_scan();
262
263 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
264
265#ifdef USE_I2C
266 for (int i = 0; i < ROWS_PER_HAND; ++i) {
267 /* i2c_slave_buffer[i] = matrix[offset+i]; */
268 i2c_slave_buffer[i] = matrix[offset+i];
269 }
270#else // USE_SERIAL
271 for (int i = 0; i < ROWS_PER_HAND; ++i) {
272 serial_slave_buffer[i] = matrix[offset+i];
273 }
274#endif
275}
276
277bool matrix_is_modified(void)
278{
279 if (debouncing) return false;
280 return true;
281}
282
283inline
284bool matrix_is_on(uint8_t row, uint8_t col)
285{
286 return (matrix[row] & ((matrix_row_t)1<<col));
287}
288
289inline
290matrix_row_t matrix_get_row(uint8_t row)
291{
292 return matrix[row];
293}
294
295void matrix_print(void)
296{
297 print("\nr/c 0123456789ABCDEF\n");
298 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
299 phex(row); print(": ");
300 pbin_reverse16(matrix_get_row(row));
301 print("\n");
302 }
303}
304
305uint8_t matrix_key_count(void)
306{
307 uint8_t count = 0;
308 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
309 count += bitpop16(matrix[i]);
310 }
311 return count;
312}
313
314static void init_cols(void)
315{
316 for(int x = 0; x < MATRIX_COLS; x++) {
317 _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
318 _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
319 }
320}
321
322static matrix_row_t read_cols(void)
323{
324 matrix_row_t result = 0;
325 for(int x = 0; x < MATRIX_COLS; x++) {
326 result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
327 }
328 return result;
329}
330
331static void unselect_rows(void)
332{
333 for(int x = 0; x < ROWS_PER_HAND; x++) {
334 _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
335 _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
336 }
337}
338
339static void select_row(uint8_t row)
340{
341 _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
342 _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
343}
diff --git a/keyboards/mint60/mint60.c b/keyboards/mint60/mint60.c
index 724bea757..8905392ef 100644
--- a/keyboards/mint60/mint60.c
+++ b/keyboards/mint60/mint60.c
@@ -14,30 +14,3 @@
14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */ 15 */
16#include "mint60.h" 16#include "mint60.h"
17
18void matrix_init_kb(void) {
19 // put your keyboard start-up code here
20 // runs once when the firmware starts up
21
22 matrix_init_user();
23}
24
25void matrix_scan_kb(void) {
26 // put your looping keyboard code here
27 // runs every cycle (a lot)
28
29 matrix_scan_user();
30}
31
32bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
33 // put your per-action keyboard code here
34 // runs for every action, just before processing by the firmware
35
36 return process_record_user(keycode, record);
37}
38
39void led_set_kb(uint8_t usb_led) {
40 // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
41
42 led_set_user(usb_led);
43}
diff --git a/keyboards/mint60/mint60.h b/keyboards/mint60/mint60.h
index ed2ee255f..78529921a 100644
--- a/keyboards/mint60/mint60.h
+++ b/keyboards/mint60/mint60.h
@@ -13,25 +13,10 @@
13 * You should have received a copy of the GNU General Public License 13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */ 15 */
16#ifndef MINT60_H 16#pragma once
17#define MINT60_H
18 17
19#include "quantum.h" 18#include "quantum.h"
20 19
21#ifdef RGBLIGHT_ENABLE
22//rgb led driver
23#include "ws2812.h"
24#endif
25
26#ifdef USE_I2C
27#include <stddef.h>
28#ifdef __AVR__
29 #include <avr/io.h>
30 #include <avr/interrupt.h>
31#endif
32#endif
33
34
35// This a shortcut to help you visually see your layout. 20// This a shortcut to help you visually see your layout.
36// The following is an example using the Planck MIT layout 21// The following is an example using the Planck MIT layout
37// The first section contains all of the arguments 22// The first section contains all of the arguments
@@ -55,5 +40,3 @@
55 { R30, R31, R32, R33, R34, R35, R36, R37 }, \ 40 { R30, R31, R32, R33, R34, R35, R36, R37 }, \
56 { R40, R41, KC_NO, R43, KC_NO, R45, R46, R47 }, \ 41 { R40, R41, KC_NO, R43, KC_NO, R45, R46, R47 }, \
57} 42}
58
59#endif
diff --git a/keyboards/mint60/rules.mk b/keyboards/mint60/rules.mk
index 33368caab..bd9f3154d 100644
--- a/keyboards/mint60/rules.mk
+++ b/keyboards/mint60/rules.mk
@@ -14,16 +14,17 @@ BOOTLOADER = caterina
14# Build Options 14# Build Options
15# change yes to no to disable 15# change yes to no to disable
16# 16#
17BOOTMAGIC_ENABLE = no # Virtual DIP switch configuration 17BOOTMAGIC_ENABLE = no # Virtual DIP switch configuration
18MOUSEKEY_ENABLE = no # Mouse keys 18MOUSEKEY_ENABLE = no # Mouse keys
19EXTRAKEY_ENABLE = no # Audio control and System control 19EXTRAKEY_ENABLE = no # Audio control and System control
20CONSOLE_ENABLE = no # Console for debug 20CONSOLE_ENABLE = no # Console for debug
21COMMAND_ENABLE = no # Commands for debug and configuration 21COMMAND_ENABLE = no # Commands for debug and configuration
22# 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
23SLEEP_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 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 25NKRO_ENABLE = no # USB Nkey Rollover
26BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality on B7 by default 26BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality on B7 by default
27RGBLIGHT_ENABLE = yes # Enable WS2812 RGB underlight.
27MIDI_ENABLE = no # MIDI support 28MIDI_ENABLE = no # MIDI support
28UNICODE_ENABLE = no # Unicode 29UNICODE_ENABLE = no # Unicode
29BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID 30BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
@@ -31,10 +32,4 @@ AUDIO_ENABLE = no # Audio output on port C6
31FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches 32FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches
32HD44780_ENABLE = no # Enable support for HD44780 based LCDs 33HD44780_ENABLE = no # Enable support for HD44780 based LCDs
33 34
34CUSTOM_MATRIX = yes 35SPLIT_KEYBOARD = yes
35SRC += i2c.c \
36 serial.c \
37 matrix.c \
38 split_util.c
39USE_I2C = yes
40RGBLIGHT_ENABLE = yes # Enable WS2812 RGB underlight.
diff --git a/keyboards/mint60/serial.c b/keyboards/mint60/serial.c
deleted file mode 100644
index 591941587..000000000
--- a/keyboards/mint60/serial.c
+++ /dev/null
@@ -1,295 +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#ifdef USE_SERIAL
16
17#define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
18
19// Serial pulse period in microseconds.
20#define SELECT_SERIAL_SPEED 1
21#if SELECT_SERIAL_SPEED == 0
22 // Very High speed
23 #define SERIAL_DELAY 4 // micro sec
24 #define READ_WRITE_START_ADJUST 30 // cycles
25 #define READ_WRITE_WIDTH_ADJUST 10 // cycles
26#elif SELECT_SERIAL_SPEED == 1
27 // High speed
28 #define SERIAL_DELAY 6 // micro sec
29 #define READ_WRITE_START_ADJUST 23 // cycles
30 #define READ_WRITE_WIDTH_ADJUST 10 // cycles
31#elif SELECT_SERIAL_SPEED == 2
32 // Middle speed
33 #define SERIAL_DELAY 12 // micro sec
34 #define READ_WRITE_START_ADJUST 25 // cycles
35 #define READ_WRITE_WIDTH_ADJUST 10 // cycles
36#elif SELECT_SERIAL_SPEED == 3
37 // Low speed
38 #define SERIAL_DELAY 24 // micro sec
39 #define READ_WRITE_START_ADJUST 25 // cycles
40 #define READ_WRITE_WIDTH_ADJUST 10 // cycles
41#elif SELECT_SERIAL_SPEED == 4
42 // Very Low speed
43 #define SERIAL_DELAY 50 // micro sec
44 #define READ_WRITE_START_ADJUST 25 // cycles
45 #define READ_WRITE_WIDTH_ADJUST 10 // cycles
46#else
47#error Illegal Serial Speed
48#endif
49
50
51#define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
52#define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
53
54#define SLAVE_INT_WIDTH 1
55#define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
56
57uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
58uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
59
60#define SLAVE_DATA_CORRUPT (1<<0)
61volatile uint8_t status = 0;
62
63inline static
64void serial_delay(void) {
65 _delay_us(SERIAL_DELAY);
66}
67
68inline static
69void serial_delay_half1(void) {
70 _delay_us(SERIAL_DELAY_HALF1);
71}
72
73inline static
74void serial_delay_half2(void) {
75 _delay_us(SERIAL_DELAY_HALF2);
76}
77
78inline static
79void serial_output(void) {
80 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
81}
82
83// make the serial pin an input with pull-up resistor
84inline static
85void serial_input_with_pullup(void) {
86 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
87 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
88}
89
90inline static
91uint8_t serial_read_pin(void) {
92 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
93}
94
95inline static
96void serial_low(void) {
97 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
98}
99
100inline static
101void serial_high(void) {
102 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
103}
104
105void serial_master_init(void) {
106 serial_output();
107 serial_high();
108}
109
110void serial_slave_init(void) {
111 serial_input_with_pullup();
112
113#if SERIAL_PIN_MASK == _BV(PD0)
114 // Enable INT0
115 EIMSK |= _BV(INT0);
116 // Trigger on falling edge of INT0
117 EICRA &= ~(_BV(ISC00) | _BV(ISC01));
118#elif SERIAL_PIN_MASK == _BV(PD2)
119 // Enable INT2
120 EIMSK |= _BV(INT2);
121 // Trigger on falling edge of INT2
122 EICRA &= ~(_BV(ISC20) | _BV(ISC21));
123#else
124 #error unknown SERIAL_PIN_MASK value
125#endif
126}
127
128// Used by the sender to synchronize timing with the reciver.
129static
130void sync_recv(void) {
131 for (int i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
132 }
133 // This shouldn't hang if the slave disconnects because the
134 // serial line will float to high if the slave does disconnect.
135 while (!serial_read_pin());
136}
137
138// Used by the reciver to send a synchronization signal to the sender.
139static
140void sync_send(void) {
141 serial_low();
142 serial_delay();
143 serial_high();
144}
145
146// Reads a byte from the serial line
147static
148uint8_t serial_read_byte(void) {
149 uint8_t byte = 0;
150 _delay_sub_us(READ_WRITE_START_ADJUST);
151 for ( uint8_t i = 0; i < 8; ++i) {
152 serial_delay_half1(); // read the middle of pulses
153 byte = (byte << 1) | serial_read_pin();
154 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
155 serial_delay_half2();
156 }
157 return byte;
158}
159
160// Sends a byte with MSB ordering
161static
162void serial_write_byte(uint8_t data) {
163 uint8_t b = 1<<7;
164 while( b ) {
165 if(data & b) {
166 serial_high();
167 } else {
168 serial_low();
169 }
170 b >>= 1;
171 serial_delay();
172 }
173 serial_low(); // sync_send() / senc_recv() need raise edge
174}
175
176// interrupt handle to be used by the slave device
177ISR(SERIAL_PIN_INTERRUPT) {
178 serial_output();
179
180 // slave send phase
181 uint8_t checksum = 0;
182 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
183 sync_send();
184 serial_write_byte(serial_slave_buffer[i]);
185 checksum += serial_slave_buffer[i];
186 }
187 sync_send();
188 serial_write_byte(checksum);
189
190 // slave switch to input
191 sync_send(); //0
192 serial_delay_half1(); //1
193 serial_low(); //2
194 serial_input_with_pullup(); //2
195 serial_delay_half1(); //3
196
197 // slave recive phase
198 uint8_t checksum_computed = 0;
199 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
200 sync_recv();
201 serial_master_buffer[i] = serial_read_byte();
202 checksum_computed += serial_master_buffer[i];
203 }
204 sync_recv();
205 uint8_t checksum_received = serial_read_byte();
206
207 if ( checksum_computed != checksum_received ) {
208 status |= SLAVE_DATA_CORRUPT;
209 } else {
210 status &= ~SLAVE_DATA_CORRUPT;
211 }
212
213 sync_recv(); //weit master output to high
214}
215
216inline
217bool serial_slave_DATA_CORRUPT(void) {
218 return status & SLAVE_DATA_CORRUPT;
219}
220
221// Copies the serial_slave_buffer to the master and sends the
222// serial_master_buffer to the slave.
223//
224// Returns:
225// 0 => no error
226// 1 => slave did not respond
227// 2 => checksum error
228int serial_update_buffers(void) {
229 // this code is very time dependent, so we need to disable interrupts
230 cli();
231
232 // signal to the slave that we want to start a transaction
233 serial_output();
234 serial_low();
235 _delay_us(SLAVE_INT_WIDTH);
236
237 // wait for the slaves response
238 serial_input_with_pullup();
239 _delay_us(SLAVE_INT_RESPONSE_TIME);
240
241 // check if the slave is present
242 if (serial_read_pin()) {
243 // slave failed to pull the line low, assume not present
244 serial_output();
245 serial_high();
246 sei();
247 return 1;
248 }
249
250 // master recive phase
251 // if the slave is present syncronize with it
252
253 uint8_t checksum_computed = 0;
254 // receive data from the slave
255 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
256 sync_recv();
257 serial_slave_buffer[i] = serial_read_byte();
258 checksum_computed += serial_slave_buffer[i];
259 }
260 sync_recv();
261 uint8_t checksum_received = serial_read_byte();
262
263 if (checksum_computed != checksum_received) {
264 serial_output();
265 serial_high();
266 sei();
267 return 2;
268 }
269
270 // master switch to output
271 sync_recv(); //0
272 serial_delay(); //1
273 serial_low(); //3
274 serial_output(); // 3
275 serial_delay_half1(); //4
276
277 // master send phase
278 uint8_t checksum = 0;
279
280 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
281 sync_send();
282 serial_write_byte(serial_master_buffer[i]);
283 checksum += serial_master_buffer[i];
284 }
285 sync_send();
286 serial_write_byte(checksum);
287
288 // always, release the line when not in use
289 sync_send();
290
291 sei();
292 return 0;
293}
294
295#endif
diff --git a/keyboards/mint60/serial.h b/keyboards/mint60/serial.h
deleted file mode 100644
index c3c9569b2..000000000
--- a/keyboards/mint60/serial.h
+++ /dev/null
@@ -1,27 +0,0 @@
1#ifndef SOFT_SERIAL_H
2#define SOFT_SERIAL_H
3
4#include <stdbool.h>
5
6// ////////////////////////////////////////////
7// Need Soft Serial defines in serial_config.h
8// ////////////////////////////////////////////
9// ex.
10// #define SERIAL_PIN_DDR DDRD
11// #define SERIAL_PIN_PORT PORTD
12// #define SERIAL_PIN_INPUT PIND
13// #define SERIAL_PIN_MASK _BV(PD?) ?=0,2
14// #define SERIAL_PIN_INTERRUPT INT?_vect ?=0,2
15// #define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2
16// #define SERIAL_MASTER_BUFFER_LENGTH MATRIX_ROWS/2
17
18// Buffers for master - slave communication
19extern volatile uint8_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH];
20extern volatile uint8_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 /* SOFT_SERIAL_H */
diff --git a/keyboards/mint60/serial_config.h b/keyboards/mint60/serial_config.h
deleted file mode 100644
index 82c6e4e83..000000000
--- a/keyboards/mint60/serial_config.h
+++ /dev/null
@@ -1,16 +0,0 @@
1#ifndef SOFT_SERIAL_CONFIG_H
2#define SOFT_SERIAL_CONFIG_H
3
4/* Soft Serial defines */
5#define SERIAL_PIN_DDR DDRD
6#define SERIAL_PIN_PORT PORTD
7#define SERIAL_PIN_INPUT PIND
8#define SERIAL_PIN_MASK _BV(PD2)
9#define SERIAL_PIN_INTERRUPT INT2_vect
10
11#define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2
12#define SERIAL_MASTER_BUFFER_LENGTH MATRIX_ROWS/2
13
14//// #error rev2 serial config
15
16#endif /* SOFT_SERIAL_CONFIG_H */
diff --git a/keyboards/mint60/split_util.c b/keyboards/mint60/split_util.c
deleted file mode 100644
index cd3a1896e..000000000
--- a/keyboards/mint60/split_util.c
+++ /dev/null
@@ -1,70 +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
11#ifdef USE_I2C
12# include "i2c.h"
13#else
14# include "serial.h"
15#endif
16
17volatile bool isLeftHand = true;
18
19static void setup_handedness(void) {
20 #ifdef EE_HANDS
21 isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
22 #else
23 // I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c
24 #if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT)
25 isLeftHand = !has_usb();
26 #else
27 isLeftHand = has_usb();
28 #endif
29 #endif
30}
31
32static void keyboard_master_setup(void) {
33
34#ifdef USE_I2C
35 i2c_master_init();
36#else
37 serial_master_init();
38#endif
39}
40
41static void keyboard_slave_setup(void) {
42
43#ifdef USE_I2C
44 i2c_slave_init(SLAVE_I2C_ADDRESS);
45#else
46 serial_slave_init();
47#endif
48}
49
50bool has_usb(void) {
51 USBCON |= (1 << OTGPADE); //enables VBUS pad
52 _delay_us(5);
53 return (USBSTA & (1<<VBUS)); //checks state of VBUS
54}
55
56void split_keyboard_setup(void) {
57 setup_handedness();
58
59 if (has_usb()) {
60 keyboard_master_setup();
61 } else {
62 keyboard_slave_setup();
63 }
64 sei();
65}
66
67// this code runs before the usb and keyboard is initialized
68void matrix_setup(void) {
69 split_keyboard_setup();
70}
diff --git a/keyboards/mint60/split_util.h b/keyboards/mint60/split_util.h
deleted file mode 100644
index 687ca19bd..000000000
--- a/keyboards/mint60/split_util.h
+++ /dev/null
@@ -1,19 +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);
16
17void matrix_master_OLED_init (void);
18
19#endif