]> git.donarmstrong.com Git - qmk_firmware.git/blob - docs/feature_backlight.md
Change newbs.md to link to the new newbs_git_best_practices.md instead of newbs_best_...
[qmk_firmware.git] / docs / feature_backlight.md
1 # Backlighting
2
3 Many keyboards support backlit keys by way of individual LEDs placed through or underneath the keyswitches. This feature is distinct from both the [RGB underglow](feature_rgblight.md) and [RGB matrix](feature_rgb_matrix.md) features as it usually allows for only a single colour per switch, though you can obviously install multiple different single coloured LEDs on a keyboard.
4
5 QMK is able to control the brightness of these LEDs by switching them on and off rapidly in a certain ratio, a technique known as *Pulse Width Modulation*, or PWM. By altering the duty cycle of the PWM signal, it creates the illusion of dimming.
6
7 The MCU can only supply so much current to its GPIO pins. Instead of powering the backlight directly from the MCU, the backlight pin is connected to a transistor or MOSFET that switches the power to the LEDs.
8
9 ## Driver configuration
10
11 Most keyboards have backlighting enabled by default if they support it, but if it is not working for you, check that your `rules.mk` includes the following:
12
13 ```makefile
14 BACKLIGHT_ENABLE = software # Valid driver values are 'yes,software,no'
15 ```
16
17 See below for help on individual drivers.
18
19 ## Keycodes
20 Once enabled the following keycodes below can be used to change the backlight level.
21
22 |Key      |Description                               |
23 |---------|------------------------------------------|
24 |`BL_TOGG`|Turn the backlight on or off              |
25 |`BL_STEP`|Cycle through backlight levels            |
26 |`BL_ON`  |Set the backlight to max brightness       |
27 |`BL_OFF` |Turn the backlight off                    |
28 |`BL_INC` |Increase the backlight level              |
29 |`BL_DEC` |Decrease the backlight level              |
30 |`BL_BRTG`|Toggle backlight breathing                |
31
32 ## Backlight Functions
33
34 |Function  |Description                                                |
35 |----------|-----------------------------------------------------------|
36 |`backlight_toggle()`    |Turn the backlight on or off                 |
37 |`backlight_enable()`    |Turn the backlight on                        |
38 |`backlight_disable()`   |Turn the backlight off                       |
39 |`backlight_step()`      |Cycle through backlight levels               |
40 |`backlight_increase()`  |Increase the backlight level                 |
41 |`backlight_decrease()`  |Decrease the backlight level                 |
42 |`backlight_level(x)`    |Sets the backlight level to specified level  |
43 |`get_backlight_level()` |Return the current backlight level           |
44 |`is_backlight_enabled()`|Return whether the backlight is currently on |
45
46 ### Backlight Breathing Functions
47
48 |Function  |Description                                        |
49 |----------|---------------------------------------------------|
50 |`breathing_toggle()`  |Turn the backlight breathing on or off |
51 |`breathing_enable()`  |Turns on backlight breathing           |
52 |`breathing_disable()` |Turns off backlight breathing          |
53
54 ## Common Driver Configuration
55
56 To change the behavior of the backlighting, `#define` these in your `config.h`:
57
58 |Define               |Default      |Description                                                                           |
59 |---------------------|-------------|--------------------------------------------------------------------------------------|
60 |`BACKLIGHT_LEVELS`   |`3`          |The number of brightness levels (maximum 31 excluding off)                            |
61 |`BACKLIGHT_CAPS_LOCK`|*Not defined*|Enable Caps Lock indicator using backlight (for keyboards without dedicated LED)      |
62 |`BACKLIGHT_BREATHING`|*Not defined*|Enable backlight breathing, if supported                                              |
63 |`BREATHING_PERIOD`   |`6`          |The length of one backlight "breath" in seconds                                       |
64 |`BACKLIGHT_ON_STATE` |`0`          |The state of the backlight pin when the backlight is "on" - `1` for high, `0` for low |
65
66 ### Backlight On State
67
68 Most backlight circuits are driven by an N-channel MOSFET or NPN transistor. This means that to turn the transistor *on* and light the LEDs, you must drive the backlight pin, connected to the gate or base, *high*.
69 Sometimes, however, a P-channel MOSFET, or a PNP transistor is used. In this case, when the transistor is on, the pin is driven *low* instead.
70
71 This functionality is configured at the keyboard level with the `BACKLIGHT_ON_STATE` define.
72
73 ## AVR driver
74
75 On AVR boards, the default driver currently sniffs the configuration to pick the best scenario. To enable it, add this to your rules.mk:
76 ```makefile
77 BACKLIGHT_ENABLE = yes
78 ```
79
80 ### Caveats
81
82 Hardware PWM is supported according to the following table:
83
84 |Backlight Pin|AT90USB64/128|ATmega16/32U4|ATmega16/32U2|ATmega32A|ATmega328P|
85 |-------------|-------------|-------------|-------------|---------|----------|
86 |`B1`         |             |             |             |         |Timer 1   |
87 |`B2`         |             |             |             |         |Timer 1   |
88 |`B5`         |Timer 1      |Timer 1      |             |         |          |
89 |`B6`         |Timer 1      |Timer 1      |             |         |          |
90 |`B7`         |Timer 1      |Timer 1      |Timer 1      |         |          |
91 |`C4`         |Timer 3      |             |             |         |          |
92 |`C5`         |Timer 3      |             |Timer 1      |         |          |
93 |`C6`         |Timer 3      |Timer 3      |Timer 1      |         |          |
94 |`D4`         |             |             |             |Timer 1  |          |
95 |`D5`         |             |             |             |Timer 1  |          |
96
97 All other pins will use software PWM. If the [Audio](feature_audio.md) feature is disabled or only using one timer, the backlight PWM can be triggered by a hardware timer:
98
99 |Audio Pin|Audio Timer|Software PWM Timer|
100 |---------|-----------|------------------|
101 |`C4`     |Timer 3    |Timer 1           |
102 |`C5`     |Timer 3    |Timer 1           |
103 |`C6`     |Timer 3    |Timer 1           |
104 |`B5`     |Timer 1    |Timer 3           |
105 |`B6`     |Timer 1    |Timer 3           |
106 |`B7`     |Timer 1    |Timer 3           |
107
108 When both timers are in use for Audio, the backlight PWM will not use a hardware timer, but will instead be triggered during the matrix scan. In this case, breathing is not supported, and the backlight might flicker, because the PWM computation may not be called with enough timing precision.
109
110 ### AVR Configuration
111
112 To change the behavior of the backlighting, `#define` these in your `config.h`:
113
114 |Define               |Default      |Description                                                                                                   |
115 |---------------------|-------------|--------------------------------------------------------------------------------------------------------------|
116 |`BACKLIGHT_PIN`      |`B7`         |The pin that controls the LEDs. Unless you are designing your own keyboard, you shouldn't need to change this |
117 |`BACKLIGHT_PINS`     |*Not defined*|experimental: see below for more information                                                                  |
118
119 ### Multiple backlight pins
120
121 Most keyboards have only one backlight pin which control all backlight LEDs (especially if the backlight is connected to an hardware PWM pin).
122 In software PWM, it is possible to define multiple backlight pins. All those pins will be turned on and off at the same time during the PWM duty cycle.
123 This feature allows to set for instance the Caps Lock LED (or any other controllable LED) brightness at the same level as the other LEDs of the backlight. This is useful if you have mapped LCTRL in place of Caps Lock and you need the Caps Lock LED to be part of the backlight instead of being activated when Caps Lock is on.
124
125 To activate multiple backlight pins, you need to add something like this to your user `config.h`:
126
127 ```c
128 #define BACKLIGHT_LED_COUNT 2
129 #undef BACKLIGHT_PIN
130 #define BACKLIGHT_PINS { F5, B2 }
131 ```
132
133 ### Hardware PWM Implementation
134
135 When using the supported pins for backlighting, QMK will use a hardware timer configured to output a PWM signal. This timer will count up to `ICRx` (by default `0xFFFF`) before resetting to 0.
136 The desired brightness is calculated and stored in the `OCRxx` register. When the counter reaches this value, the backlight pin will go low, and is pulled high again when the counter resets.
137 In this way `OCRxx` essentially controls the duty cycle of the LEDs, and thus the brightness, where `0x0000` is completely off and `0xFFFF` is completely on.
138
139 The breathing effect is achieved by registering an interrupt handler for `TIMER1_OVF_vect` that is called whenever the counter resets, roughly 244 times per second.
140 In this handler, the value of an incrementing counter is mapped onto a precomputed brightness curve. To turn off breathing, the interrupt handler is simply disabled, and the brightness reset to the level stored in EEPROM.
141
142 ### Timer Assisted PWM Implementation
143
144 When `BACKLIGHT_PIN` is not set to a hardware backlight pin, QMK will use a hardware timer configured to trigger software interrupts. This time will count up to `ICRx` (by default `0xFFFF`) before resetting to 0.
145 When resetting to 0, the CPU will fire an OVF (overflow) interrupt that will turn the LEDs on, starting the duty cycle.
146 The desired brightness is calculated and stored in the `OCRxx` register. When the counter reaches this value, the CPU will fire a Compare Output match interrupt, which will turn the LEDs off.
147 In this way `OCRxx` essentially controls the duty cycle of the LEDs, and thus the brightness, where `0x0000` is completely off and `0xFFFF` is completely on.
148
149 The breathing effect is the same as in the hardware PWM implementation.
150
151 ## ARM Driver
152
153 While still in its early stages, ARM backlight support aims to eventually have feature parity with AVR. To enable it, add this to your rules.mk:
154 ```makefile
155 BACKLIGHT_ENABLE = yes
156 ```
157
158 ### Caveats
159
160 Currently only hardware PWM is supported, not timer assisted, and does not provide automatic configuration.
161
162 ?> STMF072 support is being investigated.
163
164 ### ARM Configuration
165
166 To change the behavior of the backlighting, `#define` these in your `config.h`:
167
168 |Define                  |Default      |Description                                                                                                  |
169 |------------------------|-------------|-------------------------------------------------------------------------------------------------------------|
170 |`BACKLIGHT_PIN`         |`B7`         |The pin that controls the LEDs. Unless you are designing your own keyboard, you shouldn't need to change this|
171 |`BACKLIGHT_PWM_DRIVER`  |`PWMD4`      |The PWM driver to use, see ST datasheets for pin to PWM timer mapping. Unless you are designing your own keyboard, you shouldn't need to change this|
172 |`BACKLIGHT_PWM_CHANNEL` |`3`          |The PWM channel to use, see ST datasheets for pin to PWM channel mapping. Unless you are designing your own keyboard, you shouldn't need to change this|
173 |`BACKLIGHT_PAL_MODE`    |`2`          |The pin alternative function to use, see ST datasheets for pin AF mapping. Unless you are designing your own keyboard, you shouldn't need to change this|
174
175 ## Software PWM Driver
176
177 Emulation of PWM while running other keyboard tasks, it offers maximum hardware compatibility without extra platform configuration. The tradeoff is the backlight might jitter when the keyboard is busy. To enable, add this to your rules.mk:
178 ```makefile
179 BACKLIGHT_ENABLE = software
180 ```
181
182 ### Software PWM Configuration
183
184 To change the behavior of the backlighting, `#define` these in your `config.h`:
185
186 |Define           |Default      |Description                                                                                                  |
187 |-----------------|-------------|-------------------------------------------------------------------------------------------------------------|
188 |`BACKLIGHT_PIN`  |`B7`         |The pin that controls the LEDs. Unless you are designing your own keyboard, you shouldn't need to change this|
189 |`BACKLIGHT_PINS` |*Not defined*|experimental: see below for more information                                                                 |
190
191 ### Multiple backlight pins
192
193 Most keyboards have only one backlight pin which control all backlight LEDs (especially if the backlight is connected to an hardware PWM pin).
194 In software PWM, it is possible to define multiple backlight pins. All those pins will be turned on and off at the same time during the PWM duty cycle.
195 This feature allows to set for instance the Caps Lock LED (or any other controllable LED) brightness at the same level as the other LEDs of the backlight. This is useful if you have mapped LCTRL in place of Caps Lock and you need the Caps Lock LED to be part of the backlight instead of being activated when Caps Lock is on.
196
197 To activate multiple backlight pins, you need to add something like this to your user `config.h`:
198
199 ```c
200 #undef BACKLIGHT_PIN
201 #define BACKLIGHT_PINS { F5, B2 }
202 ```