backlight: split AVR PWM and timer drivers (#21540)

This commit is contained in:
Ryan 2023-07-21 09:27:55 +10:00 committed by GitHub
parent b090354143
commit 4137685f8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 570 additions and 317 deletions

View file

@ -1,12 +1,12 @@
# Backlighting :id=backlighting
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.
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.
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.
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.
## Usage :id=usage
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:
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:
```make
BACKLIGHT_ENABLE = yes
@ -14,10 +14,8 @@ BACKLIGHT_ENABLE = yes
## Keycodes :id=keycodes
Once enabled, the following keycodes below can be used to change the backlight level.
|Key |Aliases |Description |
|---------------------------------|-----------|-------------------------------------|
|-------------------------------|---------|-----------------------------------|
|`QK_BACKLIGHT_TOGGLE` |`BL_TOGG`|Turn the backlight on or off |
|`QK_BACKLIGHT_STEP` |`BL_STEP`|Cycle through backlight levels |
|`QK_BACKLIGHT_ON` |`BL_ON` |Set the backlight to max brightness|
@ -26,45 +24,13 @@ Once enabled, the following keycodes below can be used to change the backlight l
|`QK_BACKLIGHT_DOWN` |`BL_DOWN`|Decrease the backlight level |
|`QK_BACKLIGHT_TOGGLE_BREATHING`|`BL_BRTG`|Toggle backlight breathing |
## Functions :id=functions
## Basic Configuration :id=basic-configuration
These functions can be used to change the backlighting in custom code:
|Function |Description |
|------------------------|--------------------------------------------|
|`backlight_toggle()` |Turn the backlight on or off |
|`backlight_enable()` |Turn the backlight on |
|`backlight_disable()` |Turn the backlight off |
|`backlight_step()` |Cycle through backlight levels |
|`backlight_increase()` |Increase the backlight level |
|`backlight_decrease()` |Decrease the backlight level |
|`backlight_level(x)` |Sets the backlight level to specified level |
|`get_backlight_level()` |Return the current backlight level |
|`is_backlight_enabled()`|Return whether the backlight is currently on|
If backlight breathing is enabled (see below), the following functions are also available:
|Function |Description |
|---------------------|--------------------------------------|
|`breathing_toggle()` |Turn the backlight breathing on or off|
|`breathing_enable()` |Turns on backlight breathing |
|`breathing_disable()`|Turns off backlight breathing |
## Configuration :id=configuration
To select which driver to use, configure your `rules.mk` with the following:
```make
BACKLIGHT_DRIVER = software
```
Valid driver values are `pwm`, `software`, `custom` or `no`. See below for help on individual drivers.
To configure the backlighting, `#define` these in your `config.h`:
Add the following to your `config.h`:
|Define |Default |Description |
|-----------------------------|------------------|-----------------------------------------------------------------------------------------------------------------|
|`BACKLIGHT_PIN` |*Not defined* |The pin that controls the LED(s) |
|`BACKLIGHT_PIN` |*Not defined* |The pin that controls the LEDs |
|`BACKLIGHT_LEVELS` |`3` |The number of brightness levels (maximum 31 excluding off) |
|`BACKLIGHT_CAPS_LOCK` |*Not defined* |Enable Caps Lock indicator using backlight (for keyboards without dedicated LED) |
|`BACKLIGHT_BREATHING` |*Not defined* |Enable backlight breathing, if supported |
@ -76,124 +42,66 @@ To configure the backlighting, `#define` these in your `config.h`:
Unless you are designing your own keyboard, you generally should not need to change the `BACKLIGHT_PIN` or `BACKLIGHT_ON_STATE`.
### Backlight On State :id=backlight-on-state
### "On" State :id=on-state
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*.
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.
This functionality is configured at the keyboard level with the `BACKLIGHT_ON_STATE` define.
To configure the "on" state of the backlight circuit, add the following to your `config.h`:
### AVR Driver :id=avr-driver
The `pwm` driver is configured by default, however the equivalent setting within `rules.mk` would be:
```make
BACKLIGHT_DRIVER = pwm
```c
#define BACKLIGHT_ON_STATE 0
```
#### Caveats :id=avr-caveats
On AVR boards, QMK automatically decides which driver to use according to the following table:
|Backlight Pin|AT90USB64/128|AT90USB162|ATmega16/32U4|ATmega16/32U2|ATmega32A|ATmega328/P|
|-------------|-------------|----------|-------------|-------------|---------|-----------|
|`B1` | | | | | |Timer 1 |
|`B2` | | | | | |Timer 1 |
|`B5` |Timer 1 | |Timer 1 | | | |
|`B6` |Timer 1 | |Timer 1 | | | |
|`B7` |Timer 1 |Timer 1 |Timer 1 |Timer 1 | | |
|`C4` |Timer 3 | | | | | |
|`C5` |Timer 3 |Timer 1 | |Timer 1 | | |
|`C6` |Timer 3 |Timer 1 |Timer 3 |Timer 1 | | |
|`D4` | | | | |Timer 1 | |
|`D5` | | | | |Timer 1 | |
All other pins will use timer-assisted software PWM:
|Audio Pin|Audio Timer|Software PWM Timer|
|---------|-----------|------------------|
|`C4` |Timer 3 |Timer 1 |
|`C5` |Timer 3 |Timer 1 |
|`C6` |Timer 3 |Timer 1 |
|`B5` |Timer 1 |Timer 3 |
|`B6` |Timer 1 |Timer 3 |
|`B7` |Timer 1 |Timer 3 |
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.
#### Hardware PWM Implementation :id=hardware-pwm-implementation
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.
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.
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.
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.
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.
#### Timer Assisted PWM Implementation :id=timer-assisted-implementation
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.
When resetting to 0, the CPU will fire an OVF (overflow) interrupt that will turn the LEDs on, starting the duty cycle.
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.
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.
The breathing effect is the same as in the hardware PWM implementation.
### ARM Driver :id=arm-configuration
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:
```make
BACKLIGHT_DRIVER = pwm
```
#### ChibiOS Configuration :id=arm-configuration
The following `#define`s apply only to ARM-based keyboards:
|Define |Default|Description |
|-----------------------|-------|-----------------------------------|
|`BACKLIGHT_PWM_DRIVER` |`PWMD4`|The PWM driver to use |
|`BACKLIGHT_PWM_CHANNEL`|`3` |The PWM channel to use |
|`BACKLIGHT_PAL_MODE` |`2` |The pin alternative function to use|
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.
#### Caveats :id=arm-caveats
Currently only hardware PWM is supported, not timer assisted, and does not provide automatic configuration.
### Software PWM Driver :id=software-pwm-driver
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`:
```make
BACKLIGHT_DRIVER = software
```
#### Multiple Backlight Pins :id=multiple-backlight-pins
### Multiple Backlight Pins :id=multiple-backlight-pins
Most keyboards have only one backlight pin which controls all backlight LEDs (especially if the backlight is connected to a hardware PWM pin).
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.
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.
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.
To activate multiple backlight pins, add something like this to your `config.h`, instead of `BACKLIGHT_PIN`:
To configure multiple backlight pins, add something like this to your `config.h`, instead of `BACKLIGHT_PIN`:
```c
#define BACKLIGHT_PINS { F5, B2 }
```
## Driver Configuration :id=driver-configuration
Backlight driver selection is configured in `rules.mk`. Valid drivers are `pwm` (default), `timer`, `software`, or `custom`. See below for information on individual drivers.
### PWM Driver :id=pwm-driver
This is the default backlight driver, which leverages the hardware PWM output capability of the microcontroller.
```make
BACKLIGHT_DRIVER = pwm
```
### Timer Driver :id=timer-driver
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.
```make
BACKLIGHT_DRIVER = timer
```
### Software Driver :id=software-driver
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.
```make
BACKLIGHT_DRIVER = software
```
### Custom Driver :id=custom-driver
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`:
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.
```make
BACKLIGHT_DRIVER = custom
```
Then implement any of these hooks:
```c
void backlight_init_ports(void) {
// Optional - runs on startup
@ -211,10 +119,188 @@ void backlight_task(void) {
}
```
## AVR Configuration :id=avr-configuration
### PWM Driver :id=avr-pwm-driver
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.
|Backlight Pin|AT90USB64/128|AT90USB162|ATmega16/32U4|ATmega16/32U2|ATmega32A|ATmega328/P|
|-------------|-------------|----------|-------------|-------------|---------|-----------|
|`B1` | | | | | |Timer 1 |
|`B2` | | | | | |Timer 1 |
|`B5` |Timer 1 | |Timer 1 | | | |
|`B6` |Timer 1 | |Timer 1 | | | |
|`B7` |Timer 1 |Timer 1 |Timer 1 |Timer 1 | | |
|`C4` |Timer 3 | | | | | |
|`C5` |Timer 3 |Timer 1 | |Timer 1 | | |
|`C6` |Timer 3 |Timer 1 |Timer 3 |Timer 1 | | |
|`D4` | | | | |Timer 1 | |
|`D5` | | | | |Timer 1 | |
### Timer Driver :id=avr-timer-driver
Any GPIO pin can be used with this driver. The following table describes the supported timers:
|AT90USB64/128|AT90USB162|ATmega16/32U4|ATmega16/32U2|ATmega32A|ATmega328/P|
|-------------|----------|-------------|-------------|---------|-----------|
|Timers 1 & 3 |Timer 1 |Timers 1 & 3 |Timer 1 |Timer 1 |Timer 1 |
The following `#define`s apply only to the `timer` driver:
|Define |Default|Description |
|-----------------------|-------|----------------|
|`BACKLIGHT_PWM_TIMER` |`1` |The timer to use|
Note that the choice of timer may conflict with the [Audio](feature_audio.md) feature.
## ChibiOS/ARM Configuration :id=arm-configuration
### PWM Driver :id=arm-pwm-driver
Depending on the ChibiOS board configuration, you may need to enable PWM at the keyboard level. For STM32, this would look like:
`halconf.h`:
```c
#define HAL_USE_PWM TRUE
```
`mcuconf.h`:
```c
#undef STM32_PWM_USE_TIM4
#define STM32_PWM_USE_TIM4 TRUE
```
The following `#define`s apply only to the `pwm` driver:
|Define |Default |Description |
|-----------------------|--------|-----------------------------------|
|`BACKLIGHT_PWM_DRIVER` |`PWMD4` |The PWM driver to use |
|`BACKLIGHT_PWM_CHANNEL`|`3` |The PWM channel to use |
|`BACKLIGHT_PAL_MODE` |`2` |The pin alternative function to use|
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.
### Timer Driver :id=arm-timer-driver
Depending on the ChibiOS board configuration, you may need to enable general-purpose timers at the keyboard level. For STM32, this would look like:
`halconf.h`:
```c
#define HAL_USE_GPT TRUE
```
`mcuconf.h`:
```c
#undef STM32_GPT_USE_TIM15
#define STM32_GPT_USE_TIM15 TRUE
```
The following `#define`s apply only to the `timer` driver:
|Define |Default |Description |
|----------------------|--------|----------------|
|`BACKLIGHT_GPT_DRIVER`|`GPTD15`|The timer to use|
## Example Schematic
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.
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.
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.
The values of these resistors are not critical - see [this Electronics StackExchange question](https://electronics.stackexchange.com/q/68748) for more information.
![Backlight example circuit](https://i.imgur.com/BmAvoUC.png)
## API :id=api
### `void backlight_toggle(void)` :id=api-backlight-toggle
Toggle the backlight on or off.
---
### `void backlight_enable(void)` :id=api-backlight-enable
Turn the backlight on.
---
### `void backlight_disable(void)` :id=api-backlight-disable
Turn the backlight off.
---
### `void backlight_step(void)` :id=api-backlight-step
Cycle through backlight levels.
---
### `void backlight_increase(void)` :id=api-backlight-increase
Increase the backlight level.
---
### `void backlight_decrease(void)` :id=api-backlight-decrease
Decrease the backlight level.
---
### `void backlight_level(uint8_t level)` :id=api-backlight-level
Set the backlight level.
#### Arguments :id=api-backlight-level-arguments
- `uint8_t level`
The level to set, from 0 to `BACKLIGHT_LEVELS`.
---
### `uint8_t get_backlight_level(void)` :id=api-get-backlight-level
Get the current backlight level.
#### Return Value :id=api-get-backlight-level-return
The current backlight level, from 0 to `BACKLIGHT_LEVELS`.
---
### `bool is_backlight_enabled(void)` :id=api-is-backlight-enabled
Get the current backlight state.
#### Return Value :id=api-is-backlight-enabled-return
`true` if the backlight is enabled.
---
### `void backlight_toggle_breathing(void)` :id=api-backlight-toggle-breathing
Toggle backlight breathing on or off.
---
### `void backlight_enable_breathing(void)` :id=api-backlight-enable-breathing
Turn backlight breathing on.
---
### `void backlight_disable_breathing(void)` :id=api-backlight-disable-breathing
Turn backlight breathing off.
---
### `bool is_backlight_breathing(void)` :id=api-is-backlight-breathing
Get the current backlight breathing state.
#### Return Value :id=api-is-backlight-breathing-return
`true` if backlight breathing is enabled.

View file

@ -9,6 +9,7 @@
"device_version": "0.0.1"
},
"backlight": {
"driver": "timer",
"pins": ["F6", "F7"]
},
"processor": "atmega32u4",

View file

@ -9,6 +9,7 @@
"device_version": "10.0.1"
},
"backlight": {
"driver": "timer",
"pins": ["F4", "F5"],
"levels": 6,
"breathing": true

View file

@ -8,6 +8,7 @@
"pid": "0x2171"
},
"backlight": {
"driver": "timer",
"pins": ["D1", "D0", "D4", "C6", "D7", "E6", "B4", "B5"],
"levels": 8
},

View file

@ -5,6 +5,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D1",
"levels": 5,
"breathing": true

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D4",
"breathing": true
},

View file

@ -9,6 +9,7 @@
"device_version": "0.0.1"
},
"backlight": {
"driver": "timer",
"pin": "D7",
"breathing": true
},

View file

@ -15,6 +15,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "F7"
},
"processor": "atmega32u4",

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D0",
"levels": 5,
"breathing": true

View file

@ -15,6 +15,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "E6"
},
"indicators": {

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D1",
"levels": 6,
"breathing": true

View file

@ -31,6 +31,7 @@
]
},
"backlight": {
"driver": "timer",
"pin": "C4",
"levels": 6,
"breathing": true

View file

@ -19,6 +19,7 @@
]
},
"backlight": {
"driver": "timer",
"pin": "D1",
"levels": 6,
"breathing": true

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D0",
"breathing": true
},

View file

@ -23,6 +23,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D4",
"on_state": 0
},

View file

@ -9,6 +9,7 @@
"device_version": "1.0.0"
},
"backlight": {
"driver": "timer",
"pin": "F6",
"levels": 5
},

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "F5",
"levels": 6
},

View file

@ -10,6 +10,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D2",
"levels": 5
},

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "B1",
"levels": 5
},

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "F7",
"on_state": 0
},

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D0",
"levels": 6
},

View file

@ -9,6 +9,7 @@
"device_version": "2.0.5"
},
"backlight": {
"driver": "timer",
"pins": ["C2", "C7", "D5", "D6", "B0"],
"levels": 10
},

View file

@ -13,6 +13,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D2"
},
"processor": "atmega32u4",

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D2"
},
"processor": "atmega32u4",

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D0"
},
"processor": "atmega32u4",

View file

@ -5,7 +5,6 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "software",
"pin": "B7",
"levels": 10
},

View file

@ -3,7 +3,7 @@
#pragma once
#define BACKLIGHT_CUSTOM_RESOLUTION 0x400
#define BACKLIGHT_RESOLUTION 0x400
/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
#define LOCKING_SUPPORT_ENABLE

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D2"
},
"rgblight": {

View file

@ -14,8 +14,8 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "software",
"pins": ["D6"],
"driver": "timer",
"pin": "D6",
"levels": 6
},
"indicators": {

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "F5",
"levels": 6
},

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "F0"
},
"rgblight": {

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "C7",
"levels": 5,
"breathing": true

View file

@ -21,6 +21,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D6",
"breathing": true
},

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "F7",
"on_state": 0
},

View file

@ -44,6 +44,7 @@
"pin": "D4"
},
"backlight": {
"driver": "timer",
"levels": 6,
"max_brightness": 191,
"pin": "D6"

View file

@ -12,6 +12,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D0",
"levels": 5,
"breathing": true

View file

@ -9,6 +9,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "F5",
"levels": 6,
"on_state": 0

View file

@ -9,6 +9,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "F5",
"levels": 6,
"on_state": 0

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "F5",
"levels": 6,
"breathing": true,

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "F5",
"levels": 10,
"on_state": 0

View file

@ -14,6 +14,7 @@
},
"diode_direction": "COL2ROW",
"backlight": {
"driver": "timer",
"pin": "D0",
"on_state": 0
},

View file

@ -1,5 +1,5 @@
#include "backlight.h"
#include "backlight_driver_common.h"
#include "gpio.h"
#include "progmem.h"
#include <avr/io.h>
#include <avr/interrupt.h>
@ -9,14 +9,6 @@
# define BACKLIGHT_LIMIT_VAL 255
#endif
// This logic is a bit complex, we support 3 setups:
//
// 1. Hardware PWM when backlight is wired to a PWM pin.
// Depending on this pin, we use a different output compare unit.
// 2. Software PWM with hardware timers, but the used timer
// depends on the Audio setup (Audio wins over Backlight).
// 3. Full software PWM, driven by the matrix scan, if both timers are used by Audio.
#if (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) && (BACKLIGHT_PIN == B5 || BACKLIGHT_PIN == B6 || BACKLIGHT_PIN == B7)
# define ICRx ICR1
# define TCCRxA TCCR1A
@ -122,41 +114,17 @@
# define COMxx1 COM1B1
# define OCRxx OCR1B
# endif
#elif (AUDIO_PIN != B5) && (AUDIO_PIN != B6) && (AUDIO_PIN != B7) && (AUDIO_PIN_ALT != B5) && (AUDIO_PIN_ALT != B6) && (AUDIO_PIN_ALT != B7)
// Timer 1 is not in use by Audio feature, Backlight can use it
# pragma message "Using hardware timer 1 with software PWM"
# define BACKLIGHT_PWM_TIMER
# define ICRx ICR1
# define TCCRxA TCCR1A
# define TCCRxB TCCR1B
# define TIMERx_COMPA_vect TIMER1_COMPA_vect
# define TIMERx_OVF_vect TIMER1_OVF_vect
# if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
# define TIMSKx TIMSK
# else
# define TIMSKx TIMSK1
# endif
# define TOIEx TOIE1
# define OCIExA OCIE1A
# define OCRxx OCR1A
#elif (AUDIO_PIN != C4) && (AUDIO_PIN != C5) && (AUDIO_PIN != C6)
# pragma message "Using hardware timer 3 with software PWM"
// Timer 3 is not in use by Audio feature, Backlight can use it
# define BACKLIGHT_PWM_TIMER
# define ICRx ICR1
# define TCCRxA TCCR3A
# define TCCRxB TCCR3B
# define TIMERx_COMPA_vect TIMER3_COMPA_vect
# define TIMERx_OVF_vect TIMER3_OVF_vect
# define TIMSKx TIMSK3
# define TOIEx TOIE3
# define OCIExA OCIE3A
# define OCRxx OCR3A
#endif
#ifndef BACKLIGHT_PWM_TIMER // pwm through software
#ifndef BACKLIGHT_RESOLUTION
# define BACKLIGHT_RESOLUTION 0xFFFFU
#endif
#if (BACKLIGHT_RESOLUTION > 0xFFFF || BACKLIGHT_RESOLUTION < 0x00FF)
# error "Backlight resolution must be between 0x00FF and 0xFFFF"
#endif
#define BREATHING_SCALE_FACTOR F_CPU / BACKLIGHT_RESOLUTION / 120
static inline void enable_pwm(void) {
#if BACKLIGHT_ON_STATE == 1
@ -174,54 +142,6 @@ static inline void disable_pwm(void) {
#endif
}
#endif
#ifdef BACKLIGHT_PWM_TIMER
// The idea of software PWM assisted by hardware timers is the following
// we use the hardware timer in fast PWM mode like for hardware PWM, but
// instead of letting the Output Match Comparator control the led pin
// (which is not possible since the backlight is not wired to PWM pins on the
// CPU), we do the LED on/off by oursleves.
// The timer is setup to count up to 0xFFFF, and we set the Output Compare
// register to the current 16bits backlight level (after CIE correction).
// This means the CPU will trigger a compare match interrupt when the counter
// reaches the backlight level, where we turn off the LEDs,
// but also an overflow interrupt when the counter rolls back to 0,
// in which we're going to turn on the LEDs.
// The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz,
// or F_CPU/BACKLIGHT_CUSTOM_RESOLUTION if used.
// Triggered when the counter reaches the OCRx value
ISR(TIMERx_COMPA_vect) {
backlight_pins_off();
}
// Triggered when the counter reaches the TOP value
// this one triggers at F_CPU/ICRx = 16MHz/65536 =~ 244 Hz
ISR(TIMERx_OVF_vect) {
# ifdef BACKLIGHT_BREATHING
if (is_breathing()) {
breathing_task();
}
# endif
// for very small values of OCRxx (or backlight level)
// we can't guarantee this whole code won't execute
// at the same time as the compare match interrupt
// which means that we might turn on the leds while
// trying to turn them off, leading to flickering
// artifacts (especially while breathing, because breathing_task
// takes many computation cycles).
// so better not turn them on while the counter TOP is very low.
if (OCRxx > ICRx / 250 + 5) {
backlight_pins_on();
}
}
#endif
#define TIMER_TOP 0xFFFFU
// See http://jared.geek.nz/2013/feb/linear-led-pwm
static uint16_t cie_lightness(uint16_t v) {
if (v <= (uint32_t)ICRx / 12) // If the value is less than or equal to ~8% of max
@ -254,26 +174,11 @@ void backlight_set(uint8_t level) {
if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
if (level == 0) {
#ifdef BACKLIGHT_PWM_TIMER
if (OCRxx) {
TIMSKx &= ~(_BV(OCIExA));
TIMSKx &= ~(_BV(TOIEx));
}
#else
// Turn off PWM control on backlight pin
disable_pwm();
#endif
backlight_pins_off();
} else {
#ifdef BACKLIGHT_PWM_TIMER
if (!OCRxx) {
TIMSKx |= _BV(OCIExA);
TIMSKx |= _BV(TOIEx);
}
#else
// Turn on PWM control of backlight pin
enable_pwm();
#endif
}
// Set the brightness
set_pwm(cie_lightness(rescale_limit_val(ICRx * (uint32_t)level / BACKLIGHT_LEVELS)));
@ -282,7 +187,6 @@ void backlight_set(uint8_t level) {
void backlight_task(void) {}
#ifdef BACKLIGHT_BREATHING
# define BREATHING_NO_HALT 0
# define BREATHING_HALT_OFF 1
# define BREATHING_HALT_ON 2
@ -294,24 +198,6 @@ static uint16_t breathing_counter = 0;
static uint8_t breath_scale_counter = 1;
/* Run the breathing loop at ~120Hz*/
const uint8_t breathing_ISR_frequency = 120;
static uint16_t breathing_freq_scale_factor = 2;
# ifdef BACKLIGHT_PWM_TIMER
static bool breathing = false;
bool is_breathing(void) {
return breathing;
}
# define breathing_interrupt_enable() \
do { \
breathing = true; \
} while (0)
# define breathing_interrupt_disable() \
do { \
breathing = false; \
} while (0)
# else
bool is_breathing(void) {
return !!(TIMSKx & _BV(TOIEx));
@ -325,7 +211,6 @@ bool is_breathing(void) {
do { \
TIMSKx &= ~_BV(TOIEx); \
} while (0)
# endif
# define breathing_min() \
do { \
@ -374,20 +259,14 @@ static inline uint16_t scale_backlight(uint16_t v) {
return v / BACKLIGHT_LEVELS * get_backlight_level();
}
# ifdef BACKLIGHT_PWM_TIMER
void breathing_task(void)
# else
/* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
* about 244 times per second.
*
* The following ISR runs at F_CPU/ISRx. With a 16MHz clock and default pwm resolution, that means 244Hz
*/
ISR(TIMERx_OVF_vect)
# endif
{
ISR(TIMERx_OVF_vect) {
// Only run this ISR at ~120 Hz
if (breath_scale_counter++ == breathing_freq_scale_factor) {
if (breath_scale_counter++ == BREATHING_SCALE_FACTOR) {
breath_scale_counter = 1;
} else {
return;
@ -412,19 +291,17 @@ ISR(TIMERx_OVF_vect)
#endif // BACKLIGHT_BREATHING
void backlight_init_ports(void) {
// Setup backlight pin as output and output to on state.
backlight_pins_init();
setPinOutput(BACKLIGHT_PIN);
#if BACKLIGHT_ON_STATE == 1
writePinLow(BACKLIGHT_PIN);
#else
writePinHigh(BACKLIGHT_PIN);
#endif
// I could write a wall of text here to explain... but TL;DW
// Go read the ATmega32u4 datasheet.
// And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
#ifdef BACKLIGHT_PWM_TIMER
// TimerX setup, Fast PWM mode count to TOP set in ICRx
TCCRxA = _BV(WGM11); // = 0b00000010;
// clock select clk/1
TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
#else // hardware PWM
// Pin PB7 = OCR1C (Timer 1, Channel C)
// Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
// (i.e. start high, go low when counter matches.)
@ -438,23 +315,10 @@ void backlight_init_ports(void) {
*/
TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
#endif
#ifdef BACKLIGHT_CUSTOM_RESOLUTION
# if (BACKLIGHT_CUSTOM_RESOLUTION > 0xFFFF || BACKLIGHT_CUSTOM_RESOLUTION < 1)
# error "This out of range of the timer capabilities"
# elif (BACKLIGHT_CUSTOM_RESOLUTION < 0xFF)
# warning "Resolution lower than 0xFF isn't recommended"
# endif
# ifdef BACKLIGHT_BREATHING
breathing_freq_scale_factor = F_CPU / BACKLIGHT_CUSTOM_RESOLUTION / 120;
# endif
ICRx = BACKLIGHT_CUSTOM_RESOLUTION;
#else
ICRx = TIMER_TOP;
#endif
ICRx = BACKLIGHT_RESOLUTION;
backlight_init();
#ifdef BACKLIGHT_BREATHING
if (is_backlight_breathing()) {
breathing_enable();

View file

@ -0,0 +1,267 @@
#include "backlight.h"
#include "backlight_driver_common.h"
#include "progmem.h"
#include <avr/io.h>
#include <avr/interrupt.h>
// Maximum duty cycle limit
#ifndef BACKLIGHT_LIMIT_VAL
# define BACKLIGHT_LIMIT_VAL 255
#endif
#ifndef BACKLIGHT_PWM_TIMER
# define BACKLIGHT_PWM_TIMER 1
#endif
#if BACKLIGHT_PWM_TIMER == 1
# define ICRx ICR1
# define TCCRxA TCCR1A
# define TCCRxB TCCR1B
# define TIMERx_COMPA_vect TIMER1_COMPA_vect
# define TIMERx_OVF_vect TIMER1_OVF_vect
# if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
# define TIMSKx TIMSK
# else
# define TIMSKx TIMSK1
# endif
# define TOIEx TOIE1
# define OCIExA OCIE1A
# define OCRxx OCR1A
#elif BACKLIGHT_PWM_TIMER == 3
# define ICRx ICR1
# define TCCRxA TCCR3A
# define TCCRxB TCCR3B
# define TIMERx_COMPA_vect TIMER3_COMPA_vect
# define TIMERx_OVF_vect TIMER3_OVF_vect
# define TIMSKx TIMSK3
# define TOIEx TOIE3
# define OCIExA OCIE3A
# define OCRxx OCR3A
#else
# error Invalid backlight PWM timer!
#endif
#ifndef BACKLIGHT_RESOLUTION
# define BACKLIGHT_RESOLUTION 0xFFFFU
#endif
#if (BACKLIGHT_RESOLUTION > 0xFFFF || BACKLIGHT_RESOLUTION < 0x00FF)
# error "Backlight resolution must be between 0x00FF and 0xFFFF"
#endif
#define BREATHING_SCALE_FACTOR F_CPU / BACKLIGHT_RESOLUTION / 120
// The idea of software PWM assisted by hardware timers is the following
// we use the hardware timer in fast PWM mode like for hardware PWM, but
// instead of letting the Output Match Comparator control the led pin
// (which is not possible since the backlight is not wired to PWM pins on the
// CPU), we do the LED on/off by oursleves.
// The timer is setup to count up to 0xFFFF, and we set the Output Compare
// register to the current 16bits backlight level (after CIE correction).
// This means the CPU will trigger a compare match interrupt when the counter
// reaches the backlight level, where we turn off the LEDs,
// but also an overflow interrupt when the counter rolls back to 0,
// in which we're going to turn on the LEDs.
// The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz,
// or F_CPU/BACKLIGHT_RESOLUTION if used.
// Triggered when the counter reaches the OCRx value
ISR(TIMERx_COMPA_vect) {
backlight_pins_off();
}
// Triggered when the counter reaches the TOP value
// this one triggers at F_CPU/ICRx = 16MHz/65536 =~ 244 Hz
ISR(TIMERx_OVF_vect) {
#ifdef BACKLIGHT_BREATHING
if (is_breathing()) {
breathing_task();
}
#endif
// for very small values of OCRxx (or backlight level)
// we can't guarantee this whole code won't execute
// at the same time as the compare match interrupt
// which means that we might turn on the leds while
// trying to turn them off, leading to flickering
// artifacts (especially while breathing, because breathing_task
// takes many computation cycles).
// so better not turn them on while the counter TOP is very low.
if (OCRxx > ICRx / 250 + 5) {
backlight_pins_on();
}
}
// See http://jared.geek.nz/2013/feb/linear-led-pwm
static uint16_t cie_lightness(uint16_t v) {
if (v <= (uint32_t)ICRx / 12) // If the value is less than or equal to ~8% of max
{
return v / 9; // Same as dividing by 900%
} else {
// In the next two lines values are bit-shifted. This is to avoid loosing decimals in integer math.
uint32_t y = (((uint32_t)v + (uint32_t)ICRx / 6) << 5) / ((uint32_t)ICRx / 6 + ICRx); // If above 8%, add ~16% of max, and normalize with (max + ~16% max)
uint32_t out = (y * y * y * ICRx) >> 15; // Cube it and undo the bit-shifting. (which is now three times as much due to the cubing)
if (out > ICRx) // Avoid overflows
{
out = ICRx;
}
return (uint16_t)out;
}
}
// rescale the supplied backlight value to be in terms of the value limit // range for val is [0..ICRx]. PWM pin is high while the timer count is below val.
static uint32_t rescale_limit_val(uint32_t val) {
return (val * (BACKLIGHT_LIMIT_VAL + 1)) / 256;
}
// range for val is [0..ICRx]. PWM pin is high while the timer count is below val.
static inline void set_pwm(uint16_t val) {
OCRxx = val;
}
void backlight_set(uint8_t level) {
if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
if (level == 0) {
if (OCRxx) {
TIMSKx &= ~(_BV(OCIExA));
TIMSKx &= ~(_BV(TOIEx));
}
backlight_pins_off();
} else {
if (!OCRxx) {
TIMSKx |= _BV(OCIExA);
TIMSKx |= _BV(TOIEx);
}
}
// Set the brightness
set_pwm(cie_lightness(rescale_limit_val(ICRx * (uint32_t)level / BACKLIGHT_LEVELS)));
}
void backlight_task(void) {}
#ifdef BACKLIGHT_BREATHING
# define BREATHING_NO_HALT 0
# define BREATHING_HALT_OFF 1
# define BREATHING_HALT_ON 2
# define BREATHING_STEPS 128
static uint8_t breathing_halt = BREATHING_NO_HALT;
static uint16_t breathing_counter = 0;
static uint8_t breath_scale_counter = 1;
/* Run the breathing loop at ~120Hz*/
const uint8_t breathing_ISR_frequency = 120;
static bool breathing = false;
bool is_breathing(void) {
return breathing;
}
# define breathing_interrupt_enable() \
do { \
breathing = true; \
} while (0)
# define breathing_interrupt_disable() \
do { \
breathing = false; \
} while (0)
# define breathing_min() \
do { \
breathing_counter = 0; \
} while (0)
# define breathing_max() \
do { \
breathing_counter = get_breathing_period() * breathing_ISR_frequency / 2; \
} while (0)
void breathing_enable(void) {
breathing_counter = 0;
breathing_halt = BREATHING_NO_HALT;
breathing_interrupt_enable();
}
void breathing_pulse(void) {
if (get_backlight_level() == 0)
breathing_min();
else
breathing_max();
breathing_halt = BREATHING_HALT_ON;
breathing_interrupt_enable();
}
void breathing_disable(void) {
breathing_interrupt_disable();
// Restore backlight level
backlight_set(get_backlight_level());
}
void breathing_self_disable(void) {
if (get_backlight_level() == 0)
breathing_halt = BREATHING_HALT_OFF;
else
breathing_halt = BREATHING_HALT_ON;
}
/* To generate breathing curve in python:
* from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
*/
static const uint8_t breathing_table[BREATHING_STEPS] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Use this before the cie_lightness function.
static inline uint16_t scale_backlight(uint16_t v) {
return v / BACKLIGHT_LEVELS * get_backlight_level();
}
void breathing_task(void) {
// Only run this ISR at ~120 Hz
if (breath_scale_counter++ == BREATHING_SCALE_FACTOR) {
breath_scale_counter = 1;
} else {
return;
}
uint16_t interval = (uint16_t)get_breathing_period() * breathing_ISR_frequency / BREATHING_STEPS;
// resetting after one period to prevent ugly reset at overflow.
breathing_counter = (breathing_counter + 1) % (get_breathing_period() * breathing_ISR_frequency);
uint8_t index = breathing_counter / interval;
// limit index to max step value
if (index >= BREATHING_STEPS) {
index = BREATHING_STEPS - 1;
}
if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) {
breathing_interrupt_disable();
}
// Set PWM to a brightnessvalue scaled to the configured resolution
set_pwm(cie_lightness(rescale_limit_val(scale_backlight((uint32_t)pgm_read_byte(&breathing_table[index]) * ICRx / 255))));
}
#endif // BACKLIGHT_BREATHING
void backlight_init_ports(void) {
// Setup backlight pin as output and output to on state.
backlight_pins_init();
// I could write a wall of text here to explain... but TL;DW
// Go read the ATmega32u4 datasheet.
// And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
// TimerX setup, Fast PWM mode count to TOP set in ICRx
TCCRxA = _BV(WGM11); // = 0b00000010;
// clock select clk/1
TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
ICRx = BACKLIGHT_RESOLUTION;
backlight_init();
#ifdef BACKLIGHT_BREATHING
if (is_backlight_breathing()) {
breathing_enable();
}
#endif
}