usbkvm_fw.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. RemdesKVM USB-KVM Firmware
  3. Author: tobychui
  4. This is the USB KVM part of RemdesKVM.
  5. It can be used seperately as a dedicated USB-KVM
  6. module as well as connected to a Linux SBC running
  7. remdeskvmd via a USB 2.0 connection.
  8. Upload Settings
  9. CH552G
  10. */
  11. #ifndef USER_USB_RAM
  12. #error "This firmware needs to be compiled with a USER USB setting"
  13. #endif
  14. //#define ENABLE_DEBUG_PRINT
  15. #include "usbkvm_fw.h"
  16. #include "src/remdesHid/USBHIDKeyboardMouse.h"
  17. /*
  18. instr_count store the current read index
  19. index of current instruction, all instructions have 3 bytes
  20. byte orders as follows
  21. byte 0 = opr_type
  22. byte 1 = opr_subtype
  23. byte 2 = data
  24. */
  25. uint8_t instr_count = 0;
  26. /*
  27. opr_type defines the type of the incoming data
  28. for the next byte. See usbkvm_fw.h for details.
  29. */
  30. uint8_t opr_type = 0x00;
  31. /*
  32. opr_subtype defines the sub-type of the operation
  33. Based on opr_type, there will be different catergory
  34. of operations. However, 0x00 is always reserved
  35. */
  36. uint8_t opr_subtype = 0x00;
  37. uint8_t opr_payload = 0x00;
  38. uint8_t serial_data = 0x00;
  39. /* Function Prototypes */
  40. uint8_t keyboard_emulation(uint8_t, uint8_t);
  41. uint8_t kvm_execute_opr() {
  42. switch (opr_type) {
  43. case OPR_TYPE_RESERVED:
  44. //Ping test
  45. return resp_ok;
  46. case OPR_TYPE_KEYBOARD_WRITE:
  47. //keyboard operations
  48. return keyboard_emulation(opr_subtype, opr_payload);
  49. case OPR_TYPE_MOUSE_WRITE:
  50. //mouse operations
  51. return resp_ok;
  52. default:
  53. return resp_unknown_opr;
  54. }
  55. return resp_ok;
  56. }
  57. void setup() {
  58. Serial0_begin(115200);
  59. pinMode(USB_SW_SEL, OUTPUT);
  60. pinMode(LED_RW_SIG, OUTPUT);
  61. digitalWrite(LED_RW_SIG, HIGH);
  62. digitalWrite(USB_SW_SEL, LOW);
  63. USBInit();
  64. }
  65. void loop() {
  66. if (Serial0_available()) {
  67. serial_data = 0x00;
  68. serial_data = Serial0_read();
  69. #ifdef ENABLE_DEBUG_PRINT
  70. //Debug print the Serial input message
  71. Serial0_write(resp_start_of_debug_msg);
  72. Serial0_write(serial_data);
  73. Serial0_write(resp_end_of_debug_msg);
  74. #endif
  75. if (serial_data == OPR_TYPE_DATA_RESET) {
  76. //Reset opr data
  77. opr_type = 0x00;
  78. opr_subtype = 0x00;
  79. opr_payload = 0x00;
  80. instr_count = 0;
  81. Serial0_write(resp_ok);
  82. } else if (instr_count == 0) {
  83. //Set opr type
  84. opr_type = serial_data;
  85. instr_count++;
  86. Serial0_write(resp_ok);
  87. } else if (instr_count == 1) {
  88. //Set opr subtype
  89. opr_subtype = serial_data;
  90. instr_count++;
  91. Serial0_write(resp_ok);
  92. } else {
  93. opr_payload = serial_data;
  94. #ifdef ENABLE_DEBUG_PRINT
  95. //Debug print the opr sequence
  96. Serial0_write(resp_start_of_debug_msg);
  97. Serial0_write(opr_type);
  98. Serial0_write(opr_subtype);
  99. Serial0_write(opr_payload);
  100. Serial0_write(resp_end_of_debug_msg);
  101. #endif
  102. //Execute the kvm operation
  103. uint8_t err = kvm_execute_opr();
  104. if (err != resp_ok) {
  105. //Check if there are any execution error. If yes, return error code with 0xEE prefix
  106. Serial0_write(resp_start_of_err_msg);
  107. Serial0_write(err);
  108. Serial0_write(resp_end_of_err_msg);
  109. } else {
  110. Serial0_write(resp_ok);
  111. }
  112. //Reset the instruction counter and ready for the next instruction
  113. instr_count = 0;
  114. }
  115. }
  116. delay(1);
  117. }