usbkvm_fw.ino 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. RemdesKVM USB-KVM
  3. Firmware for PCB design v6
  4. Author: tobychui
  5. Upload Settings
  6. CH552G
  7. 24Mhz (Internal)
  8. */
  9. #include <Serial.h>
  10. /* Build flags */
  11. //#define ENABLE_DEBUG 1 //Enable debug print to Serial, do not use this in IP-KVM setup
  12. //#define ENABLE_ATX_CTRL 1 //Enable ATX power control
  13. //#define COMPATIBLE_VERSION_FIVE_PCB //Enable v5 PCB compatibility
  14. /* Enums */
  15. #define USB_MS_SIDE_KVM_HOST 0
  16. #define USB_MS_SIDE_REMOTE_PC 1
  17. /* Pins definations */
  18. #define LED_PROG 14
  19. #define ATX_PWR_LED 15
  20. #define ATX_HDD_LED 16
  21. #define ATX_RST_BTN 33
  22. #define ATX_PWR_BTN 34
  23. #define USB_MS_PWR 31 //Active high, set to HIGH to enable USB 5V power and LOW to disable
  24. #define USB_MS_SW 30 //LOW = remote computer, HIGH = KVM
  25. /* Software definations */
  26. #define USB_PWR_SW_PWR_DELAY 100 //ms
  27. #define USB_PWR_SW_DATA_DELAY 10 //ms
  28. /* Runtime variables */
  29. uint8_t atx_status[2] = { 0, 0 }; //PWR LED, HDD LED
  30. uint8_t usb_ms_side = USB_MS_SIDE_REMOTE_PC;
  31. char c;
  32. int led_tmp;
  33. bool led_status = true; //Default LED PROG state is high, on every command recv it switch state
  34. /* Function Prototypes */
  35. void report_status();
  36. void update_atx_led_status();
  37. void switch_usbms_to_kvm();
  38. void switch_usbms_to_remote();
  39. void init_device_uuid();
  40. void print_device_uuid();
  41. void renew_device_uuid();
  42. void dumpEEPROM();
  43. //execute_cmd match and execute host to remote commands
  44. void execute_cmd(char c) {
  45. switch (c) {
  46. case 'p':
  47. //Press down the power button
  48. digitalWrite(ATX_PWR_BTN, HIGH);
  49. break;
  50. case 's':
  51. //Release the power button
  52. digitalWrite(ATX_PWR_BTN, LOW);
  53. break;
  54. case 'r':
  55. //Press down the reset button
  56. digitalWrite(ATX_RST_BTN, HIGH);
  57. break;
  58. case 'd':
  59. //Release the reset button
  60. digitalWrite(ATX_RST_BTN, LOW);
  61. break;
  62. case 'm':
  63. //Switch USB mass storage to KVM side
  64. switch_usbms_to_kvm();
  65. break;
  66. case 'n':
  67. //Switch USB mass storage to remote computer
  68. switch_usbms_to_remote();
  69. break;
  70. case 'u':
  71. //Return the UUID of this device
  72. print_device_uuid();
  73. break;
  74. #ifdef ENABLE_DEBUG
  75. case 'z':
  76. //Regenerate the UUID of this device, dev mode firmware only
  77. renew_device_uuid();
  78. break;
  79. #endif
  80. default:
  81. //Unknown command
  82. break;
  83. }
  84. }
  85. void setup() {
  86. pinMode(ATX_PWR_LED, INPUT);
  87. pinMode(ATX_HDD_LED, INPUT);
  88. pinMode(ATX_RST_BTN, OUTPUT);
  89. pinMode(ATX_PWR_BTN, OUTPUT);
  90. pinMode(USB_MS_PWR, OUTPUT);
  91. pinMode(USB_MS_SW, OUTPUT);
  92. pinMode(LED_PROG, OUTPUT);
  93. digitalWrite(LED_PROG, HIGH);
  94. digitalWrite(ATX_RST_BTN, LOW);
  95. digitalWrite(ATX_PWR_BTN, LOW);
  96. digitalWrite(USB_MS_PWR, LOW);
  97. digitalWrite(USB_MS_SW, LOW);
  98. //Setup the system UUID
  99. init_device_uuid();
  100. //Blink 10 times for initiations
  101. for (int i = 0; i < 10; i++) {
  102. digitalWrite(LED_PROG, HIGH);
  103. delay(100);
  104. digitalWrite(LED_PROG, LOW);
  105. delay(100);
  106. }
  107. digitalWrite(LED_PROG, HIGH);
  108. delay(1000);
  109. #ifdef ENABLE_DEBUG
  110. USBSerial_println("[WARNING] The current firmware has debug mode turned on. Do not use in production or IP-KVM setup!");
  111. #endif
  112. //Switch the USB thumbnail to host
  113. switch_usbms_to_kvm();
  114. }
  115. void loop() {
  116. if (USBSerial_available()) {
  117. c = USBSerial_read();
  118. #ifdef ENABLE_DEBUG
  119. USBSerial_print("[DEBUG] Serial Recv: ");
  120. USBSerial_println(c);
  121. #endif
  122. execute_cmd(c);
  123. led_status = !led_status;
  124. digitalWrite(LED_PROG, led_status ? HIGH : LOW);
  125. }
  126. #ifdef ENABLE_ATX_CTRL
  127. update_atx_led_status();
  128. report_status();
  129. #endif
  130. delay(100);
  131. }