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.

309 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. ## Usage :id=usage
  5. Most keyboards have backlighting enabled by default if they support it, but if it is not working for you (or you have added support), check that your `rules.mk` includes the following:
  6. ```make
  7. BACKLIGHT_ENABLE = yes
  8. ```
  9. ## Keycodes :id=keycodes
  10. |Key |Aliases |Description |
  11. |-------------------------------|---------|-----------------------------------|
  12. |`QK_BACKLIGHT_TOGGLE` |`BL_TOGG`|Turn the backlight on or off |
  13. |`QK_BACKLIGHT_STEP` |`BL_STEP`|Cycle through backlight levels |
  14. |`QK_BACKLIGHT_ON` |`BL_ON` |Set the backlight to max brightness|
  15. |`QK_BACKLIGHT_OFF` |`BL_OFF` |Turn the backlight off |
  16. |`QK_BACKLIGHT_UP` |`BL_UP` |Increase the backlight level |
  17. |`QK_BACKLIGHT_DOWN` |`BL_DOWN`|Decrease the backlight level |
  18. |`QK_BACKLIGHT_TOGGLE_BREATHING`|`BL_BRTG`|Toggle backlight breathing |
  19. ## Basic Configuration :id=basic-configuration
  20. Add the following to your `config.h`:
  21. |Define |Default |Description |
  22. |-----------------------------|------------------|-----------------------------------------------------------------------------------------------------------------|
  23. |`BACKLIGHT_PIN` |*Not defined* |The pin that controls the LEDs |
  24. |`BACKLIGHT_LEVELS` |`3` |The number of brightness levels (maximum 31 excluding off) |
  25. |`BACKLIGHT_CAPS_LOCK` |*Not defined* |Enable Caps Lock indicator using backlight (for keyboards without dedicated LED) |
  26. |`BACKLIGHT_BREATHING` |*Not defined* |Enable backlight breathing, if supported |
  27. |`BREATHING_PERIOD` |`6` |The length of one backlight "breath" in seconds |
  28. |`BACKLIGHT_ON_STATE` |`1` |The state of the backlight pin when the backlight is "on" - `1` for high, `0` for low |
  29. |`BACKLIGHT_LIMIT_VAL` |`255` |The maximum duty cycle of the backlight -- `255` allows for full brightness, any lower will decrease the maximum.|
  30. |`BACKLIGHT_DEFAULT_ON` |`true` |Enable backlight upon clearing the EEPROM |
  31. |`BACKLIGHT_DEFAULT_BREATHING`|`false` |Whether to enable backlight breathing upon clearing the EEPROM |
  32. |`BACKLIGHT_DEFAULT_LEVEL` |`BACKLIGHT_LEVELS`|The default backlight level to use upon clearing the EEPROM |
  33. Unless you are designing your own keyboard, you generally should not need to change the `BACKLIGHT_PIN` or `BACKLIGHT_ON_STATE`.
  34. ### "On" State :id=on-state
  35. 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*.
  36. 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.
  37. To configure the "on" state of the backlight circuit, add the following to your `config.h`:
  38. ```c
  39. #define BACKLIGHT_ON_STATE 0
  40. ```
  41. ### Multiple Backlight Pins :id=multiple-backlight-pins
  42. Most keyboards have only one backlight pin which controls all backlight LEDs (especially if the backlight is connected to a hardware PWM pin).
  43. The `timer` and `software` drivers allow you to define multiple backlight pins, which will be turned on and off at the same time during the PWM duty cycle.
  44. 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.
  45. To configure multiple backlight pins, add something like this to your `config.h`, instead of `BACKLIGHT_PIN`:
  46. ```c
  47. #define BACKLIGHT_PINS { F5, B2 }
  48. ```
  49. ## Driver Configuration :id=driver-configuration
  50. Backlight driver selection is configured in `rules.mk`. Valid drivers are `pwm` (default), `timer`, `software`, or `custom`. See below for information on individual drivers.
  51. ### PWM Driver :id=pwm-driver
  52. This is the default backlight driver, which leverages the hardware PWM output capability of the microcontroller.
  53. ```make
  54. BACKLIGHT_DRIVER = pwm
  55. ```
  56. ### Timer Driver :id=timer-driver
  57. This driver is similar to the PWM driver, but instead of directly configuring the pin to output a PWM signal, an interrupt handler is attached to the timer to turn the pin on and off as appropriate.
  58. ```make
  59. BACKLIGHT_DRIVER = timer
  60. ```
  61. ### Software Driver :id=software-driver
  62. In this mode, PWM is "emulated" while running other keyboard tasks. It offers maximum hardware compatibility without extra platform configuration. However, breathing is not supported, and the backlight can flicker when the keyboard is busy.
  63. ```make
  64. BACKLIGHT_DRIVER = software
  65. ```
  66. ### Custom Driver :id=custom-driver
  67. 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 a simple API.
  68. ```make
  69. BACKLIGHT_DRIVER = custom
  70. ```
  71. ```c
  72. void backlight_init_ports(void) {
  73. // Optional - runs on startup
  74. // Usually you want to configure pins here
  75. }
  76. void backlight_set(uint8_t level) {
  77. // Optional - runs on level change
  78. // Usually you want to respond to the new value
  79. }
  80. void backlight_task(void) {
  81. // Optional - runs periodically
  82. // Note that this is called in the main keyboard loop,
  83. // so long running actions here can cause performance issues
  84. }
  85. ```
  86. ## AVR Configuration :id=avr-configuration
  87. ### PWM Driver :id=avr-pwm-driver
  88. The following table describes the supported pins for the PWM driver. Only cells marked with a timer number are capable of hardware PWM output; any others must use the `timer` driver.
  89. |Backlight Pin|AT90USB64/128|AT90USB162|ATmega16/32U4|ATmega16/32U2|ATmega32A|ATmega328/P|
  90. |-------------|-------------|----------|-------------|-------------|---------|-----------|
  91. |`B1` | | | | | |Timer 1 |
  92. |`B2` | | | | | |Timer 1 |
  93. |`B5` |Timer 1 | |Timer 1 | | | |
  94. |`B6` |Timer 1 | |Timer 1 | | | |
  95. |`B7` |Timer 1 |Timer 1 |Timer 1 |Timer 1 | | |
  96. |`C4` |Timer 3 | | | | | |
  97. |`C5` |Timer 3 |Timer 1 | |Timer 1 | | |
  98. |`C6` |Timer 3 |Timer 1 |Timer 3 |Timer 1 | | |
  99. |`D4` | | | | |Timer 1 | |
  100. |`D5` | | | | |Timer 1 | |
  101. ### Timer Driver :id=avr-timer-driver
  102. Any GPIO pin can be used with this driver. The following table describes the supported timers:
  103. |AT90USB64/128|AT90USB162|ATmega16/32U4|ATmega16/32U2|ATmega32A|ATmega328/P|
  104. |-------------|----------|-------------|-------------|---------|-----------|
  105. |Timers 1 & 3 |Timer 1 |Timers 1 & 3 |Timer 1 |Timer 1 |Timer 1 |
  106. The following `#define`s apply only to the `timer` driver:
  107. |Define |Default|Description |
  108. |-----------------------|-------|----------------|
  109. |`BACKLIGHT_PWM_TIMER` |`1` |The timer to use|
  110. Note that the choice of timer may conflict with the [Audio](feature_audio.md) feature.
  111. ## ChibiOS/ARM Configuration :id=arm-configuration
  112. ### PWM Driver :id=arm-pwm-driver
  113. Depending on the ChibiOS board configuration, you may need to enable PWM at the keyboard level. For STM32, this would look like:
  114. `halconf.h`:
  115. ```c
  116. #define HAL_USE_PWM TRUE
  117. ```
  118. `mcuconf.h`:
  119. ```c
  120. #undef STM32_PWM_USE_TIM4
  121. #define STM32_PWM_USE_TIM4 TRUE
  122. ```
  123. The following `#define`s apply only to the `pwm` driver:
  124. |Define |Default |Description |
  125. |-----------------------|-------------|---------------------------------------------------------------|
  126. |`BACKLIGHT_PWM_DRIVER` |`PWMD4` |The PWM driver to use |
  127. |`BACKLIGHT_PWM_CHANNEL`|`3` |The PWM channel to use |
  128. |`BACKLIGHT_PAL_MODE` |`2` |The pin alternative function to use |
  129. |`BACKLIGHT_PWM_PERIOD` |*Not defined*|The PWM period in counter ticks - Default is platform dependent|
  130. Refer to the ST datasheet for your particular MCU to determine these values. For example, these defaults are set up for pin `B8` on a Proton-C (STM32F303) using `TIM4_CH3` on AF2. Unless you are designing your own keyboard, you generally should not need to change them.
  131. ### Timer Driver :id=arm-timer-driver
  132. Depending on the ChibiOS board configuration, you may need to enable general-purpose timers at the keyboard level. For STM32, this would look like:
  133. `halconf.h`:
  134. ```c
  135. #define HAL_USE_GPT TRUE
  136. ```
  137. `mcuconf.h`:
  138. ```c
  139. #undef STM32_GPT_USE_TIM15
  140. #define STM32_GPT_USE_TIM15 TRUE
  141. ```
  142. The following `#define`s apply only to the `timer` driver:
  143. |Define |Default |Description |
  144. |----------------------|--------|----------------|
  145. |`BACKLIGHT_GPT_DRIVER`|`GPTD15`|The timer to use|
  146. ## Example Schematic
  147. Since 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.
  148. 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.
  149. 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.
  150. The values of these resistors are not critical - see [this Electronics StackExchange question](https://electronics.stackexchange.com/q/68748) for more information.
  151. ![Backlight example circuit](https://i.imgur.com/BmAvoUC.png)
  152. ## API :id=api
  153. ### `void backlight_toggle(void)` :id=api-backlight-toggle
  154. Toggle the backlight on or off.
  155. ---
  156. ### `void backlight_enable(void)` :id=api-backlight-enable
  157. Turn the backlight on.
  158. ---
  159. ### `void backlight_disable(void)` :id=api-backlight-disable
  160. Turn the backlight off.
  161. ---
  162. ### `void backlight_step(void)` :id=api-backlight-step
  163. Cycle through backlight levels.
  164. ---
  165. ### `void backlight_increase(void)` :id=api-backlight-increase
  166. Increase the backlight level.
  167. ---
  168. ### `void backlight_decrease(void)` :id=api-backlight-decrease
  169. Decrease the backlight level.
  170. ---
  171. ### `void backlight_level(uint8_t level)` :id=api-backlight-level
  172. Set the backlight level.
  173. #### Arguments :id=api-backlight-level-arguments
  174. - `uint8_t level`
  175. The level to set, from 0 to `BACKLIGHT_LEVELS`.
  176. ---
  177. ### `uint8_t get_backlight_level(void)` :id=api-get-backlight-level
  178. Get the current backlight level.
  179. #### Return Value :id=api-get-backlight-level-return
  180. The current backlight level, from 0 to `BACKLIGHT_LEVELS`.
  181. ---
  182. ### `bool is_backlight_enabled(void)` :id=api-is-backlight-enabled
  183. Get the current backlight state.
  184. #### Return Value :id=api-is-backlight-enabled-return
  185. `true` if the backlight is enabled.
  186. ---
  187. ### `void backlight_toggle_breathing(void)` :id=api-backlight-toggle-breathing
  188. Toggle backlight breathing on or off.
  189. ---
  190. ### `void backlight_enable_breathing(void)` :id=api-backlight-enable-breathing
  191. Turn backlight breathing on.
  192. ---
  193. ### `void backlight_disable_breathing(void)` :id=api-backlight-disable-breathing
  194. Turn backlight breathing off.
  195. ---
  196. ### `bool is_backlight_breathing(void)` :id=api-is-backlight-breathing
  197. Get the current backlight breathing state.
  198. #### Return Value :id=api-is-backlight-breathing-return
  199. `true` if backlight breathing is enabled.