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.

136 lines
4.8 KiB

  1. # 调试 FAQ
  2. <!---
  3. original document: 0.15.12:docs/faq_debug.md
  4. git diff 0.15.12 HEAD -- docs/faq_debug.md | cat
  5. -->
  6. 此页面详细介绍了人们对键盘故障排除的各种常见问题。
  7. ## 调试 :id=debugging
  8. 如果你在 `rules.mk` 中配置了 `CONSOLE_ENABLE = yes`,你的键盘将会输出调试信息。默认情况下输出很有限,可以启用调试模式来增加调试输出的丰富度。使用你的键映射方案中的 `DEBUG` 键码,或使用[指令](zh-cn/feature_command.md)功能来启动调试模式,或者将下面这段代码放到你的键映射中:
  9. ```c
  10. void keyboard_post_init_user(void) {
  11. // 通过调整这些值可以改变其表现
  12. debug_enable=true;
  13. debug_matrix=true;
  14. //debug_keyboard=true;
  15. //debug_mouse=true;
  16. }
  17. ```
  18. ## 调试工具
  19. 有多种可用于调试的工具。
  20. ### 使用QMK工具箱调试
  21. 在兼容的平台上,[QMK工具箱](https://github.com/qmk/qmk_toolbox)可以展示你的键盘的调试输出。
  22. ### 使用 QMK CLI 进行调试
  23. 倾向于在终端进行调试?使用 [QMK CLI 命令行](zh-cn/cli_commands.md#qmk-console)可以展示键盘输出的调试信息。
  24. ### 使用hid_listen调试
  25. 更喜欢使用终端的方案?PJRC提供的[hid_listen](https://www.pjrc.com/teensy/hid_listen.html)也可以用来展示调试信息,已有Windows、Linux及MacOS下预编译好的可执行文件。
  26. ## 发送自定义调试信息 :id=debug-api
  27. 有时在[自定义代码](zh-cn/custom_quantum_functions.md)中输出调试信息非常有用,要做到这个功能也很简单,在代码文件头部包含 `print.h` 文件:
  28. ```c
  29. #include "print.h"
  30. ```
  31. 然后可以使用以下输出函数:
  32. * `print("string")`: 字符串输出
  33. * `uprintf("%s string", var)`: 格式化字符串输出
  34. * `dprint("string")` 仅调试模式下,字符串输出
  35. * `dprintf("%s string", var)`: 仅调试模式下,格式化字符串输出
  36. ## 调试示例
  37. 以下列出了一些实际出现过的调试范例,更多资料参见[调试/定位QMK问题](zh-cn/faq_debug.md)。
  38. ### 当前按下的键的矩阵坐标是什么?
  39. 在移植或尝试诊断PCB问题时,确认按下的键被正确扫描到是很有用的排查步骤。要启用该场景的日志输出,请在 `keymap.c` 中添加:
  40. ```c
  41. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  42. // If console is enabled, it will print the matrix position and status of each key pressed
  43. #ifdef CONSOLE_ENABLE
  44. uprintf("KL: kc: 0x%04X, col: %u, row: %u, pressed: %b, time: %u, interrupt: %b, count: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed, record->event.time, record->tap.interrupted, record->tap.count);
  45. #endif
  46. return true;
  47. }
  48. ```
  49. 输出示例
  50. ```text
  51. Waiting for device:.......
  52. Listening:
  53. KL: kc: 169, col: 0, row: 0, pressed: 1
  54. KL: kc: 169, col: 0, row: 0, pressed: 0
  55. KL: kc: 174, col: 1, row: 0, pressed: 1
  56. KL: kc: 174, col: 1, row: 0, pressed: 0
  57. KL: kc: 172, col: 2, row: 0, pressed: 1
  58. KL: kc: 172, col: 2, row: 0, pressed: 0
  59. ```
  60. ### 扫描到一个键码需要多久?
  61. 调试性能问题时,知晓开关矩阵的扫描频率是很有用的排查步骤。要启用该场景的日志输出,请在 `config.h` 中添加:
  62. ```c
  63. #define DEBUG_MATRIX_SCAN_RATE
  64. ```
  65. 输出示例
  66. ```text
  67. > matrix scan frequency: 315
  68. > matrix scan frequency: 313
  69. > matrix scan frequency: 316
  70. > matrix scan frequency: 316
  71. > matrix scan frequency: 316
  72. > matrix scan frequency: 316
  73. ```
  74. ## `hid_listen` 无法识别到设备
  75. 如果设备没有就绪,在命令行下调试会看到如下输出:
  76. ```
  77. Waiting for device:.........
  78. ```
  79. 当设备插入后,*hid_listen*可以发现设备,会有如下输出:
  80. ```
  81. Waiting for new device:.........................
  82. Listening:
  83. ```
  84. 若无法出现'Listening:'消息,尝试在[Makefile]中添加 `CONSOLE_ENABLE=yes`
  85. 在类Linux系统下,访问设备可能需要一定权限,尝试使用 `sudo hid_listen`
  86. 此外,很多Linux发行版可以通过创建如下内容的文件 `/etc/udev/rules.d/70-hid-listen.rules` 来避免通过root权限执行hid_listen:
  87. ```
  88. SUBSYSTEM=="hidraw", ATTRS{idVendor}=="abcd", ATTRS{idProduct}=="def1", TAG+="uaccess", RUN{builtin}+="uaccess"
  89. ```
  90. 使用设备的真实VID和PID替换上面的abcd和def1,留意必须全小写。其中 `RUN{builtin}+="uaccess"` 仅在较老的发行版中需要使用。
  91. ## 命令行无法成功输出消息
  92. 请检查:
  93. - *hid_listen*确实找到了设备,如前文所述。
  94. - 通过**Magic**+d命令启用调试模式,参见[Magic Commands](https://github.com/tmk/tmk_keyboard#magic-commands).
  95. - 配置`debug_enable=true`. 参见[调试](#debugging)
  96. - 尝试用 `print` 替代 `dprint`, 参见**common/print.h**.
  97. - 拔出其它可能影响命令行的设备,参见[Issue #97](https://github.com/tmk/tmk_keyboard/issues/97).