]> git.gir.st - tmk_keyboard.git/blob - keyboard/ergodox/ergodox.c
build with ./make and flash with ./make flash
[tmk_keyboard.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 uint8_t mcp23018_status = 0x20;
42
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
46
47
48 void init_ergodox(void)
49 {
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
53
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
57
58 // unused pins - C7, D4, D5, D7, E6
59 // set as input with internal pull-ip enabled
60 DDRC &= ~(1<<7);
61 DDRD &= ~(1<<7 | 1<<5 | 1<<4);
62 DDRE &= ~(1<<6);
63 PORTC |= (1<<7);
64 PORTD |= (1<<7 | 1<<5 | 1<<4);
65 PORTE |= (1<<6);
66 }
67
68 void ergodox_blink_all_leds(void)
69 {
70 ergodox_led_all_off();
71 ergodox_led_all_set(LED_BRIGHTNESS_HI);
72 ergodox_led_all_on();
73 _delay_ms(333);
74 ergodox_led_all_off();
75 }
76
77 uint8_t init_mcp23018(void) {
78 mcp23018_status = 0x20;
79
80 // I2C subsystem
81 if (i2c_initialized == 0) {
82 i2c_init(); // on pins D(1,0)
83 i2c_initialized++;
84 _delay_ms(1000);
85 }
86
87 // set pin direction
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;
95 i2c_stop();
96
97 // set pull-up
98 // - unused : on : 1
99 // - input : on : 1
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;
105
106 out:
107 i2c_stop();
108
109 if (!mcp23018_status) mcp23018_status = ergodox_left_leds_update();
110
111 return mcp23018_status;
112 }
113
114 uint8_t ergodox_left_leds_update(void) {
115 if (mcp23018_status) { // if there was an error
116 return mcp23018_status;
117 }
118
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;
132
133 out:
134 i2c_stop();
135 return mcp23018_status;
136 }
Imprint / Impressum