]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/split_common/split_util.c
Added JTAG disable code to the split_common (#4309)
[qmk_firmware.git] / quantum / split_common / split_util.c
1 #include <avr/io.h>
2 #include <avr/wdt.h>
3 #include <avr/power.h>
4 #include <avr/interrupt.h>
5 #include <util/delay.h>
6 #include <avr/eeprom.h>
7 #include "split_util.h"
8 #include "matrix.h"
9 #include "keyboard.h"
10 #include "config.h"
11 #include "timer.h"
12 #include "split_flags.h"
13
14 #ifdef RGBLIGHT_ENABLE
15 #   include "rgblight.h"
16 #endif
17 #ifdef BACKLIGHT_ENABLE
18 #   include "backlight.h"
19 #endif
20
21 #ifdef SPLIT_HAND_PIN
22 #   include "pincontrol.h"
23 #endif
24
25 #if defined(USE_I2C) || defined(EH)
26 #  include "i2c.h"
27 #else
28 #  include "serial.h"
29 #endif
30
31 volatile bool isLeftHand = true;
32
33 volatile uint8_t setTries = 0;
34
35 static void setup_handedness(void) {
36   #ifdef SPLIT_HAND_PIN
37     // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
38     pinMode(SPLIT_HAND_PIN, PinDirectionInput);
39     isLeftHand = digitalRead(SPLIT_HAND_PIN);
40   #else
41     #ifdef EE_HANDS
42       isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
43     #else
44       // I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c
45       #if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT)
46         isLeftHand = !has_usb();
47       #else
48         isLeftHand = has_usb();
49       #endif
50     #endif
51   #endif
52 }
53
54 static void keyboard_master_setup(void) {
55 #if defined(USE_I2C) || defined(EH)
56   i2c_master_init();
57   #ifdef SSD1306OLED
58     matrix_master_OLED_init ();
59   #endif
60 #else
61   serial_master_init();
62 #endif
63
64     // For master the Backlight info needs to be sent on startup
65     // Otherwise the salve won't start with the proper info until an update
66     BACKLIT_DIRTY = true;
67 }
68
69 static void keyboard_slave_setup(void) {
70   timer_init();
71 #if defined(USE_I2C) || defined(EH)
72     i2c_slave_init(SLAVE_I2C_ADDRESS);
73 #else
74     serial_slave_init();
75 #endif
76 }
77
78 bool has_usb(void) {
79    USBCON |= (1 << OTGPADE); //enables VBUS pad
80    _delay_us(5);
81    return (USBSTA & (1<<VBUS));  //checks state of VBUS
82 }
83
84 void split_keyboard_setup(void) {
85    setup_handedness();
86
87    if (has_usb()) {
88       keyboard_master_setup();
89    } else {
90       keyboard_slave_setup();
91    }
92    sei();
93 }
94
95 void keyboard_slave_loop(void) {
96    // Disable JTAG since we skip calling keyboard_init() on the slave side
97    // Future fix will possible call keyboard_init() on the slave to remove this need
98    disable_JTAG();
99     
100    matrix_init();
101    
102    //Init RGB
103    #ifdef RGBLIGHT_ENABLE
104       rgblight_init();
105    #endif
106
107    while (1) {
108     // Matrix Slave Scan
109     matrix_slave_scan();
110     
111     // Read Backlight Info
112     #ifdef BACKLIGHT_ENABLE
113         #ifdef USE_I2C
114             if (BACKLIT_DIRTY) {
115                 backlight_set(i2c_slave_buffer[I2C_BACKLIT_START]);
116                 BACKLIT_DIRTY = false;
117             }
118         #else // USE_SERIAL
119             backlight_set(serial_master_buffer[SERIAL_BACKLIT_START]);
120         #endif
121     #endif
122     // Read RGB Info
123     #ifdef RGBLIGHT_ENABLE
124         #ifdef USE_I2C
125             if (RGB_DIRTY) {
126                 // Disable interupts (RGB data is big)
127                 cli();
128                 // Create new DWORD for RGB data
129                 uint32_t dword; 
130                 
131                 // Fill the new DWORD with the data that was sent over
132                 uint8_t *dword_dat = (uint8_t *)(&dword);
133                 for (int i = 0; i < 4; i++) {
134                     dword_dat[i] = i2c_slave_buffer[I2C_RGB_START+i];
135                 }
136                 
137                 // Update the RGB now with the new data and set RGB_DIRTY to false
138                 rgblight_update_dword(dword);
139                 RGB_DIRTY = false;
140                 // Re-enable interupts now that RGB is set
141                 sei();
142             }
143         #else // USE_SERIAL
144             // Add serial implementation for RGB here
145         #endif
146     #endif
147    }
148 }
149
150 // this code runs before the usb and keyboard is initialized
151 void matrix_setup(void) {
152     split_keyboard_setup();
153
154     if (!has_usb()) {
155         //rgblight_init();
156         keyboard_slave_loop();
157     }
158 }
159
160 // Temporary code to disable JTAG on the slave board
161 void disable_JTAG(void) {
162     /* Copied from tmk_core/common/keybaord.c */
163     // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
164     #if  (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
165       MCUCR |= _BV(JTD);
166       MCUCR |= _BV(JTD);
167     #endif
168 }