aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--keyboards/vitamins_included/i2c.c163
-rw-r--r--keyboards/vitamins_included/i2c.h50
-rw-r--r--keyboards/vitamins_included/matrix.c503
-rw-r--r--keyboards/vitamins_included/rev1/config.h4
-rw-r--r--keyboards/vitamins_included/rev1/rules.mk9
-rw-r--r--keyboards/vitamins_included/serial.c229
-rw-r--r--keyboards/vitamins_included/serial.h25
-rw-r--r--keyboards/vitamins_included/split_util.c28
-rw-r--r--keyboards/vitamins_included/split_util.h16
9 files changed, 5 insertions, 1022 deletions
diff --git a/keyboards/vitamins_included/i2c.c b/keyboards/vitamins_included/i2c.c
deleted file mode 100644
index 49ada2143..000000000
--- a/keyboards/vitamins_included/i2c.c
+++ /dev/null
@@ -1,163 +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 contacted_by_master = true;
162}
163#endif
diff --git a/keyboards/vitamins_included/i2c.h b/keyboards/vitamins_included/i2c.h
deleted file mode 100644
index 6cb8cce91..000000000
--- a/keyboards/vitamins_included/i2c.h
+++ /dev/null
@@ -1,50 +0,0 @@
1#ifndef I2C_H
2#define I2C_H
3
4#include <stdint.h>
5#include "split_util.h"
6
7#ifndef F_CPU
8#define F_CPU 16000000UL
9#endif
10
11#define I2C_READ 1
12#define I2C_WRITE 0
13
14#define I2C_ACK 1
15#define I2C_NACK 0
16
17#define SLAVE_BUFFER_SIZE 0x10
18
19// i2c SCL clock frequency
20#define SCL_CLOCK 100000UL
21
22extern volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
23
24void i2c_master_init(void);
25uint8_t i2c_master_start(uint8_t address);
26void i2c_master_stop(void);
27uint8_t i2c_master_write(uint8_t data);
28uint8_t i2c_master_read(int);
29void i2c_reset_state(void);
30void i2c_slave_init(uint8_t address);
31
32
33static inline unsigned char i2c_start_read(unsigned char addr) {
34 return i2c_master_start((addr << 1) | I2C_READ);
35}
36
37static inline unsigned char i2c_start_write(unsigned char addr) {
38 return i2c_master_start((addr << 1) | I2C_WRITE);
39}
40
41// from SSD1306 scrips
42extern unsigned char i2c_rep_start(unsigned char addr);
43extern void i2c_start_wait(unsigned char addr);
44extern unsigned char i2c_readAck(void);
45extern unsigned char i2c_readNak(void);
46extern unsigned char i2c_read(unsigned char ack);
47
48#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();
49
50#endif
diff --git a/keyboards/vitamins_included/matrix.c b/keyboards/vitamins_included/matrix.c
deleted file mode 100644
index df3e86985..000000000
--- a/keyboards/vitamins_included/matrix.c
+++ /dev/null
@@ -1,503 +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/interrupt.h>
25#include <util/delay.h>
26#include "wait.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#include "timer.h"
35#include <print.h>
36
37#if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
38 #include "rgblight.h"
39#endif
40
41
42#ifdef USE_I2C
43# include "i2c.h"
44#else // USE_SERIAL
45# include "serial.h"
46#endif
47
48#ifndef DEBOUNCE
49# define DEBOUNCE 5
50#endif
51
52#if (DEBOUNCE > 0)
53 static uint16_t debouncing_time;
54 static bool debouncing = false;
55#endif
56
57#if (MATRIX_COLS <= 8)
58# define print_matrix_header() print("\nr/c 01234567\n")
59# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
60# define matrix_bitpop(i) bitpop(matrix[i])
61# define ROW_SHIFTER ((uint8_t)1)
62#else
63# error "Currently only supports 8 COLS"
64#endif
65static matrix_row_t matrix_debouncing[MATRIX_ROWS];
66
67#define ERROR_DISCONNECT_COUNT 5
68
69#define ROWS_PER_HAND (MATRIX_ROWS/2)
70
71static uint8_t error_count = 0;
72
73static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
74static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
75
76/* matrix state(1:on, 0:off) */
77static matrix_row_t matrix[MATRIX_ROWS];
78static matrix_row_t matrix_debouncing[MATRIX_ROWS];
79
80#if (DIODE_DIRECTION == COL2ROW)
81 static void init_cols(void);
82 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
83 static void unselect_rows(void);
84 static void select_row(uint8_t row);
85 static void unselect_row(uint8_t row);
86#elif (DIODE_DIRECTION == ROW2COL)
87 static void init_rows(void);
88 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
89 static void unselect_cols(void);
90 static void unselect_col(uint8_t col);
91 static void select_col(uint8_t col);
92#endif
93
94
95__attribute__ ((weak))
96void matrix_init_quantum(void) {
97 matrix_init_kb();
98}
99
100__attribute__ ((weak))
101void matrix_scan_quantum(void) {
102 matrix_scan_kb();
103}
104
105__attribute__ ((weak))
106void matrix_init_kb(void) {
107 matrix_init_user();
108}
109
110__attribute__ ((weak))
111void matrix_scan_kb(void) {
112 matrix_scan_user();
113}
114
115__attribute__ ((weak))
116void matrix_init_user(void) {
117}
118
119__attribute__ ((weak))
120void matrix_scan_user(void) {
121}
122
123inline
124uint8_t matrix_rows(void) {
125 return MATRIX_ROWS;
126}
127
128inline
129uint8_t matrix_cols(void) {
130 return MATRIX_COLS;
131}
132
133bool has_usb(void) {
134 return UDADDR & _BV(ADDEN); // This will return true if a USB connection has been established
135}
136
137void matrix_init(void)
138{
139 // initialize row and col
140#if (DIODE_DIRECTION == COL2ROW)
141 unselect_rows();
142 init_cols();
143#elif (DIODE_DIRECTION == ROW2COL)
144 unselect_cols();
145 init_rows();
146#endif
147
148 TX_RX_LED_INIT;
149
150 // initialize matrix state: all keys off
151 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
152 matrix[i] = 0;
153 matrix_debouncing[i] = 0;
154 }
155
156 #ifdef RGBLIGHT_ENABLE
157 rgblight_init();
158 #endif
159
160 timer_init();
161 #ifdef USE_I2C
162 i2c_slave_init(SLAVE_I2C_ADDRESS);
163 #else
164 serial_slave_init();
165 #endif
166
167 sei();
168
169 matrix_init_quantum();
170 while(!has_usb() || contacted_by_master){
171 matrix_slave_scan();
172 }
173
174 // Set up as master
175 #ifdef USE_I2C
176 i2c_reset_state();
177 i2c_master_init();
178 #else
179 serial_master_init();
180 #endif
181}
182
183uint8_t _matrix_scan(void)
184{
185 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
186#if (DIODE_DIRECTION == COL2ROW)
187 // Set row, read cols
188 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
189# if (DEBOUNCE > 0)
190 bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
191
192 if (matrix_changed) {
193 debouncing = true;
194 debouncing_time = timer_read();
195 }
196
197# else
198 read_cols_on_row(matrix+offset, current_row);
199# endif
200
201 }
202
203#elif (DIODE_DIRECTION == ROW2COL)
204 // Set col, read rows
205 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
206# if (DEBOUNCE > 0)
207 bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
208 if (matrix_changed) {
209 debouncing = true;
210 debouncing_time = timer_read();
211 }
212# else
213 read_rows_on_col(matrix+offset, current_col);
214# endif
215
216 }
217#endif
218
219# if (DEBOUNCE > 0)
220 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
221 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
222 matrix[i+offset] = matrix_debouncing[i+offset];
223 }
224 debouncing = false;
225 }
226# endif
227
228 return 1;
229}
230
231#ifdef USE_I2C
232
233// Get rows from other half over i2c
234int i2c_transaction(void) {
235 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
236
237 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
238 if (err) goto i2c_error;
239
240 // start of matrix stored at 0x00
241 err = i2c_master_write(0x00);
242 if (err) goto i2c_error;
243
244 // Start read
245 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
246 if (err) goto i2c_error;
247
248 if (!err) {
249 int i;
250 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
251 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
252 }
253 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
254 i2c_master_stop();
255 } else {
256i2c_error: // the cable is disconnceted, or something else went wrong
257 i2c_reset_state();
258 return err;
259 }
260
261 return 0;
262}
263
264#else // USE_SERIAL
265
266int serial_transaction(void) {
267 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
268
269 if (serial_update_buffers()) {
270 return 1;
271 }
272
273 for (int i = 0; i < ROWS_PER_HAND; ++i) {
274 matrix[slaveOffset+i] = serial_slave_buffer[i];
275 }
276 return 0;
277}
278#endif
279
280uint8_t matrix_scan(void)
281{
282 uint8_t ret = _matrix_scan();
283
284#ifdef USE_I2C
285 if( i2c_transaction() ) {
286#else // USE_SERIAL
287 if( serial_transaction() ) {
288#endif
289 // turn on the indicator led when halves are disconnected
290 TXLED1;
291
292 error_count++;
293
294 if (error_count > ERROR_DISCONNECT_COUNT) {
295 // reset other half if disconnected
296 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
297 for (int i = 0; i < ROWS_PER_HAND; ++i) {
298 matrix[slaveOffset+i] = 0;
299 }
300 }
301 } else {
302 // turn off the indicator led on no error
303 TXLED0;
304 error_count = 0;
305 }
306 matrix_scan_quantum();
307 return ret;
308}
309
310void matrix_slave_scan(void) {
311 #if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
312 rgblight_task();
313 #endif
314 _matrix_scan();
315
316 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
317
318#ifdef USE_I2C
319 for (int i = 0; i < ROWS_PER_HAND; ++i) {
320 i2c_slave_buffer[i] = matrix[offset+i];
321 }
322#else // USE_SERIAL
323 for (int i = 0; i < ROWS_PER_HAND; ++i) {
324 serial_slave_buffer[i] = matrix[offset+i];
325 }
326#endif
327}
328
329bool matrix_is_modified(void)
330{
331 if (debouncing) return false;
332 return true;
333}
334
335inline
336bool matrix_is_on(uint8_t row, uint8_t col)
337{
338 return (matrix[row] & ((matrix_row_t)1<<col));
339}
340
341inline
342matrix_row_t matrix_get_row(uint8_t row)
343{
344 return matrix[row];
345}
346
347void matrix_print(void)
348{
349 print("\nr/c 0123456789ABCDEF\n");
350 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
351 phex(row); print(": ");
352 pbin_reverse16(matrix_get_row(row));
353 print("\n");
354 }
355}
356
357uint8_t matrix_key_count(void)
358{
359 uint8_t count = 0;
360 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
361 count += bitpop16(matrix[i]);
362 }
363 return count;
364}
365
366#if (DIODE_DIRECTION == COL2ROW)
367
368static void init_cols(void)
369{
370 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
371 uint8_t pin = col_pins[x];
372 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
373 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
374 }
375}
376
377static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
378{
379 // Store last value of row prior to reading
380 matrix_row_t last_row_value = current_matrix[current_row];
381
382 // Clear data in matrix row
383 current_matrix[current_row] = 0;
384
385 // Select row and wait for row selecton to stabilize
386 select_row(current_row);
387 wait_us(30);
388
389 // For each col...
390 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
391
392 // Select the col pin to read (active low)
393 uint8_t pin = col_pins[col_index];
394 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
395
396 // Populate the matrix row with the state of the col pin
397 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
398 }
399
400 // Unselect row
401 unselect_row(current_row);
402
403 return (last_row_value != current_matrix[current_row]);
404}
405
406static void select_row(uint8_t row)
407{
408 uint8_t pin = row_pins[row];
409 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
410 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
411}
412
413static void unselect_row(uint8_t row)
414{
415 uint8_t pin = row_pins[row];
416 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
417 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
418}
419
420static void unselect_rows(void)
421{
422 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
423 uint8_t pin = row_pins[x];
424 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
425 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
426 }
427}
428
429#elif (DIODE_DIRECTION == ROW2COL)
430
431static void init_rows(void)
432{
433 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
434 uint8_t pin = row_pins[x];
435 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
436 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
437 }
438}
439
440static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
441{
442 bool matrix_changed = false;
443
444 // Select col and wait for col selecton to stabilize
445 select_col(current_col);
446 wait_us(30);
447
448 // For each row...
449 for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
450 {
451
452 // Store last value of row prior to reading
453 matrix_row_t last_row_value = current_matrix[row_index];
454
455 // Check row pin state
456 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
457 {
458 // Pin LO, set col bit
459 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
460 }
461 else
462 {
463 // Pin HI, clear col bit
464 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
465 }
466
467 // Determine if the matrix changed state
468 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
469 {
470 matrix_changed = true;
471 }
472 }
473
474 // Unselect col
475 unselect_col(current_col);
476
477 return matrix_changed;
478}
479
480static void select_col(uint8_t col)
481{
482 uint8_t pin = col_pins[col];
483 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
484 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
485}
486
487static void unselect_col(uint8_t col)
488{
489 uint8_t pin = col_pins[col];
490 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
491 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
492}
493
494static void unselect_cols(void)
495{
496 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
497 uint8_t pin = col_pins[x];
498 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
499 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
500 }
501}
502
503#endif
diff --git a/keyboards/vitamins_included/rev1/config.h b/keyboards/vitamins_included/rev1/config.h
index 39b6a9ff4..a6f9c95fb 100644
--- a/keyboards/vitamins_included/rev1/config.h
+++ b/keyboards/vitamins_included/rev1/config.h
@@ -20,7 +20,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20
21#include "config_common.h" 21#include "config_common.h"
22 22
23#define SPLIT_USB_DETECT
24#define NO_USB_STARTUP_CHECK
25
23#define EE_HANDS 26#define EE_HANDS
27#define SOFT_SERIAL_PIN D0
24 28
25/* USB Device descriptor parameters */ 29/* USB Device descriptor parameters */
26#define DEVICE_VER 0x0001 30#define DEVICE_VER 0x0001
diff --git a/keyboards/vitamins_included/rev1/rules.mk b/keyboards/vitamins_included/rev1/rules.mk
index b500c8f27..4b1454e15 100644
--- a/keyboards/vitamins_included/rev1/rules.mk
+++ b/keyboards/vitamins_included/rev1/rules.mk
@@ -1,9 +1,2 @@
1SRC += matrix.c \ 1SPLIT_KEYBOARD = yes
2 split_util.c \
3 ssd1306.c
4
5QUANTUM_LIB_SRC += i2c.c \
6 serial.c \
7
8CUSTOM_MATRIX = yes
9BOOTLOADER = caterina 2BOOTLOADER = caterina
diff --git a/keyboards/vitamins_included/serial.c b/keyboards/vitamins_included/serial.c
deleted file mode 100644
index 4d37eeb8d..000000000
--- a/keyboards/vitamins_included/serial.c
+++ /dev/null
@@ -1,229 +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 contacted_by_master = true;
159}
160
161inline
162bool serial_slave_DATA_CORRUPT(void) {
163 return status & SLAVE_DATA_CORRUPT;
164}
165
166// Copies the serial_slave_buffer to the master and sends the
167// serial_master_buffer to the slave.
168//
169// Returns:
170// 0 => no error
171// 1 => slave did not respond
172int serial_update_buffers(void) {
173 // this code is very time dependent, so we need to disable interrupts
174 cli();
175
176 // signal to the slave that we want to start a transaction
177 serial_output();
178 serial_low();
179 _delay_us(1);
180
181 // wait for the slaves response
182 serial_input();
183 serial_high();
184 _delay_us(SERIAL_DELAY);
185
186 // check if the slave is present
187 if (serial_read_pin()) {
188 // slave failed to pull the line low, assume not present
189 sei();
190 return 1;
191 }
192
193 // if the slave is present syncronize with it
194 sync_recv();
195
196 uint8_t checksum_computed = 0;
197 // receive data from the slave
198 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
199 serial_slave_buffer[i] = serial_read_byte();
200 sync_recv();
201 checksum_computed += serial_slave_buffer[i];
202 }
203 uint8_t checksum_received = serial_read_byte();
204 sync_recv();
205
206 if (checksum_computed != checksum_received) {
207 sei();
208 return 1;
209 }
210
211 uint8_t checksum = 0;
212 // send data to the slave
213 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
214 serial_write_byte(serial_master_buffer[i]);
215 sync_recv();
216 checksum += serial_master_buffer[i];
217 }
218 serial_write_byte(checksum);
219 sync_recv();
220
221 // always, release the line when not in use
222 serial_output();
223 serial_high();
224
225 sei();
226 return 0;
227}
228
229#endif
diff --git a/keyboards/vitamins_included/serial.h b/keyboards/vitamins_included/serial.h
deleted file mode 100644
index 53b3a3bf0..000000000
--- a/keyboards/vitamins_included/serial.h
+++ /dev/null
@@ -1,25 +0,0 @@
1#pragma once
2
3#include "config.h"
4#include <stdbool.h>
5#include "split_util.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
diff --git a/keyboards/vitamins_included/split_util.c b/keyboards/vitamins_included/split_util.c
deleted file mode 100644
index 4a898cd7b..000000000
--- a/keyboards/vitamins_included/split_util.c
+++ /dev/null
@@ -1,28 +0,0 @@
1#include <avr/io.h>
2#include <avr/wdt.h>
3#include <avr/power.h>
4#include <avr/interrupt.h>
5#include <util/delay.h>
6#include <avr/eeprom.h>
7#include "split_util.h"
8#include "matrix.h"
9#include "keyboard.h"
10#include "config.h"
11#include "timer.h"
12#include "debug.h"
13
14volatile bool isLeftHand = true;
15volatile bool contacted_by_master = false;
16
17bool is_rev2(void) // Input with pullup, if rev2, pin is pulled down with 220R resistor.
18{
19 setPinInputHigh(B5);
20 bool rev2 = !readPin(B5);
21 setPinInput(B5);
22 return rev2;
23}
24
25// this code runs before the usb and keyboard is initialized
26void matrix_setup(void) {
27 isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
28}
diff --git a/keyboards/vitamins_included/split_util.h b/keyboards/vitamins_included/split_util.h
deleted file mode 100644
index da7be5155..000000000
--- a/keyboards/vitamins_included/split_util.h
+++ /dev/null
@@ -1,16 +0,0 @@
1#pragma once
2
3#include <stdbool.h>
4#include "eeconfig.h"
5#include "quantum.h"
6
7#define SLAVE_I2C_ADDRESS 0x32
8
9extern volatile bool isLeftHand;
10extern volatile bool contacted_by_master;
11
12bool has_usb(void);
13bool is_rev2(void);
14
15// slave version of matix scan, defined in matrix.c
16void matrix_slave_scan(void);