Prune out pure software pwm && custom driver && remove wrapping BACKLIGHT_PIN (#8041)
[jackhill/qmk/firmware.git] / quantum / backlight / backlight_avr.c
1 #include "quantum.h"
2 #include "backlight.h"
3 #include "debug.h"
4
5 #if !defined(BACKLIGHT_PIN) && !defined(BACKLIGHT_PINS)
6 # error "Backlight pin/pins not defined. Please configure."
7 #endif
8
9 // This logic is a bit complex, we support 3 setups:
10 //
11 // 1. Hardware PWM when backlight is wired to a PWM pin.
12 // Depending on this pin, we use a different output compare unit.
13 // 2. Software PWM with hardware timers, but the used timer
14 // depends on the Audio setup (Audio wins over Backlight).
15 // 3. Full software PWM, driven by the matrix scan, if both timers are used by Audio.
16
17 #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)
18 # define HARDWARE_PWM
19 # define ICRx ICR1
20 # define TCCRxA TCCR1A
21 # define TCCRxB TCCR1B
22 # define TIMERx_OVF_vect TIMER1_OVF_vect
23 # define TIMSKx TIMSK1
24 # define TOIEx TOIE1
25
26 # if BACKLIGHT_PIN == B5
27 # define COMxx0 COM1A0
28 # define COMxx1 COM1A1
29 # define OCRxx OCR1A
30 # elif BACKLIGHT_PIN == B6
31 # define COMxx0 COM1B0
32 # define COMxx1 COM1B1
33 # define OCRxx OCR1B
34 # elif BACKLIGHT_PIN == B7
35 # define COMxx0 COM1C0
36 # define COMxx1 COM1C1
37 # define OCRxx OCR1C
38 # endif
39 #elif (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) && (BACKLIGHT_PIN == C4 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
40 # define HARDWARE_PWM
41 # define ICRx ICR3
42 # define TCCRxA TCCR3A
43 # define TCCRxB TCCR3B
44 # define TIMERx_OVF_vect TIMER3_OVF_vect
45 # define TIMSKx TIMSK3
46 # define TOIEx TOIE3
47
48 # if BACKLIGHT_PIN == C4
49 # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
50 # error This MCU has no C4 pin!
51 # else
52 # define COMxx0 COM3C0
53 # define COMxx1 COM3C1
54 # define OCRxx OCR3C
55 # endif
56 # elif BACKLIGHT_PIN == C5
57 # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
58 # error This MCU has no C5 pin!
59 # else
60 # define COMxx0 COM3B0
61 # define COMxx1 COM3B1
62 # define OCRxx OCR3B
63 # endif
64 # elif BACKLIGHT_PIN == C6
65 # define COMxx0 COM3A0
66 # define COMxx1 COM3A1
67 # define OCRxx OCR3A
68 # endif
69 #elif (defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__)) && (BACKLIGHT_PIN == B7 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
70 # define HARDWARE_PWM
71 # define ICRx ICR1
72 # define TCCRxA TCCR1A
73 # define TCCRxB TCCR1B
74 # define TIMERx_OVF_vect TIMER1_OVF_vect
75 # define TIMSKx TIMSK1
76 # define TOIEx TOIE1
77
78 # if BACKLIGHT_PIN == B7
79 # define COMxx0 COM1C0
80 # define COMxx1 COM1C1
81 # define OCRxx OCR1C
82 # elif BACKLIGHT_PIN == C5
83 # define COMxx0 COM1B0
84 # define COMxx1 COM1B1
85 # define OCRxx OCR1B
86 # elif BACKLIGHT_PIN == C6
87 # define COMxx0 COM1A0
88 # define COMxx1 COM1A1
89 # define OCRxx OCR1A
90 # endif
91 #elif defined(__AVR_ATmega32A__) && (BACKLIGHT_PIN == D4 || BACKLIGHT_PIN == D5)
92 # define HARDWARE_PWM
93 # define ICRx ICR1
94 # define TCCRxA TCCR1A
95 # define TCCRxB TCCR1B
96 # define TIMERx_OVF_vect TIMER1_OVF_vect
97 # define TIMSKx TIMSK
98 # define TOIEx TOIE1
99
100 # if BACKLIGHT_PIN == D4
101 # define COMxx0 COM1B0
102 # define COMxx1 COM1B1
103 # define OCRxx OCR1B
104 # elif BACKLIGHT_PIN == D5
105 # define COMxx0 COM1A0
106 # define COMxx1 COM1A1
107 # define OCRxx OCR1A
108 # endif
109 #elif defined(__AVR_ATmega328P__) && (BACKLIGHT_PIN == B1 || BACKLIGHT_PIN == B2)
110 # define HARDWARE_PWM
111 # define ICRx ICR1
112 # define TCCRxA TCCR1A
113 # define TCCRxB TCCR1B
114 # define TIMERx_OVF_vect TIMER1_OVF_vect
115 # define TIMSKx TIMSK1
116 # define TOIEx TOIE1
117
118 # if BACKLIGHT_PIN == B1
119 # define COMxx0 COM1A0
120 # define COMxx1 COM1A1
121 # define OCRxx OCR1A
122 # elif BACKLIGHT_PIN == B2
123 # define COMxx0 COM1B0
124 # define COMxx1 COM1B1
125 # define OCRxx OCR1B
126 # endif
127 #elif !defined(B5_AUDIO) && !defined(B6_AUDIO) && !defined(B7_AUDIO)
128 // Timer 1 is not in use by Audio feature, Backlight can use it
129 # pragma message "Using hardware timer 1 with software PWM"
130 # define HARDWARE_PWM
131 # define BACKLIGHT_PWM_TIMER
132 # define ICRx ICR1
133 # define TCCRxA TCCR1A
134 # define TCCRxB TCCR1B
135 # define TIMERx_COMPA_vect TIMER1_COMPA_vect
136 # define TIMERx_OVF_vect TIMER1_OVF_vect
137 # if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
138 # define TIMSKx TIMSK
139 # else
140 # define TIMSKx TIMSK1
141 # endif
142 # define TOIEx TOIE1
143
144 # define OCIExA OCIE1A
145 # define OCRxx OCR1A
146 #elif !defined(C6_AUDIO) && !defined(C5_AUDIO) && !defined(C4_AUDIO)
147 # pragma message "Using hardware timer 3 with software PWM"
148 // Timer 3 is not in use by Audio feature, Backlight can use it
149 # define HARDWARE_PWM
150 # define BACKLIGHT_PWM_TIMER
151 # define ICRx ICR1
152 # define TCCRxA TCCR3A
153 # define TCCRxB TCCR3B
154 # define TIMERx_COMPA_vect TIMER3_COMPA_vect
155 # define TIMERx_OVF_vect TIMER3_OVF_vect
156 # define TIMSKx TIMSK3
157 # define TOIEx TOIE3
158
159 # define OCIExA OCIE3A
160 # define OCRxx OCR3A
161 #elif defined(BACKLIGHT_CUSTOM_DRIVER)
162 error("Please set 'BACKLIGHT_DRIVER = custom' within rules.mk")
163 #else
164 error("Please set 'BACKLIGHT_DRIVER = software' within rules.mk")
165 #endif
166
167 #ifndef BACKLIGHT_ON_STATE
168 # define BACKLIGHT_ON_STATE 1
169 #endif
170
171 void backlight_on(pin_t backlight_pin) {
172 #if BACKLIGHT_ON_STATE == 1
173 writePinHigh(backlight_pin);
174 #else
175 writePinLow(backlight_pin);
176 #endif
177 }
178
179 void backlight_off(pin_t backlight_pin) {
180 #if BACKLIGHT_ON_STATE == 1
181 writePinLow(backlight_pin);
182 #else
183 writePinHigh(backlight_pin);
184 #endif
185 }
186
187 #ifdef BACKLIGHT_PWM_TIMER // pwm through software
188
189 // we support multiple backlight pins
190 # ifndef BACKLIGHT_LED_COUNT
191 # define BACKLIGHT_LED_COUNT 1
192 # endif
193
194 # if BACKLIGHT_LED_COUNT == 1
195 # define BACKLIGHT_PIN_INIT \
196 { BACKLIGHT_PIN }
197 # else
198 # define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
199 # endif
200
201 # define FOR_EACH_LED(x) \
202 for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \
203 pin_t backlight_pin = backlight_pins[i]; \
204 { x } \
205 }
206
207 static const pin_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT;
208
209 #else // full hardware PWM
210
211 static inline void enable_pwm(void) {
212 # if BACKLIGHT_ON_STATE == 1
213 TCCRxA |= _BV(COMxx1);
214 # else
215 TCCRxA |= _BV(COMxx1) | _BV(COMxx0);
216 # endif
217 }
218
219 static inline void disable_pwm(void) {
220 # if BACKLIGHT_ON_STATE == 1
221 TCCRxA &= ~(_BV(COMxx1));
222 # else
223 TCCRxA &= ~(_BV(COMxx1) | _BV(COMxx0));
224 # endif
225 }
226
227 // we support only one backlight pin
228 static const pin_t backlight_pin = BACKLIGHT_PIN;
229 # define FOR_EACH_LED(x) x
230
231 #endif
232
233 #ifdef BACKLIGHT_PWM_TIMER
234
235 // The idea of software PWM assisted by hardware timers is the following
236 // we use the hardware timer in fast PWM mode like for hardware PWM, but
237 // instead of letting the Output Match Comparator control the led pin
238 // (which is not possible since the backlight is not wired to PWM pins on the
239 // CPU), we do the LED on/off by oursleves.
240 // The timer is setup to count up to 0xFFFF, and we set the Output Compare
241 // register to the current 16bits backlight level (after CIE correction).
242 // This means the CPU will trigger a compare match interrupt when the counter
243 // reaches the backlight level, where we turn off the LEDs,
244 // but also an overflow interrupt when the counter rolls back to 0,
245 // in which we're going to turn on the LEDs.
246 // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz.
247
248 // Triggered when the counter reaches the OCRx value
249 ISR(TIMERx_COMPA_vect) { FOR_EACH_LED(backlight_off(backlight_pin);) }
250
251 // Triggered when the counter reaches the TOP value
252 // this one triggers at F_CPU/65536 =~ 244 Hz
253 ISR(TIMERx_OVF_vect) {
254 # ifdef BACKLIGHT_BREATHING
255 if (is_breathing()) {
256 breathing_task();
257 }
258 # endif
259 // for very small values of OCRxx (or backlight level)
260 // we can't guarantee this whole code won't execute
261 // at the same time as the compare match interrupt
262 // which means that we might turn on the leds while
263 // trying to turn them off, leading to flickering
264 // artifacts (especially while breathing, because breathing_task
265 // takes many computation cycles).
266 // so better not turn them on while the counter TOP is very low.
267 if (OCRxx > 256) {
268 FOR_EACH_LED(backlight_on(backlight_pin);)
269 }
270 }
271
272 #endif
273
274 #define TIMER_TOP 0xFFFFU
275
276 // See http://jared.geek.nz/2013/feb/linear-led-pwm
277 static uint16_t cie_lightness(uint16_t v) {
278 if (v <= 5243) // if below 8% of max
279 return v / 9; // same as dividing by 900%
280 else {
281 uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
282 // to get a useful result with integer division, we shift left in the expression above
283 // and revert what we've done again after squaring.
284 y = y * y * y >> 8;
285 if (y > 0xFFFFUL) // prevent overflow
286 return 0xFFFFU;
287 else
288 return (uint16_t)y;
289 }
290 }
291
292 // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
293 static inline void set_pwm(uint16_t val) { OCRxx = val; }
294
295 void backlight_set(uint8_t level) {
296 if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
297
298 if (level == 0) {
299 #ifdef BACKLIGHT_PWM_TIMER
300 if (OCRxx) {
301 TIMSKx &= ~(_BV(OCIExA));
302 TIMSKx &= ~(_BV(TOIEx));
303 }
304 #else
305 // Turn off PWM control on backlight pin
306 disable_pwm();
307 #endif
308 FOR_EACH_LED(backlight_off(backlight_pin);)
309 } else {
310 #ifdef BACKLIGHT_PWM_TIMER
311 if (!OCRxx) {
312 TIMSKx |= _BV(OCIExA);
313 TIMSKx |= _BV(TOIEx);
314 }
315 #else
316 // Turn on PWM control of backlight pin
317 enable_pwm();
318 #endif
319 }
320 // Set the brightness
321 set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
322 }
323
324 void backlight_task(void) {}
325
326 #ifdef BACKLIGHT_BREATHING
327
328 # define BREATHING_NO_HALT 0
329 # define BREATHING_HALT_OFF 1
330 # define BREATHING_HALT_ON 2
331 # define BREATHING_STEPS 128
332
333 static uint8_t breathing_halt = BREATHING_NO_HALT;
334 static uint16_t breathing_counter = 0;
335
336 # ifdef BACKLIGHT_PWM_TIMER
337 static bool breathing = false;
338
339 bool is_breathing(void) { return breathing; }
340
341 # define breathing_interrupt_enable() \
342 do { \
343 breathing = true; \
344 } while (0)
345 # define breathing_interrupt_disable() \
346 do { \
347 breathing = false; \
348 } while (0)
349 # else
350
351 bool is_breathing(void) { return !!(TIMSKx & _BV(TOIEx)); }
352
353 # define breathing_interrupt_enable() \
354 do { \
355 TIMSKx |= _BV(TOIEx); \
356 } while (0)
357 # define breathing_interrupt_disable() \
358 do { \
359 TIMSKx &= ~_BV(TOIEx); \
360 } while (0)
361 # endif
362
363 # define breathing_min() \
364 do { \
365 breathing_counter = 0; \
366 } while (0)
367 # define breathing_max() \
368 do { \
369 breathing_counter = get_breathing_period() * 244 / 2; \
370 } while (0)
371
372 void breathing_enable(void) {
373 breathing_counter = 0;
374 breathing_halt = BREATHING_NO_HALT;
375 breathing_interrupt_enable();
376 }
377
378 void breathing_pulse(void) {
379 if (get_backlight_level() == 0)
380 breathing_min();
381 else
382 breathing_max();
383 breathing_halt = BREATHING_HALT_ON;
384 breathing_interrupt_enable();
385 }
386
387 void breathing_disable(void) {
388 breathing_interrupt_disable();
389 // Restore backlight level
390 backlight_set(get_backlight_level());
391 }
392
393 void breathing_self_disable(void) {
394 if (get_backlight_level() == 0)
395 breathing_halt = BREATHING_HALT_OFF;
396 else
397 breathing_halt = BREATHING_HALT_ON;
398 }
399
400 void breathing_toggle(void) {
401 if (is_breathing())
402 breathing_disable();
403 else
404 breathing_enable();
405 }
406
407 /* To generate breathing curve in python:
408 * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
409 */
410 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};
411
412 // Use this before the cie_lightness function.
413 static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); }
414
415 # ifdef BACKLIGHT_PWM_TIMER
416 void breathing_task(void)
417 # else
418 /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
419 * about 244 times per second.
420 */
421 ISR(TIMERx_OVF_vect)
422 # endif
423 {
424 uint8_t breathing_period = get_breathing_period();
425 uint16_t interval = (uint16_t)breathing_period * 244 / BREATHING_STEPS;
426 // resetting after one period to prevent ugly reset at overflow.
427 breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
428 uint8_t index = breathing_counter / interval % BREATHING_STEPS;
429
430 if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) {
431 breathing_interrupt_disable();
432 }
433
434 set_pwm(cie_lightness(scale_backlight((uint16_t)pgm_read_byte(&breathing_table[index]) * 0x0101U)));
435 }
436
437 #endif // BACKLIGHT_BREATHING
438
439 void backlight_init_ports(void) {
440 // Setup backlight pin as output and output to on state.
441 FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);)
442
443 // I could write a wall of text here to explain... but TL;DW
444 // Go read the ATmega32u4 datasheet.
445 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
446
447 #ifdef BACKLIGHT_PWM_TIMER
448 // TimerX setup, Fast PWM mode count to TOP set in ICRx
449 TCCRxA = _BV(WGM11); // = 0b00000010;
450 // clock select clk/1
451 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
452 #else // hardware PWM
453 // Pin PB7 = OCR1C (Timer 1, Channel C)
454 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
455 // (i.e. start high, go low when counter matches.)
456 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
457 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
458
459 /*
460 14.8.3:
461 "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 [..]."
462 "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)."
463 */
464 # if BACKLIGHT_ON_STATE == 1
465 TCCRxA = _BV(COMxx1) | _BV(WGM11);
466 # else
467 TCCRxA = _BV(COMxx1) | _BV(COMxx0) | _BV(WGM11);
468 # endif
469
470 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
471 #endif
472 // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
473 ICRx = TIMER_TOP;
474
475 backlight_init();
476 #ifdef BACKLIGHT_BREATHING
477 if (is_backlight_breathing()) {
478 breathing_enable();
479 }
480 #endif
481 }