]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/ergodox/ergodox.c
c6a831487f781a1b901aa007b0b78b44ecb3b0a6
[tmk_firmware.git] / keyboard / ergodox / ergodox.c
1 /*
2 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
3
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.
8
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.
13
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/>.
16
17
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>
22
23 Most used files are located at
24 <https://github.com/benblazak/ergodox-firmware/tree/partial-rewrite/firmware/keyboard/ergodox/controller>
25
26 */
27
28 #include <stdint.h>
29 #include <stdbool.h>
30 #include <avr/io.h>
31 #include <avr/interrupt.h>
32 #include <util/delay.h>
33 #include "action.h"
34 #include "command.h"
35 #include "print.h"
36 #include "debug.h"
37 #include "ergodox.h"
38 #include "i2cmaster.h"
39
40 bool i2c_initialized = 0;
41
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
45
46
47 void init_ergodox(void)
48 {
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
52
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
56
57     // unused pins - C7, D4, D5, D7, E6
58     // set as input with internal pull-ip enabled
59     DDRC  &= ~(1<<7);
60     DDRD  &= ~(1<<7 | 1<<5 | 1<<4);
61     DDRE  &= ~(1<<6);
62     PORTC |=  (1<<7);
63     PORTD |=  (1<<7 | 1<<5 | 1<<4);
64     PORTE |=  (1<<6);
65 }
66
67 void ergodox_blink_all_leds(void)
68 {
69     ergodox_led_all_off();
70     ergodox_led_all_set(LED_BRIGHTNESS_HI);
71     ergodox_led_all_on();
72     _delay_ms(333);
73     ergodox_led_all_off();
74 }
75
76 uint8_t init_mcp23018(void) {
77     uint8_t err = 0x20;
78
79     // I2C subsystem
80     if (i2c_initialized == 0) {
81         i2c_init();  // on pins D(1,0)
82         i2c_initialized++;
83         _delay_ms(1000);
84     }
85
86     // set pin direction
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;
94     i2c_stop();
95
96     // set pull-up
97     // - unused  : on  : 1
98     // - input   : on  : 1
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;
104
105 out:
106     i2c_stop();
107
108     if (!err) err = ergodox_left_leds_update();
109
110     return err;
111 }
112
113 uint8_t ergodox_left_leds_update(void) {
114     uint8_t err = 0x20;
115
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;
129
130 out:
131     i2c_stop();
132     return err;
133 }
134