diff --git a/src/firmware.c b/src/firmware.c index 888ca0b..93fed58 100644 --- a/src/firmware.c +++ b/src/firmware.c @@ -13,7 +13,6 @@ // TODO -// int main(void) { -// return 0; -// } +void main(void) { +} diff --git a/src/keyboard/ergodox/mcp23018.c b/src/keyboard/ergodox/mcp23018.c index ff0b73a..8d9b165 100644 --- a/src/keyboard/ergodox/mcp23018.c +++ b/src/keyboard/ergodox/mcp23018.c @@ -8,67 +8,26 @@ // TODO: this is not working yet -#if 0 // this is not the one we want; but it has all the right info -#include "lib-other/peter-fleury/i2cmaster/i2cmaster.h" - -// so we can say `TWI_ADDRESS|I2C_WRITE` -#define TWI_ADDRESS (MCP23018_TWI_ADDRESS<<1) - -uint8_t mcp23018_ready; // false - -uint8_t mcp23018_init(void) { - // declare vars - uint8_t error; - // see if the device is ready // - success: set `mcp23018_ready = true` and continue initializing // - failure: return `error`; we can try again later - error = i2c_start(TWI_ADDRESS|I2C_WRITE); - if(error) { - return error; - } else { - mcp23018_ready = true; - i2c_stop(); // release bus - } // set pin direction // - unused : input : 1 // - rows : output : 0 // - columns : input : 1 - i2c_start_wait(TWI_ADDRESS|I2C_WRITE); - i2c_write(IODIRA); // start register address - i2c_write(0b11000000); // IODIRA - i2c_write(0b11111111); // IODIRB - i2c_stop(); // set pull-up // - unused : on : 1 // - rows : on : 1 // - columns : on : 1 - i2c_start_wait(TWI_ADDRESS|I2C_WRITE); - i2c_write(GPPUA); // start register address - i2c_write(0b11111111); // GPPUA - i2c_write(0b11111111); // GPPUB - i2c_stop(); // set output pins high // - rows : high : 1 // - other : low : 0 (or ignored) - i2c_start_wait(TWI_ADDRESS|I2C_WRITE); - i2c_write(OLATA); // start register address - i2c_write(0b00111111); // OLATA - i2c_stop(); - - return 0; // success -} -#endif -#include #include -// #include "lib-other/arduino/arduino/libraries/Wire/utility/twi.h" -#include "lib-other/pjrc/blinky/print.h" -#include "lib-other/pjrc/blinky/usb_debug_only.h" #define MCP23018_h_INCLUDE_PRIVATE #include "mcp23018.h" @@ -86,27 +45,60 @@ uint8_t mcp23018_init(void) { #define OLATA 0x14 // output latch register #define OLATB 0x15 -static void twi_init(void) { -// TWSR &= ~( (1< +#include + +#define blink_led(time1, time2) { \ + /* Teensy 2.0 onboard LED on PD6 + on high, off low */ \ + PORTD |= (1<<6); \ + _delay_ms(time1); \ + PORTD &= ~(1<<6); \ + _delay_ms(time2); \ } -static uint8_t twi_start(void) { - TWCR = (1<=0; i--) { - (data & (1< + * ## Notes about Registers diff --git a/src/keyboard/ergodox/teensy-2-0.md b/src/keyboard/ergodox/teensy-2-0.md index b66415a..f4f3cbb 100644 --- a/src/keyboard/ergodox/teensy-2-0.md +++ b/src/keyboard/ergodox/teensy-2-0.md @@ -1,5 +1,6 @@ # Documentation : Teensy 2.0 + ## Pinouts and Pin assignments * `+` indicates pin @@ -45,6 +46,7 @@ internal pull-ups are enough (see datasheet section 20.5.1), but i think for this project we'll want external ones. + ## Notes about Registers ### General I/O (see datasheet section 10.2.1) @@ -101,6 +103,30 @@ * OCR = Output Compare Register * TCCR = Timer/Counter Control Register + +## I²C Status Codes (for Master modes) + +### Master Transmitter + +* `0x08` A START condition has been transmitted +* `0x10` A repeated START condition has been transmitted +* `0x18` SLA+W has been transmitted; ACK has been received +* `0x20` SLA+W has been transmitted; NOT ACK has been received +* `0x28` Data byte has been transmitted; ACK has been received +* `0x30` Data byte has been transmitted; NOT ACK has been received +* `0x38` Arbitration lost in SLA+W or data bytes + +### Master Receiver + +* `0x08` A START condition has been transmitted +* `0x10` A repeated START condition has been transmitted +* `0x38` Arbitration lost in SLA+R or NOT ACK bit +* `0x40` SLA+R has been transmitted; ACK has been received +* `0x48` SLA+R has been transmitted; NOT ACK has been received +* `0x50` Data byte has been received; ACK has been returned +* `0x58` Data byte has been received; NOT ACK has been returned + + ------------------------------------------------------------------------------- Copyright © 2012 Ben Blazak diff --git a/src/makefile b/src/makefile index 32902bf..af27005 100644 --- a/src/makefile +++ b/src/makefile @@ -12,11 +12,7 @@ TARGET = ergodox-firmware SRC = $(shell find -maxdepth 1 -name '*.c') \ $(shell find ./keyboard -name '*.c') \ - $(shell find ./lib -name '*.c') \ - ./lib-other/peter-fleury/i2cmaster/twimaster.c \ - ./lib-other/pjrc/blinky/print.c \ - ./lib-other/pjrc/blinky/usb_debug_only.c \ - ./lib-other/arduino/arduino/libraries/Wire/utility/twi.c + $(shell find ./lib -name '*.c') EXTRAINCDIRS = . diff --git a/src/test.c b/src/test.c deleted file mode 100644 index 1868c23..0000000 --- a/src/test.c +++ /dev/null @@ -1,102 +0,0 @@ -#include -#include - -#include "lib-other/pjrc/blinky/print.h" -#include "lib-other/pjrc/blinky/usb_debug_only.h" - -#include "lib/data-types.h" - -#define TEENSY_2_0_h_INCLUDE_PRIVATE -#include "keyboard/ergodox/teensy-2-0.h" - -int blink_leds(void); -int test_i2c(void); -int test_kb_teensy(void); -int main(void); - -// ---------------------------------------------------------------------------- - -int main(void) { - teensy_init(); - - KB_LED1_SET_PERCENT(.03); - KB_LED2_SET_PERCENT(.03); - KB_LED3_SET_PERCENT(.03); - -// blink_leds(); -// print("--------------"); - test_i2c(); - print("--------------"); - test_kb_teensy(); -} - -// ---------------------------------------------------------------------------- - -int blink_leds(void) { - for (uint8_t i=0; i<3; i++) { - KB_LED1_SET(0x10); - KB_LED2_SET(0x20); - KB_LED3_SET(0xFF); - _delay_ms(500); - KB_LED1_SET(0x20); - KB_LED2_SET(0xFF); - KB_LED3_SET(0x10); - _delay_ms(500); - KB_LED1_SET(0xFF); - KB_LED2_SET(0x10); - KB_LED3_SET(0x20); - _delay_ms(500); - } - - for (uint8_t i=0; i<2; i++) { - KB_LED1_OFF; - KB_LED2_OFF; - KB_LED3_OFF; - _delay_ms(500); - KB_LED1_ON; - KB_LED2_ON; - KB_LED3_ON; - _delay_ms(500); - } - - bool counting_up = true; - for (uint8_t i=0;;) { - (counting_up) ? i++ : i--; - if (i == 0xFF) - counting_up = false; - else if (i == 0) - counting_up = true; - - KB_LED1_SET(i/2); - KB_LED2_SET(i/2); - KB_LED3_SET(i/2); - _delay_ms(10); - } -} - -int test_i2c(void) { - usb_init(); - _delay_ms(500); // just to be safe - - uint8_t ret; - - uint8_t len; - char str[10]; - - print("\nhello"); - - ret = teensy_init(); - print("\nteensy_init() returned: 0x"); phex(ret); - - ret = mcp23018_init(); - print("\nmcp23018_init() returned: 0x"); phex(ret); - - usb_debug_flush_output(); - return 0; -} - -// ---------------------------------------------------------------------------- - -int test_kb_teensy(void) { -} - diff --git a/src/test/makefile b/src/test/makefile new file mode 100644 index 0000000..657fec0 --- /dev/null +++ b/src/test/makefile @@ -0,0 +1,34 @@ +# ----------------------------------------------------------------------------- +# This is mostly a stub at the moment, but I'm keeping it separate so I can +# mess with it later without changing the original +# ----------------------------------------------------------------------------- +# Copyright (c) 2012 Ben Blazak +# Released under The MIT License (MIT) (see "license.md") +# Project located at +# ----------------------------------------------------------------------------- + + +TARGET = test + +EXTRAINCDIRS = . + +TEENSY_MAKE = $(MAKE) -f '../lib-other/pjrc/blinky/Makefile' \ + TARGET='$(TARGET)' \ + EXTRAINCDIRS='$(EXTRAINCDIRS)' + +.PHONY: test_pwm test_twi + +test_pwm: + $(TEENSY_MAKE) all \ + SRC='test_pwm.c' + +test_twi: + $(TEENSY_MAKE) all \ + SRC='test_twi.c' + +.PHONY: clean + +clean: + $(TEENSY_MAKE) clean \ + SRC='$(shell find -name '*.c')' + diff --git a/src/test/test_pwm.c b/src/test/test_pwm.c new file mode 100644 index 0000000..aadb02a --- /dev/null +++ b/src/test/test_pwm.c @@ -0,0 +1,65 @@ +/* ---------------------------------------------------------------------------- + * Test the Teensy 2.0 PWM code (see `#include`s) + * ---------------------------------------------------------------------------- + * Copyright (c) 2012 Ben Blazak + * Released under The MIT License (MIT) (see "license.md") + * Project located at + * ------------------------------------------------------------------------- */ + + +#include + +#define TEENSY_2_0_h_INCLUDE_PRIVATE +#include "../keyboard/ergodox/teensy-2-0.h" +#include "../keyboard/ergodox/teensy-2-0.c" + + +#define bool uint8_t +#define true 1 +#define false 0 + + +void main(void) { + teensy_init(); + + for (uint8_t i=0; i<3; i++) { + KB_LED1_SET(0x10); + KB_LED2_SET(0x20); + KB_LED3_SET(0xFF); + _delay_ms(500); + KB_LED1_SET(0x20); + KB_LED2_SET(0xFF); + KB_LED3_SET(0x10); + _delay_ms(500); + KB_LED1_SET(0xFF); + KB_LED2_SET(0x10); + KB_LED3_SET(0x20); + _delay_ms(500); + } + + for (uint8_t i=0; i<2; i++) { + KB_LED1_OFF; + KB_LED2_OFF; + KB_LED3_OFF; + _delay_ms(500); + KB_LED1_ON; + KB_LED2_ON; + KB_LED3_ON; + _delay_ms(500); + } + + bool counting_up = true; + for (uint8_t i=0;;) { + (counting_up) ? i++ : i--; + if (i == 0xFF) + counting_up = false; + else if (i == 0) + counting_up = true; + + KB_LED1_SET(i/2); + KB_LED2_SET(i/2); + KB_LED3_SET(i/2); + _delay_ms(10); + } +} + diff --git a/src/test/test_twi.c b/src/test/test_twi.c new file mode 100644 index 0000000..4801c8f --- /dev/null +++ b/src/test/test_twi.c @@ -0,0 +1,105 @@ +/* ---------------------------------------------------------------------------- + * Test for ACK on address write (with a Teensy 2.0 and I/O expander) + * ---------------------------------------------------------------------------- + * Copyright (c) 2012 Ben Blazak + * Released under The MIT License (MIT) (see "license.md") + * Project located at + * ------------------------------------------------------------------------- */ + +#include +#include +#include + + +// processor frequency (from ) +#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n)) +#define CPU_16MHz 0x00 +#define CPU_8MHz 0x01 +#define CPU_4MHz 0x02 +#define CPU_2MHz 0x03 +#define CPU_1MHz 0x04 +#define CPU_500kHz 0x05 +#define CPU_250kHz 0x06 +#define CPU_125kHz 0x07 +#define CPU_62kHz 0x08 + + +// ---------------------------------------------------------------------------- +// helper functions +// ---------------------------------------------------------------------------- + +#define blink_led(time1, time2) { \ + /* Teensy 2.0 onboard LED on PD6 + on high, off low */ \ + PORTD |= (1<<6); \ + _delay_ms(time1); \ + PORTD &= ~(1<<6); \ + _delay_ms(time2); \ +} + +// ---------------------------------------------------------------------------- // TWI +// ---------------------------------------------------------------------------- + +void twi_init(void) { + TWSR &= ~( (1<