diff --git a/doc/references.md b/doc/references.md index a773443..12a83b4 100644 --- a/doc/references.md +++ b/doc/references.md @@ -49,6 +49,9 @@ (http://www.cprogramming.com/tutorial/bitwise_operators.html) (on ) +* [AVR Tutorials - \[TUT\] \[C\] Bit manipulation (AKA "Programming 101")] + (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=37871&start=0) + * [In C++ is "const" after type ID acceptable?] (http://stackoverflow.com/questions/988069/in-c-is-const-after-type-id-acceptable) (on ) @@ -63,6 +66,10 @@ * [Markdown: Syntax] (http://daringfireball.net/projects/markdown/syntax) +* [Keyboard Scan Rates] + (http://geekhack.org/showwiki.php?title=Keyboard+scan+rates) + list (on ) + ## AVR C stuff diff --git a/src/hardware/ergodox--keyboard.c b/src/ergodox/layout.c similarity index 97% rename from src/hardware/ergodox--keyboard.c rename to src/ergodox/layout.c index 53d1d15..0939b76 100644 --- a/src/hardware/ergodox--keyboard.c +++ b/src/ergodox/layout.c @@ -1,6 +1,6 @@ /* ---------------------------------------------------------------------------- - * ergoDOX keyboard specific stuff - * - public things are prefixed by `kb_` or `KB_` + * ergoDOX layout specific stuff + * - public things are prefixed by `layout_` or `LAYOUT_` * ---------------------------------------------------------------------------- * Copyright (c) 2012 Ben Blazak * Released under The MIT License (MIT) (see "license.md") at @@ -33,7 +33,7 @@ * ------------------------------------------------------------------------- */ -#include "ergodox.h" +#include "layout.h" /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/hardware/ergodox.h b/src/ergodox/layout.h similarity index 71% rename from src/hardware/ergodox.h rename to src/ergodox/layout.h index 34dbdb6..60bbfce 100644 --- a/src/hardware/ergodox.h +++ b/src/ergodox/layout.h @@ -1,4 +1,7 @@ /* ---------------------------------------------------------------------------- + * ergoDOX layout specific stuff + * - public things are prefixed by `layout_` or `LAYOUT_` + * ---------------------------------------------------------------------------- * Copyright (c) 2012 Ben Blazak * Released under The MIT License (MIT) (see "license.md") at * @@ -10,9 +13,5 @@ #define KB_COLUMNS 7 // must match real life -void controller_init(); - -void kb_init(); - // TODO diff --git a/src/hardware/ergodox--teensy-2-0--mcp23018.c b/src/ergodox/teensy-2-0--mcp23018.c similarity index 78% rename from src/hardware/ergodox--teensy-2-0--mcp23018.c rename to src/ergodox/teensy-2-0--mcp23018.c index d26da51..421a4df 100644 --- a/src/hardware/ergodox--teensy-2-0--mcp23018.c +++ b/src/ergodox/teensy-2-0--mcp23018.c @@ -16,7 +16,7 @@ * Pinouts and Pin assignments * - '+' indicates pin * - 'o' indicates unused pin - * - '-'s inserted between some of the pin names for readability + * - '-'s inserted between some of the pin functions for readability * - 'OC**' pins enclosed in parenthesis had lines over them in the pinout * ---------------------------------------------------------------------------- * ---------------------------------------------------------------------------- @@ -39,16 +39,16 @@ * ---------------------------------------------------------------------------- * MCP23018 * ======== Vss(GND) +01---.---28+ NC - * GPNC +02 27+ GPAC + * NC +02 27+ GPA7 * GPB0 +03 26+ GPA6 - * GPB1 +04 25+ GPAC - * GPB2 +05 24+ GPAC - * GPB3 +06 23+ GPAC - * GPB4 +07 22+ GPAC - * GPB5 +08 21+ GPAC - * GPB6 +09 20+ GPAC - * GPB7 +10 19+ INTC - * Vdd(Vcc) +11 18+ INTC + * GPB1 +04 25+ GPA5 + * GPB2 +05 24+ GPA4 + * GPB3 +06 23+ GPA3 + * GPB4 +07 22+ GPA2 + * GPB5 +08 21+ GPA1 + * GPB6 +09 20+ GPA0 + * GPB7 +10 19+ INTA + * Vdd(Vcc) +11 18+ INTB * SCL +12 17+ NC * SDA +13 16+ RESET * NC +14-------15+ ADDR @@ -93,12 +93,12 @@ * o14-------15+ ADDR (see note) * * notes: - * - ADDR (pin15): Set slave address to 0b0100000 by connecting to Vss(GND) - * - note: The user-defined bits are the three least significant + * - ADDR (pin15): Set slave address to 0b0100000 by connecting to Vss(GND). + * (The user-defined bits are the three least significant). * ------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------- - * Registers + * Notes about Registers * ---------------------------------------------------------------------------- * General I/O (see datasheet section 10.2.1) * @@ -134,11 +134,16 @@ * the default value of all the bits in those registers is 0) * * abbreviations: - * - OC = Output Compare + * - OCR = Output Compare Register * - TCCR = Timer/Counter Control Register * ------------------------------------------------------------------------- */ +#include "lib/twi.h" + +#include "teensy-2-0--mcp23018.h" + + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~ macros ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ @@ -155,40 +160,13 @@ #define CPU_125kHz 0x07 #define CPU_62kHz 0x08 -// pins -#define CONTROLLER_LED1_ON (OCR1A = 0xFFFF) -#define CONTROLLER_LED1_OFF (OCR1A = 0) -#define CONTROLLER_LED1_SET(n) (OCR1A = (n)) -#define CONTROLLER_LED1_SET_PERCENT(n) (OCR1A = (n) * 0xFFFF) -#define CONTROLLER_LED2_ON (OCR1B = 0xFFFF) -#define CONTROLLER_LED2_OFF (OCR1B = 0) -#define CONTROLLER_LED2_SET(n) (OCR1B = (n)) -#define CONTROLLER_LED2_SET_PERCENT(n) (OCR1B = (n) * 0xFFFF) -#define CONTROLLER_LED3_ON (OCR1C = 0xFFFF) -#define CONTROLLER_LED3_OFF (OCR1C = 0) -#define CONTROLLER_LED3_SET(n) (OCR1C = (n)) -#define CONTROLLER_LED3_SET_PERCENT(n) (OCR1C = (n) * 0xFFFF) +// TWI frequency +#define TWI_FREQ 400000 // (see lib/twi.(h|c)) + +// pins +// --- rows ?TODO +// --- columns ?TODO -// teensy pins -// TODO -// I2C SCL PD0 -// I2C SDA PD1 -// LED1 OC1A PB5 -// LED2 OC1B PB6 -// LED3 OC1C PB7 -// ROW_0 PF0 -// ROW_1 PF1 -// ROW_2 PF4 -// ROW_3 PF5 -// ROW_4 PF6 -// ROW_5 PF7 -// COLUMN_0_RH PB4 -// COLUMN_1_RH PC6 -// COLUMN_2_RH PC7 -// COLUMN_3_RH PD2 -// COLUMN_4_RH PD3 -// COLUMN_5_RH PD7 -// COLUMN_6_RH PB0 // mcp23018 pins // TODO @@ -214,33 +192,41 @@ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ void controller_init() { - teensy_init(); - mcp23018_init(); + teensy_init(); // must be first + mcp23018_init(); // must be second } -// TODO -// - set internal brown-out detection circuit? void teensy_init() { CPU_PRESCALE(CPU_16MHz); // speed should match F_CPU in makefile // unused pins DDRB &= ~0b00001110; // set B(1,2,3) as input - PORTB |= 0b00001110; // set B(1,2,3) internal pullup enabled + PORTB |= 0b00001110; // set B(1,2,3) internal pull-up enabled DDRD &= ~0b01110000; // set D(4,5,6) as input - PORTD |= 0b01110000; // set D(4,5,6) internal pullup enabled + PORTD |= 0b01110000; // set D(4,5,6) internal pull-up enabled DDRE &= ~0b01000000; // set E(6) as input - PORTE |= 0b01000000; // set E(6) internal pullup enabled + PORTE |= 0b01000000; // set E(6) internal pull-up enabled - // LEDs with PWM + // LEDs DDRB |= 0b11100000; // set B(5,6,7) as output TCCR1A = 0b10101011; // set and configure fast PWM - TCCR1B |= 0b00011001; // set and configure fast PWM - // --- --- rows - // TODO: set to high output - // --- --- columns - // TODO: set to input with pullup enabled - // --- --- I2C (TWI) - // TODO: use twi library + TCCR1B = 0b00011001; // set and configure fast PWM + + // rows + DDRF |= 0b11110011; // set F(0,1,4,5,6,7) as output + PORTF |= 0b11110011; // set F(0,1,4,5,6,7) drive high + + // columns + DDRB &= ~0b00010001; // set B(0,4) as input + PORTB |= 0b00010001; // set B(0,4) internal pull-up enabled + DDRC &= ~0b11000000; // set C(6,7) as input + PORTC |= 0b11000000; // set C(6,7) internal pull-up enabled + DDRD &= ~0b10001100; // set D(2,3,7) as input + PORTD |= 0b10001100; // set D(2,3,7) internal pull-up enabled + + // I2C (TWI) + twi_init(); // (on pins D(0,1)) + twi_setAddress(0b0100000); } // TODO diff --git a/src/ergodox/teensy-2-0--mcp23018.h b/src/ergodox/teensy-2-0--mcp23018.h new file mode 100644 index 0000000..d57cd77 --- /dev/null +++ b/src/ergodox/teensy-2-0--mcp23018.h @@ -0,0 +1,28 @@ +/* ---------------------------------------------------------------------------- + * ergodox controller (Teensy 2.0 and MCP23018) specific stuff + * - public things are prefixed by `controller_` or `CONTROLLER_` + * ---------------------------------------------------------------------------- + * Copyright (c) 2012 Ben Blazak + * Released under The MIT License (MIT) (see "license.md") at + * + * ------------------------------------------------------------------------- */ + + +// LEDs +#define CONTROLLER_LED1_ON (OCR1A = 0xFFFF) +#define CONTROLLER_LED1_OFF (OCR1A = 0x0000) +#define CONTROLLER_LED1_SET(n) (OCR1A = (uint16_t)(n)) +#define CONTROLLER_LED1_SET_PERCENT(n) (OCR1A = (uint16_t)((n) * 0xFFFF)) +#define CONTROLLER_LED2_ON (OCR1B = 0xFFFF) +#define CONTROLLER_LED2_OFF (OCR1B = 0x0000) +#define CONTROLLER_LED2_SET(n) (OCR1B = (uint16_t)(n)) +#define CONTROLLER_LED2_SET_PERCENT(n) (OCR1B = (uint16_t)((n) * 0xFFFF)) +#define CONTROLLER_LED3_ON (OCR1C = 0xFFFF) +#define CONTROLLER_LED3_OFF (OCR1C = 0x0000) +#define CONTROLLER_LED3_SET(n) (OCR1C = (uint16_t)(n)) +#define CONTROLLER_LED3_SET_PERCENT(n) (OCR1C = (uint16_t)((n) * 0xFFFF)) + + +// init +void controller_init(); + diff --git a/src/firmware.c b/src/firmware.c index ce80f70..6f727a5 100644 --- a/src/firmware.c +++ b/src/firmware.c @@ -7,9 +7,6 @@ * ------------------------------------------------------------------------- */ -#include "hardware/ergodox.h" - -#include "lib/twi.h" #include "lib/print.h" @@ -18,9 +15,5 @@ void main() { } void init() { - controller_init(); // must be first - kb_init(); // must be second - - // TODO: other stuff? }