2 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 This work is heavily based on initial firmware for Ergodox keyboard.
19 Copyright (c) 2012, 2013 Ben Blazak <benblazak.dev@gmail.com>
20 Released under The MIT License (see "doc/licenses/MIT.md")
21 Project located at <https://github.com/benblazak/ergodox-firmware>
23 Most used files are located at
24 <https://github.com/benblazak/ergodox-firmware/tree/partial-rewrite/firmware/keyboard/ergodox/controller>
31 #include <avr/interrupt.h>
32 #include <util/delay.h>
38 #include "i2cmaster.h"
40 bool i2c_initialized = 0;
42 bool ergodox_left_led_1 = 0; // left top
43 bool ergodox_left_led_2 = 0; // left middle
44 bool ergodox_left_led_3 = 0; // left bottom
47 void init_ergodox(void)
49 // keyboard LEDs (see "PWM on ports OC1(A|B|C)" in "teensy-2-0.md")
50 TCCR1A = 0b10101001; // set and configure fast PWM
51 TCCR1B = 0b00001001; // set and configure fast PWM
53 // (tied to Vcc for hardware convenience)
54 DDRB &= ~(1<<4); // set B(4) as input
55 PORTB &= ~(1<<4); // set B(4) internal pull-up disabled
57 // unused pins - C7, D4, D5, D7, E6
58 // set as input with internal pull-ip enabled
60 DDRD &= ~(1<<7 | 1<<5 | 1<<4);
63 PORTD |= (1<<7 | 1<<5 | 1<<4);
67 ergodox_led_all_off();
68 ergodox_led_all_set(LED_BRIGHTNESS_HI);
71 ergodox_led_all_off();
74 uint8_t init_mcp23018(void) {
78 if (i2c_initialized == 0) {
79 i2c_init(); // on pins D(1,0)
85 // - unused : input : 1
86 // - input : input : 1
87 // - driving : output : 0
88 err = i2c_start(I2C_ADDR_WRITE); if (err) goto out;
89 err = i2c_write(IODIRA); if (err) goto out;
90 err = i2c_write(0b00000000); if (err) goto out;
91 err = i2c_write(0b00111111); if (err) goto out;
96 // - driving : off : 0
97 err = i2c_start(I2C_ADDR_WRITE); if (err) goto out;
98 err = i2c_write(GPPUA); if (err) goto out;
99 err = i2c_write(0b00000000); if (err) goto out;
100 err = i2c_write(0b00111111); if (err) goto out;
103 // set logical value (doesn't matter on inputs)
104 // - unused : hi-Z : 1
105 // - input : hi-Z : 1
106 // - driving : hi-Z : 1
107 err = i2c_start(I2C_ADDR_WRITE); if (err) goto out;
108 err = i2c_write(OLATA); if (err) goto out;
109 err = i2c_write(0b11111111
110 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
111 ); if (err) goto out;
112 err = i2c_write(0b11111111
113 & ~(ergodox_left_led_2<<LEFT_LED_2_SHIFT)
114 & ~(ergodox_left_led_1<<LEFT_LED_1_SHIFT)
115 ); if (err) goto out;