aboutsummaryrefslogtreecommitdiff
path: root/keyboard/ergodox/ergodox.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboard/ergodox/ergodox.c')
-rw-r--r--keyboard/ergodox/ergodox.c121
1 files changed, 121 insertions, 0 deletions
diff --git a/keyboard/ergodox/ergodox.c b/keyboard/ergodox/ergodox.c
new file mode 100644
index 000000000..38e9b3a8a
--- /dev/null
+++ b/keyboard/ergodox/ergodox.c
@@ -0,0 +1,121 @@
1#include "ergodox.h"
2#include "i2cmaster.h"
3
4bool i2c_initialized = 0;
5uint8_t mcp23018_status = 0x20;
6
7bool ergodox_left_led_1 = 0; // left top
8bool ergodox_left_led_2 = 0; // left middle
9bool ergodox_left_led_3 = 0; // left bottom
10
11void * matrix_init_user(void) {
12
13};
14
15void * matrix_scan_user(void) {
16
17};
18
19void * matrix_init_kb(void) {
20 // keyboard LEDs (see "PWM on ports OC1(A|B|C)" in "teensy-2-0.md")
21 TCCR1A = 0b10101001; // set and configure fast PWM
22 TCCR1B = 0b00001001; // set and configure fast PWM
23
24 // (tied to Vcc for hardware convenience)
25 DDRB &= ~(1<<4); // set B(4) as input
26 PORTB &= ~(1<<4); // set B(4) internal pull-up disabled
27
28 // unused pins - C7, D4, D5, D7, E6
29 // set as input with internal pull-ip enabled
30 DDRC &= ~(1<<7);
31 DDRD &= ~(1<<7 | 1<<5 | 1<<4);
32 DDRE &= ~(1<<6);
33 PORTC |= (1<<7);
34 PORTD |= (1<<7 | 1<<5 | 1<<4);
35 PORTE |= (1<<6);
36
37 ergodox_blink_all_leds();
38
39 if (matrix_init_user) {
40 (*matrix_init_user)();
41 }
42};
43
44void * matrix_scan_kb(void) {
45
46 if (matrix_scan_user) {
47 (*matrix_scan_user)();
48 }
49};
50
51
52void ergodox_blink_all_leds(void)
53{
54 ergodox_led_all_off();
55 ergodox_led_all_set(LED_BRIGHTNESS_HI);
56 ergodox_led_all_on();
57 _delay_ms(333);
58 ergodox_led_all_off();
59}
60
61uint8_t init_mcp23018(void) {
62 mcp23018_status = 0x20;
63
64 // I2C subsystem
65 if (i2c_initialized == 0) {
66 i2c_init(); // on pins D(1,0)
67 i2c_initialized++;
68 _delay_ms(1000);
69 }
70
71 // set pin direction
72 // - unused : input : 1
73 // - input : input : 1
74 // - driving : output : 0
75 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
76 mcp23018_status = i2c_write(IODIRA); if (mcp23018_status) goto out;
77 mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out;
78 mcp23018_status = i2c_write(0b00111111); if (mcp23018_status) goto out;
79 i2c_stop();
80
81 // set pull-up
82 // - unused : on : 1
83 // - input : on : 1
84 // - driving : off : 0
85 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
86 mcp23018_status = i2c_write(GPPUA); if (mcp23018_status) goto out;
87 mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out;
88 mcp23018_status = i2c_write(0b00111111); if (mcp23018_status) goto out;
89
90out:
91 i2c_stop();
92
93 if (!mcp23018_status) mcp23018_status = ergodox_left_leds_update();
94
95 return mcp23018_status;
96}
97
98uint8_t ergodox_left_leds_update(void) {
99 if (mcp23018_status) { // if there was an error
100 return mcp23018_status;
101 }
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 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
108 mcp23018_status = i2c_write(OLATA); if (mcp23018_status) goto out;
109 mcp23018_status = i2c_write(0b11111111
110 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
111 ); if (mcp23018_status) goto out;
112 mcp23018_status = i2c_write(0b11111111
113 & ~(ergodox_left_led_2<<LEFT_LED_2_SHIFT)
114 & ~(ergodox_left_led_1<<LEFT_LED_1_SHIFT)
115 ); if (mcp23018_status) goto out;
116
117out:
118 i2c_stop();
119 return mcp23018_status;
120}
121