]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboard/ergodox_ez/ergodox_ez.c
Merge pull request #77 from chwilk/master
[qmk_firmware.git] / keyboard / 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     return NULL;
10 };
11
12 __attribute__ ((weak))
13 void * matrix_scan_user(void) {
14     return NULL;
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     if (matrix_init_user) {
38         (*matrix_init_user)();
39     }
40
41     return NULL;
42 };
43
44 void * matrix_scan_kb(void) {
45
46     if (matrix_scan_user) {
47         (*matrix_scan_user)();
48     }
49
50     return NULL;
51 };
52
53
54 void ergodox_blink_all_leds(void)
55 {
56     ergodox_led_all_off();
57     ergodox_led_all_set(LED_BRIGHTNESS_HI);
58     ergodox_right_led_1_on();
59     _delay_ms(50);
60     ergodox_right_led_2_on();
61     _delay_ms(50);
62     ergodox_right_led_3_on();
63     _delay_ms(50);
64     ergodox_right_led_1_off();
65     _delay_ms(50);
66     ergodox_right_led_2_off();
67     _delay_ms(50);
68     ergodox_right_led_3_off();
69     //ergodox_led_all_on();
70     //_delay_ms(333);
71     ergodox_led_all_off();
72 }
73
74 uint8_t init_mcp23018(void) {
75     mcp23018_status = 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     mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
89     mcp23018_status = i2c_write(IODIRA);            if (mcp23018_status) goto out;
90     mcp23018_status = i2c_write(0b00000000);        if (mcp23018_status) goto out;
91     mcp23018_status = i2c_write(0b00111111);        if (mcp23018_status) goto out;
92     i2c_stop();
93
94     // set pull-up
95     // - unused  : on  : 1
96     // - input   : on  : 1
97     // - driving : off : 0
98     mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
99     mcp23018_status = i2c_write(GPPUA);             if (mcp23018_status) goto out;
100     mcp23018_status = i2c_write(0b00000000);        if (mcp23018_status) goto out;
101     mcp23018_status = i2c_write(0b00111111);        if (mcp23018_status) goto out;
102
103 out:
104     i2c_stop();
105
106     return mcp23018_status;
107 }
108
109