5 #if defined(BACKLIGHT_ENABLE) && (defined(BACKLIGHT_PIN) || defined(BACKLIGHT_PINS))
7 // This logic is a bit complex, we support 3 setups:
9 // 1. Hardware PWM when backlight is wired to a PWM pin.
10 // Depending on this pin, we use a different output compare unit.
11 // 2. Software PWM with hardware timers, but the used timer
12 // depends on the Audio setup (Audio wins over Backlight).
13 // 3. Full software PWM, driven by the matrix scan, if both timers are used by Audio.
15 # 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 TCCRxA TCCR1A
19 # define TCCRxB TCCR1B
20 # define TIMERx_OVF_vect TIMER1_OVF_vect
21 # define TIMSKx TIMSK1
24 # if BACKLIGHT_PIN == B5
25 # define COMxx1 COM1A1
27 # elif BACKLIGHT_PIN == B6
28 # define COMxx1 COM1B1
30 # elif BACKLIGHT_PIN == B7
31 # define COMxx1 COM1C1
34 # 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)
37 # define TCCRxA TCCR3A
38 # define TCCRxB TCCR3B
39 # define TIMERx_OVF_vect TIMER3_OVF_vect
40 # define TIMSKx TIMSK3
43 # if BACKLIGHT_PIN == C4
44 # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
45 # error This MCU has no C4 pin!
47 # define COMxx1 COM3C1
50 # elif BACKLIGHT_PIN == C5
51 # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
52 # error This MCU has no C5 pin!
54 # define COMxx1 COM3B1
57 # elif BACKLIGHT_PIN == C6
58 # define COMxx1 COM3A1
61 # elif (defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__)) && (BACKLIGHT_PIN == B7 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
64 # define TCCRxA TCCR1A
65 # define TCCRxB TCCR1B
66 # define TIMERx_OVF_vect TIMER1_OVF_vect
67 # define TIMSKx TIMSK1
70 # if BACKLIGHT_PIN == B7
71 # define COMxx1 COM1C1
73 # elif BACKLIGHT_PIN == C5
74 # define COMxx1 COM1B1
76 # elif BACKLIGHT_PIN == C6
77 # define COMxx1 COM1A1
80 # elif defined(__AVR_ATmega32A__) && (BACKLIGHT_PIN == D4 || BACKLIGHT_PIN == D5)
83 # define TCCRxA TCCR1A
84 # define TCCRxB TCCR1B
85 # define TIMERx_OVF_vect TIMER1_OVF_vect
89 # if BACKLIGHT_PIN == D4
90 # define COMxx1 COM1B1
92 # elif BACKLIGHT_PIN == D5
93 # define COMxx1 COM1A1
96 # elif defined(__AVR_ATmega328P__) && (BACKLIGHT_PIN == B1 || BACKLIGHT_PIN == B2)
99 # define TCCRxA TCCR1A
100 # define TCCRxB TCCR1B
101 # define TIMERx_OVF_vect TIMER1_OVF_vect
102 # define TIMSKx TIMSK1
105 # if BACKLIGHT_PIN == B1
106 # define COMxx1 COM1A1
108 # elif BACKLIGHT_PIN == B2
109 # define COMxx1 COM1B1
113 # if !defined(BACKLIGHT_CUSTOM_DRIVER)
114 # if !defined(B5_AUDIO) && !defined(B6_AUDIO) && !defined(B7_AUDIO)
115 // Timer 1 is not in use by Audio feature, Backlight can use it
116 # pragma message "Using hardware timer 1 with software PWM"
117 # define HARDWARE_PWM
118 # define BACKLIGHT_PWM_TIMER
120 # define TCCRxA TCCR1A
121 # define TCCRxB TCCR1B
122 # define TIMERx_COMPA_vect TIMER1_COMPA_vect
123 # define TIMERx_OVF_vect TIMER1_OVF_vect
124 # if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
125 # define TIMSKx TIMSK
127 # define TIMSKx TIMSK1
131 # define OCIExA OCIE1A
133 # elif !defined(C6_AUDIO) && !defined(C5_AUDIO) && !defined(C4_AUDIO)
134 # pragma message "Using hardware timer 3 with software PWM"
135 // Timer 3 is not in use by Audio feature, Backlight can use it
136 # define HARDWARE_PWM
137 # define BACKLIGHT_PWM_TIMER
139 # define TCCRxA TCCR3A
140 # define TCCRxB TCCR3B
141 # define TIMERx_COMPA_vect TIMER3_COMPA_vect
142 # define TIMERx_OVF_vect TIMER3_OVF_vect
143 # define TIMSKx TIMSK3
146 # define OCIExA OCIE3A
149 # pragma message "Audio in use - using pure software PWM"
150 # define NO_HARDWARE_PWM
153 # pragma message "Custom driver defined - using pure software PWM"
154 # define NO_HARDWARE_PWM
158 # ifndef BACKLIGHT_ON_STATE
159 # define BACKLIGHT_ON_STATE 0
162 void backlight_on(uint8_t backlight_pin) {
163 # if BACKLIGHT_ON_STATE == 0
164 writePinLow(backlight_pin);
166 writePinHigh(backlight_pin);
170 void backlight_off(uint8_t backlight_pin) {
171 # if BACKLIGHT_ON_STATE == 0
172 writePinHigh(backlight_pin);
174 writePinLow(backlight_pin);
178 # if defined(NO_HARDWARE_PWM) || defined(BACKLIGHT_PWM_TIMER) // pwm through software
180 // we support multiple backlight pins
181 # ifndef BACKLIGHT_LED_COUNT
182 # define BACKLIGHT_LED_COUNT 1
185 # if BACKLIGHT_LED_COUNT == 1
186 # define BACKLIGHT_PIN_INIT \
189 # define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
192 # define FOR_EACH_LED(x) \
193 for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \
194 uint8_t backlight_pin = backlight_pins[i]; \
198 static const uint8_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT;
200 # else // full hardware PWM
202 // we support only one backlight pin
203 static const uint8_t backlight_pin = BACKLIGHT_PIN;
204 # define FOR_EACH_LED(x) x
208 # ifdef NO_HARDWARE_PWM
209 __attribute__((weak)) void backlight_init_ports(void) {
210 // Setup backlight pin as output and output to on state.
211 FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);)
213 # ifdef BACKLIGHT_BREATHING
214 if (is_backlight_breathing()) {
220 __attribute__((weak)) void backlight_set(uint8_t level) {}
222 uint8_t backlight_tick = 0;
224 # ifndef BACKLIGHT_CUSTOM_DRIVER
225 void backlight_task(void) {
226 if ((0xFFFF >> ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
227 FOR_EACH_LED(backlight_on(backlight_pin);)
229 FOR_EACH_LED(backlight_off(backlight_pin);)
231 backlight_tick = (backlight_tick + 1) % 16;
235 # ifdef BACKLIGHT_BREATHING
236 # ifndef BACKLIGHT_CUSTOM_DRIVER
237 # error "Backlight breathing only available with hardware PWM. Please disable."
241 # else // hardware pwm through timer
243 # ifdef BACKLIGHT_PWM_TIMER
245 // The idea of software PWM assisted by hardware timers is the following
246 // we use the hardware timer in fast PWM mode like for hardware PWM, but
247 // instead of letting the Output Match Comparator control the led pin
248 // (which is not possible since the backlight is not wired to PWM pins on the
249 // CPU), we do the LED on/off by oursleves.
250 // The timer is setup to count up to 0xFFFF, and we set the Output Compare
251 // register to the current 16bits backlight level (after CIE correction).
252 // This means the CPU will trigger a compare match interrupt when the counter
253 // reaches the backlight level, where we turn off the LEDs,
254 // but also an overflow interrupt when the counter rolls back to 0,
255 // in which we're going to turn on the LEDs.
256 // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz.
258 // Triggered when the counter reaches the OCRx value
259 ISR(TIMERx_COMPA_vect) { FOR_EACH_LED(backlight_off(backlight_pin);) }
261 // Triggered when the counter reaches the TOP value
262 // this one triggers at F_CPU/65536 =~ 244 Hz
263 ISR(TIMERx_OVF_vect) {
264 # ifdef BACKLIGHT_BREATHING
265 if (is_breathing()) {
269 // for very small values of OCRxx (or backlight level)
270 // we can't guarantee this whole code won't execute
271 // at the same time as the compare match interrupt
272 // which means that we might turn on the leds while
273 // trying to turn them off, leading to flickering
274 // artifacts (especially while breathing, because breathing_task
275 // takes many computation cycles).
276 // so better not turn them on while the counter TOP is very low.
278 FOR_EACH_LED(backlight_on(backlight_pin);)
284 # define TIMER_TOP 0xFFFFU
286 // See http://jared.geek.nz/2013/feb/linear-led-pwm
287 static uint16_t cie_lightness(uint16_t v) {
288 if (v <= 5243) // if below 8% of max
289 return v / 9; // same as dividing by 900%
291 uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
292 // to get a useful result with integer division, we shift left in the expression above
293 // and revert what we've done again after squaring.
295 if (y > 0xFFFFUL) // prevent overflow
302 // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
303 static inline void set_pwm(uint16_t val) { OCRxx = val; }
305 # ifndef BACKLIGHT_CUSTOM_DRIVER
306 __attribute__((weak)) void backlight_set(uint8_t level) {
307 if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
310 # ifdef BACKLIGHT_PWM_TIMER
312 TIMSKx &= ~(_BV(OCIExA));
313 TIMSKx &= ~(_BV(TOIEx));
314 FOR_EACH_LED(backlight_off(backlight_pin);)
317 // Turn off PWM control on backlight pin
318 TCCRxA &= ~(_BV(COMxx1));
321 # ifdef BACKLIGHT_PWM_TIMER
323 TIMSKx |= _BV(OCIExA);
324 TIMSKx |= _BV(TOIEx);
327 // Turn on PWM control of backlight pin
328 TCCRxA |= _BV(COMxx1);
331 // Set the brightness
332 set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
335 void backlight_task(void) {}
336 # endif // BACKLIGHT_CUSTOM_DRIVER
338 # ifdef BACKLIGHT_BREATHING
340 # define BREATHING_NO_HALT 0
341 # define BREATHING_HALT_OFF 1
342 # define BREATHING_HALT_ON 2
343 # define BREATHING_STEPS 128
345 static uint8_t breathing_period = BREATHING_PERIOD;
346 static uint8_t breathing_halt = BREATHING_NO_HALT;
347 static uint16_t breathing_counter = 0;
349 # ifdef BACKLIGHT_PWM_TIMER
350 static bool breathing = false;
352 bool is_breathing(void) { return breathing; }
354 # define breathing_interrupt_enable() \
358 # define breathing_interrupt_disable() \
364 bool is_breathing(void) { return !!(TIMSKx & _BV(TOIEx)); }
366 # define breathing_interrupt_enable() \
368 TIMSKx |= _BV(TOIEx); \
370 # define breathing_interrupt_disable() \
372 TIMSKx &= ~_BV(TOIEx); \
376 # define breathing_min() \
378 breathing_counter = 0; \
380 # define breathing_max() \
382 breathing_counter = breathing_period * 244 / 2; \
385 void breathing_enable(void) {
386 breathing_counter = 0;
387 breathing_halt = BREATHING_NO_HALT;
388 breathing_interrupt_enable();
391 void breathing_pulse(void) {
392 if (get_backlight_level() == 0)
396 breathing_halt = BREATHING_HALT_ON;
397 breathing_interrupt_enable();
400 void breathing_disable(void) {
401 breathing_interrupt_disable();
402 // Restore backlight level
403 backlight_set(get_backlight_level());
406 void breathing_self_disable(void) {
407 if (get_backlight_level() == 0)
408 breathing_halt = BREATHING_HALT_OFF;
410 breathing_halt = BREATHING_HALT_ON;
413 void breathing_toggle(void) {
420 void breathing_period_set(uint8_t value) {
421 if (!value) value = 1;
422 breathing_period = value;
425 void breathing_period_default(void) { breathing_period_set(BREATHING_PERIOD); }
427 void breathing_period_inc(void) { breathing_period_set(breathing_period + 1); }
429 void breathing_period_dec(void) { breathing_period_set(breathing_period - 1); }
431 /* To generate breathing curve in python:
432 * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
434 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};
436 // Use this before the cie_lightness function.
437 static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); }
439 # ifdef BACKLIGHT_PWM_TIMER
440 void breathing_task(void)
442 /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
443 * about 244 times per second.
448 uint16_t interval = (uint16_t)breathing_period * 244 / BREATHING_STEPS;
449 // resetting after one period to prevent ugly reset at overflow.
450 breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
451 uint8_t index = breathing_counter / interval % BREATHING_STEPS;
453 if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) {
454 breathing_interrupt_disable();
457 set_pwm(cie_lightness(scale_backlight((uint16_t)pgm_read_byte(&breathing_table[index]) * 0x0101U)));
460 # endif // BACKLIGHT_BREATHING
462 __attribute__((weak)) void backlight_init_ports(void) {
463 // Setup backlight pin as output and output to on state.
464 FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);)
466 // I could write a wall of text here to explain... but TL;DW
467 // Go read the ATmega32u4 datasheet.
468 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
470 # ifdef BACKLIGHT_PWM_TIMER
471 // TimerX setup, Fast PWM mode count to TOP set in ICRx
472 TCCRxA = _BV(WGM11); // = 0b00000010;
473 // clock select clk/1
474 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
475 # else // hardware PWM
476 // Pin PB7 = OCR1C (Timer 1, Channel C)
477 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
478 // (i.e. start high, go low when counter matches.)
479 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
480 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
484 "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 [..]."
485 "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)."
487 TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
488 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
490 // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
494 # ifdef BACKLIGHT_BREATHING
495 if (is_backlight_breathing()) {
501 # endif // hardware backlight
503 #else // no backlight
505 __attribute__((weak)) void backlight_init_ports(void) {}
507 __attribute__((weak)) void backlight_set(uint8_t level) {}