Improve backlight PWM pin support (#6202)
* Improve backlight PWM pin support * I accidentally an equals sign * Another typo * Order by pin number * Throw an error if backlight pin is C4 or C5 on 16/32U4 * Use else for clarity * Minor alignment adjustments
This commit is contained in:
parent
57540af102
commit
4d72aa428f
|
@ -76,7 +76,7 @@ This is a C header file that is one of the first things included, and will persi
|
||||||
* `#define B7_AUDIO`
|
* `#define B7_AUDIO`
|
||||||
* enables audio on pin B7 (duophony is enables if one of B[5-7]\_AUDIO is enabled along with one of C[4-6]\_AUDIO)
|
* enables audio on pin B7 (duophony is enables if one of B[5-7]\_AUDIO is enabled along with one of C[4-6]\_AUDIO)
|
||||||
* `#define BACKLIGHT_PIN B7`
|
* `#define BACKLIGHT_PIN B7`
|
||||||
* pin of the backlight - `B5`, `B6`, `B7` and `C6` (and `D4` on ATmega32A) use hardware PWM, others use software implementation
|
* pin of the backlight
|
||||||
* `#define BACKLIGHT_LEVELS 3`
|
* `#define BACKLIGHT_LEVELS 3`
|
||||||
* number of levels your backlight will have (maximum 15 excluding off)
|
* number of levels your backlight will have (maximum 15 excluding off)
|
||||||
* `#define BACKLIGHT_BREATHING`
|
* `#define BACKLIGHT_BREATHING`
|
||||||
|
|
|
@ -30,32 +30,31 @@ You should then be able to use the keycodes below to change the backlight level.
|
||||||
|
|
||||||
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 use multiple different coloured LEDs on a keyboard.
|
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 use multiple different coloured LEDs on a keyboard.
|
||||||
|
|
||||||
Hardware PWM is only supported on certain pins of the MCU, so if the backlighting is not connected to one of them, a software PWM implementation triggered by hardware timer interrupts will be used.
|
|
||||||
|
|
||||||
Hardware PWM is supported according to the following table:
|
Hardware PWM is supported according to the following table:
|
||||||
|
|
||||||
| Backlight Pin | Hardware timer |
|
|Backlight Pin|AT90USB64/128|ATmega16/32U4|ATmega16/32U2|ATmega32A|
|
||||||
|---------------|-------------------------|
|
|-------------|-------------|-------------|-------------|---------|
|
||||||
|`B5` | Timer 1 |
|
|`B5` |Timer 1 |Timer 1 | | |
|
||||||
|`B6` | Timer 1 |
|
|`B6` |Timer 1 |Timer 1 | | |
|
||||||
|`B7` | Timer 1 |
|
|`B7` |Timer 1 |Timer 1 |Timer 1 | |
|
||||||
|`C6` | Timer 3 |
|
|`C4` |Timer 3 | | | |
|
||||||
|`D4` | Timer 1 (ATmega32A only)|
|
|`C5` |Timer 3 | |Timer 1 | |
|
||||||
| other | Software PWM |
|
|`C6` |Timer 3 |Timer 3 |Timer 1 | |
|
||||||
|
|`D4` | | | |Timer 1 |
|
||||||
|
|`D5` | | | |Timer 1 |
|
||||||
|
|
||||||
The [audio feature](feature_audio.md) also uses hardware timers. Please refer to the following table to know what hardware timer the software PWM will use depending on the audio configuration:
|
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:
|
||||||
|
|
||||||
| Audio Pin(s) | Audio Timer | Software PWM Timer |
|
|Audio Pin|Audio Timer|Software PWM Timer|
|
||||||
|--------------|-------------|--------------------|
|
|---------|-----------|------------------|
|
||||||
| `C4` | Timer 3 | Timer 1 |
|
|`C4` |Timer 3 |Timer 1 |
|
||||||
| `C5` | Timer 3 | Timer 1 |
|
|`C5` |Timer 3 |Timer 1 |
|
||||||
| `C6` | Timer 3 | Timer 1 |
|
|`C6` |Timer 3 |Timer 1 |
|
||||||
| `B5` | Timer 1 | Timer 3 |
|
|`B5` |Timer 1 |Timer 3 |
|
||||||
| `B6` | Timer 1 | Timer 3 |
|
|`B6` |Timer 1 |Timer 3 |
|
||||||
| `B7` | Timer 1 | Timer 3 |
|
|`B7` |Timer 1 |Timer 3 |
|
||||||
| `Bx` & `Cx` | Timer 1 & 3 | None |
|
|
||||||
|
|
||||||
When all timers are in use for [audio](feature_audio.md), the backlight software PWM will not use a hardware timer, but instead will be triggered during the matrix scan. In this case the backlight doesn't support breathing and might show lighting artifacts (for instance flickering), because the PWM computation might not be called with enough timing precision.
|
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.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,7 @@ This allows the keyboard to tell the host OS that up to 248 keys are held down a
|
||||||
|
|
||||||
`BACKLIGHT_ENABLE`
|
`BACKLIGHT_ENABLE`
|
||||||
|
|
||||||
This enables your backlight on Timer1 and ports B5, B6, or B7 (for now). You can specify your port by putting this in your `config.h`:
|
This enables the in-switch LED backlighting. You can specify the backlight pin by putting this in your `config.h`:
|
||||||
|
|
||||||
#define BACKLIGHT_PIN B7
|
#define BACKLIGHT_PIN B7
|
||||||
|
|
||||||
|
|
|
@ -125,7 +125,7 @@ To configure a keyboard where each switch is connected to a separate pin and gro
|
||||||
|
|
||||||
### Backlight Configuration
|
### Backlight Configuration
|
||||||
|
|
||||||
By default QMK supports backlighting on pins `B5`, `B6`, and `B7`. If you are using one of those you can simply enable it here. For more details see the [Backlight Documentation](feature_backlight.md).
|
QMK supports backlighting on most GPIO pins. A select few of these can be driven by the MCU in hardware. For more details see the [Backlight Documentation](feature_backlight.md).
|
||||||
|
|
||||||
```c
|
```c
|
||||||
#define BACKLIGHT_PIN B7
|
#define BACKLIGHT_PIN B7
|
||||||
|
@ -134,8 +134,6 @@ By default QMK supports backlighting on pins `B5`, `B6`, and `B7`. If you are us
|
||||||
#define BREATHING_PERIOD 6
|
#define BREATHING_PERIOD 6
|
||||||
```
|
```
|
||||||
|
|
||||||
?> You can use backlighting on any pin you like, but you will have to do more work to support that. See the [Backlight Documentation](feature_backlight.md) for more details.
|
|
||||||
|
|
||||||
### Other Configuration Options
|
### Other Configuration Options
|
||||||
|
|
||||||
There are a lot of features that can be configured or tuned in `config.h`. You should see the [Config Options](config_options.md) page for more details.
|
There are a lot of features that can be configured or tuned in `config.h`. You should see the [Config Options](config_options.md) page for more details.
|
||||||
|
|
|
@ -34,7 +34,7 @@ For the `DIODE_DIRECTION`, most hand-wiring guides will instruct you to wire the
|
||||||
|
|
||||||
To configure a keyboard where each switch is connected to a separate pin and ground instead of sharing row and column pins, use `DIRECT_PINS`. The mapping defines the pins of each switch in rows and columns, from left to right. Must conform to the sizes within `MATRIX_ROWS` and `MATRIX_COLS`, use `NO_PIN` to fill in blank spaces. Overrides the behaviour of `DIODE_DIRECTION`, `MATRIX_ROW_PINS` and `MATRIX_COL_PINS`.
|
To configure a keyboard where each switch is connected to a separate pin and ground instead of sharing row and column pins, use `DIRECT_PINS`. The mapping defines the pins of each switch in rows and columns, from left to right. Must conform to the sizes within `MATRIX_ROWS` and `MATRIX_COLS`, use `NO_PIN` to fill in blank spaces. Overrides the behaviour of `DIODE_DIRECTION`, `MATRIX_ROW_PINS` and `MATRIX_COL_PINS`.
|
||||||
|
|
||||||
`BACKLIGHT_PIN` is the pin that your PWM-controlled backlight (if one exists) is hooked-up to. Currently only B5, B6, and B7 are supported.
|
`BACKLIGHT_PIN` is the pin that your PWM-controlled backlight (if one exists) is hooked-up to.
|
||||||
|
|
||||||
`BACKLIGHT_BREATHING` is a fancier backlight feature that adds breathing/pulsing/fading effects to the backlight. It uses the same timer as the normal backlight. These breathing effects must be called by code in your keymap.
|
`BACKLIGHT_BREATHING` is a fancier backlight feature that adds breathing/pulsing/fading effects to the backlight. It uses the same timer as the normal backlight. These breathing effects must be called by code in your keymap.
|
||||||
|
|
||||||
|
|
|
@ -1034,104 +1034,147 @@ void matrix_scan_quantum() {
|
||||||
}
|
}
|
||||||
#if defined(BACKLIGHT_ENABLE) && (defined(BACKLIGHT_PIN) || defined(BACKLIGHT_PINS))
|
#if defined(BACKLIGHT_ENABLE) && (defined(BACKLIGHT_PIN) || defined(BACKLIGHT_PINS))
|
||||||
|
|
||||||
// The logic is a bit complex, we support 3 setups:
|
// 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
|
// 1. Hardware PWM when backlight is wired to a PWM pin.
|
||||||
// 2. software PWM with hardware timers, but the used timer depends
|
// Depending on this pin, we use a different output compare unit.
|
||||||
// on the audio setup (audio wins other backlight)
|
// 2. Software PWM with hardware timers, but the used timer
|
||||||
// 3. full software PWM
|
// 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 BACKLIGHT_PIN == B7
|
#if (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) \
|
||||||
# define HARDWARE_PWM
|
|| defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) \
|
||||||
# define TCCRxA TCCR1A
|
|| defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) \
|
||||||
# define TCCRxB TCCR1B
|
&& (BACKLIGHT_PIN == B5 || BACKLIGHT_PIN == B6 || BACKLIGHT_PIN == B7)
|
||||||
# define COMxx1 COM1C1
|
#define HARDWARE_PWM
|
||||||
# define OCRxx OCR1C
|
#define ICRx ICR1
|
||||||
# define TIMERx_OVF_vect TIMER1_OVF_vect
|
#define TCCRxA TCCR1A
|
||||||
# define TOIEx TOIE1
|
#define TCCRxB TCCR1B
|
||||||
# define ICRx ICR1
|
#define TIMERx_OVF_vect TIMER1_OVF_vect
|
||||||
# define TIMSKx TIMSK1
|
#define TIMSKx TIMSK1
|
||||||
#elif BACKLIGHT_PIN == B6
|
#define TOIEx TOIE1
|
||||||
# define HARDWARE_PWM
|
|
||||||
# define TCCRxA TCCR1A
|
#if BACKLIGHT_PIN == B5
|
||||||
# define TCCRxB TCCR1B
|
#define COMxx1 COM1A1
|
||||||
# define COMxx1 COM1B1
|
#define OCRxx OCR1A
|
||||||
# define OCRxx OCR1B
|
#elif BACKLIGHT_PIN == B6
|
||||||
# define TIMERx_OVF_vect TIMER1_OVF_vect
|
#define COMxx1 COM1B1
|
||||||
# define TOIEx TOIE1
|
#define OCRxx OCR1B
|
||||||
# define ICRx ICR1
|
#elif BACKLIGHT_PIN == B7
|
||||||
# define TIMSKx TIMSK1
|
#define COMxx1 COM1C1
|
||||||
#elif BACKLIGHT_PIN == B5
|
#define OCRxx OCR1C
|
||||||
# define HARDWARE_PWM
|
#endif
|
||||||
# define TCCRxA TCCR1A
|
#elif (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) \
|
||||||
# define TCCRxB TCCR1B
|
|| defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) \
|
||||||
# define COMxx1 COM1A1
|
|| defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) \
|
||||||
# define OCRxx OCR1A
|
&& (BACKLIGHT_PIN == C4 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
|
||||||
# define TIMERx_OVF_vect TIMER1_OVF_vect
|
#define HARDWARE_PWM
|
||||||
# define TOIEx TOIE1
|
#define ICRx ICR3
|
||||||
# define ICRx ICR1
|
#define TCCRxA TCCR3A
|
||||||
# define TIMSKx TIMSK1
|
#define TCCRxB TCCR3B
|
||||||
#elif BACKLIGHT_PIN == C6
|
#define TIMERx_OVF_vect TIMER3_OVF_vect
|
||||||
# define HARDWARE_PWM
|
#define TIMSKx TIMSK3
|
||||||
# define TCCRxA TCCR3A
|
#define TOIEx TOIE3
|
||||||
# define TCCRxB TCCR3B
|
|
||||||
# define COMxx1 COM3A1
|
#if BACKLIGHT_PIN == C4
|
||||||
# define OCRxx OCR3A
|
#if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
|
||||||
# define TIMERx_OVF_vect TIMER3_OVF_vect
|
#error This MCU has no C4 pin!
|
||||||
# define TOIEx TOIE3
|
#else
|
||||||
# define ICRx ICR3
|
#define COMxx1 COM3C1
|
||||||
# define TIMSKx TIMSK3
|
#define OCRxx OCR3C
|
||||||
#elif defined(__AVR_ATmega32A__) && BACKLIGHT_PIN == D4
|
#endif
|
||||||
# define TCCRxA TCCR1A
|
#elif BACKLIGHT_PIN == C5
|
||||||
# define TCCRxB TCCR1B
|
#if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
|
||||||
# define COMxx1 COM1B1
|
#error This MCU has no C5 pin!
|
||||||
# define OCRxx OCR1B
|
#else
|
||||||
# define TIMERx_OVF_vect TIMER1_OVF_vect
|
#define COMxx1 COM3B1
|
||||||
# define TOIEx TOIE1
|
#define OCRxx OCR3B
|
||||||
# define ICRx ICR1
|
#endif
|
||||||
# define TIMSKx TIMSK1
|
#elif BACKLIGHT_PIN == C6
|
||||||
|
#define COMxx1 COM3A1
|
||||||
|
#define OCRxx OCR3A
|
||||||
|
#endif
|
||||||
|
#elif (defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__)) \
|
||||||
|
&& (BACKLIGHT_PIN == B7 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
|
||||||
|
#define HARDWARE_PWM
|
||||||
|
#define ICRx ICR1
|
||||||
|
#define TCCRxA TCCR1A
|
||||||
|
#define TCCRxB TCCR1B
|
||||||
|
#define TIMERx_OVF_vect TIMER1_OVF_vect
|
||||||
|
#define TIMSKx TIMSK1
|
||||||
|
#define TOIEx TOIE1
|
||||||
|
|
||||||
|
#if BACKLIGHT_PIN == B7
|
||||||
|
#define COMxx1 COM1C1
|
||||||
|
#define OCRxx OCR1C
|
||||||
|
#elif BACKLIGHT_PIN == C5
|
||||||
|
#define COMxx1 COM1B1
|
||||||
|
#define OCRxx OCR1B
|
||||||
|
#elif BACKLIGHT_PIN == C6
|
||||||
|
#define COMxx1 COM1A1
|
||||||
|
#define OCRxx OCR1A
|
||||||
|
#endif
|
||||||
|
#elif defined(__AVR_ATmega32A__) \
|
||||||
|
&& (BACKLIGHT_PIN == D4 || BACKLIGHT_PIN == D5)
|
||||||
|
#define HARDWARE_PWM
|
||||||
|
#define ICRx ICR1
|
||||||
|
#define TCCRxA TCCR1A
|
||||||
|
#define TCCRxB TCCR1B
|
||||||
|
#define TIMERx_OVF_vect TIMER1_OVF_vect
|
||||||
|
#define TIMSKx TIMSK
|
||||||
|
#define TOIEx TOIE1
|
||||||
|
|
||||||
|
#if BACKLIGHT_PIN == D4
|
||||||
|
#define COMxx1 COM1B1
|
||||||
|
#define OCRxx OCR1B
|
||||||
|
#elif BACKLIGHT_PIN == D5
|
||||||
|
#define COMxx1 COM1A1
|
||||||
|
#define OCRxx OCR1A
|
||||||
|
#endif
|
||||||
#else
|
#else
|
||||||
# if !defined(BACKLIGHT_CUSTOM_DRIVER)
|
#if !defined(BACKLIGHT_CUSTOM_DRIVER)
|
||||||
# if !defined(B5_AUDIO) && !defined(B6_AUDIO) && !defined(B7_AUDIO)
|
#if !defined(B5_AUDIO) && !defined(B6_AUDIO) && !defined(B7_AUDIO)
|
||||||
// timer 1 is not used by audio , backlight can use it
|
// Timer 1 is not in use by Audio feature, Backlight can use it
|
||||||
#pragma message "Using hardware timer 1 with software PWM"
|
#pragma message "Using hardware timer 1 with software PWM"
|
||||||
# define HARDWARE_PWM
|
#define HARDWARE_PWM
|
||||||
# define BACKLIGHT_PWM_TIMER
|
#define BACKLIGHT_PWM_TIMER
|
||||||
# define TCCRxA TCCR1A
|
#define ICRx ICR1
|
||||||
# define TCCRxB TCCR1B
|
#define TCCRxA TCCR1A
|
||||||
# define OCRxx OCR1A
|
#define TCCRxB TCCR1B
|
||||||
# define TIMERx_COMPA_vect TIMER1_COMPA_vect
|
#define TIMERx_COMPA_vect TIMER1_COMPA_vect
|
||||||
# define TIMERx_OVF_vect TIMER1_OVF_vect
|
#define TIMERx_OVF_vect TIMER1_OVF_vect
|
||||||
# define OCIExA OCIE1A
|
#if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
|
||||||
# define TOIEx TOIE1
|
#define TIMSKx TIMSK
|
||||||
# define ICRx ICR1
|
#else
|
||||||
# if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
|
#define TIMSKx TIMSK1
|
||||||
# define TIMSKx TIMSK
|
#endif
|
||||||
# else
|
#define TOIEx TOIE1
|
||||||
# define TIMSKx TIMSK1
|
|
||||||
# endif
|
#define OCIExA OCIE1A
|
||||||
# elif !defined(C6_AUDIO) && !defined(C5_AUDIO) && !defined(C4_AUDIO)
|
#define OCRxx OCR1A
|
||||||
#pragma message "Using hardware timer 3 with software PWM"
|
#elif !defined(C6_AUDIO) && !defined(C5_AUDIO) && !defined(C4_AUDIO)
|
||||||
// timer 3 is not used by audio, backlight can use it
|
#pragma message "Using hardware timer 3 with software PWM"
|
||||||
# define HARDWARE_PWM
|
// Timer 3 is not in use by Audio feature, Backlight can use it
|
||||||
# define BACKLIGHT_PWM_TIMER
|
#define HARDWARE_PWM
|
||||||
# define TCCRxA TCCR3A
|
#define BACKLIGHT_PWM_TIMER
|
||||||
# define TCCRxB TCCR3B
|
#define ICRx ICR1
|
||||||
# define OCRxx OCR3A
|
#define TCCRxA TCCR3A
|
||||||
# define TIMERx_COMPA_vect TIMER3_COMPA_vect
|
#define TCCRxB TCCR3B
|
||||||
# define TIMERx_OVF_vect TIMER3_OVF_vect
|
#define TIMERx_COMPA_vect TIMER3_COMPA_vect
|
||||||
# define OCIExA OCIE3A
|
#define TIMERx_OVF_vect TIMER3_OVF_vect
|
||||||
# define TOIEx TOIE3
|
#define TIMSKx TIMSK3
|
||||||
# define ICRx ICR1
|
#define TOIEx TOIE3
|
||||||
# define TIMSKx TIMSK3
|
|
||||||
# else
|
#define OCIExA OCIE3A
|
||||||
#pragma message "Audio in use - using pure software PWM"
|
#define OCRxx OCR3A
|
||||||
#define NO_HARDWARE_PWM
|
#else
|
||||||
# endif
|
#pragma message "Audio in use - using pure software PWM"
|
||||||
# else
|
#define NO_HARDWARE_PWM
|
||||||
#pragma message "Custom driver defined - using pure software PWM"
|
#endif
|
||||||
#define NO_HARDWARE_PWM
|
#else
|
||||||
# endif
|
#pragma message "Custom driver defined - using pure software PWM"
|
||||||
|
#define NO_HARDWARE_PWM
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef BACKLIGHT_ON_STATE
|
#ifndef BACKLIGHT_ON_STATE
|
||||||
|
@ -1300,7 +1343,7 @@ static uint16_t cie_lightness(uint16_t v) {
|
||||||
|
|
||||||
// range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
|
// range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
|
||||||
static inline void set_pwm(uint16_t val) {
|
static inline void set_pwm(uint16_t val) {
|
||||||
OCRxx = val;
|
OCRxx = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef BACKLIGHT_CUSTOM_DRIVER
|
#ifndef BACKLIGHT_CUSTOM_DRIVER
|
||||||
|
|
Loading…
Reference in New Issue