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;
41 uint8_t mcp23018_status = 0x20;
43 bool ergodox_left_led_1 = 0; // left top
44 bool ergodox_left_led_2 = 0; // left middle
45 bool ergodox_left_led_3 = 0; // left bottom
48 void init_ergodox(void)
50 // keyboard LEDs (see "PWM on ports OC1(A|B|C)" in "teensy-2-0.md")
51 TCCR1A = 0b10101001; // set and configure fast PWM
52 TCCR1B = 0b00001001; // set and configure fast PWM
54 // (tied to Vcc for hardware convenience)
55 DDRB &= ~(1<<4); // set B(4) as input
56 PORTB &= ~(1<<4); // set B(4) internal pull-up disabled
58 // unused pins - C7, D4, D5, D7, E6
59 // set as input with internal pull-ip enabled
61 DDRD &= ~(1<<7 | 1<<5 | 1<<4);
64 PORTD |= (1<<7 | 1<<5 | 1<<4);
68 void ergodox_blink_all_leds(void)
70 ergodox_led_all_off();
71 ergodox_led_all_set(LED_BRIGHTNESS_HI);
74 ergodox_led_all_off();
77 uint8_t init_mcp23018(void) {
78 mcp23018_status = 0x20;
81 if (i2c_initialized == 0) {
82 i2c_init(); // on pins D(1,0)
88 // - unused : input : 1
89 // - input : input : 1
90 // - driving : output : 0
91 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
92 mcp23018_status = i2c_write(IODIRA); if (mcp23018_status) goto out;
93 mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out;
94 mcp23018_status = i2c_write(0b00111111); if (mcp23018_status) goto out;
100 // - driving : off : 0
101 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
102 mcp23018_status = i2c_write(GPPUA); if (mcp23018_status) goto out;
103 mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out;
104 mcp23018_status = i2c_write(0b00111111); if (mcp23018_status) goto out;
109 if (!mcp23018_status) mcp23018_status = ergodox_left_leds_update();
111 return mcp23018_status;
114 uint8_t ergodox_left_leds_update(void) {
115 if (mcp23018_status) { // if there was an error
116 return mcp23018_status;
119 // set logical value (doesn't matter on inputs)
120 // - unused : hi-Z : 1
121 // - input : hi-Z : 1
122 // - driving : hi-Z : 1
123 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
124 mcp23018_status = i2c_write(OLATA); if (mcp23018_status) goto out;
125 mcp23018_status = i2c_write(0b11111111
126 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
127 ); if (mcp23018_status) goto out;
128 mcp23018_status = i2c_write(0b11111111
129 & ~(ergodox_left_led_2<<LEFT_LED_2_SHIFT)
130 & ~(ergodox_left_led_1<<LEFT_LED_1_SHIFT)
131 ); if (mcp23018_status) goto out;
135 return mcp23018_status;