]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/ergodox/ergodox.c
Implementation for Ergodox project
[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     // blink leds
67     ergodox_led_all_off();
68     ergodox_led_all_set(LED_BRIGHTNESS_HI);
69     ergodox_led_all_on();
70     _delay_ms(333);
71     ergodox_led_all_off();
72 }
73
74 uint8_t init_mcp23018(void) {
75     uint8_t err = 0x20;
76
77     // I2C subsystem
78     if (i2c_initialized == 0) {
79         i2c_init();  // on pins D(1,0)
80         i2c_initialized++;
81         _delay_ms(1000);
82     }
83
84     // set pin direction
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;
92     i2c_stop();
93     // set pull-up
94     // - unused  : on  : 1
95     // - input   : on  : 1
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;
101     i2c_stop();
102
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;
116
117 out:
118     i2c_stop();
119     return err;
120 }
121