aboutsummaryrefslogtreecommitdiff
path: root/quantum/backlight/backlight_avr.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/backlight/backlight_avr.c')
-rw-r--r--quantum/backlight/backlight_avr.c58
1 files changed, 46 insertions, 12 deletions
diff --git a/quantum/backlight/backlight_avr.c b/quantum/backlight/backlight_avr.c
index 7cf1e0fb3..519c0c2cf 100644
--- a/quantum/backlight/backlight_avr.c
+++ b/quantum/backlight/backlight_avr.c
@@ -22,12 +22,15 @@
22# define TOIEx TOIE1 22# define TOIEx TOIE1
23 23
24# if BACKLIGHT_PIN == B5 24# if BACKLIGHT_PIN == B5
25# define COMxx0 COM1A0
25# define COMxx1 COM1A1 26# define COMxx1 COM1A1
26# define OCRxx OCR1A 27# define OCRxx OCR1A
27# elif BACKLIGHT_PIN == B6 28# elif BACKLIGHT_PIN == B6
29# define COMxx0 COM1B0
28# define COMxx1 COM1B1 30# define COMxx1 COM1B1
29# define OCRxx OCR1B 31# define OCRxx OCR1B
30# elif BACKLIGHT_PIN == B7 32# elif BACKLIGHT_PIN == B7
33# define COMxx0 COM1C0
31# define COMxx1 COM1C1 34# define COMxx1 COM1C1
32# define OCRxx OCR1C 35# define OCRxx OCR1C
33# endif 36# endif
@@ -44,6 +47,7 @@
44# if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) 47# if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
45# error This MCU has no C4 pin! 48# error This MCU has no C4 pin!
46# else 49# else
50# define COMxx0 COM3C0
47# define COMxx1 COM3C1 51# define COMxx1 COM3C1
48# define OCRxx OCR3C 52# define OCRxx OCR3C
49# endif 53# endif
@@ -51,10 +55,12 @@
51# if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) 55# if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
52# error This MCU has no C5 pin! 56# error This MCU has no C5 pin!
53# else 57# else
58# define COMxx0 COM3B0
54# define COMxx1 COM3B1 59# define COMxx1 COM3B1
55# define OCRxx OCR3B 60# define OCRxx OCR3B
56# endif 61# endif
57# elif BACKLIGHT_PIN == C6 62# elif BACKLIGHT_PIN == C6
63# define COMxx0 COM3A0
58# define COMxx1 COM3A1 64# define COMxx1 COM3A1
59# define OCRxx OCR3A 65# define OCRxx OCR3A
60# endif 66# endif
@@ -68,12 +74,15 @@
68# define TOIEx TOIE1 74# define TOIEx TOIE1
69 75
70# if BACKLIGHT_PIN == B7 76# if BACKLIGHT_PIN == B7
77# define COMxx0 COM1C0
71# define COMxx1 COM1C1 78# define COMxx1 COM1C1
72# define OCRxx OCR1C 79# define OCRxx OCR1C
73# elif BACKLIGHT_PIN == C5 80# elif BACKLIGHT_PIN == C5
81# define COMxx0 COM1B0
74# define COMxx1 COM1B1 82# define COMxx1 COM1B1
75# define OCRxx OCR1B 83# define OCRxx OCR1B
76# elif BACKLIGHT_PIN == C6 84# elif BACKLIGHT_PIN == C6
85# define COMxx0 COM1A0
77# define COMxx1 COM1A1 86# define COMxx1 COM1A1
78# define OCRxx OCR1A 87# define OCRxx OCR1A
79# endif 88# endif
@@ -87,9 +96,11 @@
87# define TOIEx TOIE1 96# define TOIEx TOIE1
88 97
89# if BACKLIGHT_PIN == D4 98# if BACKLIGHT_PIN == D4
99# define COMxx0 COM1B0
90# define COMxx1 COM1B1 100# define COMxx1 COM1B1
91# define OCRxx OCR1B 101# define OCRxx OCR1B
92# elif BACKLIGHT_PIN == D5 102# elif BACKLIGHT_PIN == D5
103# define COMxx0 COM1A0
93# define COMxx1 COM1A1 104# define COMxx1 COM1A1
94# define OCRxx OCR1A 105# define OCRxx OCR1A
95# endif 106# endif
@@ -103,9 +114,11 @@
103# define TOIEx TOIE1 114# define TOIEx TOIE1
104 115
105# if BACKLIGHT_PIN == B1 116# if BACKLIGHT_PIN == B1
117# define COMxx0 COM1A0
106# define COMxx1 COM1A1 118# define COMxx1 COM1A1
107# define OCRxx OCR1A 119# define OCRxx OCR1A
108# elif BACKLIGHT_PIN == B2 120# elif BACKLIGHT_PIN == B2
121# define COMxx0 COM1B0
109# define COMxx1 COM1B1 122# define COMxx1 COM1B1
110# define OCRxx OCR1B 123# define OCRxx OCR1B
111# endif 124# endif
@@ -156,22 +169,22 @@
156# endif 169# endif
157 170
158# ifndef BACKLIGHT_ON_STATE 171# ifndef BACKLIGHT_ON_STATE
159# define BACKLIGHT_ON_STATE 0 172# define BACKLIGHT_ON_STATE 1
160# endif 173# endif
161 174
162void backlight_on(pin_t backlight_pin) { 175void backlight_on(pin_t backlight_pin) {
163# if BACKLIGHT_ON_STATE == 0 176# if BACKLIGHT_ON_STATE == 1
164 writePinLow(backlight_pin);
165# else
166 writePinHigh(backlight_pin); 177 writePinHigh(backlight_pin);
178# else
179 writePinLow(backlight_pin);
167# endif 180# endif
168} 181}
169 182
170void backlight_off(pin_t backlight_pin) { 183void backlight_off(pin_t backlight_pin) {
171# if BACKLIGHT_ON_STATE == 0 184# if BACKLIGHT_ON_STATE == 1
172 writePinHigh(backlight_pin);
173# else
174 writePinLow(backlight_pin); 185 writePinLow(backlight_pin);
186# else
187 writePinHigh(backlight_pin);
175# endif 188# endif
176} 189}
177 190
@@ -199,6 +212,22 @@ static const pin_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT;
199 212
200# else // full hardware PWM 213# else // full hardware PWM
201 214
215static inline void enable_pwm(void) {
216# if BACKLIGHT_ON_STATE == 1
217 TCCRxA |= _BV(COMxx1);
218# else
219 TCCRxA |= _BV(COMxx1) | _BV(COMxx0);
220# endif
221}
222
223static inline void disable_pwm(void) {
224# if BACKLIGHT_ON_STATE == 1
225 TCCRxA &= ~(_BV(COMxx1));
226# else
227 TCCRxA &= ~(_BV(COMxx1) | _BV(COMxx0));
228# endif
229}
230
202// we support only one backlight pin 231// we support only one backlight pin
203static const pin_t backlight_pin = BACKLIGHT_PIN; 232static const pin_t backlight_pin = BACKLIGHT_PIN;
204# define FOR_EACH_LED(x) x 233# define FOR_EACH_LED(x) x
@@ -309,12 +338,12 @@ void backlight_set(uint8_t level) {
309 if (OCRxx) { 338 if (OCRxx) {
310 TIMSKx &= ~(_BV(OCIExA)); 339 TIMSKx &= ~(_BV(OCIExA));
311 TIMSKx &= ~(_BV(TOIEx)); 340 TIMSKx &= ~(_BV(TOIEx));
312 FOR_EACH_LED(backlight_off(backlight_pin);)
313 } 341 }
314# else 342# else
315 // Turn off PWM control on backlight pin 343 // Turn off PWM control on backlight pin
316 TCCRxA &= ~(_BV(COMxx1)); 344 disable_pwm();
317# endif 345# endif
346 FOR_EACH_LED(backlight_off(backlight_pin);)
318 } else { 347 } else {
319# ifdef BACKLIGHT_PWM_TIMER 348# ifdef BACKLIGHT_PWM_TIMER
320 if (!OCRxx) { 349 if (!OCRxx) {
@@ -323,7 +352,7 @@ void backlight_set(uint8_t level) {
323 } 352 }
324# else 353# else
325 // Turn on PWM control of backlight pin 354 // Turn on PWM control of backlight pin
326 TCCRxA |= _BV(COMxx1); 355 enable_pwm();
327# endif 356# endif
328 } 357 }
329 // Set the brightness 358 // Set the brightness
@@ -471,8 +500,13 @@ void backlight_init_ports(void) {
471 "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 [..]." 500 "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 [..]."
472 "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)." 501 "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)."
473 */ 502 */
474 TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010; 503# if BACKLIGHT_ON_STATE == 1
475 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; 504 TCCRxA = _BV(COMxx1) | _BV(WGM11);
505# else
506 TCCRxA = _BV(COMxx1) | _BV(COMxx0) | _BV(WGM11);
507# endif
508
509 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
476# endif 510# endif
477 // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0. 511 // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
478 ICRx = TIMER_TOP; 512 ICRx = TIMER_TOP;