#include "util.h"
#include "matrix.h"
#include "i2c.h"
+#include "serial.h"
#include "split_util.h"
#include "pro_micro.h"
#include "config.h"
+Let's Split
+======
+
+This readme and most of the code are from https://github.com/ahtn/tmk_keyboard/
+
+Split keyboard firmware for Arduino Pro Micro or other ATmega32u4
+based boards.
+
+Features
+--------
+
+Some features supported by the firmware:
+
+* Either half can connect to the computer via USB, or both halves can be used
+ independently.
+* You only need 3 wires to connect the two halves. Two for VCC and GND and one
+ for serial communication.
+* Optional support for I2C connection between the two halves if for some
+ reason you require a faster connection between the two halves. Note this
+ requires an extra wire between halves and pull-up resistors on the data lines.
+
+Required Hardware
+-----------------
+
+Apart from diodes and key switches for the keyboard matrix in each half, you
+will need:
+
+* 2 Arduino Pro Micro's. You can find theses on aliexpress for ≈3.50USD each.
+* 2 TRS sockets
+* 1 TRS cable.
+
+Alternatively, you can use any sort of cable and socket that has at least 3
+wires. If you want to use I2C to communicate between halves, you will need a
+cable with at least 4 wires and 2x 4.7kΩ pull-up resistors
+
+Optional Hardware
+-----------------
+
+A speaker can be hooked-up to either side to the `5` (`C6`) pin and `GND`, and turned on via `AUDIO_ENABLE`.
+
+Wiring
+------
+
+The 3 wires of the TRS cable need to connect GND, VCC, and digital pin 3 (i.e.
+PD0 on the ATmega32u4) between the two Pro Micros.
+
+Then wire your key matrix to any of the remaining 17 IO pins of the pro micro
+and modify the `matrix.c` accordingly.
+
+The wiring for serial:
+
+![serial wiring](imgs/split-keyboard-serial-schematic.png)
+
+The wiring for i2c:
+
+![i2c wiring](imgs/split-keyboard-i2c-schematic.png)
+
+The pull-up resistors may be placed on either half. It is also possible
+to use 4 resistors and have the pull-ups in both halves, but this is
+unnecessary in simple use cases.
+
+Notes on Software Configuration
+-------------------------------
+
+Configuring the firmware is similar to any other TMK project. One thing
+to note is that `MATIX_ROWS` in `config.h` is the total number of rows between
+the two halves, i.e. if your split keyboard has 4 rows in each half, then
+`MATRIX_ROWS=8`.
+
+Also the current implementation assumes a maximum of 8 columns, but it would
+not be very difficult to adapt it to support more if required.
+
+
+Flashing
+--------
+
+If you define `EE_HANDS` in your `config.h`, you will need to set the
+EEPROM for the left and right halves. The EEPROM is used to store whether the
+half is left handed or right handed. This makes it so that the same firmware
+file will run on both hands instead of having to flash left and right handed
+versions of the firmware to each half. To flash the EEPROM file for the left
+half run:
+```
+make eeprom-left
+```
+and similarly for right half
+```
+make eeprom-right
+```
+
+After you have flashed the EEPROM for the first time, you then need to program
+the flash memory:
+```
+make program
+```
+Note that you need to program both halves, but you have the option of using
+different keymaps for each half. You could program the left half with a QWERTY
+layout and the right half with a Colemak layout. Then if you connect the left
+half to a computer by USB the keyboard will use QWERTY and Colemak when the
+right half is connected.
+
+
--- /dev/null
+/*
+ * 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"
+
+// Serial pulse period in microseconds. Its probably a bad idea to lower this
+// value.
+#define SERIAL_DELAY 24
+
+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_output(void) {
+ SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
+}
+
+// make the serial pin an input with pull-up resistor
+inline static
+void serial_input(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();
+
+ // Enable INT0
+ EIMSK |= _BV(INT0);
+ // Trigger on falling edge of INT0
+ EICRA &= ~(_BV(ISC00) | _BV(ISC01));
+}
+
+// Used by the master to synchronize timing with the slave.
+static
+void sync_recv(void) {
+ serial_input();
+ // 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());
+ serial_delay();
+}
+
+// Used by the slave to send a synchronization signal to the master.
+static
+void sync_send(void) {
+ serial_output();
+
+ serial_low();
+ serial_delay();
+
+ serial_high();
+}
+
+// Reads a byte from the serial line
+static
+uint8_t serial_read_byte(void) {
+ uint8_t byte = 0;
+ serial_input();
+ for ( uint8_t i = 0; i < 8; ++i) {
+ byte = (byte << 1) | serial_read_pin();
+ serial_delay();
+ _delay_us(1);
+ }
+
+ return byte;
+}
+
+// Sends a byte with MSB ordering
+static
+void serial_write_byte(uint8_t data) {
+ uint8_t b = 8;
+ serial_output();
+ while( b-- ) {
+ if(data & (1 << b)) {
+ serial_high();
+ } else {
+ serial_low();
+ }
+ serial_delay();
+ }
+}
+
+// interrupt handle to be used by the slave device
+ISR(SERIAL_PIN_INTERRUPT) {
+ sync_send();
+
+ uint8_t checksum = 0;
+ for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
+ serial_write_byte(serial_slave_buffer[i]);
+ sync_send();
+ checksum += serial_slave_buffer[i];
+ }
+ serial_write_byte(checksum);
+ sync_send();
+
+ // wait for the sync to finish sending
+ serial_delay();
+
+ // read the middle of pulses
+ _delay_us(SERIAL_DELAY/2);
+
+ uint8_t checksum_computed = 0;
+ for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
+ serial_master_buffer[i] = serial_read_byte();
+ sync_send();
+ checksum_computed += serial_master_buffer[i];
+ }
+ uint8_t checksum_received = serial_read_byte();
+ sync_send();
+
+ serial_input(); // end transaction
+
+ if ( checksum_computed != checksum_received ) {
+ status |= SLAVE_DATA_CORRUPT;
+ } else {
+ status &= ~SLAVE_DATA_CORRUPT;
+ }
+}
+
+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
+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(1);
+
+ // wait for the slaves response
+ serial_input();
+ serial_high();
+ _delay_us(SERIAL_DELAY);
+
+ // check if the slave is present
+ if (serial_read_pin()) {
+ // slave failed to pull the line low, assume not present
+ sei();
+ return 1;
+ }
+
+ // if the slave is present syncronize with it
+ sync_recv();
+
+ uint8_t checksum_computed = 0;
+ // receive data from the slave
+ for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
+ serial_slave_buffer[i] = serial_read_byte();
+ sync_recv();
+ checksum_computed += serial_slave_buffer[i];
+ }
+ uint8_t checksum_received = serial_read_byte();
+ sync_recv();
+
+ if (checksum_computed != checksum_received) {
+ sei();
+ return 1;
+ }
+
+ uint8_t checksum = 0;
+ // send data to the slave
+ for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
+ serial_write_byte(serial_master_buffer[i]);
+ sync_recv();
+ checksum += serial_master_buffer[i];
+ }
+ serial_write_byte(checksum);
+ sync_recv();
+
+ // always, release the line when not in use
+ serial_output();
+ serial_high();
+
+ sei();
+ return 0;
+}
--- /dev/null
+#ifndef MY_SERIAL_H
+#define MY_SERIAL_H
+
+#include "config.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(PD0)
+#define SERIAL_PIN_INTERRUPT INT0_vect
+
+#define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2
+#define SERIAL_MASTER_BUFFER_LENGTH 1
+
+// 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
#include "split_util.h"
#include "matrix.h"
#include "i2c.h"
+#include "serial.h"
#include "keyboard.h"
#include "config.h"
volatile bool isLeftHand = true;
static void setup_handedness(void) {
+ #ifdef EE_HANDS
isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
+ #else
+ #ifdef I2C_MASTER_RIGHT
+ isLeftHand = !has_usb();
+ #else
+ isLeftHand = has_usb();
+ #endif
+ #endif
}
static void keyboard_master_setup(void) {
#include <stdbool.h>
-#define EECONFIG_BOOTMAGIC_END (uint8_t *)10
-#define EECONFIG_HANDEDNESS EECONFIG_BOOTMAGIC_END
+#ifdef EE_HANDS
+ #define EECONFIG_BOOTMAGIC_END (uint8_t *)10
+ #define EECONFIG_HANDEDNESS EECONFIG_BOOTMAGIC_END
+#endif
#define SLAVE_I2C_ADDRESS 0x32
+++ /dev/null
-# Hey Emacs, this is a -*- makefile -*-
-
-# AVR-GCC Makefile template, derived from the WinAVR template (which
-# is public domain), believed to be neutral to any flavor of "make"
-# (GNU make, BSD make, SysV make)
-
-
-MCU = atmega328p
-FORMAT = ihex
-TARGET = keyboard-i2c-slave
-SRC = \
- $(TARGET).c \
- uno-matrix.c \
- ../serial.c \
- ../i2c-slave.c
-
-ASRC =
-OPT = s
-
-# Programming support using avrdude. Settings and variables.
-
-AVRDUDE_PROGRAMMER = arduino
-AVRDUDE_PORT = /dev/ttyACM0
-
-# Name of this Makefile (used for "make depend").
-MAKEFILE = Makefile
-
-# Debugging format.
-# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
-# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
-DEBUG = stabs
-
-# Compiler flag to set the C Standard level.
-# c89 - "ANSI" C
-# gnu89 - c89 plus GCC extensions
-# c99 - ISO C99 standard (not yet fully implemented)
-# gnu99 - c99 plus GCC extensions
-CSTANDARD = -std=gnu99
-
-# Place -D or -U options here
-CDEFS =
-
-# Place -I options here
-CINCS =
-
-
-CDEBUG = -g$(DEBUG)
-CWARN = -Wall -Wstrict-prototypes
-CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
-#CEXTRA = -Wa,-adhlns=$(<:.c=.lst)
-CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) \
- -fno-aggressive-loop-optimizations
-
-#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
-
-
-#Additional libraries.
-
-# Minimalistic printf version
-PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
-
-# Floating point printf version (requires MATH_LIB = -lm below)
-PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
-
-PRINTF_LIB =
-
-# Minimalistic scanf version
-SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
-
-# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
-SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
-
-SCANF_LIB =
-
-MATH_LIB = -lm
-
-# External memory options
-
-# 64 KB of external RAM, starting after internal RAM (ATmega128!),
-# used for variables (.data/.bss) and heap (malloc()).
-#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
-
-# 64 KB of external RAM, starting after internal RAM (ATmega128!),
-# only used for heap (malloc()).
-#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
-
-EXTMEMOPTS =
-
-#LDMAP = $(LDFLAGS) -Wl,-Map=$(TARGET).map,--cref
-LDFLAGS = $(EXTMEMOPTS) $(LDMAP) $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
-
-
-AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
-#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
-
-
-# Uncomment the following if you want avrdude's erase cycle counter.
-# Note that this counter needs to be initialized first using -Yn,
-# see avrdude manual.
-#AVRDUDE_ERASE_COUNTER = -y
-
-# Uncomment the following if you do /not/ wish a verification to be
-# performed after programming the device.
-#AVRDUDE_NO_VERIFY = -V
-
-# Increase verbosity level. Please use this when submitting bug
-# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
-# to submit bug reports.
-#AVRDUDE_VERBOSE = -v -v
-
-AVRDUDE_BASIC = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
-AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE) $(AVRDUDE_ERASE_COUNTER)
-
-
-CC = avr-gcc
-OBJCOPY = avr-objcopy
-OBJDUMP = avr-objdump
-SIZE = avr-size
-NM = avr-nm
-AVRDUDE = avrdude
-REMOVE = rm -f
-MV = mv -f
-
-# Define all object files.
-OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
-
-# Define all listing files.
-LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)
-
-# Combine all necessary flags and optional flags.
-# Add target processor to flags.
-ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)
-ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
-
-
-# Default target.
-all: build
-
-build: elf hex eep
-
-elf: $(TARGET).elf
-hex: $(TARGET).hex
-eep: $(TARGET).eep
-lss: $(TARGET).lss
-sym: $(TARGET).sym
-
-
-# Program the device.
-program: $(TARGET).hex $(TARGET).eep
- $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
-
-
-# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
-COFFCONVERT=$(OBJCOPY) --debugging \
---change-section-address .data-0x800000 \
---change-section-address .bss-0x800000 \
---change-section-address .noinit-0x800000 \
---change-section-address .eeprom-0x810000
-
-
-coff: $(TARGET).elf
- $(COFFCONVERT) -O coff-avr $(TARGET).elf $(TARGET).cof
-
-
-extcoff: $(TARGET).elf
- $(COFFCONVERT) -O coff-ext-avr $(TARGET).elf $(TARGET).cof
-
-
-.SUFFIXES: .elf .hex .eep .lss .sym
-
-.elf.hex:
- $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
-
-.elf.eep:
- -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
- --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
-
-# Create extended listing file from ELF output file.
-.elf.lss:
- $(OBJDUMP) -h -S $< > $@
-
-# Create a symbol table from ELF output file.
-.elf.sym:
- $(NM) -n $< > $@
-
-
-
-# Link: create ELF output file from object files.
-$(TARGET).elf: $(OBJ)
- $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)
-
-
-# Compile: create object files from C source files.
-.c.o:
- $(CC) -c $(ALL_CFLAGS) $< -o $@
-
-
-# Compile: create assembler files from C source files.
-.c.s:
- $(CC) -S $(ALL_CFLAGS) $< -o $@
-
-
-# Assemble: create object files from assembler source files.
-.S.o:
- $(CC) -c $(ALL_ASFLAGS) $< -o $@
-
-
-
-# Target: clean project.
-clean:
- $(REMOVE) $(TARGET).hex $(TARGET).eep $(TARGET).cof $(TARGET).elf \
- $(TARGET).map $(TARGET).sym $(TARGET).lss \
- $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d)
-
-depend:
- if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \
- then \
- sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \
- $(MAKEFILE).$$$$ && \
- $(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \
- fi
- echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \
- >> $(MAKEFILE); \
- $(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) $(ASRC) >> $(MAKEFILE)
-
-.PHONY: all build elf hex eep lss sym program coff extcoff clean depend
+++ /dev/null
-#include "../i2c-slave.h"
-#include "../serial.h"
-#include "uno-matrix.h"
-
-#include <avr/io.h>
-#include <avr/interrupt.h>
-#include <util/delay.h>
-
-void setup(void) {
- // give some time for noise to clear
- _delay_us(1000);
-
- // turn off arduino uno's led on pin 13
- DDRB |= (1 << 5);
- PORTB &= ~(1 << 5);
-
- matrix_init();
- /* i2c_slave_init(0x32); */
- serial_slave_init();
-
- /* serial_slave_buffer[0] = 0xa1; */
- /* serial_slave_buffer[1] = 0x52; */
- /* serial_slave_buffer[2] = 0xa2; */
- /* serial_slave_buffer[3] = 0x67; */
-
- // need interrupts for i2c slave code to work
- sei();
-}
-
-void loop(void) {
- matrix_scan();
- for(int i=0; i<MATRIX_ROWS; ++i) {
- slaveBuffer[i] = matrix_get_row(i);
- serial_slave_buffer[i] = slaveBuffer[i];
- }
-}
-
-int main(int argc, char *argv[]) {
- setup();
- while (1)
- loop();
-}
+++ /dev/null
-Code for Arduino uno (atmega328p) slave used for testing.
+++ /dev/null
-#define F_CPU 16000000UL
-
-#include <util/delay.h>
-#include <avr/io.h>
-#include <stdlib.h>
-
-#include "uno-matrix.h"
-
-#define debug(X) NULL
-#define debug_hex(X) NULL
-
-#ifndef DEBOUNCE
-# define DEBOUNCE 5
-#endif
-
-static uint8_t debouncing = DEBOUNCE;
-
-/* 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);
-
-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();
-
- // initialize matrix state: all keys off
- for (uint8_t i=0; i < MATRIX_ROWS; i++) {
- matrix[i] = 0;
- matrix_debouncing[i] = 0;
- }
-}
-
-uint8_t matrix_scan(void)
-{
- for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
- select_row(i);
- _delay_us(30); // without this wait read unstable value.
- matrix_row_t cols = read_cols();
- //Serial.println(cols, BIN);
- if (matrix_debouncing[i] != cols) {
- matrix_debouncing[i] = cols;
- if (debouncing) {
- debug("bounce!: "); debug_hex(debouncing); debug("\n");
- }
- debouncing = DEBOUNCE;
- }
- unselect_rows();
- }
-
- if (debouncing) {
- if (--debouncing) {
- _delay_ms(1);
- } else {
- for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
- matrix[i] = matrix_debouncing[i];
- }
- }
- }
-
- return 1;
-}
-
-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];
-}
-
-// TODO update this comment
-/* Column pin configuration
- * col: 0 1 2 3 4 5
- * pin: D3 D4 D5 D6 D7 B0
- */
-static void init_cols(void)
-{
- // Input with pull-up(DDR:0, PORT:1)
- DDRD &= ~(1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
- PORTD |= (1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
-
- DDRB &= ~(1<<0);
- PORTB |= (1<<0);
-}
-
-static matrix_row_t read_cols(void)
-{
- return (PIND&(1<<3) ? 0 : (1<<0)) |
- (PIND&(1<<4) ? 0 : (1<<1)) |
- (PIND&(1<<5) ? 0 : (1<<2)) |
- (PIND&(1<<6) ? 0 : (1<<3)) |
- (PIND&(1<<7) ? 0 : (1<<4)) |
- (PINB&(1<<0) ? 0 : (1<<5));
-}
-
-/* Row pin configuration
- * row: 0 1 2 3
- * pin: C0 C1 C2 C3
- */
-static void unselect_rows(void)
-{
- // Hi-Z(DDR:0, PORT:0) to unselect
- DDRC &= ~0xF;
- PORTC &= ~0xF;
-}
-
-static void select_row(uint8_t row)
-{
- // Output low(DDR:1, PORT:0) to select
- switch (row) {
- case 0:
- DDRC |= (1<<0);
- PORTC &= ~(1<<0);
- break;
- case 1:
- DDRC |= (1<<1);
- PORTC &= ~(1<<1);
- break;
- case 2:
- DDRC |= (1<<2);
- PORTC &= ~(1<<2);
- break;
- case 3:
- DDRC |= (1<<3);
- PORTC &= ~(1<<3);
- break;
- }
-}
+++ /dev/null
-#ifndef UNO_MATRIX
-#define UNO_MATRIX
-
-#define MATRIX_ROWS 4
-#define MATRIX_COLS 6
-
-#include <stdbool.h>
-
-typedef uint8_t matrix_row_t;
-
-uint8_t matrix_rows(void);
-uint8_t matrix_cols(void);
-void matrix_init(void);
-uint8_t matrix_scan(void);
-bool matrix_is_modified(void);
-bool matrix_is_on(uint8_t row, uint8_t col);
-matrix_row_t matrix_get_row(uint8_t row);
-
-#endif
--- /dev/null
+# Hey Emacs, this is a -*- makefile -*-
+
+# AVR-GCC Makefile template, derived from the WinAVR template (which
+# is public domain), believed to be neutral to any flavor of "make"
+# (GNU make, BSD make, SysV make)
+
+
+MCU = atmega328p
+FORMAT = ihex
+TARGET = keyboard-i2c-slave
+SRC = \
+ $(TARGET).c \
+ uno-matrix.c \
+ ../serial.c \
+ ../i2c-slave.c
+
+ASRC =
+OPT = s
+
+# Programming support using avrdude. Settings and variables.
+
+AVRDUDE_PROGRAMMER = arduino
+AVRDUDE_PORT = /dev/ttyACM0
+
+# Name of this Makefile (used for "make depend").
+MAKEFILE = Makefile
+
+# Debugging format.
+# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
+# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
+DEBUG = stabs
+
+# Compiler flag to set the C Standard level.
+# c89 - "ANSI" C
+# gnu89 - c89 plus GCC extensions
+# c99 - ISO C99 standard (not yet fully implemented)
+# gnu99 - c99 plus GCC extensions
+CSTANDARD = -std=gnu99
+
+# Place -D or -U options here
+CDEFS =
+
+# Place -I options here
+CINCS =
+
+
+CDEBUG = -g$(DEBUG)
+CWARN = -Wall -Wstrict-prototypes
+CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
+#CEXTRA = -Wa,-adhlns=$(<:.c=.lst)
+CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) \
+ -fno-aggressive-loop-optimizations
+
+#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
+
+
+#Additional libraries.
+
+# Minimalistic printf version
+PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
+
+# Floating point printf version (requires MATH_LIB = -lm below)
+PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
+
+PRINTF_LIB =
+
+# Minimalistic scanf version
+SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
+
+# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
+SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
+
+SCANF_LIB =
+
+MATH_LIB = -lm
+
+# External memory options
+
+# 64 KB of external RAM, starting after internal RAM (ATmega128!),
+# used for variables (.data/.bss) and heap (malloc()).
+#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
+
+# 64 KB of external RAM, starting after internal RAM (ATmega128!),
+# only used for heap (malloc()).
+#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
+
+EXTMEMOPTS =
+
+#LDMAP = $(LDFLAGS) -Wl,-Map=$(TARGET).map,--cref
+LDFLAGS = $(EXTMEMOPTS) $(LDMAP) $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
+
+
+AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
+#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
+
+
+# Uncomment the following if you want avrdude's erase cycle counter.
+# Note that this counter needs to be initialized first using -Yn,
+# see avrdude manual.
+#AVRDUDE_ERASE_COUNTER = -y
+
+# Uncomment the following if you do /not/ wish a verification to be
+# performed after programming the device.
+#AVRDUDE_NO_VERIFY = -V
+
+# Increase verbosity level. Please use this when submitting bug
+# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
+# to submit bug reports.
+#AVRDUDE_VERBOSE = -v -v
+
+AVRDUDE_BASIC = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
+AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE) $(AVRDUDE_ERASE_COUNTER)
+
+
+CC = avr-gcc
+OBJCOPY = avr-objcopy
+OBJDUMP = avr-objdump
+SIZE = avr-size
+NM = avr-nm
+AVRDUDE = avrdude
+REMOVE = rm -f
+MV = mv -f
+
+# Define all object files.
+OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
+
+# Define all listing files.
+LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)
+
+# Combine all necessary flags and optional flags.
+# Add target processor to flags.
+ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)
+ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
+
+
+# Default target.
+all: build
+
+build: elf hex eep
+
+elf: $(TARGET).elf
+hex: $(TARGET).hex
+eep: $(TARGET).eep
+lss: $(TARGET).lss
+sym: $(TARGET).sym
+
+
+# Program the device.
+program: $(TARGET).hex $(TARGET).eep
+ $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
+
+
+# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
+COFFCONVERT=$(OBJCOPY) --debugging \
+--change-section-address .data-0x800000 \
+--change-section-address .bss-0x800000 \
+--change-section-address .noinit-0x800000 \
+--change-section-address .eeprom-0x810000
+
+
+coff: $(TARGET).elf
+ $(COFFCONVERT) -O coff-avr $(TARGET).elf $(TARGET).cof
+
+
+extcoff: $(TARGET).elf
+ $(COFFCONVERT) -O coff-ext-avr $(TARGET).elf $(TARGET).cof
+
+
+.SUFFIXES: .elf .hex .eep .lss .sym
+
+.elf.hex:
+ $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
+
+.elf.eep:
+ -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
+ --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
+
+# Create extended listing file from ELF output file.
+.elf.lss:
+ $(OBJDUMP) -h -S $< > $@
+
+# Create a symbol table from ELF output file.
+.elf.sym:
+ $(NM) -n $< > $@
+
+
+
+# Link: create ELF output file from object files.
+$(TARGET).elf: $(OBJ)
+ $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)
+
+
+# Compile: create object files from C source files.
+.c.o:
+ $(CC) -c $(ALL_CFLAGS) $< -o $@
+
+
+# Compile: create assembler files from C source files.
+.c.s:
+ $(CC) -S $(ALL_CFLAGS) $< -o $@
+
+
+# Assemble: create object files from assembler source files.
+.S.o:
+ $(CC) -c $(ALL_ASFLAGS) $< -o $@
+
+
+
+# Target: clean project.
+clean:
+ $(REMOVE) $(TARGET).hex $(TARGET).eep $(TARGET).cof $(TARGET).elf \
+ $(TARGET).map $(TARGET).sym $(TARGET).lss \
+ $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d)
+
+depend:
+ if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \
+ then \
+ sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \
+ $(MAKEFILE).$$$$ && \
+ $(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \
+ fi
+ echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \
+ >> $(MAKEFILE); \
+ $(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) $(ASRC) >> $(MAKEFILE)
+
+.PHONY: all build elf hex eep lss sym program coff extcoff clean depend
--- /dev/null
+#include "../i2c-slave.h"
+#include "../serial.h"
+#include "uno-matrix.h"
+
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <util/delay.h>
+
+void setup(void) {
+ // give some time for noise to clear
+ _delay_us(1000);
+
+ // turn off arduino uno's led on pin 13
+ DDRB |= (1 << 5);
+ PORTB &= ~(1 << 5);
+
+ matrix_init();
+ /* i2c_slave_init(0x32); */
+ serial_slave_init();
+
+ /* serial_slave_buffer[0] = 0xa1; */
+ /* serial_slave_buffer[1] = 0x52; */
+ /* serial_slave_buffer[2] = 0xa2; */
+ /* serial_slave_buffer[3] = 0x67; */
+
+ // need interrupts for i2c slave code to work
+ sei();
+}
+
+void loop(void) {
+ matrix_scan();
+ for(int i=0; i<MATRIX_ROWS; ++i) {
+ slaveBuffer[i] = matrix_get_row(i);
+ serial_slave_buffer[i] = slaveBuffer[i];
+ }
+}
+
+int main(int argc, char *argv[]) {
+ setup();
+ while (1)
+ loop();
+}
--- /dev/null
+Code for Arduino uno (atmega328p) slave used for testing.
--- /dev/null
+#define F_CPU 16000000UL
+
+#include <util/delay.h>
+#include <avr/io.h>
+#include <stdlib.h>
+
+#include "uno-matrix.h"
+
+#define debug(X) NULL
+#define debug_hex(X) NULL
+
+#ifndef DEBOUNCE
+# define DEBOUNCE 5
+#endif
+
+static uint8_t debouncing = DEBOUNCE;
+
+/* 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);
+
+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();
+
+ // initialize matrix state: all keys off
+ for (uint8_t i=0; i < MATRIX_ROWS; i++) {
+ matrix[i] = 0;
+ matrix_debouncing[i] = 0;
+ }
+}
+
+uint8_t matrix_scan(void)
+{
+ for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
+ select_row(i);
+ _delay_us(30); // without this wait read unstable value.
+ matrix_row_t cols = read_cols();
+ //Serial.println(cols, BIN);
+ if (matrix_debouncing[i] != cols) {
+ matrix_debouncing[i] = cols;
+ if (debouncing) {
+ debug("bounce!: "); debug_hex(debouncing); debug("\n");
+ }
+ debouncing = DEBOUNCE;
+ }
+ unselect_rows();
+ }
+
+ if (debouncing) {
+ if (--debouncing) {
+ _delay_ms(1);
+ } else {
+ for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
+ matrix[i] = matrix_debouncing[i];
+ }
+ }
+ }
+
+ return 1;
+}
+
+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];
+}
+
+// TODO update this comment
+/* Column pin configuration
+ * col: 0 1 2 3 4 5
+ * pin: D3 D4 D5 D6 D7 B0
+ */
+static void init_cols(void)
+{
+ // Input with pull-up(DDR:0, PORT:1)
+ DDRD &= ~(1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
+ PORTD |= (1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
+
+ DDRB &= ~(1<<0);
+ PORTB |= (1<<0);
+}
+
+static matrix_row_t read_cols(void)
+{
+ return (PIND&(1<<3) ? 0 : (1<<0)) |
+ (PIND&(1<<4) ? 0 : (1<<1)) |
+ (PIND&(1<<5) ? 0 : (1<<2)) |
+ (PIND&(1<<6) ? 0 : (1<<3)) |
+ (PIND&(1<<7) ? 0 : (1<<4)) |
+ (PINB&(1<<0) ? 0 : (1<<5));
+}
+
+/* Row pin configuration
+ * row: 0 1 2 3
+ * pin: C0 C1 C2 C3
+ */
+static void unselect_rows(void)
+{
+ // Hi-Z(DDR:0, PORT:0) to unselect
+ DDRC &= ~0xF;
+ PORTC &= ~0xF;
+}
+
+static void select_row(uint8_t row)
+{
+ // Output low(DDR:1, PORT:0) to select
+ switch (row) {
+ case 0:
+ DDRC |= (1<<0);
+ PORTC &= ~(1<<0);
+ break;
+ case 1:
+ DDRC |= (1<<1);
+ PORTC &= ~(1<<1);
+ break;
+ case 2:
+ DDRC |= (1<<2);
+ PORTC &= ~(1<<2);
+ break;
+ case 3:
+ DDRC |= (1<<3);
+ PORTC &= ~(1<<3);
+ break;
+ }
+}
--- /dev/null
+#ifndef UNO_MATRIX
+#define UNO_MATRIX
+
+#define MATRIX_ROWS 4
+#define MATRIX_COLS 6
+
+#include <stdbool.h>
+
+typedef uint8_t matrix_row_t;
+
+uint8_t matrix_rows(void);
+uint8_t matrix_cols(void);
+void matrix_init(void);
+uint8_t matrix_scan(void);
+bool matrix_is_modified(void);
+bool matrix_is_on(uint8_t row, uint8_t col);
+matrix_row_t matrix_get_row(uint8_t row);
+
+#endif