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.

220 lines
13 KiB

  1. # Backlighting :id=backlighting
  2. 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.
  3. 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.
  4. 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.
  5. 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:
  6. ```make
  7. BACKLIGHT_ENABLE = yes
  8. ```
  9. ## Keycodes :id=keycodes
  10. Once enabled, the following keycodes below can be used to change the backlight level.
  11. |Key |Description |
  12. |---------|-----------------------------------|
  13. |`BL_TOGG`|Turn the backlight on or off |
  14. |`BL_STEP`|Cycle through backlight levels |
  15. |`BL_ON` |Set the backlight to max brightness|
  16. |`BL_OFF` |Turn the backlight off |
  17. |`BL_INC` |Increase the backlight level |
  18. |`BL_DEC` |Decrease the backlight level |
  19. |`BL_BRTG`|Toggle backlight breathing |
  20. ## Functions :id=functions
  21. These functions can be used to change the backlighting in custom code:
  22. |Function |Description |
  23. |------------------------|--------------------------------------------|
  24. |`backlight_toggle()` |Turn the backlight on or off |
  25. |`backlight_enable()` |Turn the backlight on |
  26. |`backlight_disable()` |Turn the backlight off |
  27. |`backlight_step()` |Cycle through backlight levels |
  28. |`backlight_increase()` |Increase the backlight level |
  29. |`backlight_decrease()` |Decrease the backlight level |
  30. |`backlight_level(x)` |Sets the backlight level to specified level |
  31. |`get_backlight_level()` |Return the current backlight level |
  32. |`is_backlight_enabled()`|Return whether the backlight is currently on|
  33. If backlight breathing is enabled (see below), the following functions are also available:
  34. |Function |Description |
  35. |---------------------|--------------------------------------|
  36. |`breathing_toggle()` |Turn the backlight breathing on or off|
  37. |`breathing_enable()` |Turns on backlight breathing |
  38. |`breathing_disable()`|Turns off backlight breathing |
  39. ## Configuration :id=configuration
  40. To select which driver to use, configure your `rules.mk` with the following:
  41. ```make
  42. BACKLIGHT_DRIVER = software
  43. ```
  44. Valid driver values are `pwm`, `software`, `custom` or `no`. See below for help on individual drivers.
  45. To configure the backlighting, `#define` these in your `config.h`:
  46. |Define |Default |Description |
  47. |-----------------------------|------------------|-----------------------------------------------------------------------------------------------------------------|
  48. |`BACKLIGHT_PIN` |*Not defined* |The pin that controls the LED(s) |
  49. |`BACKLIGHT_LEVELS` |`3` |The number of brightness levels (maximum 31 excluding off) |
  50. |`BACKLIGHT_CAPS_LOCK` |*Not defined* |Enable Caps Lock indicator using backlight (for keyboards without dedicated LED) |
  51. |`BACKLIGHT_BREATHING` |*Not defined* |Enable backlight breathing, if supported |
  52. |`BREATHING_PERIOD` |`6` |The length of one backlight "breath" in seconds |
  53. |`BACKLIGHT_ON_STATE` |`1` |The state of the backlight pin when the backlight is "on" - `1` for high, `0` for low |
  54. |`BACKLIGHT_LIMIT_VAL` |`255` |The maximum duty cycle of the backlight -- `255` allows for full brightness, any lower will decrease the maximum.|
  55. |`BACKLIGHT_DEFAULT_LEVEL` |`BACKLIGHT_LEVELS`|The default backlight level to use upon clearing the EEPROM |
  56. |`BACKLIGHT_DEFAULT_BREATHING`|*Not defined* |Whether to enable backlight breathing upon clearing the EEPROM |
  57. Unless you are designing your own keyboard, you generally should not need to change the `BACKLIGHT_PIN` or `BACKLIGHT_ON_STATE`.
  58. ### Backlight On State :id=backlight-on-state
  59. 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*.
  60. 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.
  61. This functionality is configured at the keyboard level with the `BACKLIGHT_ON_STATE` define.
  62. ### AVR Driver :id=avr-driver
  63. The `pwm` driver is configured by default, however the equivalent setting within `rules.mk` would be:
  64. ```make
  65. BACKLIGHT_DRIVER = pwm
  66. ```
  67. #### Caveats :id=avr-caveats
  68. On AVR boards, QMK automatically decides which driver to use according to the following table:
  69. |Backlight Pin|AT90USB64/128|AT90USB162|ATmega16/32U4|ATmega16/32U2|ATmega32A|ATmega328/P|
  70. |-------------|-------------|----------|-------------|-------------|---------|-----------|
  71. |`B1` | | | | | |Timer 1 |
  72. |`B2` | | | | | |Timer 1 |
  73. |`B5` |Timer 1 | |Timer 1 | | | |
  74. |`B6` |Timer 1 | |Timer 1 | | | |
  75. |`B7` |Timer 1 |Timer 1 |Timer 1 |Timer 1 | | |
  76. |`C4` |Timer 3 | | | | | |
  77. |`C5` |Timer 3 |Timer 1 | |Timer 1 | | |
  78. |`C6` |Timer 3 |Timer 1 |Timer 3 |Timer 1 | | |
  79. |`D4` | | | | |Timer 1 | |
  80. |`D5` | | | | |Timer 1 | |
  81. All other pins will use timer-assisted software PWM:
  82. |Audio Pin|Audio Timer|Software PWM Timer|
  83. |---------|-----------|------------------|
  84. |`C4` |Timer 3 |Timer 1 |
  85. |`C5` |Timer 3 |Timer 1 |
  86. |`C6` |Timer 3 |Timer 1 |
  87. |`B5` |Timer 1 |Timer 3 |
  88. |`B6` |Timer 1 |Timer 3 |
  89. |`B7` |Timer 1 |Timer 3 |
  90. When both timers are in use for Audio, the backlight PWM cannot use a hardware timer, and 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.
  91. #### Hardware PWM Implementation :id=hardware-pwm-implementation
  92. 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.
  93. 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.
  94. 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.
  95. 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.
  96. 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.
  97. #### Timer Assisted PWM Implementation :id=timer-assisted-implementation
  98. 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.
  99. When resetting to 0, the CPU will fire an OVF (overflow) interrupt that will turn the LEDs on, starting the duty cycle.
  100. 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.
  101. 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.
  102. The breathing effect is the same as in the hardware PWM implementation.
  103. ### ARM Driver :id=arm-configuration
  104. While still in its early stages, ARM backlight support aims to eventually have feature parity with AVR. The `pwm` driver is configured by default, however the equivalent setting within `rules.mk` would be:
  105. ```make
  106. BACKLIGHT_DRIVER = pwm
  107. ```
  108. #### ChibiOS Configuration :id=arm-configuration
  109. The following `#define`s apply only to ARM-based keyboards:
  110. |Define |Default|Description |
  111. |-----------------------|-------|-----------------------------------|
  112. |`BACKLIGHT_PWM_DRIVER` |`PWMD4`|The PWM driver to use |
  113. |`BACKLIGHT_PWM_CHANNEL`|`3` |The PWM channel to use |
  114. |`BACKLIGHT_PAL_MODE` |`2` |The pin alternative function to use|
  115. See the ST datasheet for your particular MCU to determine these values. Unless you are designing your own keyboard, you generally should not need to change them.
  116. #### Caveats :id=arm-caveats
  117. Currently only hardware PWM is supported, not timer assisted, and does not provide automatic configuration.
  118. ### Software PWM Driver :id=software-pwm-driver
  119. In this mode, PWM is "emulated" 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`:
  120. ```make
  121. BACKLIGHT_DRIVER = software
  122. ```
  123. #### Multiple Backlight Pins :id=multiple-backlight-pins
  124. Most keyboards have only one backlight pin which controls all backlight LEDs (especially if the backlight is connected to a hardware PWM pin).
  125. In software PWM, it is possible to define multiple backlight pins, which will be turned on and off at the same time during the PWM duty cycle.
  126. This feature allows to set, for instance, the Caps Lock LED's (or any other controllable LED) brightness at the same level as the other LEDs of the backlight. This is useful if you have mapped Control 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, as it is usually wired to a separate pin from the backlight.
  127. To activate multiple backlight pins, add something like this to your `config.h`, instead of `BACKLIGHT_PIN`:
  128. ```c
  129. #define BACKLIGHT_PINS { F5, B2 }
  130. ```
  131. ### Custom Driver :id=custom-driver
  132. If none of the above drivers apply to your board (for example, you are using a separate IC to control the backlight), you can implement a custom backlight driver using this simple API provided by QMK. To enable, add this to your `rules.mk`:
  133. ```make
  134. BACKLIGHT_DRIVER = custom
  135. ```
  136. Then implement any of these hooks:
  137. ```c
  138. void backlight_init_ports(void) {
  139. // Optional - runs on startup
  140. // Usually you want to configure pins here
  141. }
  142. void backlight_set(uint8_t level) {
  143. // Optional - runs on level change
  144. // Usually you want to respond to the new value
  145. }
  146. void backlight_task(void) {
  147. // Optional - runs periodically
  148. // Note that this is called in the main keyboard loop,
  149. // so long running actions here can cause performance issues
  150. }
  151. ```
  152. ## Example Schematic
  153. In this typical example, the backlight LEDs are all connected in parallel towards an N-channel MOSFET. Its gate pin is wired to one of the microcontroller's GPIO pins through a 470โ„ฆ resistor to avoid ringing.
  154. A pulldown resistor is also placed between the gate pin and ground to keep it at a defined state when it is not otherwise being driven by the MCU.
  155. The values of these resistors are not critical - see [this Electronics StackExchange question](https://electronics.stackexchange.com/q/68748) for more information.
  156. ![Backlight example circuit](https://i.imgur.com/BmAvoUC.png)