aboutsummaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
authorMikkel Jeppesen <2756925+Duckle29@users.noreply.github.com>2021-11-17 21:56:13 +0100
committerGitHub <noreply@github.com>2021-11-18 07:56:13 +1100
commite0a5056963beddc3430dd3c8372858288dc85809 (patch)
tree184de7d758b28bf1c5267942e6dc088e72c09203 /quantum
parent121a2e0f07307fc1238d5a05dd9a706c258ccfbf (diff)
downloadqmk_firmware-e0a5056963beddc3430dd3c8372858288dc85809.tar.gz
qmk_firmware-e0a5056963beddc3430dd3c8372858288dc85809.zip
Made AVR backlight pwm resolution configurable (#7521)
* Made static backlight pwm resolution configurable * Made breathing backlighting configurable too * Finished my ifdef * Ran clang-format * Added missing semi-colon * Solved weird behaviour by right-shifting the right amount * Made breathing period scaled on actual pwm frequency * Made the low end deadzone scaled on the top value * Moved 'pwm_frequency' declaration outside ifdef * Fixed 'never used' error * Fixed 'never used' error * Fixed breathing ISR to 120Hz * Removed pwm_frequency constant Constant is no longer needed since running the breathing ISR at a fixed 120Hz * Re-add brightness limiting * re-introduce scaling
Diffstat (limited to 'quantum')
-rw-r--r--quantum/backlight/backlight_avr.c109
1 files changed, 68 insertions, 41 deletions
diff --git a/quantum/backlight/backlight_avr.c b/quantum/backlight/backlight_avr.c
index e47192de3..59050e250 100644
--- a/quantum/backlight/backlight_avr.c
+++ b/quantum/backlight/backlight_avr.c
@@ -199,13 +199,14 @@ static inline void disable_pwm(void) {
199// reaches the backlight level, where we turn off the LEDs, 199// reaches the backlight level, where we turn off the LEDs,
200// but also an overflow interrupt when the counter rolls back to 0, 200// but also an overflow interrupt when the counter rolls back to 0,
201// in which we're going to turn on the LEDs. 201// in which we're going to turn on the LEDs.
202// The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz. 202// The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz,
203// or F_CPU/BACKLIGHT_CUSTOM_RESOLUTION if used.
203 204
204// Triggered when the counter reaches the OCRx value 205// Triggered when the counter reaches the OCRx value
205ISR(TIMERx_COMPA_vect) { backlight_pins_off(); } 206ISR(TIMERx_COMPA_vect) { backlight_pins_off(); }
206 207
207// Triggered when the counter reaches the TOP value 208// Triggered when the counter reaches the TOP value
208// this one triggers at F_CPU/65536 =~ 244 Hz 209// this one triggers at F_CPU/ICRx = 16MHz/65536 =~ 244 Hz
209ISR(TIMERx_OVF_vect) { 210ISR(TIMERx_OVF_vect) {
210# ifdef BACKLIGHT_BREATHING 211# ifdef BACKLIGHT_BREATHING
211 if (is_breathing()) { 212 if (is_breathing()) {
@@ -220,8 +221,8 @@ ISR(TIMERx_OVF_vect) {
220 // artifacts (especially while breathing, because breathing_task 221 // artifacts (especially while breathing, because breathing_task
221 // takes many computation cycles). 222 // takes many computation cycles).
222 // so better not turn them on while the counter TOP is very low. 223 // so better not turn them on while the counter TOP is very low.
223 if (OCRxx > 256) { 224 if (OCRxx > ICRx / 250 + 5) {
224 backlight_pins_on(); 225 FOR_EACH_LED(backlight_on(backlight_pin);)
225 } 226 }
226} 227}
227 228
@@ -231,24 +232,26 @@ ISR(TIMERx_OVF_vect) {
231 232
232// See http://jared.geek.nz/2013/feb/linear-led-pwm 233// See http://jared.geek.nz/2013/feb/linear-led-pwm
233static uint16_t cie_lightness(uint16_t v) { 234static uint16_t cie_lightness(uint16_t v) {
234 if (v <= 5243) // if below 8% of max 235 if (v <= ICRx / 12) // If the value is less than or equal to ~8% of max
235 return v / 9; // same as dividing by 900% 236 {
236 else { 237 return v / 9; // Same as dividing by 900%
237 uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare 238 } else {
238 // to get a useful result with integer division, we shift left in the expression above 239 // In the next two lines values are bit-shifted. This is to avoid loosing decimals in integer math.
239 // and revert what we've done again after squaring. 240 uint32_t y = (((uint32_t)v + ICRx / 6) << 5) / (ICRx / 6 + ICRx); // If above 8%, add ~16% of max, and normalize with (max + ~16% max)
240 y = y * y * y >> 8; 241 uint32_t out = (y * y * y * ICRx) >> 15; // Cube it and undo the bit-shifting. (which is now three times as much due to the cubing)
241 if (y > 0xFFFFUL) // prevent overflow 242
242 return 0xFFFFU; 243 if (out > ICRx) // Avoid overflows
243 else 244 {
244 return (uint16_t)y; 245 out = ICRx;
246 }
247 return out;
245 } 248 }
246} 249}
247 250
248// rescale the supplied backlight value to be in terms of the value limit 251// rescale the supplied backlight value to be in terms of the value limit // range for val is [0..ICRx]. PWM pin is high while the timer count is below val.
249static uint32_t rescale_limit_val(uint32_t val) { return (val * (BACKLIGHT_LIMIT_VAL + 1)) / 256; } 252static uint32_t rescale_limit_val(uint32_t val) { return (val * (BACKLIGHT_LIMIT_VAL + 1)) / 256; }
250 253
251// range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val. 254// range for val is [0..ICRx]. PWM pin is high while the timer count is below val.
252static inline void set_pwm(uint16_t val) { OCRxx = val; } 255static inline void set_pwm(uint16_t val) { OCRxx = val; }
253 256
254void backlight_set(uint8_t level) { 257void backlight_set(uint8_t level) {
@@ -277,7 +280,7 @@ void backlight_set(uint8_t level) {
277#endif 280#endif
278 } 281 }
279 // Set the brightness 282 // Set the brightness
280 set_pwm(cie_lightness(rescale_limit_val(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS))); 283 set_pwm(cie_lightness(rescale_limit_val(ICRx * (uint32_t)level / BACKLIGHT_LEVELS)));
281} 284}
282 285
283void backlight_task(void) {} 286void backlight_task(void) {}
@@ -292,6 +295,11 @@ void backlight_task(void) {}
292static uint8_t breathing_halt = BREATHING_NO_HALT; 295static uint8_t breathing_halt = BREATHING_NO_HALT;
293static uint16_t breathing_counter = 0; 296static uint16_t breathing_counter = 0;
294 297
298static uint8_t breath_scale_counter = 1;
299/* Run the breathing loop at ~120Hz*/
300const uint8_t breathing_ISR_frequency = 120;
301static uint16_t breathing_freq_scale_factor = 2;
302
295# ifdef BACKLIGHT_PWM_TIMER 303# ifdef BACKLIGHT_PWM_TIMER
296static bool breathing = false; 304static bool breathing = false;
297 305
@@ -319,14 +327,14 @@ bool is_breathing(void) { return !!(TIMSKx & _BV(TOIEx)); }
319 } while (0) 327 } while (0)
320# endif 328# endif
321 329
322# define breathing_min() \ 330# define breathing_min() \
323 do { \ 331 do { \
324 breathing_counter = 0; \ 332 breathing_counter = 0; \
325 } while (0) 333 } while (0)
326# define breathing_max() \ 334# define breathing_max() \
327 do { \ 335 do { \
328 breathing_counter = get_breathing_period() * 244 / 2; \ 336 breathing_counter = breathing_period * breathing_ISR_frequency / 2; \
329 } while (0) 337 } while (0)
330 338
331void breathing_enable(void) { 339void breathing_enable(void) {
332 breathing_counter = 0; 340 breathing_counter = 0;
@@ -369,21 +377,33 @@ void breathing_task(void)
369# else 377# else
370/* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run 378/* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
371 * about 244 times per second. 379 * about 244 times per second.
380 *
381 * The following ISR runs at F_CPU/ISRx. With a 16MHz clock and default pwm resolution, that means 244Hz
372 */ 382 */
373ISR(TIMERx_OVF_vect) 383ISR(TIMERx_OVF_vect)
374# endif 384# endif
375{ 385{
376 uint8_t breathing_period = get_breathing_period(); 386
377 uint16_t interval = (uint16_t)breathing_period * 244 / BREATHING_STEPS; 387 // Only run this ISR at ~120 Hz
388 if(breath_scale_counter++ == breathing_freq_scale_factor)
389 {
390 breath_scale_counter = 1;
391 }
392 else
393 {
394 return;
395 }
396 uint16_t interval = (uint16_t)breathing_period * breathing_ISR_frequency / BREATHING_STEPS;
378 // resetting after one period to prevent ugly reset at overflow. 397 // resetting after one period to prevent ugly reset at overflow.
379 breathing_counter = (breathing_counter + 1) % (breathing_period * 244); 398 breathing_counter = (breathing_counter + 1) % (breathing_period * breathing_ISR_frequency);
380 uint8_t index = breathing_counter / interval % BREATHING_STEPS; 399 uint8_t index = breathing_counter / interval % BREATHING_STEPS;
381 400
382 if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) { 401 if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) {
383 breathing_interrupt_disable(); 402 breathing_interrupt_disable();
384 } 403 }
385 404
386 set_pwm(cie_lightness(rescale_limit_val(scale_backlight((uint16_t)pgm_read_byte(&breathing_table[index]) * 0x0101U)))); 405 // Set PWM to a brightnessvalue scaled to the configured resolution
406 set_pwm(cie_lightness(rescale_limit_val(scale_backlight((uint16_t)pgm_read_byte(&breathing_table[index]) * ICRx / 255))));
387} 407}
388 408
389#endif // BACKLIGHT_BREATHING 409#endif // BACKLIGHT_BREATHING
@@ -413,16 +433,23 @@ void backlight_init_ports(void) {
413 "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 [..]." 433 "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 [..]."
414 "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)." 434 "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)."
415 */ 435 */
416# if BACKLIGHT_ON_STATE == 1 436 TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
417 TCCRxA = _BV(COMxx1) | _BV(WGM11); 437 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
418# else 438# endif
419 TCCRxA = _BV(COMxx1) | _BV(COMxx0) | _BV(WGM11);
420# endif
421 439
422 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); 440# ifdef BACKLIGHT_CUSTOM_RESOLUTION
423#endif 441# if (BACKLIGHT_CUSTOM_RESOLUTION > 0xFFFF || BACKLIGHT_CUSTOM_RESOLUTION < 1)
424 // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0. 442# error "This out of range of the timer capabilities"
425 ICRx = TIMER_TOP; 443# elif (BACKLIGHT_CUSTOM_RESOLUTION < 0xFF)
444# warning "Resolution lower than 0xFF isn't recommended"
445# endif
446# ifdef BACKLIGHT_BREATHING
447 breathing_freq_scale_factor = F_CPU / BACKLIGHT_CUSTOM_RESOLUTION / 120;
448# endif
449 ICRx = BACKLIGHT_CUSTOM_RESOLUTION;
450# else
451 ICRx = TIMER_TOP;
452# endif
426 453
427 backlight_init(); 454 backlight_init();
428#ifdef BACKLIGHT_BREATHING 455#ifdef BACKLIGHT_BREATHING
@@ -430,4 +457,4 @@ void backlight_init_ports(void) {
430 breathing_enable(); 457 breathing_enable();
431 } 458 }
432#endif 459#endif
433} 460} \ No newline at end of file