usbkvm_fw.ino 3.6 KB

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