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 void ergodox_blink_all_leds(void)
69 ergodox_led_all_off();
70 ergodox_led_all_set(LED_BRIGHTNESS_HI);
73 ergodox_led_all_off();
76 uint8_t init_mcp23018(void) {
80 if (i2c_initialized == 0) {
81 i2c_init(); // on pins D(1,0)
87 // - unused : input : 1
88 // - input : input : 1
89 // - driving : output : 0
90 err = i2c_start(I2C_ADDR_WRITE); if (err) goto out;
91 err = i2c_write(IODIRA); if (err) goto out;
92 err = i2c_write(0b00000000); if (err) goto out;
93 err = i2c_write(0b00111111); if (err) goto out;
99 // - driving : off : 0
100 err = i2c_start(I2C_ADDR_WRITE); if (err) goto out;
101 err = i2c_write(GPPUA); if (err) goto out;
102 err = i2c_write(0b00000000); if (err) goto out;
103 err = i2c_write(0b00111111); if (err) goto out;
108 if (!err) err = ergodox_left_leds_update();
113 uint8_t ergodox_left_leds_update(void) {
116 // set logical value (doesn't matter on inputs)
117 // - unused : hi-Z : 1
118 // - input : hi-Z : 1
119 // - driving : hi-Z : 1
120 err = i2c_start(I2C_ADDR_WRITE); if (err) goto out;
121 err = i2c_write(OLATA); if (err) goto out;
122 err = i2c_write(0b11111111
123 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
124 ); if (err) goto out;
125 err = i2c_write(0b11111111
126 & ~(ergodox_left_led_2<<LEFT_LED_2_SHIFT)
127 & ~(ergodox_left_led_1<<LEFT_LED_1_SHIFT)
128 ); if (err) goto out;