working more on controller_init(), and file factoring

partial-rewrite
Ben Blazak 2012-03-16 18:31:36 -07:00
parent 37b504adfe
commit 4dd05f515b
6 changed files with 89 additions and 76 deletions

View File

@ -49,6 +49,9 @@
(http://www.cprogramming.com/tutorial/bitwise_operators.html) (http://www.cprogramming.com/tutorial/bitwise_operators.html)
(on <http://cprogramming.com/>) (on <http://cprogramming.com/>)
* [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?] * [In C++ is "const" after type ID acceptable?]
(http://stackoverflow.com/questions/988069/in-c-is-const-after-type-id-acceptable) (http://stackoverflow.com/questions/988069/in-c-is-const-after-type-id-acceptable)
(on <http://stackoverflow.com/>) (on <http://stackoverflow.com/>)
@ -63,6 +66,10 @@
* [Markdown: Syntax] * [Markdown: Syntax]
(http://daringfireball.net/projects/markdown/syntax) (http://daringfireball.net/projects/markdown/syntax)
* [Keyboard Scan Rates]
(http://geekhack.org/showwiki.php?title=Keyboard+scan+rates)
list (on <http://geekhack.org/>)
## AVR C stuff ## AVR C stuff

View File

@ -1,6 +1,6 @@
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* ergoDOX keyboard specific stuff * ergoDOX layout specific stuff
* - public things are prefixed by `kb_` or `KB_` * - public things are prefixed by `layout_` or `LAYOUT_`
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
* Copyright (c) 2012 Ben Blazak * Copyright (c) 2012 Ben Blazak
* Released under The MIT License (MIT) (see "license.md") at * Released under The MIT License (MIT) (see "license.md") at
@ -33,7 +33,7 @@
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
#include "ergodox.h" #include "layout.h"
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -1,4 +1,7 @@
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* ergoDOX layout specific stuff
* - public things are prefixed by `layout_` or `LAYOUT_`
* ----------------------------------------------------------------------------
* Copyright (c) 2012 Ben Blazak * Copyright (c) 2012 Ben Blazak
* Released under The MIT License (MIT) (see "license.md") at * Released under The MIT License (MIT) (see "license.md") at
* <https://github.com/benblazak/ergodox-firmware> * <https://github.com/benblazak/ergodox-firmware>
@ -10,9 +13,5 @@
#define KB_COLUMNS 7 // must match real life #define KB_COLUMNS 7 // must match real life
void controller_init();
void kb_init();
// TODO // TODO

View File

@ -16,7 +16,7 @@
* Pinouts and Pin assignments * Pinouts and Pin assignments
* - '+' indicates pin * - '+' indicates pin
* - 'o' indicates unused 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 * - 'OC**' pins enclosed in parenthesis had lines over them in the pinout
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
@ -39,16 +39,16 @@
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
* MCP23018 * MCP23018
* ======== Vss(GND) +01---.---28+ NC * ======== Vss(GND) +01---.---28+ NC
* GPNC +02 27+ GPAC * NC +02 27+ GPA7
* GPB0 +03 26+ GPA6 * GPB0 +03 26+ GPA6
* GPB1 +04 25+ GPAC * GPB1 +04 25+ GPA5
* GPB2 +05 24+ GPAC * GPB2 +05 24+ GPA4
* GPB3 +06 23+ GPAC * GPB3 +06 23+ GPA3
* GPB4 +07 22+ GPAC * GPB4 +07 22+ GPA2
* GPB5 +08 21+ GPAC * GPB5 +08 21+ GPA1
* GPB6 +09 20+ GPAC * GPB6 +09 20+ GPA0
* GPB7 +10 19+ INTC * GPB7 +10 19+ INTA
* Vdd(Vcc) +11 18+ INTC * Vdd(Vcc) +11 18+ INTB
* SCL +12 17+ NC * SCL +12 17+ NC
* SDA +13 16+ RESET * SDA +13 16+ RESET
* NC +14-------15+ ADDR * NC +14-------15+ ADDR
@ -93,12 +93,12 @@
* o14-------15+ ADDR (see note) * o14-------15+ ADDR (see note)
* *
* notes: * notes:
* - ADDR (pin15): Set slave address to 0b0100000 by connecting to Vss(GND) * - ADDR (pin15): Set slave address to 0b0100000 by connecting to Vss(GND).
* - note: The user-defined bits are the three least significant * (The user-defined bits are the three least significant).
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* Registers * Notes about Registers
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
* General I/O (see datasheet section 10.2.1) * 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) * the default value of all the bits in those registers is 0)
* *
* abbreviations: * abbreviations:
* - OC = Output Compare * - OCR = Output Compare Register
* - TCCR = Timer/Counter Control Register * - TCCR = Timer/Counter Control Register
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
#include "lib/twi.h"
#include "teensy-2-0--mcp23018.h"
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ~~~ macros ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~ macros ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
@ -155,40 +160,13 @@
#define CPU_125kHz 0x07 #define CPU_125kHz 0x07
#define CPU_62kHz 0x08 #define CPU_62kHz 0x08
// pins // TWI frequency
#define CONTROLLER_LED1_ON (OCR1A = 0xFFFF) #define TWI_FREQ 400000 // (see lib/twi.(h|c))
#define CONTROLLER_LED1_OFF (OCR1A = 0)
#define CONTROLLER_LED1_SET(n) (OCR1A = (n)) // pins
#define CONTROLLER_LED1_SET_PERCENT(n) (OCR1A = (n) * 0xFFFF) // --- rows ?TODO
#define CONTROLLER_LED2_ON (OCR1B = 0xFFFF) // --- columns ?TODO
#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)
// 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 // mcp23018 pins
// TODO // TODO
@ -214,33 +192,41 @@
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void controller_init() { void controller_init() {
teensy_init(); teensy_init(); // must be first
mcp23018_init(); mcp23018_init(); // must be second
} }
// TODO
// - set internal brown-out detection circuit?
void teensy_init() { void teensy_init() {
CPU_PRESCALE(CPU_16MHz); // speed should match F_CPU in makefile CPU_PRESCALE(CPU_16MHz); // speed should match F_CPU in makefile
// unused pins // unused pins
DDRB &= ~0b00001110; // set B(1,2,3) as input 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 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 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 DDRB |= 0b11100000; // set B(5,6,7) as output
TCCR1A = 0b10101011; // set and configure fast PWM TCCR1A = 0b10101011; // set and configure fast PWM
TCCR1B |= 0b00011001; // set and configure fast PWM TCCR1B = 0b00011001; // set and configure fast PWM
// --- --- rows
// TODO: set to high output // rows
// --- --- columns DDRF |= 0b11110011; // set F(0,1,4,5,6,7) as output
// TODO: set to input with pullup enabled PORTF |= 0b11110011; // set F(0,1,4,5,6,7) drive high
// --- --- I2C (TWI)
// TODO: use twi library // 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 // TODO

View File

@ -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
* <https://github.com/benblazak/ergodox-firmware>
* ------------------------------------------------------------------------- */
// 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();

View File

@ -7,9 +7,6 @@
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
#include "hardware/ergodox.h"
#include "lib/twi.h"
#include "lib/print.h" #include "lib/print.h"
@ -18,9 +15,5 @@ void main() {
} }
void init() { void init() {
controller_init(); // must be first
kb_init(); // must be second
// TODO: other stuff?
} }