You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.9 KiB

  1. /* Copyright 2020 Dimitris Papavasiliou <dpapavas@protonmail.ch>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #include <LUFA/Drivers/USB/USB.h>
  17. #include "lagrange.h"
  18. #ifndef SPLIT_USB_TIMEOUT_POLL
  19. # define SPLIT_USB_TIMEOUT_POLL 10
  20. #endif
  21. /* Instead of timing out, the slave waits indefinitely for the other
  22. * side to signal that it has become master. This avoids both sides
  23. * assuming the slave role when the USB port is powered but not
  24. * otherwise active (e.g. when the host is turned off, or suspended).
  25. * The SPI SS line is used for signaling. On power-up it is
  26. * configured as input with pull-up enabled. When one side assumes
  27. * the master role, it reconfigures the line for SPI, and pulls it low
  28. * to select the slave, which doubles as the signal. */
  29. bool is_keyboard_master(void) {
  30. static int8_t is_master = -1;
  31. if (is_master < 0) {
  32. while (readPin(SPI_SS_PIN)) {
  33. if (USB_Device_IsAddressSet()) {
  34. is_master = 1;
  35. return is_master;
  36. }
  37. wait_ms(SPLIT_USB_TIMEOUT_POLL);
  38. }
  39. is_master = 0;
  40. USB_Disable();
  41. USB_DeviceState = DEVICE_STATE_Unattached;
  42. }
  43. return is_master;
  44. }
  45. void keyboard_pre_init_kb(void) {
  46. setPinInputHigh(SPI_SS_PIN);
  47. keyboard_pre_init_user();
  48. }