]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/ergodox/ergodox.h
Optimizing I2C
[tmk_firmware.git] / keyboard / ergodox / ergodox.h
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 Copyright (c) 2012, 2013 Ben Blazak <benblazak.dev@gmail.com>
19 Released under The MIT License (see "doc/licenses/MIT.md")
20 Project located at <https://github.com/benblazak/ergodox-firmware>
21
22 Most used files are located at
23 <https://github.com/benblazak/ergodox-firmware/tree/partial-rewrite/firmware/keyboard/ergodox/controller>
24
25 */
26
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <avr/io.h>
30 #include "i2cmaster.h"
31
32 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
33 #define CPU_16MHz       0x00
34
35 // I2C aliases and register addresses (see "mcp23018.md")
36 #define I2C_ADDR        0b0100000
37 #define I2C_ADDR_WRITE  ( (I2C_ADDR<<1) | I2C_WRITE )
38 #define I2C_ADDR_READ   ( (I2C_ADDR<<1) | I2C_READ  )
39 #define IODIRA          0x00            // i/o direction register
40 #define IODIRB          0x01
41 #define GPPUA           0x0C            // GPIO pull-up resistor register
42 #define GPPUB           0x0D
43 #define GPIOA           0x12            // general purpose i/o port register (write modifies OLAT)
44 #define GPIOB           0x13
45 #define OLATA           0x14            // output latch register
46 #define OLATB           0x15
47
48 void init_ergodox(void);
49 uint8_t init_mcp23018(void);
50 uint8_t ergodox_left_leds_update(void);
51
52 #define LED_BRIGHTNESS_LO       31
53 #define LED_BRIGHTNESS_HI       255
54
55 #define LEFT_LED_1_SHIFT        7       // in MCP23018 port B
56 #define LEFT_LED_2_SHIFT        6       // in MCP23018 port B
57 #define LEFT_LED_3_SHIFT        7       // in MCP23018 port A
58
59 extern bool ergodox_left_led_1;         // left top
60 extern bool ergodox_left_led_2;         // left middle
61 extern bool ergodox_left_led_3;         // left bottom
62
63 inline void ergodox_board_led_on(void)      { DDRD |=  (1<<6); PORTD |=  (1<<6); }
64 inline void ergodox_right_led_1_on(void)    { DDRB |=  (1<<5); PORTB |=  (1<<5); }
65 inline void ergodox_right_led_2_on(void)    { DDRB |=  (1<<6); PORTB |=  (1<<6); }
66 inline void ergodox_right_led_3_on(void)    { DDRB |=  (1<<7); PORTB |=  (1<<7); }
67 inline void ergodox_left_led_1_on(void)     { ergodox_left_led_1 = 1; }
68 inline void ergodox_left_led_2_on(void)     { ergodox_left_led_2 = 1; }
69 inline void ergodox_left_led_3_on(void)     { ergodox_left_led_3 = 1; }
70
71 inline void ergodox_board_led_off(void)     { DDRD &= ~(1<<6); PORTD &= ~(1<<6); }
72 inline void ergodox_right_led_1_off(void)   { DDRB &= ~(1<<5); PORTB &= ~(1<<5); }
73 inline void ergodox_right_led_2_off(void)   { DDRB &= ~(1<<6); PORTB &= ~(1<<6); }
74 inline void ergodox_right_led_3_off(void)   { DDRB &= ~(1<<7); PORTB &= ~(1<<7); }
75 inline void ergodox_left_led_1_off(void)    { ergodox_left_led_1 = 0; }
76 inline void ergodox_left_led_2_off(void)    { ergodox_left_led_2 = 0; }
77 inline void ergodox_left_led_3_off(void)    { ergodox_left_led_3 = 0; }
78
79 inline void ergodox_led_all_on(void)
80 {
81     ergodox_board_led_on();
82     ergodox_right_led_1_on();
83     ergodox_right_led_2_on();
84     ergodox_right_led_3_on();
85     ergodox_left_led_1_on();
86     ergodox_left_led_2_on();
87     ergodox_left_led_3_on();
88     ergodox_left_leds_update();
89 }
90
91 inline void ergodox_led_all_off(void)
92 {
93     ergodox_board_led_off();
94     ergodox_right_led_1_off();
95     ergodox_right_led_2_off();
96     ergodox_right_led_3_off();
97     ergodox_left_led_1_off();
98     ergodox_left_led_2_off();
99     ergodox_left_led_3_off();
100     ergodox_left_leds_update();
101 }
102
103 inline void ergodox_right_led_1_set(uint8_t n)    { OCR1A = n; }
104 inline void ergodox_right_led_2_set(uint8_t n)    { OCR1B = n; }
105 inline void ergodox_right_led_3_set(uint8_t n)    { OCR1C = n; }
106
107 inline void ergodox_led_all_set(uint8_t n)
108 {
109     ergodox_right_led_1_set(n);
110     ergodox_right_led_2_set(n);
111     ergodox_right_led_3_set(n);
112 }
113