usbkvm_fw3.ino 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. RemdesKVM USB-KVM
  3. Firmware for PCB design v4 or above
  4. Author: tobychui
  5. Upload Settings
  6. CH552G
  7. 24Mhz (Internal)
  8. */
  9. #include <Serial.h>
  10. uint8_t c = 0x00;
  11. // Send a CH9329 cmd via UART0
  12. void send_cmd(uint8_t cmd, uint8_t* data, uint8_t length) {
  13. uint8_t sum = 0;
  14. Serial0_write(0x57);
  15. sum += 0x57;
  16. Serial0_write(0xAB);
  17. sum += 0xAB;
  18. Serial0_write(0x00);
  19. Serial0_write(cmd);
  20. sum += cmd;
  21. Serial0_write(length);
  22. sum += length;
  23. for (int i = 0; i < length; i++) {
  24. Serial0_write(data[i]);
  25. sum += data[i];
  26. }
  27. Serial0_write(sum);
  28. }
  29. // flush the serial RX (blocking)
  30. void flush_cmd_resp() {
  31. delay(100);
  32. while (Serial0_available()) {
  33. uint8_t b = Serial0_read();
  34. USBSerial_print("0x");
  35. if (b < 0x10) USBSerial_print("0");
  36. USBSerial_print(b, HEX);
  37. USBSerial_print(" ");
  38. }
  39. USBSerial_println("");
  40. }
  41. // Set the USB descriptor exposed to the slave device
  42. void setup_keyboard_chip_cfg() {
  43. //Set manufacturer string
  44. uint8_t manufacturer_string[9] = { 0x00, 0x07, 'i', 'm', 'u', 's', 'l', 'a', 'b' };
  45. send_cmd(0x0B, manufacturer_string, 9);
  46. flush_cmd_resp();
  47. //Set product string
  48. uint8_t product_string[11] = { 0x01, 0x09, 'R', 'e', 'm', 'd', 'e', 's', 'K', 'V', 'M' };
  49. send_cmd(0x0B, product_string, 11);
  50. flush_cmd_resp();
  51. }
  52. //This function handles the incoming data from CH9329
  53. void handle_cmd_processing() {
  54. }
  55. //This function will handle the incoming cmd from RemdesKVM control software
  56. //return 0x00 if success and 0x01 if error
  57. void handle_ctrl_cmd() {
  58. }
  59. void setup() {
  60. //Serial0_begin(9600); // CH9329 UART default baudrate (uncomment this before running -cfgchip)
  61. Serial0_begin(19200);
  62. delay(100);
  63. setup_keyboard_chip_cfg();
  64. }
  65. void loop() {
  66. // Read characters into buffer
  67. if (USBSerial_available()) {
  68. c = USBSerial_read();
  69. Serial0_write(c);
  70. }
  71. if (Serial0_available()) {
  72. c = Serial0_read();
  73. USBSerial_write(c);
  74. }
  75. }