5 #if !defined(BACKLIGHT_PIN) && !defined(BACKLIGHT_PINS)
6 # error "Backlight pin/pins not defined. Please configure."
9 // This logic is a bit complex, we support 3 setups:
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.
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)
20 # define TCCRxA TCCR1A
21 # define TCCRxB TCCR1B
22 # define TIMERx_OVF_vect TIMER1_OVF_vect
23 # define TIMSKx TIMSK1
26 # if BACKLIGHT_PIN == B5
27 # define COMxx0 COM1A0
28 # define COMxx1 COM1A1
30 # elif BACKLIGHT_PIN == B6
31 # define COMxx0 COM1B0
32 # define COMxx1 COM1B1
34 # elif BACKLIGHT_PIN == B7
35 # define COMxx0 COM1C0
36 # define COMxx1 COM1C1
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)
42 # define TCCRxA TCCR3A
43 # define TCCRxB TCCR3B
44 # define TIMERx_OVF_vect TIMER3_OVF_vect
45 # define TIMSKx TIMSK3
48 # if BACKLIGHT_PIN == C4
49 # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
50 # error This MCU has no C4 pin!
52 # define COMxx0 COM3C0
53 # define COMxx1 COM3C1
56 # elif BACKLIGHT_PIN == C5
57 # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
58 # error This MCU has no C5 pin!
60 # define COMxx0 COM3B0
61 # define COMxx1 COM3B1
64 # elif BACKLIGHT_PIN == C6
65 # define COMxx0 COM3A0
66 # define COMxx1 COM3A1
69 #elif (defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__)) && (BACKLIGHT_PIN == B7 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
72 # define TCCRxA TCCR1A
73 # define TCCRxB TCCR1B
74 # define TIMERx_OVF_vect TIMER1_OVF_vect
75 # define TIMSKx TIMSK1
78 # if BACKLIGHT_PIN == B7
79 # define COMxx0 COM1C0
80 # define COMxx1 COM1C1
82 # elif BACKLIGHT_PIN == C5
83 # define COMxx0 COM1B0
84 # define COMxx1 COM1B1
86 # elif BACKLIGHT_PIN == C6
87 # define COMxx0 COM1A0
88 # define COMxx1 COM1A1
91 #elif defined(__AVR_ATmega32A__) && (BACKLIGHT_PIN == D4 || BACKLIGHT_PIN == D5)
94 # define TCCRxA TCCR1A
95 # define TCCRxB TCCR1B
96 # define TIMERx_OVF_vect TIMER1_OVF_vect
100 # if BACKLIGHT_PIN == D4
101 # define COMxx0 COM1B0
102 # define COMxx1 COM1B1
104 # elif BACKLIGHT_PIN == D5
105 # define COMxx0 COM1A0
106 # define COMxx1 COM1A1
109 #elif defined(__AVR_ATmega328P__) && (BACKLIGHT_PIN == B1 || BACKLIGHT_PIN == B2)
110 # define HARDWARE_PWM
112 # define TCCRxA TCCR1A
113 # define TCCRxB TCCR1B
114 # define TIMERx_OVF_vect TIMER1_OVF_vect
115 # define TIMSKx TIMSK1
118 # if BACKLIGHT_PIN == B1
119 # define COMxx0 COM1A0
120 # define COMxx1 COM1A1
122 # elif BACKLIGHT_PIN == B2
123 # define COMxx0 COM1B0
124 # define COMxx1 COM1B1
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
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
140 # define TIMSKx TIMSK1
144 # define OCIExA OCIE1A
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
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
159 # define OCIExA OCIE3A
161 #elif defined(BACKLIGHT_CUSTOM_DRIVER)
162 error("Please set 'BACKLIGHT_DRIVER = custom' within rules.mk")
164 error("Please set 'BACKLIGHT_DRIVER = software' within rules.mk")
167 #ifndef BACKLIGHT_ON_STATE
168 # define BACKLIGHT_ON_STATE 1
171 void backlight_on(pin_t backlight_pin
) {
172 #if BACKLIGHT_ON_STATE == 1
173 writePinHigh(backlight_pin
);
175 writePinLow(backlight_pin
);
179 void backlight_off(pin_t backlight_pin
) {
180 #if BACKLIGHT_ON_STATE == 1
181 writePinLow(backlight_pin
);
183 writePinHigh(backlight_pin
);
187 #ifdef BACKLIGHT_PWM_TIMER // pwm through software
189 // we support multiple backlight pins
190 # ifndef BACKLIGHT_LED_COUNT
191 # define BACKLIGHT_LED_COUNT 1
194 # if BACKLIGHT_LED_COUNT == 1
195 # define BACKLIGHT_PIN_INIT \
198 # define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
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]; \
207 static const pin_t backlight_pins
[BACKLIGHT_LED_COUNT
] = BACKLIGHT_PIN_INIT
;
209 #else // full hardware PWM
211 static inline void enable_pwm(void) {
212 # if BACKLIGHT_ON_STATE == 1
213 TCCRxA
|= _BV(COMxx1
);
215 TCCRxA
|= _BV(COMxx1
) | _BV(COMxx0
);
219 static inline void disable_pwm(void) {
220 # if BACKLIGHT_ON_STATE == 1
221 TCCRxA
&= ~(_BV(COMxx1
));
223 TCCRxA
&= ~(_BV(COMxx1
) | _BV(COMxx0
));
227 // we support only one backlight pin
228 static const pin_t backlight_pin
= BACKLIGHT_PIN
;
229 # define FOR_EACH_LED(x) x
233 #ifdef BACKLIGHT_PWM_TIMER
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.
248 // Triggered when the counter reaches the OCRx value
249 ISR(TIMERx_COMPA_vect
) { FOR_EACH_LED(backlight_off(backlight_pin
);) }
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()) {
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.
268 FOR_EACH_LED(backlight_on(backlight_pin
);)
274 #define TIMER_TOP 0xFFFFU
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%
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.
285 if (y
> 0xFFFFUL
) // prevent overflow
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
; }
295 void backlight_set(uint8_t level
) {
296 if (level
> BACKLIGHT_LEVELS
) level
= BACKLIGHT_LEVELS
;
299 #ifdef BACKLIGHT_PWM_TIMER
301 TIMSKx
&= ~(_BV(OCIExA
));
302 TIMSKx
&= ~(_BV(TOIEx
));
305 // Turn off PWM control on backlight pin
308 FOR_EACH_LED(backlight_off(backlight_pin
);)
310 #ifdef BACKLIGHT_PWM_TIMER
312 TIMSKx
|= _BV(OCIExA
);
313 TIMSKx
|= _BV(TOIEx
);
316 // Turn on PWM control of backlight pin
320 // Set the brightness
321 set_pwm(cie_lightness(TIMER_TOP
* (uint32_t)level
/ BACKLIGHT_LEVELS
));
324 void backlight_task(void) {}
326 #ifdef BACKLIGHT_BREATHING
328 # define BREATHING_NO_HALT 0
329 # define BREATHING_HALT_OFF 1
330 # define BREATHING_HALT_ON 2
331 # define BREATHING_STEPS 128
333 static uint8_t breathing_halt
= BREATHING_NO_HALT
;
334 static uint16_t breathing_counter
= 0;
336 # ifdef BACKLIGHT_PWM_TIMER
337 static bool breathing
= false;
339 bool is_breathing(void) { return breathing
; }
341 # define breathing_interrupt_enable() \
345 # define breathing_interrupt_disable() \
351 bool is_breathing(void) { return !!(TIMSKx
& _BV(TOIEx
)); }
353 # define breathing_interrupt_enable() \
355 TIMSKx |= _BV(TOIEx); \
357 # define breathing_interrupt_disable() \
359 TIMSKx &= ~_BV(TOIEx); \
363 # define breathing_min() \
365 breathing_counter = 0; \
367 # define breathing_max() \
369 breathing_counter = get_breathing_period() * 244 / 2; \
372 void breathing_enable(void) {
373 breathing_counter
= 0;
374 breathing_halt
= BREATHING_NO_HALT
;
375 breathing_interrupt_enable();
378 void breathing_pulse(void) {
379 if (get_backlight_level() == 0)
383 breathing_halt
= BREATHING_HALT_ON
;
384 breathing_interrupt_enable();
387 void breathing_disable(void) {
388 breathing_interrupt_disable();
389 // Restore backlight level
390 backlight_set(get_backlight_level());
393 void breathing_self_disable(void) {
394 if (get_backlight_level() == 0)
395 breathing_halt
= BREATHING_HALT_OFF
;
397 breathing_halt
= BREATHING_HALT_ON
;
400 void breathing_toggle(void) {
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)]
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};
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(); }
415 # ifdef BACKLIGHT_PWM_TIMER
416 void breathing_task(void)
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.
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
;
430 if (((breathing_halt
== BREATHING_HALT_ON
) && (index
== BREATHING_STEPS
/ 2)) || ((breathing_halt
== BREATHING_HALT_OFF
) && (index
== BREATHING_STEPS
- 1))) {
431 breathing_interrupt_disable();
434 set_pwm(cie_lightness(scale_backlight((uint16_t)pgm_read_byte(&breathing_table
[index
]) * 0x0101U
)));
437 #endif // BACKLIGHT_BREATHING
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
);)
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
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
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)."
464 # if BACKLIGHT_ON_STATE == 1
465 TCCRxA
= _BV(COMxx1
) | _BV(WGM11
);
467 TCCRxA
= _BV(COMxx1
) | _BV(COMxx0
) | _BV(WGM11
);
470 TCCRxB
= _BV(WGM13
) | _BV(WGM12
) | _BV(CS10
);
472 // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
476 #ifdef BACKLIGHT_BREATHING
477 if (is_backlight_breathing()) {