ergodox-firmware/src/keyboard/ergodox/teensy-2-0.c
Ben Blazak 19725eed4a *lots* of stuff .. :) - got PWM working!
also, i didn't make a note of it in the *.md file, but Waveform
Generation Mode 15 for fast PWM wasn't working right (well.. wasn't
working how i expected it to).  i misinterpreted what the modes were
doing, partially (haha, or all-ly?) because i didn't read the
description of fast pwm thoroughly enough...  in any case, all the
information's in the datasheet, and it's actually not terribly long.
i'm not sure how to correctly use Mode 15 yet, but i think i'll leave it
alone for now, since Mode 5 works as expected, and i think what the
datasheet says about *that* makes enough sense to me for me to be
content for the moment.
2012-03-19 00:01:41 -07:00

66 lines
2.2 KiB
C

/* ----------------------------------------------------------------------------
* ergoDOX controller: Teensy 2.0 specific code
* ----------------------------------------------------------------------------
* Copyright (c) 2012 Ben Blazak
* Released under The MIT License (MIT) (see "license.md") at
* <https://github.com/benblazak/ergodox-firmware>
* ------------------------------------------------------------------------- */
// processor frequency (from <http://www.pjrc.com/teensy/prescaler.html>)
#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
// TWI
#define TWI_FREQ 400000 // (see "lib/twi.(h|c)")
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <avr/io.h>
#include "lib/arduino/twi.h"
#include "teensy-2-0.h"
void teensy_init(void) {
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 pull-up enabled
DDRD &= ~0b01110000; // set D(4,5,6) as input
PORTD |= 0b01110000; // set D(4,5,6) internal pull-up enabled
DDRE &= ~0b01000000; // set E(6) as input
PORTE |= 0b01000000; // set E(6) internal pull-up enabled
// LEDs (see "PWM on ports OC1(A|B|C)" in "teensy-2-0.md")
DDRB |= 0b11100000; // set B(5,6,7) as output
TCCR1A = 0b10101001; // set and configure fast PWM
TCCR1B = 0b00001001; // 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))
}