split_util.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 disable_JTAG(void);
  83. void keyboard_slave_loop(void) {
  84. // Disable JTAG since we skip calling keyboard_init() on the slave side
  85. // Future fix will possible call keyboard_init() on the slave to remove this need
  86. disable_JTAG();
  87. matrix_init();
  88. //Init RGB
  89. #ifdef RGBLIGHT_ENABLE
  90. rgblight_init();
  91. #endif
  92. while (1) {
  93. // Matrix Slave Scan
  94. matrix_slave_scan();
  95. // Read Backlight Info
  96. #ifdef BACKLIGHT_ENABLE
  97. #ifdef USE_I2C
  98. if (BACKLIT_DIRTY) {
  99. backlight_set(i2c_slave_buffer[I2C_BACKLIT_START]);
  100. BACKLIT_DIRTY = false;
  101. }
  102. #else // USE_SERIAL
  103. backlight_set(serial_master_buffer[SERIAL_BACKLIT_START]);
  104. #endif
  105. #endif
  106. // Read RGB Info
  107. #ifdef RGBLIGHT_ENABLE
  108. #ifdef USE_I2C
  109. if (RGB_DIRTY) {
  110. // Disable interupts (RGB data is big)
  111. cli();
  112. // Create new DWORD for RGB data
  113. uint32_t dword;
  114. // Fill the new DWORD with the data that was sent over
  115. uint8_t *dword_dat = (uint8_t *)(&dword);
  116. for (int i = 0; i < 4; i++) {
  117. dword_dat[i] = i2c_slave_buffer[I2C_RGB_START+i];
  118. }
  119. // Update the RGB now with the new data and set RGB_DIRTY to false
  120. rgblight_update_dword(dword);
  121. RGB_DIRTY = false;
  122. // Re-enable interupts now that RGB is set
  123. sei();
  124. }
  125. #else // USE_SERIAL
  126. // Add serial implementation for RGB here
  127. #endif
  128. #endif
  129. }
  130. }
  131. // this code runs before the usb and keyboard is initialized
  132. void matrix_setup(void) {
  133. split_keyboard_setup();
  134. if (!has_usb()) {
  135. //rgblight_init();
  136. keyboard_slave_loop();
  137. }
  138. }
  139. // Temporary code to disable JTAG on the slave board
  140. void disable_JTAG(void) {
  141. /* Copied from tmk_core/common/keybaord.c */
  142. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  143. #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
  144. MCUCR |= _BV(JTD);
  145. MCUCR |= _BV(JTD);
  146. #endif
  147. }