]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergodox_ez/ergodox_ez.c
Merge branch 'master' of https://github.com/jackhumbert/qmk_firmware
[qmk_firmware.git] / keyboards / ergodox_ez / ergodox_ez.c
1 #include "ergodox_ez.h"
2 #include "i2cmaster.h"
3
4 bool i2c_initialized = 0;
5 uint8_t mcp23018_status = 0x20;
6
7 __attribute__ ((weak))
8 void matrix_init_user(void) {
9
10 }
11
12 __attribute__ ((weak))
13 void matrix_scan_user(void) {
14
15 }
16
17 void matrix_init_kb(void) {
18    // keyboard LEDs (see "PWM on ports OC1(A|B|C)" in "teensy-2-0.md")
19     TCCR1A = 0b10101001;  // set and configure fast PWM
20     TCCR1B = 0b00001001;  // set and configure fast PWM
21
22     // (tied to Vcc for hardware convenience)
23     DDRB  &= ~(1<<4);  // set B(4) as input
24     PORTB &= ~(1<<4);  // set B(4) internal pull-up disabled
25
26     // unused pins - C7, D4, D5, D7, E6
27     // set as input with internal pull-ip enabled
28     DDRC  &= ~(1<<7);
29     DDRD  &= ~(1<<7 | 1<<5 | 1<<4);
30     DDRE  &= ~(1<<6);
31     PORTC |=  (1<<7);
32     PORTD |=  (1<<7 | 1<<5 | 1<<4);
33     PORTE |=  (1<<6);
34
35     ergodox_blink_all_leds();
36
37     matrix_init_user();
38 }
39
40 void matrix_scan_kb(void) {
41     matrix_scan_user();
42 }
43
44
45 void ergodox_blink_all_leds(void)
46 {
47     ergodox_led_all_off();
48     ergodox_led_all_set(LED_BRIGHTNESS_HI);
49     ergodox_right_led_1_on();
50     _delay_ms(50);
51     ergodox_right_led_2_on();
52     _delay_ms(50);
53     ergodox_right_led_3_on();
54     _delay_ms(50);
55     ergodox_right_led_1_off();
56     _delay_ms(50);
57     ergodox_right_led_2_off();
58     _delay_ms(50);
59     ergodox_right_led_3_off();
60     //ergodox_led_all_on();
61     //_delay_ms(333);
62     ergodox_led_all_off();
63 }
64
65 uint8_t init_mcp23018(void) {
66     mcp23018_status = 0x20;
67
68     // I2C subsystem
69     if (i2c_initialized == 0) {
70         i2c_init();  // on pins D(1,0)
71         i2c_initialized++;
72         _delay_ms(1000);
73     }
74
75     // set pin direction
76     // - unused  : input  : 1
77     // - input   : input  : 1
78     // - driving : output : 0
79     mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
80     mcp23018_status = i2c_write(IODIRA);            if (mcp23018_status) goto out;
81     mcp23018_status = i2c_write(0b00000000);        if (mcp23018_status) goto out;
82     mcp23018_status = i2c_write(0b00111111);        if (mcp23018_status) goto out;
83     i2c_stop();
84
85     // set pull-up
86     // - unused  : on  : 1
87     // - input   : on  : 1
88     // - driving : off : 0
89     mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
90     mcp23018_status = i2c_write(GPPUA);             if (mcp23018_status) goto out;
91     mcp23018_status = i2c_write(0b00000000);        if (mcp23018_status) goto out;
92     mcp23018_status = i2c_write(0b00111111);        if (mcp23018_status) goto out;
93
94 out:
95     i2c_stop();
96
97     return mcp23018_status;
98 }
99
100