2017-08-06 03:54:34 +00:00
# Debugging FAQ
This page details various common questions people have about troubleshooting their keyboards.
2021-01-20 18:46:01 +00:00
## Debugging :id=debugging
2017-08-06 03:54:34 +00:00
2022-10-19 10:33:05 +00:00
Your keyboard will output debug information if you have `CONSOLE_ENABLE = yes` in your `rules.mk` . By default the output is very limited, but you can turn on debug mode to increase the amount of debug output. Use the `DB_TOGG` keycode in your keymap, use the [Command ](feature_command.md ) feature to enable debug mode, or add the following code to your keymap.
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
```c
void keyboard_post_init_user(void) {
// Customise these values to desired behaviour
debug_enable=true;
debug_matrix=true;
//debug_keyboard=true;
//debug_mouse=true;
}
2017-08-06 03:54:34 +00:00
```
2021-01-20 18:46:01 +00:00
## Debugging Tools
2017-08-06 03:54:34 +00:00
2021-12-22 14:27:25 +00:00
Various tools are available to debug your keyboard.
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
### Debugging With QMK Toolbox
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
For compatible platforms, [QMK Toolbox ](https://github.com/qmk/qmk_toolbox ) can be used to display debug messages from your keyboard.
2017-08-06 03:54:34 +00:00
2021-12-22 14:27:25 +00:00
### Debugging with QMK CLI
Prefer a terminal based solution? The [QMK CLI console command ](cli_commands.md#qmk-console ) can be used to display debug messages from your keyboard.
2021-01-20 18:46:01 +00:00
### Debugging With hid_listen
2017-08-07 03:57:57 +00:00
2021-12-22 14:27:25 +00:00
Something stand-alone? [hid_listen ](https://www.pjrc.com/teensy/hid_listen.html ), provided by PJRC, can also be used to display debug messages. Prebuilt binaries for Windows,Linux,and MacOS are available.
2017-08-07 03:57:57 +00:00
2021-07-25 16:18:09 +00:00
## Sending Your Own Debug Messages :id=debug-api
2017-08-07 03:57:57 +00:00
2021-01-20 18:46:01 +00:00
Sometimes it's useful to print debug messages from within your [custom code ](custom_quantum_functions.md ). Doing so is pretty simple. Start by including `print.h` at the top of your file:
2017-08-07 03:57:57 +00:00
2021-01-20 18:46:01 +00:00
```c
#include "print.h"
2017-08-07 03:57:57 +00:00
```
2021-01-20 18:46:01 +00:00
After that you can use a few different print functions:
2019-07-13 20:33:44 +00:00
2021-01-20 18:46:01 +00:00
* `print("string")` : Print a simple string.
* `uprintf("%s string", var)` : Print a formatted string
* `dprint("string")` Print a simple string, but only when debug mode is enabled
* `dprintf("%s string", var)` : Print a formatted string, but only when debug mode is enabled
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
## Debug Examples
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
Below is a collection of real world debugging examples. For additional information, refer to [Debugging/Troubleshooting QMK ](faq_debug.md ).
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
### Which matrix position is this keypress?
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
When porting, or when attempting to diagnose pcb issues, it can be useful to know if a keypress is scanned correctly. To enable logging for this scenario, add the following code to your keymaps `keymap.c`
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
```c
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
// If console is enabled, it will print the matrix position and status of each key pressed
#ifdef CONSOLE_ENABLE
2022-07-25 22:07:15 +00:00
uprintf("KL: kc: 0x%04X, col: %2u, row: %2u, pressed: %u, time: %5u, int: %u, count: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed, record->event.time, record->tap.interrupted, record->tap.count);
2021-01-20 18:46:01 +00:00
#endif
return true;
}
```
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
Example output
2021-09-14 12:16:24 +00:00
```
2021-01-20 18:46:01 +00:00
Waiting for device:.......
Listening:
2022-07-25 22:07:15 +00:00
KL: kc: 169, col: 0, row: 0, pressed: 1, time: 15505, int: 0, count: 0
KL: kc: 169, col: 0, row: 0, pressed: 0, time: 15510, int: 0, count: 0
KL: kc: 174, col: 1, row: 0, pressed: 1, time: 15703, int: 0, count: 0
KL: kc: 174, col: 1, row: 0, pressed: 0, time: 15843, int: 0, count: 0
KL: kc: 172, col: 2, row: 0, pressed: 1, time: 16303, int: 0, count: 0
KL: kc: 172, col: 2, row: 0, pressed: 0, time: 16411, int: 0, count: 0
2021-01-20 18:46:01 +00:00
```
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
### How long did it take to scan for a keypress?
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
When testing performance issues, it can be useful to know the frequency at which the switch matrix is being scanned. To enable logging for this scenario, add the following code to your keymaps `config.h`
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
```c
#define DEBUG_MATRIX_SCAN_RATE
```
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
Example output
2021-09-14 12:16:24 +00:00
```
2021-01-20 18:46:01 +00:00
> matrix scan frequency: 315
> matrix scan frequency: 313
> matrix scan frequency: 316
> matrix scan frequency: 316
> matrix scan frequency: 316
> matrix scan frequency: 316
```
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
## `hid_listen` Can't Recognize Device
When debug console of your device is not ready you will see like this:
2017-08-06 03:54:34 +00:00
```
2021-01-20 18:46:01 +00:00
Waiting for device:.........
2017-08-06 03:54:34 +00:00
```
2021-01-23 21:41:29 +00:00
Once the device is plugged in then *hid_listen* finds it you will get this message:
2017-08-06 03:54:34 +00:00
2021-01-20 18:46:01 +00:00
```
Waiting for new device:.........................
Listening:
2017-08-06 03:54:34 +00:00
```
2021-01-20 18:46:01 +00:00
If you can't get this 'Listening:' message try building with `CONSOLE_ENABLE=yes` in [Makefile]
2017-08-06 03:54:34 +00:00
2021-01-23 21:41:29 +00:00
You may need privileges to access the device an OS like Linux. Try `sudo hid_listen` .
2017-08-06 03:54:34 +00:00
2021-03-03 21:31:41 +00:00
On many Linux distros you can avoid having to run hid_listen as root
by creating a file called `/etc/udev/rules.d/70-hid-listen.rules` with
the following content:
```
SUBSYSTEM=="hidraw", ATTRS{idVendor}=="abcd", ATTRS{idProduct}=="def1", TAG+="uaccess", RUN{builtin}+="uaccess"
```
Replace abcd and def1 with your keyboard's vendor and product id,
letters must be lowercase. The `RUN{builtin}+="uaccess"` part is only
needed for older distros.
2021-01-20 18:46:01 +00:00
## Can't Get Message on Console
Check:
- *hid_listen* finds your device. See above.
2021-01-23 21:41:29 +00:00
- Enable debug by pressing **Magic** +d. See [Magic Commands ](https://github.com/tmk/tmk_keyboard#magic-commands ).
- Set `debug_enable=true` . See [Debugging ](#debugging )
- Try using `print` function instead of debug print. See **common/print.h** .
- Disconnect other devices with console function. See [Issue #97 ](https://github.com/tmk/tmk_keyboard/issues/97 ).
2022-10-05 02:16:11 +00:00
- Ensure all strings end with a newline character (`\n`). QMK Toolbox prints console output on a per-line basis.