split_util.c 4.2 KB

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