logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

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