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