* Split common conversion * Updated serial and encoder pins * Fixing default folder until r2 * Fixing oled driver on slave split common * Fixing keymap compile errors * Fixing oled inactivity timer on slave split common * Hoisted oled driver task, init, & activity to keyboard.c * Update keyboards/sol/config.h Co-Authored-By: XScorpion2 <rcalt2vt@gmail.com> * Remove TAPPING_FORCE_HOLDpull/5806/head 0.6.364
@ -1,71 +0,0 @@ | |||||
#include "knob_v2.h" | |||||
bool knob_prev_a = false; | |||||
static knob_report_t knob_report = {.dir = 0, .phase = 0}; | |||||
void knob_init(void) { | |||||
// I use pins D1 (ISR1) & D4 for a knob. | |||||
// Set pin mode for D4 as input. | |||||
DDRD &= ~(0UL << ENCODER_PIN_2); | |||||
// Enable internal pull-up for D4. | |||||
// This is done by "writing" 1 to a pin that has its mode set to input. | |||||
PORTD |= (1 << ENCODER_PIN_2); | |||||
// Enable interrupt for D1 | |||||
// For more info on the below flags see this awesome section 11.1 (pages 89-90) here: | |||||
// https://cdn-shop.adafruit.com/datasheets/atmel-7766-8-bit-avr-atmega16u4-32u4_datasheet.pdf | |||||
// Set pin mode & pull-up. | |||||
DDRD &= ~(0UL << ENCODER_PIN_1); | |||||
PORTD |= (1UL << ENCODER_PIN_1); | |||||
// INT: 33221100 | |||||
EICRA |= 0b00010000; // 0b01 - any edge | |||||
// INT: 6 3210 | |||||
EIMSK |= 0b00000100; | |||||
} | |||||
ISR(ENCODER_INT) { | |||||
bool a = PIND & (1 << ENCODER_PIN_1); | |||||
if (knob_prev_a != a) { | |||||
// "A" channel has REALLY changed. | |||||
knob_report.phase = a; | |||||
knob_prev_a = a; | |||||
bool b = PIND & (1 << ENCODER_PIN_2); | |||||
if (a == b) { | |||||
// Halfway through CCW rotation (A == B) | |||||
// | |||||
// +---YOU ARE HERE (A=1, B=1) | |||||
// | +---OR HERE (A=0, B=0) | |||||
// | | | |||||
// v v | |||||
// A: _____/^^^^^\__ | |||||
// B: __/^^^^^\_____ | |||||
knob_report.dir++; | |||||
} else { | |||||
// Halfway through CW rotation (A != B) | |||||
// | |||||
// +---YOU ARE HERE (A=1, B=0) | |||||
// | +---OR HERE (A=0, B=1) | |||||
// | | | |||||
// v v | |||||
// A: _____/^^^^^\_____ | |||||
// B: ________/^^^^^\__ | |||||
knob_report.dir--; | |||||
} | |||||
} | |||||
} | |||||
knob_report_t knob_report_read(void) { | |||||
// Return knob report. | |||||
return knob_report; | |||||
} | |||||
void knob_report_reset(void) { | |||||
// Call this ASAP once you've processed the previous knob report. | |||||
// TODO: This should probably be called within `knob_report_read`. | |||||
knob_report.dir = 0; | |||||
knob_report.phase = 0; | |||||
} |
@ -1,28 +0,0 @@ | |||||
// Rotary knob implementation - Version 2. | |||||
// Uses 2 digital pins - D2 (via interrupt) & D6. | |||||
// #include "rev1.h" | |||||
#include <avr/io.h> | |||||
#include <avr/interrupt.h> | |||||
#include <stdbool.h> | |||||
#ifndef ENCODER_PIN_1 | |||||
#define ENCODER_PIN_1 PD2 | |||||
#endif | |||||
#ifndef ENCODER_PIN_2 | |||||
#define ENCODER_PIN_2 PD6 | |||||
#endif | |||||
#ifndef ENCODER_INT | |||||
#define ENCODER_INT INT2_vect | |||||
#endif | |||||
typedef struct knob_report_t { | |||||
int8_t dir; // Contains number of rotations that happened | |||||
int8_t phase; // Contains 0 if last rotation happened on 90 degrees, 1 if on 270 | |||||
} knob_report_t; | |||||
void knob_init(void); | |||||
knob_report_t knob_report_read(void); | |||||
void knob_report_reset(void); | |||||
bool knob_prev_a; | |||||
int8_t knob_dir; |
@ -1,304 +0,0 @@ | |||||
/* | |||||
Copyright 2012 Jun Wako <wakojun@gmail.com> | |||||
This program is free software: you can redistribute it and/or modify | |||||
it under the terms of the GNU General Public License as published by | |||||
the Free Software Foundation, either version 2 of the License, or | |||||
(at your option) any later version. | |||||
This program is distributed in the hope that it will be useful, | |||||
but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
GNU General Public License for more details. | |||||
You should have received a copy of the GNU General Public License | |||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
/* | |||||
* scan matrix | |||||
*/ | |||||
#include <stdint.h> | |||||
#include <stdbool.h> | |||||
#include <avr/io.h> | |||||
#include <avr/wdt.h> | |||||
#include <avr/interrupt.h> | |||||
#include <util/delay.h> | |||||
#include "print.h" | |||||
#include "debug.h" | |||||
#include "util.h" | |||||
#include "matrix.h" | |||||
#include "split_util.h" | |||||
#include <drivers/avr/pro_micro.h> | |||||
#include "serial.h" | |||||
#ifndef DEBOUNCE | |||||
# define DEBOUNCE 5 | |||||
#endif | |||||
#ifdef ENCODER_ENABLE_CUSTOM | |||||
#include "common/knob_v2.h" | |||||
#endif | |||||
#define ERROR_DISCONNECT_COUNT 5 | |||||
static uint8_t debouncing = DEBOUNCE; | |||||
static const int ROWS_PER_HAND = MATRIX_ROWS/2; | |||||
static uint8_t error_count = 0; | |||||
uint8_t is_master = 0 ; | |||||
static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | |||||
static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | |||||
/* matrix state(1:on, 0:off) */ | |||||
static matrix_row_t matrix[MATRIX_ROWS]; | |||||
static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | |||||
static matrix_row_t read_cols(void); | |||||
static void init_cols(void); | |||||
static void unselect_rows(void); | |||||
static void select_row(uint8_t row); | |||||
static uint8_t matrix_master_scan(void); | |||||
__attribute__ ((weak)) | |||||
void matrix_init_kb(void) { | |||||
matrix_init_user(); | |||||
} | |||||
__attribute__ ((weak)) | |||||
void matrix_scan_kb(void) { | |||||
matrix_scan_user(); | |||||
} | |||||
__attribute__ ((weak)) | |||||
void matrix_init_user(void) { | |||||
} | |||||
__attribute__ ((weak)) | |||||
void matrix_scan_user(void) { | |||||
} | |||||
inline | |||||
uint8_t matrix_rows(void) | |||||
{ | |||||
return MATRIX_ROWS; | |||||
} | |||||
inline | |||||
uint8_t matrix_cols(void) | |||||
{ | |||||
return MATRIX_COLS; | |||||
} | |||||
void matrix_init(void) | |||||
{ | |||||
debug_enable = true; | |||||
debug_matrix = true; | |||||
debug_mouse = true; | |||||
// initialize row and col | |||||
unselect_rows(); | |||||
init_cols(); | |||||
TX_RX_LED_INIT; | |||||
// initialize matrix state: all keys off | |||||
for (uint8_t i=0; i < MATRIX_ROWS; i++) { | |||||
matrix[i] = 0; | |||||
matrix_debouncing[i] = 0; | |||||
} | |||||
is_master = has_usb(); | |||||
#ifdef ENCODER_ENABLE_CUSTOM | |||||
knob_init(); //FOR ENCODER | |||||
#endif | |||||
matrix_init_quantum(); | |||||
} | |||||
uint8_t _matrix_scan(void) | |||||
{ | |||||
// Right hand is stored after the left in the matirx so, we need to offset it | |||||
int offset = isLeftHand ? 0 : (ROWS_PER_HAND); | |||||
for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | |||||
select_row(i); | |||||
_delay_us(30); // without this wait read unstable value. | |||||
matrix_row_t cols = read_cols(); | |||||
if (matrix_debouncing[i+offset] != cols) { | |||||
matrix_debouncing[i+offset] = cols; | |||||
debouncing = DEBOUNCE; | |||||
} | |||||
unselect_rows(); | |||||
} | |||||
if (debouncing) { | |||||
if (--debouncing) { | |||||
_delay_ms(1); | |||||
} else { | |||||
for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | |||||
matrix[i+offset] = matrix_debouncing[i+offset]; | |||||
} | |||||
} | |||||
} | |||||
#ifdef ENCODER_ENABLE_CUSTOM | |||||
knob_report_t knob_report = knob_report_read(); | |||||
knob_report_reset(); | |||||
matrix[5 + offset] &= 0b11111100; | |||||
if (knob_report.phase) { // I check for phase to avoid handling the rotation twice (on 90 and 270 degrees). | |||||
if (knob_report.dir > 0) { | |||||
matrix[5 + offset] |= 0b00000001; | |||||
} else if (knob_report.dir < 0) { | |||||
matrix[5 + offset] |= 0b00000010; | |||||
} | |||||
} | |||||
#endif | |||||
return 1; | |||||
} | |||||
int serial_transaction(void) { | |||||
int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | |||||
int ret=serial_update_buffers(); | |||||
if (ret ) { | |||||
return 1; | |||||
} | |||||
for (int i = 0; i < ROWS_PER_HAND; ++i) { | |||||
matrix[slaveOffset+i] = serial_slave_buffer[i]; | |||||
} | |||||
return 0; | |||||
} | |||||
uint8_t matrix_scan(void) | |||||
{ | |||||
if (is_master) { | |||||
matrix_master_scan(); | |||||
}else{ | |||||
matrix_slave_scan(); | |||||
int offset = (isLeftHand) ? ROWS_PER_HAND : 0; | |||||
for (int i = 0; i < ROWS_PER_HAND; ++i) { | |||||
matrix[offset+i] = serial_master_buffer[i]; | |||||
} | |||||
matrix_scan_quantum(); | |||||
} | |||||
return 1; | |||||
} | |||||
uint8_t matrix_master_scan(void) { | |||||
int ret = _matrix_scan(); | |||||
int offset = (isLeftHand) ? 0 : ROWS_PER_HAND; | |||||
for (int i = 0; i < ROWS_PER_HAND; ++i) { | |||||
serial_master_buffer[i] = matrix[offset+i]; | |||||
} | |||||
if( serial_transaction() ) { | |||||
// turn on the indicator led when halves are disconnected | |||||
TXLED1; | |||||
error_count++; | |||||
if (error_count > ERROR_DISCONNECT_COUNT) { | |||||
// reset other half if disconnected | |||||
int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | |||||
for (int i = 0; i < ROWS_PER_HAND; ++i) { | |||||
matrix[slaveOffset+i] = 0; | |||||
} | |||||
} | |||||
} else { | |||||
// turn off the indicator led on no error | |||||
TXLED0; | |||||
error_count = 0; | |||||
} | |||||
matrix_scan_quantum(); | |||||
return ret; | |||||
} | |||||
void matrix_slave_scan(void) { | |||||
_matrix_scan(); | |||||
int offset = (isLeftHand) ? 0 : ROWS_PER_HAND; | |||||
for (int i = 0; i < ROWS_PER_HAND; ++i) { | |||||
serial_slave_buffer[i] = matrix[offset+i]; | |||||
} | |||||
} | |||||
bool matrix_is_modified(void) | |||||
{ | |||||
if (debouncing) return false; | |||||
return true; | |||||
} | |||||
inline | |||||
bool matrix_is_on(uint8_t row, uint8_t col) | |||||
{ | |||||
return (matrix[row] & ((matrix_row_t)1<<col)); | |||||
} | |||||
inline | |||||
matrix_row_t matrix_get_row(uint8_t row) | |||||
{ | |||||
return matrix[row]; | |||||
} | |||||
void matrix_print(void) | |||||
{ | |||||
print("\nr/c 0123456789ABCDEF\n"); | |||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | |||||
phex(row); print(": "); | |||||
pbin_reverse16(matrix_get_row(row)); | |||||
print("\n"); | |||||
} | |||||
} | |||||
uint8_t matrix_key_count(void) | |||||
{ | |||||
uint8_t count = 0; | |||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | |||||
count += bitpop16(matrix[i]); | |||||
} | |||||
return count; | |||||
} | |||||
static void init_cols(void) | |||||
{ | |||||
for(int x = 0; x < MATRIX_COLS; x++) { | |||||
_SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF); | |||||
_SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF); | |||||
} | |||||
} | |||||
static matrix_row_t read_cols(void) | |||||
{ | |||||
matrix_row_t result = 0; | |||||
for(int x = 0; x < MATRIX_COLS; x++) { | |||||
result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x); | |||||
} | |||||
return result; | |||||
} | |||||
static void unselect_rows(void) | |||||
{ | |||||
for(int x = 0; x < ROWS_PER_HAND; x++) { | |||||
_SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF); | |||||
_SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF); | |||||
} | |||||
} | |||||
static void select_row(uint8_t row) | |||||
{ | |||||
_SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF); | |||||
_SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF); | |||||
} |
@ -1,70 +1 @@ | |||||
#ifndef REV1_H | |||||
#define REV1_H | |||||
#include "sol.h" | |||||
//void promicro_bootloader_jmp(bool program); | |||||
#include "quantum.h" | |||||
#ifdef RGBLIGHT_ENABLE | |||||
//rgb led driver | |||||
#include "ws2812.h" | |||||
#endif | |||||
#ifdef USE_I2C | |||||
#include <stddef.h> | |||||
#ifdef __AVR__ | |||||
#include <avr/io.h> | |||||
#include <avr/interrupt.h> | |||||
#endif | |||||
#endif | |||||
//void promicro_bootloader_jmp(bool program); | |||||
// LEL/LER/REL/RER are | |||||
// LeftEncoderLeft, LeftEncoderRight, RightEncoderLeft, and RightEncoderRight | |||||
#define LAYOUT( \ | |||||
L00, L01, L02, L03, L04, L05, L06, R06, R00, R01, R02, R03, R04, R05, \ | |||||
L10, L11, L12, L13, L14, L15, L16, R16, R10, R11, R12, R13, R14, R15, \ | |||||
L20, L21, L22, L23, L24, L25, L26, R26, R20, R21, R22, R23, R24, R25, \ | |||||
L30, L31, L32, L33, L34, L35, L36, R36, R30, R31, R32, R33, R34, R35, \ | |||||
L40, L41, L42, L43, L44, L45, L46, R46, R40, R41, R42, R43, R44, R45, \ | |||||
LEL, LER, L55, L56, R56, R50, REL, RER \ | |||||
) \ | |||||
{ \ | |||||
{ L00, L01, L02, L03, L04, L05, L06 }, \ | |||||
{ L10, L11, L12, L13, L14, L15, L16 }, \ | |||||
{ L20, L21, L22, L23, L24, L25, L26 }, \ | |||||
{ L30, L31, L32, L33, L34, L35, L36 }, \ | |||||
{ L40, L41, L42, L43, L44, L45, L46 }, \ | |||||
{ LEL, LER, KC_NO, KC_NO, KC_NO, L55, L56 }, \ | |||||
{ R05, R04, R03, R02, R01, R00, R06 }, \ | |||||
{ R15, R14, R13, R12, R11, R10, R16 }, \ | |||||
{ R25, R24, R23, R22, R21, R20, R26 }, \ | |||||
{ R35, R34, R33, R32, R31, R30, R36 }, \ | |||||
{ R45, R44, R43, R42, R41, R40, R46 }, \ | |||||
{ REL, RER, KC_NO, KC_NO, KC_NO, R50, R56 } \ | |||||
} | |||||
#define KC________ KC_TRNS | |||||
#define KC_RGB_MOD RGB_MOD | |||||
#define KC_FN FN | |||||
#define KC_ADJ ADJ | |||||
#define LAYOUT_kc( \ | |||||
L00, L01, L02, L03, L04, L05, L06, R06, R00, R01, R02, R03, R04, R05, \ | |||||
L10, L11, L12, L13, L14, L15, L16, R16, R10, R11, R12, R13, R14, R15, \ | |||||
L20, L21, L22, L23, L24, L25, L26, R26, R20, R21, R22, R23, R24, R25, \ | |||||
L30, L31, L32, L33, L34, L35, L36, R36, R30, R31, R32, R33, R34, R35, \ | |||||
L40, L41, L42, L43, L44, L45, L46, R46, R40, R41, R42, R43, R44, R45, \ | |||||
LEL, LER, L55, L56, R56, R50, REL, RER \ | |||||
) \ | |||||
LAYOUT( \ | |||||
KC_##L00, KC_##L01, KC_##L02, KC_##L03, KC_##L04, KC_##L05, KC_##L06, KC_##R06, KC_##R00, KC_##R01, KC_##R02, KC_##R03, KC_##R04, KC_##R05, \ | |||||
KC_##L10, KC_##L11, KC_##L12, KC_##L13, KC_##L14, KC_##L15, KC_##L16, KC_##R16, KC_##R10, KC_##R11, KC_##R12, KC_##R13, KC_##R14, KC_##R15, \ | |||||
KC_##L20, KC_##L21, KC_##L22, KC_##L23, KC_##L24, KC_##L25, KC_##L26, KC_##R26, KC_##R20, KC_##R21, KC_##R22, KC_##R23, KC_##R24, KC_##R25, \ | |||||
KC_##L30, KC_##L31, KC_##L32, KC_##L33, KC_##L34, KC_##L35, KC_##L36, KC_##R36, KC_##R30, KC_##R31, KC_##R32, KC_##R33, KC_##R34, KC_##R35, \ | |||||
KC_##L40, KC_##L41, KC_##L42, KC_##L43, KC_##L44, KC_##L45, KC_##L46, KC_##R46, KC_##R40, KC_##R41, KC_##R42, KC_##R43, KC_##R44, KC_##R45, \ | |||||
KC_##LEL, KC_##LER, KC_##L55, KC_##L56, KC_##R56, KC_##R50, KC_##REL, KC_##RER \ | |||||
) | |||||
#endif | |||||
#pragma once |
@ -1,2 +0,0 @@ | |||||
SRC += rev1/matrix.c \ | |||||
rev1/split_util.c |
@ -1,54 +0,0 @@ | |||||
#include <avr/io.h> | |||||
#include <avr/wdt.h> | |||||
#include <avr/power.h> | |||||
#include <avr/interrupt.h> | |||||
#include <util/delay.h> | |||||
#include <avr/eeprom.h> | |||||
#include "split_util.h" | |||||
#include "matrix.h" | |||||
#include "keyboard.h" | |||||
#include "serial.h" | |||||
volatile bool isLeftHand = true; | |||||
static void setup_handedness(void) { | |||||
#ifdef EE_HANDS | |||||
isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS); | |||||
#else | |||||
#if defined(MASTER_RIGHT) | |||||
isLeftHand = !has_usb(); | |||||
#else | |||||
isLeftHand = has_usb(); | |||||
#endif | |||||
#endif | |||||
} | |||||
static void keyboard_master_setup(void) { | |||||
serial_master_init(); | |||||
} | |||||
static void keyboard_slave_setup(void) { | |||||
serial_slave_init(); | |||||
} | |||||
bool has_usb(void) { | |||||
USBCON |= (1 << OTGPADE); //enables VBUS pad | |||||
_delay_us(5); | |||||
return (USBSTA & (1<<VBUS)); //checks state of VBUS | |||||
} | |||||
void split_keyboard_setup(void) { | |||||
setup_handedness(); | |||||
if (has_usb()) { | |||||
keyboard_master_setup(); | |||||
} else { | |||||
keyboard_slave_setup(); | |||||
} | |||||
sei(); | |||||
} | |||||
// this code runs before the usb and keyboard is initialized | |||||
void matrix_setup(void) { | |||||
split_keyboard_setup(); | |||||
} |
@ -1,15 +0,0 @@ | |||||
#ifndef SPLIT_KEYBOARD_UTIL_H | |||||
#define SPLIT_KEYBOARD_UTIL_H | |||||
#include <stdbool.h> | |||||
#include "eeconfig.h" | |||||
extern volatile bool isLeftHand; | |||||
// slave version of matix scan, defined in matrix.c | |||||
void matrix_slave_scan(void); | |||||
void split_keyboard_setup(void); | |||||
bool has_usb(void); | |||||
#endif |
@ -1,288 +0,0 @@ | |||||
/* | |||||
* WARNING: be careful changing this code, it is very timing dependent | |||||
*/ | |||||
#ifndef F_CPU | |||||
#define F_CPU 16000000 | |||||
#endif | |||||
#include <avr/io.h> | |||||
#include <avr/interrupt.h> | |||||
#include <util/delay.h> | |||||
#include <stdbool.h> | |||||
#include "serial.h" | |||||
#ifdef USE_SERIAL | |||||
#define _delay_sub_us(x) __builtin_avr_delay_cycles(x) | |||||
// Serial pulse period in microseconds. | |||||
#define SELECT_SERIAL_SPEED 1 | |||||
#if SELECT_SERIAL_SPEED == 0 | |||||
// Very High speed | |||||
#define SERIAL_DELAY 4 // micro sec | |||||
#define READ_WRITE_START_ADJUST 30 // cycles | |||||
#define READ_WRITE_WIDTH_ADJUST 10 // cycles | |||||
#elif SELECT_SERIAL_SPEED == 1 | |||||
// High speed | |||||
#define SERIAL_DELAY 6 // micro sec | |||||
#define READ_WRITE_START_ADJUST 23 // cycles | |||||
#define READ_WRITE_WIDTH_ADJUST 10 // cycles | |||||
#elif SELECT_SERIAL_SPEED == 2 | |||||
// Middle speed | |||||
#define SERIAL_DELAY 12 // micro sec | |||||
#define READ_WRITE_START_ADJUST 25 // cycles | |||||
#define READ_WRITE_WIDTH_ADJUST 10 // cycles | |||||
#elif SELECT_SERIAL_SPEED == 3 | |||||
// Low speed | |||||
#define SERIAL_DELAY 24 // micro sec | |||||
#define READ_WRITE_START_ADJUST 25 // cycles | |||||
#define READ_WRITE_WIDTH_ADJUST 10 // cycles | |||||
#elif SELECT_SERIAL_SPEED == 4 | |||||
// Very Low speed | |||||
#define SERIAL_DELAY 50 // micro sec | |||||
#define READ_WRITE_START_ADJUST 25 // cycles | |||||
#define READ_WRITE_WIDTH_ADJUST 10 // cycles | |||||
#else | |||||
#error Illegal Serial Speed | |||||
#endif | |||||
#define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2) | |||||
#define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2) | |||||
#define SLAVE_INT_WIDTH 1 | |||||
#define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY | |||||
uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0}; | |||||
uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0}; | |||||
#define SLAVE_DATA_CORRUPT (1<<0) | |||||
volatile uint8_t status = 0; | |||||
inline static | |||||
void serial_delay(void) { | |||||
_delay_us(SERIAL_DELAY); | |||||
} | |||||
inline static | |||||
void serial_delay_half1(void) { | |||||
_delay_us(SERIAL_DELAY_HALF1); | |||||
} | |||||
inline static | |||||
void serial_delay_half2(void) { | |||||
_delay_us(SERIAL_DELAY_HALF2); | |||||
} | |||||
inline static | |||||
void serial_output(void) { | |||||
SERIAL_PIN_DDR |= SERIAL_PIN_MASK; | |||||
} | |||||
// make the serial pin an input with pull-up resistor | |||||
inline static | |||||
void serial_input_with_pullup(void) { | |||||
SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK; | |||||
SERIAL_PIN_PORT |= SERIAL_PIN_MASK; | |||||
} | |||||
inline static | |||||
uint8_t serial_read_pin(void) { | |||||
return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK); | |||||
} | |||||
inline static | |||||
void serial_low(void) { | |||||
SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK; | |||||
} | |||||
inline static | |||||
void serial_high(void) { | |||||
SERIAL_PIN_PORT |= SERIAL_PIN_MASK; | |||||
} | |||||
void serial_master_init(void) { | |||||
serial_output(); | |||||
serial_high(); | |||||
} | |||||
void serial_slave_init(void) { | |||||
serial_input_with_pullup(); | |||||
// Enable INT3 | |||||
EIMSK |= _BV(INT3); | |||||
// Trigger on falling edge of INT3 | |||||
EICRA &= ~(_BV(ISC30) | _BV(ISC31)); | |||||
} | |||||
// Used by the sender to synchronize timing with the reciver. | |||||
static | |||||
void sync_recv(void) { | |||||
for (int i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) { | |||||
} | |||||
// This shouldn't hang if the slave disconnects because the | |||||
// serial line will float to high if the slave does disconnect. | |||||
while (!serial_read_pin()); | |||||
} | |||||
// Used by the reciver to send a synchronization signal to the sender. | |||||
static | |||||
void sync_send(void) { | |||||
serial_low(); | |||||
serial_delay(); | |||||
serial_high(); | |||||
} | |||||
// Reads a byte from the serial line | |||||
static | |||||
uint8_t serial_read_byte(void) { | |||||
uint8_t byte = 0; | |||||
_delay_sub_us(READ_WRITE_START_ADJUST); | |||||
for ( uint8_t i = 0; i < 8; ++i) { | |||||
serial_delay_half1(); // read the middle of pulses | |||||
byte = (byte << 1) | serial_read_pin(); | |||||
_delay_sub_us(READ_WRITE_WIDTH_ADJUST); | |||||
serial_delay_half2(); | |||||
} | |||||
return byte; | |||||
} | |||||
// Sends a byte with MSB ordering | |||||
static | |||||
void serial_write_byte(uint8_t data) { | |||||
uint8_t b = 1<<7; | |||||
while( b ) { | |||||
if(data & b) { | |||||
serial_high(); | |||||
} else { | |||||
serial_low(); | |||||
} | |||||
b >>= 1; | |||||
serial_delay(); | |||||
} | |||||
serial_low(); // sync_send() / senc_recv() need raise edge | |||||
} | |||||
// interrupt handle to be used by the slave device | |||||
ISR(SERIAL_PIN_INTERRUPT) { | |||||
serial_output(); | |||||
// slave send phase | |||||
uint8_t checksum = 0; | |||||
for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { | |||||
sync_send(); | |||||
serial_write_byte(serial_slave_buffer[i]); | |||||
checksum += serial_slave_buffer[i]; | |||||
} | |||||
sync_send(); | |||||
serial_write_byte(checksum); | |||||
// slave switch to input | |||||
sync_send(); //0 | |||||
serial_delay_half1(); //1 | |||||
serial_low(); //2 | |||||
serial_input_with_pullup(); //2 | |||||
serial_delay_half1(); //3 | |||||
// slave recive phase | |||||
uint8_t checksum_computed = 0; | |||||
for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { | |||||
sync_recv(); | |||||
serial_master_buffer[i] = serial_read_byte(); | |||||
checksum_computed += serial_master_buffer[i]; | |||||
} | |||||
sync_recv(); | |||||
uint8_t checksum_received = serial_read_byte(); | |||||
if ( checksum_computed != checksum_received ) { | |||||
status |= SLAVE_DATA_CORRUPT; | |||||
} else { | |||||
status &= ~SLAVE_DATA_CORRUPT; | |||||
} | |||||
sync_recv(); //weit master output to high | |||||
} | |||||
inline | |||||
bool serial_slave_DATA_CORRUPT(void) { | |||||
return status & SLAVE_DATA_CORRUPT; | |||||
} | |||||
// Copies the serial_slave_buffer to the master and sends the | |||||
// serial_master_buffer to the slave. | |||||
// | |||||
// Returns: | |||||
// 0 => no error | |||||
// 1 => slave did not respond | |||||
// 2 => checksum error | |||||
int serial_update_buffers(void) { | |||||
// this code is very time dependent, so we need to disable interrupts | |||||
cli(); | |||||
// signal to the slave that we want to start a transaction | |||||
serial_output(); | |||||
serial_low(); | |||||
_delay_us(SLAVE_INT_WIDTH); | |||||
// wait for the slaves response | |||||
serial_input_with_pullup(); | |||||
_delay_us(SLAVE_INT_RESPONSE_TIME); | |||||
// check if the slave is present | |||||
if (serial_read_pin()) { | |||||
// slave failed to pull the line low, assume not present | |||||
serial_output(); | |||||
serial_high(); | |||||
sei(); | |||||
return 1; | |||||
} | |||||
// master recive phase | |||||
// if the slave is present syncronize with it | |||||
uint8_t checksum_computed = 0; | |||||
// receive data from the slave | |||||
for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { | |||||
sync_recv(); | |||||
serial_slave_buffer[i] = serial_read_byte(); | |||||
checksum_computed += serial_slave_buffer[i]; | |||||
} | |||||
sync_recv(); | |||||
uint8_t checksum_received = serial_read_byte(); | |||||
if (checksum_computed != checksum_received) { | |||||
serial_output(); | |||||
serial_high(); | |||||
sei(); | |||||
return 2; | |||||
} | |||||
// master switch to output | |||||
sync_recv(); //0 | |||||
serial_delay(); //1 | |||||
serial_low(); //3 | |||||
serial_output(); // 3 | |||||
serial_delay_half1(); //4 | |||||
// master send phase | |||||
uint8_t checksum = 0; | |||||
for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { | |||||
sync_send(); | |||||
serial_write_byte(serial_master_buffer[i]); | |||||
checksum += serial_master_buffer[i]; | |||||
} | |||||
sync_send(); | |||||
serial_write_byte(checksum); | |||||
// always, release the line when not in use | |||||
sync_send(); | |||||
sei(); | |||||
return 0; | |||||
} | |||||
#endif |
@ -1,26 +0,0 @@ | |||||
#ifndef MY_SERIAL_H | |||||
#define MY_SERIAL_H | |||||
#include <stdbool.h> | |||||
/* TODO: some defines for interrupt setup */ | |||||
#define SERIAL_PIN_DDR DDRD | |||||
#define SERIAL_PIN_PORT PORTD | |||||
#define SERIAL_PIN_INPUT PIND | |||||
#define SERIAL_PIN_MASK _BV(PD3) //SErial pin goes here, D0-D3 | |||||
#define SERIAL_PIN_INTERRUPT INT3_vect //"INT#" of your serial pin | |||||
#define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2 | |||||
#define SERIAL_MASTER_BUFFER_LENGTH MATRIX_ROWS/2 | |||||
// Buffers for master - slave communication | |||||
extern volatile uint8_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH]; | |||||
extern volatile uint8_t serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH]; | |||||
void serial_master_init(void); | |||||
void serial_slave_init(void); | |||||
int serial_update_buffers(void); | |||||
bool serial_slave_data_corrupt(void); | |||||
#endif |
@ -1,7 +1,53 @@ | |||||
#ifndef SOL_H | |||||
#define SOL_H | |||||
#pragma once | |||||
#include "rev1.h" | |||||
#include "quantum.h" | #include "quantum.h" | ||||
#ifdef KEYBOARD_sol_rev1 | |||||
#include "rev1.h" | |||||
#elif KEYBOARD_sol_rev2 | |||||
#include "rev2.h" | |||||
#endif | #endif | ||||
#define LAYOUT( \ | |||||
L00, L01, L02, L03, L04, L05, L06, R06, R00, R01, R02, R03, R04, R05, \ | |||||
L10, L11, L12, L13, L14, L15, L16, R16, R10, R11, R12, R13, R14, R15, \ | |||||
L20, L21, L22, L23, L24, L25, L26, R26, R20, R21, R22, R23, R24, R25, \ | |||||
L30, L31, L32, L33, L34, L35, L36, R36, R30, R31, R32, R33, R34, R35, \ | |||||
L40, L41, L42, L43, L44, L45, L46, R46, R40, R41, R42, R43, R44, R45, \ | |||||
L55, L56, R56, R50 \ | |||||
) \ | |||||
{ \ | |||||
{ L00, L01, L02, L03, L04, L05, L06 }, \ | |||||
{ L10, L11, L12, L13, L14, L15, L16 }, \ | |||||
{ L20, L21, L22, L23, L24, L25, L26 }, \ | |||||
{ L30, L31, L32, L33, L34, L35, L36 }, \ | |||||
{ L40, L41, L42, L43, L44, L45, L46 }, \ | |||||
{ KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, L55, L56 }, \ | |||||
{ R05, R04, R03, R02, R01, R00, R06 }, \ | |||||
{ R15, R14, R13, R12, R11, R10, R16 }, \ | |||||
{ R25, R24, R23, R22, R21, R20, R26 }, \ | |||||
{ R35, R34, R33, R32, R31, R30, R36 }, \ | |||||
{ R45, R44, R43, R42, R41, R40, R46 }, \ | |||||
{ KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, R50, R56 } \ | |||||
} | |||||
#define KC________ KC_TRNS | |||||
#define KC_RGB_MOD RGB_MOD | |||||
#define KC_FN FN | |||||
#define KC_ADJ ADJ | |||||
#define LAYOUT_kc( \ | |||||
L00, L01, L02, L03, L04, L05, L06, R06, R00, R01, R02, R03, R04, R05, \ | |||||
L10, L11, L12, L13, L14, L15, L16, R16, R10, R11, R12, R13, R14, R15, \ | |||||
L20, L21, L22, L23, L24, L25, L26, R26, R20, R21, R22, R23, R24, R25, \ | |||||
L30, L31, L32, L33, L34, L35, L36, R36, R30, R31, R32, R33, R34, R35, \ | |||||
L40, L41, L42, L43, L44, L45, L46, R46, R40, R41, R42, R43, R44, R45, \ | |||||
L55, L56, R56, R50 \ | |||||
) \ | |||||
LAYOUT( \ | |||||
KC_##L00, KC_##L01, KC_##L02, KC_##L03, KC_##L04, KC_##L05, KC_##L06, KC_##R06, KC_##R00, KC_##R01, KC_##R02, KC_##R03, KC_##R04, KC_##R05, \ | |||||
KC_##L10, KC_##L11, KC_##L12, KC_##L13, KC_##L14, KC_##L15, KC_##L16, KC_##R16, KC_##R10, KC_##R11, KC_##R12, KC_##R13, KC_##R14, KC_##R15, \ | |||||
KC_##L20, KC_##L21, KC_##L22, KC_##L23, KC_##L24, KC_##L25, KC_##L26, KC_##R26, KC_##R20, KC_##R21, KC_##R22, KC_##R23, KC_##R24, KC_##R25, \ | |||||
KC_##L30, KC_##L31, KC_##L32, KC_##L33, KC_##L34, KC_##L35, KC_##L36, KC_##R36, KC_##R30, KC_##R31, KC_##R32, KC_##R33, KC_##R34, KC_##R35, \ | |||||
KC_##L40, KC_##L41, KC_##L42, KC_##L43, KC_##L44, KC_##L45, KC_##L46, KC_##R46, KC_##R40, KC_##R41, KC_##R42, KC_##R43, KC_##R44, KC_##R45, \ | |||||
KC_##L55, KC_##L56, KC_##R56, KC_##R50 \ | |||||
) |