diff options
Diffstat (limited to 'quantum/backlight/backlight_avr.c')
-rw-r--r-- | quantum/backlight/backlight_avr.c | 58 |
1 files changed, 46 insertions, 12 deletions
diff --git a/quantum/backlight/backlight_avr.c b/quantum/backlight/backlight_avr.c index 7cf1e0fb30..519c0c2cf2 100644 --- a/quantum/backlight/backlight_avr.c +++ b/quantum/backlight/backlight_avr.c @@ -22,12 +22,15 @@ # define TOIEx TOIE1 # if BACKLIGHT_PIN == B5 +# define COMxx0 COM1A0 # define COMxx1 COM1A1 # define OCRxx OCR1A # elif BACKLIGHT_PIN == B6 +# define COMxx0 COM1B0 # define COMxx1 COM1B1 # define OCRxx OCR1B # elif BACKLIGHT_PIN == B7 +# define COMxx0 COM1C0 # define COMxx1 COM1C1 # define OCRxx OCR1C # endif @@ -44,6 +47,7 @@ # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) # error This MCU has no C4 pin! # else +# define COMxx0 COM3C0 # define COMxx1 COM3C1 # define OCRxx OCR3C # endif @@ -51,10 +55,12 @@ # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) # error This MCU has no C5 pin! # else +# define COMxx0 COM3B0 # define COMxx1 COM3B1 # define OCRxx OCR3B # endif # elif BACKLIGHT_PIN == C6 +# define COMxx0 COM3A0 # define COMxx1 COM3A1 # define OCRxx OCR3A # endif @@ -68,12 +74,15 @@ # define TOIEx TOIE1 # if BACKLIGHT_PIN == B7 +# define COMxx0 COM1C0 # define COMxx1 COM1C1 # define OCRxx OCR1C # elif BACKLIGHT_PIN == C5 +# define COMxx0 COM1B0 # define COMxx1 COM1B1 # define OCRxx OCR1B # elif BACKLIGHT_PIN == C6 +# define COMxx0 COM1A0 # define COMxx1 COM1A1 # define OCRxx OCR1A # endif @@ -87,9 +96,11 @@ # define TOIEx TOIE1 # if BACKLIGHT_PIN == D4 +# define COMxx0 COM1B0 # define COMxx1 COM1B1 # define OCRxx OCR1B # elif BACKLIGHT_PIN == D5 +# define COMxx0 COM1A0 # define COMxx1 COM1A1 # define OCRxx OCR1A # endif @@ -103,9 +114,11 @@ # define TOIEx TOIE1 # if BACKLIGHT_PIN == B1 +# define COMxx0 COM1A0 # define COMxx1 COM1A1 # define OCRxx OCR1A # elif BACKLIGHT_PIN == B2 +# define COMxx0 COM1B0 # define COMxx1 COM1B1 # define OCRxx OCR1B # endif @@ -156,22 +169,22 @@ # endif # ifndef BACKLIGHT_ON_STATE -# define BACKLIGHT_ON_STATE 0 +# define BACKLIGHT_ON_STATE 1 # endif void backlight_on(pin_t backlight_pin) { -# if BACKLIGHT_ON_STATE == 0 - writePinLow(backlight_pin); -# else +# if BACKLIGHT_ON_STATE == 1 writePinHigh(backlight_pin); +# else + writePinLow(backlight_pin); # endif } void backlight_off(pin_t backlight_pin) { -# if BACKLIGHT_ON_STATE == 0 - writePinHigh(backlight_pin); -# else +# if BACKLIGHT_ON_STATE == 1 writePinLow(backlight_pin); +# else + writePinHigh(backlight_pin); # endif } @@ -199,6 +212,22 @@ static const pin_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT; # else // full hardware PWM +static inline void enable_pwm(void) { +# if BACKLIGHT_ON_STATE == 1 + TCCRxA |= _BV(COMxx1); +# else + TCCRxA |= _BV(COMxx1) | _BV(COMxx0); +# endif +} + +static inline void disable_pwm(void) { +# if BACKLIGHT_ON_STATE == 1 + TCCRxA &= ~(_BV(COMxx1)); +# else + TCCRxA &= ~(_BV(COMxx1) | _BV(COMxx0)); +# endif +} + // we support only one backlight pin static const pin_t backlight_pin = BACKLIGHT_PIN; # define FOR_EACH_LED(x) x @@ -309,12 +338,12 @@ void backlight_set(uint8_t level) { if (OCRxx) { TIMSKx &= ~(_BV(OCIExA)); TIMSKx &= ~(_BV(TOIEx)); - FOR_EACH_LED(backlight_off(backlight_pin);) } # else // Turn off PWM control on backlight pin - TCCRxA &= ~(_BV(COMxx1)); + disable_pwm(); # endif + FOR_EACH_LED(backlight_off(backlight_pin);) } else { # ifdef BACKLIGHT_PWM_TIMER if (!OCRxx) { @@ -323,7 +352,7 @@ void backlight_set(uint8_t level) { } # else // Turn on PWM control of backlight pin - TCCRxA |= _BV(COMxx1); + enable_pwm(); # endif } // Set the brightness @@ -471,8 +500,13 @@ void backlight_init_ports(void) { "In fast PWM mode, the compare units allow generation of PWM waveforms on the OCnx pins. Setting the COMnx1:0 bits to two will produce a non-inverted PWM [..]." "In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGMn3:0 = 5, 6, or 7), the value in ICRn (WGMn3:0 = 14), or the value in OCRnA (WGMn3:0 = 15)." */ - TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010; - TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; +# if BACKLIGHT_ON_STATE == 1 + TCCRxA = _BV(COMxx1) | _BV(WGM11); +# else + TCCRxA = _BV(COMxx1) | _BV(COMxx0) | _BV(WGM11); +# endif + + TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); # endif // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0. ICRx = TIMER_TOP; |