backlight_timer.c (9341B)
1 #include "backlight.h" 2 #include "backlight_driver_common.h" 3 #include "progmem.h" 4 #include <avr/io.h> 5 #include <avr/interrupt.h> 6 7 // Maximum duty cycle limit 8 #ifndef BACKLIGHT_LIMIT_VAL 9 # define BACKLIGHT_LIMIT_VAL 255 10 #endif 11 12 #ifndef BACKLIGHT_PWM_TIMER 13 # define BACKLIGHT_PWM_TIMER 1 14 #endif 15 16 #if BACKLIGHT_PWM_TIMER == 1 17 # define ICRx ICR1 18 # define TCCRxA TCCR1A 19 # define TCCRxB TCCR1B 20 # define TIMERx_COMPA_vect TIMER1_COMPA_vect 21 # define TIMERx_OVF_vect TIMER1_OVF_vect 22 # if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register 23 # define TIMSKx TIMSK 24 # else 25 # define TIMSKx TIMSK1 26 # endif 27 # define TOIEx TOIE1 28 29 # define OCIExA OCIE1A 30 # define OCRxx OCR1A 31 #elif BACKLIGHT_PWM_TIMER == 3 32 # define ICRx ICR1 33 # define TCCRxA TCCR3A 34 # define TCCRxB TCCR3B 35 # define TIMERx_COMPA_vect TIMER3_COMPA_vect 36 # define TIMERx_OVF_vect TIMER3_OVF_vect 37 # define TIMSKx TIMSK3 38 # define TOIEx TOIE3 39 40 # define OCIExA OCIE3A 41 # define OCRxx OCR3A 42 #else 43 # error Invalid backlight PWM timer! 44 #endif 45 46 #ifndef BACKLIGHT_RESOLUTION 47 # define BACKLIGHT_RESOLUTION 0xFFFFU 48 #endif 49 50 #if (BACKLIGHT_RESOLUTION > 0xFFFF || BACKLIGHT_RESOLUTION < 0x00FF) 51 # error "Backlight resolution must be between 0x00FF and 0xFFFF" 52 #endif 53 54 #define BREATHING_SCALE_FACTOR F_CPU / BACKLIGHT_RESOLUTION / 120 55 56 // The idea of software PWM assisted by hardware timers is the following 57 // we use the hardware timer in fast PWM mode like for hardware PWM, but 58 // instead of letting the Output Match Comparator control the led pin 59 // (which is not possible since the backlight is not wired to PWM pins on the 60 // CPU), we do the LED on/off by oursleves. 61 // The timer is setup to count up to 0xFFFF, and we set the Output Compare 62 // register to the current 16bits backlight level (after CIE correction). 63 // This means the CPU will trigger a compare match interrupt when the counter 64 // reaches the backlight level, where we turn off the LEDs, 65 // but also an overflow interrupt when the counter rolls back to 0, 66 // in which we're going to turn on the LEDs. 67 // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz, 68 // or F_CPU/BACKLIGHT_RESOLUTION if used. 69 70 // Triggered when the counter reaches the OCRx value 71 ISR(TIMERx_COMPA_vect) { 72 backlight_pins_off(); 73 } 74 75 // Triggered when the counter reaches the TOP value 76 // this one triggers at F_CPU/ICRx = 16MHz/65536 =~ 244 Hz 77 ISR(TIMERx_OVF_vect) { 78 #ifdef BACKLIGHT_BREATHING 79 if (is_breathing()) { 80 breathing_task(); 81 } 82 #endif 83 // for very small values of OCRxx (or backlight level) 84 // we can't guarantee this whole code won't execute 85 // at the same time as the compare match interrupt 86 // which means that we might turn on the leds while 87 // trying to turn them off, leading to flickering 88 // artifacts (especially while breathing, because breathing_task 89 // takes many computation cycles). 90 // so better not turn them on while the counter TOP is very low. 91 if (OCRxx > ICRx / 250 + 5) { 92 backlight_pins_on(); 93 } 94 } 95 96 // See http://jared.geek.nz/2013/feb/linear-led-pwm 97 static uint16_t cie_lightness(uint16_t v) { 98 if (v <= (uint32_t)ICRx / 12) // If the value is less than or equal to ~8% of max 99 { 100 return v / 9; // Same as dividing by 900% 101 } else { 102 // In the next two lines values are bit-shifted. This is to avoid loosing decimals in integer math. 103 uint32_t y = (((uint32_t)v + (uint32_t)ICRx / 6) << 5) / ((uint32_t)ICRx / 6 + ICRx); // If above 8%, add ~16% of max, and normalize with (max + ~16% max) 104 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) 105 106 if (out > ICRx) // Avoid overflows 107 { 108 out = ICRx; 109 } 110 return (uint16_t)out; 111 } 112 } 113 114 // 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. 115 static uint32_t rescale_limit_val(uint32_t val) { 116 return (val * (BACKLIGHT_LIMIT_VAL + 1)) / 256; 117 } 118 119 // range for val is [0..ICRx]. PWM pin is high while the timer count is below val. 120 static inline void set_pwm(uint16_t val) { 121 OCRxx = val; 122 } 123 124 void backlight_set(uint8_t level) { 125 if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS; 126 127 if (level == 0) { 128 if (OCRxx) { 129 TIMSKx &= ~(_BV(OCIExA)); 130 TIMSKx &= ~(_BV(TOIEx)); 131 } 132 backlight_pins_off(); 133 } else { 134 if (!OCRxx) { 135 TIMSKx |= _BV(OCIExA); 136 TIMSKx |= _BV(TOIEx); 137 } 138 } 139 // Set the brightness 140 set_pwm(cie_lightness(rescale_limit_val(ICRx * (uint32_t)level / BACKLIGHT_LEVELS))); 141 } 142 143 void backlight_task(void) {} 144 145 #ifdef BACKLIGHT_BREATHING 146 # define BREATHING_NO_HALT 0 147 # define BREATHING_HALT_OFF 1 148 # define BREATHING_HALT_ON 2 149 # define BREATHING_STEPS 128 150 151 static uint8_t breathing_halt = BREATHING_NO_HALT; 152 static uint16_t breathing_counter = 0; 153 154 static uint8_t breath_scale_counter = 1; 155 /* Run the breathing loop at ~120Hz*/ 156 const uint8_t breathing_ISR_frequency = 120; 157 158 static bool breathing = false; 159 160 bool is_breathing(void) { 161 return breathing; 162 } 163 164 # define breathing_interrupt_enable() \ 165 do { \ 166 breathing = true; \ 167 } while (0) 168 # define breathing_interrupt_disable() \ 169 do { \ 170 breathing = false; \ 171 } while (0) 172 173 # define breathing_min() \ 174 do { \ 175 breathing_counter = 0; \ 176 } while (0) 177 # define breathing_max() \ 178 do { \ 179 breathing_counter = get_breathing_period() * breathing_ISR_frequency / 2; \ 180 } while (0) 181 182 void breathing_enable(void) { 183 breathing_counter = 0; 184 breathing_halt = BREATHING_NO_HALT; 185 breathing_interrupt_enable(); 186 } 187 188 void breathing_pulse(void) { 189 if (get_backlight_level() == 0) 190 breathing_min(); 191 else 192 breathing_max(); 193 breathing_halt = BREATHING_HALT_ON; 194 breathing_interrupt_enable(); 195 } 196 197 void breathing_disable(void) { 198 breathing_interrupt_disable(); 199 // Restore backlight level 200 backlight_set(get_backlight_level()); 201 } 202 203 void breathing_self_disable(void) { 204 if (get_backlight_level() == 0) 205 breathing_halt = BREATHING_HALT_OFF; 206 else 207 breathing_halt = BREATHING_HALT_ON; 208 } 209 210 /* To generate breathing curve in python: 211 * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)] 212 */ 213 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}; 214 215 // Use this before the cie_lightness function. 216 static inline uint16_t scale_backlight(uint16_t v) { 217 return v / BACKLIGHT_LEVELS * get_backlight_level(); 218 } 219 220 void breathing_task(void) { 221 // Only run this ISR at ~120 Hz 222 if (breath_scale_counter++ == BREATHING_SCALE_FACTOR) { 223 breath_scale_counter = 1; 224 } else { 225 return; 226 } 227 uint16_t interval = (uint16_t)get_breathing_period() * breathing_ISR_frequency / BREATHING_STEPS; 228 // resetting after one period to prevent ugly reset at overflow. 229 breathing_counter = (breathing_counter + 1) % (get_breathing_period() * breathing_ISR_frequency); 230 uint8_t index = breathing_counter / interval; 231 // limit index to max step value 232 if (index >= BREATHING_STEPS) { 233 index = BREATHING_STEPS - 1; 234 } 235 236 if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) { 237 breathing_interrupt_disable(); 238 } 239 240 // Set PWM to a brightnessvalue scaled to the configured resolution 241 set_pwm(cie_lightness(rescale_limit_val(scale_backlight((uint32_t)pgm_read_byte(&breathing_table[index]) * ICRx / 255)))); 242 } 243 244 #endif // BACKLIGHT_BREATHING 245 246 void backlight_init_ports(void) { 247 // Setup backlight pin as output and output to on state. 248 backlight_pins_init(); 249 250 // I could write a wall of text here to explain... but TL;DW 251 // Go read the ATmega32u4 datasheet. 252 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on 253 254 // TimerX setup, Fast PWM mode count to TOP set in ICRx 255 TCCRxA = _BV(WGM11); // = 0b00000010; 256 // clock select clk/1 257 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; 258 ICRx = BACKLIGHT_RESOLUTION; 259 260 backlight_init(); 261 262 #ifdef BACKLIGHT_BREATHING 263 if (is_backlight_breathing()) { 264 breathing_enable(); 265 } 266 #endif 267 }